rerere: let rerere_path() write paths into a caller-provided buffer

Same as with `get_worktree_git_dir()` a couple of commits ago, the
`rerere_path()` function returns paths that need not be free'd by the
caller because `git_path()` internally uses `get_pathname()`.

Refactor the function to instead accept a caller-provided buffer that
the path will be written into, passing on ownership to the caller. This
refactoring prepares us for the removal of `git_path()`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2025-02-07 12:03:36 +01:00 committed by Junio C Hamano
parent 07242c2a5a
commit 8ee018d863
3 changed files with 69 additions and 32 deletions

View File

@ -4,9 +4,9 @@
#include "config.h" #include "config.h"
#include "gettext.h" #include "gettext.h"
#include "parse-options.h" #include "parse-options.h"
#include "string-list.h"
#include "rerere.h" #include "rerere.h"
#include "strbuf.h"
#include "string-list.h"
#include "xdiff/xdiff.h" #include "xdiff/xdiff.h"
#include "xdiff-interface.h" #include "xdiff-interface.h"
#include "pathspec.h" #include "pathspec.h"
@ -112,15 +112,18 @@ int cmd_rerere(int argc,
merge_rr.items[i].util = NULL; merge_rr.items[i].util = NULL;
} }
} else if (!strcmp(argv[0], "diff")) { } else if (!strcmp(argv[0], "diff")) {
struct strbuf buf = STRBUF_INIT;
if (setup_rerere(the_repository, &merge_rr, if (setup_rerere(the_repository, &merge_rr,
flags | RERERE_READONLY) < 0) flags | RERERE_READONLY) < 0)
return 0; return 0;
for (size_t i = 0; i < merge_rr.nr; i++) { for (size_t i = 0; i < merge_rr.nr; i++) {
const char *path = merge_rr.items[i].string; const char *path = merge_rr.items[i].string;
const struct rerere_id *id = merge_rr.items[i].util; const struct rerere_id *id = merge_rr.items[i].util;
if (diff_two(rerere_path(id, "preimage"), path, path, path)) if (diff_two(rerere_path(&buf, id, "preimage"), path, path, path))
die(_("unable to generate diff for '%s'"), rerere_path(id, NULL)); die(_("unable to generate diff for '%s'"), rerere_path(&buf, id, NULL));
} }
strbuf_release(&buf);
} else } else
usage_with_options(rerere_usage, options); usage_with_options(rerere_usage, options);

View File

