4 static const char *result_path
, *lock_path
;
5 static const char builtin_pack_refs_usage
[] =
6 "git-pack-refs [--prune]";
9 struct ref_to_prune
*next
;
10 unsigned char sha1
[20];
11 char name
[FLEX_ARRAY
];
14 struct pack_refs_cb_data
{
16 struct ref_to_prune
*ref_to_prune
;
20 static void remove_lock_file(void)
26 static int do_not_prune(int flags
)
28 /* If it is already packed or if it is a symref,
31 return (flags
& (REF_ISSYMREF
|REF_ISPACKED
));
34 static int handle_one_ref(const char *path
, const unsigned char *sha1
,
35 int flags
, void *cb_data
)
37 struct pack_refs_cb_data
*cb
= cb_data
;
39 /* Do not pack the symbolic refs */
40 if (!(flags
& REF_ISSYMREF
))
41 fprintf(cb
->refs_file
, "%s %s\n", sha1_to_hex(sha1
), path
);
42 if (cb
->prune
&& !do_not_prune(flags
)) {
43 int namelen
= strlen(path
) + 1;
44 struct ref_to_prune
*n
= xcalloc(1, sizeof(*n
) + namelen
);
45 hashcpy(n
->sha1
, sha1
);
46 strcpy(n
->name
, path
);
47 n
->next
= cb
->ref_to_prune
;
53 /* make sure nobody touched the ref, and unlink */
54 static void prune_ref(struct ref_to_prune
*r
)
56 struct ref_lock
*lock
= lock_ref_sha1(r
->name
+ 5, r
->sha1
, 1);
59 unlink(git_path("%s", r
->name
));
64 static void prune_refs(struct ref_to_prune
*r
)
72 int cmd_pack_refs(int argc
, const char **argv
, const char *prefix
)
75 struct pack_refs_cb_data cbdata
;
77 memset(&cbdata
, 0, sizeof(cbdata
));
79 for (i
= 1; i
< argc
; i
++) {
80 const char *arg
= argv
[i
];
81 if (!strcmp(arg
, "--prune")) {
85 /* perhaps other parameters later... */
89 usage(builtin_pack_refs_usage
);
91 result_path
= xstrdup(git_path("packed-refs"));
92 lock_path
= xstrdup(mkpath("%s.lock", result_path
));
94 fd
= open(lock_path
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
96 die("unable to create new ref-pack file (%s)", strerror(errno
));
97 atexit(remove_lock_file
);
99 cbdata
.refs_file
= fdopen(fd
, "w");
100 if (!cbdata
.refs_file
)
101 die("unable to create ref-pack file structure (%s)",
103 for_each_ref(handle_one_ref
, &cbdata
);
105 fclose(cbdata
.refs_file
);
106 if (rename(lock_path
, result_path
) < 0)
107 die("unable to overwrite old ref-pack file (%s)", strerror(errno
));
110 prune_refs(cbdata
.ref_to_prune
);