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