mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 12:25:47 +02:00
Fix Stashing partial files for git version >= 2.35.0 (#3569)
- **PR Description** Use git's `--staged` flag to stash staged changes if available (requires git 2.35.0 or later), and fall back to our previous method if not. This is a lot faster than our previous method, and it fixes two bugs, see linked issues. Fixes #3333 and #3563. - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [x] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [x] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [x] Docs (specifically `docs/Config.md`) have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
This commit is contained in:
commit
189f39de2b
3 changed files with 86 additions and 2 deletions
|
@ -121,9 +121,22 @@ func (self *StashCommands) StashUnstagedChanges(message string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// SaveStagedChanges stashes only the currently staged changes. This takes a few steps
|
||||
// shoutouts to Joe on https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible
|
||||
// SaveStagedChanges stashes only the currently staged changes.
|
||||
func (self *StashCommands) SaveStagedChanges(message string) error {
|
||||
if self.version.IsAtLeast(2, 35, 0) {
|
||||
return self.cmd.New(NewGitCmd("stash").Arg("push").Arg("--staged").Arg("-m", message).ToArgv()).Run()
|
||||
}
|
||||
|
||||
// Git versions older than 2.35.0 don't support the --staged flag, so we
|
||||
// need to fall back to a more complex solution.
|
||||
// Shoutouts to Joe on https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible
|
||||
//
|
||||
// Note that this method has a few bugs:
|
||||
// - it fails when there are *only* staged changes
|
||||
// - it fails when staged and unstaged changes within a single file are too close together
|
||||
// We don't bother fixing these, because users can simply update git when
|
||||
// they are affected by these issues.
|
||||
|
||||
// wrap in 'writing', which uses a mutex
|
||||
if err := self.cmd.New(
|
||||
NewGitCmd("stash").Arg("--keep-index").ToArgv(),
|
||||
|
|
70
pkg/integration/tests/stash/stash_staged_partial_file.go
Normal file
70
pkg/integration/tests/stash/stash_staged_partial_file.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package stash
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||
)
|
||||
|
||||
var StashStagedPartialFile = NewIntegrationTest(NewIntegrationTestArgs{
|
||||
Description: "Stash staged changes when a file is partially staged",
|
||||
ExtraCmdArgs: []string{},
|
||||
GitVersion: AtLeast("git version 2.35.0"),
|
||||
Skip: false,
|
||||
SetupConfig: func(config *config.AppConfig) {},
|
||||
SetupRepo: func(shell *Shell) {
|
||||
shell.CreateFileAndAdd("file-staged", "line1\nline2\nline3\nline4\n")
|
||||
shell.Commit("initial commit")
|
||||
shell.UpdateFile("file-staged", "line1\nline2 mod\nline3\nline4 mod\n")
|
||||
},
|
||||
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||
t.Views().Files().
|
||||
IsFocused().
|
||||
PressEnter()
|
||||
|
||||
t.Views().Staging().
|
||||
Content(
|
||||
Contains(" line1\n-line2\n+line2 mod\n line3\n-line4\n+line4 mod\n"),
|
||||
).
|
||||
PressPrimaryAction().
|
||||
PressPrimaryAction().
|
||||
Content(
|
||||
Contains(" line1\n line2 mod\n line3\n-line4\n+line4 mod\n"),
|
||||
).
|
||||
PressEscape()
|
||||
|
||||
t.Views().Files().
|
||||
IsFocused().
|
||||
Press(keys.Files.ViewStashOptions)
|
||||
|
||||
t.ExpectPopup().Menu().Title(Equals("Stash options")).Select(MatchesRegexp("Stash staged changes$")).Confirm()
|
||||
|
||||
t.ExpectPopup().Prompt().Title(Equals("Stash changes")).Type("my stashed file").Confirm()
|
||||
|
||||
t.Views().Stash().
|
||||
Focus().
|
||||
Lines(
|
||||
Contains("my stashed file"),
|
||||
).
|
||||
PressEnter()
|
||||
|
||||
t.Views().CommitFiles().
|
||||
IsFocused().
|
||||
Lines(
|
||||
Contains("file-staged").IsSelected(),
|
||||
)
|
||||
t.Views().Main().
|
||||
Content(
|
||||
Contains(" line1\n-line2\n+line2 mod\n line3\n line4\n"),
|
||||
)
|
||||
|
||||
t.Views().Files().
|
||||
Lines(
|
||||
Contains("file-staged"),
|
||||
)
|
||||
|
||||
t.Views().Staging().
|
||||
Content(
|
||||
Contains(" line1\n line2\n line3\n-line4\n+line4 mod\n"),
|
||||
)
|
||||
},
|
||||
})
|
|
@ -257,6 +257,7 @@ var tests = []*components.IntegrationTest{
|
|||
stash.StashAndKeepIndex,
|
||||
stash.StashIncludingUntrackedFiles,
|
||||
stash.StashStaged,
|
||||
stash.StashStagedPartialFile,
|
||||
stash.StashUnstaged,
|
||||
status.ClickRepoNameToOpenReposMenu,
|
||||
status.ClickToFocus,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue