Skip to content

Commit

Permalink
composer#11855 fix: detect git commands that need GIT_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-ciszewski committed May 10, 2024
1 parent ecf80ed commit 2a45dbd
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/Composer/Util/ProcessExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class ProcessExecutor
private const STATUS_FAILED = 4;
private const STATUS_ABORTED = 5;

private const GIT_CMDS_NEED_GIT_DIR = [
'show',
'log',
'branch',
['remote', 'seturl']
];

/** @var int */
protected static $timeout = 300;

Expand Down Expand Up @@ -108,11 +115,10 @@ private function doExecute($command, ?string $cwd, bool $tty, &$output = null):

$env = null;

$cmdContainsGit = is_array($command) ? in_array('git', $command, true) : is_int(stripos($command, 'git'));
$requiresGitDirEnv = $this->requiresGitDirEnv($command);
$isBareRepository = !is_dir(sprintf('%s/.git', trim((string) $cwd, '/')));

$dotGitDir = sprintf('%s/.git', trim((string) $cwd, '/'));
$hasGitDotDir = file_exists($dotGitDir);
if ($hasGitDotDir === false && $cmdContainsGit) {
if ($cwd !== null && $isBareRepository && $requiresGitDirEnv) {
$env = ['GIT_DIR' => $cwd];
}

Expand Down Expand Up @@ -488,4 +494,27 @@ private static function escapeArgument($argument): string

return $argument;
}

/**
* @param string[]|string $command
*/
public function requiresGitDirEnv($command): bool
{
$cmd = ! is_array($command) ? explode(' ', $command) : $command;
if ($cmd[0] !== 'git') {
return false;
}
$requiresGitDirEnv = false;
foreach (self::GIT_CMDS_NEED_GIT_DIR as $gitCmd) {
if (is_string($gitCmd) && in_array($gitCmd, $cmd, true)) {
return true;
}

if (is_array($gitCmd) && array_intersect($cmd, $gitCmd) === $gitCmd) {
return true;
}
}

return $requiresGitDirEnv;
}
}

0 comments on commit 2a45dbd

Please sign in to comment.