flex icon indicating copy to clipboard operation
flex copied to clipboard

Updating Recipe Fails When Using Git Sub-Modules

Open PythooonUser opened this issue 4 years ago • 3 comments

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 avatar Feb 18 '22 18:02 PythooonUser

@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

nicolas-grekas avatar May 02 '22 10:05 nicolas-grekas

Hey :wave: 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.

PythooonUser avatar May 12 '22 05:05 PythooonUser

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)

Peukku avatar May 16 '22 05:05 Peukku

Closing in favor of #937

fabpot avatar Dec 20 '22 07:12 fabpot