Skip to content

Commit

Permalink
Basic Subtree support
Browse files Browse the repository at this point in the history
User can create Subtree from selected remote branch
  • Loading branch information
waelmahrous committed Nov 12, 2024
1 parent e1e4e1b commit 54abf46
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/commands/git_commands/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,12 @@ func (self *BranchCommands) IsBranchMerged(branch *models.Branch, mainBranches *

return stdout == "", nil
}

// Creates a Subtree from the selected branch
func (self *BranchCommands) CreateSubtree(remoteName string, branch string) error {
cmdArgs := NewGitCmd("subtree").
Arg("add", fmt.Sprintf("--prefix=%s", remoteName), remoteName, branch).
ToArgv()

return self.cmd.New(cmdArgs).Run()
}
2 changes: 2 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ type KeybindingBranchesConfig struct {
SetUpstream string `yaml:"setUpstream"`
FetchRemote string `yaml:"fetchRemote"`
SortOrder string `yaml:"sortOrder"`
CreateSubtree string `yaml:"createSubtree"`
}

type KeybindingWorktreesConfig struct {
Expand Down Expand Up @@ -904,6 +905,7 @@ func GetDefaultConfig() *UserConfig {
SetUpstream: "u",
FetchRemote: "f",
SortOrder: "s",
CreateSubtree: "S",
},
Worktrees: KeybindingWorktreesConfig{
ViewWorktreeOptions: "w",
Expand Down
25 changes: 25 additions & 0 deletions pkg/gui/controllers/remote_branches_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func (self *RemoteBranchesController) GetKeybindings(opts types.KeybindingsOpts)
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.OpenDiffTool,
},
{
Key: opts.GetKey(opts.Config.Branches.CreateSubtree),
Handler: self.withItem(self.createSubtree),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.Subtree,
Tooltip: self.c.Tr.SubtreeToolTip,
DisplayOnScreen: true,
},
}
}

Expand Down Expand Up @@ -198,3 +206,20 @@ func (self *RemoteBranchesController) newLocalBranch(selectedBranch *models.Remo
func (self *RemoteBranchesController) checkoutBranch(selectedBranch *models.RemoteBranch) error {
return self.c.Helpers().Refs.CheckoutRemoteBranch(selectedBranch.FullName(), selectedBranch.Name)
}

func (self *RemoteBranchesController) createSubtree(branch *models.RemoteBranch) error {
self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.Subtree,
Prompt: "Create subtree from" + " '" + branch.Name + "'?",
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.CreateSubtree)
if err := self.c.Git().Branch.CreateSubtree(branch.RemoteName, branch.Name); err != nil {
return err
}

return self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.BRANCHES, types.REMOTES}})
},
})

return nil
}
5 changes: 5 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ type TranslationSet struct {
CopyPullRequestURL string
NoBranchOnRemote string
Fetch string
Subtree string
FetchTooltip string
NoAutomaticGitFetchTitle string
NoAutomaticGitFetchBody string
Expand Down Expand Up @@ -515,6 +516,7 @@ type TranslationSet struct {
ForceTag string
ForceTagPrompt string
FetchRemoteTooltip string
SubtreeToolTip string
FetchingRemoteStatus string
CheckoutCommit string
CheckoutCommitTooltip string
Expand Down Expand Up @@ -918,6 +920,7 @@ type Actions struct {
SetBranchUpstream string
AddRemote string
RemoveRemote string
CreateSubtree string
UpdateRemote string
ApplyPatch string
Stash string
Expand Down Expand Up @@ -1232,6 +1235,7 @@ func EnglishTranslationSet() *TranslationSet {
CopyPullRequestURL: `Copy pull request URL to clipboard`,
NoBranchOnRemote: `This branch doesn't exist on remote. You need to push it to remote first.`,
Fetch: `Fetch`,
Subtree: "Subtree",
FetchTooltip: "Fetch changes from remote.",
NoAutomaticGitFetchTitle: `No automatic git fetch`,
NoAutomaticGitFetchBody: `Lazygit can't use "git fetch" in a private repo; use 'f' in the files panel to run "git fetch" manually`,
Expand Down Expand Up @@ -1508,6 +1512,7 @@ func EnglishTranslationSet() *TranslationSet {
ForceTag: "Force Tag",
ForceTagPrompt: "The tag '{{.tagName}}' exists already. Press {{.cancelKey}} to cancel, or {{.confirmKey}} to overwrite.",
FetchRemoteTooltip: "Fetch updates from the remote repository. This retrieves new commits and branches without merging them into your local branches.",
SubtreeToolTip: "Create a subtree from the selected branch.",
FetchingRemoteStatus: "Fetching remote",
CheckoutCommit: "Checkout commit",
CheckoutCommitTooltip: "Checkout the selected commit as a detached HEAD.",
Expand Down

0 comments on commit 54abf46

Please sign in to comment.