aboutsummaryrefslogtreecommitdiffstats
path: root/worktree.go
diff options
context:
space:
mode:
Diffstat (limited to 'worktree.go')
-rw-r--r--worktree.go49
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()
+}