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; |
| 30 | int is_tag_ref; |
| 31 | |
| 32 | /* Do not pack the symbolic refs */ |
| 33 | if ((flags & REF_ISSYMREF)) |
| 34 | return 0; |
| 35 | is_tag_ref = !prefixcmp(path, "refs/tags/"); |
| 36 | |
| 37 | /* ALWAYS pack refs that were already packed or are tags */ |
| 38 | if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED)) |
| 39 | return 0; |
| 40 | |
| 41 | fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path); |
| 42 | if (is_tag_ref) { |
| 43 | struct object *o = parse_object(sha1); |
| 44 | if (o->type == OBJ_TAG) { |
| 45 | o = deref_tag(o, path, 0); |
| 46 | if (o) |
| 47 | fprintf(cb->refs_file, "^%s\n", |
| 48 | sha1_to_hex(o->sha1)); |
| 49 | } |
| 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 | |
| 63 | /* make sure nobody touched the ref, and unlink */ |
| 64 | static void prune_ref(struct ref_to_prune *r) |
| 65 | { |
| 66 | struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1); |
| 67 | |
| 68 | if (lock) { |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 69 | unlink_or_warn(git_path("%s", r->name)); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 70 | unlock_ref(lock); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void prune_refs(struct ref_to_prune *r) |
| 75 | { |
| 76 | while (r) { |
| 77 | prune_ref(r); |
| 78 | r = r->next; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static struct lock_file packed; |
| 83 | |
| 84 | int pack_refs(unsigned int flags) |
| 85 | { |
| 86 | int fd; |
| 87 | struct pack_refs_cb_data cbdata; |
| 88 | |
| 89 | memset(&cbdata, 0, sizeof(cbdata)); |
| 90 | cbdata.flags = flags; |
| 91 | |
Junio C Hamano | acd3b9e | 2008-10-17 15:44:39 -0700 | [diff] [blame] | 92 | fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), |
| 93 | LOCK_DIE_ON_ERROR); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 94 | cbdata.refs_file = fdopen(fd, "w"); |
| 95 | if (!cbdata.refs_file) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 96 | die_errno("unable to create ref-pack file structure"); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 97 | |
| 98 | /* perhaps other traits later as well */ |
| 99 | fprintf(cbdata.refs_file, "# pack-refs with: peeled \n"); |
| 100 | |
| 101 | for_each_ref(handle_one_ref, &cbdata); |
| 102 | if (ferror(cbdata.refs_file)) |
| 103 | die("failed to write ref-pack file"); |
| 104 | if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 105 | die_errno("failed to write ref-pack file"); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Since the lock file was fdopen()'ed and then fclose()'ed above, |
| 108 | * assign -1 to the lock file descriptor so that commit_lock_file() |
| 109 | * won't try to close() it. |
| 110 | */ |
| 111 | packed.fd = -1; |
| 112 | if (commit_lock_file(&packed) < 0) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 113 | die_errno("unable to overwrite old ref-pack file"); |
Johan Herland | 94e724a | 2008-06-15 16:05:06 +0200 | [diff] [blame] | 114 | if (cbdata.flags & PACK_REFS_PRUNE) |
| 115 | prune_refs(cbdata.ref_to_prune); |
| 116 | return 0; |
| 117 | } |