blob: 020af7377bb9aa1bced7489d40169e3cb2ff7682 [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Johannes Schindelin658f3652006-12-20 17:39:41 +01002#include "cache.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +03003#include "dir.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01004#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +02005#include "rerere.h"
Johannes Schindelin658f3652006-12-20 17:39:41 +01006#include "xdiff/xdiff.h"
7#include "xdiff-interface.h"
8
Johannes Schindelin658f3652006-12-20 17:39:41 +01009static const char git_rerere_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020010"git rerere [clear | status | diff | gc]";
Johannes Schindelin658f3652006-12-20 17:39:41 +010011
12/* these values are days */
13static int cutoff_noresolve = 15;
14static int cutoff_resolve = 60;
15
Junio C Hamano9022a492008-06-21 23:28:58 -070016static time_t rerere_created_at(const char *name)
17{
18 struct stat st;
SZEDER Gábor90056962009-02-14 23:21:04 +010019 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano9022a492008-06-21 23:28:58 -070020}
21
Johannes Schindelin658f3652006-12-20 17:39:41 +010022static void unlink_rr_item(const char *name)
23{
SZEDER Gábor90056962009-02-14 23:21:04 +010024 unlink(rerere_path(name, "thisimage"));
25 unlink(rerere_path(name, "preimage"));
26 unlink(rerere_path(name, "postimage"));
Johannes Schindelin658f3652006-12-20 17:39:41 +010027 rmdir(git_path("rr-cache/%s", name));
28}
29
Stephan Beyer5b2fd952008-07-09 14:58:57 +020030static int git_rerere_gc_config(const char *var, const char *value, void *cb)
31{
32 if (!strcmp(var, "gc.rerereresolved"))
33 cutoff_resolve = git_config_int(var, value);
34 else if (!strcmp(var, "gc.rerereunresolved"))
35 cutoff_noresolve = git_config_int(var, value);
36 else
37 return git_default_config(var, value, cb);
38 return 0;
39}
40
Johannes Schindelinc455c872008-07-21 19:03:49 +010041static void garbage_collect(struct string_list *rr)
Johannes Schindelin658f3652006-12-20 17:39:41 +010042{
Johannes Schindelinc455c872008-07-21 19:03:49 +010043 struct string_list to_remove = { NULL, 0, 0, 1 };
Johannes Schindelin658f3652006-12-20 17:39:41 +010044 DIR *dir;
45 struct dirent *e;
Junio C Hamano9022a492008-06-21 23:28:58 -070046 int i, cutoff;
Johannes Schindelin658f3652006-12-20 17:39:41 +010047 time_t now = time(NULL), then;
48
Stephan Beyer5b2fd952008-07-09 14:58:57 +020049 git_config(git_rerere_gc_config, NULL);
Junio C Hamano9022a492008-06-21 23:28:58 -070050 dir = opendir(git_path("rr-cache"));
Johannes Schindelin658f3652006-12-20 17:39:41 +010051 while ((e = readdir(dir))) {
Alexander Potashev8ca12c02009-01-10 15:07:50 +030052 if (is_dot_or_dotdot(e->d_name))
Johannes Schindelin658f3652006-12-20 17:39:41 +010053 continue;
Alexander Potashev8ca12c02009-01-10 15:07:50 +030054 then = rerere_created_at(e->d_name);
Junio C Hamano9022a492008-06-21 23:28:58 -070055 if (!then)
Johannes Schindelin658f3652006-12-20 17:39:41 +010056 continue;
SZEDER Gábor90056962009-02-14 23:21:04 +010057 cutoff = (has_rerere_resolution(e->d_name)
Junio C Hamano9022a492008-06-21 23:28:58 -070058 ? cutoff_resolve : cutoff_noresolve);
59 if (then < now - cutoff * 86400)
Alexander Potashev8ca12c02009-01-10 15:07:50 +030060 string_list_append(e->d_name, &to_remove);
Johannes Schindelin658f3652006-12-20 17:39:41 +010061 }
62 for (i = 0; i < to_remove.nr; i++)
Johannes Schindelinc455c872008-07-21 19:03:49 +010063 unlink_rr_item(to_remove.items[i].string);
64 string_list_clear(&to_remove, 0);
Johannes Schindelin658f3652006-12-20 17:39:41 +010065}
66
67static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
68{
69 int i;
70 for (i = 0; i < nbuf; i++)
Andy Whitcroft93822c22007-01-08 15:58:23 +000071 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
72 return -1;
Johannes Schindelin658f3652006-12-20 17:39:41 +010073 return 0;
74}
75
76static int diff_two(const char *file1, const char *label1,
77 const char *file2, const char *label2)
78{
79 xpparam_t xpp;
80 xdemitconf_t xecfg;
81 xdemitcb_t ecb;
82 mmfile_t minus, plus;
83
84 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
85 return 1;
86
87 printf("--- a/%s\n+++ b/%s\n", label1, label2);
88 fflush(stdout);
Brian Downing9ccd0a82008-10-25 15:30:37 +020089 memset(&xpp, 0, sizeof(xpp));
Johannes Schindelin658f3652006-12-20 17:39:41 +010090 xpp.flags = XDF_NEED_MINIMAL;
Johannes Schindelin30b25012007-07-04 19:05:46 +010091 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin658f3652006-12-20 17:39:41 +010092 xecfg.ctxlen = 3;
Johannes Schindelin658f3652006-12-20 17:39:41 +010093 ecb.outf = outf;
Junio C Hamanoc279d7e2007-12-13 13:25:07 -080094 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
Johannes Schindelin658f3652006-12-20 17:39:41 +010095
96 free(minus.ptr);
97 free(plus.ptr);
98 return 0;
99}
100
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -0400101int cmd_rerere(int argc, const char **argv, const char *prefix)
102{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100103 struct string_list merge_rr = { NULL, 0, 0, 1 };
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -0400104 int i, fd;
105
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200106 if (argc < 2)
107 return rerere();
108
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -0400109 fd = setup_rerere(&merge_rr);
110 if (fd < 0)
111 return 0;
Johannes Schindelin658f3652006-12-20 17:39:41 +0100112
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200113 if (!strcmp(argv[1], "clear")) {
Johannes Schindelin658f3652006-12-20 17:39:41 +0100114 for (i = 0; i < merge_rr.nr; i++) {
115 const char *name = (const char *)merge_rr.items[i].util;
SZEDER Gábor90056962009-02-14 23:21:04 +0100116 if (!has_rerere_resolution(name))
Johannes Schindelin658f3652006-12-20 17:39:41 +0100117 unlink_rr_item(name);
118 }
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200119 unlink(git_path("rr-cache/MERGE_RR"));
Johannes Schindelin658f3652006-12-20 17:39:41 +0100120 } else if (!strcmp(argv[1], "gc"))
121 garbage_collect(&merge_rr);
122 else if (!strcmp(argv[1], "status"))
123 for (i = 0; i < merge_rr.nr; i++)
Johannes Schindelinc455c872008-07-21 19:03:49 +0100124 printf("%s\n", merge_rr.items[i].string);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100125 else if (!strcmp(argv[1], "diff"))
126 for (i = 0; i < merge_rr.nr; i++) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100127 const char *path = merge_rr.items[i].string;
Johannes Schindelin658f3652006-12-20 17:39:41 +0100128 const char *name = (const char *)merge_rr.items[i].util;
SZEDER Gábor90056962009-02-14 23:21:04 +0100129 diff_two(rerere_path(name, "preimage"), path, path, path);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100130 }
131 else
132 usage(git_rerere_usage);
133
Johannes Schindelinc455c872008-07-21 19:03:49 +0100134 string_list_clear(&merge_rr, 1);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100135 return 0;
136}