@matheusbsbs / matheus-bagliano / commits / 9022a495a3
Fork 0

rerere: rerere_created_at() and has_resolution() abstraction

There were too many places in the code how an entry in the rerere database looks like, and the garbage_collect() function that iterates over subdirectories of the rr-cache directory was the worse offender. Introduce two helper functions, rerere_created_at() and has_resolution(), to abstract out the logic a bit better. Incidentally this fixes a small memory leak in garbage_collect() function. The path list to collect the entries to be pruned were defined to strdup the paths but the caller was feeding a path after doing an extra copy. Because the list does not have to be sorted by conflict signature hash, we use path_list_append() instead of path_list_insert(). While we are at it, make a conflicted hunk comparision in handle_file() a bit easier to read. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 21, 2008 at 23:28 UTC 9022a495a3c407859560d4ac4a278c0d6ad58837
1 file changed +26 -31
builtin-rerere.c
+26 -31
@@ -23,6 +23,18 @@ static const char *rr_path(const char *name, const char *file)
23 return git_path("rr-cache/%s/%s", name, file);
24 }
25
26 +static time_t rerere_created_at(const char *name)
27 +{
28 + struct stat st;
29 + return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
30 +}
31 +
32 +static int has_resolution(const char *name)
33 +{
34 + struct stat st;
35 + return !stat(rr_path(name, "postimage"), &st);
36 +}
37 +
38 static void read_rr(struct path_list *rr)
39 {
40 unsigned char sha1[20];
@@ -98,13 +110,10 @@ static int handle_file(const char *path,
110 else if (!prefixcmp(buf, "======="))
111 hunk = 2;
112 else if (!prefixcmp(buf, ">>>>>>> ")) {
101 - int cmp = strbuf_cmp(&one, &two);
102 -
113 + if (strbuf_cmp(&one, &two) > 0)
114 + strbuf_swap(&one, &two);
115 hunk_no++;
116 hunk = 0;
105 - if (cmp > 0) {
106 - strbuf_swap(&one, &two);
107 - }
117 if (out) {
118 fputs("<<<<<<<\n", out);
119 fwrite(one.buf, one.len, 1, out);
@@ -201,33 +210,24 @@ static void unlink_rr_item(const char *name)
210 static void garbage_collect(struct path_list *rr)
211 {
212 struct path_list to_remove = { NULL, 0, 0, 1 };
204 - char buf[1024];
213 DIR *dir;
214 struct dirent *e;
207 - int len, i, cutoff;
215 + int i, cutoff;
216 time_t now = time(NULL), then;
217
210 - strlcpy(buf, git_path("rr-cache"), sizeof(buf));
211 - len = strlen(buf);
212 - dir = opendir(buf);
213 - strcpy(buf + len++, "/");
218 + dir = opendir(git_path("rr-cache"));
219 while ((e = readdir(dir))) {
220 const char *name = e->d_name;
216 - struct stat st;
217 - if (name[0] == '.' && (name[1] == '\0' ||
218 - (name[1] == '.' && name[2] == '\0')))
221 + if (name[0] == '.' &&
222 + (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
223 continue;
220 - i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
221 - strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
222 - if (stat(buf, &st))
224 + then = rerere_created_at(name);
225 + if (!then)
226 continue;
224 - then = st.st_mtime;
225 - strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
226 - cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
227 - if (then < now - cutoff * 86400) {
228 - buf[len + i] = '\0';
229 - path_list_insert(xstrdup(name), &to_remove);
230 - }
227 + cutoff = (has_resolution(name)
228 + ? cutoff_resolve : cutoff_noresolve);
229 + if (then < now - cutoff * 86400)
230 + path_list_append(name, &to_remove);
231 }
232 for (i = 0; i < to_remove.nr; i++)
233 unlink_rr_item(to_remove.items[i].path);
@@ -306,13 +306,11 @@ static int do_plain_rerere(struct path_list *rr, int fd)
306 */
307
308 for (i = 0; i < rr->nr; i++) {
309 - struct stat st;
309 int ret;
310 const char *path = rr->items[i].path;
311 const char *name = (const char *)rr->items[i].util;
312
314 - if (!stat(rr_path(name, "preimage"), &st) &&
315 - !stat(rr_path(name, "postimage"), &st)) {
313 + if (has_resolution(name)) {
314 if (!merge(name, path)) {
315 fprintf(stderr, "Resolved '%s' using "
316 "previous resolution.\n", path);
@@ -410,11 +408,8 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
408 return do_plain_rerere(&merge_rr, fd);
409 else if (!strcmp(argv[1], "clear")) {
410 for (i = 0; i < merge_rr.nr; i++) {
413 - struct stat st;
411 const char *name = (const char *)merge_rr.items[i].util;
415 - if (!stat(git_path("rr-cache/%s", name), &st) &&
416 - S_ISDIR(st.st_mode) &&
417 - stat(rr_path(name, "postimage"), &st))
412 + if (!has_resolution(name))
413 unlink_rr_item(name);
414 }
415 unlink(merge_rr_path);