Skip to content

Commit 8919e76

Browse files
committed
no-mistakes(review): Make legacy-exclude cleanup command robust and hedge notice
1 parent ab75670 commit 8919e76

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎docs/features/prerelease.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ unlinked too. A clone linked by an earlier version of the script holds those ent
177177
lines outside any block, which `unlink` cannot tell from a line a user added. `link` and
178178
`unlink` name such lines and print the one command that removes them, and leave them in
179179
place; `link` does not add a second copy inside the block while one is there. Run that
180-
command once no worktree of the clone is linked. Files that *are* tracked by definition
180+
command once no worktree of the clone is linked, after taking out of it any line you added
181+
yourself. Files that *are* tracked by definition
181182
(`pyproject.toml`, `pom.xml`) get a loud warning instead — there is no way to make an edit
182183
to a tracked file uncommittable, which is precisely why the detector in §5 exists.
183184

‎tools/prerelease/prerelease-link.sh‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,16 @@ legacy_exclude_notice() {
267267
[ -n "$ex" ] && [ -f "$ex" ] || return 0
268268
stale="$(written_exclude_paths | sort -u | grep -xF -f <(exclude_outside_lines "$ex") || true)"
269269
[ -n "$stale" ] || return 0
270-
warn "$ex holds entries outside the managed block that name files this script writes"
271-
warn "(left by an earlier version of it); unlink never removes a line it did not put in its block:"
270+
warn "$ex holds entries outside the managed block that name files this script writes."
271+
warn "They may have been left by an earlier version of this script; keep any you added yourself."
272+
warn "unlink never removes a line it did not put in its block:"
272273
cmd="grep -vxF"
273274
while IFS= read -r p; do
274275
printf ' %s\n' "$p"
275276
cmd+=" -e $(printf '%q' "$p")"
276277
done <<< "$stale"
277278
say "once no work tree of this clone is linked, remove them with:"
278-
say " $cmd $(printf '%q' "$ex") > $(printf '%q' "$ex.tmp") && mv $(printf '%q' "$ex.tmp") $(printf '%q' "$ex")"
279+
say " { $cmd $(printf '%q' "$ex") || [ \$? -eq 1 ]; } > $(printf '%q' "$ex.tmp") && mv $(printf '%q' "$ex.tmp") $(printf '%q' "$ex")"
279280
}
280281

281282
# Keep an untracked managed file out of commits without touching the shared .gitignore or any

‎tools/prerelease/test-prerelease-link.sh‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,32 @@ check "link names the legacy line" 'grep -q "entries outside the managed block"
173173
unlink_project "$legacy"
174174
check "unlink names the legacy line" 'grep -q "entries outside the managed block" "$WORK/out" && grep -qx " .npmrc" "$WORK/out"'
175175
check "unlink leaves the legacy line in place" 'grep -qxF .npmrc "$lcommon/info/exclude"'
176-
cleanup_cmd="$(sed -n 's/^ grep -vxF /grep -vxF /p' "$WORK/out")"
176+
cleanup_command() { sed -n 's/^ { grep -vxF /{ grep -vxF /p' "$WORK/out"; }
177+
cleanup_cmd="$(cleanup_command)"
177178
check "the notice carries one cleanup command" '[ "$(printf "%s\n" "$cleanup_cmd" | grep -c .)" = 1 ]'
178179
eval "$cleanup_cmd"
179180
check "the printed cleanup command removes the legacy line" '! grep -qxF .npmrc "$lcommon/info/exclude"'
180181
unlink_project "$legacy"
181182
check "with no legacy line left, unlink prints no notice" '! grep -q "entries outside the managed block" "$WORK/out"'
182183

184+
# A clone made without git's templates had its exclude file created by the earlier script, so
185+
# the legacy line is all it holds. The printed command must still remove it: `grep -v` that
186+
# selects nothing exits 1, which is not a failure here.
187+
bare="$WORK/bare-legacy"
188+
git init -q "$bare"
189+
cp "$WORK/main/package.json" "$bare/package.json"
190+
git -C "$bare" add -A && git -C "$bare" commit -q -m init
191+
bcommon="$(git -C "$bare" rev-parse --git-common-dir)"
192+
case "$bcommon" in /*) ;; *) bcommon="$bare/$bcommon" ;; esac
193+
printf '.npmrc\n' > "$bcommon/info/exclude"
194+
link "$bare" || fail "link over an exclude file holding only the legacy line failed: $(tail -5 "$WORK/out")"
195+
unlink_project "$bare"
196+
cleanup_cmd="$(cleanup_command)"
197+
check "the notice names a cleanup command when the legacy line is the whole file" '[ -n "$cleanup_cmd" ]'
198+
eval "$cleanup_cmd" || fail "the printed cleanup command failed on an exclude file holding only the legacy line"
199+
check "the cleanup command removes a legacy line that is the whole file" '! grep -qxF .npmrc "$bcommon/info/exclude"'
200+
check "the cleanup command leaves no temporary file behind" '[ ! -e "$bcommon/info/exclude.tmp" ]'
201+
183202
# The assertion: an .npmrc git does not ignore makes link fail.
184203
git -C "$WORK/main" worktree add -q "$WORK/wt2" -b linked2
185204
# A global excludes file is not what we control, so defeat the ignore by negating it in .gitignore.

0 commit comments

Comments
 (0)