From 7e14d88dc94c868c0ac9743b688f6d9a3d6698d7 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 11 Apr 2024 18:39:21 +0200 Subject: [PATCH] Support both Sha and Hash on commits in custom commands We achieve this by wrapping the model Commit in a custom struct that provides both. --- .../custom_commands/session_state_loader.go | 49 ++++++++++++++++--- .../access_commit_properties.go | 4 +- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/pkg/gui/services/custom_commands/session_state_loader.go b/pkg/gui/services/custom_commands/session_state_loader.go index d5d34bfc9..93ce9d179 100644 --- a/pkg/gui/services/custom_commands/session_state_loader.go +++ b/pkg/gui/services/custom_commands/session_state_loader.go @@ -1,6 +1,7 @@ package custom_commands import ( + "github.com/fsmiamoto/git-todo-parser/todo" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" ) @@ -19,11 +20,47 @@ func NewSessionStateLoader(c *helpers.HelperCommon, refsHelper *helpers.RefsHelp } } +type Commit struct { + Hash string + Sha string + Name string + Status models.CommitStatus + Action todo.TodoCommand + Tags []string + ExtraInfo string + AuthorName string + AuthorEmail string + UnixTimestamp int64 + Divergence models.Divergence + Parents []string +} + +func commitWrapperFromModelCommit(commit *models.Commit) *Commit { + if commit == nil { + return nil + } + + return &Commit{ + Hash: commit.Hash, + Sha: commit.Hash, + Name: commit.Name, + Status: commit.Status, + Action: commit.Action, + Tags: commit.Tags, + ExtraInfo: commit.ExtraInfo, + AuthorName: commit.AuthorName, + AuthorEmail: commit.AuthorEmail, + UnixTimestamp: commit.UnixTimestamp, + Divergence: commit.Divergence, + Parents: commit.Parents, + } +} + // SessionState captures the current state of the application for use in custom commands type SessionState struct { - SelectedLocalCommit *models.Commit - SelectedReflogCommit *models.Commit - SelectedSubCommit *models.Commit + SelectedLocalCommit *Commit + SelectedReflogCommit *Commit + SelectedSubCommit *Commit SelectedFile *models.File SelectedPath string SelectedLocalBranch *models.Branch @@ -41,8 +78,8 @@ func (self *SessionStateLoader) call() *SessionState { return &SessionState{ SelectedFile: self.c.Contexts().Files.GetSelectedFile(), SelectedPath: self.c.Contexts().Files.GetSelectedPath(), - SelectedLocalCommit: self.c.Contexts().LocalCommits.GetSelected(), - SelectedReflogCommit: self.c.Contexts().ReflogCommits.GetSelected(), + SelectedLocalCommit: commitWrapperFromModelCommit(self.c.Contexts().LocalCommits.GetSelected()), + SelectedReflogCommit: commitWrapperFromModelCommit(self.c.Contexts().ReflogCommits.GetSelected()), SelectedLocalBranch: self.c.Contexts().Branches.GetSelected(), SelectedRemoteBranch: self.c.Contexts().RemoteBranches.GetSelected(), SelectedRemote: self.c.Contexts().Remotes.GetSelected(), @@ -50,7 +87,7 @@ func (self *SessionStateLoader) call() *SessionState { SelectedStashEntry: self.c.Contexts().Stash.GetSelected(), SelectedCommitFile: self.c.Contexts().CommitFiles.GetSelectedFile(), SelectedCommitFilePath: self.c.Contexts().CommitFiles.GetSelectedPath(), - SelectedSubCommit: self.c.Contexts().SubCommits.GetSelected(), + SelectedSubCommit: commitWrapperFromModelCommit(self.c.Contexts().SubCommits.GetSelected()), SelectedWorktree: self.c.Contexts().Worktrees.GetSelected(), CheckedOutBranch: self.refsHelper.GetCheckedOutRef(), } diff --git a/pkg/integration/tests/custom_commands/access_commit_properties.go b/pkg/integration/tests/custom_commands/access_commit_properties.go index fbb54bf81..6ac77faf8 100644 --- a/pkg/integration/tests/custom_commands/access_commit_properties.go +++ b/pkg/integration/tests/custom_commands/access_commit_properties.go @@ -19,7 +19,7 @@ var AccessCommitProperties = NewIntegrationTest(NewIntegrationTestArgs{ { Key: "X", Context: "commits", - Command: "printf '%s\n%s' '{{ .SelectedLocalCommit.Name }}' '{{ .SelectedLocalCommit.Hash }}' > file.txt", + Command: "printf '%s\n%s\n%s' '{{ .SelectedLocalCommit.Name }}' '{{ .SelectedLocalCommit.Hash }}' '{{ .SelectedLocalCommit.Sha }}' > file.txt", }, } }, @@ -32,6 +32,6 @@ var AccessCommitProperties = NewIntegrationTest(NewIntegrationTestArgs{ Press("X") hash := t.Git().GetCommitHash("HEAD") - t.FileSystem().FileContent("file.txt", Equals(fmt.Sprintf("my change\n%s", hash))) + t.FileSystem().FileContent("file.txt", Equals(fmt.Sprintf("my change\n%s\n%s", hash, hash))) }, })