Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit d442a9a

Browse files
authored
PR #1824: clean up images that erroneously think they’re in git
1 parent 2005dee commit d442a9a

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/build_cache.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,14 +1324,28 @@ def worktrees_fix(self):
13241324
// "*" // im.GIT_DIR)) }
13251325
wt_gits = { fs.Path(i).name
13261326
for i in glob.iglob("%s/worktrees/*" % self.root) }
1327+
# Unlink images that think they are in Git but are not. This should not
1328+
# happen, but it does, and I wasn’t able to figure out how it happened.
1329+
wt_gits_orphaned = wt_actuals - wt_gits
1330+
for img_dir in wt_gits_orphaned:
1331+
link = ch.storage.unpack_base // img_dir // im.GIT_DIR
1332+
ch.WARNING("image erroneously marked cached, fixing: %s" % link,
1333+
ch.BUG_REPORT_PLZ)
1334+
link.unlink()
1335+
wt_actuals -= wt_gits_orphaned
13271336
# Delete worktree data for images that no longer exist or aren’t
13281337
# Git-enabled any more.
13291338
wt_gits_deleted = wt_gits - wt_actuals
13301339
for wt in wt_gits_deleted:
13311340
(ch.storage.build_cache // "worktrees" // wt).rmtree()
13321341
ch.VERBOSE("deleted %d stale worktree metadatas" % len(wt_gits_deleted))
13331342
wt_gits -= wt_gits_deleted
1334-
assert (wt_gits == wt_actuals)
1343+
# Validate that the pointers are in sync now.
1344+
if (wt_gits != wt_actuals):
1345+
ch.ERROR("found images -> cache links: %s" % " ".join(wt_actuals))
1346+
ch.ERROR("found cache -> images links: %s" % " ".join(wt_gits))
1347+
ch.FATAL("build cache is desynchronized, cannot proceed",
1348+
ch.BUG_REPORT_PLZ)
13351349
# If storage directory moved, repair all the paths.
13361350
if (len(wt_gits) > 0):
13371351
wt_dir_stored = fs.Path(( ch.storage.build_cache

test/build/55_cache.bats

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ EOF
11001100
diff -u <(echo "$blessed_out") <(echo "$output" | treeonly)
11011101
}
11021102

1103+
11031104
@test "${tag}: multistage COPY" {
11041105
# Multi-stage build with no instructions in the first stage.
11051106
df_no=$(cat <<'EOF'
@@ -1502,6 +1503,7 @@ EOF
15021503
[[ ! -e $CH_IMAGE_STORAGE/img/tmpimg/var/lib/rpm/__db.001 ]]
15031504
}
15041505

1506+
15051507
@test "${tag}: restore ACLs, xattrs" { # issue #1287
15061508
# Check if test needs to be skipped
15071509
touch "$BATS_TMPDIR/tmpfs_test"
@@ -1538,3 +1540,39 @@ EOF
15381540
[[ $status -eq 0 ]]
15391541
[[ $output = *"user:$USER:r--"* ]]
15401542
}
1543+
1544+
1545+
@test "${tag}: orphaned worktrees" { # PR #1824
1546+
img_metadata=$CH_IMAGE_STORAGE/img/tmpimg/ch
1547+
img_to_git=$img_metadata/git
1548+
git_worktrees=$CH_IMAGE_STORAGE/bucache/worktrees
1549+
git_to_img=$git_worktrees/tmpimg
1550+
1551+
# pull image, should be unlinked
1552+
ch-image pull --no-cache scratch tmpimg
1553+
ch-image build-cache # rm leftover $git_to_img if it exists
1554+
ls -lh "$img_metadata" "$git_worktrees"
1555+
[[ ! -e "$img_to_git" ]]
1556+
[[ ! -e "$git_to_img" ]]
1557+
1558+
# add fake link
1559+
touch "$img_to_git"
1560+
ls -lh "$img_metadata" "$git_worktrees"
1561+
[[ -e "$img_to_git" ]]
1562+
[[ ! -e "$git_to_img" ]]
1563+
1564+
# ch-image should warn and fix instead of crashing
1565+
run ch-image list
1566+
echo "$output"
1567+
[[ $status -eq 0 ]]
1568+
[[ $output = *'image erroneously marked cached, fixing'* ]]
1569+
1570+
# warning should now be gone and the state be good
1571+
ls -lh "$img_metadata" "$git_worktrees"
1572+
[[ ! -e "$img_to_git" ]]
1573+
[[ ! -e "$git_to_img" ]]
1574+
run ch-image list
1575+
echo "$output"
1576+
[[ $status -eq 0 ]]
1577+
[[ $output != *'image erroneously marked cached, fixing'* ]]
1578+
}

0 commit comments

Comments
 (0)