How do I checkout a branch in a similar way than the CLI does? #948

Open
opened 2023-03-07 12:08:46 -06:00 by pablocompagni-contractorvp · 1 comment
pablocompagni-contractorvp commented 2023-03-07 12:08:46 -06:00 (Migrated from github.com)

Looking at this issue it seems this should be a branch switch and then a checkout, but I just can't figure out how to do it. https://github.com/libgit2/git2go/issues/291

I've tried doing this:

func CheckoutBranch(r *git.Repository, b *git.Branch) error {
	err := r.SetHead(b.Reference.Name())
	if err != nil {
		return err
	}
	return nil
}

and this:

func CheckoutBranch(r *git.Repository, b *git.Branch) error {
	err := r.SetHead(b.Reference.Name())
	if err != nil {
		return err
	}

	return r.CheckoutHead(&git.CheckoutOptions{
		Strategy: git.CheckoutForce,
	})
}

But they both end up with a detached head. I've also tried, based on the suggestions from the other ticket, to do this:

func CheckoutBranch(r *git.Repository, b *git.Branch) error {
	err := r.SetHead(b.Reference.Name())
	if err != nil {
		return err
	}
	tree, err := r.LookupTree(b.Target())
	if err != nil {
		return err
	}
	
	return r.CheckoutTree(tree, &git.CheckoutOptions{
		Strategy: git.CheckoutForce,
	})
}

But this just throws and error.

Looking at this issue it seems this should be a branch switch and then a checkout, but I just can't figure out how to do it. https://github.com/libgit2/git2go/issues/291 I've tried doing this: ```go func CheckoutBranch(r *git.Repository, b *git.Branch) error { err := r.SetHead(b.Reference.Name()) if err != nil { return err } return nil } ``` and this: ```go func CheckoutBranch(r *git.Repository, b *git.Branch) error { err := r.SetHead(b.Reference.Name()) if err != nil { return err } return r.CheckoutHead(&git.CheckoutOptions{ Strategy: git.CheckoutForce, }) } ``` But they both end up with a detached head. I've also tried, based on the suggestions from the other ticket, to do this: ```go func CheckoutBranch(r *git.Repository, b *git.Branch) error { err := r.SetHead(b.Reference.Name()) if err != nil { return err } tree, err := r.LookupTree(b.Target()) if err != nil { return err } return r.CheckoutTree(tree, &git.CheckoutOptions{ Strategy: git.CheckoutForce, }) } ``` But this just throws and error.
liuqianhong6007 commented 2023-03-13 01:48:23 -05:00 (Migrated from github.com)

I do this as following

func Checkout(repo *git.Repository, branch *git.Branch) error {
	branchName, err := branch.Name()
	if err != nil {
		return err
	}

	commit, err := repo.LookupCommit(branch.Target())
	if err != nil {
		return err
	}
	defer commit.Free()

	tree, err := repo.LookupTree(commit.TreeId())
	if err != nil {
		return err
	}
	defer tree.Free()

	err = repo.CheckoutTree(tree, &git.CheckoutOptions{
		Strategy: git.CheckoutForce,
	})
	if err != nil {
		return err
	}

	err = repo.SetHead("refs/heads/" + branchName)
	if err != nil {
		return err
	}
	return nil
}
I do this as following ``` func Checkout(repo *git.Repository, branch *git.Branch) error { branchName, err := branch.Name() if err != nil { return err } commit, err := repo.LookupCommit(branch.Target()) if err != nil { return err } defer commit.Free() tree, err := repo.LookupTree(commit.TreeId()) if err != nil { return err } defer tree.Free() err = repo.CheckoutTree(tree, &git.CheckoutOptions{ Strategy: git.CheckoutForce, }) if err != nil { return err } err = repo.SetHead("refs/heads/" + branchName) if err != nil { return err } return nil } ```
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: jcarr/git2go#948
No description provided.