Browse Source

Fixes #1428 - gets HEAD name

main
Eric Amodio 3 years ago
parent
commit
1b6feab735
4 changed files with 74 additions and 1 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +35
    -0
      src/git/git.ts
  3. +37
    -0
      src/git/gitService.ts
  4. +1
    -1
      src/git/models/branch.ts

+ 1
- 0
CHANGELOG.md View File

@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
### Fixed
- Fixes [1428](https://github.com/eamodio/vscode-gitlens/issues/1428) - Incorrect branch name when no commits exist on new repo
- 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
- Fixes [1448](https://github.com/eamodio/vscode-gitlens/issues/1448) - Hashes (#) are percent encoded in custom remote urls
- Fixes [1447](https://github.com/eamodio/vscode-gitlens/issues/1447) - _Open File on Remote From..._ is missing remote branches

+ 35
- 0
src/git/git.ts View File

@ -1062,6 +1062,14 @@ export namespace Git {
return data.length === 0 ? undefined : data.trim();
}
export function ls_remote(repoPath: string, remote: string, ref?: string) {
return git<string>({ cwd: repoPath }, 'ls-remote', remote, ref);
}
export function ls_remote__HEAD(repoPath: string, remote: string) {
return git<string>({ cwd: repoPath }, 'ls-remote', '--symref', remote, 'HEAD');
}
export async function ls_tree(repoPath: string, ref: string, { fileName }: { fileName?: string } = {}) {
const params = ['ls-tree'];
if (fileName) {
@ -1207,6 +1215,29 @@ export namespace Git {
return [ex.stdout, undefined];
}
try {
const data = await symbolic_ref(repoPath, 'HEAD');
if (data != null) return [data.trim(), undefined];
} catch {}
try {
const data = await symbolic_ref(repoPath, 'refs/remotes/origin/HEAD');
if (data != null) return [data.trim().substr('origin/'.length), undefined];
} catch (ex) {
if (/is not a symbolic ref/.test(ex.stderr)) {
try {
const data = await ls_remote__HEAD(repoPath, 'origin');
if (data != null) {
const match = /ref:\s(\S+)\s+HEAD/m.exec(data);
if (match != null) {
const [, branch] = match;
return [branch.substr('refs/heads/'.length), undefined];
}
}
} catch {}
}
}
const defaultBranch = (await config__get('init.defaultBranch', repoPath, { local: true })) ?? 'main';
const branchConfig = await config__get_regex(`branch\\.${defaultBranch}\\.+`, repoPath, {
local: true,
@ -1493,6 +1524,10 @@ export namespace Git {
);
}
export function symbolic_ref(repoPath: string, ref: string) {
return git<string>({ cwd: repoPath }, 'symbolic-ref', '--short', ref);
}
export function tag(repoPath: string) {
return git<string>({ cwd: repoPath }, 'tag', '-l', `--format=${GitTagParser.defaultFormat}`);
}

+ 37
- 0
src/git/gitService.ts View File

@ -1609,6 +1609,43 @@ export class GitService implements Disposable {
}
@log()
async getDefaultBranchName(repoPath: string | undefined, remote?: string): Promise<string | undefined> {
if (repoPath == null) return undefined;
if (!remote) {
try {
const data = await Git.symbolic_ref(repoPath, 'HEAD');
if (data != null) return data.trim();
} catch {}
}
remote = remote ?? 'origin';
try {
const data = await Git.symbolic_ref(repoPath, `refs/remotes/${remote}/HEAD`);
if (data == null) return undefined;
return data.trim().substr(`${remote}/`.length);
} catch (ex) {
if (/is not a symbolic ref/.test(ex.stderr)) {
try {
const data = await Git.ls_remote__HEAD(repoPath, remote);
if (data == null) return undefined;
const match = /ref:\s(\S+)\s+HEAD/m.exec(data);
if (match == null) return undefined;
const [, branch] = match;
return branch.substr('refs/heads/'.length);
} catch {
return undefined;
}
}
return undefined;
}
}
@log()
async getDiffForFile(
uri: GitUri,
ref1: string | undefined,

+ 1
- 1
src/git/models/branch.ts View File

@ -134,7 +134,7 @@ export class GitBranch implements GitBranchReference {
this.name = GitBranch.formatDetached(this.sha!);
}
this.upstream = upstream == null || upstream.name.length === 0 ? undefined : upstream;
this.upstream = upstream?.name == null || upstream.name.length === 0 ? undefined : upstream;
this.state = {
ahead: ahead,
behind: behind,

Loading…
Cancel
Save