Peter Hagervall | baffc0e | 2007-07-15 01:14:45 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 2 | #include "cache.h" |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 3 | #include "dir.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 4 | #include "string-list.h" |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 5 | #include "rerere.h" |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 6 | #include "xdiff/xdiff.h" |
| 7 | #include "xdiff-interface.h" |
| 8 | |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 9 | static const char git_rerere_usage[] = |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 10 | "git rerere [clear | status | diff | gc]"; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 11 | |
| 12 | /* these values are days */ |
| 13 | static int cutoff_noresolve = 15; |
| 14 | static int cutoff_resolve = 60; |
| 15 | |
Junio C Hamano | 9022a49 | 2008-06-21 23:28:58 -0700 | [diff] [blame] | 16 | static time_t rerere_created_at(const char *name) |
| 17 | { |
| 18 | struct stat st; |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 19 | return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime; |
Junio C Hamano | 9022a49 | 2008-06-21 23:28:58 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 22 | static void unlink_rr_item(const char *name) |
| 23 | { |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 24 | unlink(rerere_path(name, "thisimage")); |
| 25 | unlink(rerere_path(name, "preimage")); |
| 26 | unlink(rerere_path(name, "postimage")); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 27 | rmdir(git_path("rr-cache/%s", name)); |
| 28 | } |
| 29 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 30 | static 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 Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 41 | static void garbage_collect(struct string_list *rr) |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 42 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 43 | struct string_list to_remove = { NULL, 0, 0, 1 }; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 44 | DIR *dir; |
| 45 | struct dirent *e; |
Junio C Hamano | 9022a49 | 2008-06-21 23:28:58 -0700 | [diff] [blame] | 46 | int i, cutoff; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 47 | time_t now = time(NULL), then; |
| 48 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 49 | git_config(git_rerere_gc_config, NULL); |
Junio C Hamano | 9022a49 | 2008-06-21 23:28:58 -0700 | [diff] [blame] | 50 | dir = opendir(git_path("rr-cache")); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 51 | while ((e = readdir(dir))) { |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 52 | if (is_dot_or_dotdot(e->d_name)) |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 53 | continue; |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 54 | then = rerere_created_at(e->d_name); |
Junio C Hamano | 9022a49 | 2008-06-21 23:28:58 -0700 | [diff] [blame] | 55 | if (!then) |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 56 | continue; |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 57 | cutoff = (has_rerere_resolution(e->d_name) |
Junio C Hamano | 9022a49 | 2008-06-21 23:28:58 -0700 | [diff] [blame] | 58 | ? cutoff_resolve : cutoff_noresolve); |
| 59 | if (then < now - cutoff * 86400) |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 60 | string_list_append(e->d_name, &to_remove); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 61 | } |
| 62 | for (i = 0; i < to_remove.nr; i++) |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 63 | unlink_rr_item(to_remove.items[i].string); |
| 64 | string_list_clear(&to_remove, 0); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static int outf(void *dummy, mmbuffer_t *ptr, int nbuf) |
| 68 | { |
| 69 | int i; |
| 70 | for (i = 0; i < nbuf; i++) |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 71 | if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size) |
| 72 | return -1; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static 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 Downing | 9ccd0a8 | 2008-10-25 15:30:37 +0200 | [diff] [blame] | 89 | memset(&xpp, 0, sizeof(xpp)); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 90 | xpp.flags = XDF_NEED_MINIMAL; |
Johannes Schindelin | 30b2501 | 2007-07-04 19:05:46 +0100 | [diff] [blame] | 91 | memset(&xecfg, 0, sizeof(xecfg)); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 92 | xecfg.ctxlen = 3; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 93 | ecb.outf = outf; |
Junio C Hamano | c279d7e | 2007-12-13 13:25:07 -0800 | [diff] [blame] | 94 | xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 95 | |
| 96 | free(minus.ptr); |
| 97 | free(plus.ptr); |
| 98 | return 0; |
| 99 | } |
| 100 | |
Kristian Høgsberg | d8b7db0 | 2007-09-17 20:06:47 -0400 | [diff] [blame] | 101 | int cmd_rerere(int argc, const char **argv, const char *prefix) |
| 102 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 103 | struct string_list merge_rr = { NULL, 0, 0, 1 }; |
Kristian Høgsberg | d8b7db0 | 2007-09-17 20:06:47 -0400 | [diff] [blame] | 104 | int i, fd; |
| 105 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 106 | if (argc < 2) |
| 107 | return rerere(); |
| 108 | |
Kristian Høgsberg | d8b7db0 | 2007-09-17 20:06:47 -0400 | [diff] [blame] | 109 | fd = setup_rerere(&merge_rr); |
| 110 | if (fd < 0) |
| 111 | return 0; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 112 | |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 113 | if (!strcmp(argv[1], "clear")) { |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 114 | for (i = 0; i < merge_rr.nr; i++) { |
| 115 | const char *name = (const char *)merge_rr.items[i].util; |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 116 | if (!has_rerere_resolution(name)) |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 117 | unlink_rr_item(name); |
| 118 | } |
Stephan Beyer | 5b2fd95 | 2008-07-09 14:58:57 +0200 | [diff] [blame] | 119 | unlink(git_path("rr-cache/MERGE_RR")); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 120 | } 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 Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 124 | printf("%s\n", merge_rr.items[i].string); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 125 | else if (!strcmp(argv[1], "diff")) |
| 126 | for (i = 0; i < merge_rr.nr; i++) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 127 | const char *path = merge_rr.items[i].string; |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 128 | const char *name = (const char *)merge_rr.items[i].util; |
SZEDER Gábor | 9005696 | 2009-02-14 23:21:04 +0100 | [diff] [blame] | 129 | diff_two(rerere_path(name, "preimage"), path, path, path); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | usage(git_rerere_usage); |
| 133 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 134 | string_list_clear(&merge_rr, 1); |
Johannes Schindelin | 658f365 | 2006-12-20 17:39:41 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |