This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
git.go
134 lines (121 loc) · 3.35 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"net/url"
"os"
"regexp"
"strings"
. "github.com/reujab/bronze/types"
"gopkg.in/libgit2/git2go.v27"
)
// reformat scp-like url (e. g. ssh)
// https://github.com/motemen/ghq/blob/master/url.go
func fixUrl(url string) string {
hasSchemePattern := regexp.MustCompile("^[^:]+://")
scpLikeUrlPattern := regexp.MustCompile("^([^@]+@)?([^:]+):/?(.+)$")
if !hasSchemePattern.MatchString(url) && scpLikeUrlPattern.MatchString(url) {
matched := scpLikeUrlPattern.FindStringSubmatch(url)
user := matched[1]
host := matched[2]
path := matched[3]
return fmt.Sprintf("ssh://%s%s/%s", user, host, path)
}
return url
}
// the git segment provides useful information about a git repository such as the domain of the "origin" remote (with an icon), the current branch, and whether the HEAD is dirty
func gitSegment(segment *Segment) {
dir, err := os.Getwd()
die(err)
repo, err := git.OpenRepositoryExtended(dir, 0, "/")
if err != nil {
return
}
defer repo.Free()
var domainName string
remote, err := repo.Remotes.Lookup("origin")
if err == nil {
remoteUrl := fixUrl(remote.Url())
uri, err := url.Parse(remoteUrl)
if err == nil && len(uri.Hostname()) > 4 {
// strip the tld off the hostname
domainName = uri.Hostname()[:len(uri.Hostname())-4]
}
remote.Free()
}
var stashes int
repo.Stashes.Foreach(func(int, string, *git.Oid) error {
stashes++
return nil
})
var ahead, behind int
var branch string
head, err := repo.Head()
if err == nil {
upstream, err := head.Branch().Upstream()
if err == nil {
ahead, behind, err = repo.AheadBehind(head.Branch().Target(), upstream.Target())
die(err)
}
branch, err = head.Branch().Name()
if err != nil {
// head is detached
branch = head.Branch().Target().String()[:7]
}
head.Free()
}
var dirty, modified, staged bool
status, err := repo.StatusList(&git.StatusOptions{
Flags: git.StatusOptIncludeUntracked,
})
if err != nil {
// bare repository
return
}
count, err := status.EntryCount()
die(err)
if count != 0 {
dirty = true
}
for i := 0; i < count; i++ {
entry, err := status.ByIndex(i)
die(err)
if entry.Status&git.StatusWtNew != 0 || entry.Status&git.StatusWtModified != 0 || entry.Status&git.StatusWtDeleted != 0 || entry.Status&git.StatusWtTypeChange != 0 || entry.Status&git.StatusWtRenamed != 0 {
modified = true
}
if entry.Status&git.StatusIndexNew != 0 || entry.Status&git.StatusIndexModified != 0 || entry.Status&git.StatusIndexDeleted != 0 || entry.Status&git.StatusIndexRenamed != 0 || entry.Status&git.StatusIndexTypeChange != 0 {
staged = true
}
}
status.Free()
var segments []string
domainIcon := icons[domainName]
if domainIcon == "" {
domainIcon = icons["git"]
}
if domainIcon != "" {
segments = append(segments, domainIcon)
}
if stashes != 0 || ahead != 0 || behind != 0 {
section := strings.Repeat(icons["stash"], stashes) + strings.Repeat(icons["ahead"], ahead) + strings.Repeat(icons["behind"], behind)
if section != "" {
segments = append(segments, section)
}
}
if branch != "" {
segments = append(segments, branch)
}
if dirty {
segment.Background = "yellow"
var section string
if modified {
section += icons["modified"]
}
if staged {
section += icons["staged"]
}
if section != "" {
segments = append(segments, section)
}
}
segment.Value = strings.Join(segments, " ")
}