7 #include "list-objects.h"
8 #include "run-command.h"
10 static const char bundle_signature
[] = "# v2 git bundle\n";
12 static void add_to_ref_list(const unsigned char *sha1
, const char *name
,
13 struct ref_list
*list
)
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]));
20 memcpy(list
->list
[list
->nr
].sha1
, sha1
, 20);
21 list
->list
[list
->nr
].name
= xstrdup(name
);
26 int read_bundle_header(const char *path
, struct bundle_header
*header
) {
30 FILE *ffd
= fopen(path
, "rb");
33 return error("could not open '%s'", path
);
34 if (!fgets(buffer
, sizeof(buffer
), ffd
) ||
35 strcmp(buffer
, bundle_signature
)) {
37 return error("'%s' does not look like a v2 bundle file", path
);
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
;
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
);
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
);
63 fd
= open(path
, O_RDONLY
);
65 return error("could not open '%s'", path
);
66 lseek(fd
, fpos
, SEEK_SET
);
70 static int list_refs(struct ref_list
*r
, int argc
, const char **argv
)
74 for (i
= 0; i
< r
->nr
; i
++) {
77 for (j
= 1; j
< argc
; j
++)
78 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
83 printf("%s %s\n", sha1_to_hex(r
->list
[i
].sha1
),
89 #define PREREQ_MARK (1u<<16)
91 int verify_bundle(struct bundle_header
*header
, int verbose
)
94 * Do fast check, then if any prereqs are missing then go line by line
95 * to be verbose about the errors
97 struct ref_list
*p
= &header
->prerequisites
;
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:";
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
);
110 o
->flags
|= PREREQ_MARK
;
111 add_pending_object(&revs
, o
, e
->name
);
116 error("%s %s", sha1_to_hex(e
->sha1
), e
->name
);
118 if (revs
.pending
.nr
!= p
->nr
)
120 req_nr
= revs
.pending
.nr
;
121 setup_revisions(2, argv
, &revs
, NULL
);
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
);
129 prepare_revision_walk(&revs
);
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
)) {
140 error("%s %s", sha1_to_hex(refs
.objects
[i
].item
->sha1
),
141 refs
.objects
[i
].name
);
144 for (i
= 0; i
< refs
.nr
; i
++)
145 clear_commit_marks((struct commit
*)refs
.objects
[i
].item
, -1);
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 int create_bundle(struct bundle_header
*header
, const char *path
,
168 int argc
, const char **argv
)
170 static struct lock_file lock
;
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;
177 struct rev_info revs
;
178 struct child_process rls
;
181 bundle_to_stdout
= !strcmp(path
, "-");
182 if (bundle_to_stdout
)
185 bundle_fd
= hold_lock_file_for_update(&lock
, path
, 1);
187 /* write signature */
188 write_or_die(bundle_fd
, bundle_signature
, strlen(bundle_signature
));
190 /* init revs to list objects for pack-objects later */
191 save_commit_buffer
= 0;
192 init_revisions(&revs
, NULL
);
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
;
204 if (start_command(&rls
))
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
);
216 } else if (!get_sha1_hex(buffer
, sha1
)) {
217 struct object
*object
= parse_object(sha1
);
218 object
->flags
|= SHOWN
;
222 if (finish_command(&rls
))
223 return error("rev-list died");
225 /* write references */
226 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
228 return error("unrecognized argument: %s'", argv
[1]);
230 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
231 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
232 unsigned char sha1
[20];
235 if (e
->item
->flags
& UNINTERESTING
)
237 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
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.
244 * Non commit objects such as tags and blobs do not have
245 * this issue as they are not affected by those extra
248 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
249 warning("ref '%s' is excluded by the rev-list options",
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
260 if (hashcmp(sha1
, e
->item
->sha1
)) {
262 * Is this the positive end of a range expressed
263 * in terms of a tag (e.g. v2.0 from the range
266 struct commit
*one
= lookup_commit_reference(sha1
);
269 if (e
->item
== &(one
->object
)) {
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"
278 obj
= parse_object(sha1
);
280 add_pending_object(&revs
, obj
, e
->name
);
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);
294 die ("Refusing to create empty bundle.");
297 write_or_die(bundle_fd
, "\n", 1);
300 argv_pack
[0] = "pack-objects";
301 argv_pack
[1] = "--all-progress";
302 argv_pack
[2] = "--stdout";
303 argv_pack
[3] = "--thin";
305 memset(&rls
, 0, sizeof(rls
));
306 rls
.argv
= argv_pack
;
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);
319 if (finish_command(&rls
))
320 return error ("pack-objects died");
322 if (!bundle_to_stdout
)
323 commit_lock_file(&lock
);
327 int unbundle(struct bundle_header
*header
, int bundle_fd
)
329 const char *argv_index_pack
[] = {"index-pack",
330 "--fix-thin", "--stdin", NULL
};
331 struct child_process ip
;
333 if (verify_bundle(header
, 0))
335 memset(&ip
, 0, sizeof(ip
));
336 ip
.argv
= argv_index_pack
;
340 if (run_command(&ip
))
341 return error("index-pack died");