Skip to content

Commit

Permalink
feat: git optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Dec 6, 2024
1 parent d90ee68 commit 94127bb
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,25 +529,32 @@ public function generateCloneCommand(string $owner, string $repositoryName, stri
"git config --global init.defaultBranch main",
"git init",
"git remote add origin {$cloneUrl}",
// Enable sparse checkout
"git config core.sparseCheckout true",
"echo {$rootDirectory} >> .git/info/sparse-checkout",
// Disable fetching of tags to save space
"git config remote.origin.tagopt --no-tags",
// Disable fetching of refs we don't need
"git config --add remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'",
];

switch ($versionType) {
case self::CLONE_TYPE_BRANCH:
$branchName = escapeshellarg($version);
$commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi";
// Use --depth 1 for shallow clone and --single-branch to fetch only the needed branch
$commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull --depth=1 --single-branch origin {$branchName} && git checkout {$branchName}; else git checkout -b {$branchName}; fi";
break;
case self::CLONE_TYPE_COMMIT:
$commitHash = escapeshellarg($version);
$commands[] = "git pull origin {$commitHash}";
// For specific commit, fetch only that commit and its direct parent
$commands[] = "git fetch --depth=1 origin {$commitHash} && git checkout {$commitHash}";
break;
case self::CLONE_TYPE_TAG:
$tagName = escapeshellarg($version);
$commands[] = "git pull origin $(git ls-remote --tags origin {$tagName} | tail -n 1 | awk -F '/' '{print $3}')";
// For tags, fetch only the specific tag with minimal history
$commands[] = "git fetch --depth=1 --no-tags origin 'refs/tags/{$tagName}:refs/tags/{$tagName}' && git checkout tags/{$tagName}";
break;
}

$fullCommand = implode(" && ", $commands);

return $fullCommand;
Expand Down

0 comments on commit 94127bb

Please sign in to comment.