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