ソースを参照

Renames git methods to align better with git commands

main
Eric Amodio 7年前
コミット
9a0ce83260
3個のファイルの変更90行の追加105行の削除
  1. +1
    -1
      src/extension.ts
  2. +80
    -93
      src/git/git.ts
  3. +9
    -11
      src/gitService.ts

+ 1
- 1
src/extension.ts ファイルの表示

@ -41,7 +41,7 @@ export async function activate(context: ExtensionContext) {
let repoPath: string;
try {
repoPath = await Git.repoPath(rootPath, gitPath);
repoPath = await Git.getRepoPath(rootPath, gitPath);
}
catch (ex) {
Logger.error(ex);

+ 80
- 93
src/git/git.ts ファイルの表示

@ -11,10 +11,8 @@ export * from './enrichers/blameParserEnricher';
export * from './enrichers/logParserEnricher';
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[]) {
try {
@ -43,6 +41,56 @@ export const GitBlameFormat = {
export class Git {
static ShaRegex = /^[0-9a-f]{40}( -)?$/;
static UncommittedRegex = /^[0]+$/;
static gitInfo(): IGit {
return git;
}
static async getRepoPath(cwd: string, gitPath?: string) {
git = await findGitPath(gitPath);
Logger.log(`Git found: ${git.version} @ ${git.path === 'git' ? 'PATH' : git.path}`);
let data = await gitCommand(cwd, 'rev-parse', '--show-toplevel');
data = data.replace(/\r?\n|\r/g, '').replace(/\\/g, '/');
return data;
}
static async getVersionedFile(fileName: string, repoPath: string, branchOrSha: string) {
const data = await Git.show(repoPath, fileName, branchOrSha);
const suffix = Git.isSha(branchOrSha) ? branchOrSha.substring(0, 8) : branchOrSha;
const ext = path.extname(fileName);
return new Promise<string>((resolve, reject) => {
tmp.file({ prefix: `${path.basename(fileName, ext)}-${suffix}__`, postfix: ext },
(err, destination, fd, cleanupCallback) => {
if (err) {
reject(err);
return;
}
Logger.log(`getVersionedFile(${fileName}, ${repoPath}, ${branchOrSha}); destination=${destination}`);
fs.appendFile(destination, data, err => {
if (err) {
reject(err);
return;
}
resolve(destination);
});
});
});
}
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, '/');
}
@ -61,30 +109,17 @@ export class Git {
];
}
static async repoPath(cwd: string, gitPath?: string) {
git = await findGitPath(gitPath);
Logger.log(`Git found: ${git.version} @ ${git.path === 'git' ? 'PATH' : git.path}`);
// Git commands
let data = await gitCommand(cwd, 'rev-parse', '--show-toplevel');
data = data.replace(/\r?\n|\r/g, '').replace(/\\/g, '/');
return data;
}
static blame(format: GitBlameFormat, fileName: string, sha?: string, repoPath?: string) {
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 (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);
if (startLine != null && endLine != null) {
params.push(`-L ${startLine},${endLine}`);
}
const params = [`blame`, `--root`, format, `-L ${startLine},${endLine}`];
if (sha) {
params.push(sha);
}
@ -92,16 +127,7 @@ export class Git {
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) {
static diff_nameStatus(repoPath: string, sha1?: string, sha2?: string) {
const params = [`diff`, `--name-status`, `-M`];
if (sha1) {
params.push(sha1);
@ -113,33 +139,16 @@ export class Git {
return gitCommand(repoPath, ...params);
}
static async getVersionedFile(fileName: string, repoPath: string, branchOrSha: string) {
const data = await Git.getVersionedFileText(fileName, repoPath, branchOrSha);
const suffix = Git.isSha(branchOrSha) ? branchOrSha.substring(0, 8) : branchOrSha;
const ext = path.extname(fileName);
return new Promise<string>((resolve, reject) => {
tmp.file({ prefix: `${path.basename(fileName, ext)}-${suffix}__`, postfix: ext },
(err, destination, fd, cleanupCallback) => {
if (err) {
reject(err);
return;
}
Logger.log(`getVersionedFile(${fileName}, ${repoPath}, ${branchOrSha}); destination=${destination}`);
fs.appendFile(destination, data, err => {
if (err) {
reject(err);
return;
}
static difftool_dirDiff(repoPath: string, sha1: string, sha2?: string) {
const params = [`difftool`, `--dir-diff`, sha1];
if (sha2) {
params.push(sha2);
}
resolve(destination);
});
});
});
return gitCommand(repoPath, ...params);
}
static getVersionedFileText(fileName: string, repoPath: string, branchOrSha: string) {
static show(repoPath: string, fileName: string, branchOrSha: string) {
const [file, root] = Git.splitPath(Git.normalizePath(fileName), repoPath);
branchOrSha = branchOrSha.replace('^', '');
@ -147,14 +156,8 @@ export class Git {
return gitCommand(root, 'show', `${branchOrSha}:./${file}`);
}
static gitInfo(): IGit {
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`];
static log(repoPath: string, sha?: string, maxCount?: number, reverse: boolean = false) {
const params = [...DefaultLogParams];
if (maxCount && !reverse) {
params.push(`-n${maxCount}`);
}
@ -167,30 +170,14 @@ export class Git {
else {
params.push(sha);
}
params.push(`--`);
}
return gitCommand(root, ...params, file);
return gitCommand(repoPath, ...params);
}
static logRange(fileName: string, start: number, end: number, sha?: string, repoPath?: string, maxCount?: number) {
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`];
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, `--no-merges`, `--follow`];
if (maxCount && !reverse) {
params.push(`-n${maxCount}`);
}
@ -204,26 +191,26 @@ export class Git {
params.push(sha);
}
}
return gitCommand(repoPath, ...params);
}
static statusFile(fileName: string, repoPath: string): Promise<string> {
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
if (startLine != null && endLine != null) {
params.push(`-L ${startLine},${endLine}:${file}`);
}
params.push(`--`);
params.push(file);
const params = ['status', file, '--short'];
return gitCommand(root, ...params);
}
static statusRepo(repoPath: string): Promise<string> {
static status(repoPath: string): Promise<string> {
const params = ['status', '--short'];
return gitCommand(repoPath, ...params);
}
static isSha(sha: string) {
return ShaRegex.test(sha);
}
static status_file(repoPath: string, fileName: string): Promise<string> {
const [file, root]: [string, string] = Git.splitPath(Git.normalizePath(fileName), repoPath);
static isUncommitted(sha: string) {
return UncommittedRegex.test(sha);
const params = ['status', file, '--short'];
return gitCommand(root, ...params);
}
}

+ 9
- 11
src/gitService.ts ファイルの表示

@ -294,7 +294,7 @@ export class GitService extends Disposable {
}
getRepoPath(cwd: string): Promise<string> {
return Git.repoPath(cwd);
return Git.getRepoPath(cwd);
}
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 Git.blame(GitService.BlameFormat, fileName, sha, repoPath)
return Git.blame(repoPath, fileName, GitService.BlameFormat, sha)
.then(data => new GitBlameParserEnricher(GitService.BlameFormat).enrich(data, fileName))
.catch(ex => {
// Trap and cache expected blame errors
@ -393,7 +393,7 @@ export class GitService extends Disposable {
fileName = Git.normalizePath(fileName);
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);
if (!blame) return undefined;
@ -498,7 +498,7 @@ export class GitService extends Disposable {
}
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);
}
catch (ex) {
@ -530,9 +530,7 @@ export class GitService extends Disposable {
return GitService.EmptyPromise as Promise<IGitLog>;
}
return (range
? Git.logRange(fileName, range.start.line + 1, range.end.line + 1, sha, repoPath, maxCount)
: Git.log(fileName, sha, repoPath, maxCount, reverse))
return Git.log_file(repoPath, fileName, sha, maxCount, reverse, range && range.start.line + 1, range && range.end.line + 1)
.then(data => new GitLogParserEnricher().enrich(data, 'file', repoPath || fileName, maxCount, !!repoPath, reverse))
.catch(ex => {
// Trap and cache expected log errors
@ -592,13 +590,13 @@ export class GitService extends Disposable {
async getStatusForFile(fileName: string, repoPath: string): Promise<GitFileStatusItem> {
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);
}
async getStatusesForRepo(repoPath: string): Promise<GitFileStatusItem[]> {
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, _));
}
@ -622,7 +620,7 @@ export class GitService extends Disposable {
getVersionedFileText(fileName: string, repoPath: string, sha: string) {
Logger.log(`getVersionedFileText('${fileName}', '${repoPath}', ${sha})`);
return Git.getVersionedFileText(fileName, repoPath, sha);
return Git.show(repoPath, fileName, sha);
}
isEditorBlameable(editor: TextEditor): boolean {
@ -634,7 +632,7 @@ export class GitService extends Disposable {
openDirectoryDiff(repoPath: string, sha1: string, sha2?: string) {
Logger.log(`openDirectoryDiff('${repoPath}', ${sha1}, ${sha2})`);
return Git.diffDir(repoPath, sha1, sha2);
return Git.difftool_dirDiff(repoPath, sha1, sha2);
}
toggleCodeLens(editor: TextEditor) {

読み込み中…
キャンセル
保存