]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-pack-refs.c
GIT 1.5.3.2
[thirdparty/git.git] / builtin-pack-refs.c
CommitLineData
baffc0e7 1#include "builtin.h"
e1e22e37
LT
2#include "cache.h"
3#include "refs.h"
cf0adba7
JH
4#include "object.h"
5#include "tag.h"
e1e22e37 6
96884601 7static const char builtin_pack_refs_usage[] =
0f018bab 8"git-pack-refs [--all] [--prune | --no-prune]";
96884601
JH
9
10struct ref_to_prune {
11 struct ref_to_prune *next;
12 unsigned char sha1[20];
13 char name[FLEX_ARRAY];
14};
15
99b5a79e
LT
16#define PACK_REFS_PRUNE 0x0001
17#define PACK_REFS_ALL 0x0002
18
96884601 19struct pack_refs_cb_data {
99b5a79e 20 unsigned int flags;
96884601
JH
21 struct ref_to_prune *ref_to_prune;
22 FILE *refs_file;
23};
e1e22e37 24
96884601
JH
25static int do_not_prune(int flags)
26{
27 /* If it is already packed or if it is a symref,
28 * do not prune it.
29 */
30 return (flags & (REF_ISSYMREF|REF_ISPACKED));
31}
32
8da19775
JH
33static int handle_one_ref(const char *path, const unsigned char *sha1,
34 int flags, void *cb_data)
e1e22e37 35{
96884601 36 struct pack_refs_cb_data *cb = cb_data;
cf0adba7 37 int is_tag_ref;
cb5d709f 38
13e4aa90 39 /* Do not pack the symbolic refs */
cf0adba7
JH
40 if ((flags & REF_ISSYMREF))
41 return 0;
cc44c765 42 is_tag_ref = !prefixcmp(path, "refs/tags/");
1b555932
LT
43
44 /* ALWAYS pack refs that were already packed or are tags */
99b5a79e 45 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
cf0adba7
JH
46 return 0;
47
48 fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
49 if (is_tag_ref) {
50 struct object *o = parse_object(sha1);
51 if (o->type == OBJ_TAG) {
52 o = deref_tag(o, path, 0);
53 if (o)
f4204ab9
JH
54 fprintf(cb->refs_file, "^%s\n",
55 sha1_to_hex(o->sha1));
cf0adba7
JH
56 }
57 }
58
99b5a79e 59 if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
96884601
JH
60 int namelen = strlen(path) + 1;
61 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
62 hashcpy(n->sha1, sha1);
63 strcpy(n->name, path);
64 n->next = cb->ref_to_prune;
65 cb->ref_to_prune = n;
66 }
e1e22e37
LT
67 return 0;
68}
69
96884601
JH
70/* make sure nobody touched the ref, and unlink */
71static void prune_ref(struct ref_to_prune *r)
72{
4431fcc4 73 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
96884601
JH
74
75 if (lock) {
6d15987c 76 unlink(git_path("%s", r->name));
96884601
JH
77 unlock_ref(lock);
78 }
79}
80
81static void prune_refs(struct ref_to_prune *r)
82{
83 while (r) {
84 prune_ref(r);
85 r = r->next;
86 }
87}
88
03a18210
JH
89static struct lock_file packed;
90
99b5a79e 91static int pack_refs(unsigned int flags)
e1e22e37 92{
99b5a79e 93 int fd;
96884601
JH
94 struct pack_refs_cb_data cbdata;
95
96 memset(&cbdata, 0, sizeof(cbdata));
99b5a79e
LT
97 cbdata.flags = flags;
98
99 fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
100 cbdata.refs_file = fdopen(fd, "w");
101 if (!cbdata.refs_file)
102 die("unable to create ref-pack file structure (%s)",
103 strerror(errno));
104
105 /* perhaps other traits later as well */
106 fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
107
108 for_each_ref(handle_one_ref, &cbdata);
384f122b
JM
109 if (ferror(cbdata.refs_file))
110 die("failed to write ref-pack file");
99b5a79e
LT
111 if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
112 die("failed to write ref-pack file (%s)", strerror(errno));
113 if (commit_lock_file(&packed) < 0)
114 die("unable to overwrite old ref-pack file (%s)", strerror(errno));
115 if (cbdata.flags & PACK_REFS_PRUNE)
116 prune_refs(cbdata.ref_to_prune);
117 return 0;
118}
96884601 119
99b5a79e
LT
120int cmd_pack_refs(int argc, const char **argv, const char *prefix)
121{
122 int i;
123 unsigned int flags;
124
125 flags = PACK_REFS_PRUNE;
96884601
JH
126 for (i = 1; i < argc; i++) {
127 const char *arg = argv[i];
128 if (!strcmp(arg, "--prune")) {
99b5a79e 129 flags |= PACK_REFS_PRUNE; /* now the default */
0f018bab
JH
130 continue;
131 }
132 if (!strcmp(arg, "--no-prune")) {
99b5a79e 133 flags &= ~PACK_REFS_PRUNE;
96884601
JH
134 continue;
135 }
b3d4204f 136 if (!strcmp(arg, "--all")) {
99b5a79e 137 flags |= PACK_REFS_ALL;
b3d4204f
JH
138 continue;
139 }
96884601
JH
140 /* perhaps other parameters later... */
141 break;
142 }
143 if (i != argc)
144 usage(builtin_pack_refs_usage);
e1e22e37 145
99b5a79e 146 return pack_refs(flags);
e1e22e37 147}