Skip to content

Updating Recipe Fails When Using Git Sub-Modules #873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Updating Recipe Fails When Using Git Sub-Modules #873

wants to merge 2 commits into from

Conversation

PythooonUser
Copy link

Closes #864 .

When using git sub-modules and a project therein, composer cannot update recipes, as flex does not know how to handle sub-modules.

The .git folder gets replaced by a file with the following contents:

gitdir: ../.git/modules/project

This PR fixes the used path to follow the sub-module structure.

Questions

  • I need some help figuring out how to test this. We assume a git repository is used for all tests.
  • I'm certain that the regex needs improvements
  • There could still be an issue when installing a recipe the first time

@PythooonUser PythooonUser marked this pull request as draft February 23, 2022 05:59
@nicolas-grekas
Copy link
Member

@PythooonUser PythooonUser marked this pull request as draft 2 months ago

What's the status here?

About "There could still be an issue when installing a recipe the first time", this should be fixed by #907

@PythooonUser
Copy link
Author

Hey 👋 I have not further touched this PR (but I should). I had troubles with implementing a proper test, as the entire test structure assumes a single git repository, not a sub-modules structure.

@Peukku
Copy link
Contributor

Peukku commented May 16, 2022

Hi,
Taking you approach a bit further, I propose using git binary to resolve paths instead of making own function to figure out correct .git location.
git rev-parse --git-dir gives you the .git folder with path in both normal and submodule cases (that is .git or ../.git/modules/project).
in generateBlobs function we need absolute-git-dir because we need to access the repo in $tmpPath, not project repo.

So this should do the job:

diff --git a/src/Update/RecipePatcher.php b/src/Update/RecipePatcher.php
index 4e0c61a..533040c 100644
--- a/src/Update/RecipePatcher.php
+++ b/src/Update/RecipePatcher.php
@@ -166,8 +166,9 @@ class RecipePatcher
    private function addMissingBlobs(array $blobs): array
    {
        $addedBlobs = [];
+        $gitDir = trim($this->execute('git rev-parse --git-dir', $this->rootDir));
        foreach ($blobs as $hash => $contents) {
-            $blobPath = $this->rootDir.'/'.$this->getBlobPath($hash);
+            $blobPath = $gitDir.'/'.$this->getBlobPath($hash);
            if (file_exists($blobPath)) {
                continue;
            }
@@ -185,6 +186,7 @@ class RecipePatcher
    private function generateBlobs(array $originalFiles, string $originalFilesRoot): array
    {
        $addedBlobs = [];
+        $originalFilesGitDir = trim($this->execute('git rev-parse --absolute-git-dir', $originalFilesRoot));
        foreach ($originalFiles as $filename => $contents) {
            // if the file didn't originally exist, no blob needed
            if (!file_exists($originalFilesRoot.'/'.$filename)) {
@@ -192,7 +194,7 @@ class RecipePatcher
            }

            $hash = trim($this->execute('git hash-object '.ProcessExecutor::escape($filename), $originalFilesRoot));
-            $addedBlobs[$hash] = file_get_contents($originalFilesRoot.'/'.$this->getBlobPath($hash));
+            $addedBlobs[$hash] = file_get_contents($originalFilesGitDir.'/'.$this->getBlobPath($hash));
        }

        return $addedBlobs;
@@ -203,7 +205,7 @@ class RecipePatcher
        $hashStart = substr($hash, 0, 2);
        $hashEnd = substr($hash, 2);

-        return '.git/objects/'.$hashStart.'/'.$hashEnd;
+        return 'objects/'.$hashStart.'/'.$hashEnd;
    }

    private function _applyPatchFile(RecipePatch $patch)

@fabpot
Copy link
Member

fabpot commented Dec 20, 2022

Closing in favor of #937

@fabpot fabpot closed this Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants