diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-07-18 21:41:34 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-07-18 21:41:34 +0200 |
commit | 9afc47237c301ecee66619d1ef8ec286185cb070 (patch) | |
tree | 5b7d67f820b14db690edcc6f506df64aaf28b717 /worktree.go | |
parent | d3c7400c39f86a4c59340c7a9cda8497186e00fc (diff) | |
download | go-git-9afc47237c301ecee66619d1ef8ec286185cb070.tar.gz |
worktree: checkout, create branch
Diffstat (limited to 'worktree.go')
-rw-r--r-- | worktree.go | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/worktree.go b/worktree.go index ae1ab28..e0f5fdf 100644 --- a/worktree.go +++ b/worktree.go @@ -38,8 +38,14 @@ func (w *Worktree) Checkout(opts *CheckoutOptions) error { return err } + if opts.Create { + if err := w.createBranch(opts); err != nil { + return err + } + } + if !opts.Force { - unstaged, err := w.cointainsUnstagedChanges() + unstaged, err := w.containsUnstagedChanges() if err != nil { return err } @@ -59,7 +65,7 @@ func (w *Worktree) Checkout(opts *CheckoutOptions) error { ro.Mode = HardReset } - if !opts.Hash.IsZero() { + if !opts.Hash.IsZero() && !opts.Create { err = w.setHEADToCommit(opts.Hash) } else { err = w.setHEADToBranch(opts.Branch, c) @@ -71,6 +77,29 @@ func (w *Worktree) Checkout(opts *CheckoutOptions) error { return w.Reset(ro) } +func (w *Worktree) createBranch(opts *CheckoutOptions) error { + _, err := w.r.Storer.Reference(opts.Branch) + if err == nil { + return fmt.Errorf("a branch named %q already exists", opts.Branch) + } + + if err != plumbing.ErrReferenceNotFound { + return err + } + + if opts.Hash.IsZero() { + ref, err := w.r.Head() + if err != nil { + return err + } + + opts.Hash = ref.Hash() + } + + return w.r.Storer.SetReference( + plumbing.NewHashReference(opts.Branch, opts.Hash), + ) +} func (w *Worktree) getCommitFromCheckoutOptions(opts *CheckoutOptions) (plumbing.Hash, error) { if !opts.Hash.IsZero() { @@ -133,7 +162,7 @@ func (w *Worktree) Reset(opts *ResetOptions) error { } if opts.Mode == MergeReset { - unstaged, err := w.cointainsUnstagedChanges() + unstaged, err := w.containsUnstagedChanges() if err != nil { return err } @@ -171,7 +200,7 @@ func (w *Worktree) Reset(opts *ResetOptions) error { return w.setHEADCommit(opts.Commit) } -func (w *Worktree) cointainsUnstagedChanges() (bool, error) { +func (w *Worktree) containsUnstagedChanges() (bool, error) { ch, err := w.diffStagingWithWorktree() if err != nil { return false, err |