Skip to content

Commit 10fb8c4

Browse files
committed
feat: support git rebase statuses
1 parent 34501ac commit 10fb8c4

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

cmd/goprompt/cmdQuery.go

+34-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/signal"
88
"os/user"
9+
"path/filepath"
910
"strings"
1011
"syscall"
1112
"time"
@@ -74,6 +75,9 @@ const (
7475
_partVcsStgTop = "vcs_git_stg_top"
7576
_partVcsStgDirty = "vcs_git_stg_dirty"
7677

78+
_partVcsGitRebaseOp = "vcs_git_rebase_op"
79+
_partVcsGitRebaseLeft = "vcs_git_rebase_op_left"
80+
7781
_partVcsGitIdxTotal = "vcs_git_idx_total"
7882
_partVcsGitIdxIncluded = "vcs_git_idx_incl"
7983
_partVcsGitIdxExcluded = "vcs_git_idx_excl"
@@ -273,24 +277,43 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
273277
printPart(_partVcs, "git")
274278
} else {
275279
return nil
280+
276281
}
277282

283+
gitDir, _ := stringExec("git", "rev-parse", "--path-format=absolute", "--git-dir")
284+
278285
subTasks.Go(func(ctx context.Context) error {
279-
if branch, err := stringExec("git", "branch", "--show-current"); err == nil {
280-
branch = trim(branch)
281-
if len(branch) > 0 {
282-
printPart(_partVcsBranch, trim(branch))
283-
return nil
286+
287+
288+
289+
headRef := ""
290+
if cherryHeadB, _ := os.ReadFile(filepath.Join(gitDir, "CHERRY_PICK_HEAD")); len(cherryHeadB) > 0 {
291+
headRef = trim(string(cherryHeadB))
292+
printPart(_partVcsGitRebaseOp, "cherry")
293+
} else if mergeHeadB, _ := os.ReadFile(filepath.Join(gitDir, "MERGE_HEAD")); len(mergeHeadB) > 0 {
294+
headRef = trim(string(mergeHeadB))
295+
printPart(_partVcsGitRebaseOp, "merge")
296+
} else if rebaseHeadB, _ := os.ReadFile(filepath.Join(gitDir, "rebase-merge", "orig-head")); len(rebaseHeadB) > 0 {
297+
headRef = trim(string(rebaseHeadB))
298+
printPart(_partVcsGitRebaseOp, "rebase")
299+
300+
actionsLeftB, _ := os.ReadFile(filepath.Join(gitDir, "rebase-merge", "git-rebase-todo"))
301+
actionsLeft := trim(string(actionsLeftB))
302+
if len(actionsLeftB) == 0 {
303+
printPart(_partVcsGitRebaseLeft, 1)
304+
} else {
305+
printPart(_partVcsGitRebaseLeft, len(strings.Split(string(actionsLeft), "\n"))+1)
284306
}
285307
}
286308

287-
if branch, err := stringExec("git", "name-rev", "--name-only", "HEAD"); err == nil {
288-
branch = trim(branch)
289-
if len(branch) > 0 {
290-
printPart(_partVcsBranch, trim(branch))
291-
return nil
292-
}
309+
branch := ""
310+
311+
if len(headRef) != 0 {
312+
branch, _ = stringExec("git", "name-rev", "--name-only", headRef)
313+
} else {
314+
branch, _ = stringExec("git", "branch", "--show-current")
293315
}
316+
printPart(_partVcsBranch, branch)
294317

295318
return nil
296319
})

cmd/goprompt/cmdRender.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
125125
distanceMarks = fmt.Sprintf("[+%v:-%v]", distanceAhead, distanceBehind)
126126
}
127127

128+
rebaseOp := ""
129+
rebaseOpC := redC
130+
if len(p[_partVcsGitRebaseOp]) != 0 {
131+
rebaseOp = p[_partVcsGitRebaseOp]
132+
if p[_partVcsGitRebaseLeft] != "" {
133+
rebaseOp += fmt.Sprintf("(%v)", p[_partVcsGitRebaseLeft])
134+
}
135+
}
136+
128137
gitParts = append(gitParts, gitMarkC(gitMark))
129138
gitParts = append(gitParts, gitBranchC(gitBranch))
130139
if len(gitDirtyMarks) > 0 {
@@ -133,6 +142,9 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
133142
if len(distanceMarks) > 0 {
134143
gitParts = append(gitParts, distanceMarksC(distanceMarks))
135144
}
145+
if len(rebaseOp) > 0 {
146+
gitParts = append(gitParts, rebaseOpC(rebaseOp))
147+
}
136148

137149
partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(gitParts, ":")))
138150
}
@@ -152,23 +164,11 @@ func cmdRenderRun(_ *cobra.Command, _ []string) error {
152164
saplDirtyMarks = "&"
153165
}
154166

155-
// distanceMarks := ""
156-
// distanceMarksC := magentaC
157-
158-
// distanceAhead := strInt(p[_partVcsLogAhead])
159-
// distanceBehind := strInt(p[_partVcsLogBehind])
160-
// if distanceAhead > 0 || distanceBehind > 0 {
161-
// distanceMarks = fmt.Sprintf("[+%v:-%v]", distanceAhead, distanceBehind)
162-
// }
163-
164167
saplParts = append(saplParts, saplMarkC(saplMark))
165168
saplParts = append(saplParts, saplBookmarkC(saplBookmark))
166169
if len(saplDirtyMarks) > 0 {
167170
saplParts = append(saplParts, saplDirtyMarksC(saplDirtyMarks))
168171
}
169-
// if len(distanceMarks) > 0 {
170-
// saplParts = append(saplParts, distanceMarksC(distanceMarks))
171-
// }
172172

173173
partsTop = append(partsTop, fmt.Sprintf("{%v}", strings.Join(saplParts, ":")))
174174
}

0 commit comments

Comments
 (0)