Skip to content

Commit 8814f27

Browse files
committed
Merge branch 'tb/rerere-wait-for-merge-rr-lock'
Instead of failing to record conflicts to be resolved immediately, wait while "rerere gc" is ongoing. * tb/rerere-wait-for-merge-rr-lock: rerere: add "gc --skip-locked" for auto maintenance rerere: wait for MERGE_RR.lock before giving up
2 parents 84733a6 + 5f6b6aa commit 8814f27

7 files changed

Lines changed: 157 additions & 12 deletions

File tree

‎Documentation/config/rerere.adoc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ rerere.enabled::
1010
enabled if there is an `rr-cache` directory under the
1111
`$GIT_DIR`, e.g. if "rerere" was previously used in the
1212
repository.
13+
14+
rerere.lockTimeout::
15+
The length of time, in milliseconds, to wait for the rerere
16+
lock when another process holds it, typically a background
17+
`git rerere gc`. Value 0 means not to wait at all; -1 means
18+
to wait indefinitely. Default is 1000 (i.e., wait for 1
19+
second). A `git rerere gc` run by `git maintenance run --auto`
20+
or `git gc --auto` does not wait and does nothing while the
21+
lock is held.

‎builtin/gc.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,14 @@ static int worktree_prune_condition(struct gc_config *cfg)
385385
return should_prune;
386386
}
387387

