7 struct ref_to_prune *next;
8 unsigned char sha1[20];
12 struct pack_refs_cb_data {
14 struct ref_to_prune *ref_to_prune;
18 static int do_not_prune(int flags)
20 /* If it is already packed or if it is a symref,
23 return (flags & (REF_ISSYMREF|REF_ISPACKED));
26 static int handle_one_ref(const char *path, const unsigned char *sha1,
27 int flags, void *cb_data)
29 struct pack_refs_cb_data *cb = cb_data;
32 /* Do not pack the symbolic refs */
33 if ((flags & REF_ISSYMREF))
35 is_tag_ref = !prefixcmp(path, "refs/tags/");
37 /* ALWAYS pack refs that were already packed or are tags */
38 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
41 fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
43 struct object *o = parse_object(sha1);
44 if (o->type == OBJ_TAG) {
45 o = deref_tag(o, path, 0);
47 fprintf(cb->refs_file, "^%s\n",
48 sha1_to_hex(o->sha1));
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;
64 * Remove empty parents, but spare refs/ and immediate subdirs.
67 static void try_remove_empty_parents(char *name)
72 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
73 while (*p && *p != '/')
75 /* tolerate duplicate slashes; see check_ref_format() */
82 while (q > p && *q != '/')
84 while (q > p && *(q-1) == '/')
89 if (rmdir(git_path("%s", name)))
94 /* make sure nobody touched the ref, and unlink */
95 static void prune_ref(struct ref_to_prune *r)
97 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
100 unlink_or_warn(git_path("%s", r->name));
102 try_remove_empty_parents(r->name);
106 static void prune_refs(struct ref_to_prune *r)
114 static struct lock_file packed;
116 int pack_refs(unsigned int flags)
119 struct pack_refs_cb_data cbdata;
121 memset(&cbdata, 0, sizeof(cbdata));
122 cbdata.flags = flags;
124 fd = hold_lock_file_for_update(&packed, git_path("packed-refs"),
126 cbdata.refs_file = fdopen(fd, "w");
127 if (!cbdata.refs_file)
128 die_errno("unable to create ref-pack file structure");
130 /* perhaps other traits later as well */
131 fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
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))
137 die_errno("failed to write ref-pack file");
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.
144 if (commit_lock_file(&packed) < 0)
145 die_errno("unable to overwrite old ref-pack file");
146 if (cbdata.flags & PACK_REFS_PRUNE)
147 prune_refs(cbdata.ref_to_prune);