Move bundle specific stuff into bundle.[ch]
The transport specific stuff was moved into libgit.a, and the bundle specific stuff will not be left behind. This is a big code move, with one exception: the function unbundle() no longer outputs the list of refs. You have to call list_bundle_refs() yourself for that. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Sep 10, 2007 at 23:03 UTC
30415d50cccef0ec22bb4a6d58138760b142758d
4 files changed
+375
-360
Makefile
+1
-1
@@ -311,7 +311,7 @@ LIB_OBJS = \
311
alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
312
color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
313
convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
314
- transport.o
314
+ transport.o bundle.o
315
316
BUILTIN_OBJS = \
317
builtin-add.o \
builtin-bundle.c
+6
-359
@@ -1,11 +1,6 @@
1
#include "builtin.h"
2
#include "cache.h"
3
-#include "object.h"
4
-#include "commit.h"
5
-#include "diff.h"
6
-#include "revision.h"
7
-#include "list-objects.h"
8
-#include "run-command.h"
3
+#include "bundle.h"
4
5
/*
6
* Basic handler for bundle files to connect repositories via sneakernet.
@@ -16,355 +11,6 @@
11
12
static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
13
19
-static const char bundle_signature[] = "# v2 git bundle\n";
20
-
21
-struct ref_list {
22
- unsigned int nr, alloc;
23
- struct ref_list_entry {
24
- unsigned char sha1[20];
25
- char *name;
26
- } *list;
27
-};
28
-
29
-static void add_to_ref_list(const unsigned char *sha1, const char *name,
30
- struct ref_list *list)
31
-{
32
- if (list->nr + 1 >= list->alloc) {
33
- list->alloc = alloc_nr(list->nr + 1);
34
- list->list = xrealloc(list->list,
35
- list->alloc * sizeof(list->list[0]));
36
- }
37
- memcpy(list->list[list->nr].sha1, sha1, 20);
38
- list->list[list->nr].name = xstrdup(name);
39
- list->nr++;
40
-}
41
-
42
-struct bundle_header {
43
- struct ref_list prerequisites;
44
- struct ref_list references;
45
-};
46
-
47
-/* returns an fd */
48
-static int read_header(const char *path, struct bundle_header *header) {
49
- char buffer[1024];
50
- int fd;
51
- long fpos;
52
- FILE *ffd = fopen(path, "rb");
53
-
54
- if (!ffd)
55
- return error("could not open '%s'", path);
56
- if (!fgets(buffer, sizeof(buffer), ffd) ||
57
- strcmp(buffer, bundle_signature)) {
58
- fclose(ffd);
59
- return error("'%s' does not look like a v2 bundle file", path);
60
- }
61
- while (fgets(buffer, sizeof(buffer), ffd)
62
- && buffer[0] != '\n') {
63
- int is_prereq = buffer[0] == '-';
64
- int offset = is_prereq ? 1 : 0;
65
- int len = strlen(buffer);
66
- unsigned char sha1[20];
67
- struct ref_list *list = is_prereq ? &header->prerequisites
68
- : &header->references;
69
- char delim;
70
-
71
- if (buffer[len - 1] == '\n')
72
- buffer[len - 1] = '\0';
73
- if (get_sha1_hex(buffer + offset, sha1)) {
74
- warning("unrecognized header: %s", buffer);
75
- continue;
76
- }
77
- delim = buffer[40 + offset];
78
- if (!isspace(delim) && (delim != '\0' || !is_prereq))
79
- die ("invalid header: %s", buffer);
80
- add_to_ref_list(sha1, isspace(delim) ?
81
- buffer + 41 + offset : "", list);
82
- }
83
- fpos = ftell(ffd);
84
- fclose(ffd);
85
- fd = open(path, O_RDONLY);
86
- if (fd < 0)
87
- return error("could not open '%s'", path);
88
- lseek(fd, fpos, SEEK_SET);
89
- return fd;
90
-}
91
-
92
-static int list_refs(struct ref_list *r, int argc, const char **argv)
93
-{
94
- int i;
95
-
96
- for (i = 0; i < r->nr; i++) {
97
- if (argc > 1) {
98
- int j;
99
- for (j = 1; j < argc; j++)
100
- if (!strcmp(r->list[i].name, argv[j]))
101
- break;
102
- if (j == argc)
103
- continue;
104
- }
105
- printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
106
- r->list[i].name);
107
- }
108
- return 0;
109
-}
110
-
111
-#define PREREQ_MARK (1u<<16)
112
-
113
-static int verify_bundle(struct bundle_header *header, int verbose)
114
-{
115
- /*
116
- * Do fast check, then if any prereqs are missing then go line by line
117
- * to be verbose about the errors
118
- */
119
- struct ref_list *p = &header->prerequisites;
120
- struct rev_info revs;
121
- const char *argv[] = {NULL, "--all"};
122
- struct object_array refs;
123
- struct commit *commit;
124
- int i, ret = 0, req_nr;
125
- const char *message = "Repository lacks these prerequisite commits:";
126
-
127
- init_revisions(&revs, NULL);
128
- for (i = 0; i < p->nr; i++) {
129
- struct ref_list_entry *e = p->list + i;
130
- struct object *o = parse_object(e->sha1);
131
- if (o) {
132
- o->flags |= PREREQ_MARK;
133
- add_pending_object(&revs, o, e->name);
134
- continue;
135
- }
136
- if (++ret == 1)
137
- error(message);
138
- error("%s %s", sha1_to_hex(e->sha1), e->name);
139
- }
140
- if (revs.pending.nr != p->nr)
141
- return ret;
142
- req_nr = revs.pending.nr;
143
- setup_revisions(2, argv, &revs, NULL);
144
-
145
- memset(&refs, 0, sizeof(struct object_array));
146
- for (i = 0; i < revs.pending.nr; i++) {
147
- struct object_array_entry *e = revs.pending.objects + i;
148
- add_object_array(e->item, e->name, &refs);
149
- }
150
-
151
- prepare_revision_walk(&revs);
152
-
153
- i = req_nr;
154
- while (i && (commit = get_revision(&revs)))
155
- if (commit->object.flags & PREREQ_MARK)
156
- i--;
157
-
158
- for (i = 0; i < req_nr; i++)
159
- if (!(refs.objects[i].item->flags & SHOWN)) {
160
- if (++ret == 1)
161
- error(message);
162
- error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
163
- refs.objects[i].name);
164
- }
165
-
166
- for (i = 0; i < refs.nr; i++)
167
- clear_commit_marks((struct commit *)refs.objects[i].item, -1);
168
-
169
- if (verbose) {
170
- struct ref_list *r;
171
-
172
- r = &header->references;
173
- printf("The bundle contains %d ref%s\n",
174
- r->nr, (1 < r->nr) ? "s" : "");
175
- list_refs(r, 0, NULL);
176
- r = &header->prerequisites;
177
- printf("The bundle requires these %d ref%s\n",
178
- r->nr, (1 < r->nr) ? "s" : "");
179
- list_refs(r, 0, NULL);
180
- }
181
- return ret;
182
-}
183
-
184
-static int list_heads(struct bundle_header *header, int argc, const char **argv)
185
-{
186
- return list_refs(&header->references, argc, argv);
187
-}
188
-
189
-static int create_bundle(struct bundle_header *header, const char *path,
190
- int argc, const char **argv)
191
-{
192
- static struct lock_file lock;
193
- int bundle_fd = -1;
194
- int bundle_to_stdout;
195
- const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
196
- const char **argv_pack = xmalloc(5 * sizeof(const char *));
197
- int i, ref_count = 0;
198
- char buffer[1024];
199
- struct rev_info revs;
200
- struct child_process rls;
201
- FILE *rls_fout;
202
-
203
- bundle_to_stdout = !strcmp(path, "-");
204
- if (bundle_to_stdout)
205
- bundle_fd = 1;
206
- else
207
- bundle_fd = hold_lock_file_for_update(&lock, path, 1);
208
-
209
- /* write signature */
210
- write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
211
-
212
- /* init revs to list objects for pack-objects later */
213
- save_commit_buffer = 0;
214
- init_revisions(&revs, NULL);
215
-
216
- /* write prerequisites */
217
- memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
218
- argv_boundary[0] = "rev-list";
219
- argv_boundary[1] = "--boundary";
220
- argv_boundary[2] = "--pretty=oneline";
221
- argv_boundary[argc + 2] = NULL;
222
- memset(&rls, 0, sizeof(rls));
223
- rls.argv = argv_boundary;
224
- rls.out = -1;
225
- rls.git_cmd = 1;
226
- if (start_command(&rls))
227
- return -1;
228
- rls_fout = fdopen(rls.out, "r");
229
- while (fgets(buffer, sizeof(buffer), rls_fout)) {
230
- unsigned char sha1[20];
231
- if (buffer[0] == '-') {
232
- write_or_die(bundle_fd, buffer, strlen(buffer));
233
- if (!get_sha1_hex(buffer + 1, sha1)) {
234
- struct object *object = parse_object(sha1);
235
- object->flags |= UNINTERESTING;
236
- add_pending_object(&revs, object, buffer);
237
- }
238
- } else if (!get_sha1_hex(buffer, sha1)) {
239
- struct object *object = parse_object(sha1);
240
- object->flags |= SHOWN;
241
- }
242
- }
243
- fclose(rls_fout);
244
- if (finish_command(&rls))
245
- return error("rev-list died");
246
-
247
- /* write references */
248
- argc = setup_revisions(argc, argv, &revs, NULL);
249
- if (argc > 1)
250
- return error("unrecognized argument: %s'", argv[1]);
251
-
252
- for (i = 0; i < revs.pending.nr; i++) {
253
- struct object_array_entry *e = revs.pending.objects + i;
254
- unsigned char sha1[20];
255
- char *ref;
256
-
257
- if (e->item->flags & UNINTERESTING)
258
- continue;
259
- if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
260
- continue;
261
- /*
262
- * Make sure the refs we wrote out is correct; --max-count and
263
- * other limiting options could have prevented all the tips
264
- * from getting output.
265
- *
266
- * Non commit objects such as tags and blobs do not have
267
- * this issue as they are not affected by those extra
268
- * constraints.
269
- */
270
- if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
271
- warning("ref '%s' is excluded by the rev-list options",
272
- e->name);
273
- free(ref);
274
- continue;
275
- }
276
- /*
277
- * If you run "git bundle create bndl v1.0..v2.0", the
278
- * name of the positive ref is "v2.0" but that is the
279
- * commit that is referenced by the tag, and not the tag
280
- * itself.
281
- */
282
- if (hashcmp(sha1, e->item->sha1)) {
283
- /*
284
- * Is this the positive end of a range expressed
285
- * in terms of a tag (e.g. v2.0 from the range
286
- * "v1.0..v2.0")?
287
- */
288
- struct commit *one = lookup_commit_reference(sha1);
289
- struct object *obj;
290
-
291
- if (e->item == &(one->object)) {
292
- /*
293
- * Need to include e->name as an
294
- * independent ref to the pack-objects
295
- * input, so that the tag is included
296
- * in the output; otherwise we would
297
- * end up triggering "empty bundle"
298
- * error.
299
- */
300
- obj = parse_object(sha1);
301
- obj->flags |= SHOWN;
302
- add_pending_object(&revs, obj, e->name);
303
- }
304
- free(ref);
305
- continue;
306
- }
307
-
308
- ref_count++;
309
- write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
310
- write_or_die(bundle_fd, " ", 1);
311
- write_or_die(bundle_fd, ref, strlen(ref));
312
- write_or_die(bundle_fd, "\n", 1);
313
- free(ref);
314
- }
315
- if (!ref_count)
316
- die ("Refusing to create empty bundle.");
317
-
318
- /* end header */
319
- write_or_die(bundle_fd, "\n", 1);
320
-
321
- /* write pack */
322
- argv_pack[0] = "pack-objects";
323
- argv_pack[1] = "--all-progress";
324
- argv_pack[2] = "--stdout";
325
- argv_pack[3] = "--thin";
326
- argv_pack[4] = NULL;
327
- memset(&rls, 0, sizeof(rls));
328
- rls.argv = argv_pack;
329
- rls.in = -1;
330
- rls.out = bundle_fd;
331
- rls.git_cmd = 1;
332
- if (start_command(&rls))
333
- return error("Could not spawn pack-objects");
334
- for (i = 0; i < revs.pending.nr; i++) {
335
- struct object *object = revs.pending.objects[i].item;
336
- if (object->flags & UNINTERESTING)
337
- write(rls.in, "^", 1);
338
- write(rls.in, sha1_to_hex(object->sha1), 40);
339
- write(rls.in, "\n", 1);
340
- }
341
- if (finish_command(&rls))
342
- return error ("pack-objects died");
343
- close(bundle_fd);
344
- if (!bundle_to_stdout)
345
- commit_lock_file(&lock);
346
- return 0;
347
-}
348
-
349
-static int unbundle(struct bundle_header *header, int bundle_fd,
350
- int argc, const char **argv)
351
-{
352
- const char *argv_index_pack[] = {"index-pack",
353
- "--fix-thin", "--stdin", NULL};
354
- struct child_process ip;
355
-
356
- if (verify_bundle(header, 0))
357
- return -1;
358
- memset(&ip, 0, sizeof(ip));
359
- ip.argv = argv_index_pack;
360
- ip.in = bundle_fd;
361
- ip.no_stdout = 1;
362
- ip.git_cmd = 1;
363
- if (run_command(&ip))
364
- return error("index-pack died");
365
- return list_heads(header, argc, argv);
366
-}
367
-
14
int cmd_bundle(int argc, const char **argv, const char *prefix)
15
{
16
struct bundle_header header;
@@ -388,8 +34,8 @@ int cmd_bundle(int argc, const char **argv, const char *prefix)
34
}
35
36
memset(&header, 0, sizeof(header));
391
- if (strcmp(cmd, "create") &&
392
- (bundle_fd = read_header(bundle_file, &header)) < 0)
37
+ if (strcmp(cmd, "create") && (bundle_fd =
38
+ read_bundle_header(bundle_file, &header)) < 0)
39
return 1;
40
41
if (!strcmp(cmd, "verify")) {
@@ -401,7 +47,7 @@ int cmd_bundle(int argc, const char **argv, const char *prefix)
47
}
48
if (!strcmp(cmd, "list-heads")) {
49
close(bundle_fd);
404
- return !!list_heads(&header, argc, argv);
50
+ return !!list_bundle_refs(&header, argc, argv);
51
}
52
if (!strcmp(cmd, "create")) {
53
if (nongit)
@@ -410,7 +56,8 @@ int cmd_bundle(int argc, const char **argv, const char *prefix)
56
} else if (!strcmp(cmd, "unbundle")) {
57
if (nongit)
58
die("Need a repository to unbundle.");
413
- return !!unbundle(&header, bundle_fd, argc, argv);
59
+ return !!unbundle(&header, bundle_fd) ||
60
+ list_bundle_refs(&header, argc, argv);
61
} else
62
usage(bundle_usage);
63
}
bundle.c
new
+343
@@ -0,0 +1,343 @@
1
+#include "cache.h"
2
+#include "bundle.h"
3
+#include "object.h"
4
+#include "commit.h"
5
+#include "diff.h"
6
+#include "revision.h"
7
+#include "list-objects.h"
8
+#include "run-command.h"
9
+
10
+static const char bundle_signature[] = "# v2 git bundle\n";
11
+
12
+static void add_to_ref_list(const unsigned char *sha1, const char *name,
13
+ struct ref_list *list)
14
+{
15
+ if (list->nr + 1 >= list->alloc) {
16
+ list->alloc = alloc_nr(list->nr + 1);
17
+ list->list = xrealloc(list->list,
18
+ list->alloc * sizeof(list->list[0]));
19
+ }
20
+ memcpy(list->list[list->nr].sha1, sha1, 20);
21
+ list->list[list->nr].name = xstrdup(name);
22
+ list->nr++;
23
+}
24
+
25
+/* returns an fd */
26
+int read_bundle_header(const char *path, struct bundle_header *header) {
27
+ char buffer[1024];
28
+ int fd;
29
+ long fpos;
30
+ FILE *ffd = fopen(path, "rb");
31
+
32
+ if (!ffd)
33
+ return error("could not open '%s'", path);
34
+ if (!fgets(buffer, sizeof(buffer), ffd) ||
35
+ strcmp(buffer, bundle_signature)) {
36
+ fclose(ffd);
37
+ return error("'%s' does not look like a v2 bundle file", path);
38
+ }
39
+ while (fgets(buffer, sizeof(buffer), ffd)
40
+ && buffer[0] != '\n') {
41
+ int is_prereq = buffer[0] == '-';
42
+ int offset = is_prereq ? 1 : 0;
43
+ int len = strlen(buffer);
44
+ unsigned char sha1[20];
45
+ struct ref_list *list = is_prereq ? &header->prerequisites
46
+ : &header->references;
47
+ char delim;
48
+
49
+ if (buffer[len - 1] == '\n')
50
+ buffer[len - 1] = '\0';
51
+ if (get_sha1_hex(buffer + offset, sha1)) {
52
+ warning("unrecognized header: %s", buffer);
53
+ continue;
54
+ }
55
+ delim = buffer[40 + offset];
56
+ if (!isspace(delim) && (delim != '\0' || !is_prereq))
57
+ die ("invalid header: %s", buffer);
58
+ add_to_ref_list(sha1, isspace(delim) ?
59
+ buffer + 41 + offset : "", list);
60
+ }
61
+ fpos = ftell(ffd);
62
+ fclose(ffd);
63
+ fd = open(path, O_RDONLY);
64
+ if (fd < 0)
65
+ return error("could not open '%s'", path);
66
+ lseek(fd, fpos, SEEK_SET);
67
+ return fd;
68
+}
69
+
70
+static int list_refs(struct ref_list *r, int argc, const char **argv)
71
+{
72
+ int i;
73
+
74
+ for (i = 0; i < r->nr; i++) {
75
+ if (argc > 1) {
76
+ int j;
77
+ for (j = 1; j < argc; j++)
78
+ if (!strcmp(r->list[i].name, argv[j]))
79
+ break;
80
+ if (j == argc)
81
+ continue;
82
+ }
83
+ printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
84
+ r->list[i].name);
85
+ }
86
+ return 0;
87
+}
88
+
89
+#define PREREQ_MARK (1u<<16)
90
+
91
+int verify_bundle(struct bundle_header *header, int verbose)
92
+{
93
+ /*
94
+ * Do fast check, then if any prereqs are missing then go line by line
95
+ * to be verbose about the errors
96
+ */
97
+ struct ref_list *p = &header->prerequisites;
98
+ struct rev_info revs;
99
+ const char *argv[] = {NULL, "--all"};
100
+ struct object_array refs;
101
+ struct commit *commit;
102
+ int i, ret = 0, req_nr;
103
+ const char *message = "Repository lacks these prerequisite commits:";
104
+
105
+ init_revisions(&revs, NULL);
106
+ for (i = 0; i < p->nr; i++) {
107
+ struct ref_list_entry *e = p->list + i;
108
+ struct object *o = parse_object(e->sha1);
109
+ if (o) {
110
+ o->flags |= PREREQ_MARK;
111
+ add_pending_object(&revs, o, e->name);
112
+ continue;
113
+ }
114
+ if (++ret == 1)
115
+ error(message);
116
+ error("%s %s", sha1_to_hex(e->sha1), e->name);
117
+ }
118
+ if (revs.pending.nr != p->nr)
119
+ return ret;
120
+ req_nr = revs.pending.nr;
121
+ setup_revisions(2, argv, &revs, NULL);
122
+
123
+ memset(&refs, 0, sizeof(struct object_array));
124
+ for (i = 0; i < revs.pending.nr; i++) {
125
+ struct object_array_entry *e = revs.pending.objects + i;
126
+ add_object_array(e->item, e->name, &refs);
127
+ }
128
+
129
+ prepare_revision_walk(&revs);
130
+
131
+ i = req_nr;
132
+ while (i && (commit = get_revision(&revs)))
133
+ if (commit->object.flags & PREREQ_MARK)
134
+ i--;
135
+
136
+ for (i = 0; i < req_nr; i++)
137
+ if (!(refs.objects[i].item->flags & SHOWN)) {
138
+ if (++ret == 1)
139
+ error(message);
140
+ error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
141
+ refs.objects[i].name);
142
+ }
143
+
144
+ for (i = 0; i < refs.nr; i++)
145
+ clear_commit_marks((struct commit *)refs.objects[i].item, -1);
146
+
147
+ if (verbose) {
148
+ struct ref_list *r;
149
+
150
+ r = &header->references;
151
+ printf("The bundle contains %d ref%s\n",
152
+ r->nr, (1 < r->nr) ? "s" : "");
153
+ list_refs(r, 0, NULL);
154
+ r = &header->prerequisites;
155
+ printf("The bundle requires these %d ref%s\n",
156
+ r->nr, (1 < r->nr) ? "s" : "");
157
+ list_refs(r, 0, NULL);
158
+ }
159
+ return ret;
160
+}
161
+
162
+int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
163
+{
164
+ return list_refs(&header->references, argc, argv);
165
+}
166
+
167
+int create_bundle(struct bundle_header *header, const char *path,
168
+ int argc, const char **argv)
169
+{
170
+ static struct lock_file lock;
171
+ int bundle_fd = -1;
172
+ int bundle_to_stdout;
173
+ const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
174
+ const char **argv_pack = xmalloc(5 * sizeof(const char *));
175
+ int i, ref_count = 0;
176
+ char buffer[1024];
177
+ struct rev_info revs;
178
+ struct child_process rls;
179
+ FILE *rls_fout;
180
+
181
+ bundle_to_stdout = !strcmp(path, "-");
182
+ if (bundle_to_stdout)
183
+ bundle_fd = 1;
184
+ else
185
+ bundle_fd = hold_lock_file_for_update(&lock, path, 1);
186
+
187
+ /* write signature */
188
+ write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
189
+
190
+ /* init revs to list objects for pack-objects later */
191
+ save_commit_buffer = 0;
192
+ init_revisions(&revs, NULL);
193
+
194
+ /* write prerequisites */
195
+ memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
196
+ argv_boundary[0] = "rev-list";
197
+ argv_boundary[1] = "--boundary";
198
+ argv_boundary[2] = "--pretty=oneline";
199
+ argv_boundary[argc + 2] = NULL;
200
+ memset(&rls, 0, sizeof(rls));
201
+ rls.argv = argv_boundary;
202
+ rls.out = -1;
203
+ rls.git_cmd = 1;
204
+ if (start_command(&rls))
205
+ return -1;
206
+ rls_fout = fdopen(rls.out, "r");
207
+ while (fgets(buffer, sizeof(buffer), rls_fout)) {
208
+ unsigned char sha1[20];
209
+ if (buffer[0] == '-') {
210
+ write_or_die(bundle_fd, buffer, strlen(buffer));
211
+ if (!get_sha1_hex(buffer + 1, sha1)) {
212
+ struct object *object = parse_object(sha1);
213
+ object->flags |= UNINTERESTING;
214
+ add_pending_object(&revs, object, buffer);
215
+ }
216
+ } else if (!get_sha1_hex(buffer, sha1)) {
217
+ struct object *object = parse_object(sha1);
218
+ object->flags |= SHOWN;
219
+ }
220
+ }
221
+ fclose(rls_fout);
222
+ if (finish_command(&rls))
223
+ return error("rev-list died");
224
+
225
+ /* write references */
226
+ argc = setup_revisions(argc, argv, &revs, NULL);
227
+ if (argc > 1)
228
+ return error("unrecognized argument: %s'", argv[1]);
229
+
230
+ for (i = 0; i < revs.pending.nr; i++) {
231
+ struct object_array_entry *e = revs.pending.objects + i;
232
+ unsigned char sha1[20];
233
+ char *ref;
234
+
235
+ if (e->item->flags & UNINTERESTING)
236
+ continue;
237
+ if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
238
+ continue;
239
+ /*
240
+ * Make sure the refs we wrote out is correct; --max-count and
241
+ * other limiting options could have prevented all the tips
242
+ * from getting output.
243
+ *
244
+ * Non commit objects such as tags and blobs do not have
245
+ * this issue as they are not affected by those extra
246
+ * constraints.
247
+ */
248
+ if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
249
+ warning("ref '%s' is excluded by the rev-list options",
250
+ e->name);
251
+ free(ref);
252
+ continue;
253
+ }
254
+ /*
255
+ * If you run "git bundle create bndl v1.0..v2.0", the
256
+ * name of the positive ref is "v2.0" but that is the
257
+ * commit that is referenced by the tag, and not the tag
258
+ * itself.
259
+ */
260
+ if (hashcmp(sha1, e->item->sha1)) {
261
+ /*
262
+ * Is this the positive end of a range expressed
263
+ * in terms of a tag (e.g. v2.0 from the range
264
+ * "v1.0..v2.0")?
265
+ */
266
+ struct commit *one = lookup_commit_reference(sha1);
267
+ struct object *obj;
268
+
269
+ if (e->item == &(one->object)) {
270
+ /*
271
+ * Need to include e->name as an
272
+ * independent ref to the pack-objects
273
+ * input, so that the tag is included
274
+ * in the output; otherwise we would
275
+ * end up triggering "empty bundle"
276
+ * error.
277
+ */
278
+ obj = parse_object(sha1);
279
+ obj->flags |= SHOWN;
280
+ add_pending_object(&revs, obj, e->name);
281
+ }
282
+ free(ref);
283
+ continue;
284
+ }
285
+
286
+ ref_count++;
287
+ write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
288
+ write_or_die(bundle_fd, " ", 1);
289
+ write_or_die(bundle_fd, ref, strlen(ref));
290
+ write_or_die(bundle_fd, "\n", 1);
291
+ free(ref);
292
+ }
293
+ if (!ref_count)
294
+ die ("Refusing to create empty bundle.");
295
+
296
+ /* end header */
297
+ write_or_die(bundle_fd, "\n", 1);
298
+
299
+ /* write pack */
300
+ argv_pack[0] = "pack-objects";
301
+ argv_pack[1] = "--all-progress";
302
+ argv_pack[2] = "--stdout";
303
+ argv_pack[3] = "--thin";
304
+ argv_pack[4] = NULL;
305
+ memset(&rls, 0, sizeof(rls));
306
+ rls.argv = argv_pack;
307
+ rls.in = -1;
308
+ rls.out = bundle_fd;
309
+ rls.git_cmd = 1;
310
+ if (start_command(&rls))
311
+ return error("Could not spawn pack-objects");
312
+ for (i = 0; i < revs.pending.nr; i++) {
313
+ struct object *object = revs.pending.objects[i].item;
314
+ if (object->flags & UNINTERESTING)
315
+ write(rls.in, "^", 1);
316
+ write(rls.in, sha1_to_hex(object->sha1), 40);
317
+ write(rls.in, "\n", 1);
318
+ }
319
+ if (finish_command(&rls))
320
+ return error ("pack-objects died");
321
+ close(bundle_fd);
322
+ if (!bundle_to_stdout)
323
+ commit_lock_file(&lock);
324
+ return 0;
325
+}
326
+
327
+int unbundle(struct bundle_header *header, int bundle_fd)
328
+{
329
+ const char *argv_index_pack[] = {"index-pack",
330
+ "--fix-thin", "--stdin", NULL};
331
+ struct child_process ip;
332
+
333
+ if (verify_bundle(header, 0))
334
+ return -1;
335
+ memset(&ip, 0, sizeof(ip));
336
+ ip.argv = argv_index_pack;
337
+ ip.in = bundle_fd;
338
+ ip.no_stdout = 1;
339
+ ip.git_cmd = 1;
340
+ if (run_command(&ip))
341
+ return error("index-pack died");
342
+ return 0;
343
+}
bundle.h
new
+25
@@ -0,0 +1,25 @@
1
+#ifndef BUNDLE_H
2
+#define BUNDLE_H
3
+
4
+struct ref_list {
5
+ unsigned int nr, alloc;
6
+ struct ref_list_entry {
7
+ unsigned char sha1[20];
8
+ char *name;
9
+ } *list;
10
+};
11
+
12
+struct bundle_header {
13
+ struct ref_list prerequisites;
14
+ struct ref_list references;
15
+};
16
+
17
+int read_bundle_header(const char *path, struct bundle_header *header);
18
+int create_bundle(struct bundle_header *header, const char *path,
19
+ int argc, const char **argv);
20
+int verify_bundle(struct bundle_header *header, int verbose);
21
+int unbundle(struct bundle_header *header, int bundle_fd);
22
+int list_bundle_refs(struct bundle_header *header,
23
+ int argc, const char **argv);
24
+
25
+#endif