Skip to content

Commit 5b80c0c

Browse files
dsolerhwwstefanhaller
authored andcommitted
Fix stashing partialy staged files for git version >= 2.35.0
Use `git stash push --staged` git feature available on git version > 2.35.0.
1 parent d8b3c0e commit 5b80c0c

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

pkg/commands/git_commands/stash.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,22 @@ func (self *StashCommands) StashUnstagedChanges(message string) error {
121121
return nil
122122
}
123123

124-
// SaveStagedChanges stashes only the currently staged changes. This takes a few steps
125-
// shoutouts to Joe on https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible
124+
// SaveStagedChanges stashes only the currently staged changes.
126125
func (self *StashCommands) SaveStagedChanges(message string) error {
126+
if self.version.IsAtLeast(2, 35, 0) {
127+
return self.cmd.New(NewGitCmd("stash").Arg("push").Arg("--staged").Arg("-m", message).ToArgv()).Run()
128+
}
129+
130+
// Git versions older than 2.35.0 don't support the --staged flag, so we
131+
// need to fall back to a more complex solution.
132+
// Shoutouts to Joe on https://stackoverflow.com/questions/14759748/stashing-only-staged-changes-in-git-is-it-possible
133+
//
134+
// Note that this method has a few bugs:
135+
// - it fails when there are *only* staged changes
136+
// - it fails when staged and unstaged changes within a single file are too close together
137+
// We don't bother fixing these, because users can simply update git when
138+
// they are affected by these issues.
139+
127140
// wrap in 'writing', which uses a mutex
128141
if err := self.cmd.New(
129142
NewGitCmd("stash").Arg("--keep-index").ToArgv(),
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package stash
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var StashStagedPartialFile = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Stash staged changes when a file is partially staged",
10+
ExtraCmdArgs: []string{},
11+
GitVersion: AtLeast("git version 2.35.0"),
12+
Skip: false,
13+
SetupConfig: func(config *config.AppConfig) {},
14+
SetupRepo: func(shell *Shell) {
15+
shell.CreateFileAndAdd("file-staged", "line1\nline2\nline3\nline4\n")
16+
shell.Commit("initial commit")
17+
shell.UpdateFile("file-staged", "line1\nline2 mod\nline3\nline4 mod\n")
18+
},
19+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
20+
t.Views().Files().
21+
IsFocused().
22+
PressEnter()
23+
24+
t.Views().Staging().
25+
Content(
26+
Contains(" line1\n-line2\n+line2 mod\n line3\n-line4\n+line4 mod\n"),
27+
).
28+
PressPrimaryAction().
29+
PressPrimaryAction().
30+
Content(
31+
Contains(" line1\n line2 mod\n line3\n-line4\n+line4 mod\n"),
32+
).
33+
PressEscape()
34+
35+
t.Views().Files().
36+
IsFocused().
37+
Press(keys.Files.ViewStashOptions)
38+
39+
t.ExpectPopup().Menu().Title(Equals("Stash options")).Select(MatchesRegexp("Stash staged changes$")).Confirm()
40+
41+
t.ExpectPopup().Prompt().Title(Equals("Stash changes")).Type("my stashed file").Confirm()
42+
43+
t.Views().Stash().
44+
Focus().
45+
Lines(
46+
Contains("my stashed file"),
47+
).
48+
PressEnter()
49+
50+
t.Views().CommitFiles().
51+
IsFocused().
52+
Lines(
53+
Contains("file-staged").IsSelected(),
54+
)
55+
t.Views().Main().
56+
Content(
57+
Contains(" line1\n-line2\n+line2 mod\n line3\n line4\n"),
58+
)
59+
60+
t.Views().Files().
61+
Lines(
62+
Contains("file-staged"),
63+
)
64+
65+
t.Views().Staging().
66+
Content(
67+
Contains(" line1\n line2\n line3\n-line4\n+line4 mod\n"),
68+
)
69+
},
70+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ var tests = []*components.IntegrationTest{
257257
stash.StashAndKeepIndex,
258258
stash.StashIncludingUntrackedFiles,
259259
stash.StashStaged,
260+
stash.StashStagedPartialFile,
260261
stash.StashUnstaged,
261262
status.ClickRepoNameToOpenReposMenu,
262263
status.ClickToFocus,

0 commit comments

Comments
 (0)