diff --git a/pkg/commands/git_commands/commit.go b/pkg/commands/git_commands/commit.go index b876cd02d..444ec17ac 100644 --- a/pkg/commands/git_commands/commit.go +++ b/pkg/commands/git_commands/commit.go @@ -28,6 +28,12 @@ func (self *CommitCommands) ResetAuthor() error { return self.cmd.New("git commit --allow-empty --only --no-edit --amend --reset-author").Run() } +// Sets the commit's author to the supplied value. Value is expected to be of the form 'Name ' +func (self *CommitCommands) SetAuthor(value string) error { + commandStr := fmt.Sprintf("git commit --allow-empty --only --no-edit --amend --author=%s", self.cmd.Quote(value)) + return self.cmd.New(commandStr).Run() +} + // ResetToCommit reset to commit func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error { return self.cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)). diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index d94060f2a..acf1c810b 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -63,9 +63,21 @@ func (self *RebaseCommands) RewordCommitInEditor(commits []*models.Commit, index } func (self *RebaseCommands) ResetCommitAuthor(commits []*models.Commit, index int) error { + return self.GenericAmend(commits, index, func() error { + return self.commit.ResetAuthor() + }) +} + +func (self *RebaseCommands) SetCommitAuthor(commits []*models.Commit, index int, value string) error { + return self.GenericAmend(commits, index, func() error { + return self.commit.SetAuthor(value) + }) +} + +func (self *RebaseCommands) GenericAmend(commits []*models.Commit, index int, f func() error) error { if index == 0 { // we've selected the top commit so no rebase is required - return self.commit.ResetAuthor() + return f() } err := self.BeginInteractiveRebaseForCommit(commits, index) @@ -73,8 +85,8 @@ func (self *RebaseCommands) ResetCommitAuthor(commits []*models.Commit, index in return err } - // now the selected commit should be our head so we'll amend it with the new author - err = self.commit.ResetAuthor() + // now the selected commit should be our head so we'll amend it + err = f() if err != nil { return err } diff --git a/pkg/gui/controllers/helpers/suggestions_helper.go b/pkg/gui/controllers/helpers/suggestions_helper.go index 52ccf9d96..0cc4a642b 100644 --- a/pkg/gui/controllers/helpers/suggestions_helper.go +++ b/pkg/gui/controllers/helpers/suggestions_helper.go @@ -10,6 +10,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/minimal/gitignore" + "github.com/samber/lo" "gopkg.in/ozeidan/fuzzy-patricia.v3/patricia" ) @@ -174,6 +175,14 @@ func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Su return FuzzySearchFunc(refNames) } +func (self *SuggestionsHelper) GetAuthorsSuggestionsFunc() func(string) []*types.Suggestion { + authors := lo.Uniq(slices.Map(self.model.Commits, func(commit *models.Commit) string { + return fmt.Sprintf("%s <%s>", commit.AuthorName, commit.AuthorEmail) + })) + + return FuzzySearchFunc(authors) +} + func FuzzySearchFunc(options []string) func(string) []*types.Suggestion { return func(input string) []*types.Suggestion { var matches []string diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 7d0f144cd..1d1420a97 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -123,7 +123,7 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ }, { Key: opts.GetKey(opts.Config.Commits.ResetCommitAuthor), - Handler: self.checkSelected(self.resetAuthor), + Handler: self.checkSelected(self.amendAttribute), Description: self.c.Tr.LcResetCommitAuthor, }, { @@ -423,17 +423,50 @@ func (self *LocalCommitsController) amendTo(commit *models.Commit) error { }) } -func (self *LocalCommitsController) resetAuthor(commit *models.Commit) error { - return self.c.Confirm(types.ConfirmOpts{ - Title: self.c.Tr.LcResetCommitAuthor, - Prompt: self.c.Tr.SureResetCommitAuthor, - HandleConfirm: func() error { - self.c.LogAction(self.c.Tr.Actions.ResetCommitAuthor) - if err := self.git.Rebase.ResetCommitAuthor(self.model.Commits, self.context().GetSelectedLineIdx()); err != nil { - return self.c.Error(err) - } +func (self *LocalCommitsController) amendAttribute(commit *models.Commit) error { + return self.c.Menu(types.CreateMenuOptions{ + Title: "Amend commit attribute", + Items: []*types.MenuItem{ + { + Label: "reset author", + OnPress: self.resetAuthor, + Key: 'a', + Tooltip: "Reset the commit's author to the currently configured user. This will also renew the author timestamp", + }, + { + Label: "set author", + OnPress: self.setAuthor, + Key: 'A', + Tooltip: "Set the author based on a prompt", + }, + }, + }) +} - return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) +func (self *LocalCommitsController) resetAuthor() error { + return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func() error { + self.c.LogAction(self.c.Tr.Actions.ResetCommitAuthor) + if err := self.git.Rebase.ResetCommitAuthor(self.model.Commits, self.context().GetSelectedLineIdx()); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) + }) +} + +func (self *LocalCommitsController) setAuthor() error { + return self.c.Prompt(types.PromptOpts{ + Title: self.c.Tr.SetAuthorPromptTitle, + FindSuggestionsFunc: self.helpers.Suggestions.GetAuthorsSuggestionsFunc(), + HandleConfirm: func(value string) error { + return self.c.WithWaitingStatus(self.c.Tr.AmendingStatus, func() error { + self.c.LogAction(self.c.Tr.Actions.SetCommitAuthor) + if err := self.git.Rebase.SetCommitAuthor(self.model.Commits, self.context().GetSelectedLineIdx(), value); err != nil { + return self.c.Error(err) + } + + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) + }) }, }) } diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 3eea542c6..975cc9285 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -101,6 +101,7 @@ type TranslationSet struct { LcEditCommit string LcAmendToCommit string LcResetCommitAuthor string + SetAuthorPromptTitle string SureResetCommitAuthor string LcRenameCommitEditor string NoCommitsThisBranch string @@ -537,6 +538,7 @@ type Actions struct { EditCommit string AmendCommit string ResetCommitAuthor string + SetCommitAuthor string RevertCommit string CreateFixupCommit string SquashAllAboveFixupCommits string @@ -725,6 +727,7 @@ func EnglishTranslationSet() TranslationSet { LcEditCommit: "edit commit", LcAmendToCommit: "amend commit with staged changes", LcResetCommitAuthor: "reset commit author", + SetAuthorPromptTitle: "Set author (must look like 'Name ')", SureResetCommitAuthor: "The author field of this commit will be updated to match the configured user. This also renews the author timestamp. Continue?", LcRenameCommitEditor: "reword commit with editor", Error: "Error", @@ -1142,6 +1145,7 @@ func EnglishTranslationSet() TranslationSet { EditCommit: "Edit commit", AmendCommit: "Amend commit", ResetCommitAuthor: "Reset commit author", + SetCommitAuthor: "Set commit author", RevertCommit: "Revert commit", CreateFixupCommit: "Create fixup commit", SquashAllAboveFixupCommits: "Squash all above fixup commits", diff --git a/test/integration/setAuthor/expected/repo/.git_keep/COMMIT_EDITMSG b/test/integration/setAuthor/expected/repo/.git_keep/COMMIT_EDITMSG new file mode 100644 index 000000000..c23aa0355 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/COMMIT_EDITMSG @@ -0,0 +1 @@ +myfile3 diff --git a/test/integration/setAuthor/expected/repo/.git_keep/FETCH_HEAD b/test/integration/setAuthor/expected/repo/.git_keep/FETCH_HEAD new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration/setAuthor/expected/repo/.git_keep/HEAD b/test/integration/setAuthor/expected/repo/.git_keep/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/integration/setAuthor/expected/repo/.git_keep/config b/test/integration/setAuthor/expected/repo/.git_keep/config new file mode 100644 index 000000000..85e571409 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/config @@ -0,0 +1,10 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true +[user] + email = Author2@example.com + name = Author2 diff --git a/test/integration/setAuthor/expected/repo/.git_keep/description b/test/integration/setAuthor/expected/repo/.git_keep/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/integration/setAuthor/expected/repo/.git_keep/index b/test/integration/setAuthor/expected/repo/.git_keep/index new file mode 100644 index 000000000..78afb6d69 Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/index differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/info/exclude b/test/integration/setAuthor/expected/repo/.git_keep/info/exclude new file mode 100644 index 000000000..8e9f2071f --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/info/exclude @@ -0,0 +1,7 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ +.DS_Store diff --git a/test/integration/setAuthor/expected/repo/.git_keep/logs/HEAD b/test/integration/setAuthor/expected/repo/.git_keep/logs/HEAD new file mode 100644 index 000000000..38d45fb5d --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/logs/HEAD @@ -0,0 +1,4 @@ +0000000000000000000000000000000000000000 2075aeb39a2a66a9607860a65b2a71c517760254 Author1 1652008089 +1000 commit (initial): myfile1 +2075aeb39a2a66a9607860a65b2a71c517760254 d01c8bb001458d0a7c01193813685c658e0355ac Author1 1652008089 +1000 commit: myfile2 +d01c8bb001458d0a7c01193813685c658e0355ac 8710ece70b7db9638b9645e93abdbcf210fa4595 Author2 1652008089 +1000 commit: myfile3 +8710ece70b7db9638b9645e93abdbcf210fa4595 baf3189129ba8878ba9b4107eaaaf3389287259b Author2 1652008097 +1000 commit (amend): myfile3 diff --git a/test/integration/setAuthor/expected/repo/.git_keep/logs/refs/heads/master b/test/integration/setAuthor/expected/repo/.git_keep/logs/refs/heads/master new file mode 100644 index 000000000..38d45fb5d --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/logs/refs/heads/master @@ -0,0 +1,4 @@ +0000000000000000000000000000000000000000 2075aeb39a2a66a9607860a65b2a71c517760254 Author1 1652008089 +1000 commit (initial): myfile1 +2075aeb39a2a66a9607860a65b2a71c517760254 d01c8bb001458d0a7c01193813685c658e0355ac Author1 1652008089 +1000 commit: myfile2 +d01c8bb001458d0a7c01193813685c658e0355ac 8710ece70b7db9638b9645e93abdbcf210fa4595 Author2 1652008089 +1000 commit: myfile3 +8710ece70b7db9638b9645e93abdbcf210fa4595 baf3189129ba8878ba9b4107eaaaf3389287259b Author2 1652008097 +1000 commit (amend): myfile3 diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/0e/6cf0a6b79e8d44e186d812a1f74b43d64fac52 b/test/integration/setAuthor/expected/repo/.git_keep/objects/0e/6cf0a6b79e8d44e186d812a1f74b43d64fac52 new file mode 100644 index 000000000..7f2ebf4ee Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/0e/6cf0a6b79e8d44e186d812a1f74b43d64fac52 differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/18/0cf8328022becee9aaa2577a8f84ea2b9f3827 b/test/integration/setAuthor/expected/repo/.git_keep/objects/18/0cf8328022becee9aaa2577a8f84ea2b9f3827 new file mode 100644 index 000000000..f74bf2335 Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/18/0cf8328022becee9aaa2577a8f84ea2b9f3827 differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/20/75aeb39a2a66a9607860a65b2a71c517760254 b/test/integration/setAuthor/expected/repo/.git_keep/objects/20/75aeb39a2a66a9607860a65b2a71c517760254 new file mode 100644 index 000000000..ce4b3233f --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/objects/20/75aeb39a2a66a9607860a65b2a71c517760254 @@ -0,0 +1,2 @@ +xQ +0D)_ݸn%M6Xhz{xfx0ob-em@"ČAq2MF*I#/|N9l\cSҵGT ~+nFȋ5' \ No newline at end of file diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/2b/173c861df433fa43ffad13f80c8b312c5c8bce b/test/integration/setAuthor/expected/repo/.git_keep/objects/2b/173c861df433fa43ffad13f80c8b312c5c8bce new file mode 100644 index 000000000..0a734f981 Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/2b/173c861df433fa43ffad13f80c8b312c5c8bce differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/87/10ece70b7db9638b9645e93abdbcf210fa4595 b/test/integration/setAuthor/expected/repo/.git_keep/objects/87/10ece70b7db9638b9645e93abdbcf210fa4595 new file mode 100644 index 000000000..573b0d49c Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/87/10ece70b7db9638b9645e93abdbcf210fa4595 differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/a5/bce3fd2565d8f458555a0c6f42d0504a848bd5 b/test/integration/setAuthor/expected/repo/.git_keep/objects/a5/bce3fd2565d8f458555a0c6f42d0504a848bd5 new file mode 100644 index 000000000..285df3e5f Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/a5/bce3fd2565d8f458555a0c6f42d0504a848bd5 differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/a7/341a59f0ddeef969e69fb6368266d22b0f2416 b/test/integration/setAuthor/expected/repo/.git_keep/objects/a7/341a59f0ddeef969e69fb6368266d22b0f2416 new file mode 100644 index 000000000..96d2e71a6 Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/a7/341a59f0ddeef969e69fb6368266d22b0f2416 differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/ba/f3189129ba8878ba9b4107eaaaf3389287259b b/test/integration/setAuthor/expected/repo/.git_keep/objects/ba/f3189129ba8878ba9b4107eaaaf3389287259b new file mode 100644 index 000000000..18a5ff1cd Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/ba/f3189129ba8878ba9b4107eaaaf3389287259b differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/d0/1c8bb001458d0a7c01193813685c658e0355ac b/test/integration/setAuthor/expected/repo/.git_keep/objects/d0/1c8bb001458d0a7c01193813685c658e0355ac new file mode 100644 index 000000000..e9d69bf0f Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/d0/1c8bb001458d0a7c01193813685c658e0355ac differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/objects/df/6b0d2bcc76e6ec0fca20c227104a4f28bac41b b/test/integration/setAuthor/expected/repo/.git_keep/objects/df/6b0d2bcc76e6ec0fca20c227104a4f28bac41b new file mode 100644 index 000000000..9b771fc2f Binary files /dev/null and b/test/integration/setAuthor/expected/repo/.git_keep/objects/df/6b0d2bcc76e6ec0fca20c227104a4f28bac41b differ diff --git a/test/integration/setAuthor/expected/repo/.git_keep/refs/heads/master b/test/integration/setAuthor/expected/repo/.git_keep/refs/heads/master new file mode 100644 index 000000000..2499d7343 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/.git_keep/refs/heads/master @@ -0,0 +1 @@ +baf3189129ba8878ba9b4107eaaaf3389287259b diff --git a/test/integration/setAuthor/expected/repo/myfile1 b/test/integration/setAuthor/expected/repo/myfile1 new file mode 100644 index 000000000..a5bce3fd2 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/myfile1 @@ -0,0 +1 @@ +test1 diff --git a/test/integration/setAuthor/expected/repo/myfile2 b/test/integration/setAuthor/expected/repo/myfile2 new file mode 100644 index 000000000..180cf8328 --- /dev/null +++ b/test/integration/setAuthor/expected/repo/myfile2 @@ -0,0 +1 @@ +test2 diff --git a/test/integration/setAuthor/expected/repo/myfile3 b/test/integration/setAuthor/expected/repo/myfile3 new file mode 100644 index 000000000..df6b0d2bc --- /dev/null +++ b/test/integration/setAuthor/expected/repo/myfile3 @@ -0,0 +1 @@ +test3 diff --git a/test/integration/setAuthor/recording.json b/test/integration/setAuthor/recording.json new file mode 100644 index 000000000..9084af754 --- /dev/null +++ b/test/integration/setAuthor/recording.json @@ -0,0 +1 @@ +{"KeyEvents":[{"Timestamp":1118,"Mod":0,"Key":259,"Ch":0},{"Timestamp":1382,"Mod":0,"Key":259,"Ch":0},{"Timestamp":2654,"Mod":0,"Key":256,"Ch":97},{"Timestamp":3632,"Mod":0,"Key":258,"Ch":0},{"Timestamp":4070,"Mod":0,"Key":13,"Ch":13},{"Timestamp":6702,"Mod":0,"Key":9,"Ch":9},{"Timestamp":7486,"Mod":0,"Key":258,"Ch":0},{"Timestamp":7899,"Mod":0,"Key":13,"Ch":13},{"Timestamp":9141,"Mod":0,"Key":256,"Ch":113}],"ResizeEvents":[{"Timestamp":0,"Width":272,"Height":74}]} \ No newline at end of file diff --git a/test/integration/setAuthor/setup.sh b/test/integration/setAuthor/setup.sh new file mode 100644 index 000000000..2eeb4d549 --- /dev/null +++ b/test/integration/setAuthor/setup.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +set -e + +cd $1 + +git init + +git config user.email "Author1@example.com" +git config user.name "Author1" + +echo test1 > myfile1 +git add . +git commit -am "myfile1" +echo test2 > myfile2 +git add . +git commit -am "myfile2" + +git config user.email "Author2@example.com" +git config user.name "Author2" + +echo test3 > myfile3 +git add . +git commit -am "myfile3" diff --git a/test/integration/setAuthor/test.json b/test/integration/setAuthor/test.json new file mode 100644 index 000000000..c8426b12c --- /dev/null +++ b/test/integration/setAuthor/test.json @@ -0,0 +1,4 @@ +{ + "description": "In this test the author of a commit is set to a different name/email.", + "speed": 5 +}