mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-15 17:25:33 -05:00
Reworks branch parsing to include tracking info
Reworks current branch retrieval for better performance
This commit is contained in:
@@ -10,6 +10,7 @@ import * as iconv from 'iconv-lite';
|
||||
export { IGit };
|
||||
export * from './models/models';
|
||||
export * from './parsers/blameParser';
|
||||
export * from './parsers/branchParser';
|
||||
export * from './parsers/diffParser';
|
||||
export * from './parsers/logParser';
|
||||
export * from './parsers/stashParser';
|
||||
@@ -33,10 +34,11 @@ const GitWarnings = [
|
||||
/no such path/,
|
||||
/does not have any commits/,
|
||||
/Path \'.*?\' does not exist in/,
|
||||
/Path \'.*?\' exists on disk, but not in/
|
||||
/Path \'.*?\' exists on disk, but not in/,
|
||||
/no upstream configured for branch/
|
||||
];
|
||||
|
||||
async function gitCommand(options: { cwd: string, encoding?: string }, ...args: any[]) {
|
||||
async function gitCommand(options: { cwd: string, encoding?: string, onError?: (ex: Error) => string | undefined }, ...args: any[]) {
|
||||
try {
|
||||
// Fixes https://github.com/eamodio/vscode-gitlens/issues/73
|
||||
// See https://stackoverflow.com/questions/4144417/how-to-handle-asian-characters-in-file-names-in-git-on-os-x
|
||||
@@ -50,6 +52,11 @@ async function gitCommand(options: { cwd: string, encoding?: string }, ...args:
|
||||
return iconv.decode(Buffer.from(s, 'binary'), opts.encoding);
|
||||
}
|
||||
catch (ex) {
|
||||
if (options.onError !== undefined) {
|
||||
const result = options.onError(ex);
|
||||
if (result !== undefined) return result;
|
||||
}
|
||||
|
||||
const msg = ex && ex.toString();
|
||||
if (msg) {
|
||||
for (const warning of GitWarnings) {
|
||||
@@ -168,15 +175,27 @@ export class Git {
|
||||
return gitCommand({ cwd: root }, ...params, `--`, file);
|
||||
}
|
||||
|
||||
static branch(repoPath: string, all: boolean) {
|
||||
const params = [`branch`];
|
||||
if (all) {
|
||||
static branch(repoPath: string, options: { all: boolean } = { all: false }) {
|
||||
const params = [`branch`, `-vv`];
|
||||
if (options.all) {
|
||||
params.push(`-a`);
|
||||
}
|
||||
|
||||
return gitCommand({ cwd: repoPath }, ...params);
|
||||
}
|
||||
|
||||
static branch_current(repoPath: string) {
|
||||
const params = [`rev-parse`, `--abbrev-ref`, `--symbolic-full-name`, `@`, `@{u}`];
|
||||
const onError = (ex: Error) => {
|
||||
if (/no upstream configured for branch/.test(ex && ex.toString())) {
|
||||
return ex.message.split('\n')[0];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
return gitCommand({ cwd: repoPath, onError: onError }, ...params);
|
||||
}
|
||||
|
||||
static checkout(repoPath: string, fileName: string, sha: string) {
|
||||
const [file, root] = Git.splitPath(fileName, repoPath);
|
||||
|
||||
|
||||
@@ -5,15 +5,9 @@ export class GitBranch {
|
||||
current: boolean;
|
||||
name: string;
|
||||
remote: boolean;
|
||||
tracking?: string;
|
||||
|
||||
constructor(branch: string) {
|
||||
branch = branch.trim();
|
||||
|
||||
if (branch.startsWith('* ')) {
|
||||
branch = branch.substring(2);
|
||||
this.current = true;
|
||||
}
|
||||
|
||||
constructor(public readonly repoPath: string, branch: string, current: boolean = false, tracking?: string) {
|
||||
if (branch.startsWith('remotes/')) {
|
||||
branch = branch.substring(8);
|
||||
this.remote = true;
|
||||
@@ -24,6 +18,24 @@ export class GitBranch {
|
||||
branch = branch.substring(0, index);
|
||||
}
|
||||
|
||||
this.current = current;
|
||||
this.name = branch;
|
||||
this.tracking = tracking;
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return this.remote
|
||||
? this.name.substring(this.name.indexOf('/') + 1)
|
||||
: this.name;
|
||||
}
|
||||
|
||||
getRemote(): string | undefined {
|
||||
if (this.remote) return GitBranch.getRemote(this.name);
|
||||
if (this.tracking !== undefined) return GitBranch.getRemote(this.tracking);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
static getRemote(branch: string): string {
|
||||
return branch.substring(0, branch.indexOf('/'));
|
||||
}
|
||||
}
|
||||
24
src/git/parsers/branchParser.ts
Normal file
24
src/git/parsers/branchParser.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
import { GitBranch } from './../git';
|
||||
const branchWithTrackingRegex = /^(\*?)\s+(.+?)\s+([0-9,a-f]+)\s+(?:\[(.*?\/.*?)(?:\:|\]))?/gm;
|
||||
|
||||
export class GitBranchParser {
|
||||
|
||||
static parse(data: string, repoPath: string): GitBranch[] | undefined {
|
||||
if (!data) return undefined;
|
||||
|
||||
const branches: GitBranch[] = [];
|
||||
|
||||
let match: RegExpExecArray | null = null;
|
||||
do {
|
||||
match = branchWithTrackingRegex.exec(data);
|
||||
if (match == null) break;
|
||||
|
||||
branches.push(new GitBranch(repoPath, match[2], match[1] === '*', match[4]));
|
||||
} while (match != null);
|
||||
|
||||
if (!branches.length) return undefined;
|
||||
|
||||
return branches;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user