Browse Source

Add checks for commits with missing author and time (#2771)

* Add checks for commits with missing author and time

* Fix validate commits with emails if it has no Author
Lauris BH 2 years ago
parent
commit
6a107e57f6
3 changed files with 43 additions and 18 deletions
  1. 22 6
      models/update.go
  2. 11 4
      models/user.go
  3. 10 8
      templates/repo/view_list.tmpl

+ 22 - 6
models/update.go

@@ -9,6 +9,7 @@ import (
9 9
 	"fmt"
10 10
 	"os/exec"
11 11
 	"strings"
12
+	"time"
12 13
 
13 14
 	"code.gitea.io/git"
14 15
 	"code.gitea.io/gitea/modules/cache"
@@ -119,11 +120,24 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string)
119 120
 	if err != nil {
120 121
 		return fmt.Errorf("Commit: %v", err)
121 122
 	}
122
-	tagCreatedUnix := commit.Author.When.Unix()
123 123
 
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
127 141
 	}
128 142
 
129 143
 	commitsCount, err := commit.CommitsCount()
@@ -144,7 +158,8 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string)
144 158
 			IsDraft:      false,
145 159
 			IsPrerelease: false,
146 160
 			IsTag:        true,
147
-			CreatedUnix:  tagCreatedUnix,
161
+			Created:      createdAt,
162
+			CreatedUnix:  createdAt.Unix(),
148 163
 		}
149 164
 		if author != nil {
150 165
 			rel.PublisherID = author.ID
@@ -155,7 +170,8 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string)
155 170
 		}
156 171
 	} else {
157 172
 		rel.Sha1 = commit.ID.String()
158
-		rel.CreatedUnix = tagCreatedUnix
173
+		rel.Created = createdAt
174
+		rel.CreatedUnix = createdAt.Unix()
159 175
 		rel.NumCommits = commitsCount
160 176
 		rel.IsDraft = false
161 177
 		if rel.IsTag && author != nil {

+ 11 - 4
models/user.go

@@ -1193,6 +1193,9 @@ type UserCommit struct {
1193 1193
 
1194 1194
 // ValidateCommitWithEmail check if author's e-mail of commit is corresponding to a user.
1195 1195
 func ValidateCommitWithEmail(c *git.Commit) *User {
1196
+	if c.Author == nil {
1197
+		return nil
1198
+	}
1196 1199
 	u, err := GetUserByEmail(c.Author.Email)
1197 1200
 	if err != nil {
1198 1201
 		return nil
@@ -1211,11 +1214,15 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
1211 1214
 	for e != nil {
1212 1215
 		c := e.Value.(*git.Commit)
1213 1216
 
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
+			}
1217 1224
 		} else {
1218
-			u = v
1225
+			u = nil
1219 1226
 		}
1220 1227
 
1221 1228
 		newCommits.PushBack(UserCommit{

+ 10 - 8
templates/repo/view_list.tmpl

@@ -4,14 +4,16 @@
4 4
 			<th class="four wide">
5 5
 				{{if .LatestCommitUser}}
6 6
 					<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}}
12 12
 				{{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}}
15 17
 				{{end}}
16 18
 				<a rel="nofollow" class="ui sha label {{if .LatestCommit.Signature}} isSigned {{if .LatestCommitVerification.Verified }} isVerified {{end}}{{end}}" href="{{.RepoLink}}/commit/{{.LatestCommit.ID}}">
17 19
 						{{ShortSha .LatestCommit.ID.String}}
@@ -30,7 +32,7 @@
30 32
 			</th>
31 33
 			<th class="nine wide">
32 34
 			</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>
34 36
 		</tr>
35 37
 	</thead>
36 38
 	<tbody>