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", NULL
};
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
);
126 revs
.leak_pending
= 1;
128 if (prepare_revision_walk(&revs
))
129 die("revision walk setup failed");
132 while (i
&& (commit
= get_revision(&revs
)))
133 if (commit
->object
.flags
& PREREQ_MARK
)
136 for (i
= 0; i
< req_nr
; i
++)
137 if (!(refs
.objects
[i
].item
->flags
& SHOWN
)) {
139 error("%s", message
);
140 error("%s %s", sha1_to_hex(refs
.objects
[i
].item
->sha1
),
141 refs
.objects
[i
].name
);
144 clear_commit_marks_for_object_array(&refs
, ALL_REV_FLAGS
);
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
);
162 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
164 return list_refs(&header
->references
, argc
, argv
);
167 static int is_tag_in_date_range(struct object
*tag
, struct rev_info
*revs
)
170 enum object_type type
;
171 char *buf
, *line
, *lineend
;
174 if (revs
->max_age
== -1 && revs
->min_age
== -1)
177 buf
= read_sha1_file(tag
->sha1
, &type
, &size
);
180 line
= memmem(buf
, size
, "\ntagger ", 8);
183 lineend
= memchr(line
, buf
+ size
- line
, '\n');
184 line
= memchr(line
, lineend
? lineend
- line
: buf
+ size
- line
, '>');
187 date
= strtoul(line
, NULL
, 10);
189 return (revs
->max_age
== -1 || revs
->max_age
< date
) &&
190 (revs
->min_age
== -1 || revs
->min_age
> date
);
193 int create_bundle(struct bundle_header
*header
, const char *path
,
194 int argc
, const char **argv
)
196 static struct lock_file lock
;
198 int bundle_to_stdout
;
199 const char **argv_boundary
= xmalloc((argc
+ 4) * sizeof(const char *));
200 const char **argv_pack
= xmalloc(6 * sizeof(const char *));
201 int i
, ref_count
= 0;
203 struct rev_info revs
;
204 struct child_process rls
;
207 bundle_to_stdout
= !strcmp(path
, "-");
208 if (bundle_to_stdout
)
211 bundle_fd
= hold_lock_file_for_update(&lock
, path
,
214 /* write signature */
215 write_or_die(bundle_fd
, bundle_signature
, strlen(bundle_signature
));
217 /* init revs to list objects for pack-objects later */
218 save_commit_buffer
= 0;
219 init_revisions(&revs
, NULL
);
221 /* write prerequisites */
222 memcpy(argv_boundary
+ 3, argv
+ 1, argc
* sizeof(const char *));
223 argv_boundary
[0] = "rev-list";
224 argv_boundary
[1] = "--boundary";
225 argv_boundary
[2] = "--pretty=oneline";
226 argv_boundary
[argc
+ 2] = NULL
;
227 memset(&rls
, 0, sizeof(rls
));
228 rls
.argv
= argv_boundary
;
231 if (start_command(&rls
))
233 rls_fout
= xfdopen(rls
.out
, "r");
234 while (fgets(buffer
, sizeof(buffer
), rls_fout
)) {
235 unsigned char sha1
[20];
236 if (buffer
[0] == '-') {
237 write_or_die(bundle_fd
, buffer
, strlen(buffer
));
238 if (!get_sha1_hex(buffer
+ 1, sha1
)) {
239 struct object
*object
= parse_object(sha1
);
240 object
->flags
|= UNINTERESTING
;
241 add_pending_object(&revs
, object
, buffer
);
243 } else if (!get_sha1_hex(buffer
, sha1
)) {
244 struct object
*object
= parse_object(sha1
);
245 object
->flags
|= SHOWN
;
249 if (finish_command(&rls
))
250 return error("rev-list died");
252 /* write references */
253 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
256 return error("unrecognized argument: %s'", argv
[1]);
258 object_array_remove_duplicates(&revs
.pending
);
260 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
261 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
262 unsigned char sha1
[20];
264 const char *display_ref
;
267 if (e
->item
->flags
& UNINTERESTING
)
269 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
271 if (!resolve_ref(e
->name
, sha1
, 1, &flag
))
273 display_ref
= (flag
& REF_ISSYMREF
) ? e
->name
: ref
;
275 if (e
->item
->type
== OBJ_TAG
&&
276 !is_tag_in_date_range(e
->item
, &revs
)) {
277 e
->item
->flags
|= UNINTERESTING
;
282 * Make sure the refs we wrote out is correct; --max-count and
283 * other limiting options could have prevented all the tips
284 * from getting output.
286 * Non commit objects such as tags and blobs do not have
287 * this issue as they are not affected by those extra
290 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
291 warning("ref '%s' is excluded by the rev-list options",
297 * If you run "git bundle create bndl v1.0..v2.0", the
298 * name of the positive ref is "v2.0" but that is the
299 * commit that is referenced by the tag, and not the tag
302 if (hashcmp(sha1
, e
->item
->sha1
)) {
304 * Is this the positive end of a range expressed
305 * in terms of a tag (e.g. v2.0 from the range
308 struct commit
*one
= lookup_commit_reference(sha1
);
311 if (e
->item
== &(one
->object
)) {
313 * Need to include e->name as an
314 * independent ref to the pack-objects
315 * input, so that the tag is included
316 * in the output; otherwise we would
317 * end up triggering "empty bundle"
320 obj
= parse_object(sha1
);
322 add_pending_object(&revs
, obj
, e
->name
);
329 write_or_die(bundle_fd
, sha1_to_hex(e
->item
->sha1
), 40);
330 write_or_die(bundle_fd
, " ", 1);
331 write_or_die(bundle_fd
, display_ref
, strlen(display_ref
));
332 write_or_die(bundle_fd
, "\n", 1);
336 die ("Refusing to create empty bundle.");
339 write_or_die(bundle_fd
, "\n", 1);
342 argv_pack
[0] = "pack-objects";
343 argv_pack
[1] = "--all-progress-implied";
344 argv_pack
[2] = "--stdout";
345 argv_pack
[3] = "--thin";
346 argv_pack
[4] = "--delta-base-offset";
348 memset(&rls
, 0, sizeof(rls
));
349 rls
.argv
= argv_pack
;
353 if (start_command(&rls
))
354 return error("Could not spawn pack-objects");
357 * start_command closed bundle_fd if it was > 1
358 * so set the lock fd to -1 so commit_lock_file()
359 * won't fail trying to close it.
363 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
364 struct object
*object
= revs
.pending
.objects
[i
].item
;
365 if (object
->flags
& UNINTERESTING
)
366 write_or_die(rls
.in
, "^", 1);
367 write_or_die(rls
.in
, sha1_to_hex(object
->sha1
), 40);
368 write_or_die(rls
.in
, "\n", 1);
371 if (finish_command(&rls
))
372 return error ("pack-objects died");
373 if (!bundle_to_stdout
) {
374 if (commit_lock_file(&lock
))
375 die_errno("cannot create '%s'", path
);
380 int unbundle(struct bundle_header
*header
, int bundle_fd
, int flags
)
382 const char *argv_index_pack
[] = {"index-pack",
383 "--fix-thin", "--stdin", NULL
, NULL
};
384 struct child_process ip
;
386 if (flags
& BUNDLE_VERBOSE
)
387 argv_index_pack
[3] = "-v";
389 if (verify_bundle(header
, 0))
391 memset(&ip
, 0, sizeof(ip
));
392 ip
.argv
= argv_index_pack
;
396 if (run_command(&ip
))
397 return error("index-pack died");