Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 2 | #include "dir.h" |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 3 | #include "resolve-undo.h" |
| 4 | #include "string-list.h" |
| 5 | |
| 6 | /* The only error case is to run out of memory in string-list */ |
| 7 | void record_resolve_undo(struct index_state *istate, struct cache_entry *ce) |
| 8 | { |
| 9 | struct string_list_item *lost; |
| 10 | struct resolve_undo_info *ui; |
| 11 | struct string_list *resolve_undo; |
| 12 | int stage = ce_stage(ce); |
| 13 | |
| 14 | if (!stage) |
| 15 | return; |
| 16 | |
| 17 | if (!istate->resolve_undo) { |
| 18 | resolve_undo = xcalloc(1, sizeof(*resolve_undo)); |
| 19 | resolve_undo->strdup_strings = 1; |
| 20 | istate->resolve_undo = resolve_undo; |
| 21 | } |
| 22 | resolve_undo = istate->resolve_undo; |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 23 | lost = string_list_insert(resolve_undo, ce->name); |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 24 | if (!lost->util) |
| 25 | lost->util = xcalloc(1, sizeof(*ui)); |
| 26 | ui = lost->util; |
| 27 | hashcpy(ui->sha1[stage - 1], ce->sha1); |
| 28 | ui->mode[stage - 1] = ce->ce_mode; |
| 29 | } |
| 30 | |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 31 | void resolve_undo_write(struct strbuf *sb, struct string_list *resolve_undo) |
| 32 | { |
Alex Riesen | 8a57c6e | 2010-07-03 14:41:54 +0200 | [diff] [blame] | 33 | struct string_list_item *item; |
| 34 | for_each_string_list_item(item, resolve_undo) { |
| 35 | struct resolve_undo_info *ui = item->util; |
| 36 | int i; |
| 37 | |
| 38 | if (!ui) |
| 39 | continue; |
| 40 | strbuf_addstr(sb, item->string); |
| 41 | strbuf_addch(sb, 0); |
| 42 | for (i = 0; i < 3; i++) |
| 43 | strbuf_addf(sb, "%o%c", ui->mode[i], 0); |
| 44 | for (i = 0; i < 3; i++) { |
| 45 | if (!ui->mode[i]) |
| 46 | continue; |
| 47 | strbuf_add(sb, ui->sha1[i], 20); |
| 48 | } |
| 49 | } |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Junio C Hamano | b8bba41 | 2010-02-01 22:04:03 -0800 | [diff] [blame] | 52 | struct string_list *resolve_undo_read(const char *data, unsigned long size) |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 53 | { |
| 54 | struct string_list *resolve_undo; |
| 55 | size_t len; |
| 56 | char *endptr; |
| 57 | int i; |
| 58 | |
| 59 | resolve_undo = xcalloc(1, sizeof(*resolve_undo)); |
| 60 | resolve_undo->strdup_strings = 1; |
| 61 | |
| 62 | while (size) { |
| 63 | struct string_list_item *lost; |
| 64 | struct resolve_undo_info *ui; |
| 65 | |
| 66 | len = strlen(data) + 1; |
| 67 | if (size <= len) |
| 68 | goto error; |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 69 | lost = string_list_insert(resolve_undo, data); |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 70 | if (!lost->util) |
| 71 | lost->util = xcalloc(1, sizeof(*ui)); |
| 72 | ui = lost->util; |
| 73 | size -= len; |
| 74 | data += len; |
| 75 | |
| 76 | for (i = 0; i < 3; i++) { |
| 77 | ui->mode[i] = strtoul(data, &endptr, 8); |
| 78 | if (!endptr || endptr == data || *endptr) |
| 79 | goto error; |
| 80 | len = (endptr + 1) - (char*)data; |
| 81 | if (size <= len) |
| 82 | goto error; |
| 83 | size -= len; |
| 84 | data += len; |
| 85 | } |
| 86 | |
| 87 | for (i = 0; i < 3; i++) { |
| 88 | if (!ui->mode[i]) |
| 89 | continue; |
| 90 | if (size < 20) |
| 91 | goto error; |
Junio C Hamano | b8bba41 | 2010-02-01 22:04:03 -0800 | [diff] [blame] | 92 | hashcpy(ui->sha1[i], (const unsigned char *)data); |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 93 | size -= 20; |
| 94 | data += 20; |
| 95 | } |
| 96 | } |
| 97 | return resolve_undo; |
| 98 | |
| 99 | error: |
| 100 | string_list_clear(resolve_undo, 1); |
| 101 | error("Index records invalid resolve-undo information"); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | void resolve_undo_clear_index(struct index_state *istate) |
| 106 | { |
| 107 | struct string_list *resolve_undo = istate->resolve_undo; |
| 108 | if (!resolve_undo) |
| 109 | return; |
| 110 | string_list_clear(resolve_undo, 1); |
| 111 | free(resolve_undo); |
| 112 | istate->resolve_undo = NULL; |
Nguyễn Thái Ngọc Duy | 6c306a3 | 2014-06-13 19:19:29 +0700 | [diff] [blame] | 113 | istate->cache_changed |= RESOLVE_UNDO_CHANGED; |
Junio C Hamano | cfc5789 | 2009-12-25 00:30:51 -0800 | [diff] [blame] | 114 | } |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 115 | |
| 116 | int unmerge_index_entry_at(struct index_state *istate, int pos) |
| 117 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 118 | const struct cache_entry *ce; |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 119 | struct string_list_item *item; |
| 120 | struct resolve_undo_info *ru; |
Nguyễn Thái Ngọc Duy | e721c15 | 2013-03-27 12:58:21 +0700 | [diff] [blame] | 121 | int i, err = 0, matched; |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 122 | char *name; |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 123 | |
| 124 | if (!istate->resolve_undo) |
| 125 | return pos; |
| 126 | |
| 127 | ce = istate->cache[pos]; |
| 128 | if (ce_stage(ce)) { |
| 129 | /* already unmerged */ |
| 130 | while ((pos < istate->cache_nr) && |
| 131 | ! strcmp(istate->cache[pos]->name, ce->name)) |
| 132 | pos++; |
| 133 | return pos - 1; /* return the last entry processed */ |
| 134 | } |
Julian Phillips | e8c8b71 | 2010-06-26 00:41:37 +0100 | [diff] [blame] | 135 | item = string_list_lookup(istate->resolve_undo, ce->name); |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 136 | if (!item) |
| 137 | return pos; |
| 138 | ru = item->util; |
| 139 | if (!ru) |
| 140 | return pos; |
Nguyễn Thái Ngọc Duy | e721c15 | 2013-03-27 12:58:21 +0700 | [diff] [blame] | 141 | matched = ce->ce_flags & CE_MATCHED; |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 142 | name = xstrdup(ce->name); |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 143 | remove_index_entry_at(istate, pos); |
| 144 | for (i = 0; i < 3; i++) { |
| 145 | struct cache_entry *nce; |
| 146 | if (!ru->mode[i]) |
| 147 | continue; |
| 148 | nce = make_cache_entry(ru->mode[i], ru->sha1[i], |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 149 | name, i + 1, 0); |
Nguyễn Thái Ngọc Duy | e721c15 | 2013-03-27 12:58:21 +0700 | [diff] [blame] | 150 | if (matched) |
| 151 | nce->ce_flags |= CE_MATCHED; |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 152 | if (add_index_entry(istate, nce, ADD_CACHE_OK_TO_ADD)) { |
| 153 | err = 1; |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 154 | error("cannot unmerge '%s'", name); |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
Karsten Blees | 5699d17 | 2013-11-14 20:24:37 +0100 | [diff] [blame] | 157 | free(name); |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 158 | if (err) |
| 159 | return pos; |
| 160 | free(ru); |
| 161 | item->util = NULL; |
| 162 | return unmerge_index_entry_at(istate, pos); |
| 163 | } |
| 164 | |
Nguyễn Thái Ngọc Duy | e721c15 | 2013-03-27 12:58:21 +0700 | [diff] [blame] | 165 | void unmerge_marked_index(struct index_state *istate) |
| 166 | { |
| 167 | int i; |
| 168 | |
| 169 | if (!istate->resolve_undo) |
| 170 | return; |
| 171 | |
| 172 | for (i = 0; i < istate->cache_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 173 | const struct cache_entry *ce = istate->cache[i]; |
Nguyễn Thái Ngọc Duy | e721c15 | 2013-03-27 12:58:21 +0700 | [diff] [blame] | 174 | if (ce->ce_flags & CE_MATCHED) |
| 175 | i = unmerge_index_entry_at(istate, i); |
| 176 | } |
| 177 | } |
| 178 | |
Nguyễn Thái Ngọc Duy | 5ab0651 | 2013-07-14 15:35:51 +0700 | [diff] [blame] | 179 | void unmerge_index(struct index_state *istate, const struct pathspec *pathspec) |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 180 | { |
| 181 | int i; |
| 182 | |
| 183 | if (!istate->resolve_undo) |
| 184 | return; |
| 185 | |
| 186 | for (i = 0; i < istate->cache_nr; i++) { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 187 | const struct cache_entry *ce = istate->cache[i]; |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 188 | if (!ce_path_match(ce, pathspec, NULL)) |
Junio C Hamano | 4421a82 | 2009-12-25 11:57:11 -0800 | [diff] [blame] | 189 | continue; |
| 190 | i = unmerge_index_entry_at(istate, i); |
| 191 | } |
| 192 | } |