Pārlūkot izejas kodu

Fix commit and contributors counts

Aka: a short and painful reminder that the Git history is a graph.

Thankfully, libgit2 knows about walking the revision graph.
Lucas Stadler 8 gadi atpakaļ
vecāks
revīzija
dd6f872e17
1 mainītis faili ar 25 papildinājumiem un 14 dzēšanām
  1. 25 14
      quit.go

+ 25 - 14
quit.go

@ -68,24 +68,30 @@ func (fr *FancyRepo) Commit() (*FancyCommit, error) {
68 68
69 69
func (fr *FancyRepo) CommitCount() (int, error) {
70 70
	if fr.commitCount < 0 {
71
		walk, err := fr.repo.Walk()
72
		if err != nil {
73
			return -1, err
74
		}
75
71 76
		head, err := fr.repo.Head()
72 77
		if err != nil {
73 78
			return -1, err
74 79
		}
75 80
76
		commit, err := fr.repo.LookupCommit(head.Target())
81
		err = walk.Push(head.Target())
77 82
		if err != nil {
78 83
			return -1, err
79 84
		}
80 85
81
		c := 1
82
		for {
83
			commit = commit.Parent(0)
84
			if commit == nil {
85
				break
86
			}
86
		c := 0
87
		err = walk.Iterate(func(commit *git.Commit) bool {
87 88
			c += 1
89
			return true
90
		})
91
		if err != nil {
92
			return -1, err
88 93
		}
94
89 95
		fr.commitCount = c
90 96
	}
91 97
	return fr.commitCount, nil
@ -93,25 +99,30 @@ func (fr *FancyRepo) CommitCount() (int, error) {
93 99
94 100
func (fr *FancyRepo) Contributors() ([]*git.Signature, error) {
95 101
	if fr.contributors == nil {
102
		walk, err := fr.repo.Walk()
103
		if err != nil {
104
			return nil, err
105
		}
106
96 107
		head, err := fr.repo.Head()
97 108
		if err != nil {
98 109
			return nil, err
99 110
		}
100 111
101
		commit, err := fr.repo.LookupCommit(head.Target())
112
		err = walk.Push(head.Target())
102 113
		if err != nil {
103 114
			return nil, err
104 115
		}
105 116
106 117
		authors := make(map[string]*git.Signature)
107
		authors[commit.Author().Name] = commit.Author()
108
		for {
109
			commit = commit.Parent(0)
110
			if commit == nil {
111
				break
112
			}
118
		err = walk.Iterate(func(commit *git.Commit) bool {
113 119
			authors[commit.Author().Name] = commit.Author()
120
			return true
121
		})
122
		if err != nil {
123
			return nil, err
114 124
		}
125
115 126
		for _, author := range authors {
116 127
			fr.contributors = append(fr.contributors, author)
117 128
		}