Skip to content

Commit

Permalink
Merge pull request #2948 from ActiveState/mitchell/dx-136
Browse files Browse the repository at this point in the history
Added `state revert HEAD` to revert latest commit.
  • Loading branch information
mitchell-as authored Dec 15, 2023
2 parents 427de37 + 6199541 commit 3916611
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/state/internal/cmdtree/revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &params.CommitID,
},
Expand Down
23 changes: 16 additions & 7 deletions internal/runners/revert/revert.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -53,32 +55,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) && !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 strings.EqualFold(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 := ""
Expand All @@ -90,7 +99,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"),
)
}
Expand Down Expand Up @@ -121,7 +130,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"))
}
Expand All @@ -137,7 +146,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"`
}{
Expand Down
24 changes: 24 additions & 0 deletions test/integration/revert_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3916611

Please sign in to comment.