1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00

better handling of discarding files

This commit is contained in:
Jesse Duffield 2021-03-20 12:07:11 +11:00
parent 058bcddc53
commit ad1468f66f
67 changed files with 163 additions and 4 deletions

View File

@ -107,15 +107,34 @@ func (c *GitCommand) DiscardAllFileChanges(file *models.File) error {
return nil
}
// if the file isn't tracked, we assume you want to delete it
quotedFileName := c.OSCommand.Quote(file.Name)
if file.ShortStatus == "AA" {
if err := c.OSCommand.RunCommand("git checkout --ours -- %s", quotedFileName); err != nil {
return err
}
if err := c.OSCommand.RunCommand("git add %s", quotedFileName); err != nil {
return err
}
return nil
}
if file.ShortStatus == "DU" {
return c.OSCommand.RunCommand("git rm %s", quotedFileName)
}
// if the file isn't tracked, we assume you want to delete it
if file.HasStagedChanges || file.HasMergeConflicts {
if err := c.OSCommand.RunCommand("git reset -- %s", quotedFileName); err != nil {
return err
}
}
if !file.Tracked {
if file.ShortStatus == "DD" || file.ShortStatus == "AU" {
return nil
}
if file.Added {
return c.removeFile(file.Name)
}
return c.DiscardUnstagedFileChanges(file)

View File

@ -344,6 +344,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: true,
Added: false,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
@ -356,6 +357,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
HasStagedChanges: true,
HasUnstagedChanges: false,
Tracked: false,
Added: true,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
@ -368,6 +370,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
HasStagedChanges: true,
HasUnstagedChanges: true,
Tracked: false,
Added: true,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
@ -380,6 +383,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: false,
Added: true,
Deleted: false,
HasMergeConflicts: false,
HasInlineMergeConflicts: false,
@ -392,6 +396,7 @@ func TestGitCommandGetStatusFiles(t *testing.T) {
HasStagedChanges: false,
HasUnstagedChanges: true,
Tracked: true,
Added: false,
Deleted: false,
HasMergeConflicts: true,
HasInlineMergeConflicts: true,
@ -1093,6 +1098,9 @@ func TestGitCommandUnstageFile(t *testing.T) {
}
// TestGitCommandDiscardAllFileChanges is a function.
// these tests don't cover everything, in part because we already have an integration
// test which does cover everything. I don't want to unnecessarily assert on the 'how'
// when the 'what' is what matters
func TestGitCommandDiscardAllFileChanges(t *testing.T) {
type scenario struct {
testName string
@ -1146,6 +1154,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
&models.File{
Name: "test",
Tracked: false,
Added: true,
},
func(string) error {
return fmt.Errorf("an error occurred when removing file")
@ -1277,6 +1286,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
&models.File{
Name: "test",
Tracked: false,
Added: true,
HasStagedChanges: true,
},
func(filename string) error {
@ -1301,6 +1311,7 @@ func TestGitCommandDiscardAllFileChanges(t *testing.T) {
&models.File{
Name: "test",
Tracked: false,
Added: true,
HasStagedChanges: false,
},
func(filename string) error {

View File

@ -50,6 +50,7 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*models.File {
HasUnstagedChanges: unstagedChange != " ",
Tracked: !untracked,
Deleted: unstagedChange == "D" || stagedChange == "D",
Added: unstagedChange == "A" || untracked,
HasMergeConflicts: hasMergeConflicts,
HasInlineMergeConflicts: hasInlineMergeConflicts,
Type: c.OSCommand.FileType(filename),

View File

@ -13,6 +13,7 @@ type File struct {
HasStagedChanges bool
HasUnstagedChanges bool
Tracked bool
Added bool
Deleted bool
HasMergeConflicts bool
HasInlineMergeConflicts bool

View File

@ -451,6 +451,7 @@ func (gui *Gui) Run() error {
if err != nil {
return err
}
gui.g = g // TODO: always use gui.g rather than passing g around everywhere
defer g.Close()
if recordEvents {
@ -478,8 +479,6 @@ func (gui *Gui) Run() error {
g.Mouse = true
}
gui.g = g // TODO: always use gui.g rather than passing g around everywhere
if err := gui.setColorScheme(); err != nil {
return err
}

View File

@ -0,0 +1 @@
three

View File

@ -0,0 +1 @@
ref: refs/heads/conflict_second

View File

@ -0,0 +1 @@
38b645bf2388b21695d22d7f673d87971a78e4ad

View File

@ -0,0 +1,10 @@
Merge branch 'conflict' into conflict_second
# Conflicts:
# added-them-changed-us.txt
# both-added.txt
# both-deleted.txt
# both-modded.txt
# changed-them-added-us.txt
# deleted-them.txt
# deleted-us.txt

View File

@ -0,0 +1 @@
c64dd6aea3efd197ed630dedc1ffb538d35c3fdb

View File

@ -0,0 +1,10 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
email = CI@example.com
name = CI

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store

View File

@ -0,0 +1,7 @@
0000000000000000000000000000000000000000 0e5c9fd43732ca064946e9c582fd6f159774db84 CI <CI@example.com> 1616203051 +1100 commit (initial): one
0e5c9fd43732ca064946e9c582fd6f159774db84 eb4814e3f9d5eb1869704cf4498420c9f4a141da CI <CI@example.com> 1616203051 +1100 commit: both-deleted.txt renamed in added-them-changed-us.txt
eb4814e3f9d5eb1869704cf4498420c9f4a141da 38b645bf2388b21695d22d7f673d87971a78e4ad CI <CI@example.com> 1616203051 +1100 commit: two
38b645bf2388b21695d22d7f673d87971a78e4ad 0e5c9fd43732ca064946e9c582fd6f159774db84 CI <CI@example.com> 1616203051 +1100 checkout: moving from conflict to conflict_second
0e5c9fd43732ca064946e9c582fd6f159774db84 698e214196fed018bb4164380745dd2df3bbac74 CI <CI@example.com> 1616203051 +1100 commit: both-deleted.txt renamed in changed-them-added-us.txt
698e214196fed018bb4164380745dd2df3bbac74 c64dd6aea3efd197ed630dedc1ffb538d35c3fdb CI <CI@example.com> 1616203052 +1100 commit: three
c64dd6aea3efd197ed630dedc1ffb538d35c3fdb c64dd6aea3efd197ed630dedc1ffb538d35c3fdb CI <CI@example.com> 1616203052 +1100 reset: moving to conflict_second

View File

@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 0e5c9fd43732ca064946e9c582fd6f159774db84 CI <CI@example.com> 1616203051 +1100 commit (initial): one
0e5c9fd43732ca064946e9c582fd6f159774db84 eb4814e3f9d5eb1869704cf4498420c9f4a141da CI <CI@example.com> 1616203051 +1100 commit: both-deleted.txt renamed in added-them-changed-us.txt
eb4814e3f9d5eb1869704cf4498420c9f4a141da 38b645bf2388b21695d22d7f673d87971a78e4ad CI <CI@example.com> 1616203051 +1100 commit: two

View File

@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 0e5c9fd43732ca064946e9c582fd6f159774db84 CI <CI@example.com> 1616203051 +1100 branch: Created from conflict
0e5c9fd43732ca064946e9c582fd6f159774db84 698e214196fed018bb4164380745dd2df3bbac74 CI <CI@example.com> 1616203051 +1100 commit: both-deleted.txt renamed in changed-them-added-us.txt
698e214196fed018bb4164380745dd2df3bbac74 c64dd6aea3efd197ed630dedc1ffb538d35c3fdb CI <CI@example.com> 1616203052 +1100 commit: three

View File

@ -0,0 +1,2 @@
x�ÍA
Â@ @Q×sŠì ’¤cR¡ˆÐU�‘§”)x|{„n?~ª¥¼PäKÛÜ¡GÔtO¨KÐ̔ʔ£kvæEt¶·WÝ`šaœæ§ÿ¬¬¿¦Z@BÂØã� #B G=&ÍOòP¿þIÔ*Ê

View File

@ -0,0 +1,2 @@
x�ÎA
Â0@Q×9Eö‚ÌLÒd"BW=F&™ `l)<¾=‚ÛÏ_¼²öþ�Æ®j§TZ”ȹ¥œ…=£“" …E)¶Za³å]߯ÄJè1…¦�E<j¥ÚœH.Ñ›ü�u·ób¯ór×oîÛK/eí7‹�ƒ‰ìÀõ@ ýs7ãqÐÍÛ':G

View File

@ -0,0 +1 @@
x�ŽAjÃ0E»Ö)f_F²$[PJ!«c$}Õ�Èîrü:7Èò=Þâ•­÷«’óü¡;@à9K.1[N�­mÞA²„6%DÄ4N˜Ì]v¬JŒPR«~œFW„£O>"•0»Vc³!M“¯yöFºl;�/ôu¾üà)ý~élý›l´ÑñÈÁÒ§µÌæ°Ç”âÍÜäM—¡âE=éSéX“ŽJו¤VÔAô¡,²þðø{EæVJK–

View File

@ -0,0 +1 @@
38b645bf2388b21695d22d7f673d87971a78e4ad

View File

@ -0,0 +1 @@
c64dd6aea3efd197ed630dedc1ffb538d35c3fdb

View File

@ -0,0 +1 @@
blah2

View File

@ -0,0 +1 @@
mod2

View File

@ -0,0 +1 @@
change-delete

View File

@ -0,0 +1 @@
delete-change

View File

@ -0,0 +1 @@
del

View File

@ -0,0 +1 @@
modded

View File

@ -0,0 +1 @@
del

View File

@ -0,0 +1 @@
double-modded

View File

@ -0,0 +1 @@
mod

View File

@ -0,0 +1 @@
mod

View File

@ -0,0 +1 @@
renamed\nhaha

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,62 @@
#!/bin/sh
cd $1
git init
git config user.email "CI@example.com"
git config user.name "CI"
# common stuff
echo test > both-deleted.txt
git checkout -b conflict && git add both-deleted.txt
echo bothmodded > both-modded.txt && git add both-modded.txt
echo haha > deleted-them.txt && git add deleted-them.txt
echo haha2 > deleted-us.txt && git add deleted-us.txt
echo mod > modded.txt & git add modded.txt
echo mod > modded-staged.txt & git add modded-staged.txt
echo del > deleted.txt && git add deleted.txt
echo del > deleted-staged.txt && git add deleted-staged.txt
echo change-delete > change-delete.txt && git add change-delete.txt
echo delete-change > delete-change.txt && git add delete-change.txt
echo double-modded > double-modded.txt && git add double-modded.txt
echo "renamed\nhaha" > renamed.txt && git add renamed.txt
git commit -m one
# stuff on other branch
git branch conflict_second && git mv both-deleted.txt added-them-changed-us.txt
git commit -m "both-deleted.txt renamed in added-them-changed-us.txt"
echo blah > both-added.txt && git add both-added.txt
echo mod1 > both-modded.txt && git add both-modded.txt
rm deleted-them.txt && git add deleted-them.txt
echo modded > deleted-us.txt && git add deleted-us.txt
git commit -m "two"
# stuff on our branch
git checkout conflict_second
git mv both-deleted.txt changed-them-added-us.txt
git commit -m "both-deleted.txt renamed in changed-them-added-us.txt"
echo mod2 > both-modded.txt && git add both-modded.txt
echo blah2 > both-added.txt && git add both-added.txt
echo modded > deleted-them.txt && git add deleted-them.txt
rm deleted-us.txt && git add deleted-us.txt
git commit -m "three"
git reset --hard conflict_second
git merge conflict
echo "new" > new.txt
echo "new staged" > new-staged.txt && git add new-staged.txt
echo mod2 > modded.txt
echo mod2 > modded-staged.txt && git add modded-staged.txt
rm deleted.txt
rm deleted-staged.txt && git add deleted-staged.txt
echo change-delete2 > change-delete.txt && git add change-delete.txt
rm change-delete.txt
rm delete-change.txt && git add delete-change.txt
echo "changed" > delete-change.txt
echo "change1" > double-modded.txt && git add double-modded.txt
echo "change2" > double-modded.txt
echo before > added-changed.txt && git add added-changed.txt
echo after > added-changed.txt
rm renamed.txt && git add renamed.txt
echo "renamed\nhaha" > renamed2.txt && git add renamed2.txt

View File

@ -0,0 +1 @@
{ "description": "discard file changes for various kinds of situations (merge conflicts, etc)", "speed": 2 }