Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace git command with git extension api (2/2) #434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/adapter/parsers/log/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ export class LogParser implements ILogParser {
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
@inject(IActionDetailsParser) private actionDetailsParser: IActionDetailsParser,
) {}
public parse(
gitRepoPath: string,
summaryEntry: string,
itemEntrySeparator: string,
logFormatArgs: string[],
filesWithNumStat?: string,
filesWithNameStatus?: string,
): LogEntry {
public parse(summaryEntry: string, itemEntrySeparator: string, logFormatArgs: string[]): LogEntry {
const logItems = summaryEntry.split(itemEntrySeparator);

const fullParentHash = this.getCommitInfo(logItems, logFormatArgs, CommitInfo.ParentFullHash)
Expand All @@ -29,7 +22,6 @@ export class LogParser implements ILogParser {
const parents = fullParentHash.map((hash, index) => {
return { full: hash, short: shortParentHash[index] };
});
const committedFiles = this.parserCommittedFiles(gitRepoPath, filesWithNumStat, filesWithNameStatus);

return {
refs: [],
Expand All @@ -38,7 +30,6 @@ export class LogParser implements ILogParser {
body: this.getCommitInfo(logItems, logFormatArgs, CommitInfo.Body),
notes: this.getCommitInfo(logItems, logFormatArgs, CommitInfo.Notes),
parents,
committedFiles,
hash: {
full: this.getCommitInfo(logItems, logFormatArgs, CommitInfo.FullHash),
short: this.getCommitInfo(logItems, logFormatArgs, CommitInfo.ShortHash),
Expand All @@ -50,7 +41,8 @@ export class LogParser implements ILogParser {
subject: this.getCommitInfo(logItems, logFormatArgs, CommitInfo.Subject),
};
}
private parserCommittedFiles(

public parserCommittedFiles(
gitRepoPath: string,
filesWithNumStat?: string,
filesWithNameStatus?: string,
Expand All @@ -70,6 +62,7 @@ export class LogParser implements ILogParser {
return [];
}
}

private getCommitInfo(logItems: string[], logFormatArgs: string[], info: CommitInfo): string {
const commitInfoFormatCode = Helpers.GetCommitInfoFormatCode(info);
const indexInArgs = logFormatArgs.indexOf(commitInfoFormatCode);
Expand Down
10 changes: 2 additions & 8 deletions src/adapter/parsers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ export interface IActionDetailsParser {
}
export const ILogParser = Symbol.for('ILogParser');
export interface ILogParser {
parse(
gitRepoPath: string,
summaryEntry: string,
itemEntrySeparator: string,
logFormatArgs: string[],
filesWithNumStat?: string,
filesWithNameStatus?: string,
): LogEntry;
parse(summaryEntry: string, itemEntrySeparator: string, logFormatArgs: string[]): LogEntry;
parserCommittedFiles(gitRepoPath: string, filesWithNumStat?: string, filesWithNameStatus?: string): CommittedFile[];
}
65 changes: 34 additions & 31 deletions src/adapter/repository/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ export class Git implements IGitService {
author,
);

const gitRepoPath = this.getGitRoot();
const countPromise = lineNumber
? Promise.resolve(-1)
: this.exec(...args.counterArgs).then(value => parseInt(value));
Expand All @@ -242,7 +241,7 @@ export class Git implements IGitService {
if (entry.length === 0) {
return;
}
return this.logParser.parse(gitRepoPath, entry, ITEM_ENTRY_SEPARATOR, LOG_FORMAT_ARGS);
return this.logParser.parse(entry, ITEM_ENTRY_SEPARATOR, LOG_FORMAT_ARGS);
})
.filter(logEntry => logEntry !== undefined)
.map(logEntry => {
Expand Down Expand Up @@ -275,43 +274,47 @@ export class Git implements IGitService {

@captureTelemetry()
public async getCommit(hash: string, withRefs = false): Promise<LogEntry | undefined> {
const commitArgs = this.gitArgsService.getCommitArgs(hash);
const commitMessage = await this.repo.getCommit(hash);

const numStatusArgs = this.gitArgsService.getCommitWithNumStatArgsForMerge(hash);
const nameStatusArgs = this.gitArgsService.getCommitNameStatusArgsForMerge(hash);

const gitRootPath = this.getGitRoot();
const commitOutput = await this.exec(...commitArgs);

const filesWithNumStat = commitOutput.slice(
commitOutput.lastIndexOf(ITEM_ENTRY_SEPARATOR) + ITEM_ENTRY_SEPARATOR.length,
const [filesWithNumStatus, filesWithNameStatus] = await Promise.all([
this.exec(...numStatusArgs),
this.exec(...nameStatusArgs),
]);

const committedFiles = this.logParser.parserCommittedFiles(
gitRootPath,
filesWithNumStatus,
filesWithNameStatus,
);
const filesWithNameStatus = await this.exec(...nameStatusArgs);

const entries = commitOutput
.split(LOG_ENTRY_SEPARATOR)
.map(entry => {
if (entry.trim().length === 0) {
return undefined;
}
return this.logParser.parse(
gitRootPath,
entry,
ITEM_ENTRY_SEPARATOR,
LOG_FORMAT_ARGS,
filesWithNumStat,
filesWithNameStatus,
);
})
.filter(entry => entry !== undefined)
.map(entry => entry!);
const lineBreakIndex = commitMessage.message.indexOf('\n');
let title = commitMessage.message;

if (entries.length > 0) {
if (withRefs) {
entries[0].refs = this.getRefsContainingCommit(hash);
}
return entries[0];
if (lineBreakIndex > 0) {
title = title.substr(0, lineBreakIndex);
}

return undefined;
return {
author: {
name: commitMessage.authorName,
email: commitMessage.authorEmail,
date: commitMessage.authorDate,
},
parents: commitMessage.parents.map<Hash>(x => {
return { full: x, short: x.substr(0, 8) };
}),
hash: { full: commitMessage.hash, short: commitMessage.hash.substr(0, 8) } as Hash,
tree: { full: commitMessage.hash, short: commitMessage.hash.substr(0, 8) } as Hash,
refs: withRefs ? this.getRefsContainingCommit(hash) : [],
subject: title,
notes: '',
body: lineBreakIndex > 0 ? commitMessage.message.substr(lineBreakIndex + 1).trimLeft() : '',
committedFiles: committedFiles,
} as LogEntry;
}

@cache('IGitService')
Expand Down
3 changes: 0 additions & 3 deletions src/adapter/repository/gitArgsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import { GitLogArgs, IGitArgsService } from './types';

@injectable()
export class GitArgsService implements IGitArgsService {
public getCommitArgs(hash: string): string[] {
return ['show', LOG_FORMAT, '--decorate=full', '--numstat', hash];
}
public getCommitParentHashesArgs(hash: string): string[] {
return ['log', '--format=%p', '-n1', hash];
}
Expand Down
1 change: 0 additions & 1 deletion src/adapter/repository/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const IGitArgsService = Symbol.for('IGitArgsService');

export interface IGitArgsService {
getAuthorsArgs(): string[];
getCommitArgs(hash: string): string[];
getCommitParentHashesArgs(hash: string): string[];
getCommitWithNumStatArgs(hash: string): string[];
getCommitNameStatusArgs(hash: string): string[];
Expand Down