diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-27 17:46:46 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-01-28 00:42:22 +0100 |
commit | 24c1878260351d9f9f6c575cbeeb5878104d6a0e (patch) | |
tree | c6a9575aa84197f7f33515ca4d55816134d1569b /worktree.go | |
parent | 1c2602a791371e76d52f89b2c8193cb200c66ad6 (diff) | |
download | go-git-24c1878260351d9f9f6c575cbeeb5878104d6a0e.tar.gz |
new repository constructors and worktree
Diffstat (limited to 'worktree.go')
-rw-r--r-- | worktree.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/worktree.go b/worktree.go new file mode 100644 index 0000000..2aefa76 --- /dev/null +++ b/worktree.go @@ -0,0 +1,49 @@ +package git + +import ( + "io" + "os" + + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/object" + + "srcd.works/go-billy.v1" +) + +type Worktree struct { + r *Repository + fs billy.Filesystem +} + +func (w *Worktree) Checkout(commit plumbing.Hash) error { + c, err := w.r.Commit(commit) + if err != nil { + return err + } + + files, err := c.Files() + if err != nil { + return err + } + + return files.ForEach(w.checkoutFile) +} + +func (w *Worktree) checkoutFile(f *object.File) error { + from, err := f.Reader() + if err != nil { + return err + } + + defer from.Close() + to, err := w.fs.OpenFile(f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode) + if err != nil { + return err + } + + if _, err := io.Copy(to, from); err != nil { + return err + } + + return to.Close() +} |