Skip to content

Commit 487f153

Browse files
committed
use word dict
1 parent 7f6d74b commit 487f153

3 files changed

Lines changed: 57 additions & 29 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,6 +4314,7 @@
43144314
"webpack-cli": "4.2.0"
43154315
},
43164316
"dependencies": {
4317+
"@joaomoreno/unique-names-generator": "^5.2.0",
43174318
"@octokit/rest": "22.0.0",
43184319
"@octokit/types": "14.1.0",
43194320
"@vscode/codicons": "^0.0.36",

src/github/copilotRemoteAgent/gitOperationsManager.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { adjectives, animals, colors, NumberDictionary, uniqueNamesGenerator } from '@joaomoreno/unique-names-generator';
67
import vscode from 'vscode';
78
import { Repository } from '../../api/api';
89
import Logger from '../../common/logger';
@@ -13,7 +14,10 @@ export class GitOperationsManager {
1314

1415
async commitAndPushChanges(repoInfo: RepoInfo) {
1516
const { repository, remote, baseRef } = repoInfo;
16-
const asyncBranch = `copilot/vscode${Date.now()}`;
17+
const config = vscode.workspace.getConfiguration('git');
18+
const branchWhitespaceChar = config.get<string>('branchWhitespaceChar')!;
19+
20+
const asyncBranch = await this.generateRandomBranchName(repository, branchWhitespaceChar);
1721

1822
try {
1923
await repository.createBranch(asyncBranch, true);
@@ -138,4 +142,47 @@ export class GitOperationsManager {
138142
}
139143
}
140144
}
145+
146+
// Taken from https://github.com/microsoft/vscode/blob/e35e3b4e057450ea3d90c724fae5e3e9619b96fe/extensions/git/src/commands.ts#L3007
147+
private async generateRandomBranchName(repository: Repository, separator: string): Promise<string> {
148+
const config = vscode.workspace.getConfiguration('git');
149+
const branchRandomNameDictionary = config.get<string[]>('branchRandomName.dictionary')!;
150+
151+
const dictionaries: string[][] = [];
152+
for (const dictionary of branchRandomNameDictionary) {
153+
if (dictionary.toLowerCase() === 'adjectives') {
154+
dictionaries.push(adjectives);
155+
}
156+
if (dictionary.toLowerCase() === 'animals') {
157+
dictionaries.push(animals);
158+
}
159+
if (dictionary.toLowerCase() === 'colors') {
160+
dictionaries.push(colors);
161+
}
162+
if (dictionary.toLowerCase() === 'numbers') {
163+
dictionaries.push(NumberDictionary.generate({ length: 3 }));
164+
}
165+
}
166+
167+
if (dictionaries.length === 0) {
168+
return '';
169+
}
170+
171+
// 5 attempts to generate a random branch name
172+
for (let index = 0; index < 5; index++) {
173+
const randomName = uniqueNamesGenerator({
174+
dictionaries,
175+
length: dictionaries.length,
176+
separator
177+
});
178+
179+
// Check for local ref conflict
180+
const refs = await repository.getRefs?.({ pattern: `refs/heads/${randomName}` });
181+
if (!refs || refs.length === 0) {
182+
return randomName;
183+
}
184+
}
185+
186+
return '';
187+
}
141188
}

yarn.lock

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@
277277
"@types/istanbul-reports" "^1.1.1"
278278
"@types/yargs" "^13.0.0"
279279

280+
"@joaomoreno/unique-names-generator@^5.2.0":
281+
version "5.2.0"
282+
resolved "https://registry.yarnpkg.com/@joaomoreno/unique-names-generator/-/unique-names-generator-5.2.0.tgz#f7cf42c8734ef4b8f2c8d2ecb2baf9d4e08664ce"
283+
integrity sha512-JEh3qZ85Z6syFvQlhRGRyTPI1M5VticiiP8Xl8EV0XfyfI4Mwzd6Zw28BBrEgUJCYv/cpKCQClVj3J8Tn0KFiA==
284+
280285
"@jridgewell/gen-mapping@^0.3.0":
281286
version "0.3.2"
282287
resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz#c1aedc61e853f2bb9f5dfe6d4442d3b565b253b9"
@@ -6214,16 +6219,7 @@ streamx@^2.15.0, streamx@^2.21.0:
62146219
optionalDependencies:
62156220
bare-events "^2.2.0"
62166221

6217-
"string-width-cjs@npm:string-width@^4.2.0":
6218-
version "4.2.3"
6219-
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
6220-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
6221-
dependencies:
6222-
emoji-regex "^8.0.0"
6223-
is-fullwidth-code-point "^3.0.0"
6224-
strip-ansi "^6.0.1"
6225-
6226-
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
6222+
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
62276223
version "4.2.3"
62286224
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
62296225
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -6281,7 +6277,7 @@ stringify-entities@^4.0.0:
62816277
character-entities-html4 "^2.0.0"
62826278
character-entities-legacy "^3.0.0"
62836279

6284-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
6280+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
62856281
version "6.0.1"
62866282
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
62876283
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -6295,13 +6291,6 @@ strip-ansi@^4.0.0:
62956291
dependencies:
62966292
ansi-regex "^3.0.0"
62976293

6298-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
6299-
version "6.0.1"
6300-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
6301-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
6302-
dependencies:
6303-
ansi-regex "^5.0.1"
6304-
63056294
strip-ansi@^7.0.1:
63066295
version "7.1.2"
63076296
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.2.tgz#132875abde678c7ea8d691533f2e7e22bb744dba"
@@ -7199,16 +7188,7 @@ workerpool@^9.2.0:
71997188
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-9.3.4.tgz#f6c92395b2141afd78e2a889e80cb338fe9fca41"
72007189
integrity sha512-TmPRQYYSAnnDiEB0P/Ytip7bFGvqnSU6I2BcuSw7Hx+JSg/DsUi5ebYfc8GYaSdpuvOcEs6dXxPurOYpe9QFwg==
72017190

7202-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
7203-
version "7.0.0"
7204-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
7205-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
7206-
dependencies:
7207-
ansi-styles "^4.0.0"
7208-
string-width "^4.1.0"
7209-
strip-ansi "^6.0.0"
7210-
7211-
wrap-ansi@^7.0.0:
7191+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
72127192
version "7.0.0"
72137193
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
72147194
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==

0 commit comments

Comments
 (0)