Skip to content

Commit 36f2189

Browse files
lafriksvdbt
authored andcommitted
Add checks for commits with missing author and time (go-gitea#2771)
* Add checks for commits with missing author and time * Fix validate commits with emails if it has no Author
1 parent 785a930 commit 36f2189

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

models/update.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os/exec"
1111
"strings"
12+
"time"
1213

1314
"code.gitea.io/git"
1415
"code.gitea.io/gitea/modules/cache"
@@ -119,11 +120,24 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string)
119120
if err != nil {
120121
return fmt.Errorf("Commit: %v", err)
121122
}
122-
tagCreatedUnix := commit.Author.When.Unix()
123123

124-
author, err := GetUserByEmail(commit.Author.Email)
125-
if err != nil && !IsErrUserNotExist(err) {
126-
return fmt.Errorf("GetUserByEmail: %v", err)
124+
sig := tag.Tagger
125+
if sig == nil {
126+
sig = commit.Author
127+
}
128+
if sig == nil {
129+
sig = commit.Committer
130+
}
131+
132+
var author *User
133+
var createdAt = time.Unix(1, 0)
134+
135+
if sig != nil {
136+
author, err = GetUserByEmail(sig.Email)
137+
if err != nil && !IsErrUserNotExist(err) {
138+
return fmt.Errorf("GetUserByEmail: %v", err)
139+
}
140+
createdAt = sig.When
127141
}
128142

129143
commitsCount, err := commit.CommitsCount()
@@ -144,7 +158,8 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string)
144158
IsDraft: false,
145159
IsPrerelease: false,
146160
IsTag: true,
147-
CreatedUnix: tagCreatedUnix,
161+
Created: createdAt,
162+
CreatedUnix: createdAt.Unix(),
148163
}
149164
if author != nil {
150165
rel.PublisherID = author.ID
@@ -155,7 +170,8 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string)
155170
}
156171
} else {
157172
rel.Sha1 = commit.ID.String()
158-
rel.CreatedUnix = tagCreatedUnix
173+
rel.Created = createdAt
174+
rel.CreatedUnix = createdAt.Unix()
159175
rel.NumCommits = commitsCount
160176
rel.IsDraft = false
161177
if rel.IsTag && author != nil {

models/user.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,9 @@ type UserCommit struct {
11931193

11941194
// ValidateCommitWithEmail check if author's e-mail of commit is corresponding to a user.
11951195
func ValidateCommitWithEmail(c *git.Commit) *User {
1196+
if c.Author == nil {
1197+
return nil
1198+
}
11961199
u, err := GetUserByEmail(c.Author.Email)
11971200
if err != nil {
11981201
return nil
@@ -1211,11 +1214,15 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
12111214
for e != nil {
12121215
c := e.Value.(*git.Commit)
12131216

1214-
if v, ok := emails[c.Author.Email]; !ok {
1215-
u, _ = GetUserByEmail(c.Author.Email)
1216-
emails[c.Author.Email] = u
1217+
if c.Author != nil {
1218+
if v, ok := emails[c.Author.Email]; !ok {
1219+
u, _ = GetUserByEmail(c.Author.Email)
1220+
emails[c.Author.Email] = u
1221+
} else {
1222+
u = v
1223+
}
12171224
} else {
1218-
u = v
1225+
u = nil
12191226
}
12201227

12211228
newCommits.PushBack(UserCommit{

templates/repo/view_list.tmpl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
<th class="four wide">
55
{{if .LatestCommitUser}}
66
<img class="ui avatar image img-12" src="{{.LatestCommitUser.RelAvatarLink}}" />
7-
{{if .LatestCommitUser.FullName}}
8-
<a href="{{AppSubUrl}}/{{.LatestCommitUser.Name}}"><strong>{{.LatestCommitUser.FullName}}</strong></a>
9-
{{else}}
10-
<a href="{{AppSubUrl}}/{{.LatestCommitUser.Name}}"><strong>{{.LatestCommit.Author.Name}}</strong></a>
11-
{{end}}
7+
{{if .LatestCommitUser.FullName}}
8+
<a href="{{AppSubUrl}}/{{.LatestCommitUser.Name}}"><strong>{{.LatestCommitUser.FullName}}</strong></a>
9+
{{else}}
10+
<a href="{{AppSubUrl}}/{{.LatestCommitUser.Name}}"><strong>{{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}}</strong></a>
11+
{{end}}
1212
{{else}}
13-
<img class="ui avatar image img-12" src="{{AvatarLink .LatestCommit.Author.Email}}" />
14-
<strong>{{.LatestCommit.Author.Name}}</strong>
13+
{{if .LatestCommit.Author}}
14+
<img class="ui avatar image img-12" src="{{AvatarLink .LatestCommit.Author.Email}}" />
15+
<strong>{{.LatestCommit.Author.Name}}</strong>
16+
{{end}}
1517
{{end}}
1618
<a rel="nofollow" class="ui sha label {{if .LatestCommit.Signature}} isSigned {{if .LatestCommitVerification.Verified }} isVerified {{end}}{{end}}" href="{{.RepoLink}}/commit/{{.LatestCommit.ID}}">
1719
{{ShortSha .LatestCommit.ID.String}}
@@ -30,7 +32,7 @@
3032
</th>
3133
<th class="nine wide">
3234
</th>
33-
<th class="three wide text grey right age">{{TimeSince .LatestCommit.Author.When $.Lang}}</th>
35+
<th class="three wide text grey right age">{{if .LatestCommit.Author}}{{TimeSince .LatestCommit.Author.When $.Lang}}{{end}}</th>
3436
</tr>
3537
</thead>
3638
<tbody>

0 commit comments

Comments
 (0)