From 9b05da41f6eeffd71d17529ff230da678afa25cd Mon Sep 17 00:00:00 2001 From: Martin Strobel Date: Wed, 1 Apr 2026 20:25:37 -0700 Subject: [PATCH 1/2] Add commit --dry-run flag This will eventually help with getting rid of the amount calculation that slows down the help text and can timeout on slow storage. --- cmd/commit.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/cmd/commit.go b/cmd/commit.go index 55c2de4..22f9159 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "io" - "os" "path/filepath" "strings" "time" @@ -32,6 +31,7 @@ import ( "github.com/spf13/cast" "github.com/spf13/cobra" + "github.com/marstr/baronial/internal/format" "github.com/marstr/baronial/internal/index" ) @@ -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" @@ -174,8 +181,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 +262,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...) + } + + err = format.PrettyPrintTransaction(ctx, cmd.OutOrStdout(), repo, commitTransactionFromFlags) + if err != nil { + logrus.Fatal(err) + } + + } else { + err = persist.Commit(ctx, repo, commitTransactionFromFlags, additionalParents...) + if err != nil { + logrus.Fatal(err) + } + } }, } @@ -272,6 +313,7 @@ func init() { commitCmd.Flags().StringP(amountFlag, amountShorthand, amountDefault, amountUsage) commitCmd.Flags().StringP(bankRecordIDFlag, bankRecordIDShorthand, bankRecordIDDefault, bankRecordIDUsage) commitCmd.Flags().BoolP(forceFlag, forceShorthand, forceDefault, forceUsage) + commitCmd.Flags().BoolP(dryrunFlag, dryrunShorthand, dryrunDefault, dryrunUsage) } func promptToContinue(ctx context.Context, message string, output io.Writer, input io.Reader) (bool, error) { From 30c31156c0a3346217acb98beff27a554f3bebd4 Mon Sep 17 00:00:00 2001 From: Martin Strobel Date: Wed, 1 Apr 2026 20:43:15 -0700 Subject: [PATCH 2/2] Move commit amount calculation out of args This was wreaking havoc when storage access was slow. In tandem with a new dry-run flag, people should have the same ability to demystify what is happening with amount calculation but only doing it if you actually need to, and not timing out unless to respect the global timeout flag. --- cmd/commit.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/cmd/commit.go b/cmd/commit.go index 22f9159..cf842a4 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -38,7 +38,7 @@ import ( const ( amountFlag = "amount" amountShorthand = "a" - amountDefault = "" + amountDefault = "" amountUsage = "The magnitude of the transaction that should be displayed in logs." ) @@ -128,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() @@ -153,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) @@ -388,9 +386,6 @@ func promptToContinue(ctx context.Context, message string, output io.Writer, inp } func calculateAmount(ctx context.Context, targetDir string) (envelopes.Balance, error) { - ctx, cancel := context.WithTimeout(ctx, 2*time.Second) - defer cancel() - targetDir, err := index.RootDirectory(targetDir) if err != nil { return envelopes.Balance{}, err