aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2018-12-22 17:13:44 +0100
committerStefan Majewsky <majewsky@gmx.net>2018-12-22 17:13:44 +0100
commitef84ca3e82f422118c69a0c562433c185e1dcb9b (patch)
treebeaaab3434175de20806b4ee19dc35d23eef759f /pkg
parente4d68911c533a750022ddfe06aff080ca6cf678b (diff)
downloadgofu-ef84ca3e82f422118c69a0c562433c185e1dcb9b.tar.gz
prettyprompt: ref file must be preferred over packed-refs
Diffstat (limited to 'pkg')
-rw-r--r--pkg/prompt/git.go41
1 files changed, 23 insertions, 18 deletions
diff --git a/pkg/prompt/git.go b/pkg/prompt/git.go
index 9ace960..8393c3e 100644
--- a/pkg/prompt/git.go
+++ b/pkg/prompt/git.go
@@ -93,28 +93,15 @@ func getRepoStatusField(repo *gitRepo) string {
refSpecDisplay := strings.TrimPrefix(refSpec, "refs/")
refSpecDisplay = strings.TrimPrefix(refSpecDisplay, "heads/")
- //attempt to read packed-refs
- bytes, err = ioutil.ReadFile(filepath.Join(repo.GitDir, "packed-refs"))
- if err == nil {
- for _, line := range strings.Split(string(bytes), "\n") {
- line = strings.TrimSpace(line)
- if line == "" || strings.HasPrefix(line, "#") {
- continue
- }
- fields := strings.Fields(line)
- if len(fields) == 2 && fields[1] == refSpec {
- return formatRepoStatusField(refSpecDisplay, fields[0])
- }
- }
- }
-
- //if reading packed-refs did not work or did not yield the commit ID, read
- //file corresponding to refspec to find commit ID
+ //read file corresponding to refspec to find commit ID
bytes, err = ioutil.ReadFile(filepath.Join(repo.GitDir, refSpec))
commitID := strings.TrimSpace(string(bytes))
if err != nil {
if os.IsNotExist(err) {
- commitID = withColor("37", "blank")
+ commitID = tryReadFromPackedRefs(repo, refSpec)
+ if commitID == "" {
+ commitID = withColor("37", "blank")
+ }
} else {
handleError(err)
commitID = withColor("1;41", "unknown")
@@ -124,6 +111,24 @@ func getRepoStatusField(repo *gitRepo) string {
return formatRepoStatusField(refSpecDisplay, commitID)
}
+func tryReadFromPackedRefs(repo *gitRepo, refSpec string) string {
+ bytes, err := ioutil.ReadFile(filepath.Join(repo.GitDir, "packed-refs"))
+ if err != nil {
+ return ""
+ }
+ for _, line := range strings.Split(string(bytes), "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+ fields := strings.Fields(line)
+ if len(fields) == 2 && fields[1] == refSpec {
+ return fields[0]
+ }
+ }
+ return ""
+}
+
func formatRepoStatusField(refSpec, commitID string) string {
//shorten plain commit IDs from 40 to 10 bytes
if len(commitID) == 40 && !strings.Contains(commitID, "\x1B") {