Skip to content

Commit 36a3dee

Browse files
committed
fix(publish): clean stale registry entries before publishing
2 parents 19586b9 + 5ffdab5 commit 36a3dee

1 file changed

Lines changed: 114 additions & 35 deletions

File tree

src/commands/PublishCommand.cpp

Lines changed: 114 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,79 @@ namespace vix::commands
799799
return r.exitCode == 0 && trim_copy(r.out) == "true";
800800
}
801801

802+
static int normalize_registry_worktree_for_publish(const fs::path &regRepo)
803+
{
804+
{
805+
const auto r = run_process_retry_debug(
806+
{"git", "-C", regRepo.string(), "fetch", "-q", "origin", "--prune"});
807+
808+
if (r.exitCode != 0)
809+
{
810+
vix::cli::util::err_line(
811+
std::cerr,
812+
"failed to fetch registry origin");
813+
814+
if (!r.err.empty())
815+
vix::cli::util::warn_line(std::cerr, r.err);
816+
817+
return r.exitCode;
818+
}
819+
}
820+
821+
{
822+
const auto r = run_process_retry_debug(
823+
{"git", "-C", regRepo.string(), "checkout", "-q", "-B", "main", "origin/main"});
824+
825+
if (r.exitCode != 0)
826+
{
827+
vix::cli::util::err_line(
828+
std::cerr,
829+
"failed to checkout registry main branch");
830+
831+
if (!r.err.empty())
832+
vix::cli::util::warn_line(std::cerr, r.err);
833+
834+
return r.exitCode;
835+
}
836+
}
837+
838+
{
839+
const auto r = run_process_retry_debug(
840+
{"git", "-C", regRepo.string(), "reset", "-q", "--hard", "origin/main"});
841+
842+
if (r.exitCode != 0)
843+
{
844+
vix::cli::util::err_line(
845+
std::cerr,
846+
"failed to reset registry to origin/main");
847+
848+
if (!r.err.empty())
849+
vix::cli::util::warn_line(std::cerr, r.err);
850+
851+
return r.exitCode;
852+
}
853+
}
854+
855+
{
856+
const auto r = run_process_retry_debug(
857+
{"git", "-C", regRepo.string(), "clean", "-q", "-fd", "--", "index"});
858+
859+
if (r.exitCode != 0)
860+
{
861+
vix::cli::util::err_line(
862+
std::cerr,
863+
"failed to clean untracked registry files");
864+
865+
if (!r.err.empty())
866+
vix::cli::util::warn_line(std::cerr, r.err);
867+
868+
return r.exitCode;
869+
}
870+
}
871+
872+
return 0;
873+
}
874+
802875
static bool command_exists_on_path(const std::string &exe)
803876
{
804877
// No shell: just try to execute "<exe> --version"
@@ -1022,6 +1095,22 @@ namespace vix::commands
10221095
return 1;
10231096
}
10241097

1098+
{
1099+
const int rc = normalize_registry_worktree_for_publish(regRepo);
1100+
if (rc != 0)
1101+
{
1102+
vix::cli::util::err_line(
1103+
std::cerr,
1104+
"failed to prepare local registry repo");
1105+
1106+
vix::cli::util::tip_line(
1107+
std::cerr,
1108+
"Check network access or run: vix registry sync");
1109+
1110+
return rc;
1111+
}
1112+
}
1113+
10251114
const fs::path entryPath = regIndex / registry_file_name(*ns, *name);
10261115
vix::cli::util::kv(std::cout, "entry", entryPath.string());
10271116

@@ -1074,10 +1163,18 @@ namespace vix::commands
10741163

10751164
if (entry["versions"].contains(resolvedVersion))
10761165
{
1077-
vix::cli::util::err_line(std::cerr,
1078-
"version already registered: " + pkgId + "@" + resolvedVersion);
1079-
vix::cli::util::warn_line(std::cerr,
1080-
"This tag/version is already present in the registry.");
1166+
vix::cli::util::err_line(
1167+
std::cerr,
1168+
"version already exists in registry main: " + pkgId + "@" + resolvedVersion);
1169+
1170+
vix::cli::util::warn_line(
1171+
std::cerr,
1172+
"This version is already present in the registry index.");
1173+
1174+
vix::cli::util::warn_line(
1175+
std::cerr,
1176+
"If a previous publish failed before PR merge, run: vix registry sync");
1177+
10811178
return 1;
10821179
}
10831180
}
@@ -1252,51 +1349,33 @@ namespace vix::commands
12521349
return 0;
12531350
}
12541351

1255-
std::error_code ec;
1256-
fs::create_directories(entryPath.parent_path(), ec);
1257-
1258-
try
1259-
{
1260-
write_json_or_throw(entryPath, entry);
1261-
}
1262-
catch (const std::exception &ex)
1263-
{
1264-
vix::cli::util::err_line(std::cerr,
1265-
std::string("failed to write registry entry: ") + ex.what());
1266-
return 1;
1267-
}
1268-
12691352
const std::string branch = branch_name(*ns, *name, resolvedVersion);
12701353
vix::cli::util::kv(std::cout, "branch", branch);
12711354

12721355
{
12731356
const auto r = run_process_retry_debug(
1274-
{"git", "-C", regRepo.string(), "pull", "-q", "--ff-only"});
1357+
{"git", "-C", regRepo.string(), "checkout", "-B", branch, "-q"});
12751358
if (r.exitCode != 0)
12761359
{
1277-
vix::cli::util::err_line(std::cerr,
1278-
"failed to update local registry repo (pull --ff-only)");
1279-
1360+
vix::cli::util::err_line(std::cerr, "failed to create branch: " + branch);
12801361
if (!r.err.empty())
12811362
vix::cli::util::warn_line(std::cerr, r.err);
1282-
1283-
vix::cli::util::tip_line(std::cerr,
1284-
"Run 'vix registry sync' to reset the local registry.");
1285-
12861363
return r.exitCode;
12871364
}
12881365
}
12891366

1367+
std::error_code ec;
1368+
fs::create_directories(entryPath.parent_path(), ec);
1369+
1370+
try
12901371
{
1291-
const auto r = run_process_retry_debug(
1292-
{"git", "-C", regRepo.string(), "checkout", "-B", branch, "-q"});
1293-
if (r.exitCode != 0)
1294-
{
1295-
vix::cli::util::err_line(std::cerr, "failed to create branch: " + branch);
1296-
if (!r.err.empty())
1297-
vix::cli::util::warn_line(std::cerr, r.err);
1298-
return r.exitCode;
1299-
}
1372+
write_json_or_throw(entryPath, entry);
1373+
}
1374+
catch (const std::exception &ex)
1375+
{
1376+
vix::cli::util::err_line(std::cerr,
1377+
std::string("failed to write registry entry: ") + ex.what());
1378+
return 1;
13001379
}
13011380

13021381
{

0 commit comments

Comments
 (0)