7 #include "list-objects.h"
8 #include "run-command.h"
11 static const char bundle_signature
[] = "# v2 git bundle\n";
13 static void add_to_ref_list(const unsigned char *sha1
, const char *name
,
14 struct ref_list
*list
)
16 if (list
->nr
+ 1 >= list
->alloc
) {
17 list
->alloc
= alloc_nr(list
->nr
+ 1);
18 list
->list
= xrealloc(list
->list
,
19 list
->alloc
* sizeof(list
->list
[0]));
21 memcpy(list
->list
[list
->nr
].sha1
, sha1
, 20);
22 list
->list
[list
->nr
].name
= xstrdup(name
);
27 int read_bundle_header(const char *path
, struct bundle_header
*header
)
32 FILE *ffd
= fopen(path
, "rb");
35 return error("could not open '%s'", path
);
36 if (!fgets(buffer
, sizeof(buffer
), ffd
) ||
37 strcmp(buffer
, bundle_signature
)) {
39 return error("'%s' does not look like a v2 bundle file", path
);
41 while (fgets(buffer
, sizeof(buffer
), ffd
)
42 && buffer
[0] != '\n') {
43 int is_prereq
= buffer
[0] == '-';
44 int offset
= is_prereq
? 1 : 0;
45 int len
= strlen(buffer
);
46 unsigned char sha1
[20];
47 struct ref_list
*list
= is_prereq
? &header
->prerequisites
48 : &header
->references
;
51 if (len
&& buffer
[len
- 1] == '\n')
52 buffer
[len
- 1] = '\0';
53 if (get_sha1_hex(buffer
+ offset
, sha1
)) {
54 warning("unrecognized header: %s", buffer
);
57 delim
= buffer
[40 + offset
];
58 if (!isspace(delim
) && (delim
!= '\0' || !is_prereq
))
59 die ("invalid header: %s", buffer
);
60 add_to_ref_list(sha1
, isspace(delim
) ?
61 buffer
+ 41 + offset
: "", list
);
65 fd
= open(path
, O_RDONLY
);
67 return error("could not open '%s'", path
);
68 lseek(fd
, fpos
, SEEK_SET
);
72 static int list_refs(struct ref_list
*r
, int argc
, const char **argv
)
76 for (i
= 0; i
< r
->nr
; i
++) {
79 for (j
= 1; j
< argc
; j
++)
80 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
85 printf("%s %s\n", sha1_to_hex(r
->list
[i
].sha1
),
91 #define PREREQ_MARK (1u<<16)
93 int verify_bundle(struct bundle_header
*header
, int verbose
)
96 * Do fast check, then if any prereqs are missing then go line by line
97 * to be verbose about the errors
99 struct ref_list
*p
= &header
->prerequisites
;
100 struct rev_info revs
;
101 const char *argv
[] = {NULL
, "--all"};
102 struct object_array refs
;
103 struct commit
*commit
;
104 int i
, ret
= 0, req_nr
;
105 const char *message
= "Repository lacks these prerequisite commits:";
107 init_revisions(&revs
, NULL
);
108 for (i
= 0; i
< p
->nr
; i
++) {
109 struct ref_list_entry
*e
= p
->list
+ i
;
110 struct object
*o
= parse_object(e
->sha1
);
112 o
->flags
|= PREREQ_MARK
;
113 add_pending_object(&revs
, o
, e
->name
);
117 error("%s", message
);
118 error("%s %s", sha1_to_hex(e
->sha1
), e
->name
);
120 if (revs
.pending
.nr
!= p
->nr
)
122 req_nr
= revs
.pending
.nr
;
123 setup_revisions(2, argv
, &revs
, NULL
);
125 memset(&refs
, 0, sizeof(struct object_array
));
126 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
127 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
128 add_object_array(e
->item
, e
->name
, &refs
);
131 if (prepare_revision_walk(&revs
))
132 die("revision walk setup failed");
135 while (i
&& (commit
= get_revision(&revs
)))
136 if (commit
->object
.flags
& PREREQ_MARK
)
139 for (i
= 0; i
< req_nr
; i
++)
140 if (!(refs
.objects
[i
].item
->flags
& SHOWN
)) {
142 error("%s", message
);
143 error("%s %s", sha1_to_hex(refs
.objects
[i
].item
->sha1
),
144 refs
.objects
[i
].name
);
147 for (i
= 0; i
< refs
.nr
; i
++)
148 clear_commit_marks((struct commit
*)refs
.objects
[i
].item
, -1);
153 r
= &header
->references
;
154 printf("The bundle contains %d ref%s\n",
155 r
->nr
, (1 < r
->nr
) ? "s" : "");
156 list_refs(r
, 0, NULL
);
157 r
= &header
->prerequisites
;
158 printf("The bundle requires these %d ref%s\n",
159 r
->nr
, (1 < r
->nr
) ? "s" : "");
160 list_refs(r
, 0, NULL
);
165 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
167 return list_refs(&header
->references
, argc
, argv
);
170 int create_bundle(struct bundle_header
*header
, const char *path
,
171 int argc
, const char **argv
)
173 static struct lock_file lock
;
175 int bundle_to_stdout
;
176 const char **argv_boundary
= xmalloc((argc
+ 4) * sizeof(const char *));
177 const char **argv_pack
= xmalloc(5 * sizeof(const char *));
178 int i
, ref_count
= 0;
180 struct rev_info revs
;
181 int read_from_stdin
= 0;
182 struct child_process rls
;
185 bundle_to_stdout
= !strcmp(path
, "-");
186 if (bundle_to_stdout
)
189 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
192 /* write signature */
193 write_or_die(bundle_fd
, bundle_signature
, strlen(bundle_signature
));
195 /* init revs to list objects for pack-objects later */
196 save_commit_buffer
= 0;
197 init_revisions(&revs
, NULL
);
199 /* write prerequisites */
200 memcpy(argv_boundary
+ 3, argv
+ 1, argc
* sizeof(const char *));
201 argv_boundary
[0] = "rev-list";
202 argv_boundary
[1] = "--boundary";
203 argv_boundary
[2] = "--pretty=oneline";
204 argv_boundary
[argc
+ 2] = NULL
;
205 memset(&rls
, 0, sizeof(rls
));
206 rls
.argv
= argv_boundary
;
209 if (start_command(&rls
))
211 rls_fout
= fdopen(rls
.out
, "r");
212 while (fgets(buffer
, sizeof(buffer
), rls_fout
)) {
213 unsigned char sha1
[20];
214 if (buffer
[0] == '-') {
215 write_or_die(bundle_fd
, buffer
, strlen(buffer
));
216 if (!get_sha1_hex(buffer
+ 1, sha1
)) {
217 struct object
*object
= parse_object(sha1
);
218 object
->flags
|= UNINTERESTING
;
219 add_pending_object(&revs
, object
, buffer
);
221 } else if (!get_sha1_hex(buffer
, sha1
)) {
222 struct object
*object
= parse_object(sha1
);
223 object
->flags
|= SHOWN
;
227 if (finish_command(&rls
))
228 return error("rev-list died");
230 /* write references */
231 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
233 for (i
= 1; i
< argc
; i
++) {
234 if (!strcmp(argv
[i
], "--stdin")) {
235 if (read_from_stdin
++)
236 die("--stdin given twice?");
237 read_revisions_from_stdin(&revs
);
240 return error("unrecognized argument: %s'", argv
[i
]);
243 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
244 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
245 unsigned char sha1
[20];
247 const char *display_ref
;
250 if (e
->item
->flags
& UNINTERESTING
)
252 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
254 if (!resolve_ref(e
->name
, sha1
, 1, &flag
))
256 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
259 * Make sure the refs we wrote out is correct; --max-count and
260 * other limiting options could have prevented all the tips
261 * from getting output.
263 * Non commit objects such as tags and blobs do not have
264 * this issue as they are not affected by those extra
267 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
268 warning("ref '%s' is excluded by the rev-list options",
274 * If you run "git bundle create bndl v1.0..v2.0", the
275 * name of the positive ref is "v2.0" but that is the
276 * commit that is referenced by the tag, and not the tag
279 if (hashcmp(sha1
, e
->item
->sha1
)) {
281 * Is this the positive end of a range expressed
282 * in terms of a tag (e.g. v2.0 from the range
285 struct commit
*one
= lookup_commit_reference(sha1
);
288 if (e
->item
== &(one
->object
)) {
290 * Need to include e->name as an
291 * independent ref to the pack-objects
292 * input, so that the tag is included
293 * in the output; otherwise we would
294 * end up triggering "empty bundle"
297 obj
= parse_object(sha1
);
299 add_pending_object(&revs
, obj
, e
->name
);
306 write_or_die(bundle_fd
, sha1_to_hex(e
->item
->sha1
), 40);
307 write_or_die(bundle_fd
, " ", 1);
308 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
309 write_or_die(bundle_fd
, "\n", 1);
313 die ("Refusing to create empty bundle.");
316 write_or_die(bundle_fd
, "\n", 1);
319 argv_pack
[0] = "pack-objects";
320 argv_pack
[1] = "--all-progress";
321 argv_pack
[2] = "--stdout";
322 argv_pack
[3] = "--thin";
324 memset(&rls
, 0, sizeof(rls
));
325 rls
.argv
= argv_pack
;
329 if (start_command(&rls
))
330 return error("Could not spawn pack-objects");
333 * start_command closed bundle_fd if it was > 1
334 * so set the lock fd to -1 so commit_lock_file()
335 * won't fail trying to close it.
339 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
340 struct object
*object
= revs
.pending
.objects
[i
].item
;
341 if (object
->flags
& UNINTERESTING
)
342 write_or_die(rls
.in
, "^", 1);
343 write_or_die(rls
.in
, sha1_to_hex(object
->sha1
), 40);
344 write_or_die(rls
.in
, "\n", 1);
347 if (finish_command(&rls
))
348 return error ("pack-objects died");
349 if (!bundle_to_stdout
)
350 commit_lock_file(&lock
);
354 int unbundle(struct bundle_header
*header
, int bundle_fd
)
356 const char *argv_index_pack
[] = {"index-pack",
357 "--fix-thin", "--stdin", NULL
};
358 struct child_process ip
;
360 if (verify_bundle(header
, 0))
362 memset(&ip
, 0, sizeof(ip
));
363 ip
.argv
= argv_index_pack
;
367 if (run_command(&ip
))
368 return error("index-pack died");