|
1 | 1 | package controllers |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + |
4 | 6 | "github.com/jesseduffield/gocui" |
| 7 | + "github.com/jesseduffield/lazygit/pkg/gui/keybindings" |
5 | 8 | "github.com/jesseduffield/lazygit/pkg/gui/types" |
6 | 9 | "github.com/samber/lo" |
7 | 10 | ) |
@@ -42,6 +45,14 @@ func (self *PatchBuildingController) GetKeybindings(opts types.KeybindingsOpts) |
42 | 45 | Description: self.c.Tr.ToggleSelectionForPatch, |
43 | 46 | DisplayOnScreen: true, |
44 | 47 | }, |
| 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 | + }, |
45 | 56 | { |
46 | 57 | Key: opts.GetKey(opts.Config.Universal.Return), |
47 | 58 | Handler: self.Escape, |
@@ -168,6 +179,83 @@ func (self *PatchBuildingController) toggleSelection() error { |
168 | 179 | return nil |
169 | 180 | } |
170 | 181 |
|
| 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 | + |
171 | 259 | func (self *PatchBuildingController) Escape() error { |
172 | 260 | context := self.c.Contexts().CustomPatchBuilder |
173 | 261 | state := context.GetState() |
|
0 commit comments