// Branch starts a new lookahead branch.
347326
func (p *parseContext) Branch() *parseContext {
	branch := &parseContext{}
	*branch = *p
	branch.apply = nil
	branch.rewinder = p.rewinder.Lookahead()
	return branch
}
1208874
func (b *Branch) add(entry []byte) (addedBranch *Branch) {
	if b.LeafValue == nil && len(b.Branches) == 0 {
		if len(entry) > 0 {
			b.LeafValue = entry
		} else {
			// something came in but we already have branches for it
			// so the tail was the current branches index but no value
			// to push. just mark the current idx position as End
		}
		b.setEnd(true)
		addedBranch = b
		return
	}

	// check the overlap between the current LeafValue and the new entry
	newLeaf := func(LeafValue, newEntry []byte) (leaf []byte) {
		for li, b := range LeafValue {
			if li > len(newEntry)-1 {
				break
			}
			if b == newEntry[li] {
				leaf = append(leaf, b)
			} else {
				break
			}
		}
		return
	}(b.LeafValue, entry)

	newLeafLen := len(newLeaf)

	// the new leaf is smaller than the current leaf.
	// we will push the old leaf down the branch
	if newLeafLen < len(b.LeafValue) {
		tail := b.LeafValue[newLeafLen:]
		idx := tail[0]
		newBranch := b.NewBranch()
		newBranch.LeafValue = tail[1:]

		b.LeafValue = newLeaf
		newBranch.Branches, b.Branches = b.Branches, newBranch.Branches
		newBranch.End, b.End = b.End, newBranch.End
		if newBranch.End {
			if b.Count > 0 {
				newBranch.Count = b.Count
			} else {
				newBranch.Count = 1
			}
		} else {
			newBranch.Count = 0
		}
		if b.End {
			b.Count = 1
		} else {
			b.Count = 0
		}
		b.Branches[idx] = newBranch
	}

	// new leaf is smaller than the entry, which means there will be more stuff
	// that we need to push down
	if newLeafLen < len(entry) {
		tail := entry[newLeafLen:]
		idx := tail[0]

		// create new branch at idx if it does not exists yet
		if _, notPresent := b.Branches[idx]; !notPresent {
			b.Branches[idx] = b.NewBranch()
		}
		// check whether the idx itself marks an End $. if so add a new idx
		addedBranch = b.Branches[idx].add(tail[1:])
	} else {
		// if there is nothing else to be pushed down we just have to mark the
		// current branch as an end. this happens when you add a value that already
		// is covered by the index but this particular end had not been marked.
		// eg. you already have 'food' and 'foot' (shared LeafValue of 'foo') in
		// your index and now add 'foo'.
		b.setEnd(true)
		addedBranch = b
	}
	return addedBranch
}
1072973
func (m *Model) AddBranch(branchName, userName string) error {
	return errors.Trace(m.st.AddBranch(branchName, userName))
}
214632
func main() {
	CheckArgs("<path>")
	path := os.Args[1]

	// We instance\iate a new repository targeting the given path (the .git folder)
	r, err := git.PlainOpen(path)
	CheckIfError(err)

	// Get the working directory for the repository
	w, err := r.Worktree()
	CheckIfError(err)

	// Pull the latest changes from the origin remote and merge into the current branch
	Info("git pull origin")
	err = w.Pull(&git.PullOptions{RemoteName: "origin"})
	CheckIfError(err)

	// Print the latest commit that was just pulled
	ref, err := r.Head()
	CheckIfError(err)
	commit, err := r.CommitObject(ref.Hash())
	CheckIfError(err)

	fmt.Println(commit)
}
857924
func (api *API) BranchInfo(args params.BranchInfoArgs) (params.GenerationResults, error) {
	result := params.GenerationResults{}

	isModelAdmin, err := api.hasAdminAccess()
	if err != nil {
		return result, errors.Trace(err)
	}
	if !isModelAdmin && !api.isControllerAdmin {
		return result, common.ErrPerm
	}

	// From clients, we expect a single branch name or none,
	// but we accommodate any number - they all must exist to avoid an error.
	// If no branch is supplied, get them all.
	var branches []Generation
	if len(args.BranchNames) > 0 {
		branches = make([]Generation, len(args.BranchNames))
		for i, name := range args.BranchNames {
			if branches[i], err = api.model.Branch(name); err != nil {
				return generationInfoError(err)
			}
		}
	} else {
		if branches, err = api.model.Branches(); err != nil {
			return generationInfoError(err)
		}
	}

	results := make([]params.Generation, len(branches))
	for i, b := range branches {
		if results[i], err = api.oneBranchInfo(b, args.Detailed); err != nil {
			return generationInfoError(err)
		}
	}
	result.Generations = results
	return result, nil
}
1292076
func New(branch int) (generator.Generator, error) {
	if branch < 1 {
		return nil, fmt.Errorf("invalid branch factor %d", branch)
	}
	nt, err := node.NewType("/tn")
	if err != nil {
		return nil, err
	}
	p, err := predicate.NewImmutable("parent_of")
	if err != nil {
		return nil, err
	}
	return &treeGenerator{
		branch:    branch,
		nodeType:  nt,
		predicate: p,
	}, nil
}
1353082
func (g *Git) NewBranch(branch string) *command.Model {
	return g.command("checkout", "-b", branch)
}
357442
func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
	if err := git.Checkout(localPath, git.CheckoutOptions{
		Timeout:   time.Duration(setting.Git.Timeout.Pull) * time.Second,
		Branch:    newBranch,
		OldBranch: oldBranch,
	}); err != nil {
		return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
	}
	return nil
}
973867
func (c *checkoutCommand) Run(ctx *cmd.Context) error {
	// If the active branch is not being set to the (default) master,
	// then first ensure that a branch with the supplied name exists.
	if c.branchName != model.GenerationMaster {
		client, err := c.getAPI()
		if err != nil {
			return err
		}
		defer func() { _ = client.Close() }()

		hasBranch, err := client.HasActiveBranch(c.branchName)
		if err != nil {
			return errors.Annotate(err, "checking for active branch")
		}
		if !hasBranch {
			return errors.Errorf("this model has no active branch %q", c.branchName)
		}
	}

	if err := c.SetActiveBranch(c.branchName); err != nil {
		return err
	}
	msg := fmt.Sprintf("Active branch set to %q\n", c.branchName)
	_, err := ctx.Stdout.Write([]byte(msg))
	return err
}
950836
func (s *GetBranchOutput) SetBranch(v *Branch) *GetBranchOutput {
	s.Branch = v
	return s
}
149781
func main() {
	CheckArgs("<url>", "<directory>")
	url, directory := os.Args[1], os.Args[2]

	// Clone the given repository to the given directory
	Info("git clone %s %s", url, directory)
	r, err := git.PlainClone(directory, false, &git.CloneOptions{
		URL: url,
	})
	CheckIfError(err)

	// Create a new branch to the current HEAD
	Info("git branch my-branch")

	headRef, err := r.Head()
	CheckIfError(err)

	// Create a new plumbing.HashReference object with the name of the branch
	// and the hash from the HEAD. The reference name should be a full reference
	// name and not an abbreviated one, as is used on the git cli.
	//
	// For tags we should use `refs/tags/%s` instead of `refs/heads/%s` used
	// for branches.
	ref := plumbing.NewHashReference("refs/heads/my-branch", headRef.Hash())

	// The created reference is saved in the storage.
	err = r.Storer.SetReference(ref)
	CheckIfError(err)

	// Or deleted from it.
	Info("git branch -D my-branch")
	err = r.Storer.RemoveReference(ref.Name())
	CheckIfError(err)
}
398827
func main() {
	CheckArgs("<path>", "<revision>")

	path := os.Args[1]
	revision := os.Args[2]

	// We instantiate a new repository targeting the given path (the .git folder)
	r, err := git.PlainOpen(path)
	CheckIfError(err)

	// Resolve revision into a sha1 commit, only some revisions are resolved
	// look at the doc to get more details
	Info("git rev-parse %s", revision)

	h, err := r.ResolveRevision(plumbing.Revision(revision))

	CheckIfError(err)

	fmt.Println(h.String())
}
564808
func (b *Branch) NewBranch() *Branch {
	return &Branch{
		Branches: make(map[byte]*Branch),
		Count:    0,
	}
}
924710
func Compare(local, global HashSet) HashSet {
	missingLocal := make(map[string]bool)
	log.Info("comparing branches: local [", len(local), "] <-> global [", len(global), "]")
	for _, e := range global {
		missingLocal[e.String()] = true
	}
	for _, e := range local {
		if _, ok := missingLocal[e.String()]; ok {
			missingLocal[e.String()] = false
		}
	}
	missingFiles := make(HashSet, 0)
	for _, e := range global {
		if missingLocal[e.String()] {
			missingFiles = append(missingFiles, e)
		}
	}
	log.Notice("missing in local branch:", len(missingFiles))

	return missingFiles
}
288544
func (ini *Init) Do() (interface{}, error) {
	if ini == nil {
		panic("init is nil")
	}

	ini.init.Do(func() {
		prev := ini.prev
		if prev != nil {
			// A branch. Initialize the ancestors.
			if prev.shouldInitialize() {
				_, err := prev.Do()
				if err != nil {
					ini.err = err
					return
				}
			} else if prev.inProgress() {
				// Concurrent initialization. The following init func
				// may depend on earlier state, so wait.
				prev.wait()
			}
		}

		if ini.f != nil {
			ini.out, ini.err = ini.f()
		}

		for _, child := range ini.children {
			if child.shouldInitialize() {
				_, err := child.Do()
				if err != nil {
					ini.err = err
					return
				}
			}
		}
	})

	ini.wait()

	return ini.out, ini.err

}
1015962
func (r *rewinder) Lookahead() *rewinder {
	clone := &rewinder{}
	*clone = *r
	clone.limit = clone.cursor
	return clone
}
693668
func (s *Action) GitPush(ctx context.Context, c *cli.Context) error {
	store := c.String("store")
	origin := c.Args().Get(0)
	branch := c.Args().Get(1)

	if origin == "" || branch == "" {
		return ExitError(ctx, ExitUsage, nil, "Usage: %s git push <ORIGIN> <BRANCH>", s.Name)
	}
	return s.Store.GitPush(ctx, store, origin, branch)
}