-
Notifications
You must be signed in to change notification settings - Fork 0
Overhaul Commit Amount Calculation Timing and Exposure #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,7 +20,6 @@ import ( | |||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||
|
|
@@ -32,13 +31,14 @@ import ( | |||||||||||||||||||||||||||
| "github.com/spf13/cast" | ||||||||||||||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| "github.com/marstr/baronial/internal/format" | ||||||||||||||||||||||||||||
| "github.com/marstr/baronial/internal/index" | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||
| amountFlag = "amount" | ||||||||||||||||||||||||||||
| amountShorthand = "a" | ||||||||||||||||||||||||||||
| amountDefault = "<calculated>" | ||||||||||||||||||||||||||||
| amountDefault = "<gross change>" | ||||||||||||||||||||||||||||
| amountUsage = "The magnitude of the transaction that should be displayed in logs." | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -77,6 +77,13 @@ const ( | |||||||||||||||||||||||||||
| forceUsage = "Ignore warnings, commit the transaction anyway." | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||
| dryrunFlag = "dry-run" | ||||||||||||||||||||||||||||
| dryrunShorthand = "d" | ||||||||||||||||||||||||||||
| dryrunDefault = false | ||||||||||||||||||||||||||||
| dryrunUsage = "Generates and prints a commit without writing it or updating any references." | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||
| bankRecordIDFlag = "bank-record-id" | ||||||||||||||||||||||||||||
| bankRecordIDShorthand = "b" | ||||||||||||||||||||||||||||
|
|
@@ -121,16 +128,6 @@ var commitCmd = &cobra.Command{ | |||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||||||||||||||||||||||||||||
| defer cancel() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| var err error | ||||||||||||||||||||||||||||
| commitTransactionFromFlags.Amount, err = calculateAmount(ctx, ".") | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatalf("Failed to calculate the amount from %q because of the following error: %s", amountDefault, err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| commitTransactionFromFlags.EnteredTime = time.Now() | ||||||||||||||||||||||||||||
|
|
@@ -146,6 +143,14 @@ var commitCmd = &cobra.Command{ | |||||||||||||||||||||||||||
| logrus.Fatal(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if !cmd.Flags().Changed(amountFlag) { | ||||||||||||||||||||||||||||
| var err error | ||||||||||||||||||||||||||||
| commitTransactionFromFlags.Amount, err = calculateAmount(ctx, ".") | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatalf("Failed to calculate the amount from %q because of the following error: %s", amountDefault, err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| commitTransactionFromFlags.State, err = index.LoadState(ctx, targetDir) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatal(err) | ||||||||||||||||||||||||||||
|
|
@@ -174,8 +179,8 @@ var commitCmd = &cobra.Command{ | |||||||||||||||||||||||||||
| shouldContinue, err := promptToContinue( | ||||||||||||||||||||||||||||
| ctx, | ||||||||||||||||||||||||||||
| "proceed despite imbalance?", | ||||||||||||||||||||||||||||
| os.Stdout, | ||||||||||||||||||||||||||||
| os.Stdin) | ||||||||||||||||||||||||||||
| cmd.OutOrStdout(), | ||||||||||||||||||||||||||||
| cmd.InOrStdin()) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatal(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
@@ -255,10 +260,44 @@ var commitCmd = &cobra.Command{ | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| commitTransactionFromFlags.RecordID = envelopes.BankRecordID(rawRecordId) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| err = persist.Commit(ctx, repo, commitTransactionFromFlags, additionalParents...) | ||||||||||||||||||||||||||||
| var dryrun bool | ||||||||||||||||||||||||||||
| dryrun, err = cmd.Flags().GetBool(dryrunFlag) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatal(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if dryrun { | ||||||||||||||||||||||||||||
| var head persist.RefSpec | ||||||||||||||||||||||||||||
| head, err = repo.Current(ctx) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatal(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| var parent envelopes.ID | ||||||||||||||||||||||||||||
| if head != "" { | ||||||||||||||||||||||||||||
| parent, err = persist.Resolve(ctx, repo, head) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| logrus.Fatal(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if parent.Equal(envelopes.ID{}) { | ||||||||||||||||||||||||||||
| commitTransactionFromFlags.Parents = []envelopes.ID{} | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| commitTransactionFromFlags.Parents = append([]envelopes.ID{parent}, additionalParents...) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+284
to
+288
|
||||||||||||||||||||||||||||
| if parent.Equal(envelopes.ID{}) { | |
| commitTransactionFromFlags.Parents = []envelopes.ID{} | |
| } else { | |
| commitTransactionFromFlags.Parents = append([]envelopes.ID{parent}, additionalParents...) | |
| } | |
| var parents []envelopes.ID | |
| if parent.Equal(envelopes.ID{}) { | |
| // No current HEAD; include a zero ID placeholder as the first parent. | |
| parents = append([]envelopes.ID{envelopes.ID{}}, additionalParents...) | |
| } else { | |
| parents = append([]envelopes.ID{parent}, additionalParents...) | |
| } | |
| commitTransactionFromFlags.Parents = parents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
calculateAmountloads state from disk (index.LoadState) and opens/reads the repo, and then the Run path immediately loads state again (commitTransactionFromFlags.State = index.LoadState(...)) and later re-opens/reads the repo (especially in--dry-run). On slow storage this doubles the I/O cost the PR is trying to avoid. Consider refactoring so state is loaded once and reused for both amount calculation and the commit/dry-run logic (e.g., pass the already-loaded state into the amount calculation helper, and reuse the already-open repository).