Skip to content

Commit ae675dc

Browse files
PeukkuMikko Peltola
authored andcommitted
Use git binary to resolve location of .git folder and current relative path
1 parent 2c5e9cb commit ae675dc

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Update/RecipePatcher.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function generatePatch(array $originalFiles, array $newFiles): RecipePatc
8383
}
8484
}
8585

86+
// Use git binary to get project path from repository root
87+
$prefix = trim($this->execute('git rev-parse --show-prefix', $this->rootDir));
8688
$tmpPath = sys_get_temp_dir().'/_flex_recipe_update'.uniqid(mt_rand(), true);
8789
$this->filesystem->mkdir($tmpPath);
8890

@@ -104,7 +106,7 @@ public function generatePatch(array $originalFiles, array $newFiles): RecipePatc
104106
$this->writeFiles($newFiles, $tmpPath);
105107
$this->execute('git add -A', $tmpPath);
106108

107-
$patchString = $this->execute('git diff --cached', $tmpPath);
109+
$patchString = $this->execute(sprintf('git diff --cached --src-prefix "a/%s" --dst-prefix "b/%s"', $prefix, $prefix), $tmpPath);
108110
$removedPatches = [];
109111
$patchString = DiffHelper::removeFilesFromPatch($patchString, $deletedModifiedFiles, $removedPatches);
110112

@@ -166,8 +168,9 @@ private function execute(string $command, string $cwd): string
166168
private function addMissingBlobs(array $blobs): array
167169
{
168170
$addedBlobs = [];
171+
$gitDir = trim($this->execute('git rev-parse --absolute-git-dir', $this->rootDir));
169172
foreach ($blobs as $hash => $contents) {
170-
$blobPath = $this->rootDir.'/'.$this->getBlobPath($hash);
173+
$blobPath = $gitDir.'/'.$this->getBlobPath($hash);
171174
if (file_exists($blobPath)) {
172175
continue;
173176
}
@@ -185,14 +188,15 @@ private function addMissingBlobs(array $blobs): array
185188
private function generateBlobs(array $originalFiles, string $originalFilesRoot): array
186189
{
187190
$addedBlobs = [];
191+
$originalFilesGitDir = trim($this->execute('git rev-parse --absolute-git-dir', $originalFilesRoot));
188192
foreach ($originalFiles as $filename => $contents) {
189193
// if the file didn't originally exist, no blob needed
190194
if (!file_exists($originalFilesRoot.'/'.$filename)) {
191195
continue;
192196
}
193197

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

198202
return $addedBlobs;
@@ -203,7 +207,7 @@ private function getBlobPath(string $hash): string
203207
$hashStart = substr($hash, 0, 2);
204208
$hashEnd = substr($hash, 2);
205209

206-
return '.git/objects/'.$hashStart.'/'.$hashEnd;
210+
return 'objects/'.$hashStart.'/'.$hashEnd;
207211
}
208212

209213
private function _applyPatchFile(RecipePatch $patch)

0 commit comments

Comments
 (0)