Skip to content

Commit 74fcd11

Browse files
authored
Support more languages (git branch) (#4150)
1 parent 18a79c8 commit 74fcd11

File tree

7 files changed

+94
-77
lines changed

7 files changed

+94
-77
lines changed

extension/src/cli/git/executor.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,31 @@ export class GitExecutor extends GitCli {
1717
)
1818

1919
public gitInit(cwd: string) {
20-
const options = getOptions(cwd, Command.INITIALIZE)
20+
const options = getOptions({ args: [Command.INITIALIZE], cwd })
2121

2222
return this.executeProcess(options)
2323
}
2424

2525
public reset(cwd: string, ...args: (Flag | Commit)[]) {
26-
const options = getOptions(cwd, Command.RESET, ...args)
26+
const options = getOptions({ args: [Command.RESET, ...args], cwd })
2727

2828
return this.executeProcess(options)
2929
}
3030

3131
public async resetWorkspace(cwd: string) {
3232
await this.reset(cwd, Flag.HARD, Commit.HEAD)
3333

34-
const options = getOptions(
35-
cwd,
36-
Command.CLEAN,
37-
Flag.FORCE,
38-
Flag.DIRECTORIES,
39-
Flag.QUIET
40-
)
34+
const options = getOptions({
35+
args: [Command.CLEAN, Flag.FORCE, Flag.DIRECTORIES, Flag.QUIET],
36+
cwd
37+
})
4138

4239
return this.executeProcess(options)
4340
}
4441

4542
public async stageAll(cwd: string) {
4643
const gitRoot = await this.getGitRepositoryRoot(cwd)
47-
const options = getOptions(gitRoot, Command.ADD, Flag.DOT)
44+
const options = getOptions({ args: [Command.ADD, Flag.DOT], cwd: gitRoot })
4845

4946
return this.executeProcess(options)
5047
}

extension/src/cli/git/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { standardizePath } from '../../fileSystem/path'
55

66
export class GitCli extends Cli {
77
public async getGitRepositoryRoot(cwd: string) {
8-
const options = getOptions(cwd, Command.REV_PARSE, Flag.SHOW_TOPLEVEL)
8+
const options = getOptions({
9+
args: [Command.REV_PARSE, Flag.SHOW_TOPLEVEL],
10+
cwd
11+
})
912

1013
return standardizePath(await this.executeProcess(options))
1114
}

extension/src/cli/git/options.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import { Args } from './constants'
22
import { ProcessOptions } from '../../process/execution'
33

4-
export const getOptions = (cwd: string, ...args: Args): ProcessOptions => ({
5-
args,
4+
export const getOptions = ({
65
cwd,
7-
executable: 'git'
8-
})
6+
args = [],
7+
env
8+
}: {
9+
cwd: string
10+
args?: Args
11+
env?: NodeJS.ProcessEnv
12+
}): ProcessOptions => {
13+
const options: ProcessOptions = {
14+
args,
15+
cwd,
16+
executable: 'git'
17+
}
18+
19+
if (env) {
20+
options.env = env
21+
}
22+
23+
return options
24+
}

extension/src/cli/git/reader.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('GitReader', () => {
5454
expect(mockedCreateProcess).toHaveBeenCalledWith({
5555
args: ['branch'],
5656
cwd,
57+
env: { LANG: 'en_US.UTF-8' },
5758
executable: 'git'
5859
})
5960
})
@@ -82,6 +83,7 @@ describe('GitReader', () => {
8283
expect(mockedCreateProcess).toHaveBeenCalledWith({
8384
args: ['branch'],
8485
cwd,
86+
env: { LANG: 'en_US.UTF-8' },
8587
executable: 'git'
8688
})
8789
})

extension/src/cli/git/reader.ts

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GitCli } from '.'
33
import { Command, Flag } from './constants'
44
import { getOptions } from './options'
55
import { typeCheckCommands } from '..'
6-
import { cleanUpBranchName, trimAndSplit } from '../../util/stdout'
6+
import { trimAndSplit } from '../../util/stdout'
77
import { isDirectory } from '../../fileSystem'
88

99
export const autoRegisteredCommands = {
@@ -25,25 +25,20 @@ export class GitReader extends GitCli {
2525
)
2626

2727
public async hasChanges(cwd: string) {
28-
const options = getOptions(
29-
cwd,
30-
Command.DIFF,
31-
Flag.NAME_ONLY,
32-
Flag.RAW_WITH_NUL
33-
)
28+
const options = getOptions({
29+
args: [Command.DIFF, Flag.NAME_ONLY, Flag.RAW_WITH_NUL],
30+
cwd
31+
})
3432
const output = await this.executeProcess(options)
3533

3634
return !!output
3735
}
3836

3937
public async hasNoCommits(cwd: string) {
40-
const options = getOptions(
41-
cwd,
42-
Command.REV_LIST,
43-
Flag.NUMBER,
44-
'1',
45-
Flag.ALL
46-
)
38+
const options = getOptions({
39+
args: [Command.REV_LIST, Flag.NUMBER, '1', Flag.ALL],
40+
cwd
41+
})
4742
const output = await this.executeProcess(options)
4843

4944
return !output
@@ -54,15 +49,17 @@ export class GitReader extends GitCli {
5449
revision: string,
5550
revisions: string
5651
): Promise<string> {
57-
const options = getOptions(
58-
cwd,
59-
Command.LOG,
60-
revision,
61-
Flag.PRETTY_FORMAT_COMMIT_MESSAGE,
62-
Flag.SEPARATE_WITH_NULL,
63-
Flag.NUMBER,
64-
revisions
65-
)
52+
const options = getOptions({
53+
args: [
54+
Command.LOG,
55+
revision,
56+
Flag.PRETTY_FORMAT_COMMIT_MESSAGE,
57+
Flag.SEPARATE_WITH_NULL,
58+
Flag.NUMBER,
59+
revisions
60+
],
61+
cwd
62+
})
6663
try {
6764
return await this.executeProcess(options)
6865
} catch {
@@ -71,7 +68,7 @@ export class GitReader extends GitCli {
7168
}
7269

7370
public async getRemoteUrl(cwd: string): Promise<string> {
74-
const options = getOptions(cwd, Command.LS_REMOTE, Flag.GET_URL)
71+
const options = getOptions({ args: [Command.LS_REMOTE, Flag.GET_URL], cwd })
7572
try {
7673
return await this.executeProcess(options)
7774
} catch {
@@ -88,7 +85,10 @@ export class GitReader extends GitCli {
8885
}
8986

9087
public async getNumCommits(cwd: string, branch: string) {
91-
const options = getOptions(cwd, Command.REV_LIST, Flag.COUNT, branch)
88+
const options = getOptions({
89+
args: [Command.REV_LIST, Flag.COUNT, branch],
90+
cwd
91+
})
9292
try {
9393
const nbCommits = await this.executeProcess(options)
9494
return Number.parseInt(nbCommits)
@@ -98,37 +98,49 @@ export class GitReader extends GitCli {
9898
}
9999

100100
public async getBranches(cwd: string): Promise<string[]> {
101-
const options = getOptions(cwd, Command.BRANCH)
101+
const options = getOptions({
102+
args: [Command.BRANCH],
103+
cwd,
104+
env: { LANG: 'en_US.UTF-8' }
105+
})
102106
try {
103107
const branches = await this.executeProcess(options)
104-
return trimAndSplit(branches).map(cleanUpBranchName)
108+
return trimAndSplit(branches).map(branch =>
109+
this.cleanUpBranchName(branch)
110+
)
105111
} catch {
106112
return []
107113
}
108114
}
109115

110116
public async getCurrentBranch(cwd: string): Promise<string> {
111-
const options = getOptions(cwd, Command.BRANCH)
117+
const options = getOptions({
118+
args: [Command.BRANCH],
119+
cwd,
120+
env: { LANG: 'en_US.UTF-8' }
121+
})
112122
try {
113123
const branches = await this.executeProcess(options)
114124
const currentBranch = trimAndSplit(branches).find(
115125
branch => branch.indexOf('*') === 0
116126
)
117-
return (currentBranch && cleanUpBranchName(currentBranch)) || ''
127+
return (currentBranch && this.cleanUpBranchName(currentBranch)) || ''
118128
} catch {
119129
return ''
120130
}
121131
}
122132

123133
private async getUntrackedDirectories(cwd: string): Promise<string[]> {
124-
const options = getOptions(
125-
cwd,
126-
Command.LS_FILES,
127-
Flag.OTHERS,
128-
Flag.EXCLUDE_STANDARD,
129-
Flag.DIRECTORY,
130-
Flag.NO_EMPTY_DIRECTORY
131-
)
134+
const options = getOptions({
135+
args: [
136+
Command.LS_FILES,
137+
Flag.OTHERS,
138+
Flag.EXCLUDE_STANDARD,
139+
Flag.DIRECTORY,
140+
Flag.NO_EMPTY_DIRECTORY
141+
],
142+
cwd
143+
})
132144

133145
const output = await this.executeProcess(options)
134146
return this.getUris(cwd, trimAndSplit(output)).filter(path =>
@@ -137,12 +149,10 @@ export class GitReader extends GitCli {
137149
}
138150

139151
private async getUntrackedFiles(cwd: string): Promise<string[]> {
140-
const options = getOptions(
141-
cwd,
142-
Command.LS_FILES,
143-
Flag.OTHERS,
144-
Flag.EXCLUDE_STANDARD
145-
)
152+
const options = getOptions({
153+
args: [Command.LS_FILES, Flag.OTHERS, Flag.EXCLUDE_STANDARD],
154+
cwd
155+
})
146156

147157
const output = await this.executeProcess(options)
148158
return this.getUris(cwd, trimAndSplit(output))
@@ -151,4 +161,11 @@ export class GitReader extends GitCli {
151161
private getUris(repositoryRoot: string, relativePaths: string[]) {
152162
return relativePaths.map(path => resolve(repositoryRoot, path))
153163
}
164+
165+
private cleanUpBranchName(branch: string) {
166+
return branch
167+
.replace('* ', '')
168+
.replace(/\(HEAD\s\w+\s\w+\s/, '')
169+
.replace(')', '')
170+
}
154171
}

extension/src/util/stdout.test.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cleanUpBranchName, trimAndSplit } from './stdout'
1+
import { trimAndSplit } from './stdout'
22

33
describe('trimAndSplit', () => {
44
it('should return an empty array given an empty string', () => {
@@ -13,15 +13,3 @@ describe('trimAndSplit', () => {
1313
expect(trimAndSplit('a\nb\nc\n')).toStrictEqual(['a', 'b', 'c'])
1414
})
1515
})
16-
17-
describe('cleanUpBranchName', () => {
18-
it('should clean up a detached head message in English', () => {
19-
const branchName = '(HEAD detached at 786cfcd)'
20-
expect(cleanUpBranchName(branchName)).toStrictEqual('786cfcd')
21-
})
22-
23-
it('should clean up a detached head message in Spanish', () => {
24-
const branchName = '(HEAD desacoplado en 786cfcd)'
25-
expect(cleanUpBranchName(branchName)).toStrictEqual('786cfcd')
26-
})
27-
})

extension/src/util/stdout.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@ export const trimAndSplit = (stdout: string): string[] =>
44
.split('\n')
55
.map(line => line.trim())
66
.filter(Boolean)
7-
8-
export const cleanUpBranchName = (branch: string) =>
9-
branch
10-
.replace('* ', '')
11-
.replace(/\(HEAD\s\w+\s\w+\s/, '')
12-
.replace(')', '')

0 commit comments

Comments
 (0)