Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SelectedCommit and SelectedPath Generic for all views #3752

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions pkg/gui/services/custom_commands/session_state_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ type SessionState struct {
SelectedLocalCommit *Commit
SelectedReflogCommit *Commit
SelectedSubCommit *Commit
SelectedCommit *Commit
SelectedFile *File
SelectedPath string
SelectedLocalBranch *Branch
Expand All @@ -181,19 +182,35 @@ type SessionState struct {
}

func (self *SessionStateLoader) call() *SessionState {
commit := commitShimFromModelCommit(self.c.Contexts().LocalCommits.GetSelected())
path := self.c.Contexts().Files.GetSelectedPath()

if path == "" {
path = self.c.Contexts().CommitFiles.GetSelectedPath()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here; testing if path is empty doesn't make sense. We need to set SelectedPath to either Files.GetSelectedPath() or CommitFiles.GetSelectedPath(), depending on which view is focused.


if commit == nil {
commit = commitShimFromModelCommit(self.c.Contexts().ReflogCommits.GetSelected())

if commit == nil {
commit = commitShimFromModelCommit(self.c.Contexts().SubCommits.GetSelected())
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic doesn't make sense: we need to test which panel is focused, and set commit to LocalCommits.GetSelected() if it's the local commits panel, or to ReflogCommits.GetSelected() if it's the reflogs panel, or to SubCommits.GetSelected() if it's the subcommits panel. Or to nil if it's not any of these, although we might consider setting it to LocalCommits.GetSelected() in that case because that's probably a useful default.

I think you can use self.c.CurrentSideContext() to find out which panel is focused.


return &SessionState{
SelectedFile: fileShimFromModelFile(self.c.Contexts().Files.GetSelectedFile()),
SelectedPath: self.c.Contexts().Files.GetSelectedPath(),
SelectedLocalCommit: commitShimFromModelCommit(self.c.Contexts().LocalCommits.GetSelected()),
SelectedReflogCommit: commitShimFromModelCommit(self.c.Contexts().ReflogCommits.GetSelected()),
SelectedPath: path,
SelectedLocalCommit: commit,
SelectedReflogCommit: commit,
SelectedCommit: commit,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is backwards. We need to keep setting SelectedLocalCommit, SelectedReflogCommit, and SelectedSubCommit to what they were set to before, for backwards compatibility. Only SelectedCommit will point to one of them, depending on context; see above.

SelectedLocalBranch: branchShimFromModelBranch(self.c.Contexts().Branches.GetSelected()),
SelectedRemoteBranch: remoteBranchShimFromModelRemoteBranch(self.c.Contexts().RemoteBranches.GetSelected()),
SelectedRemote: remoteShimFromModelRemote(self.c.Contexts().Remotes.GetSelected()),
SelectedTag: tagShimFromModelRemote(self.c.Contexts().Tags.GetSelected()),
SelectedStashEntry: stashEntryShimFromModelRemote(self.c.Contexts().Stash.GetSelected()),
SelectedCommitFile: commitFileShimFromModelRemote(self.c.Contexts().CommitFiles.GetSelectedFile()),
SelectedCommitFilePath: self.c.Contexts().CommitFiles.GetSelectedPath(),
SelectedSubCommit: commitShimFromModelCommit(self.c.Contexts().SubCommits.GetSelected()),
SelectedCommitFilePath: path,
SelectedSubCommit: commit,
SelectedWorktree: worktreeShimFromModelRemote(self.c.Contexts().Worktrees.GetSelected()),
CheckedOutBranch: branchShimFromModelBranch(self.refsHelper.GetCheckedOutRef()),
}
Expand Down
37 changes: 28 additions & 9 deletions pkg/integration/tests/custom_commands/access_commit_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,38 @@ var AccessCommitProperties = NewIntegrationTest(NewIntegrationTestArgs{
{
Key: "X",
Context: "commits",
Command: "printf '%s\n%s\n%s' '{{ .SelectedLocalCommit.Name }}' '{{ .SelectedLocalCommit.Hash }}' '{{ .SelectedLocalCommit.Sha }}' > file.txt",
Command: "printf '%s\n%s\n%s' '{{ .SelectedCommit.Name }}' '{{ .SelectedLocalCommit.Hash }}' '{{ .SelectedLocalCommit.Sha }}' > file.txt",
},
{
Key: "X",
Context: "reflogCommits",
Command: "printf '%s\n%s\n%s' '{{ .SelectedCommit.Name }}' '{{ .SelectedReflogCommit.Hash }}' '{{ .SelectedReflogCommit.Sha }}' > file.txt",
},
}
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("my change").IsSelected(),
).
Press("X")
{
t.Views().Commits().
Focus().
Lines(
Contains("my change").IsSelected(),
).
Press("X")

hash := t.Git().GetCommitHash("HEAD")
t.FileSystem().FileContent("file.txt", Equals(fmt.Sprintf("my change\n%s\n%s", hash, hash)))
hash := t.Git().GetCommitHash("HEAD")
t.FileSystem().FileContent("file.txt", Equals(fmt.Sprintf("my change\n%s\n%s", hash, hash)))
}

{
t.Views().Commits().
Focus().
Lines(
Contains("my change").IsSelected(),
).
Press("X")

hash := t.Git().GetCommitHash("HEAD")
t.FileSystem().FileContent("file.txt", Equals(fmt.Sprintf("my change\n%s\n%s", hash, hash)))
}
},
})