Debian Codegraph @

internal/git/commit.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package git
2
3 import (
4 "regexp"
5 "time"
6
7 git "github.com/go-git/go-git/v5"
8 "github.com/go-git/go-git/v5/plumbing/object"
9 )
10
11 // Commit is the lightweight version of go-git's Reference struct. it holds
12 // hash of the commit, author's e-mail address, Message (subject and body
13 // combined) commit date and commit type whether it is local commit or a remote
14 type Commit struct {
15 Hash string
16 Author *Contributor
17 Commiter *Contributor
18 Message string
19 Time string
20 CommitType CommitType
21 C *object.Commit
22 }
23
24 // Contributor is the person
25 type Contributor struct {
26 Name string
27 Email string
28 When time.Time
29 }
30
31 // CommitType is the Type of the commit; it can be local or remote (upstream diff)
32 type CommitType string
33
34 const (
35 // LocalCommit is the commit that not pushed to remote branch
36 LocalCommit CommitType = "local"
37 // EvenCommit is the commit that recorded locally
38 EvenCommit CommitType = "even"
39 // RemoteCommit is the commit that not merged to local branch
40 RemoteCommit CommitType = "remote"
41 )
42
43 // loads the local commits by simply using git log way. ALso, gets the upstream
44 // diff commits
45 func (b *Branch) initCommits(r *Repository) error {
46 b.Commits = make([]*Commit, 0)
47 ref := b.Reference
48
49 // git log first
50 cIter, err := r.Repo.Log(&git.LogOptions{
51 From: ref.Hash(),
52 Order: git.LogOrderCommitterTime,
53 })
54 if err != nil {
55 return err
56 }
57 defer cIter.Close()
58 // find commits that fetched from upstream but not merged commits
59 rmcs, _ := b.pullDiffsToUpstream(r)
60 b.Commits = append(b.Commits, rmcs...)
61
62 // find commits that not pushed to upstream
63 lcs, _ := b.pushDiffsToUpstream(r)
64
65 // ... just iterates over the commits
66 err = cIter.ForEach(func(c *object.Commit) error {
67
68 cmType := EvenCommit
69 for _, lc := range lcs {
70 if lc.Hash == c.Hash.String() {
71 cmType = LocalCommit
72 }
73 }
74
75 commit := commit(c, cmType)
76 b.Commits = append(b.Commits, commit)
77 return nil
78 })
79 if err != nil {
80 return err
81 }
82 if b.State.Commit == nil {
83 b.State.Commit = b.Commits[0]
84 }
85 return nil
86 }
87
88 // this function creates the commit entities according to active branchs diffs
89 // to *its* configured upstream
90 func (b *Branch) pullDiffsToUpstream(r *Repository) ([]*Commit, error) {
91 remoteCommits := make([]*Commit, 0)
92 if r.State.Branch.Upstream == nil {
93 return remoteCommits, nil
94 }
95 head := b.Reference.Hash().String()
96
97 pullables, err := RevList(r, RevListOptions{
98 Ref1: head,
99 Ref2: b.Upstream.Reference.Hash().String(),
100 })
101 if err != nil {
102 // possibly found nothing or no upstream set
103 } else {
104 for _, c := range pullables {
105 commit := commit(c, RemoteCommit)
106 remoteCommits = append(remoteCommits, commit)
107 }
108 }
109 return remoteCommits, nil
110 }
111
112 // this function returns the hashes of the commits that are not pushed to the
113 // upstream of the specific branch
114 func (b *Branch) pushDiffsToUpstream(r *Repository) ([]*Commit, error) {
115 notPushedCommits := make([]*Commit, 0)
116 if r.State.Branch.Upstream == nil {
117 return notPushedCommits, nil
118 }
119 head := b.Reference.Hash().String()
120
121 pushables, err := RevList(r, RevListOptions{
122 Ref1: b.Upstream.Reference.Hash().String(),
123 Ref2: head,
124 })
125
126 if err != nil {
127 // possibly found nothing or no upstream set
128 } else {
129 for _, c := range pushables {
130 commit := commit(c, LocalCommit)
131 notPushedCommits = append(notPushedCommits, commit)
132 }
133 }
134 return notPushedCommits, nil
135 }
136
137 func commit(c *object.Commit, t CommitType) *Commit {
138 commit := &Commit{
139 Hash: c.Hash.String(),
140 Author: &Contributor{
141 Name: c.Author.Name,
142 Email: c.Author.Email,
143 When: c.Author.When,
144 },
145 Commiter: &Contributor{
146 Name: c.Committer.Name,
147 Email: c.Committer.Email,
148 When: c.Committer.When,
149 },
150 Message: c.Message,
151 CommitType: t,
152 C: c,
153 }
154 return commit
155 }
156
157 // DiffStat Show diff stat
158 func (c *Commit) DiffStat(done chan bool) string {
159
160 var str string
161 defer recoverDiff(&str)
162 if c.C == nil {
163 return ""
164 }
165 d, err := c.C.Stats()
166 if err != nil {
167 return ""
168 }
169 str = d.String()
170 done <- true
171 return str
172 }
173
174 func (c *Commit) String() string {
175 d := "Hash:" + " " + c.Hash
176 d = d + "\n" + "Author:" + " " + c.Author.Name + " <" + c.Author.Email + ">"
177 d = d + "\n" + "Date:" + " " + c.Author.When.String() + "\n"
178 re := regexp.MustCompile(`\r?\n`)
179 s := re.Split(c.Message, -1)
180 for _, l := range s {
181 d = d + "\n" + " " + l
182 }
183 return d
184 }
185
186 func recoverDiff(str *string) {
187 if r := recover(); r != nil {
188 *str = "diffstat overloaded"
189 }
190 }