Skip to content
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

Added state revert HEAD to revert latest commit. #2948

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading