Skip to content

Commit 6d7fe1e

Browse files
feat: enhance checkout to update existing git repositories and handle remote changes
Co-authored-by: aider (anthropic/claude-sonnet-4-20250514) <[email protected]>
1 parent b9c6c47 commit 6d7fe1e

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/Downloader.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,43 @@ protected function checkout($name, $repo, $version)
182182
$target = $this->datadir . '/src/' . $name;
183183
$tmp = $this->datadir . '/meta/' . $name . '.tmp';
184184

185-
$this->info('Cloning {url}', ['url' => $repo]);
186-
187-
// clone the repo
188-
if (is_dir($tmp)) $this->delTree($tmp);
189185
$e_repo = escapeshellarg($repo);
186+
$e_target = escapeshellarg($target);
190187
$e_tmp = escapeshellarg($tmp);
191188
$ok = 0;
192-
system("GIT_TERMINAL_PROMPT=0 git clone $e_repo $e_tmp", $ok);
193-
if ($ok !== 0) throw new Exception('Cloning failed');
194189

195-
// move cloned directory to target
196-
if (is_dir($target)) $this->delTree($target);
197-
rename($tmp, $target);
198-
file_put_contents($last, $version);
190+
// Check if target is already a git repository
191+
if (is_dir($target) && is_dir($target . '/.git')) {
192+
$this->info('Updating existing git repository {url}', ['url' => $repo]);
193+
194+
// Change to target directory and update
195+
system("cd $e_target && GIT_TERMINAL_PROMPT=0 git remote set-url origin $e_repo", $ok);
196+
if ($ok !== 0) throw new Exception('Failed to update remote URL');
197+
198+
system("cd $e_target && GIT_TERMINAL_PROMPT=0 git fetch origin", $ok);
199+
if ($ok !== 0) throw new Exception('Failed to fetch from remote');
200+
201+
system("cd $e_target && GIT_TERMINAL_PROMPT=0 git reset --hard origin/HEAD", $ok);
202+
if ($ok !== 0) throw new Exception('Failed to reset to remote HEAD');
203+
204+
system("cd $e_target && GIT_TERMINAL_PROMPT=0 git clean -fd", $ok);
205+
if ($ok !== 0) throw new Exception('Failed to clean working directory');
199206

207+
} else {
208+
$this->info('Cloning {url}', ['url' => $repo]);
209+
210+
// clone the repo
211+
if (is_dir($tmp)) $this->delTree($tmp);
212+
213+
system("GIT_TERMINAL_PROMPT=0 git clone $e_repo $e_tmp", $ok);
214+
if ($ok !== 0) throw new Exception('Cloning failed');
215+
216+
// move cloned directory to target
217+
if (is_dir($target)) $this->delTree($target);
218+
rename($tmp, $target);
219+
}
220+
221+
file_put_contents($last, $version);
200222
$this->success('Downloaded {p} version {d}', ['p' => $name, 'd' => $version]);
201223
}
202224

0 commit comments

Comments
 (0)