Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mmontes11
Copy link
Contributor

Fixes #1886

@mmontes11
Copy link
Contributor Author

mmontes11 commented Sep 21, 2020

I have a suggestion for defaults.go, specifically for Branch :

		Branch: func() (string, error) {
			currentBranch, err := git.CurrentBranch()
			if err != nil {
				return "", fmt.Errorf("could not determine current branch: %w", err)
			}
			return currentBranch, nil
		},

It could be useful to return the error directly in this case instead of formatting it:

		Branch: func() (string, error) {
			return git.CurrentBranch();
		},

This will allow us to handle the error like this:

		currentBranch, err := opts.Branch()
		if err != nil && !errors.Is(err, git.ErrNotOnAnyBranch) {
			return err
		}

@vilmibm vilmibm added checks and removed checks labels Sep 22, 2020
@edualb
Copy link
Contributor

edualb commented Sep 22, 2020

Hello, I would like to suggest another approaching.

Into the git/git.go file there is a function CurrentBranch():

func CurrentBranch() (string, error) {
	refCmd := GitCommand("symbolic-ref", "--quiet", "--short", "HEAD")

	output, err := run.PrepareCmd(refCmd).Output()
	if err == nil {
		// Found the branch name
		return firstLine(output), nil
	}

	var cmdErr *run.CmdError
	if errors.As(err, &cmdErr) {
		if cmdErr.Stderr.Len() == 0 {
			// Detached head
			return "", ErrNotOnAnyBranch
		}
	}

	// Unknown error
	return "", err
}

If you are in the HEAD branch and put the git symbolic-ref --quiet HEAD, you will get an empty output, and that is the error. Instead you have to use git rev-parse --abbrev-ref HEAD, when put this command you will get the branch name on output.

I suggest this:

func CurrentBranch() (string, error) {
	refCmd := GitCommand("rev-parse", "--abbrev-ref", "HEAD") // Change here

	output, err := run.PrepareCmd(refCmd).Output()
	if err == nil {
		// Found the branch name
		return firstLine(output), nil
	}

	var cmdErr *run.CmdError
	if errors.As(err, &cmdErr) {
		if cmdErr.Stderr.Len() == 0 {
			// Detached head
			return "", ErrNotOnAnyBranch
		}
	}

	// Unknown error
	return "", err
}

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks great. Thank you!!

@mislav
Copy link
Contributor

mislav commented Sep 22, 2020

@mmontes11 errors.Is(err, git.ErrNotOnAnyBranch) will also work even if the original error was wrapped.

@edualb The current PR takes care of the issue, but if you think that there are potential improvements to CurrentBranch, please open a separate PR! Be sure to reference the issue that the change is fixing. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"gr pr checkout" fails because HEAD is not on a branch

4 participants