Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74066bd

Browse files
committed
feat: set and remove git safe directory
1 parent 16ae6c4 commit 74066bd

File tree

5 files changed

+96
-24
lines changed

5 files changed

+96
-24
lines changed

__test__/git-auth-helper.int.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,25 @@ describe('git-auth-helper tests', () => {
4646

4747
await gitAuthHelper.removeAuth()
4848
})
49+
50+
it('tests adding and removing the safe.directory config', async () => {
51+
await git.config('safe.directory', '/another-value', true, true)
52+
53+
await gitAuthHelper.removeSafeDirectory()
54+
await gitAuthHelper.addSafeDirectory()
55+
56+
expect(
57+
await git.configExists('safe.directory', REPO_PATH, true)
58+
).toBeTruthy()
59+
60+
await gitAuthHelper.addSafeDirectory()
61+
await gitAuthHelper.removeSafeDirectory()
62+
63+
expect(
64+
await git.configExists('safe.directory', REPO_PATH, true)
65+
).toBeFalsy()
66+
expect(
67+
await git.configExists('safe.directory', '/another-value', true)
68+
).toBeTruthy()
69+
})
4970
})

dist/index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,9 @@ function createPullRequest(inputs) {
327327
// Create a git command manager
328328
const git = yield git_command_manager_1.GitCommandManager.create(repoPath);
329329
// Save and unset the extraheader auth config if it exists
330-
core.startGroup('Save persisted git credentials');
330+
core.startGroup('Prepare git configuration');
331331
gitAuthHelper = new git_auth_helper_1.GitAuthHelper(git);
332+
yield gitAuthHelper.addSafeDirectory();
332333
yield gitAuthHelper.savePersistedAuth();
333334
core.endGroup();
334335
// Init the GitHub client
@@ -486,9 +487,10 @@ function createPullRequest(inputs) {
486487
}
487488
finally {
488489
// Remove auth and restore persisted auth config if it existed
489-
core.startGroup('Restore persisted git credentials');
490+
core.startGroup('Restore git configuration');
490491
yield gitAuthHelper.removeAuth();
491492
yield gitAuthHelper.restorePersistedAuth();
493+
yield gitAuthHelper.removeSafeDirectory();
492494
core.endGroup();
493495
}
494496
});
@@ -544,14 +546,33 @@ const url_1 = __nccwpck_require__(7310);
544546
const utils = __importStar(__nccwpck_require__(918));
545547
class GitAuthHelper {
546548
constructor(git) {
549+
this.safeDirectoryConfigKey = 'safe.directory';
550+
this.safeDirectoryAdded = false;
547551
this.extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***';
548552
this.extraheaderConfigValueRegex = '^AUTHORIZATION:';
549553
this.persistedExtraheaderConfigValue = '';
550554
this.git = git;
551-
this.gitConfigPath = path.join(this.git.getWorkingDirectory(), '.git', 'config');
555+
this.workingDirectory = this.git.getWorkingDirectory();
556+
this.gitConfigPath = path.join(this.workingDirectory, '.git', 'config');
552557
const serverUrl = this.getServerUrl();
553558
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`;
554559
}
560+
addSafeDirectory() {
561+
return __awaiter(this, void 0, void 0, function* () {
562+
const exists = yield this.git.configExists(this.safeDirectoryConfigKey, this.workingDirectory, true);
563+
if (!exists) {
564+
yield this.git.config(this.safeDirectoryConfigKey, this.workingDirectory, true, true);
565+
this.safeDirectoryAdded = true;
566+
}
567+
});
568+
}
569+
removeSafeDirectory() {
570+
return __awaiter(this, void 0, void 0, function* () {
571+
if (this.safeDirectoryAdded) {
572+
yield this.git.tryConfigUnset(this.safeDirectoryConfigKey, this.workingDirectory, true);
573+
}
574+
});
575+
}
555576
savePersistedAuth() {
556577
return __awaiter(this, void 0, void 0, function* () {
557578
// Save and unset persisted extraheader credential in git config if it exists
@@ -732,14 +753,14 @@ class GitCommandManager {
732753
return yield this.exec(args, allowAllExitCodes);
733754
});
734755
}
735-
config(configKey, configValue, globalConfig) {
756+
config(configKey, configValue, globalConfig, add) {
736757
return __awaiter(this, void 0, void 0, function* () {
737-
yield this.exec([
738-
'config',
739-
globalConfig ? '--global' : '--local',
740-
configKey,
741-
configValue
742-
]);
758+
const args = ['config', globalConfig ? '--global' : '--local'];
759+
if (add) {
760+
args.push('--add');
761+
}
762+
args.push(...[configKey, configValue]);
763+
yield this.exec(args);
743764
});
744765
}
745766
configExists(configKey, configValue = '.', globalConfig) {

src/create-pull-request.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
4545
const git = await GitCommandManager.create(repoPath)
4646

4747
// Save and unset the extraheader auth config if it exists
48-
core.startGroup('Save persisted git credentials')
48+
core.startGroup('Prepare git configuration')
4949
gitAuthHelper = new GitAuthHelper(git)
50+
await gitAuthHelper.addSafeDirectory()
5051
await gitAuthHelper.savePersistedAuth()
5152
core.endGroup()
5253

@@ -252,9 +253,10 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
252253
core.setFailed(utils.getErrorMessage(error))
253254
} finally {
254255
// Remove auth and restore persisted auth config if it existed
255-
core.startGroup('Restore persisted git credentials')
256+
core.startGroup('Restore git configuration')
256257
await gitAuthHelper.removeAuth()
257258
await gitAuthHelper.restorePersistedAuth()
259+
await gitAuthHelper.removeSafeDirectory()
258260
core.endGroup()
259261
}
260262
}

src/git-auth-helper.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,49 @@ import * as utils from './utils'
88
export class GitAuthHelper {
99
private git: GitCommandManager
1010
private gitConfigPath: string
11+
private workingDirectory: string
12+
private safeDirectoryConfigKey = 'safe.directory'
13+
private safeDirectoryAdded = false
1114
private extraheaderConfigKey: string
1215
private extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***'
1316
private extraheaderConfigValueRegex = '^AUTHORIZATION:'
1417
private persistedExtraheaderConfigValue = ''
1518

1619
constructor(git: GitCommandManager) {
1720
this.git = git
18-
this.gitConfigPath = path.join(
19-
this.git.getWorkingDirectory(),
20-
'.git',
21-
'config'
22-
)
21+
this.workingDirectory = this.git.getWorkingDirectory()
22+
this.gitConfigPath = path.join(this.workingDirectory, '.git', 'config')
2323
const serverUrl = this.getServerUrl()
2424
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`
2525
}
2626

27+
async addSafeDirectory(): Promise<void> {
28+
const exists = await this.git.configExists(
29+
this.safeDirectoryConfigKey,
30+
this.workingDirectory,
31+
true
32+
)
33+
if (!exists) {
34+
await this.git.config(
35+
this.safeDirectoryConfigKey,
36+
this.workingDirectory,
37+
true,
38+
true
39+
)
40+
this.safeDirectoryAdded = true
41+
}
42+
}
43+
44+
async removeSafeDirectory(): Promise<void> {
45+
if (this.safeDirectoryAdded) {
46+
await this.git.tryConfigUnset(
47+
this.safeDirectoryConfigKey,
48+
this.workingDirectory,
49+
true
50+
)
51+
}
52+
}
53+
2754
async savePersistedAuth(): Promise<void> {
2855
// Save and unset persisted extraheader credential in git config if it exists
2956
this.persistedExtraheaderConfigValue = await this.getAndUnset()

src/git-command-manager.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ export class GitCommandManager {
7272
async config(
7373
configKey: string,
7474
configValue: string,
75-
globalConfig?: boolean
75+
globalConfig?: boolean,
76+
add?: boolean
7677
): Promise<void> {
77-
await this.exec([
78-
'config',
79-
globalConfig ? '--global' : '--local',
80-
configKey,
81-
configValue
82-
])
78+
const args: string[] = ['config', globalConfig ? '--global' : '--local']
79+
if (add) {
80+
args.push('--add')
81+
}
82+
args.push(...[configKey, configValue])
83+
await this.exec(args)
8384
}
8485

8586
async configExists(

0 commit comments

Comments
 (0)