diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-03-16 17:16:09 +0100 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-03-16 22:01:03 +0100 |
commit | 64a1a455d6648fc9b36a841dde1d88250b145b9b (patch) | |
tree | 089a34194073bcd8c2c69830172daa43e51be70f /pkg | |
parent | bd975a8dcb4205998e9c2594351c0977ad2b6775 (diff) |
Extract a findTodo helper function
We will reuse it in the next commit.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/utils/rebase_todo.go | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/pkg/utils/rebase_todo.go b/pkg/utils/rebase_todo.go index f8a4de998..abd15b9dc 100644 --- a/pkg/utils/rebase_todo.go +++ b/pkg/utils/rebase_todo.go @@ -56,6 +56,19 @@ func equalShas(a, b string) bool { return strings.HasPrefix(a, b) || strings.HasPrefix(b, a) } +func findTodo(todos []todo.Todo, todoToFind Todo) (int, bool) { + _, idx, ok := lo.FindIndexOf(todos, func(t todo.Todo) bool { + // Comparing just the sha is not enough; we need to compare both the + // action and the sha, as the sha could appear multiple times (e.g. in a + // pick and later in a merge). For update-ref todos we also must compare + // the Ref. + return t.Command == todoToFind.Action && + equalShas(t.Commit, todoToFind.Sha) && + t.Ref == todoToFind.Ref + }) + return idx, ok +} + func ReadRebaseTodoFile(fileName string, commentChar byte) ([]todo.Todo, error) { f, err := os.Open(fileName) if err != nil { @@ -128,15 +141,7 @@ func moveTodosDown(todos []todo.Todo, todosToMove []Todo) ([]todo.Todo, error) { } func moveTodoUp(todos []todo.Todo, todoToMove Todo) ([]todo.Todo, error) { - _, sourceIdx, ok := lo.FindIndexOf(todos, func(t todo.Todo) bool { - // Comparing just the sha is not enough; we need to compare both the - // action and the sha, as the sha could appear multiple times (e.g. in a - // pick and later in a merge). For update-ref todos we also must compare - // the Ref. - return t.Command == todoToMove.Action && - equalShas(t.Commit, todoToMove.Sha) && - t.Ref == todoToMove.Ref - }) + sourceIdx, ok := findTodo(todos, todoToMove) if !ok { // Should never happen |