@ -91,16 +91,18 @@ static void assign_variant(struct rerere_id *id)
id->variant = variant; id->variant = variant;
} }
const char *rerere_path(const struct rerere_id *id, const char *file) const char *rerere_path(struct strbuf *buf, const struct rerere_id *id, const char *file)
{ {
if (!file) if (!file)
return git_path("rr-cache/%s", rerere_id_hex(id)); return repo_git_path_replace(the_repository, buf, "rr-cache/%s",
rerere_id_hex(id));
if (id->variant <= 0) if (id->variant <= 0)
return git_path("rr-cache/%s/%s", rerere_id_hex(id), file); return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s",
rerere_id_hex(id), file);
return git_path("rr-cache/%s/%s.%d", return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s.%d",
rerere_id_hex(id), file, id->variant); rerere_id_hex(id), file, id->variant);
} }
static int is_rr_file(const char *name, const char *filename, int *variant) static int is_rr_file(const char *name, const char *filename, int *variant)
@ -624,9 +626,10 @@ static int try_merge(struct index_state *istate,
{ {
enum ll_merge_result ret; enum ll_merge_result ret;
mmfile_t base = {NULL, 0}, other = {NULL, 0}; mmfile_t base = {NULL, 0}, other = {NULL, 0};
struct strbuf buf = STRBUF_INIT;
if (read_mmfile(&base, rerere_path(id, "preimage")) || if (read_mmfile(&base, rerere_path(&buf, id, "preimage")) ||
read_mmfile(&other, rerere_path(id, "postimage"))) { read_mmfile(&other, rerere_path(&buf, id, "postimage"))) {
ret = LL_MERGE_CONFLICT; ret = LL_MERGE_CONFLICT;
} else { } else {
/* /*
@ -637,6 +640,7 @@ static int try_merge(struct index_state *istate,
istate, NULL); istate, NULL);
} }
strbuf_release(&buf);
free(base.ptr); free(base.ptr);
free(other.ptr); free(other.ptr);
@ -657,6 +661,7 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
{ {
FILE *f; FILE *f;
int ret; int ret;
struct strbuf buf = STRBUF_INIT;
mmfile_t cur = {NULL, 0}; mmfile_t cur = {NULL, 0};
mmbuffer_t result = {NULL, 0}; mmbuffer_t result = {NULL, 0};
@ -664,8 +669,8 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
* Normalize the conflicts in path and write it out to * Normalize the conflicts in path and write it out to
* "thisimage" temporary file. * "thisimage" temporary file.
*/ */
if ((handle_file(istate, path, NULL, rerere_path(id, "thisimage")) < 0) || if ((handle_file(istate, path, NULL, rerere_path(&buf, id, "thisimage")) < 0) ||
read_mmfile(&cur, rerere_path(id, "thisimage"))) { read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) {
ret = 1; ret = 1;
goto out; goto out;
} }
@ -678,9 +683,9 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
* A successful replay of recorded resolution. * A successful replay of recorded resolution.
* Mark that "postimage" was used to help gc. * Mark that "postimage" was used to help gc.
*/ */
if (utime(rerere_path(id, "postimage"), NULL) < 0) if (utime(rerere_path(&buf, id, "postimage"), NULL) < 0)
warning_errno(_("failed utime() on '%s'"), warning_errno(_("failed utime() on '%s'"),
rerere_path(id, "postimage")); rerere_path(&buf, id, "postimage"));
/* Update "path" with the resolution */ /* Update "path" with the resolution */
f = fopen(path, "w"); f = fopen(path, "w");
@ -694,6 +699,7 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
out: out:
free(cur.ptr); free(cur.ptr);
free(result.ptr); free(result.ptr);
strbuf_release(&buf);
return ret; return ret;
} }
@ -720,9 +726,11 @@ static void update_paths(struct repository *r, struct string_list *update)
static void remove_variant(struct rerere_id *id) static void remove_variant(struct rerere_id *id)
{ {
unlink_or_warn(rerere_path(id, "postimage")); struct strbuf buf = STRBUF_INIT;
unlink_or_warn(rerere_path(id, "preimage")); unlink_or_warn(rerere_path(&buf, id, "postimage"));
unlink_or_warn(rerere_path(&buf, id, "preimage"));
id->collection->status[id->variant] = 0; id->collection->status[id->variant] = 0;
strbuf_release(&buf);
} }
/* /*
@ -739,6 +747,7 @@ static void do_rerere_one_path(struct index_state *istate,
const char *path = rr_item->string; const char *path = rr_item->string;
struct rerere_id *id = rr_item->util; struct rerere_id *id = rr_item->util;
struct rerere_dir *rr_dir = id->collection; struct rerere_dir *rr_dir = id->collection;
struct strbuf buf = STRBUF_INIT;
int variant; int variant;
variant = id->variant; variant = id->variant;
@ -746,12 +755,12 @@ static void do_rerere_one_path(struct index_state *istate,
/* Has the user resolved it already? */ /* Has the user resolved it already? */
if (variant >= 0) { if (variant >= 0) {
if (!handle_file(istate, path, NULL, NULL)) { if (!handle_file(istate, path, NULL, NULL)) {
copy_file(rerere_path(id, "postimage"), path, 0666); copy_file(rerere_path(&buf, id, "postimage"), path, 0666);
id->collection->status[variant] |= RR_HAS_POSTIMAGE; id->collection->status[variant] |= RR_HAS_POSTIMAGE;
fprintf_ln(stderr, _("Recorded resolution for '%s'."), path); fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
free_rerere_id(rr_item); free_rerere_id(rr_item);
rr_item->util = NULL; rr_item->util = NULL;
return; goto out;
} }
/* /*
* There may be other variants that can cleanly * There may be other variants that can cleanly
@ -787,22 +796,25 @@ static void do_rerere_one_path(struct index_state *istate,
path); path);
free_rerere_id(rr_item); free_rerere_id(rr_item);
rr_item->util = NULL; rr_item->util = NULL;
return; goto out;
} }
/* None of the existing one applies; we need a new variant */ /* None of the existing one applies; we need a new variant */
assign_variant(id); assign_variant(id);
variant = id->variant; variant = id->variant;
handle_file(istate, path, NULL, rerere_path(id, "preimage")); handle_file(istate, path, NULL, rerere_path(&buf, id, "preimage"));
if (id->collection->status[variant] & RR_HAS_POSTIMAGE) { if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
const char *path = rerere_path(id, "postimage"); const char *path = rerere_path(&buf, id, "postimage");
if (unlink(path)) if (unlink(path))
die_errno(_("cannot unlink stray '%s'"), path); die_errno(_("cannot unlink stray '%s'"), path);
id->collection->status[variant] &= ~RR_HAS_POSTIMAGE; id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
} }
id->collection->status[variant] |= RR_HAS_PREIMAGE; id->collection->status[variant] |= RR_HAS_PREIMAGE;
fprintf_ln(stderr, _("Recorded preimage for '%s'"), path); fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
out:
strbuf_release(&buf);
} }
static int do_plain_rerere(struct repository *r, static int do_plain_rerere(struct repository *r,
@ -810,6 +822,7 @@ static int do_plain_rerere(struct repository *r,
{ {
struct string_list conflict = STRING_LIST_INIT_DUP; struct string_list conflict = STRING_LIST_INIT_DUP;
struct string_list update = STRING_LIST_INIT_DUP; struct string_list update = STRING_LIST_INIT_DUP;
struct strbuf buf = STRBUF_INIT;
int i; int i;
find_conflict(r, &conflict); find_conflict(r, &conflict);
@ -843,7 +856,7 @@ static int do_plain_rerere(struct repository *r,
string_list_insert(rr, path)->util = id; string_list_insert(rr, path)->util = id;
/* Ensure that the directory exists. */ /* Ensure that the directory exists. */
mkdir_in_gitdir(rerere_path(id, NULL)); mkdir_in_gitdir(rerere_path(&buf, id, NULL));
} }
for (i = 0; i < rr->nr; i++) for (i = 0; i < rr->nr; i++)
@ -854,6 +867,7 @@ static int do_plain_rerere(struct repository *r,
string_list_clear(&conflict, 0); string_list_clear(&conflict, 0);
string_list_clear(&update, 0); string_list_clear(&update, 0);
strbuf_release(&buf);
return write_rr(rr, fd); return write_rr(rr, fd);
} }
@ -1033,6 +1047,7 @@ static int rerere_forget_one_path(struct index_state *istate,
struct rerere_id *id; struct rerere_id *id;
unsigned char hash[GIT_MAX_RAWSZ]; unsigned char hash[GIT_MAX_RAWSZ];
int ret; int ret;
struct strbuf buf = STRBUF_INIT;
struct string_list_item *item; struct string_list_item *item;
/* /*
@ -1056,8 +1071,8 @@ static int rerere_forget_one_path(struct index_state *istate,
if (!has_rerere_resolution(id)) if (!has_rerere_resolution(id))
continue; continue;
handle_cache(istate, path, hash, rerere_path(id, "thisimage")); handle_cache(istate, path, hash, rerere_path(&buf, id, "thisimage"));
if (read_mmfile(&cur, rerere_path(id, "thisimage"))) { if (read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) {
free(cur.ptr); free(cur.ptr);
error(_("failed to update conflicted state in '%s'"), path); error(_("failed to update conflicted state in '%s'"), path);
goto fail_exit; goto fail_exit;
@ -1074,7 +1089,7 @@ static int rerere_forget_one_path(struct index_state *istate,
goto fail_exit; goto fail_exit;
} }
filename = rerere_path(id, "postimage"); filename = rerere_path(&buf, id, "postimage");
if (unlink(filename)) { if (unlink(filename)) {
if (errno == ENOENT) if (errno == ENOENT)
error(_("no remembered resolution for '%s'"), path); error(_("no remembered resolution for '%s'"), path);
@ -1088,7 +1103,7 @@ static int rerere_forget_one_path(struct index_state *istate,
* conflict in the working tree, run us again to record * conflict in the working tree, run us again to record
* the postimage. * the postimage.
*/ */
handle_cache(istate, path, hash, rerere_path(id, "preimage")); handle_cache(istate, path, hash, rerere_path(&buf, id, "preimage"));
fprintf_ln(stderr, _("Updated preimage for '%s'"), path); fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
/* /*
@ -1099,9 +1114,11 @@ static int rerere_forget_one_path(struct index_state *istate,
free_rerere_id(item); free_rerere_id(item);
item->util = id; item->util = id;
fprintf(stderr, _("Forgot resolution for '%s'\n"), path); fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
strbuf_release(&buf);
return 0; return 0;
fail_exit: fail_exit:
strbuf_release(&buf);
free(id); free(id);
return -1; return -1;
} }
@ -1147,16 +1164,26 @@ int rerere_forget(struct repository *r, struct pathspec *pathspec)
static timestamp_t rerere_created_at(struct rerere_id *id) static timestamp_t rerere_created_at(struct rerere_id *id)
{ {
struct strbuf buf = STRBUF_INIT;
struct stat st; struct stat st;
timestamp_t ret;
return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime; ret = stat(rerere_path(&buf, id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
strbuf_release(&buf);
return ret;
} }
static timestamp_t rerere_last_used_at(struct rerere_id *id) static timestamp_t rerere_last_used_at(struct rerere_id *id)
{ {
struct strbuf buf = STRBUF_INIT;
struct stat st; struct stat st;
timestamp_t ret;
return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime; ret = stat(rerere_path(&buf, id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
strbuf_release(&buf);
return ret;
} }
/* /*
@ -1164,9 +1191,11 @@ static timestamp_t rerere_last_used_at(struct rerere_id *id)
*/ */
static void unlink_rr_item(struct rerere_id *id) static void unlink_rr_item(struct rerere_id *id)
{ {
unlink_or_warn(rerere_path(id, "thisimage")); struct strbuf buf = STRBUF_INIT;
unlink_or_warn(rerere_path(&buf, id, "thisimage"));
remove_variant(id); remove_variant(id);
id->collection->status[id->variant] = 0; id->collection->status[id->variant] = 0;
strbuf_release(&buf);
} }
static void prune_one(struct rerere_id *id, static void prune_one(struct rerere_id *id,
@ -1264,10 +1293,14 @@ void rerere_clear(struct repository *r, struct string_list *merge_rr)
for (i = 0; i < merge_rr->nr; i++) { for (i = 0; i < merge_rr->nr; i++) {
struct rerere_id *id = merge_rr->items[i].util; struct rerere_id *id = merge_rr->items[i].util;
struct strbuf buf = STRBUF_INIT;
if (!has_rerere_resolution(id)) { if (!has_rerere_resolution(id)) {
unlink_rr_item(id); unlink_rr_item(id);
rmdir(rerere_path(id, NULL)); rmdir(rerere_path(&buf, id, NULL));
} }
strbuf_release(&buf);
} }
unlink_or_warn(git_path_merge_rr(r)); unlink_or_warn(git_path_merge_rr(r));
rollback_lock_file(&write_lock); rollback_lock_file(&write_lock);

View File

@ -32,7 +32,8 @@ int repo_rerere(struct repository *, int);
* path to that filesystem entity. With "file" specified with NULL, * path to that filesystem entity. With "file" specified with NULL,
* return the path to the directory that houses these files. * return the path to the directory that houses these files.
*/ */
const char *rerere_path(const struct rerere_id *, const char *file); const char *rerere_path(struct strbuf *buf, const struct rerere_id *,
const char *file);
int rerere_forget(struct repository *, struct pathspec *); int rerere_forget(struct repository *, struct pathspec *);
int rerere_remaining(struct repository *, struct string_list *); int rerere_remaining(struct repository *, struct string_list *);
void rerere_clear(struct repository *, struct string_list *); void rerere_clear(struct repository *, struct string_list *);