From cc9fcfb77f095899f8b5eec07dbe2cb9f9493a8a Mon Sep 17 00:00:00 2001 From: mitchell Date: Thu, 14 Dec 2023 12:17:13 -0500 Subject: [PATCH 1/2] Added `state revert HEAD` to revert latest commit. --- cmd/state/internal/cmdtree/revert.go | 2 +- internal/runners/revert/revert.go | 21 ++++++++++++++------- test/integration/revert_int_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/cmd/state/internal/cmdtree/revert.go b/cmd/state/internal/cmdtree/revert.go index 896b0f75e8..8d01f8b34c 100644 --- a/cmd/state/internal/cmdtree/revert.go +++ b/cmd/state/internal/cmdtree/revert.go @@ -26,7 +26,7 @@ func newRevertCommand(prime *primer.Values, globals *globalOptions) *captain.Com []*captain.Argument{ { Name: "commit-id", - Description: locale.Tl("revert_arg_commit_id", "The commit ID to revert changes from"), + Description: locale.Tl("revert_arg_commit_id", "The commit ID to revert changes from, or HEAD for the latest commit"), Required: true, Value: ¶ms.CommitID, }, diff --git a/internal/runners/revert/revert.go b/internal/runners/revert/revert.go index 7968e9ce6e..01d8d91225 100644 --- a/internal/runners/revert/revert.go +++ b/internal/runners/revert/revert.go @@ -53,32 +53,39 @@ func New(prime primeable) *Revert { } } +const headCommitID = "HEAD" + func (r *Revert) Run(params *Params) (rerr error) { defer rationalizeError(&rerr) if r.project == nil { return locale.NewInputError("err_no_project") } - if !strfmt.IsUUID(params.CommitID) { + + commitID := params.CommitID + if !strfmt.IsUUID(commitID) && commitID != headCommitID { return locale.NewInputError("err_invalid_commit_id", "Invalid commit ID") } latestCommit, err := commitmediator.Get(r.project) if err != nil { return errs.Wrap(err, "Unable to get local commit") } + if commitID == headCommitID { + commitID = latestCommit.String() + } - if params.CommitID == latestCommit.String() && params.To { + if commitID == latestCommit.String() && params.To { return locale.NewInputError("err_revert_to_current_commit", "The commit to revert to cannot be the latest commit") } r.out.Notice(locale.Tr("operating_message", r.project.NamespaceString(), r.project.Dir())) bp := model.NewBuildPlannerModel(r.auth) - targetCommitID := params.CommitID // the commit to revert the contents of, or the commit to revert to + targetCommitID := commitID // the commit to revert the contents of, or the commit to revert to revertParams := revertParams{ organization: r.project.Owner(), project: r.project.Name(), parentCommitID: latestCommit.String(), - revertCommitID: params.CommitID, + revertCommitID: commitID, } revertFunc := r.revertCommit preposition := "" @@ -90,7 +97,7 @@ func (r *Revert) Run(params *Params) (rerr error) { targetCommit, err := model.GetCommitWithinCommitHistory(latestCommit, strfmt.UUID(targetCommitID)) if err != nil { return errs.AddTips( - locale.WrapError(err, "err_revert_get_commit", "", params.CommitID), + locale.WrapError(err, "err_revert_get_commit", "", commitID), locale.T("tip_private_project_auth"), ) } @@ -121,7 +128,7 @@ func (r *Revert) Run(params *Params) (rerr error) { revertCommit, err := revertFunc(revertParams, bp) if err != nil { return errs.AddTips( - locale.WrapError(err, "err_revert_commit", "", preposition, params.CommitID), + locale.WrapError(err, "err_revert_commit", "", preposition, commitID), locale.Tl("tip_revert_sync", "Please ensure that the local project is synchronized with the platform and that the given commit ID belongs to the current project"), locale.T("tip_private_project_auth")) } @@ -137,7 +144,7 @@ func (r *Revert) Run(params *Params) (rerr error) { } r.out.Print(output.Prepare( - locale.Tl("revert_success", "Successfully reverted{{.V0}} commit: {{.V1}}", preposition, params.CommitID), + locale.Tl("revert_success", "Successfully reverted{{.V0}} commit: {{.V1}}", preposition, commitID), &struct { CurrentCommitID string `json:"current_commit_id"` }{ diff --git a/test/integration/revert_int_test.go b/test/integration/revert_int_test.go index be86d8bd60..2caa330b59 100644 --- a/test/integration/revert_int_test.go +++ b/test/integration/revert_int_test.go @@ -66,6 +66,30 @@ func (suite *RevertIntegrationTestSuite) TestRevert() { cp.ExpectExitCode(0) } +func (suite *RevertIntegrationTestSuite) TestRevertHead() { + suite.OnlyRunForTags(tagsuite.Revert) + ts := e2e.New(suite.T(), false) + defer ts.Close() + + cp := ts.Spawn("checkout", "ActiveState-CLI/Revert", ".") + cp.Expect("Skipping runtime setup") + cp.Expect("Checked out project") + cp.ExpectExitCode(0) + + cp = ts.Spawn("install", "requests") + cp.Expect("Package added") + cp.ExpectExitCode(0) + + cp = ts.Spawn("revert", "HEAD", "--non-interactive") + cp.Expect("Successfully reverted") + cp.ExpectExitCode(0) + + cp = ts.Spawn("history") + cp.Expect("- requests") + cp.Expect("+ requests") + cp.ExpectExitCode(0) +} + func (suite *RevertIntegrationTestSuite) TestRevert_failsOnCommitNotInHistory() { suite.OnlyRunForTags(tagsuite.Revert) ts := e2e.New(suite.T(), false) From 6199541063d12e339797f373c2e103f7d71aa52d Mon Sep 17 00:00:00 2001 From: mitchell Date: Fri, 15 Dec 2023 13:13:30 -0500 Subject: [PATCH 2/2] Allow case-insensitive `state revert HEAD`. --- internal/runners/revert/revert.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/runners/revert/revert.go b/internal/runners/revert/revert.go index 01d8d91225..16a04b13f2 100644 --- a/internal/runners/revert/revert.go +++ b/internal/runners/revert/revert.go @@ -1,6 +1,8 @@ package revert import ( + "strings" + "github.com/ActiveState/cli/internal/analytics" "github.com/ActiveState/cli/internal/errs" "github.com/ActiveState/cli/internal/locale" @@ -63,14 +65,14 @@ func (r *Revert) Run(params *Params) (rerr error) { } commitID := params.CommitID - if !strfmt.IsUUID(commitID) && commitID != headCommitID { + if !strfmt.IsUUID(commitID) && !strings.EqualFold(commitID, headCommitID) { return locale.NewInputError("err_invalid_commit_id", "Invalid commit ID") } latestCommit, err := commitmediator.Get(r.project) if err != nil { return errs.Wrap(err, "Unable to get local commit") } - if commitID == headCommitID { + if strings.EqualFold(commitID, headCommitID) { commitID = latestCommit.String() }