diff options
author | Santiago M. Mola <santi@mola.io> | 2017-03-03 16:09:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-03 16:09:36 +0100 |
commit | 59335b69777f2ef311e63b7d3464459a3ac51d48 (patch) | |
tree | d0b64c76d599bb1f58fc358ae5879bef262cea0f /worktree.go | |
parent | d105e15d91e7553d9d40d6e9fffe0a5008cf8afe (diff) | |
parent | da704cfac743a91474baf45df226e5ec034146b0 (diff) | |
download | go-git-59335b69777f2ef311e63b7d3464459a3ac51d48.tar.gz |
Merge pull request #297 from alcortesm/issue274
issue #274: new filemode package
Diffstat (limited to 'worktree.go')
-rw-r--r-- | worktree.go | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/worktree.go b/worktree.go index 2a4e5d8..36b17b6 100644 --- a/worktree.go +++ b/worktree.go @@ -9,6 +9,7 @@ import ( "srcd.works/go-git.v4/config" "srcd.works/go-git.v4/plumbing" + "srcd.works/go-git.v4/plumbing/filemode" "srcd.works/go-git.v4/plumbing/format/index" "srcd.works/go-git.v4/plumbing/object" @@ -65,11 +66,11 @@ func (w *Worktree) Checkout(commit plumbing.Hash) error { } func (w *Worktree) checkoutEntry(name string, e *object.TreeEntry, idx *index.Index) error { - if e.Mode == object.SubmoduleMode { + if e.Mode == filemode.Submodule { return w.addIndexFromTreeEntry(name, e, idx) } - if e.Mode.IsDir() { + if e.Mode == filemode.Dir { return nil } @@ -86,18 +87,23 @@ func (w *Worktree) checkoutFile(name string, e *object.TreeEntry, idx *index.Ind if err != nil { return err } - defer from.Close() - to, err := w.fs.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, e.Mode.Perm()) + + mode, err := e.Mode.ToOSFileMode() + if err != nil { + return err + } + + to, err := w.fs.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm()) if err != nil { return err } + defer to.Close() if _, err := io.Copy(to, from); err != nil { return err } - defer to.Close() return w.addIndexFromFile(name, e, idx) } @@ -107,7 +113,7 @@ func (w *Worktree) addIndexFromTreeEntry(name string, f *object.TreeEntry, idx * idx.Entries = append(idx.Entries, index.Entry{ Hash: f.Hash, Name: name, - Mode: object.SubmoduleMode, + Mode: filemode.Submodule, }) return nil @@ -119,10 +125,15 @@ func (w *Worktree) addIndexFromFile(name string, f *object.TreeEntry, idx *index return err } + mode, err := filemode.NewFromOSFileMode(fi.Mode()) + if err != nil { + return err + } + e := index.Entry{ Hash: f.Hash, Name: name, - Mode: w.getMode(fi), + Mode: mode, ModifiedAt: fi.ModTime(), Size: uint32(fi.Size()), } @@ -178,7 +189,12 @@ func (w *Worktree) compareFileWithEntry(fi billy.FileInfo, e *index.Entry) (Stat return Modified, nil } - if w.getMode(fi) != e.Mode { + mode, err := filemode.NewFromOSFileMode(fi.Mode()) + if err != nil { + return Modified, err + } + + if mode != e.Mode { return Modified, nil } @@ -191,23 +207,6 @@ func (w *Worktree) compareFileWithEntry(fi billy.FileInfo, e *index.Entry) (Stat return Unmodified, nil } -func (w *Worktree) getMode(fi billy.FileInfo) os.FileMode { - if fi.Mode().IsDir() { - return object.TreeMode - } - - if fi.Mode()&os.ModeSymlink != 0 { - return object.SymlinkMode - } - - const modeExec = 0111 - if fi.Mode()&modeExec != 0 { - return object.ExecutableMode - } - - return object.FileMode -} - const gitmodulesFile = ".gitmodules" // Submodule returns the submodule with the given name |