]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-pack-refs.c
pack-refs: Store the full name of the ref even when packing only tags.
[thirdparty/git.git] / builtin-pack-refs.c
CommitLineData
e1e22e37
LT
1#include "cache.h"
2#include "refs.h"
3
96884601 4static const char builtin_pack_refs_usage[] =
b3d4204f 5"git-pack-refs [--all] [--prune]";
96884601
JH
6
7struct ref_to_prune {
8 struct ref_to_prune *next;
9 unsigned char sha1[20];
10 char name[FLEX_ARRAY];
11};
12
13struct pack_refs_cb_data {
14 int prune;
ca8e2d86 15 int all;
96884601
JH
16 struct ref_to_prune *ref_to_prune;
17 FILE *refs_file;
18};
e1e22e37 19
96884601
JH
20static int do_not_prune(int flags)
21{
22 /* If it is already packed or if it is a symref,
23 * do not prune it.
24 */
25 return (flags & (REF_ISSYMREF|REF_ISPACKED));
26}
27
8da19775
JH
28static int handle_one_ref(const char *path, const unsigned char *sha1,
29 int flags, void *cb_data)
e1e22e37 30{
96884601 31 struct pack_refs_cb_data *cb = cb_data;
cb5d709f 32
ca8e2d86
AJ
33 if (!cb->all && strncmp(path, "refs/tags/", 10))
34 return 0;
13e4aa90
JH
35 /* Do not pack the symbolic refs */
36 if (!(flags & REF_ISSYMREF))
96884601
JH
37 fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
38 if (cb->prune && !do_not_prune(flags)) {
39 int namelen = strlen(path) + 1;
40 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
41 hashcpy(n->sha1, sha1);
42 strcpy(n->name, path);
43 n->next = cb->ref_to_prune;
44 cb->ref_to_prune = n;
45 }
e1e22e37
LT
46 return 0;
47}
48
96884601
JH
49/* make sure nobody touched the ref, and unlink */
50static void prune_ref(struct ref_to_prune *r)
51{
4431fcc4 52 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
96884601
JH
53
54 if (lock) {
6d15987c 55 unlink(git_path("%s", r->name));
96884601
JH
56 unlock_ref(lock);
57 }
58}
59
60static void prune_refs(struct ref_to_prune *r)
61{
62 while (r) {
63 prune_ref(r);
64 r = r->next;
65 }
66}
67
03a18210
JH
68static struct lock_file packed;
69
e1e22e37
LT
70int cmd_pack_refs(int argc, const char **argv, const char *prefix)
71{
96884601
JH
72 int fd, i;
73 struct pack_refs_cb_data cbdata;
74
75 memset(&cbdata, 0, sizeof(cbdata));
76
77 for (i = 1; i < argc; i++) {
78 const char *arg = argv[i];
79 if (!strcmp(arg, "--prune")) {
80 cbdata.prune = 1;
81 continue;
82 }
b3d4204f 83 if (!strcmp(arg, "--all")) {
ca8e2d86 84 cbdata.all = 1;
b3d4204f
JH
85 continue;
86 }
96884601
JH
87 /* perhaps other parameters later... */
88 break;
89 }
90 if (i != argc)
91 usage(builtin_pack_refs_usage);
e1e22e37 92
03a18210 93 fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
96884601
JH
94 cbdata.refs_file = fdopen(fd, "w");
95 if (!cbdata.refs_file)
96 die("unable to create ref-pack file structure (%s)",
97 strerror(errno));
ca8e2d86 98 for_each_ref(handle_one_ref, &cbdata);
422b4a0e 99 fflush(cbdata.refs_file);
e1e22e37 100 fsync(fd);
96884601 101 fclose(cbdata.refs_file);
03a18210 102 if (commit_lock_file(&packed) < 0)
e1e22e37 103 die("unable to overwrite old ref-pack file (%s)", strerror(errno));
96884601
JH
104 if (cbdata.prune)
105 prune_refs(cbdata.ref_to_prune);
e1e22e37
LT
106 return 0;
107}