Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review GitModule.GetSelectedBranchFast #4707

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2755,27 +2755,32 @@ public static string GetSelectedBranchFast(string repositoryPath)
return string.Empty;
}

string head;
string headFileName = Path.Combine(GetGitDirectory(repositoryPath), "HEAD");
if (File.Exists(headFileName))
// eg. "/path/to/repo/.git/HEAD"
var headFileName = Path.Combine(GetGitDirectory(repositoryPath), "HEAD");

if (!File.Exists(headFileName))
{
head = File.ReadAllText(headFileName, SystemEncoding);
if (!head.Contains("ref:"))
{
return DetachedBranch;
}
return string.Empty;
}
else

var headFileContents = File.ReadAllText(headFileName, SystemEncoding);

// eg. "ref: refs/heads/master"
// "9601551c564b48208bccd50b705264e9bd68140d"

if (!headFileContents.StartsWith("ref: "))
{
return string.Empty;
return DetachedBranch;
}

if (!string.IsNullOrEmpty(head))
const string prefix = "ref: refs/heads/";

if (!headFileContents.StartsWith(prefix))
{
return head.Replace("ref:", "").Replace("refs/heads/", string.Empty).Trim();
return string.Empty;
}

return string.Empty;
return headFileContents.Substring(prefix.Length).TrimEnd();
}

/// <summary>Gets the current branch; or "(no branch)" if HEAD is detached.</summary>
Expand Down