Merge branch 'jc/for-each-ref-with-lt-refs' into jc/ref-locking
[git/dkf.git] / builtin-pack-refs.c
blobdb57fee72d55e9c54ee1a938e24703ebbdc12a90
1 #include "cache.h"
2 #include "refs.h"
4 static const char *result_path, *lock_path;
5 static const char builtin_pack_refs_usage[] =
6 "git-pack-refs [--prune]";
8 struct ref_to_prune {
9 struct ref_to_prune *next;
10 unsigned char sha1[20];
11 char name[FLEX_ARRAY];
14 struct pack_refs_cb_data {
15 int prune;
16 struct ref_to_prune *ref_to_prune;
17 FILE *refs_file;
20 static void remove_lock_file(void)
22 if (lock_path)
23 unlink(lock_path);
26 static int do_not_prune(int flags)
28 /* If it is already packed or if it is a symref,
29 * do not prune it.
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;
48 cb->ref_to_prune = n;
50 return 0;
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);
58 if (lock) {
59 unlink(git_path("%s", r->name));
60 unlock_ref(lock);
64 static void prune_refs(struct ref_to_prune *r)
66 while (r) {
67 prune_ref(r);
68 r = r->next;
72 int cmd_pack_refs(int argc, const char **argv, const char *prefix)
74 int fd, i;
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")) {
82 cbdata.prune = 1;
83 continue;
85 /* perhaps other parameters later... */
86 break;
88 if (i != argc)
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);
95 if (fd < 0)
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)",
102 strerror(errno));
103 for_each_ref(handle_one_ref, &cbdata);
104 fsync(fd);
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));
108 lock_path = NULL;
109 if (cbdata.prune)
110 prune_refs(cbdata.ref_to_prune);
111 return 0;