Skip to content

Commit 9902317

Browse files
lafrikslunny
authored andcommitted
Add checks for commits with missing author and time (#2771) (#2785)
* Add checks for commits with missing author and time * Fix validate commits with emails if it has no Author
1 parent 33e1641 commit 9902317

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

@@ -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
@@ -1205,6 +1205,9 @@ type UserCommit struct {
12051205

12061206
// ValidateCommitWithEmail check if author's e-mail of commit is corresponding to a user.
12071207
func ValidateCommitWithEmail(c *git.Commit) *User {
1208+
if c.Author == nil {
1209+
return nil
1210+
}
12081211
u, err := GetUserByEmail(c.Author.Email)
12091212
if err != nil {
12101213
return nil
@@ -1223,11 +1226,15 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
12231226
for e != nil {
12241227
c := e.Value.(*git.Commit)
12251228

1226-
if v, ok := emails[c.Author.Email]; !ok {
1227-
u, _ = GetUserByEmail(c.Author.Email)
1228-
emails[c.Author.Email] = u
1229+
if c.Author != nil {
1230+
if v, ok := emails[c.Author.Email]; !ok {
1231+
u, _ = GetUserByEmail(c.Author.Email)
1232+
emails[c.Author.Email] = u
1233+
} else {
1234+
u = v
1235+
}
12291236
} else {
1230-
u = v
1237+
u = nil
12311238
}
12321239

12331240
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}}
@@ -29,7 +31,7 @@
2931
</th>
3032
<th class="nine wide">
3133
</th>
32-
<th class="three wide text grey right age">{{TimeSince .LatestCommit.Author.When $.Lang}}</th>
34+
<th class="three wide text grey right age">{{if .LatestCommit.Author}}{{TimeSince .LatestCommit.Author.When $.Lang}}{{end}}</th>
3335
</tr>
3436
</thead>
3537
<tbody>

0 commit comments

Comments
 (0)