Skip to content

Commit a8eefbc

Browse files
committed
Disallow any changes to commits or todos when cherry-picking or reverting
We treat the .git/sequencer/todo file as read-only. Technically it seems it would be possible to treat it as modifiable in the same way as .git/rebase-merge/git-rebase-todo, effectively turning a cherry-pick or revert that stops at a conflict into an interactive rebase; however, git itself doesn't allow this (there is no "git cherry-pick --edit-todo"), so it seems safer not to rely on it. Theoretically it would be possible to allow modifying the rebase todos when a cherry-pick or revert conflicts in the middle of a rebase. However, it would introduce a bit of complexity to support this, as we would have to be able to distinguish between rebasing todos and cherry-picking/reverting todos, which we currently can't; it could also be a bit error-prone as far as edge cases are concerned. And it's really a pretty uncommon situation, so it doesn't seem worth it, and we just forbid all modifications to todos whenever we are cherry-picking or reverting.
1 parent 0924834 commit a8eefbc

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

pkg/gui/controllers/local_commits_controller.go

+21
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,11 @@ func (self *LocalCommitsController) isRebasing() bool {
684684
return self.c.Model().WorkingTreeStateAtLastCommitRefresh.IsRebasing()
685685
}
686686

687+
func (self *LocalCommitsController) isCherryPickingOrReverting() bool {
688+
return self.c.Model().WorkingTreeStateAtLastCommitRefresh != enums.WORKING_TREE_STATE_NONE &&
689+
self.c.Model().WorkingTreeStateAtLastCommitRefresh.Effective() != enums.WORKING_TREE_STATE_REBASING
690+
}
691+
687692
func (self *LocalCommitsController) moveDown(selectedCommits []*models.Commit, startIdx int, endIdx int) error {
688693
if self.isRebasing() {
689694
if err := self.c.Git().Rebase.MoveTodosDown(selectedCommits); err != nil {
@@ -1415,6 +1420,10 @@ func (self *LocalCommitsController) canMoveUp(selectedCommits []*models.Commit,
14151420

14161421
// Ensures that if we are mid-rebase, we're only selecting valid commits (non-conflict TODO commits)
14171422
func (self *LocalCommitsController) midRebaseCommandEnabled(selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason {
1423+
if self.isCherryPickingOrReverting() {
1424+
return &types.DisabledReason{Text: self.c.Tr.NotAllowedMidCherryPickOrRevert}
1425+
}
1426+
14181427
if !self.isRebasing() {
14191428
return nil
14201429
}
@@ -1434,6 +1443,10 @@ func (self *LocalCommitsController) midRebaseCommandEnabled(selectedCommits []*m
14341443

14351444
// Ensures that if we are mid-rebase, we're only selecting commits that can be moved
14361445
func (self *LocalCommitsController) midRebaseMoveCommandEnabled(selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason {
1446+
if self.isCherryPickingOrReverting() {
1447+
return &types.DisabledReason{Text: self.c.Tr.NotAllowedMidCherryPickOrRevert}
1448+
}
1449+
14371450
if !self.isRebasing() {
14381451
if lo.SomeBy(selectedCommits, func(c *models.Commit) bool { return c.IsMerge() }) {
14391452
return &types.DisabledReason{Text: self.c.Tr.CannotMoveMergeCommit}
@@ -1458,6 +1471,10 @@ func (self *LocalCommitsController) midRebaseMoveCommandEnabled(selectedCommits
14581471
}
14591472

14601473
func (self *LocalCommitsController) canDropCommits(selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason {
1474+
if self.isCherryPickingOrReverting() {
1475+
return &types.DisabledReason{Text: self.c.Tr.NotAllowedMidCherryPickOrRevert}
1476+
}
1477+
14611478
if !self.isRebasing() {
14621479
if len(selectedCommits) > 1 && lo.SomeBy(selectedCommits, func(c *models.Commit) bool { return c.IsMerge() }) {
14631480
return &types.DisabledReason{Text: self.c.Tr.DroppingMergeRequiresSingleSelection}
@@ -1502,6 +1519,10 @@ func isChangeOfRebaseTodoAllowed(oldAction todo.TodoCommand) bool {
15021519
}
15031520

15041521
func (self *LocalCommitsController) pickEnabled(selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason {
1522+
if self.isCherryPickingOrReverting() {
1523+
return &types.DisabledReason{Text: self.c.Tr.NotAllowedMidCherryPickOrRevert}
1524+
}
1525+
15051526
if !self.isRebasing() {
15061527
// if not rebasing, we're going to do a pull so we don't care about the selection
15071528
return nil

pkg/i18n/english.go

+2
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ type TranslationSet struct {
342342
YouDied string
343343
RewordNotSupported string
344344
ChangingThisActionIsNotAllowed string
345+
NotAllowedMidCherryPickOrRevert string
345346
DroppingMergeRequiresSingleSelection string
346347
CherryPickCopy string
347348
CherryPickCopyTooltip string
@@ -1396,6 +1397,7 @@ func EnglishTranslationSet() *TranslationSet {
13961397
YouDied: "YOU DIED!",
13971398
RewordNotSupported: "Rewording commits while interactively rebasing is not currently supported",
13981399
ChangingThisActionIsNotAllowed: "Changing this kind of rebase todo entry is not allowed",
1400+
NotAllowedMidCherryPickOrRevert: "This action is not allowed while cherry-picking or reverting",
13991401
DroppingMergeRequiresSingleSelection: "Dropping a merge commit requires a single selected item",
14001402
CherryPickCopy: "Copy (cherry-pick)",
14011403
CherryPickCopyTooltip: "Mark commit as copied. Then, within the local commits view, you can press `{{.paste}}` to paste (cherry-pick) the copied commit(s) into your checked out branch. At any time you can press `{{.escape}}` to cancel the selection.",

pkg/integration/tests/interactive_rebase/revert_single_commit_in_interactive_rebase.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var RevertSingleCommitInInteractiveRebase = NewIntegrationTest(NewIntegrationTes
4242
t.ExpectPopup().Menu().
4343
Title(Equals("Conflicts!")).
4444
Select(Contains("View conflicts")).
45-
Confirm()
45+
Cancel() // stay in commits panel
4646
}).
4747
Lines(
4848
Contains("CI unrelated change 2"),
@@ -51,12 +51,20 @@ var RevertSingleCommitInInteractiveRebase = NewIntegrationTest(NewIntegrationTes
5151
Contains("CI ◯ add second line"),
5252
Contains("CI ◯ add first line").IsSelected(),
5353
Contains("CI ◯ add empty file"),
54-
)
54+
).
55+
Press(keys.Commits.MoveDownCommit).
56+
Tap(func() {
57+
t.ExpectToast(Equals("Disabled: This action is not allowed while cherry-picking or reverting"))
58+
}).
59+
Press(keys.Universal.Remove).
60+
Tap(func() {
61+
t.ExpectToast(Equals("Disabled: This action is not allowed while cherry-picking or reverting"))
62+
})
5563

5664
t.Views().Options().Content(Contains("View revert options: m"))
5765
t.Views().Information().Content(Contains("Reverting (Reset)"))
5866

59-
t.Views().Files().IsFocused().
67+
t.Views().Files().Focus().
6068
Lines(
6169
Contains("UU myfile").IsSelected(),
6270
).

0 commit comments

Comments
 (0)