Skip to content

Commit

Permalink
fixup! Create shims for all model classes in SessionStateLoader
Browse files Browse the repository at this point in the history
Move them to a separate file to make it easier for users to see what fields
exist.
  • Loading branch information
stefanhaller committed May 19, 2024
1 parent cb51472 commit 7ab7fae
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 95 deletions.
100 changes: 100 additions & 0 deletions pkg/gui/services/custom_commands/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package custom_commands

import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/stefanhaller/git-todo-parser/todo"
)

// We create shims for all the model classes in order to get a more stable API
// for custom commands. At the moment these are almost identical to the model
// classes, but this allows us to add "private" fields to the model classes that
// we don't want to expose to custom commands, or rename a model field to a
// better name without breaking people's custom commands. In such a case we add
// the new, better name to the shim but keep the old one for backwards
// compatibility. We already did this for Commit.Sha, which was renamed to Hash.

type CommitShim struct {
Hash string // deprecated: use Sha
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
}

type FileShim struct {
Name string
PreviousName string
HasStagedChanges bool
HasUnstagedChanges bool
Tracked bool
Added bool
Deleted bool
HasMergeConflicts bool
HasInlineMergeConflicts bool
DisplayString string
ShortStatus string
IsWorktree bool
}

type BranchShim struct {
Name string
DisplayName string
Recency string
Pushables string // deprecated: use AheadForPull
Pullables string // deprecated: use BehindForPull
AheadForPull string
BehindForPull string
AheadForPush string
BehindForPush string
UpstreamGone bool
Head bool
DetachedHead bool
UpstreamRemote string
UpstreamBranch string
Subject string
CommitHash string
}

type RemoteBranchShim struct {
Name string
RemoteName string
}

type RemoteShim struct {
Name string
Urls []string
Branches []*RemoteBranchShim
}

type TagShim struct {
Name string
Message string
}

type StashEntryShim struct {
Index int
Recency string
Name string
}

type CommitFileShim struct {
Name string
ChangeStatus string
}

type WorktreeShim struct {
IsMain bool
IsCurrent bool
Path string
IsPathMissing bool
GitDir string
Branch string
Name string
}
95 changes: 0 additions & 95 deletions pkg/gui/services/custom_commands/session_state_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/samber/lo"
"github.com/stefanhaller/git-todo-parser/todo"
)

// loads the session state at the time that a custom command is invoked, for use
Expand All @@ -21,29 +20,6 @@ func NewSessionStateLoader(c *helpers.HelperCommon, refsHelper *helpers.RefsHelp
}
}

// We create shims for all the model classes in order to get a more stable API
// for custom commands. At the moment these are almost identical to the model
// classes, but this allows us to add "private" fields to the model classes that
// we don't want to expose to custom commands, or rename a model field to a
// better name without breaking people's custom commands. In such a case we add
// the new, better name to the shim but keep the old one for backwards
// compatibility. We already did this for Commit.Sha, which was renamed to Hash.

type CommitShim struct {
Hash string // deprecated: use Sha
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 commitShimFromModelCommit(commit *models.Commit) *CommitShim {
if commit == nil {
return nil
Expand All @@ -65,21 +41,6 @@ func commitShimFromModelCommit(commit *models.Commit) *CommitShim {
}
}

type FileShim struct {
Name string
PreviousName string
HasStagedChanges bool
HasUnstagedChanges bool
Tracked bool
Added bool
Deleted bool
HasMergeConflicts bool
HasInlineMergeConflicts bool
DisplayString string
ShortStatus string
IsWorktree bool
}

func fileShimFromModelFile(file *models.File) *FileShim {
if file == nil {
return nil
Expand All @@ -101,25 +62,6 @@ func fileShimFromModelFile(file *models.File) *FileShim {
}
}

type BranchShim struct {
Name string
DisplayName string
Recency string
Pushables string // deprecated: use AheadForPull
Pullables string // deprecated: use BehindForPull
AheadForPull string
BehindForPull string
AheadForPush string
BehindForPush string
UpstreamGone bool
Head bool
DetachedHead bool
UpstreamRemote string
UpstreamBranch string
Subject string
CommitHash string
}

func branchShimFromModelBranch(branch *models.Branch) *BranchShim {
if branch == nil {
return nil
Expand All @@ -145,11 +87,6 @@ func branchShimFromModelBranch(branch *models.Branch) *BranchShim {
}
}

type RemoteBranchShim struct {
Name string
RemoteName string
}

func remoteBranchShimFromModelRemoteBranch(remoteBranch *models.RemoteBranch) *RemoteBranchShim {
if remoteBranch == nil {
return nil
Expand All @@ -161,12 +98,6 @@ func remoteBranchShimFromModelRemoteBranch(remoteBranch *models.RemoteBranch) *R
}
}

type RemoteShim struct {
Name string
Urls []string
Branches []*RemoteBranchShim
}

func remoteShimFromModelRemote(remote *models.Remote) *RemoteShim {
if remote == nil {
return nil
Expand All @@ -181,11 +112,6 @@ func remoteShimFromModelRemote(remote *models.Remote) *RemoteShim {
}
}

type TagShim struct {
Name string
Message string
}

func tagShimFromModelRemote(tag *models.Tag) *TagShim {
if tag == nil {
return nil
Expand All @@ -197,12 +123,6 @@ func tagShimFromModelRemote(tag *models.Tag) *TagShim {
}
}

type StashEntryShim struct {
Index int
Recency string
Name string
}

func stashEntryShimFromModelRemote(stashEntry *models.StashEntry) *StashEntryShim {
if stashEntry == nil {
return nil
Expand All @@ -215,11 +135,6 @@ func stashEntryShimFromModelRemote(stashEntry *models.StashEntry) *StashEntryShi
}
}

type CommitFileShim struct {
Name string
ChangeStatus string
}

func commitFileShimFromModelRemote(commitFile *models.CommitFile) *CommitFileShim {
if commitFile == nil {
return nil
Expand All @@ -231,16 +146,6 @@ func commitFileShimFromModelRemote(commitFile *models.CommitFile) *CommitFileShi
}
}

type WorktreeShim struct {
IsMain bool
IsCurrent bool
Path string
IsPathMissing bool
GitDir string
Branch string
Name string
}

func worktreeShimFromModelRemote(worktree *models.Worktree) *WorktreeShim {
if worktree == nil {
return nil
Expand Down

0 comments on commit 7ab7fae

Please sign in to comment.