Hi,
while reviewing some patch series around git-rerere(1) recently I was a bit appalled by our custom subcommand parsing that we do here. This patch series here thus cleans up and modernizes that part a bit so that git-rerere(1) behaves like every other Git command that happens to have subcommands.
This series is based on 37b3475fb53 (The 9th batch, 2026-10-25) with ps/rerere-remove-global-state at b948c10c8b3 (rerere: get rid of global rerere cache state, 2026-10-23) merged into it.
Changes in v2:
- Document rerere flags.
- Rename `rerere_record()` to just `rerere()`.
- Link to v1: https://patch.msgid.link/20261027-pks-rerere-subcommands-v1-0-d836c36ce0c5@pks.im
Thanks!
Patrick
---
Patrick Steinhardt (12):
rerere: rename `repo_rerere()`
rerere: convert flags into an enum
builtin/rerere: drop `the_repository`
builtin/rerere: stop passing flags to subcommands that ignore them
builtin/rerere: turn "forget" into a proper subcommand
builtin/rerere: turn "clear" into a proper subcommand
builtin/rerere: turn "status" into a proper subcommand
builtin/rerere: turn "remaining" into a proper subcommand
builtin/rerere: turn "diff" into a proper subcommand
builtin/rerere: turn "gc" into a proper subcommand
builtin/rerere: use `OPT_SUBCOMMAND()`
builtin/rerere: introduce proper usage strings for subcommands Documentation/git-rerere.adoc | 8 +-
apply.c | 2 +-
builtin/am.c | 4 +-
builtin/commit.c | 2 +-
builtin/merge.c | 2 +-
builtin/rerere.c | 319 ++++++++++++++++++++++++++++++++----------
builtin/stash.c | 2 +-
rerere.c | 12 +-
rerere.h | 37 +++--
sequencer.c | 4 +-
t/t4200-rerere.sh | 5 -
11 files changed, 291 insertions(+), 106 deletions(-)
Range-diff versus v1:
1: ce75cde848d ! 1: 1f3fbbad575 rerere: rename `repo_rerere()`
@@ Commit message
repository as first parameter, that's not a good-enough reason to give
it a `repo_` prefix.
- Rename the function to `rerere_record()` to be consistent with all the
- other functions.
+ Rename the function to just `rerere()`. Move around and extend
+ documentation of this function to better explain what it does.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
@@ apply.c: static int write_out_results(struct apply_state *state, struct patch *l
*/
if (!state->cached)
- repo_rerere(state->repo, 0);
-+ rerere_record(state->repo, 0);
++ rerere(state->repo, 0);
}
return errs;
@@ builtin/am.c: static int fall_back_threeway(const struct am_state *state, const
if (merge_ort_generic(&o, &our_tree, &their_tree, 1, bases, &result)) {
- repo_rerere(the_repository, state->allow_rerere_autoupdate);
-+ rerere_record(the_repository, state->allow_rerere_autoupdate);
++ rerere(the_repository, state->allow_rerere_autoupdate);
free(their_tree_name);
return error(_("Failed to merge in the changes."));
}
@@ builtin/am.c: static void am_resolve(struct am_state *state, int allow_empty)
}
- repo_rerere(the_repository, 0);
-+ rerere_record(the_repository, 0);
++ rerere(the_repository, 0);
do_commit(state);
@@ builtin/commit.c: int cmd_commit(int argc,
git_test_write_commit_graph_or_die(the_repository);
- repo_rerere(the_repository, 0);
-+ rerere_record(the_repository, 0);
++ rerere(the_repository, 0);
run_auto_maintenance(the_repository, quiet);
run_commit_hook(use_editor, repo_get_index_file(the_repository),
NULL, "post-commit", NULL);
@@ builtin/merge.c: static int suggest_conflicts(void)
strbuf_release(&msgbuf);
fclose(fp);
- repo_rerere(the_repository, allow_rerere_auto);
-+ rerere_record(the_repository, allow_rerere_auto);
++ rerere(the_repository, allow_rerere_auto);
printf(_("Automatic merge failed; "
"fix conflicts and then commit the result.\n"));
return 1;
@@ builtin/rerere.c: int cmd_rerere(int argc,
if (argc < 1)
- return repo_rerere(the_repository, flags);
-+ return rerere_record(the_repository, flags);
++ return rerere(the_repository, flags);
if (!strcmp(argv[0], "forget")) {
struct pathspec pathspec;
@@ builtin/stash.c: static enum stash_apply_result do_apply_stash(const char *prefi
if (ret) {
- repo_rerere(the_repository, 0);
-+ rerere_record(the_repository, 0);
++ rerere(the_repository, 0);
if (index)
fprintf_ln(stderr, _("Index was not unstashed."));
## rerere.c ##
@@ rerere.c: int rerere_cache_setup(struct rerere_cache *cache,
- * perform mergy operations, possibly leaving conflicted index entries
- * and working tree files.
- */
+ return fd;
+ }
+
+-/*
+- * The main entry point that is called internally from codepaths that
+- * perform mergy operations, possibly leaving conflicted index entries
+- * and working tree files.
+- */
-int repo_rerere(struct repository *r, int flags)
-+int rerere_record(struct repository *r, int flags)
++int rerere(struct repository *r, int flags)
{
struct string_list merge_rr = STRING_LIST_INIT_DUP;
struct rerere_cache cache;
@@ rerere.h: int rerere_cache_setup(struct rerere_cache *cache,
void rerere_cache_release(struct rerere_cache *cache);
-int repo_rerere(struct repository *, int);
-+int rerere_record(struct repository *, int);
++/*
++ * The main entry point that is called internally from codepaths that
++ * perform mergy operations, possibly leaving conflicted index entries
++ * and working tree files. It records (and optionally updates) resolved
++ * conflicts and reuses recorded resolutions for unresolved conflicts.
++ */
++int rerere(struct repository *r, int flags);
++
/*
* Given the conflict ID and the name of a "file" used for replaying
* the recorded resolution (e.g. "preimage", "postimage"), return the
@@ sequencer.c: static enum pick_result do_pick_commit(struct repository *r,
short_commit_name(r, commit), msg.subject);
print_advice(r, res == 1, opts);
- repo_rerere(r, opts->allow_rerere_auto);
-+ rerere_record(r, opts->allow_rerere_auto);
++ rerere(r, opts->allow_rerere_auto);
goto leave;
}
@@ sequencer.c: static int do_merge(struct repository *r,
rollback_lock_file(&lock);
if (ret)
- repo_rerere(r, opts->allow_rerere_auto);
-+ rerere_record(r, opts->allow_rerere_auto);
++ rerere(r, opts->allow_rerere_auto);
else
/*
* In case of problems, we now want to return a positive
2: 18259e1556a ! 2: e910458a47c rerere: convert flags into an enum
@@ Commit message
Our modern coding guidelines say that flags should typically be passed
around as enums so that it becomes trivial to see what kind of flags are
- expected. Adapt the rerere flags accordingly.
+ expected. Adapt the rerere flags accordingly and document them.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
@@ rerere.c: static int is_rerere_enabled(struct repository *r)
int lock_timeout_ms = 1000;
int fd;
@@ rerere.c: int rerere_cache_setup(struct rerere_cache *cache,
- * perform mergy operations, possibly leaving conflicted index entries
- * and working tree files.
- */
--int rerere_record(struct repository *r, int flags)
-+int rerere_record(struct repository *r, enum rerere_flags flags)
+ return fd;
+ }
+
+-int rerere(struct repository *r, int flags)
++int rerere(struct repository *r, enum rerere_flags flags)
{
struct string_list merge_rr = STRING_LIST_INIT_DUP;
struct rerere_cache cache;
@@ rerere.h: struct rerere_id {
};
+enum rerere_flags {
++ /*
++ * Auto-update the index with previously recorded resolutions. This can
++ * be used to explicitly override "rerere.autoUpdate".
++ */
+ RERERE_AUTOUPDATE = (1 << 0),
++
++ /* Reverse of RERERE_AUTOUPDATE. */
+ RERERE_NOAUTOUPDATE = (1 << 1),
++
++ /* Skip acquiring the MERGE_RR.lock for a read-only operation. */
+ RERERE_READONLY = (1 << 2),
-+ /* If MERGE_RR.lock is taken, return -1 as if rerere were disabled */
++
++ /* If MERGE_RR.lock is taken, return -1 as if rerere were disabled. */
+ RERERE_NOWAIT = (1 << 3),
+};
+
@@ rerere.h: struct rerere_id {
+ enum rerere_flags flags);
void rerere_cache_release(struct rerere_cache *cache);
--int rerere_record(struct repository *, int);
-+int rerere_record(struct repository *r, enum rerere_flags flags);
+ /*
+@@ rerere.h: void rerere_cache_release(struct rerere_cache *cache);
+ * and working tree files. It records (and optionally updates) resolved
+ * conflicts and reuses recorded resolutions for unresolved conflicts.
+ */
+-int rerere(struct repository *r, int flags);
++int rerere(struct repository *r, enum rerere_flags flags);
+
/*
* Given the conflict ID and the name of a "file" used for replaying
- * the recorded resolution (e.g. "preimage", "postimage"), return the
@@ rerere.h: const char *rerere_path(struct repository *,
int rerere_forget(struct repository *, struct pathspec *);
int rerere_remaining(struct repository *, struct string_list *);
3: 89ae664d90d ! 3: 5254d1ac3ba builtin/rerere: drop `the_repository`
@@ builtin/rerere.c: int cmd_rerere(int argc,
flags = RERERE_NOAUTOUPDATE;
if (argc < 1)
-- return rerere_record(the_repository, flags);
-+ return rerere_record(repo, flags);
+- return rerere(the_repository, flags);
++ return rerere(repo, flags);
if (!strcmp(argv[0], "forget")) {
struct pathspec pathspec;
4: 5eff73de7bc = 4: 8b24e253e6b builtin/rerere: stop passing flags to subcommands that ignore them
5: 621c5f6ab1b ! 5: 43dde532e63 builtin/rerere: turn "forget" into a proper subcommand
@@ builtin/rerere.c: static int diff_two(const char *file1, const char *label1,
const char *prefix,
@@ builtin/rerere.c: int cmd_rerere(int argc,
if (argc < 1)
- return rerere_record(repo, flags);
+ return rerere(repo, flags);
- if (!strcmp(argv[0], "forget")) {
- struct pathspec pathspec;
6: d01fa02064c = 6: 13fd07a2f53 builtin/rerere: turn "clear" into a proper subcommand
7: 2bebea6449e = 7: c5d48081cc9 builtin/rerere: turn "status" into a proper subcommand
8: 5594019f31f = 8: 5c6891f6a48 builtin/rerere: turn "remaining" into a proper subcommand
9: e8a2f8ce209 = 9: b7ed9ef810b builtin/rerere: turn "diff" into a proper subcommand
10: deebcee0379 = 10: 0ceff6d241b builtin/rerere: turn "gc" into a proper subcommand
11: 56037e9a761 ! 11: 3b8b4b65a53 builtin/rerere: use `OPT_SUBCOMMAND()`
@@ builtin/rerere.c: static int cmd_rerere_gc(int argc,
return 0;
}
-+static int cmd_rerere_record(int argc,
-+ const char **argv,
-+ const char *prefix,
-+ struct repository *repo)
++static int cmd_rerere_perform(int argc,
++ const char **argv,
++ const char *prefix,
++ struct repository *repo)
+{
+ int autoupdate = -1;
+ struct option options[] = {
@@ builtin/rerere.c: static int cmd_rerere_gc(int argc,
+ if (autoupdate == 0)
+ flags = RERERE_NOAUTOUPDATE;
+
-+ return rerere_record(repo, flags);
++ return rerere(repo, flags);
+}
+
static int cmd_rerere_remaining(int argc,
@@ builtin/rerere.c: int cmd_rerere(int argc,
+ int ret;
- if (argc < 1)
-- return rerere_record(repo, flags);
+- return rerere(repo, flags);
-
- if (!strcmp(argv[0], "forget"))
- return cmd_rerere_forget(argc, argv, prefix, repo);
@@ builtin/rerere.c: int cmd_rerere(int argc,
- return cmd_rerere_diff(argc, argv, prefix, repo);
- else
- usage_with_options(rerere_usage, options);
-+ strvec_push(&args, "record");
++ strvec_push(&args, "perform");
+ strvec_pushv(&args, argv);
- return 0;
@@ builtin/rerere.c: int cmd_rerere(int argc,
+ CALLOC_ARRAY(args_copy, args.nr);
+ COPY_ARRAY(args_copy, args.v, args.nr);
+
-+ ret = cmd_rerere_record(args.nr, args_copy, prefix, repo);
++ ret = cmd_rerere_perform(args.nr, args_copy, prefix, repo);
+
+ strvec_clear(&args);
+ free(args_copy);
12: 102223da27b ! 12: 05adadc9dd8 builtin/rerere: introduce proper usage strings for subcommands
@@ builtin/rerere.c: static int cmd_rerere_gc(int argc,
if (argc)
usage(_("too many arguments"));
-@@ builtin/rerere.c: static int cmd_rerere_record(int argc,
+@@ builtin/rerere.c: static int cmd_rerere_perform(int argc,
N_("register clean resolutions in index"), 1),
OPT_END(),
};--- base-commit: 710910c03a22ac663885049213b9fef5853ea8e0 change-id: 20261027-pks-rerere-subcommands-318d41553438