Просмотр исходного кода

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
Родитель
Сommit
dd6f872e17
1 измененных файлов с 25 добавлено и 14 удалено
  1. 25 14
      quit.go

+ 25 - 14
quit.go

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