| Daniel Barkalow | 95fc751 | 2005-06-06 20:31:29 | [diff] [blame] | 1 | #include "cache.h" |
| Michael Haggerty | 697cc8e | 2014-10-01 10:28:42 | [diff] [blame] | 2 | #include "lockfile.h" |
| Junio C Hamano | 8502357 | 2006-12-19 22:34:12 | [diff] [blame] | 3 | #include "refs.h" |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 4 | #include "object.h" |
| 5 | #include "tag.h" |
| Johannes Schindelin | 7155b72 | 2007-09-28 15:28:54 | [diff] [blame] | 6 | #include "dir.h" |
| Junio C Hamano | daebaa7 | 2013-01-19 00:08:30 | [diff] [blame] | 7 | #include "string-list.h" |
| Daniel Barkalow | 95fc751 | 2005-06-06 20:31:29 | [diff] [blame] | 8 | |
| Stefan Beller | 3581d79 | 2014-12-12 08:57:02 | [diff] [blame] | 9 | struct ref_lock { |
| 10 | char *ref_name; |
| 11 | char *orig_ref_name; |
| 12 | struct lock_file *lk; |
| 13 | unsigned char old_sha1[20]; |
| 14 | int lock_fd; |
| Stefan Beller | 3581d79 | 2014-12-12 08:57:02 | [diff] [blame] | 15 | }; |
| 16 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 17 | /* |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 18 | * How to handle various characters in refnames: |
| 19 | * 0: An acceptable character for refs |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 20 | * 1: End-of-component |
| 21 | * 2: ., look for a preceding . to reject .. in refs |
| 22 | * 3: {, look for a preceding @ to reject @{ in refs |
| 23 | * 4: A bad character: ASCII control characters, "~", "^", ":" or SP |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 24 | */ |
| 25 | static unsigned char refname_disposition[256] = { |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 26 | 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 27 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 28 | 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 1, |
| 29 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 30 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 31 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
| 32 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 33 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* |
| Michael Haggerty | 581d4e0 | 2015-02-12 11:12:12 | [diff] [blame] | 37 | * Flag passed to lock_ref_sha1_basic() telling it to tolerate broken |
| 38 | * refs (i.e., because the reference is about to be deleted anyway). |
| 39 | */ |
| 40 | #define REF_DELETING 0x02 |
| 41 | |
| 42 | /* |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 43 | * Used as a flag in ref_update::flags when a loose ref is being |
| Ronnie Sahlberg | 029cdb4 | 2014-04-30 16:03:36 | [diff] [blame] | 44 | * pruned. |
| 45 | */ |
| Michael Haggerty | 31e79f0 | 2015-02-12 11:12:13 | [diff] [blame] | 46 | #define REF_ISPRUNING 0x04 |
| 47 | |
| Ronnie Sahlberg | 029cdb4 | 2014-04-30 16:03:36 | [diff] [blame] | 48 | /* |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 49 | * Used as a flag in ref_update::flags when the reference should be |
| 50 | * updated to new_sha1. |
| 51 | */ |
| 52 | #define REF_HAVE_NEW 0x08 |
| 53 | |
| 54 | /* |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 55 | * Used as a flag in ref_update::flags when old_sha1 should be |
| 56 | * checked. |
| 57 | */ |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 58 | #define REF_HAVE_OLD 0x10 |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 59 | |
| 60 | /* |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 61 | * Used as a flag in ref_update::flags when the lockfile needs to be |
| 62 | * committed. |
| 63 | */ |
| 64 | #define REF_NEEDS_COMMIT 0x20 |
| 65 | |
| 66 | /* |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 67 | * Try to read one refname component from the front of refname. |
| 68 | * Return the length of the component found, or -1 if the component is |
| 69 | * not legal. It is legal if it is something reasonable to have under |
| 70 | * ".git/refs/"; We do not like it if: |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 71 | * |
| 72 | * - any path component of it begins with ".", or |
| 73 | * - it has double dots "..", or |
| 74 | * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 75 | * - it ends with a "/". |
| 76 | * - it ends with ".lock" |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 77 | * - it contains a "\" (backslash) |
| 78 | */ |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 79 | static int check_refname_component(const char *refname, int flags) |
| 80 | { |
| 81 | const char *cp; |
| 82 | char last = '\0'; |
| 83 | |
| 84 | for (cp = refname; ; cp++) { |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 85 | int ch = *cp & 255; |
| 86 | unsigned char disp = refname_disposition[ch]; |
| 87 | switch (disp) { |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 88 | case 1: |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 89 | goto out; |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 90 | case 2: |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 91 | if (last == '.') |
| 92 | return -1; /* Refname contains "..". */ |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 93 | break; |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 94 | case 3: |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 95 | if (last == '@') |
| 96 | return -1; /* Refname contains "@{". */ |
| 97 | break; |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 98 | case 4: |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 101 | last = ch; |
| 102 | } |
| David Turner | dde8a90 | 2014-06-04 03:38:10 | [diff] [blame] | 103 | out: |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 104 | if (cp == refname) |
| Michael Haggerty | dac529e | 2012-04-10 05:30:22 | [diff] [blame] | 105 | return 0; /* Component has zero length. */ |
| Jonathan Nieder | f3cc52d | 2014-09-26 19:22:22 | [diff] [blame] | 106 | if (refname[0] == '.') |
| 107 | return -1; /* Component starts with '.'. */ |
| Michael Haggerty | 7108ad2 | 2014-10-01 10:28:15 | [diff] [blame] | 108 | if (cp - refname >= LOCK_SUFFIX_LEN && |
| 109 | !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN)) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 110 | return -1; /* Refname ends with ".lock". */ |
| 111 | return cp - refname; |
| 112 | } |
| 113 | |
| Junio C Hamano | 5e65022 | 2014-07-28 17:41:53 | [diff] [blame] | 114 | int check_refname_format(const char *refname, int flags) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 115 | { |
| 116 | int component_len, component_count = 0; |
| 117 | |
| Felipe Contreras | 9ba89f4 | 2013-09-02 06:34:30 | [diff] [blame] | 118 | if (!strcmp(refname, "@")) |
| 119 | /* Refname is a single character '@'. */ |
| 120 | return -1; |
| 121 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 122 | while (1) { |
| 123 | /* We are at the start of a path component. */ |
| 124 | component_len = check_refname_component(refname, flags); |
| Michael Haggerty | dac529e | 2012-04-10 05:30:22 | [diff] [blame] | 125 | if (component_len <= 0) { |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 126 | if ((flags & REFNAME_REFSPEC_PATTERN) && |
| 127 | refname[0] == '*' && |
| 128 | (refname[1] == '\0' || refname[1] == '/')) { |
| 129 | /* Accept one wildcard as a full refname component. */ |
| 130 | flags &= ~REFNAME_REFSPEC_PATTERN; |
| 131 | component_len = 1; |
| 132 | } else { |
| 133 | return -1; |
| 134 | } |
| 135 | } |
| 136 | component_count++; |
| 137 | if (refname[component_len] == '\0') |
| 138 | break; |
| 139 | /* Skip to next component. */ |
| 140 | refname += component_len + 1; |
| 141 | } |
| 142 | |
| 143 | if (refname[component_len - 1] == '.') |
| 144 | return -1; /* Refname ends with '.'. */ |
| 145 | if (!(flags & REFNAME_ALLOW_ONELEVEL) && component_count < 2) |
| 146 | return -1; /* Refname has only one component. */ |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | struct ref_entry; |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 151 | |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 152 | /* |
| 153 | * Information used (along with the information in ref_entry) to |
| 154 | * describe a single cached reference. This data structure only |
| 155 | * occurs embedded in a union in struct ref_entry, and only when |
| 156 | * (ref_entry->flag & REF_DIR) is zero. |
| 157 | */ |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 158 | struct ref_value { |
| Michael Haggerty | 6c6f58d | 2013-04-14 12:54:17 | [diff] [blame] | 159 | /* |
| 160 | * The name of the object to which this reference resolves |
| 161 | * (which may be a tag object). If REF_ISBROKEN, this is |
| 162 | * null. If REF_ISSYMREF, then this is the name of the object |
| 163 | * referred to by the last reference in the symlink chain. |
| 164 | */ |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 165 | unsigned char sha1[20]; |
| Michael Haggerty | 6c6f58d | 2013-04-14 12:54:17 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * If REF_KNOWS_PEELED, then this field holds the peeled value |
| 169 | * of this reference, or null if the reference is known not to |
| Michael Haggerty | 2312a79 | 2013-04-22 19:52:21 | [diff] [blame] | 170 | * be peelable. See the documentation for peel_ref() for an |
| 171 | * exact definition of "peelable". |
| Michael Haggerty | 6c6f58d | 2013-04-14 12:54:17 | [diff] [blame] | 172 | */ |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 173 | unsigned char peeled[20]; |
| 174 | }; |
| 175 | |
| Michael Haggerty | f006c42 | 2012-04-26 22:27:05 | [diff] [blame] | 176 | struct ref_cache; |
| 177 | |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 178 | /* |
| 179 | * Information used (along with the information in ref_entry) to |
| 180 | * describe a level in the hierarchy of references. This data |
| 181 | * structure only occurs embedded in a union in struct ref_entry, and |
| 182 | * only when (ref_entry.flag & REF_DIR) is set. In that case, |
| 183 | * (ref_entry.flag & REF_INCOMPLETE) determines whether the references |
| 184 | * in the directory have already been read: |
| 185 | * |
| 186 | * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose |
| 187 | * or packed references, already read. |
| 188 | * |
| 189 | * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose |
| 190 | * references that hasn't been read yet (nor has any of its |
| 191 | * subdirectories). |
| 192 | * |
| 193 | * Entries within a directory are stored within a growable array of |
| 194 | * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i < |
| 195 | * sorted are sorted by their component name in strcmp() order and the |
| 196 | * remaining entries are unsorted. |
| 197 | * |
| 198 | * Loose references are read lazily, one directory at a time. When a |
| 199 | * directory of loose references is read, then all of the references |
| 200 | * in that directory are stored, and REF_INCOMPLETE stubs are created |
| 201 | * for any subdirectories, but the subdirectories themselves are not |
| 202 | * read. The reading is triggered by get_ref_dir(). |
| 203 | */ |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 204 | struct ref_dir { |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 205 | int nr, alloc; |
| Michael Haggerty | e6ed3ca | 2012-01-17 05:50:32 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * Entries with index 0 <= i < sorted are sorted by name. New |
| 209 | * entries are appended to the list unsorted, and are sorted |
| 210 | * only when required; thus we avoid the need to sort the list |
| 211 | * after the addition of every reference. |
| 212 | */ |
| 213 | int sorted; |
| 214 | |
| Michael Haggerty | f006c42 | 2012-04-26 22:27:05 | [diff] [blame] | 215 | /* A pointer to the ref_cache that contains this ref_dir. */ |
| 216 | struct ref_cache *ref_cache; |
| 217 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 218 | struct ref_entry **entries; |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 219 | }; |
| 220 | |
| Michael Haggerty | 89df9c8 | 2013-04-14 12:54:16 | [diff] [blame] | 221 | /* |
| 222 | * Bit values for ref_entry::flag. REF_ISSYMREF=0x01, |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 223 | * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are |
| 224 | * public values; see refs.h. |
| Michael Haggerty | 89df9c8 | 2013-04-14 12:54:16 | [diff] [blame] | 225 | */ |
| 226 | |
| 227 | /* |
| 228 | * The field ref_entry->u.value.peeled of this value entry contains |
| 229 | * the correct peeled value for the reference, which might be |
| 230 | * null_sha1 if the reference is not a tag or if it is broken. |
| 231 | */ |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 232 | #define REF_KNOWS_PEELED 0x10 |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 233 | |
| 234 | /* ref_entry represents a directory of references */ |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 235 | #define REF_DIR 0x20 |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 236 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 237 | /* |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 238 | * Entry has not yet been read from disk (used only for REF_DIR |
| 239 | * entries representing loose references) |
| 240 | */ |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 241 | #define REF_INCOMPLETE 0x40 |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 242 | |
| 243 | /* |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 244 | * A ref_entry represents either a reference or a "subdirectory" of |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 245 | * references. |
| 246 | * |
| 247 | * Each directory in the reference namespace is represented by a |
| 248 | * ref_entry with (flags & REF_DIR) set and containing a subdir member |
| 249 | * that holds the entries in that directory that have been read so |
| 250 | * far. If (flags & REF_INCOMPLETE) is set, then the directory and |
| 251 | * its subdirectories haven't been read yet. REF_INCOMPLETE is only |
| 252 | * used for loose reference directories. |
| 253 | * |
| 254 | * References are represented by a ref_entry with (flags & REF_DIR) |
| 255 | * unset and a value member that describes the reference's value. The |
| 256 | * flag member is at the ref_entry level, but it is also needed to |
| 257 | * interpret the contents of the value field (in other words, a |
| 258 | * ref_value object is not very much use without the enclosing |
| 259 | * ref_entry). |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 260 | * |
| 261 | * Reference names cannot end with slash and directories' names are |
| 262 | * always stored with a trailing slash (except for the top-level |
| 263 | * directory, which is always denoted by ""). This has two nice |
| 264 | * consequences: (1) when the entries in each subdir are sorted |
| 265 | * lexicographically by name (as they usually are), the references in |
| 266 | * a whole tree can be generated in lexicographic order by traversing |
| 267 | * the tree in left-to-right, depth-first order; (2) the names of |
| 268 | * references and subdirectories cannot conflict, and therefore the |
| 269 | * presence of an empty subdirectory does not block the creation of a |
| 270 | * similarly-named reference. (The fact that reference names with the |
| 271 | * same leading components can conflict *with each other* is a |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 272 | * separate issue that is regulated by verify_refname_available().) |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 273 | * |
| 274 | * Please note that the name field contains the fully-qualified |
| 275 | * reference (or subdirectory) name. Space could be saved by only |
| 276 | * storing the relative names. But that would require the full names |
| 277 | * to be generated on the fly when iterating in do_for_each_ref(), and |
| 278 | * would break callback functions, who have always been able to assume |
| 279 | * that the name strings that they are passed will not be freed during |
| 280 | * the iteration. |
| 281 | */ |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 282 | struct ref_entry { |
| 283 | unsigned char flag; /* ISSYMREF? ISPACKED? */ |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 284 | union { |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 285 | struct ref_value value; /* if not (flags&REF_DIR) */ |
| 286 | struct ref_dir subdir; /* if (flags&REF_DIR) */ |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 287 | } u; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 288 | /* |
| 289 | * The full name of the reference (e.g., "refs/heads/master") |
| 290 | * or the full name of the directory with a trailing slash |
| 291 | * (e.g., "refs/heads/"): |
| 292 | */ |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 293 | char name[FLEX_ARRAY]; |
| 294 | }; |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 295 | |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 296 | static void read_loose_refs(const char *dirname, struct ref_dir *dir); |
| 297 | |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 298 | static struct ref_dir *get_ref_dir(struct ref_entry *entry) |
| 299 | { |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 300 | struct ref_dir *dir; |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 301 | assert(entry->flag & REF_DIR); |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 302 | dir = &entry->u.subdir; |
| 303 | if (entry->flag & REF_INCOMPLETE) { |
| 304 | read_loose_refs(entry->name, dir); |
| 305 | entry->flag &= ~REF_INCOMPLETE; |
| 306 | } |
| 307 | return dir; |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 308 | } |
| 309 | |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 310 | /* |
| 311 | * Check if a refname is safe. |
| 312 | * For refs that start with "refs/" we consider it safe as long they do |
| 313 | * not try to resolve to outside of refs/. |
| 314 | * |
| 315 | * For all other refs we only consider them safe iff they only contain |
| 316 | * upper case characters and '_' (like "HEAD" AND "MERGE_HEAD", and not like |
| 317 | * "config"). |
| 318 | */ |
| 319 | static int refname_is_safe(const char *refname) |
| 320 | { |
| 321 | if (starts_with(refname, "refs/")) { |
| 322 | char *buf; |
| 323 | int result; |
| 324 | |
| 325 | buf = xmalloc(strlen(refname) + 1); |
| 326 | /* |
| 327 | * Does the refname try to escape refs/? |
| 328 | * For example: refs/foo/../bar is safe but refs/foo/../../bar |
| 329 | * is not. |
| 330 | */ |
| 331 | result = !normalize_path_copy(buf, refname + strlen("refs/")); |
| 332 | free(buf); |
| 333 | return result; |
| 334 | } |
| 335 | while (*refname) { |
| 336 | if (!isupper(*refname) && *refname != '_') |
| 337 | return 0; |
| 338 | refname++; |
| 339 | } |
| 340 | return 1; |
| 341 | } |
| 342 | |
| Michael Haggerty | cddc425 | 2011-12-12 05:38:22 | [diff] [blame] | 343 | static struct ref_entry *create_ref_entry(const char *refname, |
| 344 | const unsigned char *sha1, int flag, |
| 345 | int check_name) |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 346 | { |
| 347 | int len; |
| Michael Haggerty | cddc425 | 2011-12-12 05:38:22 | [diff] [blame] | 348 | struct ref_entry *ref; |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 349 | |
| Junio C Hamano | 09116a1 | 2011-11-17 00:54:32 | [diff] [blame] | 350 | if (check_name && |
| Jonathan Nieder | f3cc52d | 2014-09-26 19:22:22 | [diff] [blame] | 351 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 352 | die("Reference has invalid format: '%s'", refname); |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 353 | if (!check_name && !refname_is_safe(refname)) |
| 354 | die("Reference has invalid name: '%s'", refname); |
| Michael Haggerty | cddc425 | 2011-12-12 05:38:22 | [diff] [blame] | 355 | len = strlen(refname) + 1; |
| 356 | ref = xmalloc(sizeof(struct ref_entry) + len); |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 357 | hashcpy(ref->u.value.sha1, sha1); |
| 358 | hashclr(ref->u.value.peeled); |
| Michael Haggerty | cddc425 | 2011-12-12 05:38:22 | [diff] [blame] | 359 | memcpy(ref->name, refname, len); |
| 360 | ref->flag = flag; |
| 361 | return ref; |
| 362 | } |
| 363 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 364 | static void clear_ref_dir(struct ref_dir *dir); |
| 365 | |
| Michael Haggerty | 732134e | 2012-04-10 05:30:21 | [diff] [blame] | 366 | static void free_ref_entry(struct ref_entry *entry) |
| 367 | { |
| Michael Haggerty | 27b5587 | 2012-05-20 06:49:32 | [diff] [blame] | 368 | if (entry->flag & REF_DIR) { |
| 369 | /* |
| 370 | * Do not use get_ref_dir() here, as that might |
| 371 | * trigger the reading of loose refs. |
| 372 | */ |
| 373 | clear_ref_dir(&entry->u.subdir); |
| 374 | } |
| Michael Haggerty | 732134e | 2012-04-10 05:30:21 | [diff] [blame] | 375 | free(entry); |
| 376 | } |
| 377 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 378 | /* |
| 379 | * Add a ref_entry to the end of dir (unsorted). Entry is always |
| 380 | * stored directly in dir; no recursion into subdirectories is |
| 381 | * done. |
| 382 | */ |
| 383 | static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry) |
| Michael Haggerty | cddc425 | 2011-12-12 05:38:22 | [diff] [blame] | 384 | { |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 385 | ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc); |
| 386 | dir->entries[dir->nr++] = entry; |
| Michael Haggerty | 654ad40 | 2012-05-24 12:16:50 | [diff] [blame] | 387 | /* optimize for the case that entries are added in order */ |
| 388 | if (dir->nr == 1 || |
| 389 | (dir->nr == dir->sorted + 1 && |
| 390 | strcmp(dir->entries[dir->nr - 2]->name, |
| 391 | dir->entries[dir->nr - 1]->name) < 0)) |
| 392 | dir->sorted = dir->nr; |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 393 | } |
| 394 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 395 | /* |
| 396 | * Clear and free all entries in dir, recursively. |
| 397 | */ |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 398 | static void clear_ref_dir(struct ref_dir *dir) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 399 | { |
| 400 | int i; |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 401 | for (i = 0; i < dir->nr; i++) |
| 402 | free_ref_entry(dir->entries[i]); |
| 403 | free(dir->entries); |
| 404 | dir->sorted = dir->nr = dir->alloc = 0; |
| 405 | dir->entries = NULL; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 406 | } |
| 407 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 408 | /* |
| 409 | * Create a struct ref_entry object for the specified dirname. |
| 410 | * dirname is the name of the directory with a trailing slash (e.g., |
| 411 | * "refs/heads/") or "" for the top-level directory. |
| 412 | */ |
| Michael Haggerty | f006c42 | 2012-04-26 22:27:05 | [diff] [blame] | 413 | static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache, |
| René Scharfe | b9146f5 | 2012-05-22 18:50:52 | [diff] [blame] | 414 | const char *dirname, size_t len, |
| 415 | int incomplete) |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 416 | { |
| 417 | struct ref_entry *direntry; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 418 | direntry = xcalloc(1, sizeof(struct ref_entry) + len + 1); |
| René Scharfe | b9146f5 | 2012-05-22 18:50:52 | [diff] [blame] | 419 | memcpy(direntry->name, dirname, len); |
| 420 | direntry->name[len] = '\0'; |
| Michael Haggerty | f006c42 | 2012-04-26 22:27:05 | [diff] [blame] | 421 | direntry->u.subdir.ref_cache = ref_cache; |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 422 | direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 423 | return direntry; |
| 424 | } |
| 425 | |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 426 | static int ref_entry_cmp(const void *a, const void *b) |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 427 | { |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 428 | struct ref_entry *one = *(struct ref_entry **)a; |
| 429 | struct ref_entry *two = *(struct ref_entry **)b; |
| 430 | return strcmp(one->name, two->name); |
| 431 | } |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 432 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 433 | static void sort_ref_dir(struct ref_dir *dir); |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 434 | |
| Junio C Hamano | e1980c9 | 2012-05-22 21:03:29 | [diff] [blame] | 435 | struct string_slice { |
| 436 | size_t len; |
| 437 | const char *str; |
| 438 | }; |
| 439 | |
| 440 | static int ref_entry_cmp_sslice(const void *key_, const void *ent_) |
| 441 | { |
| René Scharfe | c971ddf | 2013-01-16 01:08:16 | [diff] [blame] | 442 | const struct string_slice *key = key_; |
| 443 | const struct ref_entry *ent = *(const struct ref_entry * const *)ent_; |
| 444 | int cmp = strncmp(key->str, ent->name, key->len); |
| Junio C Hamano | e1980c9 | 2012-05-22 21:03:29 | [diff] [blame] | 445 | if (cmp) |
| 446 | return cmp; |
| René Scharfe | c971ddf | 2013-01-16 01:08:16 | [diff] [blame] | 447 | return '\0' - (unsigned char)ent->name[key->len]; |
| Junio C Hamano | e1980c9 | 2012-05-22 21:03:29 | [diff] [blame] | 448 | } |
| 449 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 450 | /* |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 451 | * Return the index of the entry with the given refname from the |
| 452 | * ref_dir (non-recursively), sorting dir if necessary. Return -1 if |
| 453 | * no such entry is found. dir must already be complete. |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 454 | */ |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 455 | static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 456 | { |
| Junio C Hamano | e1980c9 | 2012-05-22 21:03:29 | [diff] [blame] | 457 | struct ref_entry **r; |
| 458 | struct string_slice key; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 459 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 460 | if (refname == NULL || !dir->nr) |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 461 | return -1; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 462 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 463 | sort_ref_dir(dir); |
| Junio C Hamano | e1980c9 | 2012-05-22 21:03:29 | [diff] [blame] | 464 | key.len = len; |
| 465 | key.str = refname; |
| 466 | r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries), |
| 467 | ref_entry_cmp_sslice); |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 468 | |
| 469 | if (r == NULL) |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 470 | return -1; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 471 | |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 472 | return r - dir->entries; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 473 | } |
| 474 | |
| Michael Haggerty | 202a56a | 2011-12-12 05:38:15 | [diff] [blame] | 475 | /* |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 476 | * Search for a directory entry directly within dir (without |
| 477 | * recursing). Sort dir if necessary. subdirname must be a directory |
| 478 | * name (i.e., end in '/'). If mkdir is set, then create the |
| 479 | * directory if it is missing; otherwise, return NULL if the desired |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 480 | * directory cannot be found. dir must already be complete. |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 481 | */ |
| Michael Haggerty | 3f3aa1b | 2012-04-26 22:27:04 | [diff] [blame] | 482 | static struct ref_dir *search_for_subdir(struct ref_dir *dir, |
| René Scharfe | dd02e72 | 2012-05-22 18:50:58 | [diff] [blame] | 483 | const char *subdirname, size_t len, |
| 484 | int mkdir) |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 485 | { |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 486 | int entry_index = search_ref_dir(dir, subdirname, len); |
| 487 | struct ref_entry *entry; |
| 488 | if (entry_index == -1) { |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 489 | if (!mkdir) |
| 490 | return NULL; |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 491 | /* |
| 492 | * Since dir is complete, the absence of a subdir |
| 493 | * means that the subdir really doesn't exist; |
| 494 | * therefore, create an empty record for it but mark |
| 495 | * the record complete. |
| 496 | */ |
| René Scharfe | b9146f5 | 2012-05-22 18:50:52 | [diff] [blame] | 497 | entry = create_dir_entry(dir->ref_cache, subdirname, len, 0); |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 498 | add_entry_to_dir(dir, entry); |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 499 | } else { |
| 500 | entry = dir->entries[entry_index]; |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 501 | } |
| Michael Haggerty | 3f3aa1b | 2012-04-26 22:27:04 | [diff] [blame] | 502 | return get_ref_dir(entry); |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 506 | * If refname is a reference name, find the ref_dir within the dir |
| 507 | * tree that should hold refname. If refname is a directory name |
| 508 | * (i.e., ends in '/'), then return that ref_dir itself. dir must |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 509 | * represent the top-level directory and must already be complete. |
| 510 | * Sort ref_dirs and recurse into subdirectories as necessary. If |
| 511 | * mkdir is set, then create any missing directories; otherwise, |
| 512 | * return NULL if the desired directory cannot be found. |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 513 | */ |
| 514 | static struct ref_dir *find_containing_dir(struct ref_dir *dir, |
| 515 | const char *refname, int mkdir) |
| 516 | { |
| Michael Haggerty | 5fa0441 | 2012-04-26 22:27:00 | [diff] [blame] | 517 | const char *slash; |
| Michael Haggerty | 5fa0441 | 2012-04-26 22:27:00 | [diff] [blame] | 518 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { |
| René Scharfe | dd02e72 | 2012-05-22 18:50:58 | [diff] [blame] | 519 | size_t dirnamelen = slash - refname + 1; |
| Michael Haggerty | 3f3aa1b | 2012-04-26 22:27:04 | [diff] [blame] | 520 | struct ref_dir *subdir; |
| René Scharfe | dd02e72 | 2012-05-22 18:50:58 | [diff] [blame] | 521 | subdir = search_for_subdir(dir, refname, dirnamelen, mkdir); |
| Junio C Hamano | 663c129 | 2012-05-03 22:12:54 | [diff] [blame] | 522 | if (!subdir) { |
| 523 | dir = NULL; |
| Michael Haggerty | f348ac9 | 2012-04-24 22:45:11 | [diff] [blame] | 524 | break; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 525 | } |
| Michael Haggerty | 3f3aa1b | 2012-04-26 22:27:04 | [diff] [blame] | 526 | dir = subdir; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 527 | } |
| 528 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 529 | return dir; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Find the value entry with the given name in dir, sorting ref_dirs |
| 534 | * and recursing into subdirectories as necessary. If the name is not |
| 535 | * found or it corresponds to a directory entry, return NULL. |
| 536 | */ |
| 537 | static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname) |
| 538 | { |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 539 | int entry_index; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 540 | struct ref_entry *entry; |
| 541 | dir = find_containing_dir(dir, refname, 0); |
| 542 | if (!dir) |
| 543 | return NULL; |
| Michael Haggerty | 9fc0a64 | 2013-04-22 19:52:26 | [diff] [blame] | 544 | entry_index = search_ref_dir(dir, refname, strlen(refname)); |
| 545 | if (entry_index == -1) |
| 546 | return NULL; |
| 547 | entry = dir->entries[entry_index]; |
| 548 | return (entry->flag & REF_DIR) ? NULL : entry; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* |
| Michael Haggerty | 506a760 | 2013-04-22 19:52:27 | [diff] [blame] | 552 | * Remove the entry with the given name from dir, recursing into |
| 553 | * subdirectories as necessary. If refname is the name of a directory |
| 554 | * (i.e., ends with '/'), then remove the directory and its contents. |
| 555 | * If the removal was successful, return the number of entries |
| 556 | * remaining in the directory entry that contained the deleted entry. |
| 557 | * If the name was not found, return -1. Please note that this |
| 558 | * function only deletes the entry from the cache; it does not delete |
| 559 | * it from the filesystem or ensure that other cache entries (which |
| 560 | * might be symbolic references to the removed entry) are updated. |
| 561 | * Nor does it remove any containing dir entries that might be made |
| 562 | * empty by the removal. dir must represent the top-level directory |
| 563 | * and must already be complete. |
| 564 | */ |
| 565 | static int remove_entry(struct ref_dir *dir, const char *refname) |
| 566 | { |
| 567 | int refname_len = strlen(refname); |
| 568 | int entry_index; |
| 569 | struct ref_entry *entry; |
| 570 | int is_dir = refname[refname_len - 1] == '/'; |
| 571 | if (is_dir) { |
| 572 | /* |
| 573 | * refname represents a reference directory. Remove |
| 574 | * the trailing slash; otherwise we will get the |
| 575 | * directory *representing* refname rather than the |
| 576 | * one *containing* it. |
| 577 | */ |
| 578 | char *dirname = xmemdupz(refname, refname_len - 1); |
| 579 | dir = find_containing_dir(dir, dirname, 0); |
| 580 | free(dirname); |
| 581 | } else { |
| 582 | dir = find_containing_dir(dir, refname, 0); |
| 583 | } |
| 584 | if (!dir) |
| 585 | return -1; |
| 586 | entry_index = search_ref_dir(dir, refname, refname_len); |
| 587 | if (entry_index == -1) |
| 588 | return -1; |
| 589 | entry = dir->entries[entry_index]; |
| 590 | |
| 591 | memmove(&dir->entries[entry_index], |
| 592 | &dir->entries[entry_index + 1], |
| 593 | (dir->nr - entry_index - 1) * sizeof(*dir->entries) |
| 594 | ); |
| 595 | dir->nr--; |
| 596 | if (dir->sorted > entry_index) |
| 597 | dir->sorted--; |
| 598 | free_ref_entry(entry); |
| 599 | return dir->nr; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Add a ref_entry to the ref_dir (unsorted), recursing into |
| 604 | * subdirectories as necessary. dir must represent the top-level |
| 605 | * directory. Return 0 on success. |
| 606 | */ |
| 607 | static int add_ref(struct ref_dir *dir, struct ref_entry *ref) |
| 608 | { |
| 609 | dir = find_containing_dir(dir, ref->name, 1); |
| 610 | if (!dir) |
| 611 | return -1; |
| 612 | add_entry_to_dir(dir, ref); |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | /* |
| Michael Haggerty | 202a56a | 2011-12-12 05:38:15 | [diff] [blame] | 617 | * Emit a warning and return true iff ref1 and ref2 have the same name |
| 618 | * and the same sha1. Die if they have the same name but different |
| 619 | * sha1s. |
| 620 | */ |
| 621 | static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2) |
| 622 | { |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 623 | if (strcmp(ref1->name, ref2->name)) |
| Michael Haggerty | 202a56a | 2011-12-12 05:38:15 | [diff] [blame] | 624 | return 0; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 625 | |
| 626 | /* Duplicate name; make sure that they don't conflict: */ |
| 627 | |
| 628 | if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR)) |
| 629 | /* This is impossible by construction */ |
| 630 | die("Reference directory conflict: %s", ref1->name); |
| 631 | |
| 632 | if (hashcmp(ref1->u.value.sha1, ref2->u.value.sha1)) |
| 633 | die("Duplicated ref, and SHA1s don't match: %s", ref1->name); |
| 634 | |
| 635 | warning("Duplicated ref: %s", ref1->name); |
| 636 | return 1; |
| Michael Haggerty | 202a56a | 2011-12-12 05:38:15 | [diff] [blame] | 637 | } |
| 638 | |
| Michael Haggerty | e6ed3ca | 2012-01-17 05:50:32 | [diff] [blame] | 639 | /* |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 640 | * Sort the entries in dir non-recursively (if they are not already |
| 641 | * sorted) and remove any duplicate entries. |
| Michael Haggerty | e6ed3ca | 2012-01-17 05:50:32 | [diff] [blame] | 642 | */ |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 643 | static void sort_ref_dir(struct ref_dir *dir) |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 644 | { |
| Michael Haggerty | 202a56a | 2011-12-12 05:38:15 | [diff] [blame] | 645 | int i, j; |
| Michael Haggerty | 81a79d8 | 2012-04-10 05:30:25 | [diff] [blame] | 646 | struct ref_entry *last = NULL; |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 647 | |
| Michael Haggerty | e6ed3ca | 2012-01-17 05:50:32 | [diff] [blame] | 648 | /* |
| 649 | * This check also prevents passing a zero-length array to qsort(), |
| 650 | * which is a problem on some platforms. |
| 651 | */ |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 652 | if (dir->sorted == dir->nr) |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 653 | return; |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 654 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 655 | qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp); |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 656 | |
| Michael Haggerty | 81a79d8 | 2012-04-10 05:30:25 | [diff] [blame] | 657 | /* Remove any duplicates: */ |
| 658 | for (i = 0, j = 0; j < dir->nr; j++) { |
| 659 | struct ref_entry *entry = dir->entries[j]; |
| 660 | if (last && is_dup_ref(last, entry)) |
| 661 | free_ref_entry(entry); |
| 662 | else |
| 663 | last = dir->entries[i++] = entry; |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 664 | } |
| Michael Haggerty | 81a79d8 | 2012-04-10 05:30:25 | [diff] [blame] | 665 | dir->sorted = dir->nr = i; |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 666 | } |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 667 | |
| Michael Haggerty | fcce170 | 2013-04-22 19:52:11 | [diff] [blame] | 668 | /* Include broken references in a do_for_each_ref*() iteration: */ |
| 669 | #define DO_FOR_EACH_INCLUDE_BROKEN 0x01 |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 670 | |
| Michael Haggerty | 7d76fdc | 2013-04-22 19:52:12 | [diff] [blame] | 671 | /* |
| Michael Haggerty | 662428f | 2013-04-22 19:52:18 | [diff] [blame] | 672 | * Return true iff the reference described by entry can be resolved to |
| 673 | * an object in the database. Emit a warning if the referred-to |
| 674 | * object does not exist. |
| 675 | */ |
| 676 | static int ref_resolves_to_object(struct ref_entry *entry) |
| 677 | { |
| 678 | if (entry->flag & REF_ISBROKEN) |
| 679 | return 0; |
| 680 | if (!has_sha1_file(entry->u.value.sha1)) { |
| 681 | error("%s does not point to a valid object!", entry->name); |
| 682 | return 0; |
| 683 | } |
| 684 | return 1; |
| 685 | } |
| 686 | |
| 687 | /* |
| Michael Haggerty | 7d76fdc | 2013-04-22 19:52:12 | [diff] [blame] | 688 | * current_ref is a performance hack: when iterating over references |
| 689 | * using the for_each_ref*() functions, current_ref is set to the |
| 690 | * current reference's entry before calling the callback function. If |
| 691 | * the callback function calls peel_ref(), then peel_ref() first |
| 692 | * checks whether the reference to be peeled is the current reference |
| 693 | * (it usually is) and if so, returns that reference's peeled version |
| 694 | * if it is available. This avoids a refname lookup in a common case. |
| 695 | */ |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 696 | static struct ref_entry *current_ref; |
| 697 | |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 698 | typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data); |
| 699 | |
| 700 | struct ref_entry_cb { |
| 701 | const char *base; |
| 702 | int trim; |
| 703 | int flags; |
| 704 | each_ref_fn *fn; |
| 705 | void *cb_data; |
| 706 | }; |
| 707 | |
| Michael Haggerty | fcce170 | 2013-04-22 19:52:11 | [diff] [blame] | 708 | /* |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 709 | * Handle one reference in a do_for_each_ref*()-style iteration, |
| 710 | * calling an each_ref_fn for each entry. |
| Michael Haggerty | fcce170 | 2013-04-22 19:52:11 | [diff] [blame] | 711 | */ |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 712 | static int do_one_ref(struct ref_entry *entry, void *cb_data) |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 713 | { |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 714 | struct ref_entry_cb *data = cb_data; |
| Michael Haggerty | d0cf51e | 2013-07-15 15:24:17 | [diff] [blame] | 715 | struct ref_entry *old_current_ref; |
| Michael Haggerty | 429213e | 2012-04-10 05:30:14 | [diff] [blame] | 716 | int retval; |
| Michael Haggerty | d0cf51e | 2013-07-15 15:24:17 | [diff] [blame] | 717 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 718 | if (!starts_with(entry->name, data->base)) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 719 | return 0; |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 720 | |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 721 | if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) && |
| Michael Haggerty | 662428f | 2013-04-22 19:52:18 | [diff] [blame] | 722 | !ref_resolves_to_object(entry)) |
| 723 | return 0; |
| 724 | |
| Michael Haggerty | d0cf51e | 2013-07-15 15:24:17 | [diff] [blame] | 725 | /* Store the old value, in case this is a recursive call: */ |
| 726 | old_current_ref = current_ref; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 727 | current_ref = entry; |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 728 | retval = data->fn(entry->name + data->trim, entry->u.value.sha1, |
| 729 | entry->flag, data->cb_data); |
| Michael Haggerty | d0cf51e | 2013-07-15 15:24:17 | [diff] [blame] | 730 | current_ref = old_current_ref; |
| Michael Haggerty | 429213e | 2012-04-10 05:30:14 | [diff] [blame] | 731 | return retval; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 732 | } |
| Julian Phillips | c774aab | 2007-04-17 01:42:50 | [diff] [blame] | 733 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 734 | /* |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 735 | * Call fn for each reference in dir that has index in the range |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 736 | * offset <= index < dir->nr. Recurse into subdirectories that are in |
| 737 | * that index range, sorting them before iterating. This function |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 738 | * does not sort dir itself; it should be sorted beforehand. fn is |
| 739 | * called for all references, including broken ones. |
| Michael Haggerty | c36b5bc | 2012-04-10 05:30:15 | [diff] [blame] | 740 | */ |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 741 | static int do_for_each_entry_in_dir(struct ref_dir *dir, int offset, |
| 742 | each_ref_entry_fn fn, void *cb_data) |
| Michael Haggerty | c36b5bc | 2012-04-10 05:30:15 | [diff] [blame] | 743 | { |
| 744 | int i; |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 745 | assert(dir->sorted == dir->nr); |
| 746 | for (i = offset; i < dir->nr; i++) { |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 747 | struct ref_entry *entry = dir->entries[i]; |
| 748 | int retval; |
| 749 | if (entry->flag & REF_DIR) { |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 750 | struct ref_dir *subdir = get_ref_dir(entry); |
| 751 | sort_ref_dir(subdir); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 752 | retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 753 | } else { |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 754 | retval = fn(entry, cb_data); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 755 | } |
| Michael Haggerty | c36b5bc | 2012-04-10 05:30:15 | [diff] [blame] | 756 | if (retval) |
| 757 | return retval; |
| 758 | } |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | /* |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 763 | * Call fn for each reference in the union of dir1 and dir2, in order |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 764 | * by refname. Recurse into subdirectories. If a value entry appears |
| 765 | * in both dir1 and dir2, then only process the version that is in |
| 766 | * dir2. The input dirs must already be sorted, but subdirs will be |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 767 | * sorted as needed. fn is called for all references, including |
| 768 | * broken ones. |
| Michael Haggerty | b3fd060 | 2012-04-10 05:30:16 | [diff] [blame] | 769 | */ |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 770 | static int do_for_each_entry_in_dirs(struct ref_dir *dir1, |
| 771 | struct ref_dir *dir2, |
| 772 | each_ref_entry_fn fn, void *cb_data) |
| Michael Haggerty | b3fd060 | 2012-04-10 05:30:16 | [diff] [blame] | 773 | { |
| 774 | int retval; |
| 775 | int i1 = 0, i2 = 0; |
| 776 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 777 | assert(dir1->sorted == dir1->nr); |
| 778 | assert(dir2->sorted == dir2->nr); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 779 | while (1) { |
| 780 | struct ref_entry *e1, *e2; |
| 781 | int cmp; |
| 782 | if (i1 == dir1->nr) { |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 783 | return do_for_each_entry_in_dir(dir2, i2, fn, cb_data); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 784 | } |
| 785 | if (i2 == dir2->nr) { |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 786 | return do_for_each_entry_in_dir(dir1, i1, fn, cb_data); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 787 | } |
| 788 | e1 = dir1->entries[i1]; |
| 789 | e2 = dir2->entries[i2]; |
| 790 | cmp = strcmp(e1->name, e2->name); |
| 791 | if (cmp == 0) { |
| 792 | if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) { |
| 793 | /* Both are directories; descend them in parallel. */ |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 794 | struct ref_dir *subdir1 = get_ref_dir(e1); |
| 795 | struct ref_dir *subdir2 = get_ref_dir(e2); |
| 796 | sort_ref_dir(subdir1); |
| 797 | sort_ref_dir(subdir2); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 798 | retval = do_for_each_entry_in_dirs( |
| 799 | subdir1, subdir2, fn, cb_data); |
| Michael Haggerty | b3fd060 | 2012-04-10 05:30:16 | [diff] [blame] | 800 | i1++; |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 801 | i2++; |
| 802 | } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) { |
| 803 | /* Both are references; ignore the one from dir1. */ |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 804 | retval = fn(e2, cb_data); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 805 | i1++; |
| 806 | i2++; |
| 807 | } else { |
| 808 | die("conflict between reference and directory: %s", |
| 809 | e1->name); |
| 810 | } |
| 811 | } else { |
| 812 | struct ref_entry *e; |
| 813 | if (cmp < 0) { |
| 814 | e = e1; |
| 815 | i1++; |
| 816 | } else { |
| 817 | e = e2; |
| 818 | i2++; |
| 819 | } |
| 820 | if (e->flag & REF_DIR) { |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 821 | struct ref_dir *subdir = get_ref_dir(e); |
| 822 | sort_ref_dir(subdir); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 823 | retval = do_for_each_entry_in_dir( |
| 824 | subdir, 0, fn, cb_data); |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 825 | } else { |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 826 | retval = fn(e, cb_data); |
| Michael Haggerty | b3fd060 | 2012-04-10 05:30:16 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | if (retval) |
| 830 | return retval; |
| 831 | } |
| Michael Haggerty | b3fd060 | 2012-04-10 05:30:16 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
| Jeff King | 98eeb09 | 2013-06-20 08:37:53 | [diff] [blame] | 835 | * Load all of the refs from the dir into our in-memory cache. The hard work |
| 836 | * of loading loose refs is done by get_ref_dir(), so we just need to recurse |
| 837 | * through all of the sub-directories. We do not even need to care about |
| 838 | * sorting, as traversal order does not matter to us. |
| 839 | */ |
| 840 | static void prime_ref_dir(struct ref_dir *dir) |
| 841 | { |
| 842 | int i; |
| 843 | for (i = 0; i < dir->nr; i++) { |
| 844 | struct ref_entry *entry = dir->entries[i]; |
| 845 | if (entry->flag & REF_DIR) |
| 846 | prime_ref_dir(get_ref_dir(entry)); |
| 847 | } |
| 848 | } |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 849 | |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 850 | struct nonmatching_ref_data { |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 851 | const struct string_list *skip; |
| Michael Haggerty | 521331c | 2015-05-11 15:25:09 | [diff] [blame] | 852 | const char *conflicting_refname; |
| Michael Haggerty | 5a4d494 | 2012-04-10 05:30:19 | [diff] [blame] | 853 | }; |
| 854 | |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 855 | static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata) |
| Michael Haggerty | 5a4d494 | 2012-04-10 05:30:19 | [diff] [blame] | 856 | { |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 857 | struct nonmatching_ref_data *data = vdata; |
| 858 | |
| Michael Haggerty | 8bfac19 | 2015-05-11 15:25:07 | [diff] [blame] | 859 | if (data->skip && string_list_has_string(data->skip, entry->name)) |
| Michael Haggerty | 5a4d494 | 2012-04-10 05:30:19 | [diff] [blame] | 860 | return 0; |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 861 | |
| Michael Haggerty | 521331c | 2015-05-11 15:25:09 | [diff] [blame] | 862 | data->conflicting_refname = entry->name; |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 863 | return 1; |
| 864 | } |
| 865 | |
| Michael Haggerty | d66da47 | 2012-04-10 05:30:17 | [diff] [blame] | 866 | /* |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 867 | * Return 0 if a reference named refname could be created without |
| 868 | * conflicting with the name of an existing reference in dir. |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 869 | * Otherwise, return a negative value and write an explanation to err. |
| 870 | * If extras is non-NULL, it is a list of additional refnames with |
| 871 | * which refname is not allowed to conflict. If skip is non-NULL, |
| 872 | * ignore potential conflicts with refs in skip (e.g., because they |
| 873 | * are scheduled for deletion in the same operation). Behavior is |
| 874 | * undefined if the same name is listed in both extras and skip. |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 875 | * |
| 876 | * Two reference names conflict if one of them exactly matches the |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 877 | * leading components of the other; e.g., "refs/foo/bar" conflicts |
| 878 | * with both "refs/foo" and with "refs/foo/bar/baz" but not with |
| 879 | * "refs/foo/bar" or "refs/foo/barbados". |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 880 | * |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 881 | * extras and skip must be sorted. |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 882 | */ |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 883 | static int verify_refname_available(const char *refname, |
| 884 | const struct string_list *extras, |
| 885 | const struct string_list *skip, |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 886 | struct ref_dir *dir, |
| 887 | struct strbuf *err) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 888 | { |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 889 | const char *slash; |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 890 | int pos; |
| Michael Haggerty | 6075f30 | 2015-05-11 15:25:06 | [diff] [blame] | 891 | struct strbuf dirname = STRBUF_INIT; |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 892 | int ret = -1; |
| Michael Haggerty | 5a4d494 | 2012-04-10 05:30:19 | [diff] [blame] | 893 | |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 894 | /* |
| 895 | * For the sake of comments in this function, suppose that |
| 896 | * refname is "refs/foo/bar". |
| 897 | */ |
| 898 | |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 899 | assert(err); |
| 900 | |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 901 | strbuf_grow(&dirname, strlen(refname) + 1); |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 902 | for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) { |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 903 | /* Expand dirname to the new prefix, not including the trailing slash: */ |
| 904 | strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len); |
| 905 | |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 906 | /* |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 907 | * We are still at a leading dir of the refname (e.g., |
| 908 | * "refs/foo"; if there is a reference with that name, |
| 909 | * it is a conflict, *unless* it is in skip. |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 910 | */ |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 911 | if (dir) { |
| 912 | pos = search_ref_dir(dir, dirname.buf, dirname.len); |
| 913 | if (pos >= 0 && |
| 914 | (!skip || !string_list_has_string(skip, dirname.buf))) { |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 915 | /* |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 916 | * We found a reference whose name is |
| 917 | * a proper prefix of refname; e.g., |
| 918 | * "refs/foo", and is not in skip. |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 919 | */ |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 920 | strbuf_addf(err, "'%s' exists; cannot create '%s'", |
| 921 | dirname.buf, refname); |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 922 | goto cleanup; |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 923 | } |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 924 | } |
| 925 | |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 926 | if (extras && string_list_has_string(extras, dirname.buf) && |
| 927 | (!skip || !string_list_has_string(skip, dirname.buf))) { |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 928 | strbuf_addf(err, "cannot process '%s' and '%s' at the same time", |
| 929 | refname, dirname.buf); |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 930 | goto cleanup; |
| 931 | } |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 932 | |
| 933 | /* |
| 934 | * Otherwise, we can try to continue our search with |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 935 | * the next component. So try to look up the |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 936 | * directory, e.g., "refs/foo/". If we come up empty, |
| 937 | * we know there is nothing under this whole prefix, |
| 938 | * but even in that case we still have to continue the |
| 939 | * search for conflicts with extras. |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 940 | */ |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 941 | strbuf_addch(&dirname, '/'); |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 942 | if (dir) { |
| 943 | pos = search_ref_dir(dir, dirname.buf, dirname.len); |
| 944 | if (pos < 0) { |
| 945 | /* |
| 946 | * There was no directory "refs/foo/", |
| 947 | * so there is nothing under this |
| 948 | * whole prefix. So there is no need |
| 949 | * to continue looking for conflicting |
| 950 | * references. But we need to continue |
| 951 | * looking for conflicting extras. |
| 952 | */ |
| 953 | dir = NULL; |
| 954 | } else { |
| 955 | dir = get_ref_dir(dir->entries[pos]); |
| 956 | } |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 957 | } |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| Michael Haggerty | 49e8187 | 2015-05-11 15:25:04 | [diff] [blame] | 961 | * We are at the leaf of our refname (e.g., "refs/foo/bar"). |
| 962 | * There is no point in searching for a reference with that |
| 963 | * name, because a refname isn't considered to conflict with |
| 964 | * itself. But we still need to check for references whose |
| 965 | * names are in the "refs/foo/bar/" namespace, because they |
| 966 | * *do* conflict. |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 967 | */ |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 968 | strbuf_addstr(&dirname, refname + dirname.len); |
| Michael Haggerty | 6075f30 | 2015-05-11 15:25:06 | [diff] [blame] | 969 | strbuf_addch(&dirname, '/'); |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 970 | |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 971 | if (dir) { |
| 972 | pos = search_ref_dir(dir, dirname.buf, dirname.len); |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 973 | |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 974 | if (pos >= 0) { |
| 975 | /* |
| 976 | * We found a directory named "$refname/" |
| 977 | * (e.g., "refs/foo/bar/"). It is a problem |
| 978 | * iff it contains any ref that is not in |
| 979 | * "skip". |
| 980 | */ |
| 981 | struct nonmatching_ref_data data; |
| 982 | |
| 983 | data.skip = skip; |
| 984 | data.conflicting_refname = NULL; |
| 985 | dir = get_ref_dir(dir->entries[pos]); |
| 986 | sort_ref_dir(dir); |
| 987 | if (do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) { |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 988 | strbuf_addf(err, "'%s' exists; cannot create '%s'", |
| 989 | data.conflicting_refname, refname); |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 990 | goto cleanup; |
| 991 | } |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 992 | } |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 993 | } |
| Jeff King | cbe7333 | 2014-09-10 11:11:55 | [diff] [blame] | 994 | |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 995 | if (extras) { |
| 996 | /* |
| 997 | * Check for entries in extras that start with |
| 998 | * "$refname/". We do that by looking for the place |
| 999 | * where "$refname/" would be inserted in extras. If |
| 1000 | * there is an entry at that position that starts with |
| 1001 | * "$refname/" and is not in skip, then we have a |
| 1002 | * conflict. |
| 1003 | */ |
| 1004 | for (pos = string_list_find_insert_index(extras, dirname.buf, 0); |
| 1005 | pos < extras->nr; pos++) { |
| 1006 | const char *extra_refname = extras->items[pos].string; |
| 1007 | |
| 1008 | if (!starts_with(extra_refname, dirname.buf)) |
| 1009 | break; |
| 1010 | |
| 1011 | if (!skip || !string_list_has_string(skip, extra_refname)) { |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 1012 | strbuf_addf(err, "cannot process '%s' and '%s' at the same time", |
| 1013 | refname, extra_refname); |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 1014 | goto cleanup; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | /* No conflicts were found */ |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 1020 | ret = 0; |
| Michael Haggerty | 61da596 | 2015-05-11 15:25:10 | [diff] [blame] | 1021 | |
| 1022 | cleanup: |
| 1023 | strbuf_release(&dirname); |
| 1024 | return ret; |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1025 | } |
| 1026 | |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1027 | struct packed_ref_cache { |
| 1028 | struct ref_entry *root; |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 1029 | |
| 1030 | /* |
| Michael Haggerty | 5f5e2a8 | 2013-06-20 08:37:47 | [diff] [blame] | 1031 | * Count of references to the data structure in this instance, |
| 1032 | * including the pointer from ref_cache::packed if any. The |
| 1033 | * data will not be freed as long as the reference count is |
| 1034 | * nonzero. |
| 1035 | */ |
| 1036 | unsigned int referrers; |
| 1037 | |
| 1038 | /* |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 1039 | * Iff the packed-refs file associated with this instance is |
| 1040 | * currently locked for writing, this points at the associated |
| Michael Haggerty | 4f6b83e | 2013-06-20 08:37:49 | [diff] [blame] | 1041 | * lock (which is owned by somebody else). The referrer count |
| 1042 | * is also incremented when the file is locked and decremented |
| 1043 | * when it is unlocked. |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 1044 | */ |
| 1045 | struct lock_file *lock; |
| Jeff King | ca91993 | 2013-06-20 08:37:52 | [diff] [blame] | 1046 | |
| 1047 | /* The metadata from when this packed-refs cache was read */ |
| 1048 | struct stat_validity validity; |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1049 | }; |
| 1050 | |
| Junio C Hamano | 5e290ff | 2006-09-30 19:37:37 | [diff] [blame] | 1051 | /* |
| 1052 | * Future: need to be in "struct repository" |
| 1053 | * when doing a full libification. |
| 1054 | */ |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1055 | static struct ref_cache { |
| 1056 | struct ref_cache *next; |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1057 | struct ref_entry *loose; |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1058 | struct packed_ref_cache *packed; |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 1059 | /* |
| 1060 | * The submodule name, or "" for the main repo. We allocate |
| 1061 | * length 1 rather than FLEX_ARRAY so that the main ref_cache |
| 1062 | * is initialized correctly. |
| 1063 | */ |
| 1064 | char name[1]; |
| 1065 | } ref_cache, *submodule_ref_caches; |
| Michael Haggerty | 0e88c13 | 2011-08-12 22:36:29 | [diff] [blame] | 1066 | |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 1067 | /* Lock used for the main packed-refs file: */ |
| 1068 | static struct lock_file packlock; |
| 1069 | |
| Michael Haggerty | 5f5e2a8 | 2013-06-20 08:37:47 | [diff] [blame] | 1070 | /* |
| 1071 | * Increment the reference count of *packed_refs. |
| 1072 | */ |
| 1073 | static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs) |
| 1074 | { |
| 1075 | packed_refs->referrers++; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * Decrease the reference count of *packed_refs. If it goes to zero, |
| 1080 | * free *packed_refs and return true; otherwise return false. |
| 1081 | */ |
| 1082 | static int release_packed_ref_cache(struct packed_ref_cache *packed_refs) |
| 1083 | { |
| 1084 | if (!--packed_refs->referrers) { |
| 1085 | free_ref_entry(packed_refs->root); |
| Jeff King | ca91993 | 2013-06-20 08:37:52 | [diff] [blame] | 1086 | stat_validity_clear(&packed_refs->validity); |
| Michael Haggerty | 5f5e2a8 | 2013-06-20 08:37:47 | [diff] [blame] | 1087 | free(packed_refs); |
| 1088 | return 1; |
| 1089 | } else { |
| 1090 | return 0; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| Michael Haggerty | 760c451 | 2011-10-17 02:38:09 | [diff] [blame] | 1094 | static void clear_packed_ref_cache(struct ref_cache *refs) |
| Junio C Hamano | 5e290ff | 2006-09-30 19:37:37 | [diff] [blame] | 1095 | { |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1096 | if (refs->packed) { |
| Michael Haggerty | 5f5e2a8 | 2013-06-20 08:37:47 | [diff] [blame] | 1097 | struct packed_ref_cache *packed_refs = refs->packed; |
| 1098 | |
| 1099 | if (packed_refs->lock) |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 1100 | die("internal error: packed-ref cache cleared while locked"); |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1101 | refs->packed = NULL; |
| Michael Haggerty | 5f5e2a8 | 2013-06-20 08:37:47 | [diff] [blame] | 1102 | release_packed_ref_cache(packed_refs); |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1103 | } |
| Junio C Hamano | 5e290ff | 2006-09-30 19:37:37 | [diff] [blame] | 1104 | } |
| 1105 | |
| Michael Haggerty | 760c451 | 2011-10-17 02:38:09 | [diff] [blame] | 1106 | static void clear_loose_ref_cache(struct ref_cache *refs) |
| Junio C Hamano | 5e290ff | 2006-09-30 19:37:37 | [diff] [blame] | 1107 | { |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1108 | if (refs->loose) { |
| 1109 | free_ref_entry(refs->loose); |
| 1110 | refs->loose = NULL; |
| 1111 | } |
| Michael Haggerty | 760c451 | 2011-10-17 02:38:09 | [diff] [blame] | 1112 | } |
| 1113 | |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1114 | static struct ref_cache *create_ref_cache(const char *submodule) |
| Michael Haggerty | e5dbf60 | 2011-08-12 22:36:27 | [diff] [blame] | 1115 | { |
| Michael Haggerty | ce40979 | 2011-08-12 22:36:28 | [diff] [blame] | 1116 | int len; |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1117 | struct ref_cache *refs; |
| Michael Haggerty | ce40979 | 2011-08-12 22:36:28 | [diff] [blame] | 1118 | if (!submodule) |
| 1119 | submodule = ""; |
| 1120 | len = strlen(submodule) + 1; |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1121 | refs = xcalloc(1, sizeof(struct ref_cache) + len); |
| Michael Haggerty | ce40979 | 2011-08-12 22:36:28 | [diff] [blame] | 1122 | memcpy(refs->name, submodule, len); |
| Michael Haggerty | e5dbf60 | 2011-08-12 22:36:27 | [diff] [blame] | 1123 | return refs; |
| 1124 | } |
| 1125 | |
| Michael Haggerty | 4349a66 | 2011-08-12 22:36:25 | [diff] [blame] | 1126 | /* |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1127 | * Return a pointer to a ref_cache for the specified submodule. For |
| Michael Haggerty | 4349a66 | 2011-08-12 22:36:25 | [diff] [blame] | 1128 | * the main repository, use submodule==NULL. The returned structure |
| 1129 | * will be allocated and initialized but not necessarily populated; it |
| 1130 | * should not be freed. |
| 1131 | */ |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1132 | static struct ref_cache *get_ref_cache(const char *submodule) |
| Michael Haggerty | 4349a66 | 2011-08-12 22:36:25 | [diff] [blame] | 1133 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 1134 | struct ref_cache *refs; |
| 1135 | |
| 1136 | if (!submodule || !*submodule) |
| 1137 | return &ref_cache; |
| 1138 | |
| 1139 | for (refs = submodule_ref_caches; refs; refs = refs->next) |
| Michael Haggerty | 0e88c13 | 2011-08-12 22:36:29 | [diff] [blame] | 1140 | if (!strcmp(submodule, refs->name)) |
| 1141 | return refs; |
| Michael Haggerty | 0e88c13 | 2011-08-12 22:36:29 | [diff] [blame] | 1142 | |
| Michael Haggerty | 79c7ca5 | 2011-10-17 02:38:05 | [diff] [blame] | 1143 | refs = create_ref_cache(submodule); |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 1144 | refs->next = submodule_ref_caches; |
| 1145 | submodule_ref_caches = refs; |
| Michael Haggerty | 0e88c13 | 2011-08-12 22:36:29 | [diff] [blame] | 1146 | return refs; |
| Michael Haggerty | 4349a66 | 2011-08-12 22:36:25 | [diff] [blame] | 1147 | } |
| 1148 | |
| Michael Haggerty | 3feb4f0 | 2013-04-22 19:52:13 | [diff] [blame] | 1149 | /* The length of a peeled reference line in packed-refs, including EOL: */ |
| 1150 | #define PEELED_LINE_LENGTH 42 |
| 1151 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1152 | /* |
| Michael Haggerty | 694b7a1 | 2013-04-22 19:52:29 | [diff] [blame] | 1153 | * The packed-refs header line that we write out. Perhaps other |
| 1154 | * traits will be added later. The trailing space is required. |
| 1155 | */ |
| 1156 | static const char PACKED_REFS_HEADER[] = |
| 1157 | "# pack-refs with: peeled fully-peeled \n"; |
| 1158 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1159 | /* |
| 1160 | * Parse one line from a packed-refs file. Write the SHA1 to sha1. |
| 1161 | * Return a pointer to the refname within the line (null-terminated), |
| 1162 | * or NULL if there was a problem. |
| 1163 | */ |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1164 | static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1165 | { |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1166 | const char *ref; |
| 1167 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1168 | /* |
| 1169 | * 42: the answer to everything. |
| 1170 | * |
| 1171 | * In this case, it happens to be the answer to |
| 1172 | * 40 (length of sha1 hex representation) |
| 1173 | * +1 (space in between hex and name) |
| 1174 | * +1 (newline at the end of the line) |
| 1175 | */ |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1176 | if (line->len <= 42) |
| 1177 | return NULL; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1178 | |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1179 | if (get_sha1_hex(line->buf, sha1) < 0) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1180 | return NULL; |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1181 | if (!isspace(line->buf[40])) |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1182 | return NULL; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1183 | |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1184 | ref = line->buf + 41; |
| 1185 | if (isspace(*ref)) |
| 1186 | return NULL; |
| 1187 | |
| 1188 | if (line->buf[line->len - 1] != '\n') |
| 1189 | return NULL; |
| 1190 | line->buf[--line->len] = 0; |
| 1191 | |
| 1192 | return ref; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1193 | } |
| 1194 | |
| Michael Haggerty | c29c46f | 2013-03-18 11:37:32 | [diff] [blame] | 1195 | /* |
| 1196 | * Read f, which is a packed-refs file, into dir. |
| 1197 | * |
| 1198 | * A comment line of the form "# pack-refs with: " may contain zero or |
| 1199 | * more traits. We interpret the traits as follows: |
| 1200 | * |
| 1201 | * No traits: |
| 1202 | * |
| 1203 | * Probably no references are peeled. But if the file contains a |
| 1204 | * peeled value for a reference, we will use it. |
| 1205 | * |
| 1206 | * peeled: |
| 1207 | * |
| 1208 | * References under "refs/tags/", if they *can* be peeled, *are* |
| 1209 | * peeled in this file. References outside of "refs/tags/" are |
| 1210 | * probably not peeled even if they could have been, but if we find |
| 1211 | * a peeled value for such a reference we will use it. |
| 1212 | * |
| 1213 | * fully-peeled: |
| 1214 | * |
| 1215 | * All references in the file that can be peeled are peeled. |
| 1216 | * Inversely (and this is more important), any references in the |
| 1217 | * file for which no peeled value is recorded is not peelable. This |
| 1218 | * trait should typically be written alongside "peeled" for |
| 1219 | * compatibility with older clients, but we do not require it |
| 1220 | * (i.e., "peeled" is a no-op if "fully-peeled" is set). |
| 1221 | */ |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 1222 | static void read_packed_refs(FILE *f, struct ref_dir *dir) |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1223 | { |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 1224 | struct ref_entry *last = NULL; |
| Jeff King | 10c497a | 2014-12-10 10:40:07 | [diff] [blame] | 1225 | struct strbuf line = STRBUF_INIT; |
| Michael Haggerty | c29c46f | 2013-03-18 11:37:32 | [diff] [blame] | 1226 | enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1227 | |
| Jeff King | 10c497a | 2014-12-10 10:40:07 | [diff] [blame] | 1228 | while (strbuf_getwholeline(&line, f, '\n') != EOF) { |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1229 | unsigned char sha1[20]; |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1230 | const char *refname; |
| Jeff King | ea41783 | 2014-12-10 10:40:36 | [diff] [blame] | 1231 | const char *traits; |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1232 | |
| Jeff King | ea41783 | 2014-12-10 10:40:36 | [diff] [blame] | 1233 | if (skip_prefix(line.buf, "# pack-refs with:", &traits)) { |
| Michael Haggerty | c29c46f | 2013-03-18 11:37:32 | [diff] [blame] | 1234 | if (strstr(traits, " fully-peeled ")) |
| 1235 | peeled = PEELED_FULLY; |
| 1236 | else if (strstr(traits, " peeled ")) |
| 1237 | peeled = PEELED_TAGS; |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1238 | /* perhaps other traits later as well */ |
| 1239 | continue; |
| 1240 | } |
| 1241 | |
| Jeff King | 6a49870 | 2014-12-10 10:40:19 | [diff] [blame] | 1242 | refname = parse_ref_line(&line, sha1); |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1243 | if (refname) { |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1244 | int flag = REF_ISPACKED; |
| 1245 | |
| 1246 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
| 1247 | hashclr(sha1); |
| 1248 | flag |= REF_BAD_NAME | REF_ISBROKEN; |
| 1249 | } |
| 1250 | last = create_ref_entry(refname, sha1, flag, 0); |
| Michael Haggerty | c29c46f | 2013-03-18 11:37:32 | [diff] [blame] | 1251 | if (peeled == PEELED_FULLY || |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1252 | (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/"))) |
| Michael Haggerty | c29c46f | 2013-03-18 11:37:32 | [diff] [blame] | 1253 | last->flag |= REF_KNOWS_PEELED; |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 1254 | add_ref(dir, last); |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1255 | continue; |
| 1256 | } |
| 1257 | if (last && |
| Jeff King | 10c497a | 2014-12-10 10:40:07 | [diff] [blame] | 1258 | line.buf[0] == '^' && |
| 1259 | line.len == PEELED_LINE_LENGTH && |
| 1260 | line.buf[PEELED_LINE_LENGTH - 1] == '\n' && |
| 1261 | !get_sha1_hex(line.buf + 1, sha1)) { |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 1262 | hashcpy(last->u.value.peeled, sha1); |
| Michael Haggerty | c29c46f | 2013-03-18 11:37:32 | [diff] [blame] | 1263 | /* |
| 1264 | * Regardless of what the file header said, |
| 1265 | * we definitely know the value of *this* |
| 1266 | * reference: |
| 1267 | */ |
| 1268 | last->flag |= REF_KNOWS_PEELED; |
| 1269 | } |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1270 | } |
| Jeff King | 10c497a | 2014-12-10 10:40:07 | [diff] [blame] | 1271 | |
| 1272 | strbuf_release(&line); |
| Junio C Hamano | f4204ab | 2006-11-22 07:36:35 | [diff] [blame] | 1273 | } |
| 1274 | |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1275 | /* |
| 1276 | * Get the packed_ref_cache for the specified ref_cache, creating it |
| 1277 | * if necessary. |
| 1278 | */ |
| 1279 | static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs) |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1280 | { |
| Jeff King | ca91993 | 2013-06-20 08:37:52 | [diff] [blame] | 1281 | const char *packed_refs_file; |
| 1282 | |
| 1283 | if (*refs->name) |
| 1284 | packed_refs_file = git_path_submodule(refs->name, "packed-refs"); |
| 1285 | else |
| 1286 | packed_refs_file = git_path("packed-refs"); |
| 1287 | |
| 1288 | if (refs->packed && |
| 1289 | !stat_validity_check(&refs->packed->validity, packed_refs_file)) |
| 1290 | clear_packed_ref_cache(refs); |
| 1291 | |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1292 | if (!refs->packed) { |
| Michael Haggerty | 4349a66 | 2011-08-12 22:36:25 | [diff] [blame] | 1293 | FILE *f; |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1294 | |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1295 | refs->packed = xcalloc(1, sizeof(*refs->packed)); |
| Michael Haggerty | 5f5e2a8 | 2013-06-20 08:37:47 | [diff] [blame] | 1296 | acquire_packed_ref_cache(refs->packed); |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1297 | refs->packed->root = create_dir_entry(refs, "", 0, 0); |
| Michael Haggerty | 4349a66 | 2011-08-12 22:36:25 | [diff] [blame] | 1298 | f = fopen(packed_refs_file, "r"); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1299 | if (f) { |
| Jeff King | ca91993 | 2013-06-20 08:37:52 | [diff] [blame] | 1300 | stat_validity_update(&refs->packed->validity, fileno(f)); |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1301 | read_packed_refs(f, get_ref_dir(refs->packed->root)); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1302 | fclose(f); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1303 | } |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1304 | } |
| Michael Haggerty | 2fff781 | 2013-06-20 08:37:45 | [diff] [blame] | 1305 | return refs->packed; |
| 1306 | } |
| 1307 | |
| 1308 | static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) |
| 1309 | { |
| 1310 | return get_ref_dir(packed_ref_cache->root); |
| 1311 | } |
| 1312 | |
| 1313 | static struct ref_dir *get_packed_refs(struct ref_cache *refs) |
| 1314 | { |
| 1315 | return get_packed_ref_dir(get_packed_ref_cache(refs)); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1316 | } |
| 1317 | |
| Michael Haggerty | 30249ee | 2012-01-17 05:50:33 | [diff] [blame] | 1318 | void add_packed_ref(const char *refname, const unsigned char *sha1) |
| 1319 | { |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 1320 | struct packed_ref_cache *packed_ref_cache = |
| 1321 | get_packed_ref_cache(&ref_cache); |
| 1322 | |
| 1323 | if (!packed_ref_cache->lock) |
| 1324 | die("internal error: packed refs not locked"); |
| 1325 | add_ref(get_packed_ref_dir(packed_ref_cache), |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 1326 | create_ref_entry(refname, sha1, REF_ISPACKED, 1)); |
| Michael Haggerty | 30249ee | 2012-01-17 05:50:33 | [diff] [blame] | 1327 | } |
| 1328 | |
| Michael Haggerty | abc3909 | 2012-04-24 22:45:10 | [diff] [blame] | 1329 | /* |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 1330 | * Read the loose references from the namespace dirname into dir |
| 1331 | * (without recursing). dirname must end with '/'. dir must be the |
| 1332 | * directory entry corresponding to dirname. |
| Michael Haggerty | abc3909 | 2012-04-24 22:45:10 | [diff] [blame] | 1333 | */ |
| Michael Haggerty | 423a1af | 2012-04-26 22:27:06 | [diff] [blame] | 1334 | static void read_loose_refs(const char *dirname, struct ref_dir *dir) |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1335 | { |
| Michael Haggerty | 423a1af | 2012-04-26 22:27:06 | [diff] [blame] | 1336 | struct ref_cache *refs = dir->ref_cache; |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 1337 | DIR *d; |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1338 | const char *path; |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1339 | struct dirent *de; |
| Michael Haggerty | abc3909 | 2012-04-24 22:45:10 | [diff] [blame] | 1340 | int dirnamelen = strlen(dirname); |
| Michael Haggerty | 72b64b4 | 2012-04-24 22:45:08 | [diff] [blame] | 1341 | struct strbuf refname; |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1342 | |
| Michael Haggerty | 3b12482 | 2011-12-12 05:38:17 | [diff] [blame] | 1343 | if (*refs->name) |
| Michael Haggerty | 66a3d20 | 2012-04-24 22:45:09 | [diff] [blame] | 1344 | path = git_path_submodule(refs->name, "%s", dirname); |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1345 | else |
| Michael Haggerty | 66a3d20 | 2012-04-24 22:45:09 | [diff] [blame] | 1346 | path = git_path("%s", dirname); |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1347 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 1348 | d = opendir(path); |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1349 | if (!d) |
| 1350 | return; |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1351 | |
| Michael Haggerty | 66a3d20 | 2012-04-24 22:45:09 | [diff] [blame] | 1352 | strbuf_init(&refname, dirnamelen + 257); |
| 1353 | strbuf_add(&refname, dirname, dirnamelen); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1354 | |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1355 | while ((de = readdir(d)) != NULL) { |
| 1356 | unsigned char sha1[20]; |
| 1357 | struct stat st; |
| 1358 | int flag; |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1359 | const char *refdir; |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1360 | |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1361 | if (de->d_name[0] == '.') |
| 1362 | continue; |
| Jeff King | 2975c77 | 2014-06-30 16:58:25 | [diff] [blame] | 1363 | if (ends_with(de->d_name, ".lock")) |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1364 | continue; |
| Michael Haggerty | 72b64b4 | 2012-04-24 22:45:08 | [diff] [blame] | 1365 | strbuf_addstr(&refname, de->d_name); |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1366 | refdir = *refs->name |
| Michael Haggerty | 72b64b4 | 2012-04-24 22:45:08 | [diff] [blame] | 1367 | ? git_path_submodule(refs->name, "%s", refname.buf) |
| 1368 | : git_path("%s", refname.buf); |
| 1369 | if (stat(refdir, &st) < 0) { |
| 1370 | ; /* silently ignore */ |
| 1371 | } else if (S_ISDIR(st.st_mode)) { |
| Michael Haggerty | abc3909 | 2012-04-24 22:45:10 | [diff] [blame] | 1372 | strbuf_addch(&refname, '/'); |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 1373 | add_entry_to_dir(dir, |
| René Scharfe | b9146f5 | 2012-05-22 18:50:52 | [diff] [blame] | 1374 | create_dir_entry(refs, refname.buf, |
| 1375 | refname.len, 1)); |
| Michael Haggerty | 72b64b4 | 2012-04-24 22:45:08 | [diff] [blame] | 1376 | } else { |
| Michael Haggerty | 3b12482 | 2011-12-12 05:38:17 | [diff] [blame] | 1377 | if (*refs->name) { |
| Junio C Hamano | f8948e2 | 2009-02-09 07:27:10 | [diff] [blame] | 1378 | hashclr(sha1); |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1379 | flag = 0; |
| Michael Haggerty | 72b64b4 | 2012-04-24 22:45:08 | [diff] [blame] | 1380 | if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) { |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1381 | hashclr(sha1); |
| Junio C Hamano | 98ac34b | 2011-10-19 20:45:50 | [diff] [blame] | 1382 | flag |= REF_ISBROKEN; |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 1383 | } |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1384 | } else if (read_ref_full(refname.buf, |
| 1385 | RESOLVE_REF_READING, |
| 1386 | sha1, &flag)) { |
| Junio C Hamano | 09116a1 | 2011-11-17 00:54:32 | [diff] [blame] | 1387 | hashclr(sha1); |
| 1388 | flag |= REF_ISBROKEN; |
| 1389 | } |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1390 | if (check_refname_format(refname.buf, |
| 1391 | REFNAME_ALLOW_ONELEVEL)) { |
| 1392 | hashclr(sha1); |
| 1393 | flag |= REF_BAD_NAME | REF_ISBROKEN; |
| 1394 | } |
| Michael Haggerty | 9f2fb4a | 2012-04-24 22:45:12 | [diff] [blame] | 1395 | add_entry_to_dir(dir, |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1396 | create_ref_entry(refname.buf, sha1, flag, 0)); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1397 | } |
| Michael Haggerty | 66a3d20 | 2012-04-24 22:45:09 | [diff] [blame] | 1398 | strbuf_setlen(&refname, dirnamelen); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1399 | } |
| Michael Haggerty | 72b64b4 | 2012-04-24 22:45:08 | [diff] [blame] | 1400 | strbuf_release(&refname); |
| Michael Haggerty | d5fdae6 | 2012-04-24 22:45:07 | [diff] [blame] | 1401 | closedir(d); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1402 | } |
| 1403 | |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 1404 | static struct ref_dir *get_loose_refs(struct ref_cache *refs) |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1405 | { |
| Michael Haggerty | d12229f | 2012-04-26 22:27:01 | [diff] [blame] | 1406 | if (!refs->loose) { |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 1407 | /* |
| 1408 | * Mark the top-level directory complete because we |
| 1409 | * are about to read the only subdirectory that can |
| 1410 | * hold references: |
| 1411 | */ |
| René Scharfe | b9146f5 | 2012-05-22 18:50:52 | [diff] [blame] | 1412 | refs->loose = create_dir_entry(refs, "", 0, 0); |
| Michael Haggerty | 28e6a34 | 2012-04-26 22:27:07 | [diff] [blame] | 1413 | /* |
| 1414 | * Create an incomplete entry for "refs/": |
| 1415 | */ |
| 1416 | add_entry_to_dir(get_ref_dir(refs->loose), |
| René Scharfe | b9146f5 | 2012-05-22 18:50:52 | [diff] [blame] | 1417 | create_dir_entry(refs, "refs/", 5, 1)); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1418 | } |
| Michael Haggerty | d7826d5 | 2012-04-26 22:27:03 | [diff] [blame] | 1419 | return get_ref_dir(refs->loose); |
| Linus Torvalds | e1e22e3 | 2006-09-11 23:37:32 | [diff] [blame] | 1420 | } |
| 1421 | |
| Linus Torvalds | ca8db14 | 2005-09-25 16:59:37 | [diff] [blame] | 1422 | /* We allow "recursive" symbolic refs. Only within reason, though */ |
| 1423 | #define MAXDEPTH 5 |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1424 | #define MAXREFLEN (1024) |
| 1425 | |
| Junio C Hamano | e5fa45c | 2011-10-17 18:43:30 | [diff] [blame] | 1426 | /* |
| 1427 | * Called by resolve_gitlink_ref_recursive() after it failed to read |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1428 | * from the loose refs in ref_cache refs. Find <refname> in the |
| 1429 | * packed-refs file for the submodule. |
| Junio C Hamano | e5fa45c | 2011-10-17 18:43:30 | [diff] [blame] | 1430 | */ |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1431 | static int resolve_gitlink_packed_ref(struct ref_cache *refs, |
| Michael Haggerty | 85be1fe | 2011-12-12 05:38:10 | [diff] [blame] | 1432 | const char *refname, unsigned char *sha1) |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1433 | { |
| Junio C Hamano | 2c5c66b | 2011-10-10 22:56:19 | [diff] [blame] | 1434 | struct ref_entry *ref; |
| Michael Haggerty | d317727 | 2012-04-10 05:30:24 | [diff] [blame] | 1435 | struct ref_dir *dir = get_packed_refs(refs); |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1436 | |
| Michael Haggerty | 432ad41 | 2012-04-10 05:30:26 | [diff] [blame] | 1437 | ref = find_ref(dir, refname); |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1438 | if (ref == NULL) |
| 1439 | return -1; |
| 1440 | |
| Sun He | 50546b1 | 2014-03-03 09:39:59 | [diff] [blame] | 1441 | hashcpy(sha1, ref->u.value.sha1); |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1442 | return 0; |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1443 | } |
| 1444 | |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1445 | static int resolve_gitlink_ref_recursive(struct ref_cache *refs, |
| Michael Haggerty | 85be1fe | 2011-12-12 05:38:10 | [diff] [blame] | 1446 | const char *refname, unsigned char *sha1, |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1447 | int recursion) |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1448 | { |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1449 | int fd, len; |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1450 | char buffer[128], *p; |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1451 | char *path; |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1452 | |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1453 | if (recursion > MAXDEPTH || strlen(refname) > MAXREFLEN) |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1454 | return -1; |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1455 | path = *refs->name |
| 1456 | ? git_path_submodule(refs->name, "%s", refname) |
| 1457 | : git_path("%s", refname); |
| 1458 | fd = open(path, O_RDONLY); |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1459 | if (fd < 0) |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1460 | return resolve_gitlink_packed_ref(refs, refname, sha1); |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1461 | |
| 1462 | len = read(fd, buffer, sizeof(buffer)-1); |
| 1463 | close(fd); |
| 1464 | if (len < 0) |
| 1465 | return -1; |
| 1466 | while (len && isspace(buffer[len-1])) |
| 1467 | len--; |
| 1468 | buffer[len] = 0; |
| 1469 | |
| 1470 | /* Was it a detached head or an old-fashioned symlink? */ |
| Michael Haggerty | 85be1fe | 2011-12-12 05:38:10 | [diff] [blame] | 1471 | if (!get_sha1_hex(buffer, sha1)) |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1472 | return 0; |
| 1473 | |
| 1474 | /* Symref? */ |
| 1475 | if (strncmp(buffer, "ref:", 4)) |
| 1476 | return -1; |
| 1477 | p = buffer + 4; |
| 1478 | while (isspace(*p)) |
| 1479 | p++; |
| 1480 | |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1481 | return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1); |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1482 | } |
| 1483 | |
| Michael Haggerty | 85be1fe | 2011-12-12 05:38:10 | [diff] [blame] | 1484 | int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1) |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1485 | { |
| 1486 | int len = strlen(path), retval; |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1487 | char *submodule; |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1488 | struct ref_cache *refs; |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1489 | |
| 1490 | while (len && path[len-1] == '/') |
| 1491 | len--; |
| 1492 | if (!len) |
| 1493 | return -1; |
| Michael Haggerty | b062660 | 2011-12-12 05:38:19 | [diff] [blame] | 1494 | submodule = xstrndup(path, len); |
| 1495 | refs = get_ref_cache(submodule); |
| 1496 | free(submodule); |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1497 | |
| Michael Haggerty | 064d51d | 2011-12-12 05:38:20 | [diff] [blame] | 1498 | retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0); |
| Linus Torvalds | 0ebde32 | 2007-04-10 04:14:26 | [diff] [blame] | 1499 | return retval; |
| 1500 | } |
| Linus Torvalds | ca8db14 | 2005-09-25 16:59:37 | [diff] [blame] | 1501 | |
| Christian Couder | 4886b89 | 2008-09-09 05:10:56 | [diff] [blame] | 1502 | /* |
| Michael Haggerty | 6333158 | 2013-04-22 19:52:15 | [diff] [blame] | 1503 | * Return the ref_entry for the given refname from the packed |
| 1504 | * references. If it does not exist, return NULL. |
| Christian Couder | 4886b89 | 2008-09-09 05:10:56 | [diff] [blame] | 1505 | */ |
| Michael Haggerty | 6333158 | 2013-04-22 19:52:15 | [diff] [blame] | 1506 | static struct ref_entry *get_packed_ref(const char *refname) |
| Michael Haggerty | c224ca7 | 2011-09-15 21:10:35 | [diff] [blame] | 1507 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 1508 | return find_ref(get_packed_refs(&ref_cache), refname); |
| Michael Haggerty | c224ca7 | 2011-09-15 21:10:35 | [diff] [blame] | 1509 | } |
| 1510 | |
| Michael Haggerty | 47f534b | 2013-06-19 06:36:26 | [diff] [blame] | 1511 | /* |
| 1512 | * A loose ref file doesn't exist; check for a packed ref. The |
| 1513 | * options are forwarded from resolve_safe_unsafe(). |
| 1514 | */ |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1515 | static int resolve_missing_loose_ref(const char *refname, |
| 1516 | int resolve_flags, |
| 1517 | unsigned char *sha1, |
| 1518 | int *flags) |
| Michael Haggerty | 47f534b | 2013-06-19 06:36:26 | [diff] [blame] | 1519 | { |
| 1520 | struct ref_entry *entry; |
| 1521 | |
| 1522 | /* |
| 1523 | * The loose reference file does not exist; check for a packed |
| 1524 | * reference. |
| 1525 | */ |
| 1526 | entry = get_packed_ref(refname); |
| 1527 | if (entry) { |
| 1528 | hashcpy(sha1, entry->u.value.sha1); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1529 | if (flags) |
| 1530 | *flags |= REF_ISPACKED; |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1531 | return 0; |
| Michael Haggerty | 47f534b | 2013-06-19 06:36:26 | [diff] [blame] | 1532 | } |
| 1533 | /* The reference is not a packed reference, either. */ |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1534 | if (resolve_flags & RESOLVE_REF_READING) { |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1535 | errno = ENOENT; |
| 1536 | return -1; |
| Michael Haggerty | 47f534b | 2013-06-19 06:36:26 | [diff] [blame] | 1537 | } else { |
| 1538 | hashclr(sha1); |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1539 | return 0; |
| Michael Haggerty | 47f534b | 2013-06-19 06:36:26 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1543 | /* This function needs to return a meaningful errno on failure */ |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1544 | const char *resolve_ref_unsafe(const char *refname, int resolve_flags, unsigned char *sha1, int *flags) |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1545 | { |
| Heikki Orsila | 0104ca0 | 2008-04-27 18:21:58 | [diff] [blame] | 1546 | int depth = MAXDEPTH; |
| 1547 | ssize_t len; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1548 | char buffer[256]; |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1549 | static char refname_buffer[256]; |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1550 | int bad_name = 0; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1551 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1552 | if (flags) |
| 1553 | *flags = 0; |
| Junio C Hamano | 8da1977 | 2006-09-21 05:02:01 | [diff] [blame] | 1554 | |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1555 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1556 | if (flags) |
| 1557 | *flags |= REF_BAD_NAME; |
| 1558 | |
| 1559 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || |
| 1560 | !refname_is_safe(refname)) { |
| 1561 | errno = EINVAL; |
| 1562 | return NULL; |
| 1563 | } |
| 1564 | /* |
| 1565 | * dwim_ref() uses REF_ISBROKEN to distinguish between |
| 1566 | * missing refs and refs that were present but invalid, |
| 1567 | * to complain about the latter to stderr. |
| 1568 | * |
| 1569 | * We don't know whether the ref exists, so don't set |
| 1570 | * REF_ISBROKEN yet. |
| 1571 | */ |
| 1572 | bad_name = 1; |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1573 | } |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1574 | for (;;) { |
| Junio C Hamano | 5595635 | 2011-10-19 20:55:49 | [diff] [blame] | 1575 | char path[PATH_MAX]; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1576 | struct stat st; |
| 1577 | char *buf; |
| 1578 | int fd; |
| 1579 | |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1580 | if (--depth < 0) { |
| 1581 | errno = ELOOP; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1582 | return NULL; |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1583 | } |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1584 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1585 | git_snpath(path, sizeof(path), "%s", refname); |
| Michael Haggerty | c224ca7 | 2011-09-15 21:10:35 | [diff] [blame] | 1586 | |
| Michael Haggerty | fcb7c76 | 2013-06-19 06:36:28 | [diff] [blame] | 1587 | /* |
| 1588 | * We might have to loop back here to avoid a race |
| 1589 | * condition: first we lstat() the file, then we try |
| 1590 | * to read it as a link or as a file. But if somebody |
| 1591 | * changes the type of the file (file <-> directory |
| 1592 | * <-> symlink) between the lstat() and reading, then |
| 1593 | * we don't want to report that as an error but rather |
| 1594 | * try again starting with the lstat(). |
| 1595 | */ |
| 1596 | stat_ref: |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1597 | if (lstat(path, &st) < 0) { |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1598 | if (errno != ENOENT) |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1599 | return NULL; |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1600 | if (resolve_missing_loose_ref(refname, resolve_flags, |
| 1601 | sha1, flags)) |
| 1602 | return NULL; |
| 1603 | if (bad_name) { |
| 1604 | hashclr(sha1); |
| 1605 | if (flags) |
| 1606 | *flags |= REF_ISBROKEN; |
| 1607 | } |
| 1608 | return refname; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
| 1612 | if (S_ISLNK(st.st_mode)) { |
| 1613 | len = readlink(path, buffer, sizeof(buffer)-1); |
| Michael Haggerty | fcb7c76 | 2013-06-19 06:36:28 | [diff] [blame] | 1614 | if (len < 0) { |
| 1615 | if (errno == ENOENT || errno == EINVAL) |
| 1616 | /* inconsistent with lstat; retry */ |
| 1617 | goto stat_ref; |
| 1618 | else |
| 1619 | return NULL; |
| 1620 | } |
| Michael Haggerty | b54cb79 | 2011-09-15 21:10:32 | [diff] [blame] | 1621 | buffer[len] = 0; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1622 | if (starts_with(buffer, "refs/") && |
| Michael Haggerty | 1f58a03 | 2011-09-15 21:10:33 | [diff] [blame] | 1623 | !check_refname_format(buffer, 0)) { |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1624 | strcpy(refname_buffer, buffer); |
| 1625 | refname = refname_buffer; |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1626 | if (flags) |
| 1627 | *flags |= REF_ISSYMREF; |
| Jonathan Nieder | 62a2d52 | 2014-09-11 01:22:48 | [diff] [blame] | 1628 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { |
| 1629 | hashclr(sha1); |
| 1630 | return refname; |
| 1631 | } |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1632 | continue; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| Dennis Stosberg | 7a21632 | 2006-10-02 17:23:53 | [diff] [blame] | 1636 | /* Is it a directory? */ |
| 1637 | if (S_ISDIR(st.st_mode)) { |
| 1638 | errno = EISDIR; |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1642 | /* |
| 1643 | * Anything else, just open it and try to use it as |
| 1644 | * a ref |
| 1645 | */ |
| 1646 | fd = open(path, O_RDONLY); |
| Michael Haggerty | fcb7c76 | 2013-06-19 06:36:28 | [diff] [blame] | 1647 | if (fd < 0) { |
| 1648 | if (errno == ENOENT) |
| 1649 | /* inconsistent with lstat; retry */ |
| 1650 | goto stat_ref; |
| 1651 | else |
| 1652 | return NULL; |
| 1653 | } |
| Andy Whitcroft | 93d26e4 | 2007-01-08 15:58:08 | [diff] [blame] | 1654 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1655 | if (len < 0) { |
| 1656 | int save_errno = errno; |
| 1657 | close(fd); |
| 1658 | errno = save_errno; |
| Michael Haggerty | 2877505 | 2011-09-15 21:10:34 | [diff] [blame] | 1659 | return NULL; |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1660 | } |
| 1661 | close(fd); |
| Michael Haggerty | 2877505 | 2011-09-15 21:10:34 | [diff] [blame] | 1662 | while (len && isspace(buffer[len-1])) |
| 1663 | len--; |
| 1664 | buffer[len] = '\0'; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1665 | |
| 1666 | /* |
| 1667 | * Is it a symbolic ref? |
| 1668 | */ |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 1669 | if (!starts_with(buffer, "ref:")) { |
| Michael Haggerty | 2884c06 | 2013-06-19 06:36:27 | [diff] [blame] | 1670 | /* |
| 1671 | * Please note that FETCH_HEAD has a second |
| 1672 | * line containing other data. |
| 1673 | */ |
| 1674 | if (get_sha1_hex(buffer, sha1) || |
| 1675 | (buffer[40] != '\0' && !isspace(buffer[40]))) { |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1676 | if (flags) |
| 1677 | *flags |= REF_ISBROKEN; |
| Ronnie Sahlberg | 76d70dc | 2014-06-20 14:42:54 | [diff] [blame] | 1678 | errno = EINVAL; |
| Michael Haggerty | 2884c06 | 2013-06-19 06:36:27 | [diff] [blame] | 1679 | return NULL; |
| 1680 | } |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1681 | if (bad_name) { |
| 1682 | hashclr(sha1); |
| 1683 | if (flags) |
| 1684 | *flags |= REF_ISBROKEN; |
| 1685 | } |
| Michael Haggerty | 2884c06 | 2013-06-19 06:36:27 | [diff] [blame] | 1686 | return refname; |
| 1687 | } |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1688 | if (flags) |
| 1689 | *flags |= REF_ISSYMREF; |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1690 | buf = buffer + 4; |
| Michael Haggerty | 2877505 | 2011-09-15 21:10:34 | [diff] [blame] | 1691 | while (isspace(*buf)) |
| 1692 | buf++; |
| Jonathan Nieder | 62a2d52 | 2014-09-11 01:22:48 | [diff] [blame] | 1693 | refname = strcpy(refname_buffer, buf); |
| 1694 | if (resolve_flags & RESOLVE_REF_NO_RECURSE) { |
| 1695 | hashclr(sha1); |
| 1696 | return refname; |
| 1697 | } |
| Michael Haggerty | 313fb01 | 2011-09-15 21:10:36 | [diff] [blame] | 1698 | if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) { |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1699 | if (flags) |
| 1700 | *flags |= REF_ISBROKEN; |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 1701 | |
| 1702 | if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || |
| 1703 | !refname_is_safe(buf)) { |
| 1704 | errno = EINVAL; |
| 1705 | return NULL; |
| 1706 | } |
| 1707 | bad_name = 1; |
| Michael Haggerty | 313fb01 | 2011-09-15 21:10:36 | [diff] [blame] | 1708 | } |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1709 | } |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1710 | } |
| 1711 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1712 | char *resolve_refdup(const char *ref, int resolve_flags, unsigned char *sha1, int *flags) |
| Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 14:17:48 | [diff] [blame] | 1713 | { |
| Junio C Hamano | 092c4be | 2015-02-11 21:39:50 | [diff] [blame] | 1714 | return xstrdup_or_null(resolve_ref_unsafe(ref, resolve_flags, sha1, flags)); |
| Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 14:17:48 | [diff] [blame] | 1715 | } |
| 1716 | |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1717 | /* The argument to filter_refs */ |
| 1718 | struct ref_filter { |
| 1719 | const char *pattern; |
| 1720 | each_ref_fn *fn; |
| 1721 | void *cb_data; |
| 1722 | }; |
| 1723 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1724 | int read_ref_full(const char *refname, int resolve_flags, unsigned char *sha1, int *flags) |
| Linus Torvalds | 8a65ff7 | 2005-07-03 03:23:36 | [diff] [blame] | 1725 | { |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1726 | if (resolve_ref_unsafe(refname, resolve_flags, sha1, flags)) |
| Junio C Hamano | a876ed8 | 2005-09-30 21:08:25 | [diff] [blame] | 1727 | return 0; |
| 1728 | return -1; |
| Linus Torvalds | 8a65ff7 | 2005-07-03 03:23:36 | [diff] [blame] | 1729 | } |
| 1730 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1731 | int read_ref(const char *refname, unsigned char *sha1) |
| Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 10:22:14 | [diff] [blame] | 1732 | { |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1733 | return read_ref_full(refname, RESOLVE_REF_READING, sha1, NULL); |
| Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 10:22:14 | [diff] [blame] | 1734 | } |
| 1735 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1736 | int ref_exists(const char *refname) |
| Junio C Hamano | ef06b91 | 2006-11-19 06:13:33 | [diff] [blame] | 1737 | { |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1738 | unsigned char sha1[20]; |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1739 | return !!resolve_ref_unsafe(refname, RESOLVE_REF_READING, sha1, NULL); |
| Junio C Hamano | ef06b91 | 2006-11-19 06:13:33 | [diff] [blame] | 1740 | } |
| 1741 | |
| Michael Haggerty | 85be1fe | 2011-12-12 05:38:10 | [diff] [blame] | 1742 | static int filter_refs(const char *refname, const unsigned char *sha1, int flags, |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1743 | void *data) |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1744 | { |
| 1745 | struct ref_filter *filter = (struct ref_filter *)data; |
| Nguyễn Thái Ngọc Duy | eb07894 | 2014-02-15 02:01:46 | [diff] [blame] | 1746 | if (wildmatch(filter->pattern, refname, 0, NULL)) |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1747 | return 0; |
| Michael Haggerty | 85be1fe | 2011-12-12 05:38:10 | [diff] [blame] | 1748 | return filter->fn(refname, sha1, flags, filter->cb_data); |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 1749 | } |
| 1750 | |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1751 | enum peel_status { |
| 1752 | /* object was peeled successfully: */ |
| 1753 | PEEL_PEELED = 0, |
| 1754 | |
| 1755 | /* |
| 1756 | * object cannot be peeled because the named object (or an |
| 1757 | * object referred to by a tag in the peel chain), does not |
| 1758 | * exist. |
| 1759 | */ |
| 1760 | PEEL_INVALID = -1, |
| 1761 | |
| 1762 | /* object cannot be peeled because it is not a tag: */ |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1763 | PEEL_NON_TAG = -2, |
| 1764 | |
| 1765 | /* ref_entry contains no peeled value because it is a symref: */ |
| 1766 | PEEL_IS_SYMREF = -3, |
| 1767 | |
| 1768 | /* |
| 1769 | * ref_entry cannot be peeled because it is broken (i.e., the |
| 1770 | * symbolic reference cannot even be resolved to an object |
| 1771 | * name): |
| 1772 | */ |
| 1773 | PEEL_BROKEN = -4 |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1774 | }; |
| 1775 | |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1776 | /* |
| 1777 | * Peel the named object; i.e., if the object is a tag, resolve the |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1778 | * tag recursively until a non-tag is found. If successful, store the |
| 1779 | * result to sha1 and return PEEL_PEELED. If the object is not a tag |
| 1780 | * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively, |
| 1781 | * and leave sha1 unchanged. |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1782 | */ |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1783 | static enum peel_status peel_object(const unsigned char *name, unsigned char *sha1) |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1784 | { |
| 1785 | struct object *o = lookup_unknown_object(name); |
| 1786 | |
| 1787 | if (o->type == OBJ_NONE) { |
| 1788 | int type = sha1_object_info(name, NULL); |
| Jeff King | 8ff226a | 2014-07-13 06:42:03 | [diff] [blame] | 1789 | if (type < 0 || !object_as_type(o, type, 0)) |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1790 | return PEEL_INVALID; |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | if (o->type != OBJ_TAG) |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1794 | return PEEL_NON_TAG; |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1795 | |
| 1796 | o = deref_tag_noverify(o); |
| 1797 | if (!o) |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1798 | return PEEL_INVALID; |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1799 | |
| 1800 | hashcpy(sha1, o->sha1); |
| Michael Haggerty | 68cf870 | 2013-04-22 19:52:20 | [diff] [blame] | 1801 | return PEEL_PEELED; |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1802 | } |
| 1803 | |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1804 | /* |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 1805 | * Peel the entry (if possible) and return its new peel_status. If |
| 1806 | * repeel is true, re-peel the entry even if there is an old peeled |
| 1807 | * value that is already stored in it. |
| Michael Haggerty | 694b7a1 | 2013-04-22 19:52:29 | [diff] [blame] | 1808 | * |
| 1809 | * It is OK to call this function with a packed reference entry that |
| 1810 | * might be stale and might even refer to an object that has since |
| 1811 | * been garbage-collected. In such a case, if the entry has |
| 1812 | * REF_KNOWS_PEELED then leave the status unchanged and return |
| 1813 | * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID. |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1814 | */ |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 1815 | static enum peel_status peel_entry(struct ref_entry *entry, int repeel) |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1816 | { |
| 1817 | enum peel_status status; |
| 1818 | |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 1819 | if (entry->flag & REF_KNOWS_PEELED) { |
| 1820 | if (repeel) { |
| 1821 | entry->flag &= ~REF_KNOWS_PEELED; |
| 1822 | hashclr(entry->u.value.peeled); |
| 1823 | } else { |
| 1824 | return is_null_sha1(entry->u.value.peeled) ? |
| 1825 | PEEL_NON_TAG : PEEL_PEELED; |
| 1826 | } |
| 1827 | } |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1828 | if (entry->flag & REF_ISBROKEN) |
| 1829 | return PEEL_BROKEN; |
| 1830 | if (entry->flag & REF_ISSYMREF) |
| 1831 | return PEEL_IS_SYMREF; |
| 1832 | |
| 1833 | status = peel_object(entry->u.value.sha1, entry->u.value.peeled); |
| 1834 | if (status == PEEL_PEELED || status == PEEL_NON_TAG) |
| 1835 | entry->flag |= REF_KNOWS_PEELED; |
| 1836 | return status; |
| 1837 | } |
| 1838 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1839 | int peel_ref(const char *refname, unsigned char *sha1) |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 1840 | { |
| 1841 | int flag; |
| 1842 | unsigned char base[20]; |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 1843 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 1844 | if (current_ref && (current_ref->name == refname |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1845 | || !strcmp(current_ref->name, refname))) { |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 1846 | if (peel_entry(current_ref, 0)) |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1847 | return -1; |
| 1848 | hashcpy(sha1, current_ref->u.value.peeled); |
| 1849 | return 0; |
| Shawn O. Pearce | 0ae91be | 2008-02-24 08:07:22 | [diff] [blame] | 1850 | } |
| 1851 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1852 | if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag)) |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 1853 | return -1; |
| 1854 | |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1855 | /* |
| 1856 | * If the reference is packed, read its ref_entry from the |
| 1857 | * cache in the hope that we already know its peeled value. |
| 1858 | * We only try this optimization on packed references because |
| 1859 | * (a) forcing the filling of the loose reference cache could |
| 1860 | * be expensive and (b) loose references anyway usually do not |
| 1861 | * have REF_KNOWS_PEELED. |
| 1862 | */ |
| 1863 | if (flag & REF_ISPACKED) { |
| Michael Haggerty | f361bae | 2013-04-22 19:52:16 | [diff] [blame] | 1864 | struct ref_entry *r = get_packed_ref(refname); |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1865 | if (r) { |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 1866 | if (peel_entry(r, 0)) |
| Michael Haggerty | 9a489f3 | 2013-04-22 19:52:22 | [diff] [blame] | 1867 | return -1; |
| Michael Haggerty | 593f1bb | 2012-04-10 05:30:23 | [diff] [blame] | 1868 | hashcpy(sha1, r->u.value.peeled); |
| Julian Phillips | e9c4c11 | 2011-09-29 22:11:42 | [diff] [blame] | 1869 | return 0; |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 1870 | } |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 1871 | } |
| 1872 | |
| Michael Haggerty | cb2ae1c | 2013-04-22 19:52:19 | [diff] [blame] | 1873 | return peel_object(base, sha1); |
| Junio C Hamano | cf0adba | 2006-11-19 21:22:44 | [diff] [blame] | 1874 | } |
| 1875 | |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1876 | struct warn_if_dangling_data { |
| 1877 | FILE *fp; |
| 1878 | const char *refname; |
| Jens Lindström | e6bea66 | 2014-05-23 10:30:25 | [diff] [blame] | 1879 | const struct string_list *refnames; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1880 | const char *msg_fmt; |
| 1881 | }; |
| 1882 | |
| 1883 | static int warn_if_dangling_symref(const char *refname, const unsigned char *sha1, |
| 1884 | int flags, void *cb_data) |
| 1885 | { |
| 1886 | struct warn_if_dangling_data *d = cb_data; |
| 1887 | const char *resolves_to; |
| 1888 | unsigned char junk[20]; |
| 1889 | |
| 1890 | if (!(flags & REF_ISSYMREF)) |
| 1891 | return 0; |
| 1892 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 1893 | resolves_to = resolve_ref_unsafe(refname, 0, junk, NULL); |
| Jens Lindström | e6bea66 | 2014-05-23 10:30:25 | [diff] [blame] | 1894 | if (!resolves_to |
| 1895 | || (d->refname |
| 1896 | ? strcmp(resolves_to, d->refname) |
| 1897 | : !string_list_has_string(d->refnames, resolves_to))) { |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1898 | return 0; |
| Jens Lindström | e6bea66 | 2014-05-23 10:30:25 | [diff] [blame] | 1899 | } |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1900 | |
| 1901 | fprintf(d->fp, d->msg_fmt, refname); |
| Junio C Hamano | 1be65ed | 2012-05-02 20:51:35 | [diff] [blame] | 1902 | fputc('\n', d->fp); |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1903 | return 0; |
| 1904 | } |
| 1905 | |
| 1906 | void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) |
| 1907 | { |
| 1908 | struct warn_if_dangling_data data; |
| 1909 | |
| 1910 | data.fp = fp; |
| 1911 | data.refname = refname; |
| Jens Lindström | e6bea66 | 2014-05-23 10:30:25 | [diff] [blame] | 1912 | data.refnames = NULL; |
| 1913 | data.msg_fmt = msg_fmt; |
| 1914 | for_each_rawref(warn_if_dangling_symref, &data); |
| 1915 | } |
| 1916 | |
| 1917 | void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames) |
| 1918 | { |
| 1919 | struct warn_if_dangling_data data; |
| 1920 | |
| 1921 | data.fp = fp; |
| 1922 | data.refname = NULL; |
| 1923 | data.refnames = refnames; |
| Michael Haggerty | bc5fd6d | 2012-04-10 05:30:13 | [diff] [blame] | 1924 | data.msg_fmt = msg_fmt; |
| 1925 | for_each_rawref(warn_if_dangling_symref, &data); |
| 1926 | } |
| 1927 | |
| Michael Haggerty | fcce170 | 2013-04-22 19:52:11 | [diff] [blame] | 1928 | /* |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 1929 | * Call fn for each reference in the specified ref_cache, omitting |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1930 | * references not in the containing_dir of base. fn is called for all |
| 1931 | * references, including broken ones. If fn ever returns a non-zero |
| Michael Haggerty | fcce170 | 2013-04-22 19:52:11 | [diff] [blame] | 1932 | * value, stop the iteration and return that value; otherwise, return |
| 1933 | * 0. |
| 1934 | */ |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 1935 | static int do_for_each_entry(struct ref_cache *refs, const char *base, |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1936 | each_ref_entry_fn fn, void *cb_data) |
| Linus Torvalds | 8a65ff7 | 2005-07-03 03:23:36 | [diff] [blame] | 1937 | { |
| Jeff King | 98eeb09 | 2013-06-20 08:37:53 | [diff] [blame] | 1938 | struct packed_ref_cache *packed_ref_cache; |
| 1939 | struct ref_dir *loose_dir; |
| 1940 | struct ref_dir *packed_dir; |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1941 | int retval = 0; |
| 1942 | |
| Jeff King | 98eeb09 | 2013-06-20 08:37:53 | [diff] [blame] | 1943 | /* |
| 1944 | * We must make sure that all loose refs are read before accessing the |
| 1945 | * packed-refs file; this avoids a race condition in which loose refs |
| 1946 | * are migrated to the packed-refs file by a simultaneous process, but |
| 1947 | * our in-memory view is from before the migration. get_packed_ref_cache() |
| 1948 | * takes care of making sure our view is up to date with what is on |
| 1949 | * disk. |
| 1950 | */ |
| 1951 | loose_dir = get_loose_refs(refs); |
| 1952 | if (base && *base) { |
| 1953 | loose_dir = find_containing_dir(loose_dir, base, 0); |
| 1954 | } |
| 1955 | if (loose_dir) |
| 1956 | prime_ref_dir(loose_dir); |
| 1957 | |
| 1958 | packed_ref_cache = get_packed_ref_cache(refs); |
| Michael Haggerty | 8baf2bb | 2013-06-20 08:37:48 | [diff] [blame] | 1959 | acquire_packed_ref_cache(packed_ref_cache); |
| Jeff King | 98eeb09 | 2013-06-20 08:37:53 | [diff] [blame] | 1960 | packed_dir = get_packed_ref_dir(packed_ref_cache); |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1961 | if (base && *base) { |
| 1962 | packed_dir = find_containing_dir(packed_dir, base, 0); |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | if (packed_dir && loose_dir) { |
| 1966 | sort_ref_dir(packed_dir); |
| 1967 | sort_ref_dir(loose_dir); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1968 | retval = do_for_each_entry_in_dirs( |
| 1969 | packed_dir, loose_dir, fn, cb_data); |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1970 | } else if (packed_dir) { |
| 1971 | sort_ref_dir(packed_dir); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1972 | retval = do_for_each_entry_in_dir( |
| 1973 | packed_dir, 0, fn, cb_data); |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1974 | } else if (loose_dir) { |
| 1975 | sort_ref_dir(loose_dir); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1976 | retval = do_for_each_entry_in_dir( |
| 1977 | loose_dir, 0, fn, cb_data); |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1978 | } |
| 1979 | |
| Michael Haggerty | 8baf2bb | 2013-06-20 08:37:48 | [diff] [blame] | 1980 | release_packed_ref_cache(packed_ref_cache); |
| Michael Haggerty | 933ac03 | 2012-04-10 05:30:27 | [diff] [blame] | 1981 | return retval; |
| Linus Torvalds | 8a65ff7 | 2005-07-03 03:23:36 | [diff] [blame] | 1982 | } |
| 1983 | |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1984 | /* |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 1985 | * Call fn for each reference in the specified ref_cache for which the |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1986 | * refname begins with base. If trim is non-zero, then trim that many |
| 1987 | * characters off the beginning of each refname before passing the |
| 1988 | * refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to include |
| 1989 | * broken references in the iteration. If fn ever returns a non-zero |
| 1990 | * value, stop the iteration and return that value; otherwise, return |
| 1991 | * 0. |
| 1992 | */ |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 1993 | static int do_for_each_ref(struct ref_cache *refs, const char *base, |
| 1994 | each_ref_fn fn, int trim, int flags, void *cb_data) |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 1995 | { |
| 1996 | struct ref_entry_cb data; |
| 1997 | data.base = base; |
| 1998 | data.trim = trim; |
| 1999 | data.flags = flags; |
| 2000 | data.fn = fn; |
| 2001 | data.cb_data = cb_data; |
| 2002 | |
| Jeff King | 49672f2 | 2015-03-20 18:43:06 | [diff] [blame] | 2003 | if (ref_paranoia < 0) |
| 2004 | ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0); |
| 2005 | if (ref_paranoia) |
| 2006 | data.flags |= DO_FOR_EACH_INCLUDE_BROKEN; |
| 2007 | |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 2008 | return do_for_each_entry(refs, base, do_one_ref, &data); |
| Michael Haggerty | 624cac3 | 2013-04-22 19:52:23 | [diff] [blame] | 2009 | } |
| 2010 | |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 2011 | static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data) |
| Linus Torvalds | 723c31f | 2005-07-05 18:31:32 | [diff] [blame] | 2012 | { |
| 2013 | unsigned char sha1[20]; |
| Junio C Hamano | 8da1977 | 2006-09-21 05:02:01 | [diff] [blame] | 2014 | int flag; |
| 2015 | |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 2016 | if (submodule) { |
| 2017 | if (resolve_gitlink_ref(submodule, "HEAD", sha1) == 0) |
| 2018 | return fn("HEAD", sha1, 0, cb_data); |
| 2019 | |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2023 | if (!read_ref_full("HEAD", RESOLVE_REF_READING, sha1, &flag)) |
| Junio C Hamano | 8da1977 | 2006-09-21 05:02:01 | [diff] [blame] | 2024 | return fn("HEAD", sha1, flag, cb_data); |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 2025 | |
| Linus Torvalds | 2f34ba3 | 2005-07-05 22:45:00 | [diff] [blame] | 2026 | return 0; |
| Linus Torvalds | 723c31f | 2005-07-05 18:31:32 | [diff] [blame] | 2027 | } |
| 2028 | |
| Heiko Voigt | 0bad611 | 2010-07-07 13:39:11 | [diff] [blame] | 2029 | int head_ref(each_ref_fn fn, void *cb_data) |
| 2030 | { |
| 2031 | return do_head_ref(NULL, fn, cb_data); |
| 2032 | } |
| 2033 | |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2034 | int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
| 2035 | { |
| 2036 | return do_head_ref(submodule, fn, cb_data); |
| 2037 | } |
| 2038 | |
| Junio C Hamano | cb5d709 | 2006-09-21 04:47:42 | [diff] [blame] | 2039 | int for_each_ref(each_ref_fn fn, void *cb_data) |
| Linus Torvalds | 8a65ff7 | 2005-07-03 03:23:36 | [diff] [blame] | 2040 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 2041 | return do_for_each_ref(&ref_cache, "", fn, 0, 0, cb_data); |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2042 | } |
| 2043 | |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2044 | int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
| 2045 | { |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 2046 | return do_for_each_ref(get_ref_cache(submodule), "", fn, 0, 0, cb_data); |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2047 | } |
| 2048 | |
| Christian Couder | 2a8177b | 2009-03-30 03:07:15 | [diff] [blame] | 2049 | int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) |
| 2050 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 2051 | return do_for_each_ref(&ref_cache, prefix, fn, strlen(prefix), 0, cb_data); |
| Christian Couder | 2a8177b | 2009-03-30 03:07:15 | [diff] [blame] | 2052 | } |
| 2053 | |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2054 | int for_each_ref_in_submodule(const char *submodule, const char *prefix, |
| 2055 | each_ref_fn fn, void *cb_data) |
| 2056 | { |
| Michael Haggerty | 65cf102 | 2013-04-22 19:52:40 | [diff] [blame] | 2057 | return do_for_each_ref(get_ref_cache(submodule), prefix, fn, strlen(prefix), 0, cb_data); |
| Christian Couder | 2a8177b | 2009-03-30 03:07:15 | [diff] [blame] | 2058 | } |
| 2059 | |
| Junio C Hamano | cb5d709 | 2006-09-21 04:47:42 | [diff] [blame] | 2060 | int for_each_tag_ref(each_ref_fn fn, void *cb_data) |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2061 | { |
| Christian Couder | 2a8177b | 2009-03-30 03:07:15 | [diff] [blame] | 2062 | return for_each_ref_in("refs/tags/", fn, cb_data); |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2063 | } |
| 2064 | |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2065 | int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
| 2066 | { |
| 2067 | return for_each_ref_in_submodule(submodule, "refs/tags/", fn, cb_data); |
| 2068 | } |
| 2069 | |
| Junio C Hamano | cb5d709 | 2006-09-21 04:47:42 | [diff] [blame] | 2070 | int for_each_branch_ref(each_ref_fn fn, void *cb_data) |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2071 | { |
| Christian Couder | 2a8177b | 2009-03-30 03:07:15 | [diff] [blame] | 2072 | return for_each_ref_in("refs/heads/", fn, cb_data); |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2073 | } |
| 2074 | |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2075 | int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
| 2076 | { |
| 2077 | return for_each_ref_in_submodule(submodule, "refs/heads/", fn, cb_data); |
| 2078 | } |
| 2079 | |
| Junio C Hamano | cb5d709 | 2006-09-21 04:47:42 | [diff] [blame] | 2080 | int for_each_remote_ref(each_ref_fn fn, void *cb_data) |
| Sean | a62be77 | 2006-05-14 01:43:00 | [diff] [blame] | 2081 | { |
| Christian Couder | 2a8177b | 2009-03-30 03:07:15 | [diff] [blame] | 2082 | return for_each_ref_in("refs/remotes/", fn, cb_data); |
| Junio C Hamano | f8948e2 | 2009-02-09 07:27:10 | [diff] [blame] | 2083 | } |
| 2084 | |
| Heiko Voigt | 9ef6aeb | 2010-07-07 13:39:12 | [diff] [blame] | 2085 | int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) |
| 2086 | { |
| 2087 | return for_each_ref_in_submodule(submodule, "refs/remotes/", fn, cb_data); |
| 2088 | } |
| 2089 | |
| Christian Couder | 2926870 | 2009-01-23 09:06:38 | [diff] [blame] | 2090 | int for_each_replace_ref(each_ref_fn fn, void *cb_data) |
| 2091 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 2092 | return do_for_each_ref(&ref_cache, "refs/replace/", fn, 13, 0, cb_data); |
| Christian Couder | 2926870 | 2009-01-23 09:06:38 | [diff] [blame] | 2093 | } |
| 2094 | |
| Josh Triplett | a1bea2c | 2011-07-05 17:54:44 | [diff] [blame] | 2095 | int head_ref_namespaced(each_ref_fn fn, void *cb_data) |
| 2096 | { |
| 2097 | struct strbuf buf = STRBUF_INIT; |
| 2098 | int ret = 0; |
| 2099 | unsigned char sha1[20]; |
| 2100 | int flag; |
| 2101 | |
| 2102 | strbuf_addf(&buf, "%sHEAD", get_git_namespace()); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2103 | if (!read_ref_full(buf.buf, RESOLVE_REF_READING, sha1, &flag)) |
| Josh Triplett | a1bea2c | 2011-07-05 17:54:44 | [diff] [blame] | 2104 | ret = fn(buf.buf, sha1, flag, cb_data); |
| 2105 | strbuf_release(&buf); |
| 2106 | |
| 2107 | return ret; |
| 2108 | } |
| 2109 | |
| 2110 | int for_each_namespaced_ref(each_ref_fn fn, void *cb_data) |
| 2111 | { |
| 2112 | struct strbuf buf = STRBUF_INIT; |
| 2113 | int ret; |
| 2114 | strbuf_addf(&buf, "%srefs/", get_git_namespace()); |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 2115 | ret = do_for_each_ref(&ref_cache, buf.buf, fn, 0, 0, cb_data); |
| Josh Triplett | a1bea2c | 2011-07-05 17:54:44 | [diff] [blame] | 2116 | strbuf_release(&buf); |
| 2117 | return ret; |
| 2118 | } |
| 2119 | |
| Ilari Liusvaara | b09fe97 | 2010-01-20 09:48:26 | [diff] [blame] | 2120 | int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, |
| 2121 | const char *prefix, void *cb_data) |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 2122 | { |
| 2123 | struct strbuf real_pattern = STRBUF_INIT; |
| 2124 | struct ref_filter filter; |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 2125 | int ret; |
| 2126 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2127 | if (!prefix && !starts_with(pattern, "refs/")) |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 2128 | strbuf_addstr(&real_pattern, "refs/"); |
| Ilari Liusvaara | b09fe97 | 2010-01-20 09:48:26 | [diff] [blame] | 2129 | else if (prefix) |
| 2130 | strbuf_addstr(&real_pattern, prefix); |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 2131 | strbuf_addstr(&real_pattern, pattern); |
| 2132 | |
| Thomas Rast | 894a9d3 | 2010-03-12 17:04:26 | [diff] [blame] | 2133 | if (!has_glob_specials(pattern)) { |
| Junio C Hamano | 9517e6b | 2010-02-04 05:23:18 | [diff] [blame] | 2134 | /* Append implied '/' '*' if not present. */ |
| Ilari Liusvaara | d08bae7 | 2010-01-20 09:48:25 | [diff] [blame] | 2135 | if (real_pattern.buf[real_pattern.len - 1] != '/') |
| 2136 | strbuf_addch(&real_pattern, '/'); |
| 2137 | /* No need to check for '*', there is none. */ |
| 2138 | strbuf_addch(&real_pattern, '*'); |
| 2139 | } |
| 2140 | |
| 2141 | filter.pattern = real_pattern.buf; |
| 2142 | filter.fn = fn; |
| 2143 | filter.cb_data = cb_data; |
| 2144 | ret = for_each_ref(filter_refs, &filter); |
| 2145 | |
| 2146 | strbuf_release(&real_pattern); |
| 2147 | return ret; |
| 2148 | } |
| 2149 | |
| Ilari Liusvaara | b09fe97 | 2010-01-20 09:48:26 | [diff] [blame] | 2150 | int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) |
| 2151 | { |
| 2152 | return for_each_glob_ref_in(fn, pattern, NULL, cb_data); |
| 2153 | } |
| 2154 | |
| Junio C Hamano | f8948e2 | 2009-02-09 07:27:10 | [diff] [blame] | 2155 | int for_each_rawref(each_ref_fn fn, void *cb_data) |
| 2156 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 2157 | return do_for_each_ref(&ref_cache, "", fn, 0, |
| Junio C Hamano | f8948e2 | 2009-02-09 07:27:10 | [diff] [blame] | 2158 | DO_FOR_EACH_INCLUDE_BROKEN, cb_data); |
| Linus Torvalds | 8a65ff7 | 2005-07-03 03:23:36 | [diff] [blame] | 2159 | } |
| 2160 | |
| Felipe Contreras | 4577e48 | 2009-05-13 21:22:04 | [diff] [blame] | 2161 | const char *prettify_refname(const char *name) |
| Daniel Barkalow | a9c37a7 | 2009-03-09 01:06:05 | [diff] [blame] | 2162 | { |
| Daniel Barkalow | a9c37a7 | 2009-03-09 01:06:05 | [diff] [blame] | 2163 | return name + ( |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2164 | starts_with(name, "refs/heads/") ? 11 : |
| 2165 | starts_with(name, "refs/tags/") ? 10 : |
| 2166 | starts_with(name, "refs/remotes/") ? 13 : |
| Daniel Barkalow | a9c37a7 | 2009-03-09 01:06:05 | [diff] [blame] | 2167 | 0); |
| 2168 | } |
| 2169 | |
| Michael Haggerty | 54457fe | 2014-01-14 03:16:07 | [diff] [blame] | 2170 | static const char *ref_rev_parse_rules[] = { |
| Steffen Prohaska | 7980332 | 2007-11-11 14:01:46 | [diff] [blame] | 2171 | "%.*s", |
| 2172 | "refs/%.*s", |
| 2173 | "refs/tags/%.*s", |
| 2174 | "refs/heads/%.*s", |
| 2175 | "refs/remotes/%.*s", |
| 2176 | "refs/remotes/%.*s/HEAD", |
| 2177 | NULL |
| 2178 | }; |
| 2179 | |
| Michael Haggerty | 54457fe | 2014-01-14 03:16:07 | [diff] [blame] | 2180 | int refname_match(const char *abbrev_name, const char *full_name) |
| Steffen Prohaska | 7980332 | 2007-11-11 14:01:46 | [diff] [blame] | 2181 | { |
| 2182 | const char **p; |
| 2183 | const int abbrev_name_len = strlen(abbrev_name); |
| 2184 | |
| Michael Haggerty | 54457fe | 2014-01-14 03:16:07 | [diff] [blame] | 2185 | for (p = ref_rev_parse_rules; *p; p++) { |
| Steffen Prohaska | 7980332 | 2007-11-11 14:01:46 | [diff] [blame] | 2186 | if (!strcmp(full_name, mkpath(*p, abbrev_name_len, abbrev_name))) { |
| 2187 | return 1; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
| Ronnie Sahlberg | 0b1e654 | 2014-12-12 08:57:00 | [diff] [blame] | 2194 | static void unlock_ref(struct ref_lock *lock) |
| 2195 | { |
| 2196 | /* Do not free lock->lk -- atexit() still looks at them */ |
| 2197 | if (lock->lk) |
| 2198 | rollback_lock_file(lock->lk); |
| 2199 | free(lock->ref_name); |
| 2200 | free(lock->orig_ref_name); |
| 2201 | free(lock); |
| 2202 | } |
| 2203 | |
| Ronnie Sahlberg | 835e3c9 | 2014-06-20 14:42:51 | [diff] [blame] | 2204 | /* This function should make sure errno is meaningful on error */ |
| Junio C Hamano | e5f38ec | 2006-06-06 21:04:17 | [diff] [blame] | 2205 | static struct ref_lock *verify_lock(struct ref_lock *lock, |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2206 | const unsigned char *old_sha1, int mustexist) |
| Daniel Barkalow | 95fc751 | 2005-06-06 20:31:29 | [diff] [blame] | 2207 | { |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2208 | if (read_ref_full(lock->ref_name, |
| 2209 | mustexist ? RESOLVE_REF_READING : 0, |
| 2210 | lock->old_sha1, NULL)) { |
| Ronnie Sahlberg | 835e3c9 | 2014-06-20 14:42:51 | [diff] [blame] | 2211 | int save_errno = errno; |
| Linus Torvalds | 434cd0c | 2006-09-14 17:14:47 | [diff] [blame] | 2212 | error("Can't verify ref %s", lock->ref_name); |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2213 | unlock_ref(lock); |
| Ronnie Sahlberg | 835e3c9 | 2014-06-20 14:42:51 | [diff] [blame] | 2214 | errno = save_errno; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2215 | return NULL; |
| 2216 | } |
| David Rientjes | a89fccd | 2006-08-17 18:54:57 | [diff] [blame] | 2217 | if (hashcmp(lock->old_sha1, old_sha1)) { |
| Linus Torvalds | 434cd0c | 2006-09-14 17:14:47 | [diff] [blame] | 2218 | error("Ref %s is at %s but expected %s", lock->ref_name, |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2219 | sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1)); |
| 2220 | unlock_ref(lock); |
| Ronnie Sahlberg | 835e3c9 | 2014-06-20 14:42:51 | [diff] [blame] | 2221 | errno = EBUSY; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2222 | return NULL; |
| 2223 | } |
| 2224 | return lock; |
| 2225 | } |
| 2226 | |
| Johannes Schindelin | 7155b72 | 2007-09-28 15:28:54 | [diff] [blame] | 2227 | static int remove_empty_directories(const char *file) |
| Junio C Hamano | bc7127e | 2006-09-30 09:25:30 | [diff] [blame] | 2228 | { |
| 2229 | /* we want to create a file but there is a directory there; |
| 2230 | * if that is an empty directory (or a directory that contains |
| 2231 | * only empty directories), remove them. |
| 2232 | */ |
| Johannes Schindelin | 7155b72 | 2007-09-28 15:28:54 | [diff] [blame] | 2233 | struct strbuf path; |
| Ronnie Sahlberg | 470a91e | 2014-06-20 14:42:52 | [diff] [blame] | 2234 | int result, save_errno; |
| Junio C Hamano | bc7127e | 2006-09-30 09:25:30 | [diff] [blame] | 2235 | |
| Johannes Schindelin | 7155b72 | 2007-09-28 15:28:54 | [diff] [blame] | 2236 | strbuf_init(&path, 20); |
| 2237 | strbuf_addstr(&path, file); |
| 2238 | |
| Junio C Hamano | a0f4afb | 2009-06-30 22:33:45 | [diff] [blame] | 2239 | result = remove_dir_recursively(&path, REMOVE_DIR_EMPTY_ONLY); |
| Ronnie Sahlberg | 470a91e | 2014-06-20 14:42:52 | [diff] [blame] | 2240 | save_errno = errno; |
| Johannes Schindelin | 7155b72 | 2007-09-28 15:28:54 | [diff] [blame] | 2241 | |
| 2242 | strbuf_release(&path); |
| Ronnie Sahlberg | 470a91e | 2014-06-20 14:42:52 | [diff] [blame] | 2243 | errno = save_errno; |
| Johannes Schindelin | 7155b72 | 2007-09-28 15:28:54 | [diff] [blame] | 2244 | |
| 2245 | return result; |
| Junio C Hamano | bc7127e | 2006-09-30 09:25:30 | [diff] [blame] | 2246 | } |
| 2247 | |
| Michael Haggerty | 19b68b1 | 2011-12-12 05:38:12 | [diff] [blame] | 2248 | /* |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2249 | * *string and *len will only be substituted, and *string returned (for |
| 2250 | * later free()ing) if the string passed in is a magic short-hand form |
| 2251 | * to name a branch. |
| 2252 | */ |
| 2253 | static char *substitute_branch_name(const char **string, int *len) |
| 2254 | { |
| 2255 | struct strbuf buf = STRBUF_INIT; |
| Felipe Contreras | cf99a76 | 2013-09-02 06:34:29 | [diff] [blame] | 2256 | int ret = interpret_branch_name(*string, *len, &buf); |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2257 | |
| 2258 | if (ret == *len) { |
| 2259 | size_t size; |
| 2260 | *string = strbuf_detach(&buf, &size); |
| 2261 | *len = size; |
| 2262 | return (char *)*string; |
| 2263 | } |
| 2264 | |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | |
| 2268 | int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref) |
| 2269 | { |
| 2270 | char *last_branch = substitute_branch_name(&str, &len); |
| 2271 | const char **p, *r; |
| 2272 | int refs_found = 0; |
| 2273 | |
| 2274 | *ref = NULL; |
| 2275 | for (p = ref_rev_parse_rules; *p; p++) { |
| 2276 | char fullref[PATH_MAX]; |
| 2277 | unsigned char sha1_from_ref[20]; |
| 2278 | unsigned char *this_result; |
| 2279 | int flag; |
| 2280 | |
| 2281 | this_result = refs_found ? sha1_from_ref : sha1; |
| 2282 | mksnpath(fullref, sizeof(fullref), *p, len, str); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2283 | r = resolve_ref_unsafe(fullref, RESOLVE_REF_READING, |
| 2284 | this_result, &flag); |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2285 | if (r) { |
| 2286 | if (!refs_found++) |
| 2287 | *ref = xstrdup(r); |
| 2288 | if (!warn_ambiguous_refs) |
| 2289 | break; |
| Junio C Hamano | 5595635 | 2011-10-19 20:55:49 | [diff] [blame] | 2290 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) { |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2291 | warning("ignoring dangling symref %s.", fullref); |
| Junio C Hamano | 5595635 | 2011-10-19 20:55:49 | [diff] [blame] | 2292 | } else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) { |
| 2293 | warning("ignoring broken ref %s.", fullref); |
| 2294 | } |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2295 | } |
| 2296 | free(last_branch); |
| 2297 | return refs_found; |
| 2298 | } |
| 2299 | |
| 2300 | int dwim_log(const char *str, int len, unsigned char *sha1, char **log) |
| 2301 | { |
| 2302 | char *last_branch = substitute_branch_name(&str, &len); |
| 2303 | const char **p; |
| 2304 | int logs_found = 0; |
| 2305 | |
| 2306 | *log = NULL; |
| 2307 | for (p = ref_rev_parse_rules; *p; p++) { |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2308 | unsigned char hash[20]; |
| 2309 | char path[PATH_MAX]; |
| 2310 | const char *ref, *it; |
| 2311 | |
| 2312 | mksnpath(path, sizeof(path), *p, len, str); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2313 | ref = resolve_ref_unsafe(path, RESOLVE_REF_READING, |
| 2314 | hash, NULL); |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2315 | if (!ref) |
| 2316 | continue; |
| Ronnie Sahlberg | 4da5883 | 2014-05-06 22:45:52 | [diff] [blame] | 2317 | if (reflog_exists(path)) |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2318 | it = path; |
| Ronnie Sahlberg | 4da5883 | 2014-05-06 22:45:52 | [diff] [blame] | 2319 | else if (strcmp(ref, path) && reflog_exists(ref)) |
| Junio C Hamano | ff74f7f | 2011-10-12 17:35:38 | [diff] [blame] | 2320 | it = ref; |
| 2321 | else |
| 2322 | continue; |
| 2323 | if (!logs_found++) { |
| 2324 | *log = xstrdup(it); |
| 2325 | hashcpy(sha1, hash); |
| 2326 | } |
| 2327 | if (!warn_ambiguous_refs) |
| 2328 | break; |
| 2329 | } |
| 2330 | free(last_branch); |
| 2331 | return logs_found; |
| 2332 | } |
| 2333 | |
| Ronnie Sahlberg | 88b680a | 2014-04-28 22:38:47 | [diff] [blame] | 2334 | /* |
| Ronnie Sahlberg | 3c93c84 | 2014-10-02 14:59:02 | [diff] [blame] | 2335 | * Locks a ref returning the lock on success and NULL on failure. |
| Ronnie Sahlberg | 88b680a | 2014-04-28 22:38:47 | [diff] [blame] | 2336 | * On failure errno is set to something meaningful. |
| 2337 | */ |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2338 | static struct ref_lock *lock_ref_sha1_basic(const char *refname, |
| 2339 | const unsigned char *old_sha1, |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 2340 | const struct string_list *extras, |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 2341 | const struct string_list *skip, |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2342 | unsigned int flags, int *type_p, |
| 2343 | struct strbuf *err) |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2344 | { |
| Linus Torvalds | 434cd0c | 2006-09-14 17:14:47 | [diff] [blame] | 2345 | char *ref_file; |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2346 | const char *orig_refname = refname; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2347 | struct ref_lock *lock; |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2348 | int last_errno = 0; |
| Junio C Hamano | acd3b9e | 2008-10-17 22:44:39 | [diff] [blame] | 2349 | int type, lflags; |
| Junio C Hamano | 4431fcc | 2006-09-27 08:09:18 | [diff] [blame] | 2350 | int mustexist = (old_sha1 && !is_null_sha1(old_sha1)); |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2351 | int resolve_flags = 0; |
| Michael Haggerty | c4c61c7 | 2014-01-18 22:48:54 | [diff] [blame] | 2352 | int attempts_remaining = 3; |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2353 | |
| 2354 | assert(err); |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2355 | |
| 2356 | lock = xcalloc(1, sizeof(struct ref_lock)); |
| 2357 | lock->lock_fd = -1; |
| 2358 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2359 | if (mustexist) |
| 2360 | resolve_flags |= RESOLVE_REF_READING; |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 2361 | if (flags & REF_DELETING) { |
| 2362 | resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; |
| 2363 | if (flags & REF_NODEREF) |
| 2364 | resolve_flags |= RESOLVE_REF_NO_RECURSE; |
| 2365 | } |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2366 | |
| 2367 | refname = resolve_ref_unsafe(refname, resolve_flags, |
| 2368 | lock->old_sha1, &type); |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2369 | if (!refname && errno == EISDIR) { |
| Junio C Hamano | bc7127e | 2006-09-30 09:25:30 | [diff] [blame] | 2370 | /* we are trying to lock foo but we used to |
| 2371 | * have foo/bar which now does not exist; |
| 2372 | * it is normal for the empty directory 'foo' |
| 2373 | * to remain. |
| 2374 | */ |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2375 | ref_file = git_path("%s", orig_refname); |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2376 | if (remove_empty_directories(ref_file)) { |
| 2377 | last_errno = errno; |
| Michael Haggerty | 5b2d8d6 | 2015-05-11 15:25:16 | [diff] [blame] | 2378 | |
| 2379 | if (!verify_refname_available(orig_refname, extras, skip, |
| 2380 | get_loose_refs(&ref_cache), err)) |
| 2381 | strbuf_addf(err, "there are still refs under '%s'", |
| 2382 | orig_refname); |
| 2383 | |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2384 | goto error_return; |
| 2385 | } |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2386 | refname = resolve_ref_unsafe(orig_refname, resolve_flags, |
| 2387 | lock->old_sha1, &type); |
| Junio C Hamano | bc7127e | 2006-09-30 09:25:30 | [diff] [blame] | 2388 | } |
| Sven Verdoolaege | 68db31c | 2007-05-09 10:33:20 | [diff] [blame] | 2389 | if (type_p) |
| 2390 | *type_p = type; |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2391 | if (!refname) { |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2392 | last_errno = errno; |
| Michael Haggerty | 5b2d8d6 | 2015-05-11 15:25:16 | [diff] [blame] | 2393 | if (last_errno != ENOTDIR || |
| 2394 | !verify_refname_available(orig_refname, extras, skip, |
| 2395 | get_loose_refs(&ref_cache), err)) |
| 2396 | strbuf_addf(err, "unable to resolve reference %s: %s", |
| 2397 | orig_refname, strerror(last_errno)); |
| 2398 | |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2399 | goto error_return; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2400 | } |
| Michael Haggerty | 074336e | 2015-03-02 09:29:53 | [diff] [blame] | 2401 | /* |
| 2402 | * If the ref did not exist and we are creating it, make sure |
| 2403 | * there is no existing packed ref whose name begins with our |
| 2404 | * refname, nor a packed ref whose name is a proper prefix of |
| 2405 | * our refname. |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2406 | */ |
| Michael Haggerty | 074336e | 2015-03-02 09:29:53 | [diff] [blame] | 2407 | if (is_null_sha1(lock->old_sha1) && |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 2408 | verify_refname_available(refname, extras, skip, |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2409 | get_packed_refs(&ref_cache), err)) { |
| Jeff King | f475e08 | 2009-05-25 10:37:15 | [diff] [blame] | 2410 | last_errno = ENOTDIR; |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2411 | goto error_return; |
| Jeff King | f475e08 | 2009-05-25 10:37:15 | [diff] [blame] | 2412 | } |
| Junio C Hamano | 22a3844 | 2006-09-30 21:19:25 | [diff] [blame] | 2413 | |
| Junio C Hamano | c33d517 | 2006-06-06 20:54:14 | [diff] [blame] | 2414 | lock->lk = xcalloc(1, sizeof(struct lock_file)); |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2415 | |
| Michael Haggerty | e5c223e | 2014-01-18 22:48:55 | [diff] [blame] | 2416 | lflags = 0; |
| Junio C Hamano | acd3b9e | 2008-10-17 22:44:39 | [diff] [blame] | 2417 | if (flags & REF_NODEREF) { |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2418 | refname = orig_refname; |
| Michael Haggerty | 47ba466 | 2014-10-01 10:28:37 | [diff] [blame] | 2419 | lflags |= LOCK_NO_DEREF; |
| Junio C Hamano | acd3b9e | 2008-10-17 22:44:39 | [diff] [blame] | 2420 | } |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2421 | lock->ref_name = xstrdup(refname); |
| 2422 | lock->orig_ref_name = xstrdup(orig_refname); |
| 2423 | ref_file = git_path("%s", refname); |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2424 | |
| Michael Haggerty | c4c61c7 | 2014-01-18 22:48:54 | [diff] [blame] | 2425 | retry: |
| 2426 | switch (safe_create_leading_directories(ref_file)) { |
| 2427 | case SCLD_OK: |
| 2428 | break; /* success */ |
| 2429 | case SCLD_VANISHED: |
| 2430 | if (--attempts_remaining > 0) |
| 2431 | goto retry; |
| 2432 | /* fall through */ |
| 2433 | default: |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2434 | last_errno = errno; |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2435 | strbuf_addf(err, "unable to create directory for %s", ref_file); |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2436 | goto error_return; |
| 2437 | } |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2438 | |
| Junio C Hamano | acd3b9e | 2008-10-17 22:44:39 | [diff] [blame] | 2439 | lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags); |
| Michael Haggerty | e5c223e | 2014-01-18 22:48:55 | [diff] [blame] | 2440 | if (lock->lock_fd < 0) { |
| Ronnie Sahlberg | 0683951 | 2014-11-19 22:28:52 | [diff] [blame] | 2441 | last_errno = errno; |
| Michael Haggerty | e5c223e | 2014-01-18 22:48:55 | [diff] [blame] | 2442 | if (errno == ENOENT && --attempts_remaining > 0) |
| 2443 | /* |
| 2444 | * Maybe somebody just deleted one of the |
| 2445 | * directories leading to ref_file. Try |
| 2446 | * again: |
| 2447 | */ |
| 2448 | goto retry; |
| Ronnie Sahlberg | 0683951 | 2014-11-19 22:28:52 | [diff] [blame] | 2449 | else { |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2450 | unable_to_lock_message(ref_file, errno, err); |
| Ronnie Sahlberg | 0683951 | 2014-11-19 22:28:52 | [diff] [blame] | 2451 | goto error_return; |
| 2452 | } |
| Michael Haggerty | e5c223e | 2014-01-18 22:48:55 | [diff] [blame] | 2453 | } |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2454 | return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; |
| Junio C Hamano | 5cc3cef | 2006-09-30 21:14:31 | [diff] [blame] | 2455 | |
| 2456 | error_return: |
| 2457 | unlock_ref(lock); |
| 2458 | errno = last_errno; |
| 2459 | return NULL; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2460 | } |
| 2461 | |
| Michael Haggerty | fec3137 | 2013-04-22 19:52:30 | [diff] [blame] | 2462 | /* |
| 2463 | * Write an entry to the packed-refs file for the specified refname. |
| 2464 | * If peeled is non-NULL, write it as the entry's peeled value. |
| 2465 | */ |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2466 | static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1, |
| Michael Haggerty | fec3137 | 2013-04-22 19:52:30 | [diff] [blame] | 2467 | unsigned char *peeled) |
| Michael Haggerty | d66da47 | 2012-04-10 05:30:17 | [diff] [blame] | 2468 | { |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2469 | fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname); |
| 2470 | if (peeled) |
| 2471 | fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled)); |
| Michael Haggerty | fec3137 | 2013-04-22 19:52:30 | [diff] [blame] | 2472 | } |
| 2473 | |
| Michael Haggerty | 7b40d39 | 2013-06-20 08:37:43 | [diff] [blame] | 2474 | /* |
| 2475 | * An each_ref_entry_fn that writes the entry to a packed-refs file. |
| 2476 | */ |
| 2477 | static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data) |
| 2478 | { |
| Michael Haggerty | 7b40d39 | 2013-06-20 08:37:43 | [diff] [blame] | 2479 | enum peel_status peel_status = peel_entry(entry, 0); |
| 2480 | |
| 2481 | if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG) |
| 2482 | error("internal error: %s is not a valid packed reference!", |
| 2483 | entry->name); |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2484 | write_packed_entry(cb_data, entry->name, entry->u.value.sha1, |
| Michael Haggerty | 7b40d39 | 2013-06-20 08:37:43 | [diff] [blame] | 2485 | peel_status == PEEL_PEELED ? |
| 2486 | entry->u.value.peeled : NULL); |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| Ronnie Sahlberg | 447ff1b | 2014-06-20 14:42:48 | [diff] [blame] | 2490 | /* This should return a meaningful errno on failure */ |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2491 | int lock_packed_refs(int flags) |
| 2492 | { |
| 2493 | struct packed_ref_cache *packed_ref_cache; |
| 2494 | |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2495 | if (hold_lock_file_for_update(&packlock, git_path("packed-refs"), flags) < 0) |
| 2496 | return -1; |
| Michael Haggerty | 5d478f5 | 2013-06-20 08:37:54 | [diff] [blame] | 2497 | /* |
| 2498 | * Get the current packed-refs while holding the lock. If the |
| 2499 | * packed-refs file has been modified since we last read it, |
| 2500 | * this will automatically invalidate the cache and re-read |
| 2501 | * the packed-refs file. |
| 2502 | */ |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2503 | packed_ref_cache = get_packed_ref_cache(&ref_cache); |
| 2504 | packed_ref_cache->lock = &packlock; |
| Michael Haggerty | 4f6b83e | 2013-06-20 08:37:49 | [diff] [blame] | 2505 | /* Increment the reference count to prevent it from being freed: */ |
| 2506 | acquire_packed_ref_cache(packed_ref_cache); |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2507 | return 0; |
| 2508 | } |
| 2509 | |
| Ronnie Sahlberg | d3f6655 | 2014-06-20 14:42:53 | [diff] [blame] | 2510 | /* |
| 2511 | * Commit the packed refs changes. |
| 2512 | * On error we must make sure that errno contains a meaningful value. |
| 2513 | */ |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2514 | int commit_packed_refs(void) |
| 2515 | { |
| 2516 | struct packed_ref_cache *packed_ref_cache = |
| 2517 | get_packed_ref_cache(&ref_cache); |
| 2518 | int error = 0; |
| Ronnie Sahlberg | d3f6655 | 2014-06-20 14:42:53 | [diff] [blame] | 2519 | int save_errno = 0; |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2520 | FILE *out; |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2521 | |
| 2522 | if (!packed_ref_cache->lock) |
| 2523 | die("internal error: packed-refs not locked"); |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2524 | |
| Michael Haggerty | 6e578a3 | 2014-10-01 11:14:49 | [diff] [blame] | 2525 | out = fdopen_lock_file(packed_ref_cache->lock, "w"); |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2526 | if (!out) |
| 2527 | die_errno("unable to fdopen packed-refs descriptor"); |
| 2528 | |
| 2529 | fprintf_or_die(out, "%s", PACKED_REFS_HEADER); |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2530 | do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache), |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2531 | 0, write_packed_entry_fn, out); |
| Jeff King | 9540ce5 | 2014-09-10 10:03:52 | [diff] [blame] | 2532 | |
| Ronnie Sahlberg | d3f6655 | 2014-06-20 14:42:53 | [diff] [blame] | 2533 | if (commit_lock_file(packed_ref_cache->lock)) { |
| 2534 | save_errno = errno; |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2535 | error = -1; |
| Ronnie Sahlberg | d3f6655 | 2014-06-20 14:42:53 | [diff] [blame] | 2536 | } |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2537 | packed_ref_cache->lock = NULL; |
| Michael Haggerty | 4f6b83e | 2013-06-20 08:37:49 | [diff] [blame] | 2538 | release_packed_ref_cache(packed_ref_cache); |
| Ronnie Sahlberg | d3f6655 | 2014-06-20 14:42:53 | [diff] [blame] | 2539 | errno = save_errno; |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2540 | return error; |
| 2541 | } |
| 2542 | |
| 2543 | void rollback_packed_refs(void) |
| 2544 | { |
| 2545 | struct packed_ref_cache *packed_ref_cache = |
| 2546 | get_packed_ref_cache(&ref_cache); |
| 2547 | |
| 2548 | if (!packed_ref_cache->lock) |
| 2549 | die("internal error: packed-refs not locked"); |
| 2550 | rollback_lock_file(packed_ref_cache->lock); |
| 2551 | packed_ref_cache->lock = NULL; |
| Michael Haggerty | 4f6b83e | 2013-06-20 08:37:49 | [diff] [blame] | 2552 | release_packed_ref_cache(packed_ref_cache); |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2553 | clear_packed_ref_cache(&ref_cache); |
| 2554 | } |
| 2555 | |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2556 | struct ref_to_prune { |
| 2557 | struct ref_to_prune *next; |
| 2558 | unsigned char sha1[20]; |
| 2559 | char name[FLEX_ARRAY]; |
| 2560 | }; |
| 2561 | |
| 2562 | struct pack_refs_cb_data { |
| 2563 | unsigned int flags; |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2564 | struct ref_dir *packed_refs; |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2565 | struct ref_to_prune *ref_to_prune; |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2566 | }; |
| 2567 | |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2568 | /* |
| 2569 | * An each_ref_entry_fn that is run over loose references only. If |
| 2570 | * the loose reference can be packed, add an entry in the packed ref |
| 2571 | * cache. If the reference should be pruned, also add it to |
| 2572 | * ref_to_prune in the pack_refs_cb_data. |
| 2573 | */ |
| 2574 | static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data) |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2575 | { |
| 2576 | struct pack_refs_cb_data *cb = cb_data; |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 2577 | enum peel_status peel_status; |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2578 | struct ref_entry *packed_entry; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 2579 | int is_tag_ref = starts_with(entry->name, "refs/tags/"); |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2580 | |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2581 | /* ALWAYS pack tags */ |
| 2582 | if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref) |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2583 | return 0; |
| 2584 | |
| Michael Haggerty | b2a8226 | 2013-04-22 19:52:39 | [diff] [blame] | 2585 | /* Do not pack symbolic or broken refs: */ |
| 2586 | if ((entry->flag & REF_ISSYMREF) || !ref_resolves_to_object(entry)) |
| 2587 | return 0; |
| 2588 | |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2589 | /* Add a packed ref cache entry equivalent to the loose entry. */ |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 2590 | peel_status = peel_entry(entry, 1); |
| Michael Haggerty | 0f29920 | 2013-04-22 19:52:38 | [diff] [blame] | 2591 | if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG) |
| Michael Haggerty | f85354b | 2013-04-22 19:52:37 | [diff] [blame] | 2592 | die("internal error peeling reference %s (%s)", |
| 2593 | entry->name, sha1_to_hex(entry->u.value.sha1)); |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2594 | packed_entry = find_ref(cb->packed_refs, entry->name); |
| 2595 | if (packed_entry) { |
| 2596 | /* Overwrite existing packed entry with info from loose entry */ |
| 2597 | packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED; |
| 2598 | hashcpy(packed_entry->u.value.sha1, entry->u.value.sha1); |
| 2599 | } else { |
| 2600 | packed_entry = create_ref_entry(entry->name, entry->u.value.sha1, |
| 2601 | REF_ISPACKED | REF_KNOWS_PEELED, 0); |
| 2602 | add_ref(cb->packed_refs, packed_entry); |
| 2603 | } |
| 2604 | hashcpy(packed_entry->u.value.peeled, entry->u.value.peeled); |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2605 | |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2606 | /* Schedule the loose reference for pruning if requested. */ |
| 2607 | if ((cb->flags & PACK_REFS_PRUNE)) { |
| Michael Haggerty | 12e7755 | 2013-04-22 19:52:35 | [diff] [blame] | 2608 | int namelen = strlen(entry->name) + 1; |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2609 | struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen); |
| Michael Haggerty | 12e7755 | 2013-04-22 19:52:35 | [diff] [blame] | 2610 | hashcpy(n->sha1, entry->u.value.sha1); |
| 2611 | strcpy(n->name, entry->name); |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2612 | n->next = cb->ref_to_prune; |
| 2613 | cb->ref_to_prune = n; |
| 2614 | } |
| Michael Haggerty | d66da47 | 2012-04-10 05:30:17 | [diff] [blame] | 2615 | return 0; |
| 2616 | } |
| 2617 | |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2618 | /* |
| 2619 | * Remove empty parents, but spare refs/ and immediate subdirs. |
| 2620 | * Note: munges *name. |
| 2621 | */ |
| 2622 | static void try_remove_empty_parents(char *name) |
| 2623 | { |
| 2624 | char *p, *q; |
| 2625 | int i; |
| 2626 | p = name; |
| 2627 | for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ |
| 2628 | while (*p && *p != '/') |
| 2629 | p++; |
| 2630 | /* tolerate duplicate slashes; see check_refname_format() */ |
| 2631 | while (*p == '/') |
| 2632 | p++; |
| 2633 | } |
| 2634 | for (q = p; *q; q++) |
| 2635 | ; |
| 2636 | while (1) { |
| 2637 | while (q > p && *q != '/') |
| 2638 | q--; |
| 2639 | while (q > p && *(q-1) == '/') |
| 2640 | q--; |
| 2641 | if (q == p) |
| 2642 | break; |
| 2643 | *q = '\0'; |
| 2644 | if (rmdir(git_path("%s", name))) |
| 2645 | break; |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | /* make sure nobody touched the ref, and unlink */ |
| 2650 | static void prune_ref(struct ref_to_prune *r) |
| 2651 | { |
| Ronnie Sahlberg | 029cdb4 | 2014-04-30 16:03:36 | [diff] [blame] | 2652 | struct ref_transaction *transaction; |
| 2653 | struct strbuf err = STRBUF_INIT; |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2654 | |
| Junio C Hamano | 88e7dff | 2014-09-11 17:33:33 | [diff] [blame] | 2655 | if (check_refname_format(r->name, 0)) |
| Ronnie Sahlberg | cba1202 | 2014-04-29 22:45:52 | [diff] [blame] | 2656 | return; |
| 2657 | |
| Ronnie Sahlberg | 029cdb4 | 2014-04-30 16:03:36 | [diff] [blame] | 2658 | transaction = ref_transaction_begin(&err); |
| 2659 | if (!transaction || |
| 2660 | ref_transaction_delete(transaction, r->name, r->sha1, |
| Michael Haggerty | fb5a6bb | 2015-02-17 17:00:16 | [diff] [blame] | 2661 | REF_ISPRUNING, NULL, &err) || |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 2662 | ref_transaction_commit(transaction, &err)) { |
| Ronnie Sahlberg | 029cdb4 | 2014-04-30 16:03:36 | [diff] [blame] | 2663 | ref_transaction_free(transaction); |
| 2664 | error("%s", err.buf); |
| 2665 | strbuf_release(&err); |
| 2666 | return; |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2667 | } |
| Ronnie Sahlberg | 029cdb4 | 2014-04-30 16:03:36 | [diff] [blame] | 2668 | ref_transaction_free(transaction); |
| 2669 | strbuf_release(&err); |
| 2670 | try_remove_empty_parents(r->name); |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | static void prune_refs(struct ref_to_prune *r) |
| 2674 | { |
| 2675 | while (r) { |
| 2676 | prune_ref(r); |
| 2677 | r = r->next; |
| 2678 | } |
| 2679 | } |
| 2680 | |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2681 | int pack_refs(unsigned int flags) |
| 2682 | { |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2683 | struct pack_refs_cb_data cbdata; |
| 2684 | |
| 2685 | memset(&cbdata, 0, sizeof(cbdata)); |
| 2686 | cbdata.flags = flags; |
| 2687 | |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2688 | lock_packed_refs(LOCK_DIE_ON_ERROR); |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2689 | cbdata.packed_refs = get_packed_refs(&ref_cache); |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2690 | |
| Michael Haggerty | 267f9a8 | 2013-06-20 08:37:44 | [diff] [blame] | 2691 | do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0, |
| 2692 | pack_if_possible_fn, &cbdata); |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2693 | |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2694 | if (commit_packed_refs()) |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2695 | die_errno("unable to overwrite old ref-pack file"); |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2696 | |
| Michael Haggerty | 32d462c | 2013-04-22 19:52:32 | [diff] [blame] | 2697 | prune_refs(cbdata.ref_to_prune); |
| 2698 | return 0; |
| 2699 | } |
| 2700 | |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 2701 | int repack_without_refs(struct string_list *refnames, struct strbuf *err) |
| Junio C Hamano | c0277d1 | 2006-09-30 22:02:00 | [diff] [blame] | 2702 | { |
| Michael Haggerty | 7618fd8 | 2013-04-22 19:52:17 | [diff] [blame] | 2703 | struct ref_dir *packed; |
| Jeff King | ea56c4e | 2015-03-20 18:43:17 | [diff] [blame] | 2704 | struct string_list_item *refname; |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 2705 | int ret, needs_repacking = 0, removed = 0; |
| Michael Haggerty | 7618fd8 | 2013-04-22 19:52:17 | [diff] [blame] | 2706 | |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 2707 | assert(err); |
| 2708 | |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2709 | /* Look for a packed ref */ |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 2710 | for_each_string_list_item(refname, refnames) { |
| 2711 | if (get_packed_ref(refname->string)) { |
| 2712 | needs_repacking = 1; |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2713 | break; |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 2714 | } |
| 2715 | } |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2716 | |
| 2717 | /* Avoid locking if we have nothing to do */ |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 2718 | if (!needs_repacking) |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2719 | return 0; /* no refname exists in packed refs */ |
| Michael Haggerty | 7618fd8 | 2013-04-22 19:52:17 | [diff] [blame] | 2720 | |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2721 | if (lock_packed_refs(0)) { |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 2722 | unable_to_lock_message(git_path("packed-refs"), errno, err); |
| 2723 | return -1; |
| Miklos Vajna | 1b018fd | 2009-09-26 23:15:09 | [diff] [blame] | 2724 | } |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 2725 | packed = get_packed_refs(&ref_cache); |
| Michael Haggerty | 7b40d39 | 2013-06-20 08:37:43 | [diff] [blame] | 2726 | |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2727 | /* Remove refnames from the cache */ |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 2728 | for_each_string_list_item(refname, refnames) |
| 2729 | if (remove_entry(packed, refname->string) != -1) |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2730 | removed = 1; |
| 2731 | if (!removed) { |
| Michael Haggerty | 506a760 | 2013-04-22 19:52:27 | [diff] [blame] | 2732 | /* |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2733 | * All packed entries disappeared while we were |
| Michael Haggerty | 506a760 | 2013-04-22 19:52:27 | [diff] [blame] | 2734 | * acquiring the lock. |
| 2735 | */ |
| Michael Haggerty | 9f69d29 | 2013-06-20 08:37:46 | [diff] [blame] | 2736 | rollback_packed_refs(); |
| Michael Haggerty | 506a760 | 2013-04-22 19:52:27 | [diff] [blame] | 2737 | return 0; |
| 2738 | } |
| Michael Haggerty | 7b40d39 | 2013-06-20 08:37:43 | [diff] [blame] | 2739 | |
| Brad King | 61cee0d | 2013-09-04 15:22:42 | [diff] [blame] | 2740 | /* Write what remains */ |
| Ronnie Sahlberg | 60bca08 | 2014-06-20 14:42:49 | [diff] [blame] | 2741 | ret = commit_packed_refs(); |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 2742 | if (ret) |
| Ronnie Sahlberg | 60bca08 | 2014-06-20 14:42:49 | [diff] [blame] | 2743 | strbuf_addf(err, "unable to overwrite old ref-pack file: %s", |
| 2744 | strerror(errno)); |
| 2745 | return ret; |
| Junio C Hamano | c0277d1 | 2006-09-30 22:02:00 | [diff] [blame] | 2746 | } |
| 2747 | |
| Ronnie Sahlberg | dbdcac7 | 2014-05-15 15:25:23 | [diff] [blame] | 2748 | static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err) |
| Brad King | 2ddb5d1 | 2013-09-04 15:22:41 | [diff] [blame] | 2749 | { |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 2750 | assert(err); |
| 2751 | |
| Brad King | 2ddb5d1 | 2013-09-04 15:22:41 | [diff] [blame] | 2752 | if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) { |
| Michael Haggerty | 91f1f19 | 2014-10-01 10:28:16 | [diff] [blame] | 2753 | /* |
| 2754 | * loose. The loose file name is the same as the |
| 2755 | * lockfile name, minus ".lock": |
| 2756 | */ |
| Michael Haggerty | ec38b4e | 2014-10-01 10:28:39 | [diff] [blame] | 2757 | char *loose_filename = get_locked_file_path(lock->lk); |
| Ronnie Sahlberg | dbdcac7 | 2014-05-15 15:25:23 | [diff] [blame] | 2758 | int res = unlink_or_msg(loose_filename, err); |
| Michael Haggerty | 91f1f19 | 2014-10-01 10:28:16 | [diff] [blame] | 2759 | free(loose_filename); |
| Ronnie Sahlberg | dbdcac7 | 2014-05-15 15:25:23 | [diff] [blame] | 2760 | if (res) |
| Brad King | 2ddb5d1 | 2013-09-04 15:22:41 | [diff] [blame] | 2761 | return 1; |
| 2762 | } |
| 2763 | return 0; |
| 2764 | } |
| 2765 | |
| Michael Haggerty | fec14ec | 2015-02-17 17:00:13 | [diff] [blame] | 2766 | int delete_ref(const char *refname, const unsigned char *sha1, unsigned int flags) |
| Junio C Hamano | c0277d1 | 2006-09-30 22:02:00 | [diff] [blame] | 2767 | { |
| Ronnie Sahlberg | 7521cc4 | 2014-04-30 16:22:45 | [diff] [blame] | 2768 | struct ref_transaction *transaction; |
| 2769 | struct strbuf err = STRBUF_INIT; |
| Junio C Hamano | c0277d1 | 2006-09-30 22:02:00 | [diff] [blame] | 2770 | |
| Ronnie Sahlberg | 7521cc4 | 2014-04-30 16:22:45 | [diff] [blame] | 2771 | transaction = ref_transaction_begin(&err); |
| 2772 | if (!transaction || |
| Michael Haggerty | fb5a6bb | 2015-02-17 17:00:16 | [diff] [blame] | 2773 | ref_transaction_delete(transaction, refname, |
| 2774 | (sha1 && !is_null_sha1(sha1)) ? sha1 : NULL, |
| 2775 | flags, NULL, &err) || |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 2776 | ref_transaction_commit(transaction, &err)) { |
| Ronnie Sahlberg | 7521cc4 | 2014-04-30 16:22:45 | [diff] [blame] | 2777 | error("%s", err.buf); |
| 2778 | ref_transaction_free(transaction); |
| 2779 | strbuf_release(&err); |
| Junio C Hamano | c0277d1 | 2006-09-30 22:02:00 | [diff] [blame] | 2780 | return 1; |
| Ronnie Sahlberg | 7521cc4 | 2014-04-30 16:22:45 | [diff] [blame] | 2781 | } |
| 2782 | ref_transaction_free(transaction); |
| 2783 | strbuf_release(&err); |
| 2784 | return 0; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 2785 | } |
| 2786 | |
| Pierre Habouzit | 765c225 | 2010-07-07 07:47:20 | [diff] [blame] | 2787 | /* |
| 2788 | * People using contrib's git-new-workdir have .git/logs/refs -> |
| 2789 | * /some/other/path/.git/logs/refs, and that may live on another device. |
| 2790 | * |
| 2791 | * IOW, to avoid cross device rename errors, the temporary renamed log must |
| 2792 | * live into logs/refs. |
| 2793 | */ |
| 2794 | #define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log" |
| 2795 | |
| Michael Haggerty | fa59ae7 | 2014-01-18 22:48:58 | [diff] [blame] | 2796 | static int rename_tmp_log(const char *newrefname) |
| 2797 | { |
| Michael Haggerty | f1e9e9a | 2014-01-18 22:49:00 | [diff] [blame] | 2798 | int attempts_remaining = 4; |
| Michael Haggerty | ae4a283 | 2014-01-18 22:48:59 | [diff] [blame] | 2799 | |
| 2800 | retry: |
| Michael Haggerty | 08f555c | 2014-01-18 22:49:01 | [diff] [blame] | 2801 | switch (safe_create_leading_directories(git_path("logs/%s", newrefname))) { |
| 2802 | case SCLD_OK: |
| 2803 | break; /* success */ |
| 2804 | case SCLD_VANISHED: |
| 2805 | if (--attempts_remaining > 0) |
| 2806 | goto retry; |
| 2807 | /* fall through */ |
| 2808 | default: |
| Michael Haggerty | fa59ae7 | 2014-01-18 22:48:58 | [diff] [blame] | 2809 | error("unable to create directory for %s", newrefname); |
| 2810 | return -1; |
| 2811 | } |
| 2812 | |
| Michael Haggerty | fa59ae7 | 2014-01-18 22:48:58 | [diff] [blame] | 2813 | if (rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", newrefname))) { |
| Michael Haggerty | f1e9e9a | 2014-01-18 22:49:00 | [diff] [blame] | 2814 | if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) { |
| Michael Haggerty | fa59ae7 | 2014-01-18 22:48:58 | [diff] [blame] | 2815 | /* |
| 2816 | * rename(a, b) when b is an existing |
| 2817 | * directory ought to result in ISDIR, but |
| 2818 | * Solaris 5.8 gives ENOTDIR. Sheesh. |
| 2819 | */ |
| 2820 | if (remove_empty_directories(git_path("logs/%s", newrefname))) { |
| 2821 | error("Directory not empty: logs/%s", newrefname); |
| 2822 | return -1; |
| 2823 | } |
| 2824 | goto retry; |
| Michael Haggerty | ae4a283 | 2014-01-18 22:48:59 | [diff] [blame] | 2825 | } else if (errno == ENOENT && --attempts_remaining > 0) { |
| 2826 | /* |
| 2827 | * Maybe another process just deleted one of |
| 2828 | * the directories in the path to newrefname. |
| 2829 | * Try again from the beginning. |
| 2830 | */ |
| 2831 | goto retry; |
| Michael Haggerty | fa59ae7 | 2014-01-18 22:48:58 | [diff] [blame] | 2832 | } else { |
| 2833 | error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s", |
| 2834 | newrefname, strerror(errno)); |
| 2835 | return -1; |
| 2836 | } |
| 2837 | } |
| 2838 | return 0; |
| 2839 | } |
| 2840 | |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 2841 | static int rename_ref_available(const char *oldname, const char *newname) |
| 2842 | { |
| 2843 | struct string_list skip = STRING_LIST_INIT_NODUP; |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 2844 | struct strbuf err = STRBUF_INIT; |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 2845 | int ret; |
| 2846 | |
| 2847 | string_list_insert(&skip, oldname); |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 2848 | ret = !verify_refname_available(newname, NULL, &skip, |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 2849 | get_packed_refs(&ref_cache), &err) |
| Michael Haggerty | 5baf37d | 2015-05-11 15:25:13 | [diff] [blame] | 2850 | && !verify_refname_available(newname, NULL, &skip, |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 2851 | get_loose_refs(&ref_cache), &err); |
| 2852 | if (!ret) |
| 2853 | error("%s", err.buf); |
| 2854 | |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 2855 | string_list_clear(&skip, 0); |
| Michael Haggerty | 1146f17 | 2015-05-11 15:25:14 | [diff] [blame] | 2856 | strbuf_release(&err); |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 2857 | return ret; |
| 2858 | } |
| 2859 | |
| Michael Haggerty | ba43b7f | 2015-05-09 15:20:39 | [diff] [blame] | 2860 | static int write_ref_to_lockfile(struct ref_lock *lock, const unsigned char *sha1); |
| 2861 | static int commit_ref_update(struct ref_lock *lock, |
| 2862 | const unsigned char *sha1, const char *logmsg); |
| Ronnie Sahlberg | aae383d | 2014-04-28 22:36:58 | [diff] [blame] | 2863 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2864 | int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg) |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2865 | { |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2866 | unsigned char sha1[20], orig_sha1[20]; |
| 2867 | int flag = 0, logmoved = 0; |
| 2868 | struct ref_lock *lock; |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2869 | struct stat loginfo; |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2870 | int log = !lstat(git_path("logs/%s", oldrefname), &loginfo); |
| Miklos Vajna | eca35a2 | 2008-10-26 02:33:56 | [diff] [blame] | 2871 | const char *symref = NULL; |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2872 | struct strbuf err = STRBUF_INIT; |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2873 | |
| Miklos Vajna | 450d4c0 | 2008-10-26 02:33:57 | [diff] [blame] | 2874 | if (log && S_ISLNK(loginfo.st_mode)) |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2875 | return error("reflog for %s is a symlink", oldrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2876 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2877 | symref = resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING, |
| 2878 | orig_sha1, &flag); |
| Miklos Vajna | eca35a2 | 2008-10-26 02:33:56 | [diff] [blame] | 2879 | if (flag & REF_ISSYMREF) |
| Miklos Vajna | fa58186c | 2008-10-29 00:05:27 | [diff] [blame] | 2880 | return error("refname %s is a symbolic ref, renaming it is not supported", |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2881 | oldrefname); |
| Miklos Vajna | eca35a2 | 2008-10-26 02:33:56 | [diff] [blame] | 2882 | if (!symref) |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2883 | return error("refname %s not found", oldrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2884 | |
| Ronnie Sahlberg | 5fe7d82 | 2014-05-01 18:16:07 | [diff] [blame] | 2885 | if (!rename_ref_available(oldrefname, newrefname)) |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2886 | return 1; |
| 2887 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2888 | if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG))) |
| Pierre Habouzit | 765c225 | 2010-07-07 07:47:20 | [diff] [blame] | 2889 | return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s", |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2890 | oldrefname, strerror(errno)); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2891 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2892 | if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) { |
| 2893 | error("unable to delete old %s", oldrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2894 | goto rollback; |
| 2895 | } |
| 2896 | |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 2897 | if (!read_ref_full(newrefname, RESOLVE_REF_READING, sha1, NULL) && |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2898 | delete_ref(newrefname, sha1, REF_NODEREF)) { |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2899 | if (errno==EISDIR) { |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2900 | if (remove_empty_directories(git_path("%s", newrefname))) { |
| 2901 | error("Directory not empty: %s", newrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2902 | goto rollback; |
| 2903 | } |
| 2904 | } else { |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2905 | error("unable to delete existing %s", newrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2906 | goto rollback; |
| 2907 | } |
| 2908 | } |
| 2909 | |
| Michael Haggerty | fa59ae7 | 2014-01-18 22:48:58 | [diff] [blame] | 2910 | if (log && rename_tmp_log(newrefname)) |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2911 | goto rollback; |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2912 | |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2913 | logmoved = log; |
| 2914 | |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2915 | lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, 0, NULL, &err); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2916 | if (!lock) { |
| Michael Haggerty | abeef9c | 2015-05-11 15:25:17 | [diff] [blame] | 2917 | error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf); |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2918 | strbuf_release(&err); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2919 | goto rollback; |
| 2920 | } |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2921 | hashcpy(lock->old_sha1, orig_sha1); |
| Michael Haggerty | ba43b7f | 2015-05-09 15:20:39 | [diff] [blame] | 2922 | |
| 2923 | if (write_ref_to_lockfile(lock, orig_sha1) || |
| 2924 | commit_ref_update(lock, orig_sha1, logmsg)) { |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2925 | error("unable to write current sha1 into %s", newrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2926 | goto rollback; |
| 2927 | } |
| 2928 | |
| 2929 | return 0; |
| 2930 | |
| 2931 | rollback: |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2932 | lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, 0, NULL, &err); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2933 | if (!lock) { |
| Michael Haggerty | abeef9c | 2015-05-11 15:25:17 | [diff] [blame] | 2934 | error("unable to lock %s for rollback: %s", oldrefname, err.buf); |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 2935 | strbuf_release(&err); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2936 | goto rollbacklog; |
| 2937 | } |
| 2938 | |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2939 | flag = log_all_ref_updates; |
| 2940 | log_all_ref_updates = 0; |
| Michael Haggerty | ba43b7f | 2015-05-09 15:20:39 | [diff] [blame] | 2941 | if (write_ref_to_lockfile(lock, orig_sha1) || |
| 2942 | commit_ref_update(lock, orig_sha1, NULL)) |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2943 | error("unable to write current sha1 into %s", oldrefname); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2944 | log_all_ref_updates = flag; |
| 2945 | |
| 2946 | rollbacklog: |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2947 | if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname))) |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2948 | error("unable to restore logfile %s from %s: %s", |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2949 | oldrefname, newrefname, strerror(errno)); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2950 | if (!logmoved && log && |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2951 | rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname))) |
| Pierre Habouzit | 765c225 | 2010-07-07 07:47:20 | [diff] [blame] | 2952 | error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s", |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 2953 | oldrefname, strerror(errno)); |
| Lars Hjemli | c976d41 | 2006-11-28 14:47:40 | [diff] [blame] | 2954 | |
| 2955 | return 1; |
| 2956 | } |
| 2957 | |
| Ronnie Sahlberg | 0b1e654 | 2014-12-12 08:57:00 | [diff] [blame] | 2958 | static int close_ref(struct ref_lock *lock) |
| Brandon Casey | b531394 | 2008-01-16 19:14:30 | [diff] [blame] | 2959 | { |
| 2960 | if (close_lock_file(lock->lk)) |
| 2961 | return -1; |
| 2962 | lock->lock_fd = -1; |
| 2963 | return 0; |
| 2964 | } |
| 2965 | |
| Ronnie Sahlberg | 0b1e654 | 2014-12-12 08:57:00 | [diff] [blame] | 2966 | static int commit_ref(struct ref_lock *lock) |
| Brandon Casey | b531394 | 2008-01-16 19:14:30 | [diff] [blame] | 2967 | { |
| 2968 | if (commit_lock_file(lock->lk)) |
| 2969 | return -1; |
| 2970 | lock->lock_fd = -1; |
| 2971 | return 0; |
| 2972 | } |
| 2973 | |
| Junio C Hamano | 0ec29a4 | 2007-07-29 00:17:17 | [diff] [blame] | 2974 | /* |
| 2975 | * copy the reflog message msg to buf, which has been allocated sufficiently |
| 2976 | * large, while cleaning up the whitespaces. Especially, convert LF to space, |
| 2977 | * because reflog file is one line per entry. |
| 2978 | */ |
| 2979 | static int copy_msg(char *buf, const char *msg) |
| 2980 | { |
| 2981 | char *cp = buf; |
| 2982 | char c; |
| 2983 | int wasspace = 1; |
| 2984 | |
| 2985 | *cp++ = '\t'; |
| 2986 | while ((c = *msg++)) { |
| 2987 | if (wasspace && isspace(c)) |
| 2988 | continue; |
| 2989 | wasspace = isspace(c); |
| 2990 | if (wasspace) |
| 2991 | c = ' '; |
| 2992 | *cp++ = c; |
| 2993 | } |
| 2994 | while (buf < cp && isspace(cp[-1])) |
| 2995 | cp--; |
| 2996 | *cp++ = '\n'; |
| 2997 | return cp - buf; |
| 2998 | } |
| 2999 | |
| Ronnie Sahlberg | bd3b02d | 2014-06-20 14:42:50 | [diff] [blame] | 3000 | /* This function must set a meaningful errno on failure */ |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3001 | int log_ref_setup(const char *refname, char *logfile, int bufsize) |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3002 | { |
| 3003 | int logfd, oflags = O_APPEND | O_WRONLY; |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3004 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3005 | git_snpath(logfile, bufsize, "logs/%s", refname); |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3006 | if (log_all_ref_updates && |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 3007 | (starts_with(refname, "refs/heads/") || |
| 3008 | starts_with(refname, "refs/remotes/") || |
| 3009 | starts_with(refname, "refs/notes/") || |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3010 | !strcmp(refname, "HEAD"))) { |
| Ronnie Sahlberg | bd3b02d | 2014-06-20 14:42:50 | [diff] [blame] | 3011 | if (safe_create_leading_directories(logfile) < 0) { |
| 3012 | int save_errno = errno; |
| 3013 | error("unable to create directory for %s", logfile); |
| 3014 | errno = save_errno; |
| 3015 | return -1; |
| 3016 | } |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3017 | oflags |= O_CREAT; |
| 3018 | } |
| 3019 | |
| Thomas Rast | 157aaea | 2010-06-10 12:54:03 | [diff] [blame] | 3020 | logfd = open(logfile, oflags, 0666); |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3021 | if (logfd < 0) { |
| Jeff King | 9233887 | 2014-11-04 13:24:53 | [diff] [blame] | 3022 | if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR)) |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3023 | return 0; |
| 3024 | |
| Jeff King | 9233887 | 2014-11-04 13:24:53 | [diff] [blame] | 3025 | if (errno == EISDIR) { |
| Thomas Rast | 157aaea | 2010-06-10 12:54:03 | [diff] [blame] | 3026 | if (remove_empty_directories(logfile)) { |
| Ronnie Sahlberg | bd3b02d | 2014-06-20 14:42:50 | [diff] [blame] | 3027 | int save_errno = errno; |
| 3028 | error("There are still logs under '%s'", |
| 3029 | logfile); |
| 3030 | errno = save_errno; |
| 3031 | return -1; |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3032 | } |
| Thomas Rast | 157aaea | 2010-06-10 12:54:03 | [diff] [blame] | 3033 | logfd = open(logfile, oflags, 0666); |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3034 | } |
| 3035 | |
| Ronnie Sahlberg | bd3b02d | 2014-06-20 14:42:50 | [diff] [blame] | 3036 | if (logfd < 0) { |
| 3037 | int save_errno = errno; |
| 3038 | error("Unable to append to %s: %s", logfile, |
| 3039 | strerror(errno)); |
| 3040 | errno = save_errno; |
| 3041 | return -1; |
| 3042 | } |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3043 | } |
| 3044 | |
| Thomas Rast | 157aaea | 2010-06-10 12:54:03 | [diff] [blame] | 3045 | adjust_shared_perm(logfile); |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3046 | close(logfd); |
| 3047 | return 0; |
| 3048 | } |
| 3049 | |
| Ronnie Sahlberg | 2c6207a | 2014-12-12 08:56:42 | [diff] [blame] | 3050 | static int log_ref_write_fd(int fd, const unsigned char *old_sha1, |
| 3051 | const unsigned char *new_sha1, |
| 3052 | const char *committer, const char *msg) |
| 3053 | { |
| 3054 | int msglen, written; |
| 3055 | unsigned maxlen, len; |
| 3056 | char *logrec; |
| 3057 | |
| 3058 | msglen = msg ? strlen(msg) : 0; |
| 3059 | maxlen = strlen(committer) + msglen + 100; |
| 3060 | logrec = xmalloc(maxlen); |
| 3061 | len = sprintf(logrec, "%s %s %s\n", |
| 3062 | sha1_to_hex(old_sha1), |
| 3063 | sha1_to_hex(new_sha1), |
| 3064 | committer); |
| 3065 | if (msglen) |
| 3066 | len += copy_msg(logrec + len - 1, msg) - 1; |
| 3067 | |
| 3068 | written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; |
| 3069 | free(logrec); |
| 3070 | if (written != len) |
| 3071 | return -1; |
| 3072 | |
| 3073 | return 0; |
| 3074 | } |
| 3075 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3076 | static int log_ref_write(const char *refname, const unsigned char *old_sha1, |
| Nicolas Pitre | 9a13f0b | 2007-01-26 22:26:05 | [diff] [blame] | 3077 | const unsigned char *new_sha1, const char *msg) |
| Shawn Pearce | 6de08ae | 2006-05-17 09:55:40 | [diff] [blame] | 3078 | { |
| Ronnie Sahlberg | 2c6207a | 2014-12-12 08:56:42 | [diff] [blame] | 3079 | int logfd, result, oflags = O_APPEND | O_WRONLY; |
| Thomas Rast | 157aaea | 2010-06-10 12:54:03 | [diff] [blame] | 3080 | char log_file[PATH_MAX]; |
| Shawn Pearce | 6de08ae | 2006-05-17 09:55:40 | [diff] [blame] | 3081 | |
| Junio C Hamano | 510c5a8 | 2007-01-07 09:35:34 | [diff] [blame] | 3082 | if (log_all_ref_updates < 0) |
| Junio C Hamano | 7d1864c | 2007-01-07 10:00:28 | [diff] [blame] | 3083 | log_all_ref_updates = !is_bare_repository(); |
| Junio C Hamano | 510c5a8 | 2007-01-07 09:35:34 | [diff] [blame] | 3084 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3085 | result = log_ref_setup(refname, log_file, sizeof(log_file)); |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3086 | if (result) |
| 3087 | return result; |
| Nicolas Pitre | 9a13f0b | 2007-01-26 22:26:05 | [diff] [blame] | 3088 | |
| Erick Mattos | 859c301 | 2010-05-22 00:28:36 | [diff] [blame] | 3089 | logfd = open(log_file, oflags); |
| 3090 | if (logfd < 0) |
| 3091 | return 0; |
| Ronnie Sahlberg | 2c6207a | 2014-12-12 08:56:42 | [diff] [blame] | 3092 | result = log_ref_write_fd(logfd, old_sha1, new_sha1, |
| 3093 | git_committer_info(0), msg); |
| 3094 | if (result) { |
| Ronnie Sahlberg | dc615de | 2014-06-20 14:42:55 | [diff] [blame] | 3095 | int save_errno = errno; |
| 3096 | close(logfd); |
| 3097 | error("Unable to append to %s", log_file); |
| 3098 | errno = save_errno; |
| 3099 | return -1; |
| 3100 | } |
| 3101 | if (close(logfd)) { |
| 3102 | int save_errno = errno; |
| 3103 | error("Unable to append to %s", log_file); |
| 3104 | errno = save_errno; |
| 3105 | return -1; |
| 3106 | } |
| Shawn Pearce | 6de08ae | 2006-05-17 09:55:40 | [diff] [blame] | 3107 | return 0; |
| 3108 | } |
| 3109 | |
| Ronnie Sahlberg | e7e0f26 | 2014-07-15 23:02:38 | [diff] [blame] | 3110 | int is_branch(const char *refname) |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3111 | { |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 3112 | return !strcmp(refname, "HEAD") || starts_with(refname, "refs/heads/"); |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3113 | } |
| 3114 | |
| Ronnie Sahlberg | aae383d | 2014-04-28 22:36:58 | [diff] [blame] | 3115 | /* |
| Michael Haggerty | e6fd3c6 | 2015-04-24 11:35:45 | [diff] [blame] | 3116 | * Write sha1 into the open lockfile, then close the lockfile. On |
| 3117 | * errors, rollback the lockfile and set errno to reflect the problem. |
| Ronnie Sahlberg | aae383d | 2014-04-28 22:36:58 | [diff] [blame] | 3118 | */ |
| Michael Haggerty | e6fd3c6 | 2015-04-24 11:35:45 | [diff] [blame] | 3119 | static int write_ref_to_lockfile(struct ref_lock *lock, |
| 3120 | const unsigned char *sha1) |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 3121 | { |
| 3122 | static char term = '\n'; |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3123 | struct object *o; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 3124 | |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3125 | o = parse_object(sha1); |
| 3126 | if (!o) { |
| Dmitry Ivankov | 7be8b3b | 2011-06-16 13:42:48 | [diff] [blame] | 3127 | error("Trying to write ref %s with nonexistent object %s", |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3128 | lock->ref_name, sha1_to_hex(sha1)); |
| 3129 | unlock_ref(lock); |
| Ronnie Sahlberg | dc615de | 2014-06-20 14:42:55 | [diff] [blame] | 3130 | errno = EINVAL; |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3131 | return -1; |
| 3132 | } |
| 3133 | if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) { |
| 3134 | error("Trying to write non-commit object %s to branch %s", |
| 3135 | sha1_to_hex(sha1), lock->ref_name); |
| 3136 | unlock_ref(lock); |
| Ronnie Sahlberg | dc615de | 2014-06-20 14:42:55 | [diff] [blame] | 3137 | errno = EINVAL; |
| Linus Torvalds | c3b0dec | 2008-01-15 23:50:17 | [diff] [blame] | 3138 | return -1; |
| 3139 | } |
| Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 | [diff] [blame] | 3140 | if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || |
| Ronnie Sahlberg | dc615de | 2014-06-20 14:42:55 | [diff] [blame] | 3141 | write_in_full(lock->lock_fd, &term, 1) != 1 || |
| 3142 | close_ref(lock) < 0) { |
| 3143 | int save_errno = errno; |
| Michael Haggerty | cf6950d | 2014-10-01 10:28:32 | [diff] [blame] | 3144 | error("Couldn't write %s", lock->lk->filename.buf); |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 3145 | unlock_ref(lock); |
| Ronnie Sahlberg | dc615de | 2014-06-20 14:42:55 | [diff] [blame] | 3146 | errno = save_errno; |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 3147 | return -1; |
| 3148 | } |
| Michael Haggerty | e6fd3c6 | 2015-04-24 11:35:45 | [diff] [blame] | 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | /* |
| Michael Haggerty | ad4cd6c | 2015-05-09 15:18:36 | [diff] [blame] | 3153 | * Commit a change to a loose reference that has already been written |
| 3154 | * to the loose reference lockfile. Also update the reflogs if |
| 3155 | * necessary, using the specified lockmsg (which can be NULL). |
| Michael Haggerty | e6fd3c6 | 2015-04-24 11:35:45 | [diff] [blame] | 3156 | */ |
| Michael Haggerty | ad4cd6c | 2015-05-09 15:18:36 | [diff] [blame] | 3157 | static int commit_ref_update(struct ref_lock *lock, |
| 3158 | const unsigned char *sha1, const char *logmsg) |
| Michael Haggerty | e6fd3c6 | 2015-04-24 11:35:45 | [diff] [blame] | 3159 | { |
| Michael Haggerty | 9da31cb | 2013-04-22 19:52:41 | [diff] [blame] | 3160 | clear_loose_ref_cache(&ref_cache); |
| Nicolas Pitre | bd104db | 2007-01-26 22:26:07 | [diff] [blame] | 3161 | if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 || |
| 3162 | (strcmp(lock->ref_name, lock->orig_ref_name) && |
| 3163 | log_ref_write(lock->orig_ref_name, lock->old_sha1, sha1, logmsg) < 0)) { |
| Shawn Pearce | 6de08ae | 2006-05-17 09:55:40 | [diff] [blame] | 3164 | unlock_ref(lock); |
| 3165 | return -1; |
| 3166 | } |
| Nicolas Pitre | 605fac8 | 2007-03-21 21:11:44 | [diff] [blame] | 3167 | if (strcmp(lock->orig_ref_name, "HEAD") != 0) { |
| 3168 | /* |
| 3169 | * Special hack: If a branch is updated directly and HEAD |
| 3170 | * points to it (may happen on the remote side of a push |
| 3171 | * for example) then logically the HEAD reflog should be |
| 3172 | * updated too. |
| 3173 | * A generic solution implies reverse symref information, |
| 3174 | * but finding all symrefs pointing to the given branch |
| 3175 | * would be rather costly for this rare event (the direct |
| 3176 | * update of a branch) to be worth it. So let's cheat and |
| 3177 | * check with HEAD only which should cover 99% of all usage |
| 3178 | * scenarios (even 100% of the default ones). |
| 3179 | */ |
| 3180 | unsigned char head_sha1[20]; |
| 3181 | int head_flag; |
| 3182 | const char *head_ref; |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 3183 | head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, |
| 3184 | head_sha1, &head_flag); |
| Nicolas Pitre | 605fac8 | 2007-03-21 21:11:44 | [diff] [blame] | 3185 | if (head_ref && (head_flag & REF_ISSYMREF) && |
| 3186 | !strcmp(head_ref, lock->ref_name)) |
| 3187 | log_ref_write("HEAD", lock->old_sha1, sha1, logmsg); |
| 3188 | } |
| Brandon Casey | b531394 | 2008-01-16 19:14:30 | [diff] [blame] | 3189 | if (commit_ref(lock)) { |
| Linus Torvalds | 434cd0c | 2006-09-14 17:14:47 | [diff] [blame] | 3190 | error("Couldn't set %s", lock->ref_name); |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 3191 | unlock_ref(lock); |
| 3192 | return -1; |
| 3193 | } |
| Shawn Pearce | 4bd18c4 | 2006-05-17 09:55:02 | [diff] [blame] | 3194 | unlock_ref(lock); |
| 3195 | return 0; |
| Daniel Barkalow | 95fc751 | 2005-06-06 20:31:29 | [diff] [blame] | 3196 | } |
| Shawn Pearce | d556fae | 2006-05-17 09:56:09 | [diff] [blame] | 3197 | |
| Nicolas Pitre | 8b5157e | 2007-01-26 22:26:10 | [diff] [blame] | 3198 | int create_symref(const char *ref_target, const char *refs_heads_master, |
| 3199 | const char *logmsg) |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3200 | { |
| 3201 | const char *lockpath; |
| 3202 | char ref[1000]; |
| 3203 | int fd, len, written; |
| Alex Riesen | a4f34cb | 2008-10-27 10:22:09 | [diff] [blame] | 3204 | char *git_HEAD = git_pathdup("%s", ref_target); |
| Nicolas Pitre | 8b5157e | 2007-01-26 22:26:10 | [diff] [blame] | 3205 | unsigned char old_sha1[20], new_sha1[20]; |
| 3206 | |
| 3207 | if (logmsg && read_ref(ref_target, old_sha1)) |
| 3208 | hashclr(old_sha1); |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3209 | |
| Junio C Hamano | d48744d | 2007-02-08 07:41:43 | [diff] [blame] | 3210 | if (safe_create_leading_directories(git_HEAD) < 0) |
| 3211 | return error("unable to create directory for %s", git_HEAD); |
| 3212 | |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3213 | #ifndef NO_SYMLINK_HEAD |
| 3214 | if (prefer_symlink_refs) { |
| 3215 | unlink(git_HEAD); |
| 3216 | if (!symlink(refs_heads_master, git_HEAD)) |
| Nicolas Pitre | 8b5157e | 2007-01-26 22:26:10 | [diff] [blame] | 3217 | goto done; |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3218 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); |
| 3219 | } |
| 3220 | #endif |
| 3221 | |
| 3222 | len = snprintf(ref, sizeof(ref), "ref: %s\n", refs_heads_master); |
| 3223 | if (sizeof(ref) <= len) { |
| 3224 | error("refname too long: %s", refs_heads_master); |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3225 | goto error_free_return; |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3226 | } |
| 3227 | lockpath = mkpath("%s.lock", git_HEAD); |
| 3228 | fd = open(lockpath, O_CREAT | O_EXCL | O_WRONLY, 0666); |
| 3229 | if (fd < 0) { |
| 3230 | error("Unable to open %s for writing", lockpath); |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3231 | goto error_free_return; |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3232 | } |
| 3233 | written = write_in_full(fd, ref, len); |
| Jim Meyering | 91c8d59 | 2007-06-24 19:20:41 | [diff] [blame] | 3234 | if (close(fd) != 0 || written != len) { |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3235 | error("Unable to write to %s", lockpath); |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3236 | goto error_unlink_return; |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3237 | } |
| 3238 | if (rename(lockpath, git_HEAD) < 0) { |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3239 | error("Unable to create %s", git_HEAD); |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3240 | goto error_unlink_return; |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3241 | } |
| 3242 | if (adjust_shared_perm(git_HEAD)) { |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3243 | error("Unable to fix permissions on %s", lockpath); |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3244 | error_unlink_return: |
| Alex Riesen | 691f1a2 | 2009-04-29 21:22:56 | [diff] [blame] | 3245 | unlink_or_warn(lockpath); |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3246 | error_free_return: |
| 3247 | free(git_HEAD); |
| 3248 | return -1; |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3249 | } |
| Nicolas Pitre | 8b5157e | 2007-01-26 22:26:10 | [diff] [blame] | 3250 | |
| Ramsay Jones | ee96d11 | 2007-03-03 18:28:46 | [diff] [blame] | 3251 | #ifndef NO_SYMLINK_HEAD |
| Nicolas Pitre | 8b5157e | 2007-01-26 22:26:10 | [diff] [blame] | 3252 | done: |
| Ramsay Jones | ee96d11 | 2007-03-03 18:28:46 | [diff] [blame] | 3253 | #endif |
| Nicolas Pitre | 8b5157e | 2007-01-26 22:26:10 | [diff] [blame] | 3254 | if (logmsg && !read_ref(refs_heads_master, new_sha1)) |
| 3255 | log_ref_write(ref_target, old_sha1, new_sha1, logmsg); |
| 3256 | |
| Junio C Hamano | 47fc52e | 2007-01-27 01:49:00 | [diff] [blame] | 3257 | free(git_HEAD); |
| Nicolas Pitre | 41b625b | 2007-01-26 22:26:09 | [diff] [blame] | 3258 | return 0; |
| 3259 | } |
| 3260 | |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3261 | struct read_ref_at_cb { |
| 3262 | const char *refname; |
| 3263 | unsigned long at_time; |
| 3264 | int cnt; |
| 3265 | int reccnt; |
| 3266 | unsigned char *sha1; |
| 3267 | int found_it; |
| 3268 | |
| 3269 | unsigned char osha1[20]; |
| 3270 | unsigned char nsha1[20]; |
| 3271 | int tz; |
| 3272 | unsigned long date; |
| 3273 | char **msg; |
| 3274 | unsigned long *cutoff_time; |
| 3275 | int *cutoff_tz; |
| 3276 | int *cutoff_cnt; |
| 3277 | }; |
| 3278 | |
| 3279 | static int read_ref_at_ent(unsigned char *osha1, unsigned char *nsha1, |
| 3280 | const char *email, unsigned long timestamp, int tz, |
| 3281 | const char *message, void *cb_data) |
| Junio C Hamano | 16d7cc9 | 2007-01-19 09:19:05 | [diff] [blame] | 3282 | { |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3283 | struct read_ref_at_cb *cb = cb_data; |
| 3284 | |
| 3285 | cb->reccnt++; |
| 3286 | cb->tz = tz; |
| 3287 | cb->date = timestamp; |
| 3288 | |
| 3289 | if (timestamp <= cb->at_time || cb->cnt == 0) { |
| 3290 | if (cb->msg) |
| 3291 | *cb->msg = xstrdup(message); |
| 3292 | if (cb->cutoff_time) |
| 3293 | *cb->cutoff_time = timestamp; |
| 3294 | if (cb->cutoff_tz) |
| 3295 | *cb->cutoff_tz = tz; |
| 3296 | if (cb->cutoff_cnt) |
| 3297 | *cb->cutoff_cnt = cb->reccnt - 1; |
| 3298 | /* |
| 3299 | * we have not yet updated cb->[n|o]sha1 so they still |
| 3300 | * hold the values for the previous record. |
| 3301 | */ |
| 3302 | if (!is_null_sha1(cb->osha1)) { |
| 3303 | hashcpy(cb->sha1, nsha1); |
| 3304 | if (hashcmp(cb->osha1, nsha1)) |
| 3305 | warning("Log for ref %s has gap after %s.", |
| 3306 | cb->refname, show_date(cb->date, cb->tz, DATE_RFC2822)); |
| 3307 | } |
| 3308 | else if (cb->date == cb->at_time) |
| 3309 | hashcpy(cb->sha1, nsha1); |
| 3310 | else if (hashcmp(nsha1, cb->sha1)) |
| 3311 | warning("Log for ref %s unexpectedly ended on %s.", |
| 3312 | cb->refname, show_date(cb->date, cb->tz, |
| 3313 | DATE_RFC2822)); |
| 3314 | hashcpy(cb->osha1, osha1); |
| 3315 | hashcpy(cb->nsha1, nsha1); |
| 3316 | cb->found_it = 1; |
| 3317 | return 1; |
| 3318 | } |
| 3319 | hashcpy(cb->osha1, osha1); |
| 3320 | hashcpy(cb->nsha1, nsha1); |
| 3321 | if (cb->cnt > 0) |
| 3322 | cb->cnt--; |
| 3323 | return 0; |
| 3324 | } |
| 3325 | |
| 3326 | static int read_ref_at_ent_oldest(unsigned char *osha1, unsigned char *nsha1, |
| 3327 | const char *email, unsigned long timestamp, |
| 3328 | int tz, const char *message, void *cb_data) |
| 3329 | { |
| 3330 | struct read_ref_at_cb *cb = cb_data; |
| 3331 | |
| 3332 | if (cb->msg) |
| 3333 | *cb->msg = xstrdup(message); |
| 3334 | if (cb->cutoff_time) |
| 3335 | *cb->cutoff_time = timestamp; |
| 3336 | if (cb->cutoff_tz) |
| 3337 | *cb->cutoff_tz = tz; |
| 3338 | if (cb->cutoff_cnt) |
| 3339 | *cb->cutoff_cnt = cb->reccnt; |
| 3340 | hashcpy(cb->sha1, osha1); |
| 3341 | if (is_null_sha1(cb->sha1)) |
| 3342 | hashcpy(cb->sha1, nsha1); |
| 3343 | /* We just want the first entry */ |
| 3344 | return 1; |
| Junio C Hamano | 16d7cc9 | 2007-01-19 09:19:05 | [diff] [blame] | 3345 | } |
| 3346 | |
| David Aguilar | c41a87d | 2014-09-19 03:45:37 | [diff] [blame] | 3347 | int read_ref_at(const char *refname, unsigned int flags, unsigned long at_time, int cnt, |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3348 | unsigned char *sha1, char **msg, |
| 3349 | unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt) |
| Shawn Pearce | d556fae | 2006-05-17 09:56:09 | [diff] [blame] | 3350 | { |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3351 | struct read_ref_at_cb cb; |
| Shawn Pearce | d556fae | 2006-05-17 09:56:09 | [diff] [blame] | 3352 | |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3353 | memset(&cb, 0, sizeof(cb)); |
| 3354 | cb.refname = refname; |
| 3355 | cb.at_time = at_time; |
| 3356 | cb.cnt = cnt; |
| 3357 | cb.msg = msg; |
| 3358 | cb.cutoff_time = cutoff_time; |
| 3359 | cb.cutoff_tz = cutoff_tz; |
| 3360 | cb.cutoff_cnt = cutoff_cnt; |
| 3361 | cb.sha1 = sha1; |
| Shawn Pearce | d556fae | 2006-05-17 09:56:09 | [diff] [blame] | 3362 | |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3363 | for_each_reflog_ent_reverse(refname, read_ref_at_ent, &cb); |
| Shawn Pearce | d556fae | 2006-05-17 09:56:09 | [diff] [blame] | 3364 | |
| David Aguilar | c41a87d | 2014-09-19 03:45:37 | [diff] [blame] | 3365 | if (!cb.reccnt) { |
| 3366 | if (flags & GET_SHA1_QUIETLY) |
| 3367 | exit(128); |
| 3368 | else |
| 3369 | die("Log for %s is empty.", refname); |
| 3370 | } |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3371 | if (cb.found_it) |
| 3372 | return 0; |
| Junio C Hamano | 16d7cc9 | 2007-01-19 09:19:05 | [diff] [blame] | 3373 | |
| Ronnie Sahlberg | 4207ed2 | 2014-06-03 16:09:59 | [diff] [blame] | 3374 | for_each_reflog_ent(refname, read_ref_at_ent_oldest, &cb); |
| 3375 | |
| Junio C Hamano | 16d7cc9 | 2007-01-19 09:19:05 | [diff] [blame] | 3376 | return 1; |
| Shawn Pearce | d556fae | 2006-05-17 09:56:09 | [diff] [blame] | 3377 | } |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3378 | |
| Ronnie Sahlberg | 4da5883 | 2014-05-06 22:45:52 | [diff] [blame] | 3379 | int reflog_exists(const char *refname) |
| 3380 | { |
| 3381 | struct stat st; |
| 3382 | |
| 3383 | return !lstat(git_path("logs/%s", refname), &st) && |
| 3384 | S_ISREG(st.st_mode); |
| 3385 | } |
| 3386 | |
| 3387 | int delete_reflog(const char *refname) |
| 3388 | { |
| 3389 | return remove_path(git_path("logs/%s", refname)); |
| 3390 | } |
| 3391 | |
| Junio C Hamano | 9a7a183 | 2013-03-08 18:36:43 | [diff] [blame] | 3392 | static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data) |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3393 | { |
| Junio C Hamano | 9a7a183 | 2013-03-08 18:36:43 | [diff] [blame] | 3394 | unsigned char osha1[20], nsha1[20]; |
| 3395 | char *email_end, *message; |
| 3396 | unsigned long timestamp; |
| 3397 | int tz; |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3398 | |
| Junio C Hamano | 9a7a183 | 2013-03-08 18:36:43 | [diff] [blame] | 3399 | /* old SP new SP name <email> SP time TAB msg LF */ |
| 3400 | if (sb->len < 83 || sb->buf[sb->len - 1] != '\n' || |
| 3401 | get_sha1_hex(sb->buf, osha1) || sb->buf[40] != ' ' || |
| 3402 | get_sha1_hex(sb->buf + 41, nsha1) || sb->buf[81] != ' ' || |
| 3403 | !(email_end = strchr(sb->buf + 82, '>')) || |
| 3404 | email_end[1] != ' ' || |
| 3405 | !(timestamp = strtoul(email_end + 2, &message, 10)) || |
| 3406 | !message || message[0] != ' ' || |
| 3407 | (message[1] != '+' && message[1] != '-') || |
| 3408 | !isdigit(message[2]) || !isdigit(message[3]) || |
| 3409 | !isdigit(message[4]) || !isdigit(message[5])) |
| 3410 | return 0; /* corrupt? */ |
| 3411 | email_end[1] = '\0'; |
| 3412 | tz = strtol(message + 1, NULL, 10); |
| 3413 | if (message[6] != '\t') |
| 3414 | message += 6; |
| 3415 | else |
| 3416 | message += 7; |
| 3417 | return fn(osha1, nsha1, sb->buf + 82, timestamp, tz, message, cb_data); |
| 3418 | } |
| 3419 | |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3420 | static char *find_beginning_of_line(char *bob, char *scan) |
| 3421 | { |
| 3422 | while (bob < scan && *(--scan) != '\n') |
| 3423 | ; /* keep scanning backwards */ |
| 3424 | /* |
| 3425 | * Return either beginning of the buffer, or LF at the end of |
| 3426 | * the previous line. |
| 3427 | */ |
| 3428 | return scan; |
| 3429 | } |
| 3430 | |
| 3431 | int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data) |
| 3432 | { |
| 3433 | struct strbuf sb = STRBUF_INIT; |
| 3434 | FILE *logfp; |
| 3435 | long pos; |
| 3436 | int ret = 0, at_tail = 1; |
| 3437 | |
| 3438 | logfp = fopen(git_path("logs/%s", refname), "r"); |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3439 | if (!logfp) |
| Johannes Schindelin | 883d60f | 2007-01-08 00:59:54 | [diff] [blame] | 3440 | return -1; |
| Junio C Hamano | 101d15e | 2009-01-20 06:18:29 | [diff] [blame] | 3441 | |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3442 | /* Jump to the end */ |
| 3443 | if (fseek(logfp, 0, SEEK_END) < 0) |
| 3444 | return error("cannot seek back reflog for %s: %s", |
| 3445 | refname, strerror(errno)); |
| 3446 | pos = ftell(logfp); |
| 3447 | while (!ret && 0 < pos) { |
| 3448 | int cnt; |
| 3449 | size_t nread; |
| 3450 | char buf[BUFSIZ]; |
| 3451 | char *endp, *scanp; |
| 3452 | |
| 3453 | /* Fill next block from the end */ |
| 3454 | cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; |
| 3455 | if (fseek(logfp, pos - cnt, SEEK_SET)) |
| 3456 | return error("cannot seek back reflog for %s: %s", |
| 3457 | refname, strerror(errno)); |
| 3458 | nread = fread(buf, cnt, 1, logfp); |
| John Keeping | e4ca819 | 2013-03-23 17:16:46 | [diff] [blame] | 3459 | if (nread != 1) |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3460 | return error("cannot read %d bytes from reflog for %s: %s", |
| 3461 | cnt, refname, strerror(errno)); |
| 3462 | pos -= cnt; |
| 3463 | |
| 3464 | scanp = endp = buf + cnt; |
| 3465 | if (at_tail && scanp[-1] == '\n') |
| 3466 | /* Looking at the final LF at the end of the file */ |
| 3467 | scanp--; |
| 3468 | at_tail = 0; |
| 3469 | |
| 3470 | while (buf < scanp) { |
| 3471 | /* |
| 3472 | * terminating LF of the previous line, or the beginning |
| 3473 | * of the buffer. |
| 3474 | */ |
| 3475 | char *bp; |
| 3476 | |
| 3477 | bp = find_beginning_of_line(buf, scanp); |
| 3478 | |
| Jeff King | e5e73ff | 2014-12-05 01:28:54 | [diff] [blame] | 3479 | if (*bp == '\n') { |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3480 | /* |
| Jeff King | e5e73ff | 2014-12-05 01:28:54 | [diff] [blame] | 3481 | * The newline is the end of the previous line, |
| 3482 | * so we know we have complete line starting |
| 3483 | * at (bp + 1). Prefix it onto any prior data |
| 3484 | * we collected for the line and process it. |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3485 | */ |
| 3486 | strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1)); |
| 3487 | scanp = bp; |
| 3488 | endp = bp + 1; |
| Jeff King | e5e73ff | 2014-12-05 01:28:54 | [diff] [blame] | 3489 | ret = show_one_reflog_ent(&sb, fn, cb_data); |
| 3490 | strbuf_reset(&sb); |
| 3491 | if (ret) |
| 3492 | break; |
| 3493 | } else if (!pos) { |
| 3494 | /* |
| 3495 | * We are at the start of the buffer, and the |
| 3496 | * start of the file; there is no previous |
| 3497 | * line, and we have everything for this one. |
| 3498 | * Process it, and we can end the loop. |
| 3499 | */ |
| 3500 | strbuf_splice(&sb, 0, 0, buf, endp - buf); |
| 3501 | ret = show_one_reflog_ent(&sb, fn, cb_data); |
| 3502 | strbuf_reset(&sb); |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3503 | break; |
| Jeff King | e5e73ff | 2014-12-05 01:28:54 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | if (bp == buf) { |
| 3507 | /* |
| 3508 | * We are at the start of the buffer, and there |
| 3509 | * is more file to read backwards. Which means |
| 3510 | * we are in the middle of a line. Note that we |
| 3511 | * may get here even if *bp was a newline; that |
| 3512 | * just means we are at the exact end of the |
| 3513 | * previous line, rather than some spot in the |
| 3514 | * middle. |
| 3515 | * |
| 3516 | * Save away what we have to be combined with |
| 3517 | * the data from the next read. |
| 3518 | */ |
| 3519 | strbuf_splice(&sb, 0, 0, buf, endp - buf); |
| 3520 | break; |
| 3521 | } |
| Brandon Casey | 9d33f7c | 2009-07-16 21:25:18 | [diff] [blame] | 3522 | } |
| Junio C Hamano | 101d15e | 2009-01-20 06:18:29 | [diff] [blame] | 3523 | |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3524 | } |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3525 | if (!ret && sb.len) |
| Jeff King | 69216bf | 2014-12-05 01:32:44 | [diff] [blame] | 3526 | die("BUG: reverse reflog parser had leftover data"); |
| Junio C Hamano | 98f85ff | 2013-03-08 21:27:37 | [diff] [blame] | 3527 | |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3528 | fclose(logfp); |
| René Scharfe | 8ca7880 | 2010-03-13 17:37:50 | [diff] [blame] | 3529 | strbuf_release(&sb); |
| Junio C Hamano | 2266bf2 | 2007-01-19 07:25:54 | [diff] [blame] | 3530 | return ret; |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3531 | } |
| Junio C Hamano | e29cb53 | 2006-12-19 06:07:45 | [diff] [blame] | 3532 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3533 | int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data) |
| Junio C Hamano | 101d15e | 2009-01-20 06:18:29 | [diff] [blame] | 3534 | { |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3535 | FILE *logfp; |
| René Scharfe | 8ca7880 | 2010-03-13 17:37:50 | [diff] [blame] | 3536 | struct strbuf sb = STRBUF_INIT; |
| Junio C Hamano | 2266bf2 | 2007-01-19 07:25:54 | [diff] [blame] | 3537 | int ret = 0; |
| Junio C Hamano | 101d15e | 2009-01-20 06:18:29 | [diff] [blame] | 3538 | |
| Junio C Hamano | 7ae07c1 | 2013-03-08 18:45:25 | [diff] [blame] | 3539 | logfp = fopen(git_path("logs/%s", refname), "r"); |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3540 | if (!logfp) |
| Johannes Schindelin | 883d60f | 2007-01-08 00:59:54 | [diff] [blame] | 3541 | return -1; |
| Junio C Hamano | 101d15e | 2009-01-20 06:18:29 | [diff] [blame] | 3542 | |
| Junio C Hamano | 9a7a183 | 2013-03-08 18:36:43 | [diff] [blame] | 3543 | while (!ret && !strbuf_getwholeline(&sb, logfp, '\n')) |
| 3544 | ret = show_one_reflog_ent(&sb, fn, cb_data); |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3545 | fclose(logfp); |
| René Scharfe | 8ca7880 | 2010-03-13 17:37:50 | [diff] [blame] | 3546 | strbuf_release(&sb); |
| Junio C Hamano | 2266bf2 | 2007-01-19 07:25:54 | [diff] [blame] | 3547 | return ret; |
| Junio C Hamano | 2ff8166 | 2006-12-18 09:18:16 | [diff] [blame] | 3548 | } |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3549 | /* |
| 3550 | * Call fn for each reflog in the namespace indicated by name. name |
| 3551 | * must be empty or end with '/'. Name will be used as a scratch |
| 3552 | * space, but its contents will be restored before return. |
| 3553 | */ |
| 3554 | static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data) |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3555 | { |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3556 | DIR *d = opendir(git_path("logs/%s", name->buf)); |
| Junio C Hamano | fcee5a1 | 2007-02-07 17:18:57 | [diff] [blame] | 3557 | int retval = 0; |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3558 | struct dirent *de; |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3559 | int oldlen = name->len; |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3560 | |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3561 | if (!d) |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3562 | return name->len ? errno : 0; |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3563 | |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3564 | while ((de = readdir(d)) != NULL) { |
| 3565 | struct stat st; |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3566 | |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3567 | if (de->d_name[0] == '.') |
| 3568 | continue; |
| Jeff King | 2975c77 | 2014-06-30 16:58:25 | [diff] [blame] | 3569 | if (ends_with(de->d_name, ".lock")) |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3570 | continue; |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3571 | strbuf_addstr(name, de->d_name); |
| 3572 | if (stat(git_path("logs/%s", name->buf), &st) < 0) { |
| 3573 | ; /* silently ignore */ |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3574 | } else { |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3575 | if (S_ISDIR(st.st_mode)) { |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3576 | strbuf_addch(name, '/'); |
| 3577 | retval = do_for_each_reflog(name, fn, cb_data); |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3578 | } else { |
| 3579 | unsigned char sha1[20]; |
| Ronnie Sahlberg | 7695d11 | 2014-07-15 19:59:36 | [diff] [blame] | 3580 | if (read_ref_full(name->buf, 0, sha1, NULL)) |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3581 | retval = error("bad ref for %s", name->buf); |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3582 | else |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3583 | retval = fn(name->buf, sha1, 0, cb_data); |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3584 | } |
| 3585 | if (retval) |
| 3586 | break; |
| 3587 | } |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3588 | strbuf_setlen(name, oldlen); |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3589 | } |
| Michael Haggerty | 93c603f | 2012-04-24 22:45:13 | [diff] [blame] | 3590 | closedir(d); |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3591 | return retval; |
| 3592 | } |
| 3593 | |
| 3594 | int for_each_reflog(each_ref_fn fn, void *cb_data) |
| 3595 | { |
| Michael Haggerty | 989c0e5 | 2012-04-24 22:45:14 | [diff] [blame] | 3596 | int retval; |
| 3597 | struct strbuf name; |
| 3598 | strbuf_init(&name, PATH_MAX); |
| 3599 | retval = do_for_each_reflog(&name, fn, cb_data); |
| 3600 | strbuf_release(&name); |
| 3601 | return retval; |
| Nicolas Pitre | eb8381c | 2007-02-03 18:25:43 | [diff] [blame] | 3602 | } |
| Carlos Rica | 3d9f037 | 2007-09-05 01:38:24 | [diff] [blame] | 3603 | |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3604 | /** |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 3605 | * Information needed for a single ref update. Set new_sha1 to the new |
| 3606 | * value or to null_sha1 to delete the ref. To check the old value |
| 3607 | * while the ref is locked, set (flags & REF_HAVE_OLD) and set |
| 3608 | * old_sha1 to the old value, or to null_sha1 to ensure the ref does |
| 3609 | * not exist before update. |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3610 | */ |
| 3611 | struct ref_update { |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 3612 | /* |
| 3613 | * If (flags & REF_HAVE_NEW), set the reference to this value: |
| 3614 | */ |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3615 | unsigned char new_sha1[20]; |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 3616 | /* |
| 3617 | * If (flags & REF_HAVE_OLD), check that the reference |
| 3618 | * previously had this value: |
| 3619 | */ |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3620 | unsigned char old_sha1[20]; |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 3621 | /* |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 3622 | * One or more of REF_HAVE_NEW, REF_HAVE_OLD, REF_NODEREF, |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 3623 | * REF_DELETING, and REF_ISPRUNING: |
| 3624 | */ |
| 3625 | unsigned int flags; |
| Michael Haggerty | 81c960e | 2014-04-07 13:48:16 | [diff] [blame] | 3626 | struct ref_lock *lock; |
| Michael Haggerty | 84178db | 2014-04-07 13:48:17 | [diff] [blame] | 3627 | int type; |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 3628 | char *msg; |
| Michael Haggerty | 8861591 | 2014-04-07 13:48:14 | [diff] [blame] | 3629 | const char refname[FLEX_ARRAY]; |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3630 | }; |
| 3631 | |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3632 | /* |
| Ronnie Sahlberg | 2bdc785 | 2014-04-29 19:06:19 | [diff] [blame] | 3633 | * Transaction states. |
| 3634 | * OPEN: The transaction is in a valid state and can accept new updates. |
| 3635 | * An OPEN transaction can be committed. |
| 3636 | * CLOSED: A closed transaction is no longer active and no other operations |
| 3637 | * than free can be used on it in this state. |
| 3638 | * A transaction can either become closed by successfully committing |
| 3639 | * an active transaction or if there is a failure while building |
| 3640 | * the transaction thus rendering it failed/inactive. |
| 3641 | */ |
| 3642 | enum ref_transaction_state { |
| 3643 | REF_TRANSACTION_OPEN = 0, |
| 3644 | REF_TRANSACTION_CLOSED = 1 |
| 3645 | }; |
| 3646 | |
| 3647 | /* |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3648 | * Data structure for holding a reference transaction, which can |
| 3649 | * consist of checks and updates to multiple references, carried out |
| 3650 | * as atomically as possible. This structure is opaque to callers. |
| 3651 | */ |
| 3652 | struct ref_transaction { |
| 3653 | struct ref_update **updates; |
| 3654 | size_t alloc; |
| 3655 | size_t nr; |
| Ronnie Sahlberg | 2bdc785 | 2014-04-29 19:06:19 | [diff] [blame] | 3656 | enum ref_transaction_state state; |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3657 | }; |
| 3658 | |
| Ronnie Sahlberg | 93a644e | 2014-05-19 17:42:34 | [diff] [blame] | 3659 | struct ref_transaction *ref_transaction_begin(struct strbuf *err) |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3660 | { |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 3661 | assert(err); |
| 3662 | |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3663 | return xcalloc(1, sizeof(struct ref_transaction)); |
| 3664 | } |
| 3665 | |
| Ronnie Sahlberg | 026bd1d | 2014-06-20 14:42:42 | [diff] [blame] | 3666 | void ref_transaction_free(struct ref_transaction *transaction) |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3667 | { |
| 3668 | int i; |
| 3669 | |
| Ronnie Sahlberg | 1b07255 | 2014-06-20 14:42:45 | [diff] [blame] | 3670 | if (!transaction) |
| 3671 | return; |
| 3672 | |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 3673 | for (i = 0; i < transaction->nr; i++) { |
| 3674 | free(transaction->updates[i]->msg); |
| Michael Haggerty | 8861591 | 2014-04-07 13:48:14 | [diff] [blame] | 3675 | free(transaction->updates[i]); |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 3676 | } |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3677 | free(transaction->updates); |
| 3678 | free(transaction); |
| 3679 | } |
| 3680 | |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3681 | static struct ref_update *add_update(struct ref_transaction *transaction, |
| 3682 | const char *refname) |
| 3683 | { |
| Michael Haggerty | 8861591 | 2014-04-07 13:48:14 | [diff] [blame] | 3684 | size_t len = strlen(refname); |
| 3685 | struct ref_update *update = xcalloc(1, sizeof(*update) + len + 1); |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3686 | |
| Michael Haggerty | 8861591 | 2014-04-07 13:48:14 | [diff] [blame] | 3687 | strcpy((char *)update->refname, refname); |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3688 | ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); |
| 3689 | transaction->updates[transaction->nr++] = update; |
| 3690 | return update; |
| 3691 | } |
| 3692 | |
| Ronnie Sahlberg | 8e34800 | 2014-06-20 14:43:00 | [diff] [blame] | 3693 | int ref_transaction_update(struct ref_transaction *transaction, |
| 3694 | const char *refname, |
| 3695 | const unsigned char *new_sha1, |
| 3696 | const unsigned char *old_sha1, |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 3697 | unsigned int flags, const char *msg, |
| Ronnie Sahlberg | 8e34800 | 2014-06-20 14:43:00 | [diff] [blame] | 3698 | struct strbuf *err) |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3699 | { |
| Ronnie Sahlberg | 8e34800 | 2014-06-20 14:43:00 | [diff] [blame] | 3700 | struct ref_update *update; |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3701 | |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 3702 | assert(err); |
| 3703 | |
| Ronnie Sahlberg | 2bdc785 | 2014-04-29 19:06:19 | [diff] [blame] | 3704 | if (transaction->state != REF_TRANSACTION_OPEN) |
| 3705 | die("BUG: update called for transaction that is not open"); |
| 3706 | |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 3707 | if (new_sha1 && !is_null_sha1(new_sha1) && |
| Ronnie Sahlberg | d0f810f | 2014-09-03 18:45:43 | [diff] [blame] | 3708 | check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { |
| 3709 | strbuf_addf(err, "refusing to update ref with bad name %s", |
| 3710 | refname); |
| 3711 | return -1; |
| 3712 | } |
| 3713 | |
| Ronnie Sahlberg | 8e34800 | 2014-06-20 14:43:00 | [diff] [blame] | 3714 | update = add_update(transaction, refname); |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 3715 | if (new_sha1) { |
| 3716 | hashcpy(update->new_sha1, new_sha1); |
| 3717 | flags |= REF_HAVE_NEW; |
| 3718 | } |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 3719 | if (old_sha1) { |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3720 | hashcpy(update->old_sha1, old_sha1); |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 3721 | flags |= REF_HAVE_OLD; |
| 3722 | } |
| 3723 | update->flags = flags; |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 3724 | if (msg) |
| 3725 | update->msg = xstrdup(msg); |
| Ronnie Sahlberg | 8e34800 | 2014-06-20 14:43:00 | [diff] [blame] | 3726 | return 0; |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3727 | } |
| 3728 | |
| Ronnie Sahlberg | b416af5 | 2014-04-16 22:26:44 | [diff] [blame] | 3729 | int ref_transaction_create(struct ref_transaction *transaction, |
| 3730 | const char *refname, |
| 3731 | const unsigned char *new_sha1, |
| Michael Haggerty | fec14ec | 2015-02-17 17:00:13 | [diff] [blame] | 3732 | unsigned int flags, const char *msg, |
| Ronnie Sahlberg | b416af5 | 2014-04-16 22:26:44 | [diff] [blame] | 3733 | struct strbuf *err) |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3734 | { |
| Michael Haggerty | f04c5b5 | 2015-02-17 17:00:19 | [diff] [blame] | 3735 | if (!new_sha1 || is_null_sha1(new_sha1)) |
| 3736 | die("BUG: create called without valid new_sha1"); |
| Ronnie Sahlberg | bc9f292 | 2014-12-04 23:08:13 | [diff] [blame] | 3737 | return ref_transaction_update(transaction, refname, new_sha1, |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 3738 | null_sha1, flags, msg, err); |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3739 | } |
| 3740 | |
| Ronnie Sahlberg | 8c8bdc0 | 2014-04-16 22:27:45 | [diff] [blame] | 3741 | int ref_transaction_delete(struct ref_transaction *transaction, |
| 3742 | const char *refname, |
| 3743 | const unsigned char *old_sha1, |
| Michael Haggerty | fb5a6bb | 2015-02-17 17:00:16 | [diff] [blame] | 3744 | unsigned int flags, const char *msg, |
| Ronnie Sahlberg | 8c8bdc0 | 2014-04-16 22:27:45 | [diff] [blame] | 3745 | struct strbuf *err) |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3746 | { |
| Michael Haggerty | 6029459 | 2015-02-17 17:00:20 | [diff] [blame] | 3747 | if (old_sha1 && is_null_sha1(old_sha1)) |
| 3748 | die("BUG: delete called with old_sha1 set to zeros"); |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 3749 | return ref_transaction_update(transaction, refname, |
| Michael Haggerty | fb5a6bb | 2015-02-17 17:00:16 | [diff] [blame] | 3750 | null_sha1, old_sha1, |
| Michael Haggerty | 1d147bd | 2015-02-17 17:00:15 | [diff] [blame] | 3751 | flags, msg, err); |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3752 | } |
| 3753 | |
| Michael Haggerty | 1618033 | 2015-02-17 17:00:21 | [diff] [blame] | 3754 | int ref_transaction_verify(struct ref_transaction *transaction, |
| 3755 | const char *refname, |
| 3756 | const unsigned char *old_sha1, |
| 3757 | unsigned int flags, |
| 3758 | struct strbuf *err) |
| 3759 | { |
| 3760 | if (!old_sha1) |
| 3761 | die("BUG: verify called with old_sha1 set to NULL"); |
| 3762 | return ref_transaction_update(transaction, refname, |
| 3763 | NULL, old_sha1, |
| 3764 | flags, NULL, err); |
| 3765 | } |
| 3766 | |
| Michael Haggerty | 4b7b520 | 2015-02-17 17:00:22 | [diff] [blame] | 3767 | int update_ref(const char *msg, const char *refname, |
| 3768 | const unsigned char *new_sha1, const unsigned char *old_sha1, |
| Michael Haggerty | fec14ec | 2015-02-17 17:00:13 | [diff] [blame] | 3769 | unsigned int flags, enum action_on_err onerr) |
| Brad King | 4738a33 | 2013-09-04 15:22:40 | [diff] [blame] | 3770 | { |
| Ronnie Sahlberg | b4d75ac | 2014-04-24 23:36:55 | [diff] [blame] | 3771 | struct ref_transaction *t; |
| 3772 | struct strbuf err = STRBUF_INIT; |
| 3773 | |
| 3774 | t = ref_transaction_begin(&err); |
| 3775 | if (!t || |
| Michael Haggerty | 4b7b520 | 2015-02-17 17:00:22 | [diff] [blame] | 3776 | ref_transaction_update(t, refname, new_sha1, old_sha1, |
| 3777 | flags, msg, &err) || |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 3778 | ref_transaction_commit(t, &err)) { |
| Ronnie Sahlberg | b4d75ac | 2014-04-24 23:36:55 | [diff] [blame] | 3779 | const char *str = "update_ref failed for ref '%s': %s"; |
| 3780 | |
| 3781 | ref_transaction_free(t); |
| 3782 | switch (onerr) { |
| 3783 | case UPDATE_REFS_MSG_ON_ERR: |
| 3784 | error(str, refname, err.buf); |
| 3785 | break; |
| 3786 | case UPDATE_REFS_DIE_ON_ERR: |
| 3787 | die(str, refname, err.buf); |
| 3788 | break; |
| 3789 | case UPDATE_REFS_QUIET_ON_ERR: |
| 3790 | break; |
| 3791 | } |
| 3792 | strbuf_release(&err); |
| Brad King | 4738a33 | 2013-09-04 15:22:40 | [diff] [blame] | 3793 | return 1; |
| Ronnie Sahlberg | b4d75ac | 2014-04-24 23:36:55 | [diff] [blame] | 3794 | } |
| 3795 | strbuf_release(&err); |
| 3796 | ref_transaction_free(t); |
| 3797 | return 0; |
| Brad King | 4738a33 | 2013-09-04 15:22:40 | [diff] [blame] | 3798 | } |
| 3799 | |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3800 | static int ref_update_reject_duplicates(struct string_list *refnames, |
| Ronnie Sahlberg | 01319837 | 2014-06-20 14:42:59 | [diff] [blame] | 3801 | struct strbuf *err) |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3802 | { |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3803 | int i, n = refnames->nr; |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 3804 | |
| 3805 | assert(err); |
| 3806 | |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3807 | for (i = 1; i < n; i++) |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3808 | if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) { |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 3809 | strbuf_addf(err, |
| 3810 | "Multiple updates for ref '%s' not allowed.", |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3811 | refnames->items[i].string); |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3812 | return 1; |
| 3813 | } |
| 3814 | return 0; |
| 3815 | } |
| 3816 | |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3817 | int ref_transaction_commit(struct ref_transaction *transaction, |
| Ronnie Sahlberg | db7516a | 2014-04-30 19:22:42 | [diff] [blame] | 3818 | struct strbuf *err) |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3819 | { |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 3820 | int ret = 0, i; |
| Michael Haggerty | b5c8ea2 | 2014-04-07 13:48:12 | [diff] [blame] | 3821 | int n = transaction->nr; |
| Michael Haggerty | 6a40233 | 2014-04-07 13:48:18 | [diff] [blame] | 3822 | struct ref_update **updates = transaction->updates; |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 3823 | struct string_list refs_to_delete = STRING_LIST_INIT_NODUP; |
| 3824 | struct string_list_item *ref_to_delete; |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3825 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3826 | |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 3827 | assert(err); |
| 3828 | |
| Ronnie Sahlberg | 2bdc785 | 2014-04-29 19:06:19 | [diff] [blame] | 3829 | if (transaction->state != REF_TRANSACTION_OPEN) |
| 3830 | die("BUG: commit called for transaction that is not open"); |
| 3831 | |
| 3832 | if (!n) { |
| 3833 | transaction->state = REF_TRANSACTION_CLOSED; |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3834 | return 0; |
| Ronnie Sahlberg | 2bdc785 | 2014-04-29 19:06:19 | [diff] [blame] | 3835 | } |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3836 | |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3837 | /* Fail if a refname appears more than once in the transaction: */ |
| 3838 | for (i = 0; i < n; i++) |
| 3839 | string_list_append(&affected_refnames, updates[i]->refname); |
| 3840 | string_list_sort(&affected_refnames); |
| 3841 | if (ref_update_reject_duplicates(&affected_refnames, err)) { |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3842 | ret = TRANSACTION_GENERIC_ERROR; |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3843 | goto cleanup; |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3844 | } |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3845 | |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 3846 | /* |
| 3847 | * Acquire all locks, verify old values if provided, check |
| 3848 | * that new values are valid, and write new values to the |
| 3849 | * lockfiles, ready to be activated. Only keep one lockfile |
| 3850 | * open at a time to avoid running out of file descriptors. |
| 3851 | */ |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3852 | for (i = 0; i < n; i++) { |
| Michael Haggerty | cb198d2 | 2014-04-07 13:48:15 | [diff] [blame] | 3853 | struct ref_update *update = updates[i]; |
| Michael Haggerty | cb198d2 | 2014-04-07 13:48:15 | [diff] [blame] | 3854 | |
| Michael Haggerty | cbf50f9 | 2015-04-24 11:35:48 | [diff] [blame] | 3855 | if ((update->flags & REF_HAVE_NEW) && |
| 3856 | is_null_sha1(update->new_sha1)) |
| 3857 | update->flags |= REF_DELETING; |
| Michael Haggerty | 8df4e51 | 2015-02-17 17:00:14 | [diff] [blame] | 3858 | update->lock = lock_ref_sha1_basic( |
| 3859 | update->refname, |
| 3860 | ((update->flags & REF_HAVE_OLD) ? |
| 3861 | update->old_sha1 : NULL), |
| Michael Haggerty | e911104 | 2015-05-11 15:25:12 | [diff] [blame] | 3862 | &affected_refnames, NULL, |
| Michael Haggerty | cbf50f9 | 2015-04-24 11:35:48 | [diff] [blame] | 3863 | update->flags, |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 3864 | &update->type, |
| 3865 | err); |
| Michael Haggerty | 81c960e | 2014-04-07 13:48:16 | [diff] [blame] | 3866 | if (!update->lock) { |
| Michael Haggerty | cbaabcb | 2015-05-11 15:25:18 | [diff] [blame] | 3867 | char *reason; |
| 3868 | |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3869 | ret = (errno == ENOTDIR) |
| 3870 | ? TRANSACTION_NAME_CONFLICT |
| 3871 | : TRANSACTION_GENERIC_ERROR; |
| Michael Haggerty | cbaabcb | 2015-05-11 15:25:18 | [diff] [blame] | 3872 | reason = strbuf_detach(err, NULL); |
| Michael Haggerty | 3553944 | 2015-05-11 15:25:19 | [diff] [blame] | 3873 | strbuf_addf(err, "Cannot lock ref '%s': %s", |
| Michael Haggerty | cbaabcb | 2015-05-11 15:25:18 | [diff] [blame] | 3874 | update->refname, reason); |
| 3875 | free(reason); |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3876 | goto cleanup; |
| 3877 | } |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 3878 | if ((update->flags & REF_HAVE_NEW) && |
| 3879 | !(update->flags & REF_DELETING)) { |
| Stefan Beller | 5a6f470 | 2015-03-03 11:43:14 | [diff] [blame] | 3880 | int overwriting_symref = ((update->type & REF_ISSYMREF) && |
| 3881 | (update->flags & REF_NODEREF)); |
| 3882 | |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 3883 | if (!overwriting_symref && |
| 3884 | !hashcmp(update->lock->old_sha1, update->new_sha1)) { |
| Stefan Beller | 5a6f470 | 2015-03-03 11:43:14 | [diff] [blame] | 3885 | /* |
| 3886 | * The reference already has the desired |
| 3887 | * value, so we don't need to write it. |
| 3888 | */ |
| Michael Haggerty | 61e51e0 | 2015-05-09 15:29:20 | [diff] [blame] | 3889 | } else if (write_ref_to_lockfile(update->lock, |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 3890 | update->new_sha1)) { |
| 3891 | /* |
| 3892 | * The lock was freed upon failure of |
| 3893 | * write_ref_to_lockfile(): |
| 3894 | */ |
| Michael Haggerty | 706d5f8 | 2015-03-02 09:29:52 | [diff] [blame] | 3895 | update->lock = NULL; |
| Jonathan Nieder | 5a603b0 | 2014-08-28 23:42:37 | [diff] [blame] | 3896 | strbuf_addf(err, "Cannot update the ref '%s'.", |
| 3897 | update->refname); |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3898 | ret = TRANSACTION_GENERIC_ERROR; |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3899 | goto cleanup; |
| Michael Haggerty | 706d5f8 | 2015-03-02 09:29:52 | [diff] [blame] | 3900 | } else { |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 3901 | update->flags |= REF_NEEDS_COMMIT; |
| 3902 | } |
| 3903 | } |
| 3904 | if (!(update->flags & REF_NEEDS_COMMIT)) { |
| 3905 | /* |
| 3906 | * We didn't have to write anything to the lockfile. |
| 3907 | * Close it to free up the file descriptor: |
| 3908 | */ |
| 3909 | if (close_ref(update->lock)) { |
| 3910 | strbuf_addf(err, "Couldn't close %s.lock", |
| 3911 | update->refname); |
| 3912 | goto cleanup; |
| 3913 | } |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | /* Perform updates first so live commits remain referenced */ |
| 3918 | for (i = 0; i < n; i++) { |
| 3919 | struct ref_update *update = updates[i]; |
| 3920 | |
| 3921 | if (update->flags & REF_NEEDS_COMMIT) { |
| 3922 | if (commit_ref_update(update->lock, |
| 3923 | update->new_sha1, update->msg)) { |
| 3924 | /* freed by commit_ref_update(): */ |
| 3925 | update->lock = NULL; |
| 3926 | strbuf_addf(err, "Cannot update the ref '%s'.", |
| 3927 | update->refname); |
| 3928 | ret = TRANSACTION_GENERIC_ERROR; |
| 3929 | goto cleanup; |
| 3930 | } else { |
| 3931 | /* freed by commit_ref_update(): */ |
| Michael Haggerty | 706d5f8 | 2015-03-02 09:29:52 | [diff] [blame] | 3932 | update->lock = NULL; |
| Ronnie Sahlberg | 04ad622 | 2014-04-29 20:42:07 | [diff] [blame] | 3933 | } |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3934 | } |
| Michael Haggerty | cb198d2 | 2014-04-07 13:48:15 | [diff] [blame] | 3935 | } |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3936 | |
| 3937 | /* Perform deletes now that updates are safely completed */ |
| Michael Haggerty | 81c960e | 2014-04-07 13:48:16 | [diff] [blame] | 3938 | for (i = 0; i < n; i++) { |
| 3939 | struct ref_update *update = updates[i]; |
| 3940 | |
| Michael Haggerty | cf018ee | 2015-04-24 11:35:49 | [diff] [blame] | 3941 | if (update->flags & REF_DELETING) { |
| Jonathan Nieder | 6573284 | 2014-08-29 00:01:35 | [diff] [blame] | 3942 | if (delete_ref_loose(update->lock, update->type, err)) { |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3943 | ret = TRANSACTION_GENERIC_ERROR; |
| Jonathan Nieder | 6573284 | 2014-08-29 00:01:35 | [diff] [blame] | 3944 | goto cleanup; |
| 3945 | } |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3946 | |
| Michael Haggerty | cbf50f9 | 2015-04-24 11:35:48 | [diff] [blame] | 3947 | if (!(update->flags & REF_ISPRUNING)) |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 3948 | string_list_append(&refs_to_delete, |
| 3949 | update->lock->ref_name); |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3950 | } |
| Michael Haggerty | 81c960e | 2014-04-07 13:48:16 | [diff] [blame] | 3951 | } |
| 3952 | |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 3953 | if (repack_without_refs(&refs_to_delete, err)) { |
| Ronnie Sahlberg | 28e6a97 | 2014-05-16 21:14:38 | [diff] [blame] | 3954 | ret = TRANSACTION_GENERIC_ERROR; |
| Jonathan Nieder | 6573284 | 2014-08-29 00:01:35 | [diff] [blame] | 3955 | goto cleanup; |
| 3956 | } |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 3957 | for_each_string_list_item(ref_to_delete, &refs_to_delete) |
| 3958 | unlink_or_warn(git_path("logs/%s", ref_to_delete->string)); |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3959 | clear_loose_ref_cache(&ref_cache); |
| 3960 | |
| 3961 | cleanup: |
| Ronnie Sahlberg | 2bdc785 | 2014-04-29 19:06:19 | [diff] [blame] | 3962 | transaction->state = REF_TRANSACTION_CLOSED; |
| 3963 | |
| Brad King | 98aee92 | 2013-09-04 15:22:43 | [diff] [blame] | 3964 | for (i = 0; i < n; i++) |
| Michael Haggerty | 81c960e | 2014-04-07 13:48:16 | [diff] [blame] | 3965 | if (updates[i]->lock) |
| 3966 | unlock_ref(updates[i]->lock); |
| Michael Haggerty | 4a45b2f | 2014-11-25 08:02:32 | [diff] [blame] | 3967 | string_list_clear(&refs_to_delete, 0); |
| Michael Haggerty | 07f9c88 | 2015-05-11 15:25:11 | [diff] [blame] | 3968 | string_list_clear(&affected_refnames, 0); |
| Michael Haggerty | caa4046 | 2014-04-07 13:48:10 | [diff] [blame] | 3969 | return ret; |
| 3970 | } |
| 3971 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 3972 | char *shorten_unambiguous_ref(const char *refname, int strict) |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 3973 | { |
| 3974 | int i; |
| 3975 | static char **scanf_fmts; |
| 3976 | static int nr_rules; |
| 3977 | char *short_name; |
| 3978 | |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 3979 | if (!nr_rules) { |
| Michael Haggerty | 4346663 | 2014-01-08 14:43:39 | [diff] [blame] | 3980 | /* |
| 3981 | * Pre-generate scanf formats from ref_rev_parse_rules[]. |
| 3982 | * Generate a format suitable for scanf from a |
| 3983 | * ref_rev_parse_rules rule by interpolating "%s" at the |
| 3984 | * location of the "%.*s". |
| 3985 | */ |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 3986 | size_t total_len = 0; |
| Michael Haggerty | 84d5633 | 2014-01-08 14:43:38 | [diff] [blame] | 3987 | size_t offset = 0; |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 3988 | |
| 3989 | /* the rule list is NULL terminated, count them first */ |
| Jeff King | a416585 | 2013-10-24 08:45:13 | [diff] [blame] | 3990 | for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++) |
| Michael Haggerty | 7902fe0 | 2014-01-08 14:43:40 | [diff] [blame] | 3991 | /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */ |
| 3992 | total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1; |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 3993 | |
| 3994 | scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len); |
| 3995 | |
| Michael Haggerty | 84d5633 | 2014-01-08 14:43:38 | [diff] [blame] | 3996 | offset = 0; |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 3997 | for (i = 0; i < nr_rules; i++) { |
| Michael Haggerty | 4346663 | 2014-01-08 14:43:39 | [diff] [blame] | 3998 | assert(offset < total_len); |
| Michael Haggerty | 84d5633 | 2014-01-08 14:43:38 | [diff] [blame] | 3999 | scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset; |
| Michael Haggerty | 4346663 | 2014-01-08 14:43:39 | [diff] [blame] | 4000 | offset += snprintf(scanf_fmts[i], total_len - offset, |
| 4001 | ref_rev_parse_rules[i], 2, "%s") + 1; |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4002 | } |
| 4003 | } |
| 4004 | |
| 4005 | /* bail out if there are no rules */ |
| 4006 | if (!nr_rules) |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 4007 | return xstrdup(refname); |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4008 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 4009 | /* buffer for scanf result, at most refname must fit */ |
| 4010 | short_name = xstrdup(refname); |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4011 | |
| 4012 | /* skip first rule, it will always match */ |
| 4013 | for (i = nr_rules - 1; i > 0 ; --i) { |
| 4014 | int j; |
| Bert Wesarg | 6e7b330 | 2009-04-13 10:25:46 | [diff] [blame] | 4015 | int rules_to_fail = i; |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4016 | int short_name_len; |
| 4017 | |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 4018 | if (1 != sscanf(refname, scanf_fmts[i], short_name)) |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4019 | continue; |
| 4020 | |
| 4021 | short_name_len = strlen(short_name); |
| 4022 | |
| 4023 | /* |
| Bert Wesarg | 6e7b330 | 2009-04-13 10:25:46 | [diff] [blame] | 4024 | * in strict mode, all (except the matched one) rules |
| 4025 | * must fail to resolve to a valid non-ambiguous ref |
| 4026 | */ |
| 4027 | if (strict) |
| 4028 | rules_to_fail = nr_rules; |
| 4029 | |
| 4030 | /* |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4031 | * check if the short name resolves to a valid ref, |
| 4032 | * but use only rules prior to the matched one |
| 4033 | */ |
| Bert Wesarg | 6e7b330 | 2009-04-13 10:25:46 | [diff] [blame] | 4034 | for (j = 0; j < rules_to_fail; j++) { |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4035 | const char *rule = ref_rev_parse_rules[j]; |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4036 | char refname[PATH_MAX]; |
| 4037 | |
| Bert Wesarg | 6e7b330 | 2009-04-13 10:25:46 | [diff] [blame] | 4038 | /* skip matched rule */ |
| 4039 | if (i == j) |
| 4040 | continue; |
| 4041 | |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4042 | /* |
| 4043 | * the short name is ambiguous, if it resolves |
| 4044 | * (with this previous rule) to a valid ref |
| 4045 | * read_ref() returns 0 on success |
| 4046 | */ |
| 4047 | mksnpath(refname, sizeof(refname), |
| 4048 | rule, short_name_len, short_name); |
| Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 10:22:14 | [diff] [blame] | 4049 | if (ref_exists(refname)) |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4050 | break; |
| 4051 | } |
| 4052 | |
| 4053 | /* |
| 4054 | * short name is non-ambiguous if all previous rules |
| 4055 | * haven't resolved to a valid ref |
| 4056 | */ |
| Bert Wesarg | 6e7b330 | 2009-04-13 10:25:46 | [diff] [blame] | 4057 | if (j == rules_to_fail) |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4058 | return short_name; |
| 4059 | } |
| 4060 | |
| 4061 | free(short_name); |
| Michael Haggerty | dfefa93 | 2011-12-12 05:38:09 | [diff] [blame] | 4062 | return xstrdup(refname); |
| Jeff King | 7c2b302 | 2009-04-07 07:14:20 | [diff] [blame] | 4063 | } |
| Junio C Hamano | daebaa7 | 2013-01-19 00:08:30 | [diff] [blame] | 4064 | |
| 4065 | static struct string_list *hide_refs; |
| 4066 | |
| 4067 | int parse_hide_refs_config(const char *var, const char *value, const char *section) |
| 4068 | { |
| 4069 | if (!strcmp("transfer.hiderefs", var) || |
| 4070 | /* NEEDSWORK: use parse_config_key() once both are merged */ |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 4071 | (starts_with(var, section) && var[strlen(section)] == '.' && |
| Junio C Hamano | daebaa7 | 2013-01-19 00:08:30 | [diff] [blame] | 4072 | !strcmp(var + strlen(section), ".hiderefs"))) { |
| 4073 | char *ref; |
| 4074 | int len; |
| 4075 | |
| 4076 | if (!value) |
| 4077 | return config_error_nonbool(var); |
| 4078 | ref = xstrdup(value); |
| 4079 | len = strlen(ref); |
| 4080 | while (len && ref[len - 1] == '/') |
| 4081 | ref[--len] = '\0'; |
| 4082 | if (!hide_refs) { |
| 4083 | hide_refs = xcalloc(1, sizeof(*hide_refs)); |
| 4084 | hide_refs->strdup_strings = 1; |
| 4085 | } |
| 4086 | string_list_append(hide_refs, ref); |
| 4087 | } |
| 4088 | return 0; |
| 4089 | } |
| 4090 | |
| 4091 | int ref_is_hidden(const char *refname) |
| 4092 | { |
| 4093 | struct string_list_item *item; |
| 4094 | |
| 4095 | if (!hide_refs) |
| 4096 | return 0; |
| 4097 | for_each_string_list_item(item, hide_refs) { |
| 4098 | int len; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 4099 | if (!starts_with(refname, item->string)) |
| Junio C Hamano | daebaa7 | 2013-01-19 00:08:30 | [diff] [blame] | 4100 | continue; |
| 4101 | len = strlen(item->string); |
| 4102 | if (!refname[len] || refname[len] == '/') |
| 4103 | return 1; |
| 4104 | } |
| 4105 | return 0; |
| 4106 | } |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4107 | |
| 4108 | struct expire_reflog_cb { |
| 4109 | unsigned int flags; |
| 4110 | reflog_expiry_should_prune_fn *should_prune_fn; |
| 4111 | void *policy_cb; |
| 4112 | FILE *newlog; |
| 4113 | unsigned char last_kept_sha1[20]; |
| 4114 | }; |
| 4115 | |
| 4116 | static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
| 4117 | const char *email, unsigned long timestamp, int tz, |
| 4118 | const char *message, void *cb_data) |
| 4119 | { |
| 4120 | struct expire_reflog_cb *cb = cb_data; |
| 4121 | struct expire_reflog_policy_cb *policy_cb = cb->policy_cb; |
| 4122 | |
| 4123 | if (cb->flags & EXPIRE_REFLOGS_REWRITE) |
| 4124 | osha1 = cb->last_kept_sha1; |
| 4125 | |
| 4126 | if ((*cb->should_prune_fn)(osha1, nsha1, email, timestamp, tz, |
| 4127 | message, policy_cb)) { |
| 4128 | if (!cb->newlog) |
| 4129 | printf("would prune %s", message); |
| 4130 | else if (cb->flags & EXPIRE_REFLOGS_VERBOSE) |
| 4131 | printf("prune %s", message); |
| 4132 | } else { |
| 4133 | if (cb->newlog) { |
| Stefan Beller | c653e03 | 2014-12-12 08:57:03 | [diff] [blame] | 4134 | fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s", |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4135 | sha1_to_hex(osha1), sha1_to_hex(nsha1), |
| Stefan Beller | c653e03 | 2014-12-12 08:57:03 | [diff] [blame] | 4136 | email, timestamp, tz, message); |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4137 | hashcpy(cb->last_kept_sha1, nsha1); |
| 4138 | } |
| 4139 | if (cb->flags & EXPIRE_REFLOGS_VERBOSE) |
| 4140 | printf("keep %s", message); |
| 4141 | } |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
| 4145 | int reflog_expire(const char *refname, const unsigned char *sha1, |
| 4146 | unsigned int flags, |
| 4147 | reflog_expiry_prepare_fn prepare_fn, |
| 4148 | reflog_expiry_should_prune_fn should_prune_fn, |
| 4149 | reflog_expiry_cleanup_fn cleanup_fn, |
| 4150 | void *policy_cb_data) |
| 4151 | { |
| 4152 | static struct lock_file reflog_lock; |
| 4153 | struct expire_reflog_cb cb; |
| 4154 | struct ref_lock *lock; |
| 4155 | char *log_file; |
| 4156 | int status = 0; |
| Michael Haggerty | 5e6f003 | 2015-03-03 11:43:16 | [diff] [blame] | 4157 | int type; |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 4158 | struct strbuf err = STRBUF_INIT; |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4159 | |
| 4160 | memset(&cb, 0, sizeof(cb)); |
| 4161 | cb.flags = flags; |
| 4162 | cb.policy_cb = policy_cb_data; |
| 4163 | cb.should_prune_fn = should_prune_fn; |
| 4164 | |
| 4165 | /* |
| 4166 | * The reflog file is locked by holding the lock on the |
| 4167 | * reference itself, plus we might need to update the |
| 4168 | * reference if --updateref was specified: |
| 4169 | */ |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 4170 | lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, 0, &type, &err); |
| 4171 | if (!lock) { |
| Michael Haggerty | c628edf | 2015-05-11 15:25:20 | [diff] [blame] | 4172 | error("cannot lock ref '%s': %s", refname, err.buf); |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 4173 | strbuf_release(&err); |
| Michael Haggerty | c628edf | 2015-05-11 15:25:20 | [diff] [blame] | 4174 | return -1; |
| Michael Haggerty | 4a32b2e | 2015-05-11 15:25:15 | [diff] [blame] | 4175 | } |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4176 | if (!reflog_exists(refname)) { |
| 4177 | unlock_ref(lock); |
| 4178 | return 0; |
| 4179 | } |
| 4180 | |
| 4181 | log_file = git_pathdup("logs/%s", refname); |
| 4182 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { |
| 4183 | /* |
| 4184 | * Even though holding $GIT_DIR/logs/$reflog.lock has |
| 4185 | * no locking implications, we use the lock_file |
| 4186 | * machinery here anyway because it does a lot of the |
| 4187 | * work we need, including cleaning up if the program |
| 4188 | * exits unexpectedly. |
| 4189 | */ |
| 4190 | if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) { |
| 4191 | struct strbuf err = STRBUF_INIT; |
| 4192 | unable_to_lock_message(log_file, errno, &err); |
| 4193 | error("%s", err.buf); |
| 4194 | strbuf_release(&err); |
| 4195 | goto failure; |
| 4196 | } |
| 4197 | cb.newlog = fdopen_lock_file(&reflog_lock, "w"); |
| 4198 | if (!cb.newlog) { |
| 4199 | error("cannot fdopen %s (%s)", |
| 4200 | reflog_lock.filename.buf, strerror(errno)); |
| 4201 | goto failure; |
| 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | (*prepare_fn)(refname, sha1, cb.policy_cb); |
| 4206 | for_each_reflog_ent(refname, expire_reflog_ent, &cb); |
| 4207 | (*cleanup_fn)(cb.policy_cb); |
| 4208 | |
| 4209 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { |
| Michael Haggerty | 5e6f003 | 2015-03-03 11:43:16 | [diff] [blame] | 4210 | /* |
| 4211 | * It doesn't make sense to adjust a reference pointed |
| 4212 | * to by a symbolic ref based on expiring entries in |
| Michael Haggerty | 423c688 | 2015-03-03 11:43:17 | [diff] [blame] | 4213 | * the symbolic reference's reflog. Nor can we update |
| 4214 | * a reference if there are no remaining reflog |
| 4215 | * entries. |
| Michael Haggerty | 5e6f003 | 2015-03-03 11:43:16 | [diff] [blame] | 4216 | */ |
| 4217 | int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) && |
| Michael Haggerty | 423c688 | 2015-03-03 11:43:17 | [diff] [blame] | 4218 | !(type & REF_ISSYMREF) && |
| 4219 | !is_null_sha1(cb.last_kept_sha1); |
| Michael Haggerty | 5e6f003 | 2015-03-03 11:43:16 | [diff] [blame] | 4220 | |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4221 | if (close_lock_file(&reflog_lock)) { |
| 4222 | status |= error("couldn't write %s: %s", log_file, |
| 4223 | strerror(errno)); |
| Michael Haggerty | 5e6f003 | 2015-03-03 11:43:16 | [diff] [blame] | 4224 | } else if (update && |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4225 | (write_in_full(lock->lock_fd, |
| 4226 | sha1_to_hex(cb.last_kept_sha1), 40) != 40 || |
| 4227 | write_str_in_full(lock->lock_fd, "\n") != 1 || |
| 4228 | close_ref(lock) < 0)) { |
| 4229 | status |= error("couldn't write %s", |
| 4230 | lock->lk->filename.buf); |
| 4231 | rollback_lock_file(&reflog_lock); |
| 4232 | } else if (commit_lock_file(&reflog_lock)) { |
| 4233 | status |= error("unable to commit reflog '%s' (%s)", |
| 4234 | log_file, strerror(errno)); |
| Michael Haggerty | 5e6f003 | 2015-03-03 11:43:16 | [diff] [blame] | 4235 | } else if (update && commit_ref(lock)) { |
| Michael Haggerty | fa5b183 | 2014-12-12 08:56:59 | [diff] [blame] | 4236 | status |= error("couldn't set %s", lock->ref_name); |
| 4237 | } |
| 4238 | } |
| 4239 | free(log_file); |
| 4240 | unlock_ref(lock); |
| 4241 | return status; |
| 4242 | |
| 4243 | failure: |
| 4244 | rollback_lock_file(&reflog_lock); |
| 4245 | free(log_file); |
| 4246 | unlock_ref(lock); |
| 4247 | return -1; |
| 4248 | } |