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