feat: determine the git dir with rev-parse

This commit is contained in:
Peter Evans 2023-01-20 15:27:35 +09:00
parent a3b217da0b
commit 6ff27e3964
4 changed files with 18 additions and 3 deletions

9
dist/index.js vendored
View file

@ -551,6 +551,7 @@ const url_1 = __nccwpck_require__(7310);
const utils = __importStar(__nccwpck_require__(918)); const utils = __importStar(__nccwpck_require__(918));
class GitAuthHelper { class GitAuthHelper {
constructor(git) { constructor(git) {
this.gitConfigPath = '';
this.safeDirectoryConfigKey = 'safe.directory'; this.safeDirectoryConfigKey = 'safe.directory';
this.safeDirectoryAdded = false; this.safeDirectoryAdded = false;
this.extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***'; this.extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***';
@ -558,7 +559,6 @@ class GitAuthHelper {
this.persistedExtraheaderConfigValue = ''; this.persistedExtraheaderConfigValue = '';
this.git = git; this.git = git;
this.workingDirectory = this.git.getWorkingDirectory(); this.workingDirectory = this.git.getWorkingDirectory();
this.gitConfigPath = path.join(this.workingDirectory, '.git', 'config');
const serverUrl = this.getServerUrl(); const serverUrl = this.getServerUrl();
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`; this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`;
} }
@ -640,6 +640,10 @@ class GitAuthHelper {
} }
gitConfigStringReplace(find, replace) { gitConfigStringReplace(find, replace) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
if (this.gitConfigPath.length === 0) {
const gitDir = yield this.git.getGitDirectory();
this.gitConfigPath = path.join(this.workingDirectory, gitDir, 'config');
}
let content = (yield fs.promises.readFile(this.gitConfigPath)).toString(); let content = (yield fs.promises.readFile(this.gitConfigPath)).toString();
const index = content.indexOf(find); const index = content.indexOf(find);
if (index < 0 || index != content.lastIndexOf(find)) { if (index < 0 || index != content.lastIndexOf(find)) {
@ -818,6 +822,9 @@ class GitCommandManager {
return output.stdout.trim().split(`${configKey} `)[1]; return output.stdout.trim().split(`${configKey} `)[1];
}); });
} }
getGitDirectory() {
return this.revParse('--git-dir');
}
getWorkingDirectory() { getWorkingDirectory() {
return this.workingDirectory; return this.workingDirectory;
} }

View file

@ -11,6 +11,7 @@
- Any uncommitted tracked or untracked changes are now stashed and restored at the end of the action run. Currently, this can only occur when using the `add-paths` input, which allows for changes to not be committed. Previously, any uncommitted changes would be destroyed. - Any uncommitted tracked or untracked changes are now stashed and restored at the end of the action run. Currently, this can only occur when using the `add-paths` input, which allows for changes to not be committed. Previously, any uncommitted changes would be destroyed.
- The proxy implementation has been revised but is not expected to have any change in behaviour. It continues to support the standard environment variables `http_proxy`, `https_proxy` and `no_proxy`. - The proxy implementation has been revised but is not expected to have any change in behaviour. It continues to support the standard environment variables `http_proxy`, `https_proxy` and `no_proxy`.
- The action now sets the git `safe.directory` configuration for the local repository path. The configuration is removed when the action completes. Fixes issue https://github.com/peter-evans/create-pull-request/issues/1170. - The action now sets the git `safe.directory` configuration for the local repository path. The configuration is removed when the action completes. Fixes issue https://github.com/peter-evans/create-pull-request/issues/1170.
- The action now determines the git directory path using the `git rev-parse --git-dir` command. This allows users with custom repository configurations to use the action.
## Updating from `v3` to `v4` ## Updating from `v3` to `v4`

View file

@ -7,7 +7,7 @@ import * as utils from './utils'
export class GitAuthHelper { export class GitAuthHelper {
private git: GitCommandManager private git: GitCommandManager
private gitConfigPath: string private gitConfigPath = ''
private workingDirectory: string private workingDirectory: string
private safeDirectoryConfigKey = 'safe.directory' private safeDirectoryConfigKey = 'safe.directory'
private safeDirectoryAdded = false private safeDirectoryAdded = false
@ -19,7 +19,6 @@ export class GitAuthHelper {
constructor(git: GitCommandManager) { constructor(git: GitCommandManager) {
this.git = git this.git = git
this.workingDirectory = this.git.getWorkingDirectory() this.workingDirectory = this.git.getWorkingDirectory()
this.gitConfigPath = path.join(this.workingDirectory, '.git', 'config')
const serverUrl = this.getServerUrl() const serverUrl = this.getServerUrl()
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader` this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`
} }
@ -133,6 +132,10 @@ export class GitAuthHelper {
find: string, find: string,
replace: string replace: string
): Promise<void> { ): Promise<void> {
if (this.gitConfigPath.length === 0) {
const gitDir = await this.git.getGitDirectory()
this.gitConfigPath = path.join(this.workingDirectory, gitDir, 'config')
}
let content = (await fs.promises.readFile(this.gitConfigPath)).toString() let content = (await fs.promises.readFile(this.gitConfigPath)).toString()
const index = content.indexOf(find) const index = content.indexOf(find)
if (index < 0 || index != content.lastIndexOf(find)) { if (index < 0 || index != content.lastIndexOf(find)) {

View file

@ -146,6 +146,10 @@ export class GitCommandManager {
return output.stdout.trim().split(`${configKey} `)[1] return output.stdout.trim().split(`${configKey} `)[1]
} }
getGitDirectory(): Promise<string> {
return this.revParse('--git-dir')
}
getWorkingDirectory(): string { getWorkingDirectory(): string {
return this.workingDirectory return this.workingDirectory
} }