Skip to content

Commit 9872f84

Browse files
authored
Feat/1074 branch show current (#1083)
* Add support for `git.branch` parsing the single-line response to `branch --show-current`. * Changeset * Configure git for use before testing
1 parent e40ab27 commit 9872f84

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

.changeset/nasty-bikes-say.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"simple-git": minor
3+
---
4+
5+
Support the use of `git.branch(['--show-current'])` to limit the branch list to only the current branch.
6+
7+
Thanks to @peterbe for pointing out the use-case.

biome.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@
131131
"**/*.ts",
132132
"**/*.json",
133133
"**/*.yml",
134-
"!node_modules/**",
135-
"!.changeset/**"
134+
"!**/coverage/**",
135+
"!.changeset/**",
136+
"!node_modules/**"
136137
]
137138
}
138139
}

simple-git/src/lib/parsers/parse-branch.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { BranchSummary } from '../../../typings';
2-
import { BranchSummaryResult } from '../responses/BranchSummary';
2+
import { BranchStatusIdentifier, BranchSummaryResult } from '../responses/BranchSummary';
33
import { LineParser, parseStringResponse } from '../utils';
44

55
const parsers: LineParser<BranchSummaryResult>[] = [
@@ -17,10 +17,18 @@ const parsers: LineParser<BranchSummaryResult>[] = [
1717
),
1818
];
1919

20+
const currentBranchParser = new LineParser<BranchSummaryResult>(/^(\S+)$/s, (result, [name]) => {
21+
result.push(BranchStatusIdentifier.CURRENT, false, name, '', '');
22+
});
23+
2024
function branchStatus(input?: string) {
2125
return input ? input.charAt(0) : '';
2226
}
2327

24-
export function parseBranchSummary(stdOut: string): BranchSummary {
25-
return parseStringResponse(new BranchSummaryResult(), parsers, stdOut);
28+
export function parseBranchSummary(stdOut: string, currentOnly = false): BranchSummary {
29+
return parseStringResponse(
30+
new BranchSummaryResult(),
31+
currentOnly ? [currentBranchParser] : parsers,
32+
stdOut
33+
);
2634
}

simple-git/src/lib/tasks/branch.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export function branchTask(
1414
customArgs: string[]
1515
): StringTask<BranchSummary | BranchSingleDeleteResult> {
1616
const isDelete = containsDeleteBranchCommand(customArgs);
17+
const isCurrentOnly = customArgs.includes('--show-current');
18+
1719
const commands = ['branch', ...customArgs];
1820

1921
if (commands.length === 1) {
@@ -32,18 +34,18 @@ export function branchTask(
3234
return parseBranchDeletions(stdOut, stdErr).all[0];
3335
}
3436

35-
return parseBranchSummary(stdOut);
37+
return parseBranchSummary(stdOut, isCurrentOnly);
3638
},
3739
};
3840
}
3941

4042
export function branchLocalTask(): StringTask<BranchSummary> {
41-
const parser = parseBranchSummary;
42-
4343
return {
4444
format: 'utf-8',
4545
commands: ['branch', '-v'],
46-
parser,
46+
parser(stdOut) {
47+
return parseBranchSummary(stdOut);
48+
},
4749
};
4850
}
4951

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createTestContext, like, setUpFilesAdded, setUpInit, SimpleGitTestContext } from '@simple-git/test-utils';
2+
3+
describe('branch-show-current', () => {
4+
let context: SimpleGitTestContext;
5+
6+
const expectedBranchSummary = (commit = '', label = '') => like({
7+
all: ['my-new-branch'],
8+
current: 'my-new-branch',
9+
branches: {
10+
'my-new-branch': {
11+
name: 'my-new-branch',
12+
commit,
13+
label,
14+
current: true,
15+
linkedWorkTree: false,
16+
},
17+
},
18+
});
19+
20+
21+
beforeEach(async () => (context = await createTestContext()));
22+
beforeEach(async () => {
23+
await setUpInit(context);
24+
await context.git.raw('checkout', '-b', 'my-new-branch');
25+
});
26+
27+
it('should be able to show you the current on an empty repo', async () => {
28+
expect(await context.git.branch(['--show-current']))
29+
.toEqual(expectedBranchSummary());
30+
});
31+
32+
it('should be able to show you the current on an regular repo', async () => {
33+
await setUpFilesAdded(context, ['some-file'], '.', 'Initial Commit');
34+
35+
expect(await context.git.branch(['--show-current']))
36+
.toEqual(expectedBranchSummary());
37+
38+
const branch = await context.git.branch();
39+
expect(branch).toEqual(expectedBranchSummary(
40+
expect.stringMatching(/^.{7}$/),
41+
'Initial Commit',
42+
));
43+
});
44+
});

0 commit comments

Comments
 (0)