From 96d599b087f91005af2d0e38e0be28184373f990 Mon Sep 17 00:00:00 2001 From: Martin Strobel Date: Thu, 24 Jul 2025 22:43:43 -0500 Subject: [PATCH 1/2] Stow revert parameters between calls --- cmd/revert.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/cmd/revert.go b/cmd/revert.go index 51f4edf..cab2b7d 100644 --- a/cmd/revert.go +++ b/cmd/revert.go @@ -16,9 +16,12 @@ limitations under the License. package cmd import ( + "bytes" + "context" + "encoding/json" "fmt" "os" - "path" + "path/filepath" "github.com/marstr/baronial/internal/index" "github.com/marstr/envelopes" @@ -28,6 +31,11 @@ import ( "github.com/spf13/cobra" ) +type RevertParameters struct { + Comment string `json:"comment,omitempty"` + Reverts []envelopes.ID `json:"reverts"` +} + // revertCmd represents the revert command var revertCmd = &cobra.Command{ Use: "revert {ref-spec}", @@ -40,6 +48,7 @@ It is not advised to revert a revert. Even if this tool handles it well, it complicates all future tools and many may not do a good job. If you accidentally reverted a transaction, just commit a new transaction that is identical to the original.`, + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { ctx, cancel := RootContext(cmd) defer cancel() @@ -49,8 +58,9 @@ identical to the original.`, logrus.Fatal(err) } + repoLoc := filepath.Join(root, index.RepoName) var repo persist.RepositoryReaderWriter - repo, err = filesystem.OpenRepositoryWithCache(ctx, path.Join(root, index.RepoName), 10000) + repo, err = filesystem.OpenRepositoryWithCache(ctx, repoLoc, 10000) if err != nil { logrus.Fatal(err) } @@ -91,10 +101,90 @@ identical to the original.`, logrus.Fatal(err) } + var inProg bool + inProg, err = RevertIsInProgress(ctx, repoLoc) + if err != nil { + logrus.Warn("couldn't see if previous revert was in progress because: ", err) + } + + var revertParams RevertParameters + + if inProg { + err = RevertUnstowProgress(ctx, repoLoc, &revertParams) + if err != nil { + logrus.Fatal("couldn't read the currently in-progress revert because: ", err) + } + } + + revertParams.Reverts = append(revertParams.Reverts, id) + revertParams.Comment = getRevertComment(revertParams.Reverts) + + err = RevertStowProgress(ctx, repoLoc, revertParams) + if err != nil { + fmt.Fprintln(os.Stderr, "Unable to stow revert information. Please reset to the last known good state.") + logrus.Fatal(err) + } + fmt.Printf("Undid the effects of transaction %s. Please check current balances for accuracy, make any necessary edits, then commit.", id) }, } +func RevertIsInProgress(_ context.Context, repoLoc string) (bool, error) { + _, err := os.Stat(getRevertParamsLoc(repoLoc)) + if err == nil { + return true, nil + } else if os.IsNotExist(err) { + return false, nil + } else { + return false, err + } +} + +func RevertStowProgress(_ context.Context, repoLoc string, parameters RevertParameters) error { + const filePermissions = 0660 // owner and group can read and write. The file is not executable. Other users cannot read it. + toWrite, err := json.Marshal(parameters) + if err != nil { + return err + } + + return os.WriteFile(getRevertParamsLoc(repoLoc), toWrite, filePermissions) +} + +func RevertUnstowProgress(_ context.Context, repoLoc string, destination *RevertParameters) error { + contents, err := os.ReadFile(getRevertParamsLoc(repoLoc)) + if err != nil { + return err + } + + return json.Unmarshal(contents, destination) +} + +func RevertResetProgress(_ context.Context, repoLoc string) error { + return os.Remove(getRevertParamsLoc(repoLoc)) +} + +func getRevertParamsLoc(repoLoc string) string { + return filepath.Join(repoLoc, "revert.json") +} + +func getRevertComment(reverts []envelopes.ID) string { + var buf bytes.Buffer + + fmt.Fprint(&buf, "Reverts ") + + seenAny := false + for _, id := range reverts { + seenAny = true + fmt.Fprintf(&buf, "%s, ", id) + } + + if seenAny { + buf.Truncate(buf.Len() - 2) + } + + return buf.String() +} + func init() { rootCmd.AddCommand(revertCmd) From a259ac96c9b585bc9f32c7dda9942b6e6d0e5c8a Mon Sep 17 00:00:00 2001 From: Martin Strobel Date: Thu, 24 Jul 2025 22:56:27 -0500 Subject: [PATCH 2/2] Have commit honor pending reverts --- cmd/commit.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/commit.go b/cmd/commit.go index 1be252b..aed2866 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -186,8 +186,9 @@ var commitCmd = &cobra.Command{ } } + repoLoc := filepath.Join(targetDir, index.RepoName) var repo persist.RepositoryReaderWriter - repo, err = filesystem.OpenRepositoryWithCache(ctx, filepath.Join(targetDir, index.RepoName), 10000) + repo, err = filesystem.OpenRepositoryWithCache(ctx, repoLoc, 10000) if err != nil { logrus.Fatal(err) } @@ -202,6 +203,28 @@ var commitCmd = &cobra.Command{ logrus.Fatal(err) } + var pendingRevert bool + pendingRevert, err = RevertIsInProgress(ctx, repoLoc) + if err != nil { + logrus.Warn("unable to read if revert is staged, assuming not") + } + + if pendingRevert { + var revertParameters RevertParameters + err = RevertUnstowProgress(ctx, repoLoc, &revertParameters) + if err != nil { + logrus.Fatal("unable to read pending revert") + } + + commitTransactionFromFlags.Reverts = revertParameters.Reverts[0] + + if commitTransactionFromFlags.Comment == "" { + commitTransactionFromFlags.Comment = revertParameters.Comment + } + + defer RevertResetProgress(ctx, repoLoc) + } + var rawRecordId string rawRecordId, err = cmd.Flags().GetString(bankRecordIDFlag) if err != nil {