Skip to content

Commit ec388e0

Browse files
authored
Merge pull request #2 from utopia-php/feat-clone-commit-hash
Added commit hash to git clone command
2 parents fc9c38a + 9ff6ba6 commit ec388e0

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

composer.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/VCS/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ abstract public function updateComment(string $owner, string $repositoryName, in
158158
/**
159159
* Generates a clone command using app access token
160160
*/
161-
abstract public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory): string;
161+
abstract public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory, string $commitHash = null): string;
162162

163163
/**
164164
* Parses webhook event payload

src/VCS/Adapter/Git/GitHub.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public function updateCommitStatus(string $repositoryName, string $commitHash, s
444444
/**
445445
* Generates a clone command using app access token
446446
*/
447-
public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory): string
447+
public function generateCloneCommand(string $owner, string $repositoryName, string $branchName, string $directory, string $rootDirectory, string $commitHash = null): string
448448
{
449449
if (empty($rootDirectory)) {
450450
$rootDirectory = '*';
@@ -459,6 +459,9 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri
459459
$directory = escapeshellarg($directory);
460460
$rootDirectory = escapeshellarg($rootDirectory);
461461
$branchName = escapeshellarg($branchName);
462+
if (!empty($commitHash)) {
463+
$commitHash = escapeshellarg($commitHash);
464+
}
462465

463466
$commands = [
464467
"mkdir -p {$directory}",
@@ -468,9 +471,14 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri
468471
"git remote add origin {$cloneUrl}",
469472
"git config core.sparseCheckout true",
470473
"echo {$rootDirectory} >> .git/info/sparse-checkout",
471-
"if git ls-remote --exit-code --heads origin {$branchName}; then git pull origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi"
472474
];
473475

476+
if (empty($commitHash)) {
477+
$commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi";
478+
} else {
479+
$commands[] = "git pull origin {$commitHash}";
480+
}
481+
474482
$fullCommand = implode(" && ", $commands);
475483

476484
return $fullCommand;

tests/VCS/Adapter/GitHubTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ public function testGenerateCloneCommand(): void
160160
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);
161161
}
162162

163+
public function testGenerateCloneCommandWithCommitHash(): void
164+
{
165+
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'main', 'test', '*', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4');
166+
$this->assertNotEmpty($gitCloneCommand);
167+
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);
168+
}
169+
163170
public function testUpdateComment(): void
164171
{
165172
$commentId = $this->vcsAdapter->updateComment('test-kh', 'test2', 1630320767, 'update');

tests/VCS/Base.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ abstract public function testUpdateComment(): void;
2121

2222
abstract public function testGenerateCloneCommand(): void;
2323

24+
abstract public function testGenerateCloneCommandWithCommitHash(): void;
25+
2426
abstract public function testgetEvent(): void;
2527

2628
abstract public function testGetRepositoryName(): void;

0 commit comments

Comments
 (0)