Skip to content

Commit a0d4eb8

Browse files
committedJun 21, 2022
Branches that have been checked out as a [linked work tree](https://git-scm.com/docs/git-worktree) will now be included in the BranchSummary output, with a linkedWorkTree property set to true in the BranchSummaryBranch.
Closes #811
1 parent c6fbb7b commit a0d4eb8

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed
 

‎.changeset/pink-gravy-boat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"simple-git": minor
3+
---
4+
5+
Branches that have been checked out as a [linked work tree](https://git-scm.com/docs/git-worktree) will now be included in the `BranchSummary` output, with a `linkedWorkTree` property set to `true` in the `BranchSummaryBranch`.
+10-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
import { BranchSummary } from '../../../typings';
1+
import type { BranchSummary } from '../../../typings';
22
import { BranchSummaryResult } from '../responses/BranchSummary';
33
import { LineParser, parseStringResponse } from '../utils';
44

55
const parsers: LineParser<BranchSummaryResult>[] = [
6-
new LineParser(/^(\*\s)?\((?:HEAD )?detached (?:from|at) (\S+)\)\s+([a-z0-9]+)\s(.*)$/, (result, [current, name, commit, label]) => {
6+
new LineParser(/^([*+]\s)?\((?:HEAD )?detached (?:from|at) (\S+)\)\s+([a-z0-9]+)\s(.*)$/, (result, [current, name, commit, label]) => {
77
result.push(
8-
!!current,
8+
branchStatus(current),
99
true,
1010
name, commit, label
1111
);
1212
}),
13-
new LineParser(/^(\*\s)?(\S+)\s+([a-z0-9]+)\s?(.*)$/s, (result, [current, name, commit, label]) => {
13+
new LineParser(/^([*+]\s)?(\S+)\s+([a-z0-9]+)\s?(.*)$/s, (result, [current, name, commit, label]) => {
1414
result.push(
15-
!!current,
15+
branchStatus(current),
1616
false,
1717
name, commit, label
1818
);
1919
})
2020
];
2121

22-
export function parseBranchSummary (stdOut: string): BranchSummary {
22+
function branchStatus(input?: string) {
23+
return input ? input.charAt(0) : '';
24+
}
25+
26+
export function parseBranchSummary(stdOut: string): BranchSummary {
2327
return parseStringResponse(new BranchSummaryResult(), parsers, stdOut);
2428
}

‎simple-git/src/lib/responses/BranchSummary.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import { BranchSummary, BranchSummaryBranch } from '../../../typings';
1+
import type { BranchSummary, BranchSummaryBranch } from '../../../typings';
2+
3+
export enum BranchStatusIdentifier {
4+
CURRENT = '*',
5+
LINKED = '+',
6+
}
27

38
export class BranchSummaryResult implements BranchSummary {
49
public all: string[] = [];
510
public branches: { [p: string]: BranchSummaryBranch } = {};
611
public current: string = '';
712
public detached: boolean = false;
813

9-
push(current: boolean, detached: boolean, name: string, commit: string, label: string) {
10-
if (current) {
14+
push(status: BranchStatusIdentifier | unknown, detached: boolean, name: string, commit: string, label: string) {
15+
if (status === BranchStatusIdentifier.CURRENT) {
1116
this.detached = detached;
1217
this.current = name;
1318
}
1419

1520
this.all.push(name);
1621
this.branches[name] = {
17-
current: current,
18-
name: name,
19-
commit: commit,
20-
label: label
22+
current: status === BranchStatusIdentifier.CURRENT,
23+
linkedWorkTree: status === BranchStatusIdentifier.LINKED,
24+
name,
25+
commit,
26+
label
2127
};
2228
}
2329
}

‎simple-git/test/unit/branch.spec.ts

+45-2
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,27 @@ describe('branch', () => {
119119
current: false,
120120
label: 'Something',
121121
name: 'branch-012de2',
122+
linkedWorkTree: false,
122123
},
123124
'branch-012de3': {
124125
commit: '012de3',
125126
current: true,
126127
label: 'Add support for carriage \r returns',
127128
name: 'branch-012de3',
129+
linkedWorkTree: false,
128130
},
129131
'branch-012de4': {
130132
commit: '012de4',
131133
current: false,
132134
label: 'Something else',
133135
name: 'branch-012de4',
136+
linkedWorkTree: false,
134137
},
135138
}
136139
}))
137140
});
138141

139142

140-
141143
it('branch detail by name', async () => {
142144
const actual = parseBranchSummary(`
143145
cflynn07-add-git-ignore a0b67a3 Add support for filenames containing spaces
@@ -154,18 +156,21 @@ describe('branch', () => {
154156
current: false,
155157
label: 'Add support for filenames containing spaces',
156158
name: 'cflynn07-add-git-ignore',
159+
linkedWorkTree: false,
157160
},
158161
'drschwabe-add-branches': {
159162
commit: '063069b',
160163
current: true,
161164
label: `Merge branch 'add-branches' of https://github.com/user/repo into drschwabe-add-branches`,
162165
name: 'drschwabe-add-branches',
166+
linkedWorkTree: false,
163167
},
164168
master: {
165169
commit: 'cb4be06',
166170
current: false,
167171
label: 'Release 1.30.0',
168172
name: 'master',
173+
linkedWorkTree: false,
169174
},
170175
},
171176
}));
@@ -210,6 +215,42 @@ describe('branch', () => {
210215
}));
211216
});
212217

218+
it(`branches in linked work trees`, () => {
219+
const actual = parseBranchSummary(`
220+
main 3c43b1d first
221+
* x e94b8dd second
222+
+ y 3c43b1d first
223+
`);
224+
225+
expect(actual).toEqual(like({
226+
current: 'x',
227+
all: ['main', 'x', 'y'],
228+
branches: {
229+
main: {
230+
commit: '3c43b1d',
231+
current: false,
232+
label: 'first',
233+
linkedWorkTree: false,
234+
name: 'main',
235+
},
236+
x: {
237+
commit: 'e94b8dd',
238+
current: true,
239+
label: 'second',
240+
linkedWorkTree: false,
241+
name: 'x',
242+
},
243+
y: {
244+
commit: '3c43b1d',
245+
current: false,
246+
label: 'first',
247+
linkedWorkTree: true,
248+
name: 'y',
249+
},
250+
}
251+
}));
252+
})
253+
213254
it('branches without labels', async () => {
214255
const actual = parseBranchSummary(`
215256
* stable f8cc2bc
@@ -225,8 +266,9 @@ describe('branch', () => {
225266
current: true,
226267
label: '',
227268
name: 'stable',
269+
linkedWorkTree: false,
228270
},
229-
['remotes/origin/stable']: {
271+
'remotes/origin/stable': {
230272
commit: 'f8cc2bd',
231273
current: false,
232274
label: '',
@@ -237,6 +279,7 @@ describe('branch', () => {
237279
current: false,
238280
label: 'wip',
239281
name: 'dev',
282+
linkedWorkTree: false,
240283
},
241284
}
242285
}));

‎simple-git/typings/response.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface BranchSummaryBranch {
55
name: string;
66
commit: string;
77
label: string;
8+
linkedWorkTree: boolean;
89
}
910

1011
export interface BranchSummary {

0 commit comments

Comments
 (0)
Please sign in to comment.