fix(cli): repair legacy core ledgers and make --keep-local durable
Store canonical core hashes in lock.core, add core.schema and repair legacy ledgers from the registered commit before deciding drift. Skipped --keep-local files remain tracked with the target canonical hash, fail closed when the old core cannot be resolved, and update core reports per-file actions.
This commit is contained in:
@@ -152,6 +152,7 @@ final class AdoptCommand extends Command
|
||||
|
||||
if (!$dryRun) {
|
||||
$lock->setCore([
|
||||
'schema' => 2,
|
||||
'version' => $provider->version(),
|
||||
'commit' => $provider->commit(),
|
||||
'files' => $files,
|
||||
|
||||
@@ -24,6 +24,20 @@ final class UpdateCommand extends Command
|
||||
if (($parsed['args'][0] ?? null) === 'core') {
|
||||
$provider = $this->coreProvider($parsed['options']);
|
||||
$result = $installer->updateCore($provider, $options);
|
||||
|
||||
$counts = [];
|
||||
foreach ($result['actions'] as $file => $action) {
|
||||
$this->console->out(' ' . $action . ' ' . $file);
|
||||
$counts[$action] = ($counts[$action] ?? 0) + 1;
|
||||
}
|
||||
if ($counts !== []) {
|
||||
$summary = [];
|
||||
foreach ($counts as $action => $count) {
|
||||
$summary[] = $action . '=' . $count;
|
||||
}
|
||||
$this->console->out(' resumen: ' . implode(' ', $summary));
|
||||
}
|
||||
|
||||
$this->console->ok('core ' . ($result['version'] ?? '?'));
|
||||
foreach ($result['aborted'] as $aborted) {
|
||||
$this->console->error('abortado: ' . $aborted);
|
||||
|
||||
@@ -189,4 +189,75 @@ final class CoreProvider
|
||||
|
||||
return (string) file_get_contents($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes canónicos de un conjunto de rutas en un ref histórico del core.
|
||||
* Se usa para reparar ledgers heredados que guardaron hashes locales.
|
||||
*
|
||||
* Prefiere el commit (identidad inmutable) y cae al tag `v<versión>`.
|
||||
* Devuelve null si el ref o alguna de las rutas no se pueden resolver.
|
||||
*
|
||||
* @param list<string> $paths
|
||||
* @return array<string, string>|null
|
||||
*/
|
||||
public function canonicalAt(?string $commit, ?string $version, array $paths): ?array
|
||||
{
|
||||
if (!$this->isGit() || $paths === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$refs = [];
|
||||
if ($commit !== null && $commit !== '') {
|
||||
$refs[] = $commit;
|
||||
}
|
||||
if ($version !== null && $version !== '') {
|
||||
$refs[] = 'v' . ltrim($version, 'vV');
|
||||
}
|
||||
|
||||
foreach (array_unique($refs) as $ref) {
|
||||
exec(
|
||||
'git -C ' . escapeshellarg($this->dir) . ' rev-parse --verify --quiet ' .
|
||||
escapeshellarg($ref . '^{commit}') . ' 2>/dev/null',
|
||||
$ignored,
|
||||
$code
|
||||
);
|
||||
if ($code !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$files = [];
|
||||
$ok = true;
|
||||
foreach ($paths as $relative) {
|
||||
$tmp = tempnam(sys_get_temp_dir(), 'dbcore');
|
||||
if ($tmp === false) {
|
||||
$ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
exec(
|
||||
'git -C ' . escapeshellarg($this->dir) . ' show ' .
|
||||
escapeshellarg($ref . ':' . $relative) .
|
||||
' > ' . escapeshellarg($tmp) . ' 2>/dev/null',
|
||||
$ignored,
|
||||
$exists
|
||||
);
|
||||
if ($exists !== 0) {
|
||||
@unlink($tmp);
|
||||
$ok = false;
|
||||
break;
|
||||
}
|
||||
|
||||
$content = (string) file_get_contents($tmp);
|
||||
@unlink($tmp);
|
||||
|
||||
$files[$relative] = 'sha256:' . hash('sha256', $content);
|
||||
}
|
||||
|
||||
if ($ok) {
|
||||
return $files;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,14 +15,12 @@ final class FileSync
|
||||
public const ABORTED = 'aborted';
|
||||
|
||||
/**
|
||||
* Decide qué haría `write()` sin tocar el disco.
|
||||
*
|
||||
* @param array{force?:bool, backup?:bool, keepLocal?:bool} $options
|
||||
*/
|
||||
public function write(string $path, string $contents, ?string $expected, array $options = []): string
|
||||
public function plan(string $path, string $contents, ?string $expected, array $options = []): string
|
||||
{
|
||||
if (!is_dir(dirname($path))) {
|
||||
mkdir(dirname($path), 0777, true);
|
||||
}
|
||||
|
||||
$existed = is_file($path);
|
||||
$drifted = $existed && Drift::isDrifted($path, $expected);
|
||||
|
||||
@@ -31,27 +29,17 @@ final class FileSync
|
||||
// heredado) un archivo existente también puede respaldarse.
|
||||
$changed = $existed && (string) file_get_contents($path) !== $contents;
|
||||
if ($changed && ($options['backup'] ?? false)) {
|
||||
copy($path, $path . '.bak');
|
||||
file_put_contents($path, $contents);
|
||||
|
||||
return self::BACKED_UP;
|
||||
}
|
||||
|
||||
file_put_contents($path, $contents);
|
||||
|
||||
return $existed ? self::OVERWRITTEN : self::WRITTEN;
|
||||
}
|
||||
|
||||
if ($options['force'] ?? false) {
|
||||
file_put_contents($path, $contents);
|
||||
|
||||
return self::OVERWRITTEN;
|
||||
}
|
||||
|
||||
if ($options['backup'] ?? false) {
|
||||
copy($path, $path . '.bak');
|
||||
file_put_contents($path, $contents);
|
||||
|
||||
return self::BACKED_UP;
|
||||
}
|
||||
|
||||
@@ -65,7 +53,34 @@ final class FileSync
|
||||
/**
|
||||
* @param array{force?:bool, backup?:bool, keepLocal?:bool} $options
|
||||
*/
|
||||
public function remove(string $path, ?string $expected, array $options = []): string
|
||||
public function write(string $path, string $contents, ?string $expected, array $options = []): string
|
||||
{
|
||||
if (!is_dir(dirname($path))) {
|
||||
mkdir(dirname($path), 0777, true);
|
||||
}
|
||||
|
||||
$action = $this->plan($path, $contents, $expected, $options);
|
||||
|
||||
if ($action === self::WRITTEN || $action === self::OVERWRITTEN) {
|
||||
file_put_contents($path, $contents);
|
||||
|
||||
return $action;
|
||||
}
|
||||
|
||||
if ($action === self::BACKED_UP) {
|
||||
copy($path, $path . '.bak');
|
||||
file_put_contents($path, $contents);
|
||||
}
|
||||
|
||||
return $action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide qué haría `remove()` sin tocar el disco.
|
||||
*
|
||||
* @param array{force?:bool, backup?:bool, keepLocal?:bool} $options
|
||||
*/
|
||||
public function planRemove(string $path, ?string $expected, array $options = []): string
|
||||
{
|
||||
if (!is_file($path)) {
|
||||
return self::SKIPPED;
|
||||
@@ -79,10 +94,28 @@ final class FileSync
|
||||
}
|
||||
|
||||
if ($options['backup'] ?? false) {
|
||||
copy($path, $path . '.bak');
|
||||
} else {
|
||||
return self::ABORTED;
|
||||
return self::BACKED_UP;
|
||||
}
|
||||
|
||||
return self::ABORTED;
|
||||
}
|
||||
|
||||
return self::OVERWRITTEN;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{force?:bool, backup?:bool, keepLocal?:bool} $options
|
||||
*/
|
||||
public function remove(string $path, ?string $expected, array $options = []): string
|
||||
{
|
||||
$action = $this->planRemove($path, $expected, $options);
|
||||
|
||||
if ($action === self::SKIPPED || $action === self::ABORTED) {
|
||||
return $action;
|
||||
}
|
||||
|
||||
if ($action === self::BACKED_UP) {
|
||||
copy($path, $path . '.bak');
|
||||
}
|
||||
|
||||
unlink($path);
|
||||
|
||||
@@ -271,8 +271,27 @@ final class Installer
|
||||
private function writeCore(CoreProvider $provider, array $options, bool $removeStale): array
|
||||
{
|
||||
$protected = $this->componentOwnedPaths();
|
||||
$expected = $this->lock->core()['files'] ?? [];
|
||||
$files = $provider->files();
|
||||
$core = $this->lock->core();
|
||||
$expected = $core['files'] ?? [];
|
||||
$origin = $core['origin'] ?? null;
|
||||
|
||||
if ($expected !== [] && $this->lock->coreSchema() < 2) {
|
||||
$canonical = $provider->canonicalAt(
|
||||
$core['commit'] ?? null,
|
||||
$core['version'] ?? null,
|
||||
array_keys($expected)
|
||||
);
|
||||
if ($canonical === null) {
|
||||
$ref = $core['commit'] ?? $core['version'] ?? '?';
|
||||
throw new ToolboxException(
|
||||
'No se pudo reparar el lock del core: la versión registrada (' . $ref . ') no está disponible. ' .
|
||||
'Actualiza la caché del core o apunta --core-dir a esa versión.'
|
||||
);
|
||||
}
|
||||
$expected = $canonical;
|
||||
}
|
||||
|
||||
$files = $provider->files();
|
||||
|
||||
$installed = [];
|
||||
$actions = [];
|
||||
@@ -283,15 +302,18 @@ final class Installer
|
||||
continue;
|
||||
}
|
||||
|
||||
$contents = $provider->read($relative);
|
||||
$absolute = $this->root . '/' . $relative;
|
||||
$hash = 'sha256:' . hash('sha256', $contents);
|
||||
|
||||
if ($options['dryRun'] ?? false) {
|
||||
$actions[$relative] = 'planned';
|
||||
$installed[$relative] = 'sha256:' . hash('sha256', $provider->read($relative));
|
||||
$planned = $this->sync->plan($absolute, $contents, $expected[$relative] ?? null, $options);
|
||||
$actions[$relative] = 'planned-' . $planned;
|
||||
$installed[$relative] = $hash;
|
||||
continue;
|
||||
}
|
||||
|
||||
$action = $this->sync->write($absolute, $provider->read($relative), $expected[$relative] ?? null, $options);
|
||||
$action = $this->sync->write($absolute, $contents, $expected[$relative] ?? null, $options);
|
||||
$actions[$relative] = $action;
|
||||
|
||||
if ($action === FileSync::ABORTED) {
|
||||
@@ -299,7 +321,9 @@ final class Installer
|
||||
continue;
|
||||
}
|
||||
|
||||
$installed[$relative] = Drift::hash($absolute);
|
||||
// Ledger canónico: también para archivos saltados con --keep-local,
|
||||
// de modo que sigan detectándose como drift en el próximo update.
|
||||
$installed[$relative] = $hash;
|
||||
}
|
||||
|
||||
if ($removeStale) {
|
||||
@@ -308,22 +332,43 @@ final class Installer
|
||||
continue;
|
||||
}
|
||||
if ($options['dryRun'] ?? false) {
|
||||
$actions[$relative] = 'remove-planned';
|
||||
$planned = $this->sync->planRemove($this->root . '/' . $relative, $hash, $options);
|
||||
$actions[$relative] = $planned === FileSync::OVERWRITTEN
|
||||
? 'planned-removed'
|
||||
: 'planned-' . $planned;
|
||||
continue;
|
||||
}
|
||||
$action = $this->sync->remove($this->root . '/' . $relative, $hash, $options);
|
||||
$absolute = $this->root . '/' . $relative;
|
||||
$action = $this->sync->remove($absolute, $hash, $options);
|
||||
if ($action === FileSync::ABORTED) {
|
||||
$aborted[] = $relative;
|
||||
continue;
|
||||
}
|
||||
if ($action === FileSync::SKIPPED) {
|
||||
if (is_file($absolute)) {
|
||||
$actions[$relative] = 'skipped';
|
||||
$this->console->warn('obsoleto conservado localmente: ' . $relative . ' (ya no gestionado)');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$actions[$relative] = 'removed';
|
||||
}
|
||||
}
|
||||
|
||||
if ($aborted === [] && !($options['dryRun'] ?? false)) {
|
||||
$this->lock->setCore([
|
||||
$newCore = [
|
||||
'schema' => 2,
|
||||
'version' => $provider->version() ?? $this->coreVersion(),
|
||||
'commit' => $provider->commit() ?? ($this->lock->core()['commit'] ?? null),
|
||||
'commit' => $provider->commit() ?? ($core['commit'] ?? null),
|
||||
'files' => $installed,
|
||||
]);
|
||||
];
|
||||
if ($origin !== null) {
|
||||
$newCore['origin'] = $origin;
|
||||
} elseif (!$removeStale) {
|
||||
$newCore['origin'] = 'toolbox';
|
||||
}
|
||||
|
||||
$this->lock->setCore($newCore);
|
||||
$this->lock->save($this->root);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,12 +58,21 @@ final class Lock
|
||||
return new self($data['core'] ?? null, $components);
|
||||
}
|
||||
|
||||
/** @return array{version?:string, commit?:string, files?:array<string,string>}|null */
|
||||
/** @return array{schema?:int, version?:string, commit?:string, origin?:string, files?:array<string,string>}|null */
|
||||
public function core(): ?array
|
||||
{
|
||||
return $this->core;
|
||||
}
|
||||
|
||||
/**
|
||||
* Esquema del ledger del core: 1 = heredado (hash del disco), 2 = hashes
|
||||
* canónicos. La ausencia de marca se interpreta como heredado.
|
||||
*/
|
||||
public function coreSchema(): int
|
||||
{
|
||||
return (int) ($this->core['schema'] ?? 1);
|
||||
}
|
||||
|
||||
/** @return array<string, array{package:string, version:string, toolbox_commit:?string, mode:string, files:array<string,string>}> */
|
||||
public function components(): array
|
||||
{
|
||||
@@ -80,7 +89,7 @@ final class Lock
|
||||
return $this->components[$id] ?? null;
|
||||
}
|
||||
|
||||
/** @param array{version?:string, commit?:string, files?:array<string,string>} $core */
|
||||
/** @param array{schema?:int, version?:string, commit?:string, origin?:string, files?:array<string,string>} $core */
|
||||
public function setCore(array $core): void
|
||||
{
|
||||
$this->core = $core;
|
||||
|
||||
Reference in New Issue
Block a user