fix: master and main

This commit is contained in:
hanyixuanten
2026-08-14 17:44:34 +08:00
parent 14c1373f01
commit 4c69987859
3 changed files with 90 additions and 3 deletions
+35 -2
View File
@@ -40,6 +40,38 @@ function git_service_write_repository_file($path, $contents) {
return @file_put_contents($path, $contents, LOCK_EX) === strlen($contents);
}
/* A newly-created repository starts with an unborn main branch. If the first
push creates a differently named branch, make that branch the default. */
function git_service_update_unborn_head($git_path, $updated_refs) {
$head = resolve_ref($git_path, 'HEAD');
if ($head[1] !== NULL) {
return TRUE;
}
$head_path = get_safe_file_path($git_path, '/HEAD');
$contents = $head_path === FALSE ? FALSE : @file_get_contents($head_path);
if ($contents === FALSE
|| !preg_match('~^ref:\s*(refs/heads/[^\r\n]+)\s*$~', $contents)) {
return TRUE;
}
$branch = NULL;
foreach ($updated_refs as $ref) {
$resolved = resolve_ref($git_path, $ref);
if (strpos($ref, 'refs/heads/') === 0 && $resolved[1] !== NULL) {
$branch = $ref;
break;
}
}
if ($branch === NULL) {
return TRUE;
}
$new_contents = "ref: ".$branch."\n";
return @file_put_contents($git_path.'/HEAD', $new_contents, LOCK_EX)
=== strlen($new_contents);
}
function git_service_init_bare_repository_with_php($path) {
$directories = array(
'',
@@ -482,9 +514,10 @@ function git_service_rpc($application, $repository, $request, $service, $input)
&& $service === 'git-receive-pack') {
if (!git_receive_pack_rpc_native($repository, $input)) {
error_log('Native Git receive-pack failed for '.$repository['url'].'.');
return 1;
}
return;
return 0;
}
git_service_run($application, $repository, $request, $service, FALSE, $input);
return git_service_run($application, $repository, $request, $service, FALSE, $input);
}
+6 -1
View File
@@ -231,6 +231,11 @@ function push_run_receive_pack($repository, $request, $application) {
send_error(403, 'Forbidden', $validation);
}
git_service_rpc($application, $repository, $request, 'git-receive-pack', $input);
$exit_code = git_service_rpc(
$application, $repository, $request, 'git-receive-pack', $input);
if ($exit_code === 0
&& !git_service_update_unborn_head($repository['path'], $updates)) {
error_log('Unable to update unborn HEAD for '.$repository['url'].'.');
}
fclose($input);
}
+49
View File
@@ -0,0 +1,49 @@
<?php
require_once dirname(__DIR__).'/lib/repository.php';
require_once dirname(__DIR__).'/lib/git_service.php';
function test_remove_directory($path) {
if (!is_dir($path)) {
return;
}
foreach (scandir($path) as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$entry_path = $path.'/'.$entry;
if (is_dir($entry_path)) {
test_remove_directory($entry_path);
} else {
unlink($entry_path);
}
}
rmdir($path);
}
$path = sys_get_temp_dir().'/php-git-server-head-'.bin2hex(random_bytes(8)).'.git';
mkdir($path.'/refs/heads', 0777, TRUE);
mkdir($path.'/objects', 0777, TRUE);
file_put_contents($path.'/HEAD', "ref: refs/heads/main\n");
file_put_contents($path.'/refs/heads/master', str_repeat('1', 40)."\n");
try {
git_service_update_unborn_head($path, array('refs/heads/master'));
$head = file_get_contents($path.'/HEAD');
if ($head !== "ref: refs/heads/master\n") {
fwrite(STDERR, 'Expected HEAD to follow the first pushed branch, got: '.$head);
exit(1);
}
file_put_contents($path.'/refs/heads/main', str_repeat('2', 40)."\n");
git_service_update_unborn_head($path, array('refs/heads/master'));
$head = file_get_contents($path.'/HEAD');
if ($head !== "ref: refs/heads/master\n") {
fwrite(STDERR, "Expected an existing HEAD target to remain unchanged.\n");
exit(1);
}
} finally {
test_remove_directory($path);
}
fwrite(STDOUT, "unborn HEAD regression test passed\n");