diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2023-04-04 10:23:50 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2023-04-15 08:36:03 +0200 |
commit | dc4e88f8a48bd52160a76b79da56e13af7b9ffc0 (patch) | |
tree | f8b070ae5d2e8e95cd3363f356d280b86808fefb /pkg/commands | |
parent | 120dd1530ae329199928c3494ea6063d741fc54d (diff) |
Make moving todo commits more robust
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git_commands/rebase.go | 31 |
1 files changed, 6 insertions, 25 deletions
diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 27b5a6ba4..e4c20426f 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -2,7 +2,6 @@ package git_commands import ( "fmt" - "os" "path/filepath" "strings" @@ -226,34 +225,16 @@ func (self *RebaseCommands) EditRebaseTodo(commit *models.Commit, action todo.To return fmt.Errorf("Todo %s not found in git-rebase-todo", commit.Sha) } -func (self *RebaseCommands) getTodoCommitCount(content []string) int { - // count lines that are not blank and are not comments - commitCount := 0 - for _, line := range content { - if line != "" && !strings.HasPrefix(line, "#") { - commitCount++ - } - } - return commitCount +// MoveTodoDown moves a rebase todo item down by one position +func (self *RebaseCommands) MoveTodoDown(commit *models.Commit) error { + fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo") + return utils.MoveTodoDown(fileName, commit.Sha, commit.Action) } // MoveTodoDown moves a rebase todo item down by one position -func (self *RebaseCommands) MoveTodoDown(index int) error { +func (self *RebaseCommands) MoveTodoUp(commit *models.Commit) error { fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo") - bytes, err := os.ReadFile(fileName) - if err != nil { - return err - } - - content := strings.Split(string(bytes), "\n") - commitCount := self.getTodoCommitCount(content) - contentIndex := commitCount - 1 - index - - rearrangedContent := append(content[0:contentIndex-1], content[contentIndex], content[contentIndex-1]) - rearrangedContent = append(rearrangedContent, content[contentIndex+1:]...) - result := strings.Join(rearrangedContent, "\n") - - return os.WriteFile(fileName, []byte(result), 0o644) + return utils.MoveTodoUp(fileName, commit.Sha, commit.Action) } // SquashAllAboveFixupCommits squashes all fixup! commits above the given one |