github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/git/reset_test.go (about)

     1  package git
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // TestResetFiles tests that the "git reset -- pathspec"
    10  // variation of git reset works as expected.
    11  func TestResetFiles(t *testing.T) {
    12  	dir, err := ioutil.TempDir("", "gitreset")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	defer os.RemoveAll(dir)
    17  
    18  	// Init a repo to test an initial commit in.
    19  	c, err := Init(nil, InitOptions{Quiet: true}, dir)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if err := os.Chdir(dir); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	// Create a commit so that there's a HEAD to reset from.
    28  	if err := ioutil.WriteFile(dir+"/foo.txt", []byte("foo\n"), 0644); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	if _, err := Add(c, AddOptions{}, []File{"foo.txt"}); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	cmt, err := Commit(c, CommitOptions{}, "Initial commit", nil)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	if err := ioutil.WriteFile(dir+"/foo.txt", []byte("bar\n"), 0644); err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	if _, err := Add(c, AddOptions{}, []File{"foo.txt"}); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	if err := Reset(c, ResetOptions{}, []File{"foo.txt"}); err != nil {
    49  		t.Error(err)
    50  	}
    51  
    52  	idx, err := LsFiles(c, LsFilesOptions{Cached: true}, nil)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	if sha := hashString("foo\n"); idx[0].Sha1 != sha {
    58  		t.Errorf("Unexpected hash for %v: got %v want %v", idx[0].PathName, idx[0].Sha1, sha)
    59  	}
    60  
    61  	if err := ioutil.WriteFile(dir+"/bar.txt", []byte("bar\n"), 0644); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	if _, err := Add(c, AddOptions{}, []File{"bar.txt"}); err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	if err := Reset(c, ResetOptions{}, []File{"bar.txt"}); err != nil {
    70  		t.Error(err)
    71  	}
    72  
    73  	idx, err = LsFiles(c, LsFilesOptions{Cached: true}, nil)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	if len(idx) != 1 {
    79  		t.Fatal("Did not correctly unstage new file")
    80  	}
    81  
    82  	if idx[0].PathName != "foo.txt" || idx[0].Sha1 != hashString("foo\n") {
    83  		t.Error("Unstaged the wrong file.")
    84  	}
    85  
    86  	// Get into a detached HEAD state and try again.
    87  	if err := CheckoutCommit(c, CheckoutOptions{Force: true}, cmt); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if _, err := SymbolicRefGet(c, SymbolicRefOptions{}, "HEAD"); err != DetachedHead {
    91  		t.Fatal("Did not correctly switch to detached head mode.")
    92  	}
    93  	if err := ioutil.WriteFile(dir+"/foo.txt", []byte("bar\n"), 0644); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	if _, err := Add(c, AddOptions{}, []File{"foo.txt"}); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	if err := Reset(c, ResetOptions{}, []File{"foo.txt"}); err != nil {
   102  		t.Error(err)
   103  	}
   104  
   105  	idx, err = LsFiles(c, LsFilesOptions{Cached: true}, nil)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	if sha := hashString("foo\n"); idx[0].Sha1 != sha {
   111  		t.Errorf("Unexpected hash for %v: got %v want %v", idx[0].PathName, idx[0].Sha1, sha)
   112  	}
   113  	if _, err := Add(c, AddOptions{}, []File{"bar.txt"}); err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	if err := Reset(c, ResetOptions{}, []File{"bar.txt"}); err != nil {
   118  		t.Error(err)
   119  	}
   120  
   121  	idx, err = LsFiles(c, LsFilesOptions{Cached: true}, nil)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	if len(idx) != 1 {
   127  		t.Fatal("Did not correctly unstage new file")
   128  	}
   129  
   130  	if idx[0].PathName != "foo.txt" || idx[0].Sha1 != hashString("foo\n") {
   131  		t.Error("Unstaged the wrong file.")
   132  	}
   133  }