Skip to content

Commit 81a507c

Browse files
committed
Use --show-current for the git branch element
The --show-current option has been introduced in git 2.22.0. https://www.git-scm.com/docs/git-branch#Documentation/git-branch.txt---show-current
1 parent 4b3cec5 commit 81a507c

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/lib/enum/promptElementType.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,7 @@ export const PROMPT_ELEMENT_TYPES = [
219219
new PromptElementType('Jobs', '\\j', [], true, false, 'The number of jobs currently managed by the shell.', '2'),
220220
new PromptElementType('Prompt Sign', '\\$', [], true, false, 'If the effective uid is 0, #, otherwise $.', '$'),
221221
new PromptElementType('Exit Status', '$?', [], true, false, 'Exit status ($?).', '0'),
222-
new PromptElementType(
223-
'Git Branch',
224-
// eslint-disable-next-line quotes
225-
"git branch 2>/dev/null | grep '*' | colrm 1 2",
226-
[],
227-
true,
228-
true,
229-
'Git branch.',
230-
'master',
231-
),
222+
new PromptElementType('Git Branch', 'git branch --show-current 2>/dev/null', [], true, true, 'Git branch.', 'master'),
232223
new PromptElementType(
233224
'Advanced Git Prompt',
234225
(args) => `__git_ps1 "${args.format ?? ''}"`,

src/lib/promptParser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,12 @@ function applyPromptCommand(ps1: PromptElement[], promptCommand: string): Prompt
313313
}
314314

315315
// use the predefined command elements (e. g. 'Git branch') if possible
316-
const predefinedCommand = PROMPT_ELEMENT_TYPES.find((e) => e.command && e.char({}) === command);
316+
let predefinedCommand = PROMPT_ELEMENT_TYPES.find((e) => e.command && e.char({}) === command);
317+
// backwards compatibility for the git branch command (replaces the old command with the new one)
318+
// eslint-disable-next-line quotes
319+
if (command === "git branch 2>/dev/null | grep '*' | colrm 1 2") {
320+
predefinedCommand = getPromptElementTypeByNameUnsafe('Git Branch');
321+
}
317322
let newElement: PromptElement;
318323
if (predefinedCommand !== undefined) {
319324
newElement = new PromptElement(predefinedCommand);

0 commit comments

Comments
 (0)