Skip to content

Commit 5c5558f

Browse files
ajaygmDonJayamanne
authored andcommitted
Feature: Provide an option to view history log for all branches (DonJayamanne#144)
* removed duplicate code * added options (all branches, current branch) to select git log mode * removed unrequired description in mode selection * fixed broken multi-repository scenario
1 parent 9f121a7 commit 5c5558f

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

src/helpers/gitHistory.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ const LOG_ENTRY_SEPARATOR = '95E9659B-27DC-43C4-A717-D75969757EA5';
99
const STATS_SEPARATOR = parser.STATS_SEPARATOR;
1010
const LOG_FORMAT = `--format="%n${LOG_ENTRY_SEPARATOR}%nrefs=%d%ncommit=%H%ncommitAbbrev=%h%ntree=%T%ntreeAbbrev=%t%nparents=%P%nparentsAbbrev=%p%nauthor=%an <%ae> %at%ncommitter=%cn <%ce> %ct%nsubject=%s%nbody=%b%n%nnotes=%N%n${STATS_SEPARATOR}%n"`;
1111

12-
export async function getLogEntries(rootDir: string, pageIndex: number = 0, pageSize: number = 100): Promise<LogEntry[]> {
13-
const args = ['log', LOG_FORMAT, '--date-order', '--decorate=full', `--skip=${pageIndex * pageSize}`, `--max-count=${pageSize}`, '--numstat', '--'];
14-
// This is how you can view the log across all branches
15-
// args = ['log', LOG_FORMAT, '--date-order', '--decorate=full', `--skip=${pageIndex * pageSize}`, `--max-count=${pageSize}`, '--all', '--']
12+
export async function getLogEntries(rootDir: string, branchName: string, pageIndex: number = 0, pageSize: number = 100): Promise<LogEntry[]> {
13+
14+
let args: string[];
15+
if (branchName && branchName.length > 0) {
16+
args = ['log', LOG_FORMAT, '--date-order', '--decorate=full', `--skip=${pageIndex * pageSize}`, `--max-count=${pageSize}`, '--numstat', '--'];
17+
}
18+
else {
19+
args = ['log', LOG_FORMAT, '--date-order', '--decorate=full', `--skip=${pageIndex * pageSize}`, `--max-count=${pageSize}`, '--all', '--numstat', '--'];
20+
}
21+
1622
const gitPath = await getGitPath();
1723
return new Promise<LogEntry[]>((resolve, reject) => {
1824
const options = { cwd: rootDir };
@@ -71,7 +77,7 @@ export async function getLogEntries(rootDir: string, pageIndex: number = 0, page
7177
error += data;
7278
});
7379

74-
ls.on('error', function(error) {
80+
ls.on('error', function (error) {
7581
logger.logError(error);
7682
reject(error);
7783
return;

src/helpers/logParser.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ export function parseLogEntry(lines: string[]): LogEntry | null {
229229
logEntry.committer = parseAuthCommitter(line.substring(prefixLengths.committer));
230230
return;
231231
}
232-
if (line.indexOf(prefixes.committer) === 0) {
233-
logEntry.committer = parseAuthCommitter(line.substring(prefixLengths.committer));
234-
return;
235-
}
236232
if (line.indexOf(prefixes.subject) === 0) {
237233
logEntry.subject = line.substring(prefixLengths.subject).trim();
238234
return;

src/logViewer/logViewer.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
2727
public async provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): Promise<string> {
2828
try {
2929
let branchName = this.getBranchFromURI(uri);
30-
if ( this.html.hasOwnProperty(branchName) ) {
30+
if (this.html.hasOwnProperty(branchName)) {
3131
return this.html[branchName];
3232
}
33-
const entries = await gitHistory.getLogEntries(gitRepoPath, pageIndex, pageSize);
33+
const entries = await gitHistory.getLogEntries(gitRepoPath, branchName, pageIndex, pageSize);
3434
canGoPrevious = pageIndex > 0;
3535
canGoNext = entries.length === pageSize;
3636
this.entries = entries;
@@ -53,14 +53,18 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
5353
}
5454

5555
private clearCache(name: string) {
56-
if ( this.html.hasOwnProperty(name) ) {
56+
if (this.html.hasOwnProperty(name)) {
5757
delete this.html[name];
5858
}
5959
}
6060

6161
private getBranchFromURI(uri: vscode.Uri): string {
62-
let re = uri.query.match(/branch=([a-z0-9_\-.]+)/i);
63-
return (re) ? re[1] : 'master';
62+
if (uri.query.length > 0) {
63+
let re = uri.query.match(/branch=([a-z0-9_\-.]+)/i);
64+
return (re) ? re[1] : 'master';
65+
} else {
66+
return '';
67+
}
6468
}
6569

6670
private getStyleSheetPath(resourceName: string): string {
@@ -115,6 +119,16 @@ export function activate(context: vscode.ExtensionContext) {
115119
let registration = vscode.workspace.registerTextDocumentContentProvider(gitHistorySchema, provider);
116120

117121
let disposable = vscode.commands.registerCommand('git.viewHistory', async (fileUri?: vscode.Uri) => {
122+
const itemPickList: vscode.QuickPickItem[] = [];
123+
itemPickList.push({ label: 'Current branch', description: '' });
124+
itemPickList.push({ label: 'All branches', description: '' });
125+
let modeChoice = await vscode.window.showQuickPick(itemPickList, { placeHolder: 'Show history for...', matchOnDescription: true });
126+
127+
let title: string;
128+
if (modeChoice === undefined) {
129+
return;
130+
}
131+
118132
let fileName = '';
119133
let branchName = 'master';
120134

@@ -138,8 +152,16 @@ export function activate(context: vscode.ExtensionContext) {
138152
pageIndex = 0;
139153
canGoPrevious = false;
140154
canGoNext = true;
141-
previewUri = vscode.Uri.parse(gitHistorySchema + '://authority/git-history?branch=' + encodeURI(branchName) );
142-
return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.One, 'Git History (' + branchName + ')').then((success) => {
155+
156+
if (modeChoice.label === 'All branches') {
157+
previewUri = vscode.Uri.parse(gitHistorySchema + '://authority/git-history');
158+
title = 'Git History (all branches)';
159+
}
160+
else {
161+
previewUri = vscode.Uri.parse(gitHistorySchema + '://authority/git-history?branch=' + encodeURI(branchName));
162+
title = 'Git History (' + branchName + ')';
163+
}
164+
return vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.One, title).then((success) => {
143165
provider.update(previewUri);
144166
}, (reason) => {
145167
vscode.window.showErrorMessage(reason);

0 commit comments

Comments
 (0)