fix(cli): adopt core records canonical hashes and flags local edits
This commit is contained in:
@@ -89,7 +89,7 @@ final class AdoptCommand extends Command
|
||||
}
|
||||
}
|
||||
|
||||
$coreAdopted = $this->adoptCore($parsed['options'], $root, $lock, $dryRun);
|
||||
[$coreAdopted, $coreHasLocal] = $this->adoptCore($parsed['options'], $root, $lock, $dryRun);
|
||||
|
||||
if (!$dryRun) {
|
||||
$lock->save($root);
|
||||
@@ -100,7 +100,8 @@ final class AdoptCommand extends Command
|
||||
$this->console->ok('adoptado ' . $id . $mark);
|
||||
}
|
||||
if ($coreAdopted > 0) {
|
||||
$this->console->ok("adoptado el core ({$coreAdopted} archivos)");
|
||||
$mark = $coreHasLocal ? ' (con ediciones locales)' : '';
|
||||
$this->console->ok("adoptado el core ({$coreAdopted} archivos){$mark}");
|
||||
}
|
||||
if ($adopted === [] && $coreAdopted === 0) {
|
||||
$this->console->out('No se encontró nada que adoptar.');
|
||||
@@ -111,33 +112,42 @@ final class AdoptCommand extends Command
|
||||
|
||||
/**
|
||||
* Registra el core existente: sólo los archivos que también existen en el
|
||||
* core de referencia, con su hash actual. No toca archivos.
|
||||
* core de referencia, con su hash CANÓNICO. Si la copia local difiere,
|
||||
* el archivo queda marcado como edición local y `update core` preguntará
|
||||
* antes de pisarla (mismo contrato que `adoptComponent`).
|
||||
*
|
||||
* @param array<string, string|bool> $options
|
||||
* @return array{0:int,1:bool}
|
||||
*/
|
||||
private function adoptCore(array $options, string $root, $lock, bool $dryRun): int
|
||||
private function adoptCore(array $options, string $root, $lock, bool $dryRun): array
|
||||
{
|
||||
if ($lock->core() !== null) {
|
||||
return 0;
|
||||
return [0, false];
|
||||
}
|
||||
|
||||
try {
|
||||
$provider = $this->coreProvider($options);
|
||||
} catch (ToolboxException $e) {
|
||||
$this->console->warn('no se pudo resolver el core para adoptar: ' . $e->getMessage());
|
||||
return 0;
|
||||
return [0, false];
|
||||
}
|
||||
|
||||
$files = [];
|
||||
$files = [];
|
||||
$hasLocal = false;
|
||||
foreach ($provider->files() as $relative) {
|
||||
$absolute = $root . '/' . $relative;
|
||||
if (is_file($absolute)) {
|
||||
$files[$relative] = Drift::hash($absolute);
|
||||
if (!is_file($absolute)) {
|
||||
continue;
|
||||
}
|
||||
$canonical = $provider->read($relative);
|
||||
$files[$relative] = 'sha256:' . hash('sha256', $canonical);
|
||||
if (file_get_contents($absolute) !== $canonical) {
|
||||
$hasLocal = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($files === []) {
|
||||
return 0;
|
||||
return [0, false];
|
||||
}
|
||||
|
||||
if (!$dryRun) {
|
||||
@@ -145,9 +155,10 @@ final class AdoptCommand extends Command
|
||||
'version' => $provider->version(),
|
||||
'commit' => $provider->commit(),
|
||||
'files' => $files,
|
||||
'origin' => $hasLocal ? 'local' : 'adopted',
|
||||
]);
|
||||
}
|
||||
|
||||
return count($files);
|
||||
return [count($files), $hasLocal];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user