@@ -52,42 +52,6 @@ func MoveTodoDown(fileName string, sha string, action todo.TodoCommand) error {
5252 return WriteRebaseTodoFile (fileName , rearrangedTodos )
5353}
5454
55- func moveTodoDown (todos []todo.Todo , sha string , action todo.TodoCommand ) ([]todo.Todo , error ) {
56- for i , t := range todos {
57- // Comparing just the sha is not enough; we need to compare both the
58- // action and the sha, as the sha could appear multiple times (e.g. in a
59- // pick and later in a merge)
60- if t .Command == action && equalShas (t .Commit , sha ) {
61- // The todos are ordered backwards compared to our model commits, so
62- // actually move the commit _up_ in the todos slice (i.e. towards 0)
63-
64- // Skip over any entries that we don't show in lazygit's view (e.g.
65- // label, reset, or comment lines).
66- j := i
67- for ; j > 0 && todos [j - 1 ].Commit == "" && todos [j - 1 ].Command != todo .UpdateRef ; j -= 1 {
68- }
69-
70- if j == 0 {
71- // We expect callers to guard against this
72- return []todo.Todo {}, fmt .Errorf ("Destination position for moving todo out of range" )
73- }
74-
75- // Need to allocate a new slice so that we don't overwrite the
76- // original one while we're picking things from it
77- rearrangedTodos := make ([]todo.Todo , 0 , len (todos ))
78- rearrangedTodos = append (rearrangedTodos , todos [0 :j - 1 ]... ) // everything up to the target position
79- rearrangedTodos = append (rearrangedTodos , todos [i ]) // the commit we are moving
80- rearrangedTodos = append (rearrangedTodos , todos [j - 1 :i ]... ) // the commits between src and dest that we skipped above
81- rearrangedTodos = append (rearrangedTodos , todos [i + 1 :]... ) // the remaining commits
82-
83- return rearrangedTodos , nil
84- }
85- }
86-
87- // Should never get here
88- return []todo.Todo {}, fmt .Errorf ("Todo %s not found in git-rebase-todo" , sha )
89- }
90-
9155func MoveTodoUp (fileName string , sha string , action todo.TodoCommand ) error {
9256 todos , err := ReadRebaseTodoFile (fileName )
9357 if err != nil {
@@ -100,7 +64,45 @@ func MoveTodoUp(fileName string, sha string, action todo.TodoCommand) error {
10064 return WriteRebaseTodoFile (fileName , rearrangedTodos )
10165}
10266
103- func moveTodoUp (todos []todo.Todo , sha string , action todo.TodoCommand ) ([]todo.Todo , error ) {
104- rearrangedTodos , err := moveTodoDown (lo .Reverse (todos ), sha , action )
67+ func moveTodoDown (todos []todo.Todo , sha string , action todo.TodoCommand ) ([]todo.Todo , error ) {
68+ rearrangedTodos , err := moveTodoUp (lo .Reverse (todos ), sha , action )
10569 return lo .Reverse (rearrangedTodos ), err
10670}
71+
72+ func moveTodoUp (todos []todo.Todo , sha string , action todo.TodoCommand ) ([]todo.Todo , error ) {
73+ _ , sourceIdx , ok := lo .FindIndexOf (todos , func (t todo.Todo ) bool {
74+ // Comparing just the sha is not enough; we need to compare both the
75+ // action and the sha, as the sha could appear multiple times (e.g. in a
76+ // pick and later in a merge)
77+ return t .Command == action && equalShas (t .Commit , sha )
78+ })
79+
80+ if ! ok {
81+ // Should never happen
82+ return []todo.Todo {}, fmt .Errorf ("Todo %s not found in git-rebase-todo" , sha )
83+ }
84+
85+ // The todos are ordered backwards compared to our model commits, so
86+ // actually move the commit _down_ in the todos slice (i.e. towards
87+ // the end of the slice)
88+
89+ // Find the next todo that we show in lazygit's commits view (skipping the rest)
90+ _ , skip , ok := lo .FindIndexOf (todos [sourceIdx + 1 :], isRenderedTodo )
91+
92+ if ! ok {
93+ // We expect callers to guard against this
94+ return []todo.Todo {}, fmt .Errorf ("Destination position for moving todo is out of range" )
95+ }
96+
97+ destinationIdx := sourceIdx + 1 + skip
98+
99+ rearrangedTodos := MoveElement (todos , sourceIdx , destinationIdx )
100+
101+ return rearrangedTodos , nil
102+ }
103+
104+ // We render a todo in the commits view if it's a commit or if it's an
105+ // update-ref. We don't render label, reset, or comment lines.
106+ func isRenderedTodo (t todo.Todo ) bool {
107+ return t .Commit != "" || t .Command == todo .UpdateRef
108+ }
0 commit comments