Skip to content

Commit 1b6feab

Browse files
committed
Fixes #1428 - gets HEAD name
1 parent 58fb823 commit 1b6feab

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1818

1919
### Fixed
2020

21+
- Fixes [1428](https://github.com/eamodio/vscode-gitlens/issues/1428) - Incorrect branch name when no commits exist on new repo
2122
- Fixes [1444](https://github.com/eamodio/vscode-gitlens/issues/1444) - File history view "Open Changes with Working File" does not work for the very first commit
2223
- Fixes [1448](https://github.com/eamodio/vscode-gitlens/issues/1448) - Hashes (#) are percent encoded in custom remote urls
2324
- Fixes [1447](https://github.com/eamodio/vscode-gitlens/issues/1447) - _Open File on Remote From..._ is missing remote branches

src/git/git.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,14 @@ export namespace Git {
10621062
return data.length === 0 ? undefined : data.trim();
10631063
}
10641064

1065+
export function ls_remote(repoPath: string, remote: string, ref?: string) {
1066+
return git<string>({ cwd: repoPath }, 'ls-remote', remote, ref);
1067+
}
1068+
1069+
export function ls_remote__HEAD(repoPath: string, remote: string) {
1070+
return git<string>({ cwd: repoPath }, 'ls-remote', '--symref', remote, 'HEAD');
1071+
}
1072+
10651073
export async function ls_tree(repoPath: string, ref: string, { fileName }: { fileName?: string } = {}) {
10661074
const params = ['ls-tree'];
10671075
if (fileName) {
@@ -1207,6 +1215,29 @@ export namespace Git {
12071215
return [ex.stdout, undefined];
12081216
}
12091217

1218+
try {
1219+
const data = await symbolic_ref(repoPath, 'HEAD');
1220+
if (data != null) return [data.trim(), undefined];
1221+
} catch {}
1222+
1223+
try {
1224+
const data = await symbolic_ref(repoPath, 'refs/remotes/origin/HEAD');
1225+
if (data != null) return [data.trim().substr('origin/'.length), undefined];
1226+
} catch (ex) {
1227+
if (/is not a symbolic ref/.test(ex.stderr)) {
1228+
try {
1229+
const data = await ls_remote__HEAD(repoPath, 'origin');
1230+
if (data != null) {
1231+
const match = /ref:\s(\S+)\s+HEAD/m.exec(data);
1232+
if (match != null) {
1233+
const [, branch] = match;
1234+
return [branch.substr('refs/heads/'.length), undefined];
1235+
}
1236+
}
1237+
} catch {}
1238+
}
1239+
}
1240+
12101241
const defaultBranch = (await config__get('init.defaultBranch', repoPath, { local: true })) ?? 'main';
12111242
const branchConfig = await config__get_regex(`branch\\.${defaultBranch}\\.+`, repoPath, {
12121243
local: true,
@@ -1493,6 +1524,10 @@ export namespace Git {
14931524
);
14941525
}
14951526

1527+
export function symbolic_ref(repoPath: string, ref: string) {
1528+
return git<string>({ cwd: repoPath }, 'symbolic-ref', '--short', ref);
1529+
}
1530+
14961531
export function tag(repoPath: string) {
14971532
return git<string>({ cwd: repoPath }, 'tag', '-l', `--format=${GitTagParser.defaultFormat}`);
14981533
}

src/git/gitService.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,43 @@ export class GitService implements Disposable {
16081608
return user;
16091609
}
16101610

1611+
@log()
1612+
async getDefaultBranchName(repoPath: string | undefined, remote?: string): Promise<string | undefined> {
1613+
if (repoPath == null) return undefined;
1614+
1615+
if (!remote) {
1616+
try {
1617+
const data = await Git.symbolic_ref(repoPath, 'HEAD');
1618+
if (data != null) return data.trim();
1619+
} catch {}
1620+
}
1621+
1622+
remote = remote ?? 'origin';
1623+
try {
1624+
const data = await Git.symbolic_ref(repoPath, `refs/remotes/${remote}/HEAD`);
1625+
if (data == null) return undefined;
1626+
1627+
return data.trim().substr(`${remote}/`.length);
1628+
} catch (ex) {
1629+
if (/is not a symbolic ref/.test(ex.stderr)) {
1630+
try {
1631+
const data = await Git.ls_remote__HEAD(repoPath, remote);
1632+
if (data == null) return undefined;
1633+
1634+
const match = /ref:\s(\S+)\s+HEAD/m.exec(data);
1635+
if (match == null) return undefined;
1636+
1637+
const [, branch] = match;
1638+
return branch.substr('refs/heads/'.length);
1639+
} catch {
1640+
return undefined;
1641+
}
1642+
}
1643+
1644+
return undefined;
1645+
}
1646+
}
1647+
16111648
@log()
16121649
async getDiffForFile(
16131650
uri: GitUri,

src/git/models/branch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class GitBranch implements GitBranchReference {
134134
this.name = GitBranch.formatDetached(this.sha!);
135135
}
136136

137-
this.upstream = upstream == null || upstream.name.length === 0 ? undefined : upstream;
137+
this.upstream = upstream?.name == null || upstream.name.length === 0 ? undefined : upstream;
138138
this.state = {
139139
ahead: ahead,
140140
behind: behind,

0 commit comments

Comments
 (0)