Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Fix for "Worktree Add function adds ".git" directory" #815

Merged
merged 1 commit into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
coverage.out
*~
coverage.txt
profile.out
10 changes: 8 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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: "[email protected]",
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)
}