Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
| 3 | #include "tag.h" |
| 4 | #include "pack-refs.h" |
| 5 | |
| 6 | struct ref_to_prune { |
| 7 | struct ref_to_prune *next; |
| 8 | unsigned char sha1[20]; |
| 9 | char name[FLEX_ARRAY]; |
| 10 | }; |
| 11 | |
| 12 | struct pack_refs_cb_data { |
| 13 | unsigned int flags; |
| 14 | struct ref_to_prune *ref_to_prune; |
| 15 | FILE *refs_file; |
| 16 | }; |
| 17 | |
| 18 | static int do_not_prune(int flags) |
| 19 | { |
| 20 | /* If it is already packed or if it is a symref, |
| 21 | * do not prune it. |
| 22 | */ |
| 23 | return (flags & (REF_ISSYMREF|REF_ISPACKED)); |
| 24 | } |
| 25 | |
| 26 | static int handle_one_ref(const char *path, const unsigned char *sha1, |
| 27 | int flags, void *cb_data) |
| 28 | { |
| 29 | struct pack_refs_cb_data *cb = cb_data; |
Jeff King | 03a8edd | 2013-03-17 04:23:46 -0400 | [diff] [blame] | 30 | struct object *o; |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 31 | int is_tag_ref; |
| 32 | |
| 33 | /* Do not pack the symbolic refs */ |
| 34 | if ((flags & REF_ISSYMREF)) |
| 35 | return 0; |
| 36 | is_tag_ref = !prefixcmp(path, "refs/tags/"); |
| 37 | |
| 38 | /* ALWAYS pack refs that were already packed or are tags */ |
| 39 | if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED)) |
| 40 | return 0; |
| 41 | |
| 42 | fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path); |
Jeff King | 03a8edd | 2013-03-17 04:23:46 -0400 | [diff] [blame] | 43 | |
| 44 | o = parse_object_or_die(sha1, path); |
| 45 | if (o->type == OBJ_TAG) { |
| 46 | o = deref_tag(o, path, 0); |
| 47 | if (o) |
| 48 | fprintf(cb->refs_file, "^%s\n", |
| 49 | sha1_to_hex(o->sha1)); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) { |
| 53 | int namelen = strlen(path) + 1; |
| 54 | struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen); |
| 55 | hashcpy(n->sha1, sha1); |
| 56 | strcpy(n->name, path); |
| 57 | n->next = cb->ref_to_prune; |
| 58 | cb->ref_to_prune = n; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
Greg Price | be7c6d4 | 2010-07-06 19:29:19 -0400 | [diff] [blame] | 63 | /* |
| 64 | * Remove empty parents, but spare refs/ and immediate subdirs. |
| 65 | * Note: munges *name. |
| 66 | */ |
| 67 | static void try_remove_empty_parents(char *name) |
| 68 | { |
| 69 | char *p, *q; |
| 70 | int i; |
| 71 | p = name; |
| 72 | for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ |
| 73 | while (*p && *p != '/') |
| 74 | p++; |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 75 | /* tolerate duplicate slashes; see check_refname_format() */ |
Greg Price | be7c6d4 | 2010-07-06 19:29:19 -0400 | [diff] [blame] | 76 | while (*p == '/') |
| 77 | p++; |
| 78 | } |
| 79 | for (q = p; *q; q++) |
| 80 | ; |
| 81 | while (1) { |
| 82 | while (q > p && *q != '/') |
| 83 | q--; |
| 84 | while (q > p && *(q-1) == '/') |
| 85 | q--; |
| 86 | if (q == p) |
| 87 | break; |
| 88 | *q = '\0'; |
| 89 | if (rmdir(git_path("%s", name))) |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 94 | /* make sure nobody touched the ref, and unlink */ |
| 95 | static void prune_ref(struct ref_to_prune *r) |
| 96 | { |
| 97 | struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1); |
| 98 | |
| 99 | if (lock) { |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 100 | unlink_or_warn(git_path("%s", r->name)); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 101 | unlock_ref(lock); |
Greg Price | be7c6d4 | 2010-07-06 19:29:19 -0400 | [diff] [blame] | 102 | try_remove_empty_parents(r->name); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | static void prune_refs(struct ref_to_prune *r) |
| 107 | { |
| 108 | while (r) { |
| 109 | prune_ref(r); |
| 110 | r = r->next; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static struct lock_file packed; |
| 115 | |
| 116 | int pack_refs(unsigned int flags) |
| 117 | { |
| 118 | int fd; |
| 119 | struct pack_refs_cb_data cbdata; |
| 120 | |
| 121 | memset(&cbdata, 0, sizeof(cbdata)); |
| 122 | cbdata.flags = flags; |
| 123 | |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 124 | fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), |
| 125 | LOCK_DIE_ON_ERROR); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 126 | cbdata.refs_file = fdopen(fd, "w"); |
| 127 | if (!cbdata.refs_file) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 128 | die_errno("unable to create ref-pack file structure"); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 129 | |
| 130 | /* perhaps other traits later as well */ |
Michael Haggerty | c29c46f | 2013-03-18 07:37:32 -0400 | [diff] [blame] | 131 | fprintf(cbdata.refs_file, "# pack-refs with: peeled fully-peeled \n"); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 132 | |
| 133 | for_each_ref(handle_one_ref, &cbdata); |
| 134 | if (ferror(cbdata.refs_file)) |
| 135 | die("failed to write ref-pack file"); |
| 136 | if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 137 | die_errno("failed to write ref-pack file"); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Since the lock file was fdopen()'ed and then fclose()'ed above, |
| 140 | * assign -1 to the lock file descriptor so that commit_lock_file() |
| 141 | * won't try to close() it. |
| 142 | */ |
| 143 | packed.fd = -1; |
| 144 | if (commit_lock_file(&packed) < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 145 | die_errno("unable to overwrite old ref-pack file"); |
Michael Haggerty | e45a599 | 2012-01-17 06:50:31 +0100 | [diff] [blame] | 146 | prune_refs(cbdata.ref_to_prune); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | } |