Merge branch 'fl/config'
[git/jnareb-git.git] / builtin-pack-refs.c
blob758499238f6d4110eb3740b77c92753edc8e5760
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "tag.h"
6 static const char builtin_pack_refs_usage[] =
7 "git-pack-refs [--all] [--prune | --no-prune]";
9 struct ref_to_prune {
10 struct ref_to_prune *next;
11 unsigned char sha1[20];
12 char name[FLEX_ARRAY];
15 #define PACK_REFS_PRUNE 0x0001
16 #define PACK_REFS_ALL 0x0002
18 struct pack_refs_cb_data {
19 unsigned int flags;
20 struct ref_to_prune *ref_to_prune;
21 FILE *refs_file;
24 static int do_not_prune(int flags)
26 /* If it is already packed or if it is a symref,
27 * do not prune it.
29 return (flags & (REF_ISSYMREF|REF_ISPACKED));
32 static int handle_one_ref(const char *path, const unsigned char *sha1,
33 int flags, void *cb_data)
35 struct pack_refs_cb_data *cb = cb_data;
36 int is_tag_ref;
38 /* Do not pack the symbolic refs */
39 if ((flags & REF_ISSYMREF))
40 return 0;
41 is_tag_ref = !prefixcmp(path, "refs/tags/");
43 /* ALWAYS pack refs that were already packed or are tags */
44 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
45 return 0;
47 fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
48 if (is_tag_ref) {
49 struct object *o = parse_object(sha1);
50 if (o->type == OBJ_TAG) {
51 o = deref_tag(o, path, 0);
52 if (o)
53 fprintf(cb->refs_file, "^%s\n",
54 sha1_to_hex(o->sha1));
58 if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
59 int namelen = strlen(path) + 1;
60 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
61 hashcpy(n->sha1, sha1);
62 strcpy(n->name, path);
63 n->next = cb->ref_to_prune;
64 cb->ref_to_prune = n;
66 return 0;
69 /* make sure nobody touched the ref, and unlink */
70 static void prune_ref(struct ref_to_prune *r)
72 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
74 if (lock) {
75 unlink(git_path("%s", r->name));
76 unlock_ref(lock);
80 static void prune_refs(struct ref_to_prune *r)
82 while (r) {
83 prune_ref(r);
84 r = r->next;
88 static struct lock_file packed;
90 static int pack_refs(unsigned int flags)
92 int fd;
93 struct pack_refs_cb_data cbdata;
95 memset(&cbdata, 0, sizeof(cbdata));
96 cbdata.flags = flags;
98 fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
99 cbdata.refs_file = fdopen(fd, "w");
100 if (!cbdata.refs_file)
101 die("unable to create ref-pack file structure (%s)",
102 strerror(errno));
104 /* perhaps other traits later as well */
105 fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
107 for_each_ref(handle_one_ref, &cbdata);
108 if (ferror(cbdata.refs_file))
109 die("failed to write ref-pack file");
110 if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
111 die("failed to write ref-pack file (%s)", strerror(errno));
112 if (commit_lock_file(&packed) < 0)
113 die("unable to overwrite old ref-pack file (%s)", strerror(errno));
114 if (cbdata.flags & PACK_REFS_PRUNE)
115 prune_refs(cbdata.ref_to_prune);
116 return 0;
119 int cmd_pack_refs(int argc, const char **argv, const char *prefix)
121 int i;
122 unsigned int flags;
124 flags = PACK_REFS_PRUNE;
125 for (i = 1; i < argc; i++) {
126 const char *arg = argv[i];
127 if (!strcmp(arg, "--prune")) {
128 flags |= PACK_REFS_PRUNE; /* now the default */
129 continue;
131 if (!strcmp(arg, "--no-prune")) {
132 flags &= ~PACK_REFS_PRUNE;
133 continue;
135 if (!strcmp(arg, "--all")) {
136 flags |= PACK_REFS_ALL;
137 continue;
139 /* perhaps other parameters later... */
140 break;
142 if (i != argc)
143 usage(builtin_pack_refs_usage);
145 return pack_refs(flags);