diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-04-18 16:37:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-18 16:37:30 +0200 |
commit | 87cc8192de93dcf7b843058f11ad85200d46bb0c (patch) | |
tree | dafcbe0c3c76881e7066c8505a8cdbf92be9f8ec | |
parent | b30763cb64afa91c016b23e905af0a378eb1b76d (diff) | |
parent | 6b33126e79695b499d7d519f69db4ca1ebd22dd1 (diff) | |
download | go-git-87cc8192de93dcf7b843058f11ad85200d46bb0c.tar.gz |
Merge pull request #815 from kuba--/fix-worktree/814
Fix for "Worktree Add function adds ".git" directory"
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | example_test.go | 10 | ||||
-rw-r--r-- | repository.go | 15 | ||||
-rw-r--r-- | worktree_status.go | 4 | ||||
-rw-r--r-- | worktree_test.go | 38 |
5 files changed, 62 insertions, 8 deletions
@@ -1 +1,4 @@ coverage.out +*~ +coverage.txt +profile.out diff --git a/example_test.go b/example_test.go index e9d8e8b..ef7e3d3 100644 --- a/example_test.go +++ b/example_test.go @@ -24,12 +24,18 @@ func ExampleClone() { // Clones the repository into the worktree (fs) and storer all the .git // content into the storer - _, _ = git.Clone(storer, fs, &git.CloneOptions{ + _, err := git.Clone(storer, fs, &git.CloneOptions{ URL: "https://github.com/git-fixtures/basic.git", }) + if err != nil { + log.Fatal(err) + } // Prints the content of the CHANGELOG file from the cloned repository - changelog, _ := fs.Open("CHANGELOG") + changelog, err := fs.Open("CHANGELOG") + if err != nil { + log.Fatal(err) + } io.Copy(os.Stdout, changelog) // Output: Initial changelog diff --git a/repository.go b/repository.go index 928ad9d..717381b 100644 --- a/repository.go +++ b/repository.go @@ -24,6 +24,9 @@ import ( "gopkg.in/src-d/go-billy.v4/osfs" ) +// GitDirName this is a special folder where all the git stuff is. +const GitDirName = ".git" + var ( // ErrBranchExists an error stating the specified branch already exists ErrBranchExists = errors.New("branch already exists") @@ -113,12 +116,12 @@ func createDotGitFile(worktree, storage billy.Filesystem) error { path = storage.Root() } - if path == ".git" { + if path == GitDirName { // not needed, since the folder is the default place return nil } - f, err := worktree.Create(".git") + f, err := worktree.Create(GitDirName) if err != nil { return err } @@ -214,7 +217,7 @@ func PlainInit(path string, isBare bool) (*Repository, error) { dot = osfs.New(path) } else { wt = osfs.New(path) - dot, _ = wt.Chroot(".git") + dot, _ = wt.Chroot(GitDirName) } s, err := filesystem.NewStorage(dot) @@ -265,7 +268,7 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, var fi os.FileInfo for { fs = osfs.New(path) - fi, err = fs.Stat(".git") + fi, err = fs.Stat(GitDirName) if err == nil { // no error; stop break @@ -288,7 +291,7 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, } if fi.IsDir() { - dot, err = fs.Chroot(".git") + dot, err = fs.Chroot(GitDirName) return dot, fs, err } @@ -301,7 +304,7 @@ func dotGitToOSFilesystems(path string, detect bool) (dot, wt billy.Filesystem, } func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (bfs billy.Filesystem, err error) { - f, err := fs.Open(".git") + f, err := fs.Open(GitDirName) if err != nil { return nil, err } diff --git a/worktree_status.go b/worktree_status.go index 2cac78e..b5f2381 100644 --- a/worktree_status.go +++ b/worktree_status.go @@ -300,6 +300,10 @@ func (w *Worktree) doAddDirectory(idx *index.Index, s Status, directory string) var a bool if file.IsDir() { + if file.Name() == GitDirName { + // ignore special git directory + continue + } a, err = w.doAddDirectory(idx, s, name) } else { a, _, err = w.doAddFile(idx, s, name) diff --git a/worktree_test.go b/worktree_test.go index b3e0a1a..05a205a 100644 --- a/worktree_test.go +++ b/worktree_test.go @@ -3,12 +3,14 @@ package git import ( "bytes" "context" + "errors" "io/ioutil" "os" "path/filepath" "regexp" "runtime" "testing" + "time" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" @@ -1833,3 +1835,39 @@ func (s *WorktreeSuite) TestGrep(c *C) { } } } + +func (s *WorktreeSuite) TestAddAndCommit(c *C) { + dir, err := ioutil.TempDir("", "plain-repo") + c.Assert(err, IsNil) + defer os.RemoveAll(dir) + + repo, err := PlainInit(dir, false) + c.Assert(err, IsNil) + + w, err := repo.Worktree() + c.Assert(err, IsNil) + + _, err = w.Add(".") + c.Assert(err, IsNil) + + w.Commit("Test Add And Commit", &CommitOptions{Author: &object.Signature{ + Name: "foo", + Email: "foo@foo.foo", + When: time.Now(), + }}) + + iter, err := w.r.Log(&LogOptions{}) + c.Assert(err, IsNil) + err = iter.ForEach(func(c *object.Commit) error { + files, err := c.Files() + if err != nil { + return err + } + + err = files.ForEach(func(f *object.File) error { + return errors.New("Expected no files, got at least 1") + }) + return err + }) + c.Assert(err, IsNil) +} |