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

Allow pasting commits onto branch from branches view #3288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ If you would instead like to start an interactive rebase from the selected commi
| `` g `` | Reset | |
| `` R `` | Rename branch | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | Paste (cherry-pick) | |
| `` <enter> `` | View commits | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand Down Expand Up @@ -313,6 +314,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Copy commit SHA to clipboard | |
| `` V `` | Paste (cherry-pick) | |
| `` <space> `` | Checkout | Checkout the selected commit as a detached HEAD. |
| `` y `` | Copy commit attribute to clipboard | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | Open commit in browser | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | コミットのSHAをクリップボードにコピー | |
| `` V `` | コミットを貼り付け (cherry-pick) | |
| `` <space> `` | チェックアウト | Checkout the selected commit as a detached HEAD. |
| `` y `` | コミットの情報をコピー | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | ブラウザでコミットを開く | |
Expand Down Expand Up @@ -239,6 +240,7 @@ If you would instead like to start an interactive rebase from the selected commi
| `` g `` | Reset | |
| `` R `` | ブランチ名を変更 | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | コミットを貼り付け (cherry-pick) | |
| `` <enter> `` | コミットを閲覧 | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | 커밋 SHA를 클립보드에 복사 | |
| `` V `` | 커밋을 붙여넣기 (cherry-pick) | |
| `` <space> `` | 체크아웃 | Checkout the selected commit as a detached HEAD. |
| `` y `` | 커밋 attribute 복사 | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | 브라우저에서 커밋 열기 | |
Expand Down Expand Up @@ -195,6 +196,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| `` g `` | View reset options | |
| `` R `` | 브랜치 이름 변경 | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | 커밋을 붙여넣기 (cherry-pick) | |
| `` <enter> `` | 커밋 보기 | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_nl.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| `` g `` | Bekijk reset opties | |
| `` R `` | Hernoem branch | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | Plak commits (cherry-pick) | |
| `` <enter> `` | Bekijk commits | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand Down Expand Up @@ -313,6 +314,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Kopieer commit SHA naar klembord | |
| `` V `` | Plak commits (cherry-pick) | |
| `` <space> `` | Uitchecken | Checkout the selected commit as a detached HEAD. |
| `` y `` | Copy commit attribute to clipboard | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | Open commit in browser | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_pl.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ If you would instead like to start an interactive rebase from the selected commi
| `` g `` | Wyświetl opcje resetu | |
| `` R `` | Rename branch | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | Wklej commity (przebieranie) | |
| `` <enter> `` | View commits | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand Down Expand Up @@ -306,6 +307,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Copy commit SHA to clipboard | |
| `` V `` | Wklej commity (przebieranie) | |
| `` <space> `` | Przełącz | Checkout the selected commit as a detached HEAD. |
| `` y `` | Copy commit attribute to clipboard | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | Open commit in browser | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ If you would instead like to start an interactive rebase from the selected commi
| `` g `` | Просмотреть параметры сброса | |
| `` R `` | Переименовать ветку | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | Вставить отобранные коммиты (cherry-pick) | |
| `` <enter> `` | Просмотреть коммиты | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand All @@ -219,6 +220,7 @@ If you would instead like to start an interactive rebase from the selected commi
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | Скопировать SHA коммита в буфер обмена | |
| `` V `` | Вставить отобранные коммиты (cherry-pick) | |
| `` <space> `` | Переключить | Checkout the selected commit as a detached HEAD. |
| `` y `` | Скопировать атрибут коммита | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | Открыть коммит в браузере | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| `` g `` | 查看重置选项 | |
| `` R `` | 重命名分支 | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | 粘贴提交(拣选) | |
| `` <enter> `` | 查看提交 | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand All @@ -107,6 +108,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | 将提交的 SHA 复制到剪贴板 | |
| `` V `` | 粘贴提交(拣选) | |
| `` <space> `` | 检出 | Checkout the selected commit as a detached HEAD. |
| `` y `` | Copy commit attribute to clipboard | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | 在浏览器中打开提交 | |
Expand Down
2 changes: 2 additions & 0 deletions docs/keybindings/Keybindings_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ _說明:`<c-b>` 表示 Ctrl+B、`<a-b>` 表示 Alt+B,`B`表示 Shift+B_
| Key | Action | Info |
|-----|--------|-------------|
| `` <c-o> `` | 複製提交 SHA 到剪貼簿 | |
| `` V `` | 貼上提交 (揀選) | |
| `` <space> `` | 檢出 | Checkout the selected commit as a detached HEAD. |
| `` y `` | 複製提交屬性 | Copy commit attribute to clipboard (e.g. hash, URL, diff, message, author). |
| `` o `` | 在瀏覽器中開啟提交 | |
Expand Down Expand Up @@ -270,6 +271,7 @@ If you would instead like to start an interactive rebase from the selected commi
| `` g `` | 檢視重設選項 | |
| `` R `` | 重新命名分支 | |
| `` u `` | View upstream options | View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream. |
| `` V `` | 貼上提交 (揀選) | |
| `` <enter> `` | 檢視提交 | |
| `` w `` | View worktree options | |
| `` / `` | Filter the current view by text | |
Expand Down
5 changes: 4 additions & 1 deletion pkg/gui/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ func (gui *Gui) resetHelpersAndControllers() {
searchHelper,
)
diffHelper := helpers.NewDiffHelper(helperCommon)
workingTreeHelper := helpers.NewWorkingTreeHelper(helperCommon, refsHelper, commitsHelper, gpgHelper)
cherryPickHelper := helpers.NewCherryPickHelper(
helperCommon,
rebaseHelper,
refsHelper,
workingTreeHelper,
)
bisectHelper := helpers.NewBisectHelper(helperCommon)
windowHelper := helpers.NewWindowHelper(helperCommon, viewHelper)
Expand Down Expand Up @@ -97,7 +100,7 @@ func (gui *Gui) resetHelpersAndControllers() {
Bisect: bisectHelper,
Suggestions: suggestionsHelper,
Files: helpers.NewFilesHelper(helperCommon),
WorkingTree: helpers.NewWorkingTreeHelper(helperCommon, refsHelper, commitsHelper, gpgHelper),
WorkingTree: workingTreeHelper,
Tags: helpers.NewTagsHelper(helperCommon, commitsHelper),
BranchesHelper: helpers.NewBranchesHelper(helperCommon),
GPG: helpers.NewGpgHelper(helperCommon),
Expand Down
25 changes: 25 additions & 0 deletions pkg/gui/controllers/branches_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
Expand Down Expand Up @@ -159,7 +160,31 @@ func (self *BranchesController) GetKeybindings(opts types.KeybindingsOpts) []*ty
OpensMenu: true,
DisplayOnScreen: true,
},
{
Key: opts.GetKey(opts.Config.Commits.PasteCommits),
Handler: self.checkoutAndPaste,
GetDisabledReason: self.require(self.canPaste),
Description: self.c.Tr.PasteCommits,
DisplayStyle: &style.FgCyan,
},
}
}

