Skip to content

Commit 590719d

Browse files
committed
Allow removing lines from patch directly
I always press 'd' in the patch building view, expecting that I can do exactly what I can do in the staging view, to find out I need to go space -> ctrl+p -> d and I think it's time to honour the muscle memory and support this convenience keybinding.
1 parent 1d0db51 commit 590719d

13 files changed

Lines changed: 173 additions & 0 deletions

docs-master/keybindings/Keybindings_en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
237237
| `` o `` | Open file | Open file in default application. |
238238
| `` e `` | Edit file | Open file in external editor. |
239239
| `` <space> `` | Toggle lines in patch | |
240+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
240241
| `` <esc> `` | Exit custom patch builder | |
241242
| `` / `` | Search the current view by text | |
242243

docs-master/keybindings/Keybindings_ja.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ _凡例:`<c-b>` はctrl+b、`<a-b>` はalt+b、`B` はshift+bを意味
278278
| `` o `` | ファイルを開く | デフォルトのアプリケーションでファイルを開きます。 |
279279
| `` e `` | ファイルを編集 | 外部エディタでファイルを開きます。 |
280280
| `` <space> `` | パッチ内の行を切り替え | |
281+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
281282
| `` <esc> `` | カスタムパッチビルダーを終了 | |
282283
| `` / `` | 現在のビューをテキストで検索 | |
283284

docs-master/keybindings/Keybindings_ko.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
178178
| `` o `` | 파일 닫기 | Open file in default application. |
179179
| `` e `` | 파일 편집 | Open file in external editor. |
180180
| `` <space> `` | Line(s)을 패치에 추가/삭제 | |
181+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
181182
| `` <esc> `` | Exit custom patch builder | |
182183
| `` / `` | 검색 시작 | |
183184

docs-master/keybindings/Keybindings_nl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
245245
| `` o `` | Open bestand | Open file in default application. |
246246
| `` e `` | Verander bestand | Open file in external editor. |
247247
| `` <space> `` | Voeg toe/verwijder lijn(en) in patch | |
248+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
248249
| `` <esc> `` | Sluit lijn-bij-lijn modus | |
249250
| `` / `` | Start met zoeken | |
250251

docs-master/keybindings/Keybindings_pl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ _Legenda: `<c-b>` oznacza ctrl+b, `<a-b>` oznacza alt+b, `B` oznacza shift+b_
124124
| `` o `` | Otwórz plik | Otwórz plik w domyślnej aplikacji. |
125125
| `` e `` | Edytuj plik | Otwórz plik w zewnętrznym edytorze. |
126126
| `` <space> `` | Przełącz linie w łatce | |
127+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
127128
| `` <esc> `` | Wyjdź z budowniczego niestandardowej łatki | |
128129
| `` / `` | Szukaj w bieżącym widoku po tekście | |
129130

docs-master/keybindings/Keybindings_pt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ _Legend: `<c-b>` means ctrl+b, `<a-b>` means alt+b, `B` means shift+b_
295295
| `` o `` | Abrir arquivo | Abrir arquivo no aplicativo padrão. |
296296
| `` e `` | Editar arquivo | Abrir arquivo no editor externo. |
297297
| `` <space> `` | Alternar linhas no caminho | |
298+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
298299
| `` <esc> `` | Sair do construtor de patch personalizado | |
299300
| `` / `` | Search the current view by text | |
300301

docs-master/keybindings/Keybindings_ru.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ _Связки клавиш_
138138
| `` o `` | Открыть файл | Open file in default application. |
139139
| `` e `` | Редактировать файл | Open file in external editor. |
140140
| `` <space> `` | Добавить/удалить строку(и) для патча | |
141+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
141142
| `` <esc> `` | Выйти из сборщика пользовательских патчей | |
142143
| `` / `` | Найти | |
143144

