From 101080ba74dda6ccaec7f6bc756c95aa6f53431a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 29 Jun 2023 18:42:17 +0800 Subject: [PATCH 1/2] Fix branch commit message too long problem --- models/git/branch.go | 6 ++++-- modules/git/commit.go | 6 +++++- modules/repository/branch.go | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index adf8b0a78d223..b8e20fbe18f53 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -157,6 +158,7 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e func AddBranches(ctx context.Context, branches []*Branch) error { for _, branch := range branches { + branch.CommitMessage = git.SummaryOfCommitMessage(branch.CommitMessage) if _, err := db.GetEngine(ctx).Insert(branch); err != nil { return err } @@ -209,7 +211,7 @@ func UpdateBranch(ctx context.Context, repoID int64, branchName, commitID, commi Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted, updated_unix"). Update(&Branch{ CommitID: commitID, - CommitMessage: commitMessage, + CommitMessage: git.SummaryOfCommitMessage(commitMessage), PusherID: pusherID, CommitTime: timeutil.TimeStamp(commitTime.Unix()), IsDeleted: false, @@ -225,7 +227,7 @@ func UpdateBranch(ctx context.Context, repoID int64, branchName, commitID, commi RepoID: repoID, Name: branchName, CommitID: commitID, - CommitMessage: commitMessage, + CommitMessage: git.SummaryOfCommitMessage(commitMessage), PusherID: pusherID, CommitTime: timeutil.TimeStamp(commitTime.Unix()), }) diff --git a/modules/git/commit.go b/modules/git/commit.go index ff654f394d221..b5aa0061e0de1 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -43,9 +43,13 @@ func (c *Commit) Message() string { return c.CommitMessage } +func SummaryOfCommitMessage(commitMessage string) string { + return strings.Split(strings.TrimSpace(commitMessage), "\n")[0] +} + // Summary returns first line of commit message. func (c *Commit) Summary() string { - return strings.Split(strings.TrimSpace(c.CommitMessage), "\n")[0] + return SummaryOfCommitMessage(c.CommitMessage) } // ParentID returns oid of n-th parent (0-based index). diff --git a/modules/repository/branch.go b/modules/repository/branch.go index 7fd29e3f7d027..801f860a56f63 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -114,6 +114,7 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, } for _, b := range toUpdate { + b.CommitMessage = git.SummaryOfCommitMessage(b.CommitMessage) if _, err := db.GetEngine(subCtx).ID(b.ID). Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted"). Update(b); err != nil { From 9793862a22882b8228ab9748a351efbb2a35cf74 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 30 Jun 2023 14:03:27 +0800 Subject: [PATCH 2/2] simple the code --- models/git/branch.go | 15 +++++++-------- models/git/branch_test.go | 11 ++++++++++- modules/git/commit.go | 6 +----- modules/repository/branch.go | 9 ++++----- services/repository/push.go | 2 +- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index b8e20fbe18f53..255b230b515cd 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -158,7 +158,6 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e func AddBranches(ctx context.Context, branches []*Branch) error { for _, branch := range branches { - branch.CommitMessage = git.SummaryOfCommitMessage(branch.CommitMessage) if _, err := db.GetEngine(ctx).Insert(branch); err != nil { return err } @@ -206,14 +205,14 @@ func DeleteBranches(ctx context.Context, repoID, doerID int64, branchIDs []int64 // UpdateBranch updates the branch information in the database. If the branch exist, it will update latest commit of this branch information // If it doest not exist, insert a new record into database -func UpdateBranch(ctx context.Context, repoID int64, branchName, commitID, commitMessage string, pusherID int64, commitTime time.Time) error { +func UpdateBranch(ctx context.Context, repoID, pusherID int64, branchName string, commit *git.Commit) error { cnt, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repoID, branchName). Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted, updated_unix"). Update(&Branch{ - CommitID: commitID, - CommitMessage: git.SummaryOfCommitMessage(commitMessage), + CommitID: commit.ID.String(), + CommitMessage: commit.Summary(), PusherID: pusherID, - CommitTime: timeutil.TimeStamp(commitTime.Unix()), + CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()), IsDeleted: false, }) if err != nil { @@ -226,10 +225,10 @@ func UpdateBranch(ctx context.Context, repoID int64, branchName, commitID, commi return db.Insert(ctx, &Branch{ RepoID: repoID, Name: branchName, - CommitID: commitID, - CommitMessage: git.SummaryOfCommitMessage(commitMessage), + CommitID: commit.ID.String(), + CommitMessage: commit.Summary(), PusherID: pusherID, - CommitTime: timeutil.TimeStamp(commitTime.Unix()), + CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()), }) } diff --git a/models/git/branch_test.go b/models/git/branch_test.go index bb63660d078a9..ba6902692792e 100644 --- a/models/git/branch_test.go +++ b/models/git/branch_test.go @@ -11,6 +11,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" @@ -28,7 +29,15 @@ func TestAddDeletedBranch(t *testing.T) { secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "branch2"}) assert.True(t, secondBranch.IsDeleted) - err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.Name, secondBranch.CommitID, secondBranch.CommitMessage, secondBranch.PusherID, secondBranch.CommitTime.AsLocalTime()) + commit := &git.Commit{ + ID: git.MustIDFromString(secondBranch.CommitID), + CommitMessage: secondBranch.CommitMessage, + Committer: &git.Signature{ + When: secondBranch.CommitTime.AsLocalTime(), + }, + } + + err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit) assert.NoError(t, err) } diff --git a/modules/git/commit.go b/modules/git/commit.go index b5aa0061e0de1..ff654f394d221 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -43,13 +43,9 @@ func (c *Commit) Message() string { return c.CommitMessage } -func SummaryOfCommitMessage(commitMessage string) string { - return strings.Split(strings.TrimSpace(commitMessage), "\n")[0] -} - // Summary returns first line of commit message. func (c *Commit) Summary() string { - return SummaryOfCommitMessage(c.CommitMessage) + return strings.Split(strings.TrimSpace(c.CommitMessage), "\n")[0] } // ParentID returns oid of n-th parent (0-based index). diff --git a/modules/repository/branch.go b/modules/repository/branch.go index 801f860a56f63..bffadd62f4d92 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -77,9 +77,9 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, RepoID: repo.ID, Name: branch, CommitID: commit.ID.String(), - CommitMessage: commit.CommitMessage, + CommitMessage: commit.Summary(), PusherID: doerID, - CommitTime: timeutil.TimeStamp(commit.Author.When.Unix()), + CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()), }) } else if commit.ID.String() != dbb.CommitID { toUpdate = append(toUpdate, &git_model.Branch{ @@ -87,9 +87,9 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, RepoID: repo.ID, Name: branch, CommitID: commit.ID.String(), - CommitMessage: commit.CommitMessage, + CommitMessage: commit.Summary(), PusherID: doerID, - CommitTime: timeutil.TimeStamp(commit.Author.When.Unix()), + CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()), }) } } @@ -114,7 +114,6 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, } for _, b := range toUpdate { - b.CommitMessage = git.SummaryOfCommitMessage(b.CommitMessage) if _, err := db.GetEngine(subCtx).ID(b.ID). Cols("commit_id, commit_message, pusher_id, commit_time, is_deleted"). Update(b); err != nil { diff --git a/services/repository/push.go b/services/repository/push.go index 7e7069f580b42..8e4bab156293c 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -259,7 +259,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { notification.NotifyPushCommits(ctx, pusher, repo, opts, commits) - if err = git_model.UpdateBranch(ctx, repo.ID, branch, newCommit.ID.String(), newCommit.CommitMessage, opts.PusherID, newCommit.Committer.When); err != nil { + if err = git_model.UpdateBranch(ctx, repo.ID, opts.PusherID, branch, newCommit); err != nil { return fmt.Errorf("git_model.UpdateBranch %s:%s failed: %v", repo.FullName(), branch, err) }