Skip to content

Commit e884e2b

Browse files
committed
#18 Fixed handling of detached HEAD's
1 parent 86e02b2 commit e884e2b

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

src/dataSource.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as cp from 'child_process';
22
import { Config } from './config';
3-
import { GitCommandStatus, GitCommit, GitCommitDetails, GitCommitNode, GitFileChangeType, GitRef, GitResetMode, GitUnsavedChanges } from './types';
3+
import { GitCommandStatus, GitCommit, GitCommitDetails, GitCommitNode, GitFileChangeType, GitRefData, GitResetMode, GitUnsavedChanges } from './types';
44

55
const eolRegex = /\r\n|\r|\n/g;
6+
const headRegex = /^\(HEAD detached at [0-9A-Za-z]+\)/g;
7+
68
const gitLogSeparator = 'XX7Nal-YARtTpjCikii9nJxER19D6diSyk-AWkPb';
79
const gitLogFormat = ['%H', '%P', '%an', '%ae', '%at', '%s'].join(gitLogSeparator);
810
const gitCommitDetailsFormat = ['%H', '%P', '%an', '%ae', '%at', '%cn', '%B'].join(gitLogSeparator);
@@ -37,9 +39,10 @@ export class DataSource {
3739
let lines = stdout.split(eolRegex);
3840
let branches: string[] = [];
3941
for (let i = 0; i < lines.length - 1; i++) {
40-
let active = lines[i][0] === '*';
41-
let name = lines[i].substring(2).split(' ')[0];
42-
if (active) {
42+
let name = lines[i].substring(2).split(' -> ')[0];
43+
if (name.match(headRegex) !== null) continue;
44+
45+
if (lines[i][0] === '*') {
4346
branches.unshift(name);
4447
} else {
4548
branches.push(name);
@@ -53,30 +56,22 @@ export class DataSource {
5356
});
5457
}
5558

56-
public async getCommits(branch: string, maxCommits: number, showRemoteBranches: boolean, currentBranch: string | null) {
57-
let i, j;
59+
public async getCommits(branch: string, maxCommits: number, showRemoteBranches: boolean) {
5860
let commits = await this.getGitLog(branch, maxCommits + 1, showRemoteBranches);
59-
let refs = await this.getRefs(showRemoteBranches);
60-
let unsavedChanges = null;
61+
let refData = await this.getRefs(showRemoteBranches);
62+
let i, unsavedChanges = null;
6163

6264
let moreCommitsAvailable = commits.length === maxCommits + 1;
6365
if (moreCommitsAvailable) commits.pop();
6466

65-
let currentBranchHash = null;
66-
for (i = 0; i < refs.length; i++) {
67-
if (refs[i].name === currentBranch && refs[i].type === 'head') {
68-
currentBranchHash = refs[i].hash;
69-
break;
70-
}
71-
}
72-
if (currentBranchHash !== null && (branch === '' || branch === currentBranch)) {
73-
unsavedChanges = (new Config()).showUncommittedChanges() ? await this.getGitUnsavedChanges() : null;
74-
if (unsavedChanges !== null) {
75-
for (j = 0; j < commits.length; j++) {
76-
if (currentBranchHash === commits[j].hash) {
77-
commits.unshift({ hash: '*', parentHashes: [currentBranchHash], author: '*', email: '', date: Math.round((new Date()).getTime() / 1000), message: 'Uncommitted Changes (' + unsavedChanges.changes + ')' });
78-
break;
67+
if (refData.head !== null) {
68+
for (i = 0; i < commits.length; i++) {
69+
if (refData.head === commits[i].hash) {
70+
unsavedChanges = (new Config()).showUncommittedChanges() ? await this.getGitUnsavedChanges() : null;
71+
if (unsavedChanges !== null) {
72+
commits.unshift({ hash: '*', parentHashes: [refData.head], author: '*', email: '', date: Math.round((new Date()).getTime() / 1000), message: 'Uncommitted Changes (' + unsavedChanges.changes + ')' });
7973
}
74+
break;
8075
}
8176
}
8277
}
@@ -88,16 +83,16 @@ export class DataSource {
8883
commitLookup[commits[i].hash] = i;
8984
commitNodes.push({ hash: commits[i].hash, parentHashes: commits[i].parentHashes, author: commits[i].author, email: commits[i].email, date: commits[i].date, message: commits[i].message, refs: [], current: false });
9085
}
91-
for (i = 0; i < refs.length; i++) {
92-
if (typeof commitLookup[refs[i].hash] === 'number') {
93-
commitNodes[commitLookup[refs[i].hash]].refs.push(refs[i]);
86+
for (i = 0; i < refData.refs.length; i++) {
87+
if (typeof commitLookup[refData.refs[i].hash] === 'number') {
88+
commitNodes[commitLookup[refData.refs[i].hash]].refs.push(refData.refs[i]);
9489
}
9590
}
9691

9792
if (unsavedChanges !== null) {
9893
commitNodes[0].current = true;
99-
} else if (currentBranchHash !== null && typeof commitLookup[currentBranchHash] === 'number') {
100-
commitNodes[commitLookup[currentBranchHash]].current = true;
94+
} else if (refData.head !== null && typeof commitLookup[refData.head] === 'number') {
95+
commitNodes[commitLookup[refData.head]].current = true;
10196
}
10297

10398
return { commits: commitNodes, moreCommitsAvailable: moreCommitsAvailable };
@@ -214,11 +209,11 @@ export class DataSource {
214209
}
215210

216211
private getRefs(showRemoteBranches: boolean) {
217-
return new Promise<GitRef[]>((resolve) => {
218-
this.execGit('show-ref ' + (showRemoteBranches ? '' : '--heads --tags') + ' -d', (err, stdout) => {
212+
return new Promise<GitRefData>((resolve) => {
213+
this.execGit('show-ref ' + (showRemoteBranches ? '' : '--heads --tags') + ' -d --head', (err, stdout) => {
214+
let refData: GitRefData = { head: null, refs: [] };
219215
if (!err) {
220216
let lines = stdout.split(eolRegex);
221-
let refs: GitRef[] = [];
222217
for (let i = 0; i < lines.length - 1; i++) {
223218
let line = lines[i].split(' ');
224219
if (line.length < 2) continue;
@@ -227,17 +222,17 @@ export class DataSource {
227222
let ref = line.join(' ');
228223

229224
if (ref.startsWith('refs/heads/')) {
230-
refs.push({ hash: hash, name: ref.substring(11), type: 'head' });
225+
refData.refs.push({ hash: hash, name: ref.substring(11), type: 'head' });
231226
} else if (ref.startsWith('refs/tags/')) {
232-
refs.push({ hash: hash, name: (ref.endsWith('^{}') ? ref.substring(10, ref.length - 3) : ref.substring(10)), type: 'tag' });
227+
refData.refs.push({ hash: hash, name: (ref.endsWith('^{}') ? ref.substring(10, ref.length - 3) : ref.substring(10)), type: 'tag' });
233228
} else if (ref.startsWith('refs/remotes/')) {
234-
refs.push({ hash: hash, name: ref.substring(13), type: 'remote' });
229+
refData.refs.push({ hash: hash, name: ref.substring(13), type: 'remote' });
230+
} else if (ref === 'HEAD') {
231+
refData.head = hash;
235232
}
236233
}
237-
resolve(refs);
238-
} else {
239-
resolve([]);
240234
}
235+
resolve(refData);
241236
});
242237
});
243238
}

src/gitGraphView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class GitGraphView {
102102
case 'loadCommits':
103103
this.sendMessage({
104104
command: 'loadCommits',
105-
... await this.dataSource.getCommits(msg.branchName, msg.maxCommits, msg.showRemoteBranches, msg.currentBranch)
105+
... await this.dataSource.getCommits(msg.branchName, msg.maxCommits, msg.showRemoteBranches)
106106
});
107107
return;
108108
case 'mergeBranch':

src/types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface GitRef {
3737
type: 'head' | 'tag' | 'remote';
3838
}
3939

40+
export interface GitRefData {
41+
head: string | null;
42+
refs: GitRef[];
43+
}
44+
4045
export interface GitUnsavedChanges {
4146
branch: string;
4247
changes: number;
@@ -51,7 +56,7 @@ export interface GitGraphViewSettings {
5156
loadMoreCommits: number;
5257
}
5358

54-
export interface GitFileChange{
59+
export interface GitFileChange {
5560
oldFilePath: string;
5661
newFilePath: string;
5762
type: GitFileChangeType;
@@ -159,7 +164,6 @@ export interface RequestLoadCommits {
159164
branchName: string;
160165
maxCommits: number;
161166
showRemoteBranches: boolean;
162-
currentBranch: string | null;
163167
}
164168
export interface ResponseLoadCommits {
165169
command: 'loadCommits';

web/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@
343343
command: 'loadCommits',
344344
branchName: (this.selectedBranch !== null ? this.selectedBranch : ''),
345345
maxCommits: this.maxCommits,
346-
showRemoteBranches: this.showRemoteBranches,
347-
currentBranch: this.branchOptions.length > 0 ? this.branchOptions[0] : null
346+
showRemoteBranches: this.showRemoteBranches
348347
});
349348
}
350349

0 commit comments

Comments
 (0)