Commit | Line | Data |
---|---|---|
e1e22e37 LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | ||
e1e22e37 LT |
4 | static const char *result_path, *lock_path; |
5 | ||
6 | static void remove_lock_file(void) | |
7 | { | |
8 | if (lock_path) | |
9 | unlink(lock_path); | |
10 | } | |
11 | ||
cb5d709f | 12 | static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data) |
e1e22e37 | 13 | { |
cb5d709f JH |
14 | FILE *refs_file = cb_data; |
15 | ||
e1e22e37 LT |
16 | fprintf(refs_file, "%s %s\n", sha1_to_hex(sha1), path); |
17 | return 0; | |
18 | } | |
19 | ||
20 | int cmd_pack_refs(int argc, const char **argv, const char *prefix) | |
21 | { | |
22 | int fd; | |
cb5d709f | 23 | FILE *refs_file; |
e1e22e37 LT |
24 | |
25 | result_path = xstrdup(git_path("packed-refs")); | |
26 | lock_path = xstrdup(mkpath("%s.lock", result_path)); | |
27 | ||
28 | fd = open(lock_path, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
29 | if (fd < 0) | |
30 | die("unable to create new ref-pack file (%s)", strerror(errno)); | |
31 | atexit(remove_lock_file); | |
32 | ||
33 | refs_file = fdopen(fd, "w"); | |
34 | if (!refs_file) | |
35 | die("unable to create ref-pack file structure (%s)", strerror(errno)); | |
cb5d709f | 36 | for_each_ref(handle_one_ref, refs_file); |
e1e22e37 LT |
37 | fsync(fd); |
38 | fclose(refs_file); | |
39 | if (rename(lock_path, result_path) < 0) | |
40 | die("unable to overwrite old ref-pack file (%s)", strerror(errno)); | |
41 | lock_path = NULL; | |
42 | return 0; | |
43 | } |