blob: d080e30d67a913529d46e0da169faa4b0be75580 [file] [log] [blame]
Linus Torvaldse1e22e32006-09-11 16:37:32 -07001#include "cache.h"
2#include "refs.h"
Junio C Hamanocf0adba2006-11-19 13:22:44 -08003#include "object.h"
4#include "tag.h"
Linus Torvaldse1e22e32006-09-11 16:37:32 -07005
Junio C Hamano96884602006-09-21 00:06:06 -07006static const char builtin_pack_refs_usage[] =
Junio C Hamano0f018ba2007-01-08 14:40:33 -08007"git-pack-refs [--all] [--prune | --no-prune]";
Junio C Hamano96884602006-09-21 00:06:06 -07008
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;
Alexandre Julliardca8e2d82006-11-02 12:13:32 +010017 int all;
Junio C Hamano96884602006-09-21 00:06:06 -070018 struct ref_to_prune *ref_to_prune;
19 FILE *refs_file;
20};
Linus Torvaldse1e22e32006-09-11 16:37:32 -070021
Junio C Hamano96884602006-09-21 00:06:06 -070022static 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
Junio C Hamano8da19772006-09-20 22:02:01 -070030static int handle_one_ref(const char *path, const unsigned char *sha1,
31 int flags, void *cb_data)
Linus Torvaldse1e22e32006-09-11 16:37:32 -070032{
Junio C Hamano96884602006-09-21 00:06:06 -070033 struct pack_refs_cb_data *cb = cb_data;
Junio C Hamanocf0adba2006-11-19 13:22:44 -080034 int is_tag_ref;
Junio C Hamanocb5d7092006-09-20 21:47:42 -070035
Junio C Hamano13e4aa92006-09-21 00:06:05 -070036 /* Do not pack the symbolic refs */
Junio C Hamanocf0adba2006-11-19 13:22:44 -080037 if ((flags & REF_ISSYMREF))
38 return 0;
Junio C Hamanocc44c762007-02-20 01:53:29 -080039 is_tag_ref = !prefixcmp(path, "refs/tags/");
Linus Torvalds1b555932007-01-25 16:51:21 -080040
41 /* ALWAYS pack refs that were already packed or are tags */
42 if (!cb->all && !is_tag_ref && !(flags & REF_ISPACKED))
Junio C Hamanocf0adba2006-11-19 13:22:44 -080043 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)
Junio C Hamanof4204ab2006-11-21 23:36:35 -080051 fprintf(cb->refs_file, "^%s\n",
52 sha1_to_hex(o->sha1));
Junio C Hamanocf0adba2006-11-19 13:22:44 -080053 }
54 }
55
Junio C Hamano96884602006-09-21 00:06:06 -070056 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 }
Linus Torvaldse1e22e32006-09-11 16:37:32 -070064 return 0;
65}
66
Junio C Hamano96884602006-09-21 00:06:06 -070067/* make sure nobody touched the ref, and unlink */
68static void prune_ref(struct ref_to_prune *r)
69{
Junio C Hamano4431fcc2006-09-27 01:09:18 -070070 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
Junio C Hamano96884602006-09-21 00:06:06 -070071
72 if (lock) {
Junio C Hamano6d159872006-09-22 21:31:40 -070073 unlink(git_path("%s", r->name));
Junio C Hamano96884602006-09-21 00:06:06 -070074 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
Junio C Hamano03a18212006-10-03 02:15:18 -070086static struct lock_file packed;
87
Linus Torvaldse1e22e32006-09-11 16:37:32 -070088int cmd_pack_refs(int argc, const char **argv, const char *prefix)
89{
Junio C Hamano96884602006-09-21 00:06:06 -070090 int fd, i;
91 struct pack_refs_cb_data cbdata;
92
93 memset(&cbdata, 0, sizeof(cbdata));
94
Junio C Hamano0f018ba2007-01-08 14:40:33 -080095 cbdata.prune = 1;
Junio C Hamano96884602006-09-21 00:06:06 -070096 for (i = 1; i < argc; i++) {
97 const char *arg = argv[i];
98 if (!strcmp(arg, "--prune")) {
Junio C Hamano0f018ba2007-01-08 14:40:33 -080099 cbdata.prune = 1; /* now the default */
100 continue;
101 }
102 if (!strcmp(arg, "--no-prune")) {
103 cbdata.prune = 0;
Junio C Hamano96884602006-09-21 00:06:06 -0700104 continue;
105 }
Junio C Hamanob3d42042006-10-08 01:36:08 -0700106 if (!strcmp(arg, "--all")) {
Alexandre Julliardca8e2d82006-11-02 12:13:32 +0100107 cbdata.all = 1;
Junio C Hamanob3d42042006-10-08 01:36:08 -0700108 continue;
109 }
Junio C Hamano96884602006-09-21 00:06:06 -0700110 /* perhaps other parameters later... */
111 break;
112 }
113 if (i != argc)
114 usage(builtin_pack_refs_usage);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700115
Junio C Hamano03a18212006-10-03 02:15:18 -0700116 fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
Junio C Hamano96884602006-09-21 00:06:06 -0700117 cbdata.refs_file = fdopen(fd, "w");
118 if (!cbdata.refs_file)
119 die("unable to create ref-pack file structure (%s)",
120 strerror(errno));
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800121
122 /* perhaps other traits later as well */
123 fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
124
Alexandre Julliardca8e2d82006-11-02 12:13:32 +0100125 for_each_ref(handle_one_ref, &cbdata);
Junio C Hamano422b4a02006-10-04 21:37:15 -0700126 fflush(cbdata.refs_file);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700127 fsync(fd);
Junio C Hamano96884602006-09-21 00:06:06 -0700128 fclose(cbdata.refs_file);
Junio C Hamano03a18212006-10-03 02:15:18 -0700129 if (commit_lock_file(&packed) < 0)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700130 die("unable to overwrite old ref-pack file (%s)", strerror(errno));
Junio C Hamano96884602006-09-21 00:06:06 -0700131 if (cbdata.prune)
132 prune_refs(cbdata.ref_to_prune);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700133 return 0;
134}