Skip to content

Commit 66dc0a3

Browse files
Martin Ågrengitster
authored andcommitted
gc: fix handling of crontab magic markers
On `git maintenance start`, we add a few entries to the user's cron table. We wrap our entries using two magic markers, "# BEGIN GIT MAINTENANCE SCHEDULE" and "# END GIT MAINTENANCE SCHEDULE". At a later `git maintenance stop`, we will go through the table and remove these lines. Or rather, we will remove the "BEGIN" marker, the "END" marker and everything between them. Alas, we have a bug in how we detect the "END" marker: we don't. As we loop through all the lines of the crontab, if we are in the "old region", i.e., the region we're aiming to remove, we make an early `continue` and don't get as far as checking for the "END" marker. Thus, once we've seen our "BEGIN", we remove everything until the end of the file. Rewrite the logic for identifying these markers. There are four cases that are mutually exclusive: The current line starts a region or it ends it, or it's firmly within the region, or it's outside of it (and should be printed). Signed-off-by: Martin Ågren <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 83fcadd commit 66dc0a3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

builtin/gc.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,11 +1554,10 @@ static int update_background_schedule(int run_maintenance)
15541554
while (!strbuf_getline_lf(&line, cron_list)) {
15551555
if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
15561556
in_old_region = 1;
1557-
if (in_old_region)
1558-
continue;
1559-
fprintf(cron_in, "%s\n", line.buf);
1560-
if (in_old_region && !strcmp(line.buf, END_LINE))
1557+
else if (in_old_region && !strcmp(line.buf, END_LINE))
15611558
in_old_region = 0;
1559+
else if (!in_old_region)
1560+
fprintf(cron_in, "%s\n", line.buf);
15621561
}
15631562

15641563
if (run_maintenance) {

t/t7900-maintenance.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,13 @@ test_expect_success 'start preserves existing schedule' '
446446
grep "Important information!" cron.txt
447447
'
448448

449+
test_expect_success 'stop preserves surrounding schedule' '
450+
echo "Crucial information!" >>cron.txt &&
451+
GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
452+
grep "Important information!" cron.txt &&
453+
grep "Crucial information!" cron.txt
454+
'
455+
449456
test_expect_success 'register preserves existing strategy' '
450457
git config maintenance.strategy none &&
451458
git maintenance register &&

0 commit comments

Comments
 (0)