docs-master/keybindings/Keybindings_zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ _图例:`<c-b>` 意味着ctrl+b, `<a-b>意味着Alt+b, `B` 意味着shift+b_
260260
| `` o `` | 打开文件 | 使用默认程序打开该文件 |
261261
| `` e `` | 编辑文件 | 使用外部编辑器打开文件 |
262262
| `` <space> `` | 添加/移除 行到补丁 | |
263+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
263264
| `` <esc> `` | 退出逐行模式 | |
264265
| `` / `` | 开始搜索 | |
265266

docs-master/keybindings/Keybindings_zh-TW.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ _說明:`<c-b>` 表示 Ctrl+B、`<a-b>` 表示 Alt+B,`B`表示 Shift+B
7272
| `` o `` | 開啟檔案 | 使用預設軟體開啟 |
7373
| `` e `` | 編輯檔案 | 使用外部編輯器開啟 |
7474
| `` <space> `` | 向 (或從) 補丁中添加/刪除行 | |
75+
| `` d `` | Remove lines from commit | Remove the selected lines from this commit. This runs an interactive rebase in the background, so you may get a merge conflict if a later commit also changes these lines. |
7576
| `` <esc> `` | 退出自訂補丁建立器 | |
7677
| `` / `` | 搜尋 | |
7778

pkg/gui/controllers/patch_building_controller.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package controllers
22

33
import (
4+
"fmt"
5+
46
"github.com/jesseduffield/gocui"
7+
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
58
"github.com/jesseduffield/lazygit/pkg/gui/types"
69
"github.com/samber/lo"
710
)
@@ -42,6 +45,14 @@ func (self *PatchBuildingController) GetKeybindings(opts types.KeybindingsOpts)
4245
Description: self.c.Tr.ToggleSelectionForPatch,
4346
DisplayOnScreen: true,
4447
},
48+
{
49+
Key: opts.GetKey(opts.Config.Universal.Remove),
50+
Handler: self.DiscardSelection,
51+
GetDisabledReason: self.getDisabledReasonForDiscard,
52+
Description: self.c.Tr.RemoveSelectionFromPatch,
53+
Tooltip: self.c.Tr.RemoveSelectionFromPatchTooltip,
54+
DisplayOnScreen: true,
55+
},
4556
{
4657
Key: opts.GetKey(opts.Config.Universal.Return),
4758
Handler: self.Escape,
@@ -168,6 +179,83 @@ func (self *PatchBuildingController) toggleSelection() error {
168179
return nil
169180
}
170181

182+
func (self *PatchBuildingController) getDisabledReasonForDiscard() *types.DisabledReason {
183+
if !self.c.Git().Patch.PatchBuilder.CanRebase {
184+
return &types.DisabledReason{Text: self.c.Tr.CanOnlyRemoveLinesFromLocalCommits}
185+
}
186+
if !self.c.Git().Patch.PatchBuilder.IsEmpty() {
187+
return &types.DisabledReason{Text: self.c.Tr.MustClearPatchBeforeRemovingLines}
188+
}
189+
return nil
190+
}
191+
192+
func (self *PatchBuildingController) DiscardSelection() error {
193+
if self.c.UserConfig().Git.DiffContextSize == 0 {
194+
return fmt.Errorf(self.c.Tr.Actions.NotEnoughContextToStage,
195+
keybindings.Label(self.c.UserConfig().Keybinding.Universal.IncreaseContextInDiffView))
196+
}
197+
198+
if ok, err := self.c.Helpers().PatchBuilding.ValidateNormalWorkingTreeState(); !ok {
199+
return err
200+
}
201+
202+
self.c.Confirm(types.ConfirmOpts{
203+
Title: self.c.Tr.RemoveLinesFromCommitTitle,
204+
Prompt: self.c.Tr.RemoveLinesFromCommitPrompt,
205+
HandleConfirm: func() error {
206+
return self.removeSelectionFromCommit()
207+
},
208+
})
209+
210+
return nil
211+
}
212+
213+
func (self *PatchBuildingController) addSelectionToPatch() error {
214+
self.context().GetMutex().Lock()
215+
defer self.context().GetMutex().Unlock()
216+
217+
filename := self.c.Contexts().CommitFiles.GetSelectedPath()
218+
if filename == "" {
219+
return nil
220+
}
221+
222+
state := self.context().GetState()
223+
lineIndicesToToggle := state.LineIndicesOfAddedOrDeletedLinesInSelectedPatchRange()
224+
if len(lineIndicesToToggle) == 0 {
225+
return nil
226+
}
227+
228+
return self.c.Git().Patch.PatchBuilder.AddFileLineRange(filename, lineIndicesToToggle)
229+
}
230+
231+
func (self *PatchBuildingController) removeSelectionFromCommit() error {
232+
if err := self.addSelectionToPatch(); err != nil {
233+
return err
234+
}
235+
236+
if self.c.Git().Patch.PatchBuilder.IsEmpty() {
237+
return nil
238+
}
239+
240+
self.c.Helpers().PatchBuilding.Escape()
241+
242+
return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func(gocui.Task) error {
243+
commitIndex := self.getPatchCommitIndex()
244+
self.c.LogAction(self.c.Tr.Actions.RemovePatchFromCommit)
245+
err := self.c.Git().Patch.DeletePatchesFromCommit(self.c.Model().Commits, commitIndex)
246+
return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err)
247+
})
248+
}
249+
250+
func (self *PatchBuildingController) getPatchCommitIndex() int {
251+
for index, commit := range self.c.Model().Commits {
252+
if commit.Hash() == self.c.Git().Patch.PatchBuilder.To {
253+
return index
254+
}
255+
}
256+
return -1
257+
}
258+
171259
func (self *PatchBuildingController) Escape() error {
172260
context := self.c.Contexts().CustomPatchBuilder
173261
state := context.GetState()

0 commit comments

Comments
 (0)