Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "sha1-lookup.h" |
| 3 | #include "refs.h" |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 4 | #include "commit.h" |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 5 | |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 6 | /* |
| 7 | * An array of replacements. The array is kept sorted by the original |
| 8 | * sha1. |
| 9 | */ |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 10 | static struct replace_object { |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 11 | unsigned char original[20]; |
| 12 | unsigned char replacement[20]; |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 13 | } **replace_object; |
| 14 | |
| 15 | static int replace_object_alloc, replace_object_nr; |
| 16 | |
| 17 | static const unsigned char *replace_sha1_access(size_t index, void *table) |
| 18 | { |
| 19 | struct replace_object **replace = table; |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 20 | return replace[index]->original; |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static int replace_object_pos(const unsigned char *sha1) |
| 24 | { |
| 25 | return sha1_pos(sha1, replace_object, replace_object_nr, |
| 26 | replace_sha1_access); |
| 27 | } |
| 28 | |
| 29 | static int register_replace_object(struct replace_object *replace, |
| 30 | int ignore_dups) |
| 31 | { |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 32 | int pos = replace_object_pos(replace->original); |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 33 | |
| 34 | if (0 <= pos) { |
| 35 | if (ignore_dups) |
| 36 | free(replace); |
| 37 | else { |
| 38 | free(replace_object[pos]); |
| 39 | replace_object[pos] = replace; |
| 40 | } |
| 41 | return 1; |
| 42 | } |
| 43 | pos = -pos - 1; |
Dmitry S. Dolzhenko | 72004b4 | 2014-03-04 02:31:56 +0400 | [diff] [blame] | 44 | ALLOC_GROW(replace_object, replace_object_nr + 1, replace_object_alloc); |
| 45 | replace_object_nr++; |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 46 | if (pos < replace_object_nr) |
| 47 | memmove(replace_object + pos + 1, |
| 48 | replace_object + pos, |
| 49 | (replace_object_nr - pos - 1) * |
| 50 | sizeof(*replace_object)); |
| 51 | replace_object[pos] = replace; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int register_replace_ref(const char *refname, |
| 56 | const unsigned char *sha1, |
| 57 | int flag, void *cb_data) |
| 58 | { |
| 59 | /* Get sha1 from refname */ |
| 60 | const char *slash = strrchr(refname, '/'); |
| 61 | const char *hash = slash ? slash + 1 : refname; |
| 62 | struct replace_object *repl_obj = xmalloc(sizeof(*repl_obj)); |
| 63 | |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 64 | if (strlen(hash) != 40 || get_sha1_hex(hash, repl_obj->original)) { |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 65 | free(repl_obj); |
| 66 | warning("bad replace ref name: %s", refname); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* Copy sha1 from the read ref */ |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 71 | hashcpy(repl_obj->replacement, sha1); |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 72 | |
| 73 | /* Register new object */ |
| 74 | if (register_replace_object(repl_obj, 1)) |
| 75 | die("duplicate replace ref: %s", refname); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void prepare_replace_object(void) |
| 81 | { |
| 82 | static int replace_object_prepared; |
| 83 | |
| 84 | if (replace_object_prepared) |
| 85 | return; |
| 86 | |
| 87 | for_each_replace_ref(register_replace_ref, NULL); |
| 88 | replace_object_prepared = 1; |
Junio C Hamano | e1111ce | 2011-05-15 12:54:53 -0700 | [diff] [blame] | 89 | if (!replace_object_nr) |
Michael Haggerty | afc711b | 2014-02-18 12:24:55 +0100 | [diff] [blame] | 90 | check_replace_refs = 0; |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* We allow "recursive" replacement. Only within reason, though */ |
| 94 | #define MAXREPLACEDEPTH 5 |
| 95 | |
Michael Haggerty | 1f91e79 | 2014-02-28 17:29:16 +0100 | [diff] [blame] | 96 | /* |
| 97 | * If a replacement for object sha1 has been set up, return the |
| 98 | * replacement object's name (replaced recursively, if necessary). |
| 99 | * The return value is either sha1 or a pointer to a |
| 100 | * permanently-allocated value. This function always respects replace |
| 101 | * references, regardless of the value of check_replace_refs. |
| 102 | */ |
Junio C Hamano | e1111ce | 2011-05-15 12:54:53 -0700 | [diff] [blame] | 103 | const unsigned char *do_lookup_replace_object(const unsigned char *sha1) |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 104 | { |
| 105 | int pos, depth = MAXREPLACEDEPTH; |
| 106 | const unsigned char *cur = sha1; |
| 107 | |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 108 | prepare_replace_object(); |
| 109 | |
| 110 | /* Try to recursively replace the object */ |
| 111 | do { |
| 112 | if (--depth < 0) |
| 113 | die("replace depth too high for object %s", |
| 114 | sha1_to_hex(sha1)); |
| 115 | |
| 116 | pos = replace_object_pos(cur); |
| 117 | if (0 <= pos) |
Michael Haggerty | ce37586 | 2014-02-21 17:32:03 +0100 | [diff] [blame] | 118 | cur = replace_object[pos]->replacement; |
Christian Couder | 6809557 | 2009-01-23 10:06:53 +0100 | [diff] [blame] | 119 | } while (0 <= pos); |
| 120 | |
| 121 | return cur; |
| 122 | } |