From 6a107e57f6ded230b9e9f6861ac2101a34f260b1 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Thu, 26 Oct 2017 10:45:14 +0300 Subject: [PATCH] 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 --- models/update.go | 28 ++++++++++++++++++++++------ models/user.go | 15 +++++++++++---- templates/repo/view_list.tmpl | 18 ++++++++++-------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/models/update.go b/models/update.go index 82369bf636..f3bd6cce13 100644 --- a/models/update.go +++ b/models/update.go @@ -9,6 +9,7 @@ import ( "fmt" "os/exec" "strings" + "time" "code.gitea.io/git" "code.gitea.io/gitea/modules/cache" @@ -119,11 +120,24 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) if err != nil { return fmt.Errorf("Commit: %v", err) } - tagCreatedUnix := commit.Author.When.Unix() - author, err := GetUserByEmail(commit.Author.Email) - if err != nil && !IsErrUserNotExist(err) { - return fmt.Errorf("GetUserByEmail: %v", err) + sig := tag.Tagger + if sig == nil { + sig = commit.Author + } + if sig == nil { + sig = commit.Committer + } + + var author *User + var createdAt = time.Unix(1, 0) + + if sig != nil { + author, err = GetUserByEmail(sig.Email) + if err != nil && !IsErrUserNotExist(err) { + return fmt.Errorf("GetUserByEmail: %v", err) + } + createdAt = sig.When } commitsCount, err := commit.CommitsCount() @@ -144,7 +158,8 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) IsDraft: false, IsPrerelease: false, IsTag: true, - CreatedUnix: tagCreatedUnix, + Created: createdAt, + CreatedUnix: createdAt.Unix(), } if author != nil { rel.PublisherID = author.ID @@ -155,7 +170,8 @@ func pushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) } } else { rel.Sha1 = commit.ID.String() - rel.CreatedUnix = tagCreatedUnix + rel.Created = createdAt + rel.CreatedUnix = createdAt.Unix() rel.NumCommits = commitsCount rel.IsDraft = false if rel.IsTag && author != nil { diff --git a/models/user.go b/models/user.go index 337c39efe4..4dd2ad8d89 100644 --- a/models/user.go +++ b/models/user.go @@ -1193,6 +1193,9 @@ type UserCommit struct { // ValidateCommitWithEmail check if author's e-mail of commit is corresponding to a user. func ValidateCommitWithEmail(c *git.Commit) *User { + if c.Author == nil { + return nil + } u, err := GetUserByEmail(c.Author.Email) if err != nil { return nil @@ -1211,11 +1214,15 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List { for e != nil { c := e.Value.(*git.Commit) - if v, ok := emails[c.Author.Email]; !ok { - u, _ = GetUserByEmail(c.Author.Email) - emails[c.Author.Email] = u + if c.Author != nil { + if v, ok := emails[c.Author.Email]; !ok { + u, _ = GetUserByEmail(c.Author.Email) + emails[c.Author.Email] = u + } else { + u = v + } } else { - u = v + u = nil } newCommits.PushBack(UserCommit{ diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 67164753e9..7be2183fa8 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -4,14 +4,16 @@ {{if .LatestCommitUser}} - {{if .LatestCommitUser.FullName}} - {{.LatestCommitUser.FullName}} - {{else}} - {{.LatestCommit.Author.Name}} - {{end}} + {{if .LatestCommitUser.FullName}} + {{.LatestCommitUser.FullName}} + {{else}} + {{if .LatestCommit.Author}}{{.LatestCommit.Author.Name}}{{else}}{{.LatestCommitUser.Name}}{{end}} + {{end}} {{else}} - - {{.LatestCommit.Author.Name}} + {{if .LatestCommit.Author}} + + {{.LatestCommit.Author.Name}} + {{end}} {{end}} {{ShortSha .LatestCommit.ID.String}} @@ -30,7 +32,7 @@ - {{TimeSince .LatestCommit.Author.When $.Lang}} + {{if .LatestCommit.Author}}{{TimeSince .LatestCommit.Author.When $.Lang}}{{end}}