blob: 1aaa76dd1fe42f56e25dac6c3ca0e787eb7b005e [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Linus Torvaldse1e22e32006-09-11 16:37:32 -07002#include "cache.h"
3#include "refs.h"
Junio C Hamanocf0adba2006-11-19 13:22:44 -08004#include "object.h"
5#include "tag.h"
Pierre Habouzitb2565ae2007-10-15 23:06:02 +02006#include "parse-options.h"
Junio C Hamano96884602006-09-21 00:06:06 -07007
8struct ref_to_prune {
9 struct ref_to_prune *next;
10 unsigned char sha1[20];
11 char name[FLEX_ARRAY];
12};
13
Linus Torvalds99b5a792007-05-26 09:25:31 -070014#define PACK_REFS_PRUNE 0x0001
15#define PACK_REFS_ALL 0x0002
16
Junio C Hamano96884602006-09-21 00:06:06 -070017struct pack_refs_cb_data {
Linus Torvalds99b5a792007-05-26 09:25:31 -070018 unsigned int flags;
Junio C Hamano96884602006-09-21 00:06:06 -070019 struct ref_to_prune *ref_to_prune;
20 FILE *refs_file;
21};
Linus Torvaldse1e22e32006-09-11 16:37:32 -070022
Junio C Hamano96884602006-09-21 00:06:06 -070023static int do_not_prune(int flags)
24{
25 /* If it is already packed or if it is a symref,
26 * do not prune it.
27 */
28 return (flags & (REF_ISSYMREF|REF_ISPACKED));
29}
30
Junio C Hamano8da19772006-09-20 22:02:01 -070031static int handle_one_ref(const char *path, const unsigned char *sha1,
32 int flags, void *cb_data)
Linus Torvaldse1e22e32006-09-11 16:37:32 -070033{
Junio C Hamano96884602006-09-21 00:06:06 -070034 struct pack_refs_cb_data *cb = cb_data;
Junio C Hamanocf0adba2006-11-19 13:22:44 -080035 int is_tag_ref;
Junio C Hamanocb5d7092006-09-20 21:47:42 -070036
Junio C Hamano13e4aa92006-09-21 00:06:05 -070037 /* Do not pack the symbolic refs */
Junio C Hamanocf0adba2006-11-19 13:22:44 -080038 if ((flags & REF_ISSYMREF))
39 return 0;
Junio C Hamanocc44c762007-02-20 01:53:29 -080040 is_tag_ref = !prefixcmp(path, "refs/tags/");
Linus Torvalds1b555932007-01-25 16:51:21 -080041
42 /* ALWAYS pack refs that were already packed or are tags */
Linus Torvalds99b5a792007-05-26 09:25:31 -070043 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
Junio C Hamanocf0adba2006-11-19 13:22:44 -080044 return 0;
45
46 fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
47 if (is_tag_ref) {
48 struct object *o = parse_object(sha1);
49 if (o->type == OBJ_TAG) {
50 o = deref_tag(o, path, 0);
51 if (o)
Junio C Hamanof4204ab2006-11-21 23:36:35 -080052 fprintf(cb->refs_file, "^%s\n",
53 sha1_to_hex(o->sha1));
Junio C Hamanocf0adba2006-11-19 13:22:44 -080054 }
55 }
56
Linus Torvalds99b5a792007-05-26 09:25:31 -070057 if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
Junio C Hamano96884602006-09-21 00:06:06 -070058 int namelen = strlen(path) + 1;
59 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
60 hashcpy(n->sha1, sha1);
61 strcpy(n->name, path);
62 n->next = cb->ref_to_prune;
63 cb->ref_to_prune = n;
64 }
Linus Torvaldse1e22e32006-09-11 16:37:32 -070065 return 0;
66}
67
Junio C Hamano96884602006-09-21 00:06:06 -070068/* make sure nobody touched the ref, and unlink */
69static void prune_ref(struct ref_to_prune *r)
70{
Junio C Hamano4431fcc2006-09-27 01:09:18 -070071 struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
Junio C Hamano96884602006-09-21 00:06:06 -070072
73 if (lock) {
Junio C Hamano6d159872006-09-22 21:31:40 -070074 unlink(git_path("%s", r->name));
Junio C Hamano96884602006-09-21 00:06:06 -070075 unlock_ref(lock);
76 }
77}
78
79static void prune_refs(struct ref_to_prune *r)
80{
81 while (r) {
82 prune_ref(r);
83 r = r->next;
84 }
85}
86
Junio C Hamano03a18212006-10-03 02:15:18 -070087static struct lock_file packed;
88
Linus Torvalds99b5a792007-05-26 09:25:31 -070089static int pack_refs(unsigned int flags)
Linus Torvaldse1e22e32006-09-11 16:37:32 -070090{
Linus Torvalds99b5a792007-05-26 09:25:31 -070091 int fd;
Junio C Hamano96884602006-09-21 00:06:06 -070092 struct pack_refs_cb_data cbdata;
93
94 memset(&cbdata, 0, sizeof(cbdata));
Linus Torvalds99b5a792007-05-26 09:25:31 -070095 cbdata.flags = flags;
Linus Torvaldse1e22e32006-09-11 16:37:32 -070096
Junio C Hamano03a18212006-10-03 02:15:18 -070097 fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
Junio C Hamano96884602006-09-21 00:06:06 -070098 cbdata.refs_file = fdopen(fd, "w");
99 if (!cbdata.refs_file)
100 die("unable to create ref-pack file structure (%s)",
101 strerror(errno));
Junio C Hamanof4204ab2006-11-21 23:36:35 -0800102
103 /* perhaps other traits later as well */
104 fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
105
Alexandre Julliardca8e2d82006-11-02 12:13:32 +0100106 for_each_ref(handle_one_ref, &cbdata);
Jim Meyering384f1222007-06-24 21:13:11 +0200107 if (ferror(cbdata.refs_file))
108 die("failed to write ref-pack file");
Linus Torvalds99b5a792007-05-26 09:25:31 -0700109 if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
110 die("failed to write ref-pack file (%s)", strerror(errno));
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600111 /*
112 * Since the lock file was fdopen()'ed and then fclose()'ed above,
113 * assign -1 to the lock file descriptor so that commit_lock_file()
114 * won't try to close() it.
115 */
116 packed.fd = -1;
Junio C Hamano03a18212006-10-03 02:15:18 -0700117 if (commit_lock_file(&packed) < 0)
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700118 die("unable to overwrite old ref-pack file (%s)", strerror(errno));
Linus Torvalds99b5a792007-05-26 09:25:31 -0700119 if (cbdata.flags & PACK_REFS_PRUNE)
Junio C Hamano96884602006-09-21 00:06:06 -0700120 prune_refs(cbdata.ref_to_prune);
Linus Torvaldse1e22e32006-09-11 16:37:32 -0700121 return 0;
122}
Linus Torvalds99b5a792007-05-26 09:25:31 -0700123
Pierre Habouzitb2565ae2007-10-15 23:06:02 +0200124static char const * const pack_refs_usage[] = {
125 "git-pack-refs [options]",
126 NULL
127};
128
Linus Torvalds99b5a792007-05-26 09:25:31 -0700129int cmd_pack_refs(int argc, const char **argv, const char *prefix)
130{
Pierre Habouzit68dce6e2007-11-07 11:20:30 +0100131 unsigned int flags = PACK_REFS_PRUNE;
Pierre Habouzitb2565ae2007-10-15 23:06:02 +0200132 struct option opts[] = {
Pierre Habouzit68dce6e2007-11-07 11:20:30 +0100133 OPT_BIT(0, "all", &flags, "pack everything", PACK_REFS_ALL),
134 OPT_BIT(0, "prune", &flags, "prune loose refs (default)", PACK_REFS_PRUNE),
Pierre Habouzitb2565ae2007-10-15 23:06:02 +0200135 OPT_END(),
136 };
Pierre Habouzitb2565ae2007-10-15 23:06:02 +0200137 if (parse_options(argc, argv, opts, pack_refs_usage, 0))
138 usage_with_options(pack_refs_usage, opts);
Linus Torvalds99b5a792007-05-26 09:25:31 -0700139 return pack_refs(flags);
140}