6 static const char builtin_pack_refs_usage
[] =
7 "git-pack-refs [--all] [--prune | --no-prune]";
10 struct ref_to_prune
*next
;
11 unsigned char sha1
[20];
12 char name
[FLEX_ARRAY
];
15 struct pack_refs_cb_data
{
18 struct ref_to_prune
*ref_to_prune
;
22 static int do_not_prune(int flags
)
24 /* If it is already packed or if it is a symref,
27 return (flags
& (REF_ISSYMREF
|REF_ISPACKED
));
30 static int handle_one_ref(const char *path
, const unsigned char *sha1
,
31 int flags
, void *cb_data
)
33 struct pack_refs_cb_data
*cb
= cb_data
;
36 /* Do not pack the symbolic refs */
37 if ((flags
& REF_ISSYMREF
))
39 is_tag_ref
= !strncmp(path
, "refs/tags/", 10);
41 /* ALWAYS pack refs that were already packed or are tags */
42 if (!cb
->all
&& !is_tag_ref
&& !(flags
& REF_ISPACKED
))
45 fprintf(cb
->refs_file
, "%s %s\n", sha1_to_hex(sha1
), path
);
47 struct object
*o
= parse_object(sha1
);
48 if (o
->type
== OBJ_TAG
) {
49 o
= deref_tag(o
, path
, 0);
51 fprintf(cb
->refs_file
, "^%s\n",
52 sha1_to_hex(o
->sha1
));
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
;
67 /* make sure nobody touched the ref, and unlink */
68 static void prune_ref(struct ref_to_prune
*r
)
70 struct ref_lock
*lock
= lock_ref_sha1(r
->name
+ 5, r
->sha1
);
73 unlink(git_path("%s", r
->name
));
78 static void prune_refs(struct ref_to_prune
*r
)
86 static struct lock_file packed
;
88 int cmd_pack_refs(int argc
, const char **argv
, const char *prefix
)
91 struct pack_refs_cb_data cbdata
;
93 memset(&cbdata
, 0, sizeof(cbdata
));
96 for (i
= 1; i
< argc
; i
++) {
97 const char *arg
= argv
[i
];
98 if (!strcmp(arg
, "--prune")) {
99 cbdata
.prune
= 1; /* now the default */
102 if (!strcmp(arg
, "--no-prune")) {
106 if (!strcmp(arg
, "--all")) {
110 /* perhaps other parameters later... */
114 usage(builtin_pack_refs_usage
);
116 fd
= hold_lock_file_for_update(&packed
, git_path("packed-refs"), 1);
117 cbdata
.refs_file
= fdopen(fd
, "w");
118 if (!cbdata
.refs_file
)
119 die("unable to create ref-pack file structure (%s)",
122 /* perhaps other traits later as well */
123 fprintf(cbdata
.refs_file
, "# pack-refs with: peeled \n");
125 for_each_ref(handle_one_ref
, &cbdata
);
126 fflush(cbdata
.refs_file
);
128 fclose(cbdata
.refs_file
);
129 if (commit_lock_file(&packed
) < 0)
130 die("unable to overwrite old ref-pack file (%s)", strerror(errno
));
132 prune_refs(cbdata
.ref_to_prune
);