@matheusbsbs / matheus-bagliano / commits / 94e724a741
Fork 0

Move pack_refs() and friends into libgit

This moves pack_refs() and underlying functionality into the library, to make pack-refs functionality easily available to all git programs. Most of builtin-pack-refs.c has been moved verbatim into a new file pack-refs.c that is compiled into libgit.a. A corresponding header file, pack-refs.h, has also been added, declaring pack_refs() and the #defines associated with the flags parameter to pack_refs(). This patch introduces no other changes in functionality. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johan Herland committed Jun 15, 2008 at 16:05 UTC 94e724a741590e741a540075c07a961082e3c80a
4 files changed +138 -120
Makefile
+2
@@ -354,6 +354,7 @@ LIB_H += log-tree.h
354 LIB_H += mailmap.h
355 LIB_H += object.h
356 LIB_H += pack.h
357 +LIB_H += pack-refs.h
358 LIB_H += pack-revindex.h
359 LIB_H += parse-options.h
360 LIB_H += patch-ids.h
@@ -429,6 +430,7 @@ LIB_OBJS += merge-file.o
430 LIB_OBJS += name-hash.o
431 LIB_OBJS += object.o
432 LIB_OBJS += pack-check.o
433 +LIB_OBJS += pack-refs.o
434 LIB_OBJS += pack-revindex.o
435 LIB_OBJS += pack-write.o
436 LIB_OBJS += pager.o
builtin-pack-refs.c
+1 -120
@@ -1,125 +1,6 @@
1 -#include "builtin.h"
1 #include "cache.h"
3 -#include "refs.h"
4 -#include "object.h"
5 -#include "tag.h"
2 #include "parse-options.h"
7 -
8 -struct ref_to_prune {
9 - struct ref_to_prune *next;
10 - unsigned char sha1[20];
11 - char name[FLEX_ARRAY];
12 -};
13 -
14 -#define PACK_REFS_PRUNE 0x0001
15 -#define PACK_REFS_ALL 0x0002
16 -
17 -struct pack_refs_cb_data {
18 - unsigned int flags;
19 - struct ref_to_prune *ref_to_prune;
20 - FILE *refs_file;
21 -};
22 -
23 -static int do_not_prune(int flags)
24 -{
25 - /* If it is already packed or if it is a symref,
26 - * do not prune it.
27 - */
28 - return (flags & (REF_ISSYMREF|REF_ISPACKED));
29 -}
30 -
31 -static int handle_one_ref(const char *path, const unsigned char *sha1,
32 - int flags, void *cb_data)
33 -{
34 - struct pack_refs_cb_data *cb = cb_data;
35 - int is_tag_ref;
36 -
37 - /* Do not pack the symbolic refs */
38 - if ((flags & REF_ISSYMREF))
39 - return 0;
40 - is_tag_ref = !prefixcmp(path, "refs/tags/");
41 -
42 - /* ALWAYS pack refs that were already packed or are tags */
43 - if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
44 - return 0;
45 -
46 - fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
47 - if (is_tag_ref) {
48 - struct object *o = parse_object(sha1);
49 - if (o->type == OBJ_TAG) {
50 - o = deref_tag(o, path, 0);
51 - if (o)
52 - fprintf(cb->refs_file, "^%s\n",
53 - sha1_to_hex(o->sha1));
54 - }
55 - }
56 -
57 - if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
58 - int namelen = strlen(path) + 1;
59 - struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
60 - hashcpy(n->sha1, sha1);
61 - strcpy(n->name, path);
62 - n->next = cb->ref_to_prune;
63 - cb->ref_to_prune = n;
64 - }
65 - return 0;
66 -}
67 -
68 -/* make sure nobody touched the ref, and unlink */
69 -static void prune_ref(struct ref_to_prune *r)
70 -{
71 - struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
72 -
73 - if (lock) {
74 - unlink(git_path("%s", r->name));
75 - unlock_ref(lock);
76 - }
77 -}
78 -
79 -static void prune_refs(struct ref_to_prune *r)
80 -{
81 - while (r) {
82 - prune_ref(r);
83 - r = r->next;
84 - }
85 -}
86 -
87 -static struct lock_file packed;
88 -
89 -static int pack_refs(unsigned int flags)
90 -{
91 - int fd;
92 - struct pack_refs_cb_data cbdata;
93 -
94 - memset(&cbdata, 0, sizeof(cbdata));
95 - cbdata.flags = flags;
96 -
97 - fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
98 - cbdata.refs_file = fdopen(fd, "w");
99 - if (!cbdata.refs_file)
100 - die("unable to create ref-pack file structure (%s)",
101 - strerror(errno));
102 -
103 - /* perhaps other traits later as well */
104 - fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
105 -
106 - for_each_ref(handle_one_ref, &cbdata);
107 - if (ferror(cbdata.refs_file))
108 - die("failed to write ref-pack file");
109 - if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
110 - die("failed to write ref-pack file (%s)", strerror(errno));
111 - /*
112 - * Since the lock file was fdopen()'ed and then fclose()'ed above,
113 - * assign -1 to the lock file descriptor so that commit_lock_file()
114 - * won't try to close() it.
115 - */
116 - packed.fd = -1;
117 - if (commit_lock_file(&packed) < 0)
118 - die("unable to overwrite old ref-pack file (%s)", strerror(errno));
119 - if (cbdata.flags & PACK_REFS_PRUNE)
120 - prune_refs(cbdata.ref_to_prune);
121 - return 0;
122 -}
3 +#include "pack-refs.h"
4
5 static char const * const pack_refs_usage[] = {
6 "git-pack-refs [options]",
pack-refs.c new
+117
@@ -0,0 +1,117 @@
1 +#include "cache.h"
2 +#include "refs.h"
3 +#include "tag.h"
4 +#include "pack-refs.h"
5 +
6 +struct ref_to_prune {
7 + struct ref_to_prune *next;
8 + unsigned char sha1[20];
9 + char name[FLEX_ARRAY];
10 +};
11 +
12 +struct pack_refs_cb_data {
13 + unsigned int flags;
14 + struct ref_to_prune *ref_to_prune;
15 + FILE *refs_file;
16 +};
17 +
18 +static int do_not_prune(int flags)
19 +{
20 + /* If it is already packed or if it is a symref,
21 + * do not prune it.
22 + */
23 + return (flags & (REF_ISSYMREF|REF_ISPACKED));
24 +}
25 +
26 +static int handle_one_ref(const char *path, const unsigned char *sha1,
27 + int flags, void *cb_data)
28 +{
29 + struct pack_refs_cb_data *cb = cb_data;
30 + int is_tag_ref;
31 +
32 + /* Do not pack the symbolic refs */
33 + if ((flags & REF_ISSYMREF))
34 + return 0;
35 + is_tag_ref = !prefixcmp(path, "refs/tags/");
36 +
37 + /* ALWAYS pack refs that were already packed or are tags */
38 + if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
39 + return 0;
40 +
41 + fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
42 + if (is_tag_ref) {
43 + struct object *o = parse_object(sha1);
44 + if (o->type == OBJ_TAG) {
45 + o = deref_tag(o, path, 0);
46 + if (o)
47 + fprintf(cb->refs_file, "^%s\n",
48 + sha1_to_hex(o->sha1));
49 + }
50 + }
51 +
52 + if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
53 + int namelen = strlen(path) + 1;
54 + struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
55 + hashcpy(n->sha1, sha1);
56 + strcpy(n->name, path);
57 + n->next = cb->ref_to_prune;
58 + cb->ref_to_prune = n;
59 + }
60 + return 0;
61 +}
62 +
63 +/* make sure nobody touched the ref, and unlink */
64 +static void prune_ref(struct ref_to_prune *r)
65 +{
66 + struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
67 +
68 + if (lock) {
69 + unlink(git_path("%s", r->name));
70 + unlock_ref(lock);
71 + }
72 +}
73 +
74 +static void prune_refs(struct ref_to_prune *r)
75 +{
76 + while (r) {
77 + prune_ref(r);
78 + r = r->next;
79 + }
80 +}
81 +
82 +static struct lock_file packed;
83 +
84 +int pack_refs(unsigned int flags)
85 +{
86 + int fd;
87 + struct pack_refs_cb_data cbdata;
88 +
89 + memset(&cbdata, 0, sizeof(cbdata));
90 + cbdata.flags = flags;
91 +
92 + fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
93 + cbdata.refs_file = fdopen(fd, "w");
94 + if (!cbdata.refs_file)
95 + die("unable to create ref-pack file structure (%s)",
96 + strerror(errno));
97 +
98 + /* perhaps other traits later as well */
99 + fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
100 +
101 + for_each_ref(handle_one_ref, &cbdata);
102 + if (ferror(cbdata.refs_file))
103 + die("failed to write ref-pack file");
104 + if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
105 + die("failed to write ref-pack file (%s)", strerror(errno));
106 + /*
107 + * Since the lock file was fdopen()'ed and then fclose()'ed above,
108 + * assign -1 to the lock file descriptor so that commit_lock_file()
109 + * won't try to close() it.
110 + */
111 + packed.fd = -1;
112 + if (commit_lock_file(&packed) < 0)
113 + die("unable to overwrite old ref-pack file (%s)", strerror(errno));
114 + if (cbdata.flags & PACK_REFS_PRUNE)
115 + prune_refs(cbdata.ref_to_prune);
116 + return 0;
117 +}
pack-refs.h new
+18
@@ -0,0 +1,18 @@
1 +#ifndef PACK_REFS_H
2 +#define PACK_REFS_H
3 +
4 +/*
5 + * Flags for controlling behaviour of pack_refs()
6 + * PACK_REFS_PRUNE: Prune loose refs after packing
7 + * PACK_REFS_ALL: Pack _all_ refs, not just tags and already packed refs
8 + */
9 +#define PACK_REFS_PRUNE 0x0001
10 +#define PACK_REFS_ALL 0x0002
11 +
12 +/*
13 + * Write a packed-refs file for the current repository.
14 + * flags: Combination of the above PACK_REFS_* flags.
15 + */
16 +int pack_refs(unsigned int flags);
17 +
18 +#endif /* PACK_REFS_H */