Skip to content

Commit 8ee018d

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07242c2 commit 8ee018d

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

builtin/rerere.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include "config.h"
55
#include "gettext.h"
66
#include "parse-options.h"
7-
8-
#include "string-list.h"
97
#include "rerere.h"
8+
#include "strbuf.h"
9+
#include "string-list.h"
1010
#include "xdiff/xdiff.h"
1111
#include "xdiff-interface.h"
1212
#include "pathspec.h"
@@ -112,15 +112,18 @@ int cmd_rerere(int argc,
112112
merge_rr.items[i].util = NULL;
113113
}
114114
} else if (!strcmp(argv[0], "diff")) {
115+
struct strbuf buf = STRBUF_INIT;
115116
if (setup_rerere(the_repository, &merge_rr,
116117
flags | RERERE_READONLY) < 0)
117118
return 0;
118119
for (size_t i = 0; i < merge_rr.nr; i++) {
119120
const char *path = merge_rr.items[i].string;
120121
const struct rerere_id *id = merge_rr.items[i].util;
121-
if (diff_two(rerere_path(id, "preimage"), path, path, path))
122-
die(_("unable to generate diff for '%s'"), rerere_path(id, NULL));
122+
if (diff_two(rerere_path(&buf, id, "preimage"), path, path, path))
123+
die(_("unable to generate diff for '%s'"), rerere_path(&buf, id, NULL));
123124
}
125+
126+
strbuf_release(&buf);
124127
} else
125128
usage_with_options(rerere_usage, options);
126129

rerere.c

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ static void assign_variant(struct rerere_id *id)
9191
id->variant = variant;
9292
}
9393

94-
const char *rerere_path(const struct rerere_id *id, const char *file)
94+
const char *rerere_path(struct strbuf *buf, const struct rerere_id *id, const char *file)
9595
{
9696
if (!file)
97-
return git_path("rr-cache/%s", rerere_id_hex(id));
97+
return repo_git_path_replace(the_repository, buf, "rr-cache/%s",
98+
rerere_id_hex(id));
9899

99100
if (id->variant <= 0)
100-
return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
101+
return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s",
102+
rerere_id_hex(id), file);
101103

102-
return git_path("rr-cache/%s/%s.%d",
103-
rerere_id_hex(id), file, id->variant);
104+
return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s.%d",
105+
rerere_id_hex(id), file, id->variant);
104106
}
105107

