From b10225f54d5670f09f83e82e09de9d820ada6931 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Mon, 9 Dec 2024 22:10:18 -0600 Subject: [PATCH] Merge pull request #32 from utopia-php/feat-git-shallow-clone feat: git optimizations --- .github/workflows/codeql-analysis.yml | 2 +- src/VCS/Adapter/Git/GitHub.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 3253e2c3..3408058d 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -12,5 +12,5 @@ jobs: - name: Run CodeQL run: | - docker run --rm -v $PWD:/app composer sh -c \ + docker run --rm -v $PWD:/app composer:2.6 sh -c \ "composer install --profile --ignore-platform-reqs && composer check" \ No newline at end of file diff --git a/src/VCS/Adapter/Git/GitHub.php b/src/VCS/Adapter/Git/GitHub.php index 7e22428b..398b05df 100644 --- a/src/VCS/Adapter/Git/GitHub.php +++ b/src/VCS/Adapter/Git/GitHub.php @@ -532,22 +532,27 @@ 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 refs we don't need + "git config --add remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'", + // Disable fetching of tags + "git config remote.origin.tagopt --no-tags", ]; 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"; + $commands[] = "if git ls-remote --exit-code --heads origin {$branchName}; then git pull --depth=1 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}"; + $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}')"; + $commands[] = "git fetch --depth=1 origin refs/tags/$(git ls-remote --tags origin {$tagName} | tail -n 1 | awk -F '/' '{print $3}') && git checkout FETCH_HEAD"; break; }