func (self *BranchesController) checkoutAndPaste() error {
branch := self.context().GetSelected()
if branch == nil {
return nil
}

return self.c.Helpers().CherryPick.CheckoutAndPaste(branch.Name)
}

func (self *BranchesController) canPaste() *types.DisabledReason {
if !self.c.Helpers().CherryPick.CanPaste() {
return &types.DisabledReason{Text: self.c.Tr.NoCopiedCommits}
}

return nil
}

func (self *BranchesController) GetOnRenderToMain() func() error {
Expand Down
42 changes: 39 additions & 3 deletions pkg/gui/controllers/helpers/cherry_pick_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
type CherryPickHelper struct {
c *HelperCommon

rebaseHelper *MergeAndRebaseHelper
rebaseHelper *MergeAndRebaseHelper
refsHelper *RefsHelper
workingTreeHelper *WorkingTreeHelper
}

// I'm using the analogy of copy+paste in the terminology here because it's intuitively what's going on,
Expand All @@ -20,10 +22,14 @@ type CherryPickHelper struct {
func NewCherryPickHelper(
c *HelperCommon,
rebaseHelper *MergeAndRebaseHelper,
refsHelper *RefsHelper,
workingTreeHelper *WorkingTreeHelper,
) *CherryPickHelper {
return &CherryPickHelper{
c: c,
rebaseHelper: rebaseHelper,
c: c,
rebaseHelper: rebaseHelper,
refsHelper: refsHelper,
workingTreeHelper: workingTreeHelper,
}
}

Expand Down Expand Up @@ -75,6 +81,36 @@ func (self *CherryPickHelper) CopyRange(commitsList []*models.Commit, context ty
return self.rerender()
}

func (self *CherryPickHelper) CheckoutAndPaste(refName string) error {
checkoutAndPaste := func() error {
self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
if err := self.refsHelper.CheckoutRef(refName, types.CheckoutRefOptions{}); err != nil {
return err
}
return self.Paste()
}

if self.workingTreeHelper.IsWorkingTreeDirty() {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.StashChanges,
Prompt: self.c.Tr.SureStashAllChanges,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.StashAllChanges)

if err := self.c.Git().Stash.Push(self.c.Tr.StashAllChanges); err != nil {
return self.c.Error(err)
}
if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.STASH, types.FILES}}); err != nil {
return self.c.Error(err)
}
return checkoutAndPaste()
},
})
}

return checkoutAndPaste()
}

// HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied.
// Only to be called from the branch commits controller
func (self *CherryPickHelper) Paste() error {
Expand Down
32 changes: 32 additions & 0 deletions pkg/gui/controllers/sub_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)

Expand All @@ -30,6 +31,37 @@ func NewSubCommitsController(
}
}

func (self *SubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Commits.PasteCommits),
Handler: self.checkoutAndPaste,
GetDisabledReason: self.require(self.canPaste),
Description: self.c.Tr.PasteCommits,
DisplayStyle: &style.FgCyan,
},
}

return bindings
}

func (self *SubCommitsController) checkoutAndPaste() error {
ref := self.context().GetRef()
if ref == nil {
return nil
}

return self.c.Helpers().CherryPick.CheckoutAndPaste(ref.RefName())
}

func (self *SubCommitsController) canPaste() *types.DisabledReason {
if !self.c.Helpers().CherryPick.CanPaste() {
return &types.DisabledReason{Text: self.c.Tr.NoCopiedCommits}
}

return nil
}

func (self *SubCommitsController) Context() types.Context {
return self.context()
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ type TranslationSet struct {
StashAllChangesKeepIndex string
StashUnstagedChanges string
StashIncludeUntrackedChanges string
SureStashAllChanges string
StashOptions string
NotARepository string
WorkingDirectoryDoesNotExist string
Expand Down Expand Up @@ -1350,6 +1351,7 @@ func EnglishTranslationSet() TranslationSet {
StashAllChangesKeepIndex: "Stash all changes and keep index",
StashUnstagedChanges: "Stash unstaged changes",
StashIncludeUntrackedChanges: "Stash all changes including untracked files",
SureStashAllChanges: "Are you sure you want to stash all changes?",
StashOptions: "Stash options",
NotARepository: "Error: must be run inside a git repository",
WorkingDirectoryDoesNotExist: "Error: the current working directory does not exist",
Expand Down