106108
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,
624626
{
625627
enum ll_merge_result ret;
626628
mmfile_t base = {NULL, 0}, other = {NULL, 0};
629+
struct strbuf buf = STRBUF_INIT;
627630

628-
if (read_mmfile(&base, rerere_path(id, "preimage")) ||
629-
read_mmfile(&other, rerere_path(id, "postimage"))) {
631+
if (read_mmfile(&base, rerere_path(&buf, id, "preimage")) ||
632+
read_mmfile(&other, rerere_path(&buf, id, "postimage"))) {
630633
ret = LL_MERGE_CONFLICT;
631634
} else {
632635
/*
@@ -637,6 +640,7 @@ static int try_merge(struct index_state *istate,
637640
istate, NULL);
638641
}
639642

643+
strbuf_release(&buf);
640644
free(base.ptr);
641645
free(other.ptr);
642646

@@ -657,15 +661,16 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
657661
{
658662
FILE *f;
659663
int ret;
664+
struct strbuf buf = STRBUF_INIT;
660665
mmfile_t cur = {NULL, 0};
661666
mmbuffer_t result = {NULL, 0};
662667

663668
/*
664669
* Normalize the conflicts in path and write it out to
665670
* "thisimage" temporary file.
666671
*/
667-
if ((handle_file(istate, path, NULL, rerere_path(id, "thisimage")) < 0) ||
668-
read_mmfile(&cur, rerere_path(id, "thisimage"))) {
672+
if ((handle_file(istate, path, NULL, rerere_path(&buf, id, "thisimage")) < 0) ||
673+
read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) {
669674
ret = 1;
670675
goto out;
671676
}
@@ -678,9 +683,9 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
678683
* A successful replay of recorded resolution.
679684
* Mark that "postimage" was used to help gc.
680685
*/
681-
if (utime(rerere_path(id, "postimage"), NULL) < 0)
686+
if (utime(rerere_path(&buf, id, "postimage"), NULL) < 0)
682687
warning_errno(_("failed utime() on '%s'"),
683-
rerere_path(id, "postimage"));
688+
rerere_path(&buf, id, "postimage"));
684689

685690
/* Update "path" with the resolution */
686691
f = fopen(path, "w");
@@ -694,6 +699,7 @@ static int merge(struct index_state *istate, const struct rerere_id *id, const c
694699
out:
695700
free(cur.ptr);
696701
free(result.ptr);
702+
strbuf_release(&buf);
697703

698704
return ret;
699705
}
@@ -720,9 +726,11 @@ static void update_paths(struct repository *r, struct string_list *update)
720726

721727
static void remove_variant(struct rerere_id *id)
722728
{
723-
unlink_or_warn(rerere_path(id, "postimage"));
724-
unlink_or_warn(rerere_path(id, "preimage"));
729+
struct strbuf buf = STRBUF_INIT;
730+
unlink_or_warn(rerere_path(&buf, id, "postimage"));
731+
unlink_or_warn(rerere_path(&buf, id, "preimage"));
725732
id->collection->status[id->variant] = 0;
733+
strbuf_release(&buf);
726734
}
727735

728736
/*
@@ -739,19 +747,20 @@ static void do_rerere_one_path(struct index_state *istate,
739747
const char *path = rr_item->string;
740748
struct rerere_id *id = rr_item->util;
741749
struct rerere_dir *rr_dir = id->collection;
750+
struct strbuf buf = STRBUF_INIT;
742751
int variant;
743752

744753
variant = id->variant;
745754

746755
/* Has the user resolved it already? */
747756
if (variant >= 0) {
748757
if (!handle_file(istate, path, NULL, NULL)) {
749-
copy_file(rerere_path(id, "postimage"), path, 0666);
758+
copy_file(rerere_path(&buf, id, "postimage"), path, 0666);
750759
id->collection->status[variant] |= RR_HAS_POSTIMAGE;
751760
fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
752761
free_rerere_id(rr_item);
753762
rr_item->util = NULL;
754-
return;
763+
goto out;
755764
}
756765
/*
757766
* There may be other variants that can cleanly
@@ -787,29 +796,33 @@ static void do_rerere_one_path(struct index_state *istate,
787796
path);
788797
free_rerere_id(rr_item);
789798
rr_item->util = NULL;
790-
return;
799+
goto out;
791800
}
792801

793802
/* None of the existing one applies; we need a new variant */
794803
assign_variant(id);
795804

796805
variant = id->variant;
797-
handle_file(istate, path, NULL, rerere_path(id, "preimage"));
806+
handle_file(istate, path, NULL, rerere_path(&buf, id, "preimage"));
798807
if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
799-
const char *path = rerere_path(id, "postimage");
808+
const char *path = rerere_path(&buf, id, "postimage");
800809
if (unlink(path))
801810
die_errno(_("cannot unlink stray '%s'"), path);
802811
id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
803812
}
804813
id->collection->status[variant] |= RR_HAS_PREIMAGE;
805814
fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
815+
816+
out:
817+
strbuf_release(&buf);
806818
}
807819

808820
static int do_plain_rerere(struct repository *r,
809821
struct string_list *rr, int fd)
810822
{
811823
struct string_list conflict = STRING_LIST_INIT_DUP;
812824
struct string_list update = STRING_LIST_INIT_DUP;
825+
struct strbuf buf = STRBUF_INIT;
813826
int i;
814827

815828
find_conflict(r, &conflict);
@@ -843,7 +856,7 @@ static int do_plain_rerere(struct repository *r,
843856
string_list_insert(rr, path)->util = id;
844857

845858
/* Ensure that the directory exists. */
846-
mkdir_in_gitdir(rerere_path(id, NULL));
859+
mkdir_in_gitdir(rerere_path(&buf, id, NULL));
847860
}
848861

849862
for (i = 0; i < rr->nr; i++)
@@ -854,6 +867,7 @@ static int do_plain_rerere(struct repository *r,
854867

855868
string_list_clear(&conflict, 0);
856869
string_list_clear(&update, 0);
870+
strbuf_release(&buf);
857871
return write_rr(rr, fd);
858872
}
859873

@@ -1033,6 +1047,7 @@ static int rerere_forget_one_path(struct index_state *istate,
10331047
struct rerere_id *id;
10341048
unsigned char hash[GIT_MAX_RAWSZ];
10351049
int ret;
1050+
struct strbuf buf = STRBUF_INIT;
10361051
struct string_list_item *item;
10371052

10381053
/*
@@ -1056,8 +1071,8 @@ static int rerere_forget_one_path(struct index_state *istate,
10561071
if (!has_rerere_resolution(id))
10571072
continue;
10581073

1059-
handle_cache(istate, path, hash, rerere_path(id, "thisimage"));
1060-
if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1074+
handle_cache(istate, path, hash, rerere_path(&buf, id, "thisimage"));
1075+
if (read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) {
10611076
free(cur.ptr);
10621077
error(_("failed to update conflicted state in '%s'"), path);
10631078
goto fail_exit;
@@ -1074,7 +1089,7 @@ static int rerere_forget_one_path(struct index_state *istate,
10741089
goto fail_exit;
10751090
}
10761091

1077-
filename = rerere_path(id, "postimage");
1092+
filename = rerere_path(&buf, id, "postimage");
10781093
if (unlink(filename)) {
10791094
if (errno == ENOENT)
10801095
error(_("no remembered resolution for '%s'"), path);
@@ -1088,7 +1103,7 @@ static int rerere_forget_one_path(struct index_state *istate,
10881103
* conflict in the working tree, run us again to record
10891104
* the postimage.
10901105
*/
1091-
handle_cache(istate, path, hash, rerere_path(id, "preimage"));
1106+
handle_cache(istate, path, hash, rerere_path(&buf, id, "preimage"));
10921107
fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
10931108

10941109
/*
@@ -1099,9 +1114,11 @@ static int rerere_forget_one_path(struct index_state *istate,
10991114
free_rerere_id(item);
11001115
item->util = id;
11011116
fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
1117+
strbuf_release(&buf);
11021118
return 0;
11031119

11041120
fail_exit:
1121+
strbuf_release(&buf);
11051122
free(id);
11061123
return -1;
11071124
}
@@ -1147,26 +1164,38 @@ int rerere_forget(struct repository *r, struct pathspec *pathspec)
11471164

11481165
static timestamp_t rerere_created_at(struct rerere_id *id)
11491166
{
1167+
struct strbuf buf = STRBUF_INIT;
11501168
struct stat st;
1169+
timestamp_t ret;
1170+
1171+
ret = stat(rerere_path(&buf, id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
11511172

1152-
return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
1173+
strbuf_release(&buf);
1174+
return ret;
11531175
}
11541176

11551177
static timestamp_t rerere_last_used_at(struct rerere_id *id)
11561178
{
1179+
struct strbuf buf = STRBUF_INIT;
11571180
struct stat st;
1181+
timestamp_t ret;
1182+
1183+
ret = stat(rerere_path(&buf, id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
11581184

1159-
return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
1185+
strbuf_release(&buf);
1186+
return ret;
11601187
}
11611188

11621189
/*
11631190
* Remove the recorded resolution for a given conflict ID
11641191
*/
11651192
static void unlink_rr_item(struct rerere_id *id)
11661193
{
1167-
unlink_or_warn(rerere_path(id, "thisimage"));
1194+
struct strbuf buf = STRBUF_INIT;
1195+
unlink_or_warn(rerere_path(&buf, id, "thisimage"));
11681196
remove_variant(id);
11691197
id->collection->status[id->variant] = 0;
1198+
strbuf_release(&buf);
11701199
}
11711200

11721201
static void prune_one(struct rerere_id *id,
@@ -1264,10 +1293,14 @@ void rerere_clear(struct repository *r, struct string_list *merge_rr)
12641293

12651294
for (i = 0; i < merge_rr->nr; i++) {
12661295
struct rerere_id *id = merge_rr->items[i].util;
1296+
struct strbuf buf = STRBUF_INIT;
1297+
12671298
if (!has_rerere_resolution(id)) {
12681299
unlink_rr_item(id);
1269-
rmdir(rerere_path(id, NULL));
1300+
rmdir(rerere_path(&buf, id, NULL));
12701301
}
1302+
1303+
strbuf_release(&buf);
12711304
}
12721305
unlink_or_warn(git_path_merge_rr(r));
12731306
rollback_lock_file(&write_lock);

rerere.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ int repo_rerere(struct repository *, int);
3232
* path to that filesystem entity. With "file" specified with NULL,
3333
* return the path to the directory that houses these files.
3434
*/
35-
const char *rerere_path(const struct rerere_id *, const char *file);
35+
const char *rerere_path(struct strbuf *buf, const struct rerere_id *,
36+
const char *file);
3637
int rerere_forget(struct repository *, struct pathspec *);
3738
int rerere_remaining(struct repository *, struct string_list *);
3839
void rerere_clear(struct repository *, struct string_list *);

0 commit comments

Comments
 (0)