388-
static int maintenance_task_rerere_gc(struct maintenance_run_opts *opts UNUSED,
388+
static int maintenance_task_rerere_gc(struct maintenance_run_opts *opts,
389389
struct gc_config *cfg UNUSED)
390390
{
391391
struct child_process rerere_cmd = CHILD_PROCESS_INIT;
392392
rerere_cmd.git_cmd = 1;
393393
strvec_pushl(&rerere_cmd.args, "rerere", "gc", NULL);
394+
if (opts->auto_flag)
395+
strvec_push(&rerere_cmd.args, "--skip-locked");
394396
return run_command(&rerere_cmd);
395397
}
396398

‎builtin/rerere.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,21 @@ int cmd_rerere(int argc,
6060
struct repository *repo UNUSED)
6161
{
6262
struct string_list merge_rr = STRING_LIST_INIT_DUP;
63-
int autoupdate = -1, flags = 0;
63+
int autoupdate = -1, skip_locked = 0, flags = 0;
6464

6565
struct option options[] = {
6666
OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
6767
N_("register clean resolutions in index"), 1),
68+
OPT_HIDDEN_BOOL(0, "skip-locked", &skip_locked,
69+
N_("skip gc while another process holds the lock")),
6870
OPT_END(),
6971
};
7072

7173
argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
7274

75+
if (skip_locked && (argc < 1 || strcmp(argv[0], "gc")))
76+
die(_("the option '%s' requires '%s'"), "--skip-locked", "gc");
77+
7378
repo_config(the_repository, git_xmerge_config, NULL);
7479

7580
if (autoupdate == 1)
@@ -98,7 +103,8 @@ int cmd_rerere(int argc,
98103
if (!strcmp(argv[0], "clear")) {
99104
rerere_clear(the_repository, &merge_rr);
100105
} else if (!strcmp(argv[0], "gc"))
101-
rerere_gc(the_repository, &merge_rr);
106+
rerere_gc(the_repository, &merge_rr,
107+
skip_locked ? RERERE_NOWAIT : 0);
102108
else if (!strcmp(argv[0], "status")) {
103109
if (setup_rerere(the_repository, &merge_rr,
104110
flags | RERERE_READONLY) < 0)

‎rerere.c‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ static int rerere_enabled = -1;
3333
/* automatically update cleanly resolved paths to the index */
3434
static int rerere_autoupdate;
3535

36+
/* how long to wait for MERGE_RR.lock, in milliseconds */
37+
static int rerere_lock_timeout_ms = 1000;
38+
3639
#define RR_HAS_POSTIMAGE 1
3740
#define RR_HAS_PREIMAGE 2
3841
struct rerere_dir {
@@ -872,6 +875,8 @@ static void git_rerere_config(void)
872875
{
873876
repo_config_get_bool(the_repository, "rerere.enabled", &rerere_enabled);
874877
repo_config_get_bool(the_repository, "rerere.autoupdate", &rerere_autoupdate);
878+
repo_config_get_int(the_repository, "rerere.locktimeout",
879+
&rerere_lock_timeout_ms);
875880
repo_config(the_repository, git_default_config, NULL);
876881
}
877882

@@ -904,12 +909,30 @@ int setup_rerere(struct repository *r, struct string_list *merge_rr, int flags)
904909

905910
if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
906911
rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
907-
if (flags & RERERE_READONLY)
912+
if ((flags & RERERE_READONLY) && (flags & RERERE_NOWAIT))
913+
BUG("RERERE_NOWAIT does not apply with RERERE_READONLY");
914+
if (flags & RERERE_READONLY) {
908915
fd = 0;
909-
else
910-
fd = repo_hold_lock_file_for_update(r, &write_lock,
911-
git_path_merge_rr(r),
912-
LOCK_DIE_ON_ERROR);
916+
} else {
917+
int lock_flags = LOCK_DIE_ON_ERROR;
918+
int timeout_ms = rerere_lock_timeout_ms;
919+
920+
if (flags & RERERE_NOWAIT) {
921+
lock_flags = 0;
922+
timeout_ms = 0;
923+
}
924+
925+
/*
926+
* Another process may hold the lock for a while, e.g.
927+
* "git rerere gc" while it prunes rr-cache, so by default
928+
* wait for it instead of dying right away.
929+
*/
930+
fd = repo_hold_lock_file_for_update_timeout(r, &write_lock,
931+
git_path_merge_rr(r),
932+
lock_flags, timeout_ms);
933+
if (fd < 0)
934+
return -1;
935+
}
913936
read_rr(r, merge_rr);
914937
return fd;
915938
}
@@ -1293,7 +1316,7 @@ bool rerere_gc_needed(struct repository *r, size_t limit)
12931316
return needed;
12941317
}
12951318

1296-
void rerere_gc(struct repository *r, struct string_list *rr)
1319+
void rerere_gc(struct repository *r, struct string_list *rr, int flags)
12971320
{
12981321
struct string_list to_remove = STRING_LIST_INIT_DUP;
12991322
DIR *dir;
@@ -1303,7 +1326,7 @@ void rerere_gc(struct repository *r, struct string_list *rr)
13031326
timestamp_t cutoff_resolve;
13041327
struct strbuf buf = STRBUF_INIT;
13051328

1306-
if (setup_rerere(r, rr, 0) < 0)
1329+
if (setup_rerere(r, rr, flags) < 0)
13071330
return;
13081331

13091332
rerere_gc_cutoffs(r, &cutoff_resolve, &cutoff_noresolve);

‎rerere.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ struct repository;
1010
#define RERERE_AUTOUPDATE 01
1111
#define RERERE_NOAUTOUPDATE 02
1212
#define RERERE_READONLY 04
13+
/* If MERGE_RR.lock is taken, return -1 as if rerere were disabled */
14+
#define RERERE_NOWAIT 010
1315

1416
/*
1517
* Marks paths that have been hand-resolved and added to the
@@ -37,7 +39,7 @@ const char *rerere_path(struct strbuf *buf, const struct rerere_id *,
3739
int rerere_forget(struct repository *, struct pathspec *);
3840
int rerere_remaining(struct repository *, struct string_list *);
3941
void rerere_clear(struct repository *, struct string_list *);
40-
void rerere_gc(struct repository *, struct string_list *);
42+
void rerere_gc(struct repository *, struct string_list *, int);
4143

4244
/*
4345
* Check whether garbage collection for rerere entries is needed, which is

‎t/t4200-rerere.sh‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,85 @@ test_expect_success 'old records rest in peace' '
242242
test_path_is_missing $rr2/preimage
243243
'
244244

245+
test_expect_success 'gc --skip-locked does nothing while MERGE_RR is locked' '
246+
mkdir -p $rr2 &&
247+
echo Hello >$rr2/preimage &&
248+
test-tool chmtime =$just_over_15_days_ago $rr2/preimage &&
249+
250+
test_when_finished "rm -f .git/MERGE_RR.lock" &&
251+
>.git/MERGE_RR.lock &&
252+
git rerere gc --skip-locked 2>err &&
253+
test_must_be_empty err &&
254+
test_path_is_file $rr2/preimage
255+
'
256+
257+
test_expect_success 'gc --skip-locked prunes while MERGE_RR is not locked' '
258+
mkdir -p $rr2 &&
259+
echo Hello >$rr2/preimage &&
260+
test-tool chmtime =$just_over_15_days_ago $rr2/preimage &&
261+
262+
git rerere gc --skip-locked &&
263+
test_path_is_missing $rr2/preimage
264+
'
265+
266+
test_expect_success '--skip-locked is only accepted by gc' '
267+
test_must_fail git rerere --skip-locked clear 2>err &&
268+
test_grep "option .--skip-locked. requires .gc." err
269+
'
270+
271+
test_expect_success 'a held lock is waited out within rerere.lockTimeout' '
272+
git reset --hard &&
273+
rm -rf $rr &&
274+
test_when_finished "wait && rm -f .git/MERGE_RR.lock" &&
275+
>.git/MERGE_RR.lock &&
276+
{
277+
( sleep 1 && rm -f .git/MERGE_RR.lock ) &
278+
} &&
279+
test_must_fail git -c rerere.lockTimeout=5000 merge first 2>err &&
280+
test_grep ! "Unable to create.*MERGE_RR\.lock" err &&
281+
test_grep "^=======\$" $rr/preimage
282+
'
283+
284+
test_expect_success 'merge fails on a lock it cannot take' '
285+
git reset --hard &&
286+
rm -rf $rr &&
287+
test_when_finished "rm -f .git/MERGE_RR.lock" &&
288+
>.git/MERGE_RR.lock &&
289+
test_must_fail git -c rerere.lockTimeout=0 merge first 2>err &&
290+
test_grep "Unable to create.*MERGE_RR\.lock" err &&
291+
test_grep "^=======\$" a1 &&
292+
test_path_is_missing $rr/preimage
293+
'
294+
295+
test_expect_success 'rerere, forget, clear and gc fail on a lock they cannot take' '
296+
test_when_finished "rm -f .git/MERGE_RR.lock" &&
297+
>.git/MERGE_RR.lock &&
298+
test_must_fail git -c rerere.lockTimeout=0 rerere 2>err &&
299+
test_grep "Unable to create.*MERGE_RR\.lock" err &&
300+
test_must_fail git -c rerere.lockTimeout=0 rerere forget a1 2>err &&
301+
test_grep "Unable to create.*MERGE_RR\.lock" err &&
302+
test_must_fail git -c rerere.lockTimeout=0 rerere clear 2>err &&
303+
test_grep "Unable to create.*MERGE_RR\.lock" err &&
304+
test_must_fail git -c rerere.lockTimeout=0 rerere gc 2>err &&
305+
test_grep "Unable to create.*MERGE_RR\.lock" err
306+
'
307+
308+
test_expect_success 'rebase --abort fails on a lock it cannot take' '
309+
git reset --hard &&
310+
git checkout -b lock-held-abort third &&
311+
test_when_finished "git checkout third && git branch -D lock-held-abort" &&
312+
test_must_fail git rebase first &&
313+
test_when_finished "test_might_fail git rebase --abort" &&
314+
test_when_finished "rm -f .git/MERGE_RR.lock" &&
315+
>.git/MERGE_RR.lock &&
316+
test_must_fail git -c rerere.lockTimeout=0 rebase --abort 2>err &&
317+
test_grep "Unable to create.*MERGE_RR\.lock" err &&
318+
test_path_is_dir .git/rebase-merge &&
319+
rm .git/MERGE_RR.lock &&
320+
git rebase --abort &&
321+
test_path_is_missing .git/rebase-merge
322+
'
323+
245324
rerere_gc_custom_expiry_test () {
246325
five_days="$1" right_now="$2"
247326
test_expect_success "rerere gc with custom expiry ($five_days, $right_now)" '

‎t/t7900-maintenance.sh‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,17 @@ test_expect_rerere_gc () {
10071007
shift
10081008
fi
10091009

1010+
# An automatic run passes --skip-locked to "git rerere gc".
1011+
skip_locked=
1012+
case " $* " in
1013+
*" --auto "*)
1014+
skip_locked=--skip-locked
1015+
;;
1016+
esac
1017+
10101018
rm -f "rerere-gc.txt" &&
10111019
GIT_TRACE2_EVENT="$(pwd)/rerere-gc.txt" "$@" &&
1012-
test_subcommand $negate git rerere gc <rerere-gc.txt
1020+
test_subcommand $negate git rerere gc $skip_locked <rerere-gc.txt
10131021
}
10141022

10151023
test_expect_success 'rerere-gc task without --auto always collects garbage' '
@@ -1084,6 +1092,22 @@ test_expect_success 'rerere-gc task with --auto honors maintenance.rerere-gc.aut
10841092
test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=0 maintenance run --auto --task=rerere-gc
10851093
'
10861094

1095+
test_expect_success 'rerere-gc task with --auto succeeds while MERGE_RR is locked' '
1096+
test_when_finished "rm -rf .git/rr-cache .git/MERGE_RR.lock" &&
1097+
# Without "rr-cache", rerere is disabled and never takes the lock.
1098+
mkdir .git/rr-cache &&
1099+
>.git/MERGE_RR.lock &&
1100+
test_expect_rerere_gc git -c maintenance.rerere-gc.auto=-1 maintenance run --auto --task=rerere-gc
1101+
'
1102+
1103+
test_expect_success 'rerere-gc task without --auto fails while MERGE_RR is locked' '
1104+
test_when_finished "rm -rf .git/rr-cache .git/MERGE_RR.lock" &&
1105+
mkdir .git/rr-cache &&
1106+
>.git/MERGE_RR.lock &&
1107+
test_must_fail git -c rerere.lockTimeout=0 maintenance run --task=rerere-gc 2>err &&
1108+
test_grep "Unable to create.*MERGE_RR\.lock" err
1109+
'
1110+
10871111
test_expect_success '--auto and --schedule incompatible' '
10881112
test_must_fail git maintenance run --auto --schedule=daily 2>err &&
10891113
test_grep "cannot be used together" err

0 commit comments

Comments
 (0)