mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-16 10:58:34 -05:00
Renames git methods to align better with git commands
This commit is contained in:
@@ -41,7 +41,7 @@ export async function activate(context: ExtensionContext) {
|
|||||||
|
|
||||||
let repoPath: string;
|
let repoPath: string;
|
||||||
try {
|
try {
|
||||||
repoPath = await Git.repoPath(rootPath, gitPath);
|
repoPath = await Git.getRepoPath(rootPath, gitPath);
|
||||||
}
|
}
|
||||||
catch (ex) {
|
catch (ex) {
|
||||||
Logger.error(ex);
|
Logger.error(ex);
|
||||||
|
|||||||
231
src/git/git.ts
231
src/git/git.ts
@@ -11,10 +11,8 @@ export * from './enrichers/blameParserEnricher';
|
|||||||
export * from './enrichers/logParserEnricher';
|
export * from './enrichers/logParserEnricher';
|
||||||
|
|
||||||
let git: IGit;
|
let git: IGit;
|
||||||
const UncommittedRegex = /^[0]+$/;
|
|
||||||
const ShaRegex = /\b[0-9a-f]{40}\b/;
|
|
||||||
|
|
||||||
const DefaultLogParams = [`log`, `--name-status`, `--full-history`, `-m`, `--date=iso8601-strict`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparent %P%nsummary %B%nfilename ?`];
|
const DefaultLogParams = [`log`, `--name-status`, `--full-history`, `-M`, `--date=iso8601-strict`, `--format=%H -%nauthor %an%nauthor-date %ai%ncommitter %cn%ncommitter-date %ci%nparent %P%nsummary %B%nfilename ?`];
|
||||||
|
|
||||||
async function gitCommand(cwd: string, ...args: any[]) {
|
async function gitCommand(cwd: string, ...args: any[]) {
|
||||||
try {
|
try {
|
||||||
@@ -43,25 +41,14 @@ export const GitBlameFormat = {
|
|||||||
|
|
||||||
export class Git {
|
export class Git {
|
||||||
|
|
||||||
static normalizePath(fileName: string, repoPath?: string) {
|
static ShaRegex = /^[0-9a-f]{40}( -)?$/;
|
||||||
return fileName.replace(/\\/g, '/');
|
static UncommittedRegex = /^[0]+$/;
|
||||||
|
|
||||||
|
static gitInfo(): IGit {
|
||||||
|
return git;
|
||||||
}
|
}
|
||||||
|
|
||||||
static splitPath(fileName: string, repoPath?: string): [string, string] {
|
static async getRepoPath(cwd: string, gitPath?: string) {
|
||||||
if (repoPath) {
|
|
||||||
return [
|
|
||||||
fileName.replace(repoPath.endsWith('/') ? repoPath : `${repoPath}/`, ''),
|
|
||||||
repoPath
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
path.basename(fileName).replace(/\\/g, '/'),
|
|
||||||
path.dirname(fileName).replace(/\\/g, '/')
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
static async repoPath(cwd: string, gitPath?: string) {
|
|
||||||
git = await findGitPath(gitPath);
|
git = await findGitPath(gitPath);
|
||||||
Logger.log(`Git found: ${git.version} @ ${git.path === 'git' ? 'PATH' : git.path}`);
|
Logger.log(`Git found: ${git.version} @ ${git.path === 'git' ? 'PATH' : git.path}`);
|
||||||
|
|
||||||
@@ -70,51 +57,8 @@ export class Git {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static blame(format: GitBlameFormat, fileName: string, sha?: string, repoPath?: string) {
|
|
||||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
|
||||||
|
|
||||||
const params = [`blame`, `--root`, format];
|
|
||||||
if (sha) {
|
|
||||||
params.push(sha);
|
|
||||||
}
|
|
||||||
|
|
||||||
return gitCommand(root, ...params, `--`, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static blameLines(format: GitBlameFormat, fileName: string, startLine: number, endLine: number, sha?: string, repoPath?: string) {
|
|
||||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
|
||||||
|
|
||||||
const params = [`blame`, `--root`, format, `-L ${startLine},${endLine}`];
|
|
||||||
if (sha) {
|
|
||||||
params.push(sha);
|
|
||||||
}
|
|
||||||
|
|
||||||
return gitCommand(root, ...params, `--`, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static diffDir(repoPath: string, sha1: string, sha2?: string) {
|
|
||||||
const params = [`difftool`, `--dir-diff`, sha1];
|
|
||||||
if (sha2) {
|
|
||||||
params.push(sha2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return gitCommand(repoPath, ...params);
|
|
||||||
}
|
|
||||||
|
|
||||||
static diffStatus(repoPath: string, sha1?: string, sha2?: string) {
|
|
||||||
const params = [`diff`, `--name-status`, `-M`];
|
|
||||||
if (sha1) {
|
|
||||||
params.push(sha1);
|
|
||||||
}
|
|
||||||
if (sha2) {
|
|
||||||
params.push(sha2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return gitCommand(repoPath, ...params);
|
|
||||||
}
|
|
||||||
|
|
||||||
static async getVersionedFile(fileName: string, repoPath: string, branchOrSha: string) {
|
static async getVersionedFile(fileName: string, repoPath: string, branchOrSha: string) {
|
||||||
const data = await Git.getVersionedFileText(fileName, repoPath, branchOrSha);
|
const data = await Git.show(repoPath, fileName, branchOrSha);
|
||||||
|
|
||||||
const suffix = Git.isSha(branchOrSha) ? branchOrSha.substring(0, 8) : branchOrSha;
|
const suffix = Git.isSha(branchOrSha) ? branchOrSha.substring(0, 8) : branchOrSha;
|
||||||
const ext = path.extname(fileName);
|
const ext = path.extname(fileName);
|
||||||
@@ -139,7 +83,72 @@ export class Git {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static getVersionedFileText(fileName: string, repoPath: string, branchOrSha: string) {
|
static isSha(sha: string) {
|
||||||
|
return Git.ShaRegex.test(sha);
|
||||||
|
}
|
||||||
|
|
||||||
|
static isUncommitted(sha: string) {
|
||||||
|
return Git.UncommittedRegex.test(sha);
|
||||||
|
}
|
||||||
|
|
||||||
|
static normalizePath(fileName: string, repoPath?: string) {
|
||||||
|
return fileName.replace(/\\/g, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
static splitPath(fileName: string, repoPath?: string): [string, string] {
|
||||||
|
if (repoPath) {
|
||||||
|
return [
|
||||||
|
fileName.replace(repoPath.endsWith('/') ? repoPath : `${repoPath}/`, ''),
|
||||||
|
repoPath
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
path.basename(fileName).replace(/\\/g, '/'),
|
||||||
|
path.dirname(fileName).replace(/\\/g, '/')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Git commands
|
||||||
|
|
||||||
|
static blame(repoPath: string, fileName: string, format: GitBlameFormat, sha?: string, startLine?: number, endLine?: number) {
|
||||||
|
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||||
|
|
||||||
|
const params = [`blame`, `--root`, format];
|
||||||
|
|
||||||
|
if (startLine != null && endLine != null) {
|
||||||
|
params.push(`-L ${startLine},${endLine}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sha) {
|
||||||
|
params.push(sha);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gitCommand(root, ...params, `--`, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static diff_nameStatus(repoPath: string, sha1?: string, sha2?: string) {
|
||||||
|
const params = [`diff`, `--name-status`, `-M`];
|
||||||
|
if (sha1) {
|
||||||
|
params.push(sha1);
|
||||||
|
}
|
||||||
|
if (sha2) {
|
||||||
|
params.push(sha2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gitCommand(repoPath, ...params);
|
||||||
|
}
|
||||||
|
|
||||||
|
static difftool_dirDiff(repoPath: string, sha1: string, sha2?: string) {
|
||||||
|
const params = [`difftool`, `--dir-diff`, sha1];
|
||||||
|
if (sha2) {
|
||||||
|
params.push(sha2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return gitCommand(repoPath, ...params);
|
||||||
|
}
|
||||||
|
|
||||||
|
static show(repoPath: string, fileName: string, branchOrSha: string) {
|
||||||
const [file, root] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
const [file, root] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||||
branchOrSha = branchOrSha.replace('^', '');
|
branchOrSha = branchOrSha.replace('^', '');
|
||||||
|
|
||||||
@@ -147,49 +156,7 @@ export class Git {
|
|||||||
return gitCommand(root, 'show', `${branchOrSha}:./${file}`);
|
return gitCommand(root, 'show', `${branchOrSha}:./${file}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gitInfo(): IGit {
|
static log(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) {
|
||||||
return git;
|
|
||||||
}
|
|
||||||
|
|
||||||
static log(fileName: string, sha?: string, repoPath?: string, maxCount?: number, reverse: boolean = false) {
|
|
||||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
|
||||||
|
|
||||||
const params = [...DefaultLogParams, `--follow`, `--no-merges`];
|
|
||||||
if (maxCount && !reverse) {
|
|
||||||
params.push(`-n${maxCount}`);
|
|
||||||
}
|
|
||||||
if (sha) {
|
|
||||||
if (reverse) {
|
|
||||||
params.push(`--reverse`);
|
|
||||||
params.push(`--ancestry-path`);
|
|
||||||
params.push(`${sha}..HEAD`);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
params.push(sha);
|
|
||||||
}
|
|
||||||
params.push(`--`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return gitCommand(root, ...params, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static logRange(fileName: string, start: number, end: number, sha?: string, repoPath?: string, maxCount?: number) {
|
|
||||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
|
||||||
|
|
||||||
const params = [...DefaultLogParams, `--no-merges`];
|
|
||||||
if (maxCount) {
|
|
||||||
params.push(`-n${maxCount}`);
|
|
||||||
}
|
|
||||||
if (sha) {
|
|
||||||
params.push(`--follow`);
|
|
||||||
params.push(sha);
|
|
||||||
}
|
|
||||||
params.push(`-L ${start},${end}:${file}`);
|
|
||||||
|
|
||||||
return gitCommand(root, ...params);
|
|
||||||
}
|
|
||||||
|
|
||||||
static logRepo(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) {
|
|
||||||
const params = [...DefaultLogParams];
|
const params = [...DefaultLogParams];
|
||||||
if (maxCount && !reverse) {
|
if (maxCount && !reverse) {
|
||||||
params.push(`-n${maxCount}`);
|
params.push(`-n${maxCount}`);
|
||||||
@@ -207,23 +174,43 @@ export class Git {
|
|||||||
return gitCommand(repoPath, ...params);
|
return gitCommand(repoPath, ...params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static statusFile(fileName: string, repoPath: string): Promise<string> {
|
static log_file(repoPath: string, fileName: string, sha?: string, maxCount?: number, reverse: boolean = false, startLine?: number, endLine?: number) {
|
||||||
|
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||||
|
|
||||||
|
const params = [...DefaultLogParams, `--no-merges`, `--follow`];
|
||||||
|
if (maxCount && !reverse) {
|
||||||
|
params.push(`-n${maxCount}`);
|
||||||
|
}
|
||||||
|
if (sha) {
|
||||||
|
if (reverse) {
|
||||||
|
params.push(`--reverse`);
|
||||||
|
params.push(`--ancestry-path`);
|
||||||
|
params.push(`${sha}..HEAD`);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
params.push(sha);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startLine != null && endLine != null) {
|
||||||
|
params.push(`-L ${startLine},${endLine}:${file}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
params.push(`--`);
|
||||||
|
params.push(file);
|
||||||
|
|
||||||
|
return gitCommand(root, ...params);
|
||||||
|
}
|
||||||
|
|
||||||
|
static status(repoPath: string): Promise<string> {
|
||||||
|
const params = ['status', '--short'];
|
||||||
|
return gitCommand(repoPath, ...params);
|
||||||
|
}
|
||||||
|
|
||||||
|
static status_file(repoPath: string, fileName: string): Promise<string> {
|
||||||
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
|
||||||
|
|
||||||
const params = ['status', file, '--short'];
|
const params = ['status', file, '--short'];
|
||||||
return gitCommand(root, ...params);
|
return gitCommand(root, ...params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static statusRepo(repoPath: string): Promise<string> {
|
|
||||||
const params = ['status', '--short'];
|
|
||||||
return gitCommand(repoPath, ...params);
|
|
||||||
}
|
|
||||||
|
|
||||||
static isSha(sha: string) {
|
|
||||||
return ShaRegex.test(sha);
|
|
||||||
}
|
|
||||||
|
|
||||||
static isUncommitted(sha: string) {
|
|
||||||
return UncommittedRegex.test(sha);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -294,7 +294,7 @@ export class GitService extends Disposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getRepoPath(cwd: string): Promise<string> {
|
getRepoPath(cwd: string): Promise<string> {
|
||||||
return Git.repoPath(cwd);
|
return Git.getRepoPath(cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRepoPathFromUri(uri?: Uri, fallbackRepoPath?: string): Promise<string | undefined> {
|
async getRepoPathFromUri(uri?: Uri, fallbackRepoPath?: string): Promise<string | undefined> {
|
||||||
@@ -338,7 +338,7 @@ export class GitService extends Disposable {
|
|||||||
return GitService.EmptyPromise as Promise<IGitBlame>;
|
return GitService.EmptyPromise as Promise<IGitBlame>;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Git.blame(GitService.BlameFormat, fileName, sha, repoPath)
|
return Git.blame(repoPath, fileName, GitService.BlameFormat, sha)
|
||||||
.then(data => new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName))
|
.then(data => new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName))
|
||||||
.catch(ex => {
|
.catch(ex => {
|
||||||
// Trap and cache expected blame errors
|
// Trap and cache expected blame errors
|
||||||
@@ -393,7 +393,7 @@ export class GitService extends Disposable {
|
|||||||
fileName = Git.normalizePath(fileName);
|
fileName = Git.normalizePath(fileName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await Git.blameLines(GitService.BlameFormat, fileName, line + 1, line + 1, sha, repoPath);
|
const data = await Git.blame(repoPath, fileName, GitService.BlameFormat, sha, line + 1, line + 1);
|
||||||
const blame = new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName);
|
const blame = new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName);
|
||||||
if (!blame) return undefined;
|
if (!blame) return undefined;
|
||||||
|
|
||||||
@@ -498,7 +498,7 @@ export class GitService extends Disposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await Git.logRepo(repoPath, sha, maxCount, reverse);
|
const data = await Git.log(repoPath, sha, maxCount, reverse);
|
||||||
return new GitLogParserEnricher().enrich(data, 'repo', repoPath, maxCount, true, reverse);
|
return new GitLogParserEnricher().enrich(data, 'repo', repoPath, maxCount, true, reverse);
|
||||||
}
|
}
|
||||||
catch (ex) {
|
catch (ex) {
|
||||||
@@ -530,9 +530,7 @@ export class GitService extends Disposable {
|
|||||||
return GitService.EmptyPromise as Promise<IGitLog>;
|
return GitService.EmptyPromise as Promise<IGitLog>;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (range
|
return Git.log_file(repoPath, fileName, sha, maxCount, reverse, range && range.start.line + 1, range && range.end.line + 1)
|
||||||
? Git.logRange(fileName, range.start.line + 1, range.end.line + 1, sha, repoPath, maxCount)
|
|
||||||
: Git.log(fileName, sha, repoPath, maxCount, reverse))
|
|
||||||
.then(data => new GitLogParserEnricher().enrich(data, 'file', repoPath || fileName, maxCount, !!repoPath, reverse))
|
.then(data => new GitLogParserEnricher().enrich(data, 'file', repoPath || fileName, maxCount, !!repoPath, reverse))
|
||||||
.catch(ex => {
|
.catch(ex => {
|
||||||
// Trap and cache expected log errors
|
// Trap and cache expected log errors
|
||||||
@@ -592,13 +590,13 @@ export class GitService extends Disposable {
|
|||||||
|
|
||||||
async getStatusForFile(fileName: string, repoPath: string): Promise<GitFileStatusItem> {
|
async getStatusForFile(fileName: string, repoPath: string): Promise<GitFileStatusItem> {
|
||||||
Logger.log(`getStatusForFile('${fileName}', '${repoPath}')`);
|
Logger.log(`getStatusForFile('${fileName}', '${repoPath}')`);
|
||||||
const status = await Git.statusFile(fileName, repoPath);
|
const status = await Git.status_file(repoPath, fileName);
|
||||||
return status && status.trim().length && new GitFileStatusItem(repoPath, status);
|
return status && status.trim().length && new GitFileStatusItem(repoPath, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStatusesForRepo(repoPath: string): Promise<GitFileStatusItem[]> {
|
async getStatusesForRepo(repoPath: string): Promise<GitFileStatusItem[]> {
|
||||||
Logger.log(`getStatusForRepo('${repoPath}')`);
|
Logger.log(`getStatusForRepo('${repoPath}')`);
|
||||||
const statuses = (await Git.statusRepo(repoPath)).split('\n').filter(_ => !!_);
|
const statuses = (await Git.status(repoPath)).split('\n').filter(_ => !!_);
|
||||||
return statuses.map(_ => new GitFileStatusItem(repoPath, _));
|
return statuses.map(_ => new GitFileStatusItem(repoPath, _));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -622,7 +620,7 @@ export class GitService extends Disposable {
|
|||||||
|
|
||||||
getVersionedFileText(fileName: string, repoPath: string, sha: string) {
|
getVersionedFileText(fileName: string, repoPath: string, sha: string) {
|
||||||
Logger.log(`getVersionedFileText('${fileName}', '${repoPath}', ${sha})`);
|
Logger.log(`getVersionedFileText('${fileName}', '${repoPath}', ${sha})`);
|
||||||
return Git.getVersionedFileText(fileName, repoPath, sha);
|
return Git.show(repoPath, fileName, sha);
|
||||||
}
|
}
|
||||||
|
|
||||||
isEditorBlameable(editor: TextEditor): boolean {
|
isEditorBlameable(editor: TextEditor): boolean {
|
||||||
@@ -634,7 +632,7 @@ export class GitService extends Disposable {
|
|||||||
|
|
||||||
openDirectoryDiff(repoPath: string, sha1: string, sha2?: string) {
|
openDirectoryDiff(repoPath: string, sha1: string, sha2?: string) {
|
||||||
Logger.log(`openDirectoryDiff('${repoPath}', ${sha1}, ${sha2})`);
|
Logger.log(`openDirectoryDiff('${repoPath}', ${sha1}, ${sha2})`);
|
||||||
return Git.diffDir(repoPath, sha1, sha2);
|
return Git.difftool_dirDiff(repoPath, sha1, sha2);
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleCodeLens(editor: TextEditor) {
|
toggleCodeLens(editor: TextEditor) {
|
||||||
|
|||||||
Reference in New Issue
Block a user