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