From ef84ca3e82f422118c69a0c562433c185e1dcb9b Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sat, 22 Dec 2018 17:13:44 +0100 Subject: prettyprompt: ref file must be preferred over packed-refs --- pkg/prompt/git.go | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'pkg/prompt/git.go') 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") { -- cgit v1.3.1