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
)
31 FILE *ffd
= fopen(path
, "rb");
34 return error("could not open '%s'", path
);
35 if (!fgets(buffer
, sizeof(buffer
), ffd
) ||
36 strcmp(buffer
, bundle_signature
)) {
38 return error("'%s' does not look like a v2 bundle file", path
);
40 while (fgets(buffer
, sizeof(buffer
), ffd
)
41 && buffer
[0] != '\n') {
42 int is_prereq
= buffer
[0] == '-';
43 int offset
= is_prereq
? 1 : 0;
44 int len
= strlen(buffer
);
45 unsigned char sha1
[20];
46 struct ref_list
*list
= is_prereq
? &header
->prerequisites
47 : &header
->references
;
50 if (buffer
[len
- 1] == '\n')
51 buffer
[len
- 1] = '\0';
52 if (get_sha1_hex(buffer
+ offset
, sha1
)) {
53 warning("unrecognized header: %s", buffer
);
56 delim
= buffer
[40 + offset
];
57 if (!isspace(delim
) && (delim
!= '\0' || !is_prereq
))
58 die ("invalid header: %s", buffer
);
59 add_to_ref_list(sha1
, isspace(delim
) ?
60 buffer
+ 41 + offset
: "", list
);
64 fd
= open(path
, O_RDONLY
);
66 return error("could not open '%s'", path
);
67 lseek(fd
, fpos
, SEEK_SET
);
71 static int list_refs(struct ref_list
*r
, int argc
, const char **argv
)
75 for (i
= 0; i
< r
->nr
; i
++) {
78 for (j
= 1; j
< argc
; j
++)
79 if (!strcmp(r
->list
[i
].name
, argv
[j
]))
84 printf("%s %s\n", sha1_to_hex(r
->list
[i
].sha1
),
90 #define PREREQ_MARK (1u<<16)
92 int verify_bundle(struct bundle_header
*header
, int verbose
)
95 * Do fast check, then if any prereqs are missing then go line by line
96 * to be verbose about the errors
98 struct ref_list
*p
= &header
->prerequisites
;
100 const char *argv
[] = {NULL
, "--all"};
101 struct object_array refs
;
102 struct commit
*commit
;
103 int i
, ret
= 0, req_nr
;
104 const char *message
= "Repository lacks these prerequisite commits:";
106 init_revisions(&revs
, NULL
);
107 for (i
= 0; i
< p
->nr
; i
++) {
108 struct ref_list_entry
*e
= p
->list
+ i
;
109 struct object
*o
= parse_object(e
->sha1
);
111 o
->flags
|= PREREQ_MARK
;
112 add_pending_object(&revs
, o
, e
->name
);
117 error("%s %s", sha1_to_hex(e
->sha1
), e
->name
);
119 if (revs
.pending
.nr
!= p
->nr
)
121 req_nr
= revs
.pending
.nr
;
122 setup_revisions(2, argv
, &revs
, NULL
);
124 memset(&refs
, 0, sizeof(struct object_array
));
125 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
126 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
127 add_object_array(e
->item
, e
->name
, &refs
);
130 prepare_revision_walk(&revs
);
133 while (i
&& (commit
= get_revision(&revs
)))
134 if (commit
->object
.flags
& PREREQ_MARK
)
137 for (i
= 0; i
< req_nr
; i
++)
138 if (!(refs
.objects
[i
].item
->flags
& SHOWN
)) {
141 error("%s %s", sha1_to_hex(refs
.objects
[i
].item
->sha1
),
142 refs
.objects
[i
].name
);
145 for (i
= 0; i
< refs
.nr
; i
++)
146 clear_commit_marks((struct commit
*)refs
.objects
[i
].item
, -1);
151 r
= &header
->references
;
152 printf("The bundle contains %d ref%s\n",
153 r
->nr
, (1 < r
->nr
) ? "s" : "");
154 list_refs(r
, 0, NULL
);
155 r
= &header
->prerequisites
;
156 printf("The bundle requires these %d ref%s\n",
157 r
->nr
, (1 < r
->nr
) ? "s" : "");
158 list_refs(r
, 0, NULL
);
163 int list_bundle_refs(struct bundle_header
*header
, int argc
, const char **argv
)
165 return list_refs(&header
->references
, argc
, argv
);
168 int create_bundle(struct bundle_header
*header
, const char *path
,
169 int argc
, const char **argv
)
171 static struct lock_file lock
;
173 int bundle_to_stdout
;
174 const char **argv_boundary
= xmalloc((argc
+ 4) * sizeof(const char *));
175 const char **argv_pack
= xmalloc(5 * sizeof(const char *));
176 int i
, ref_count
= 0;
178 struct rev_info revs
;
179 struct child_process rls
;
182 bundle_to_stdout
= !strcmp(path
, "-");
183 if (bundle_to_stdout
)
186 bundle_fd
= hold_lock_file_for_update(&lock
, path
, 1);
188 /* write signature */
189 write_or_die(bundle_fd
, bundle_signature
, strlen(bundle_signature
));
191 /* init revs to list objects for pack-objects later */
192 save_commit_buffer
= 0;
193 init_revisions(&revs
, NULL
);
195 /* write prerequisites */
196 memcpy(argv_boundary
+ 3, argv
+ 1, argc
* sizeof(const char *));
197 argv_boundary
[0] = "rev-list";
198 argv_boundary
[1] = "--boundary";
199 argv_boundary
[2] = "--pretty=oneline";
200 argv_boundary
[argc
+ 2] = NULL
;
201 memset(&rls
, 0, sizeof(rls
));
202 rls
.argv
= argv_boundary
;
205 if (start_command(&rls
))
207 rls_fout
= fdopen(rls
.out
, "r");
208 while (fgets(buffer
, sizeof(buffer
), rls_fout
)) {
209 unsigned char sha1
[20];
210 if (buffer
[0] == '-') {
211 write_or_die(bundle_fd
, buffer
, strlen(buffer
));
212 if (!get_sha1_hex(buffer
+ 1, sha1
)) {
213 struct object
*object
= parse_object(sha1
);
214 object
->flags
|= UNINTERESTING
;
215 add_pending_object(&revs
, object
, buffer
);
217 } else if (!get_sha1_hex(buffer
, sha1
)) {
218 struct object
*object
= parse_object(sha1
);
219 object
->flags
|= SHOWN
;
223 if (finish_command(&rls
))
224 return error("rev-list died");
226 /* write references */
227 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
229 return error("unrecognized argument: %s'", argv
[1]);
231 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
232 struct object_array_entry
*e
= revs
.pending
.objects
+ i
;
233 unsigned char sha1
[20];
236 if (e
->item
->flags
& UNINTERESTING
)
238 if (dwim_ref(e
->name
, strlen(e
->name
), sha1
, &ref
) != 1)
241 * Make sure the refs we wrote out is correct; --max-count and
242 * other limiting options could have prevented all the tips
243 * from getting output.
245 * Non commit objects such as tags and blobs do not have
246 * this issue as they are not affected by those extra
249 if (!(e
->item
->flags
& SHOWN
) && e
->item
->type
== OBJ_COMMIT
) {
250 warning("ref '%s' is excluded by the rev-list options",
256 * If you run "git bundle create bndl v1.0..v2.0", the
257 * name of the positive ref is "v2.0" but that is the
258 * commit that is referenced by the tag, and not the tag
261 if (hashcmp(sha1
, e
->item
->sha1
)) {
263 * Is this the positive end of a range expressed
264 * in terms of a tag (e.g. v2.0 from the range
267 struct commit
*one
= lookup_commit_reference(sha1
);
270 if (e
->item
== &(one
->object
)) {
272 * Need to include e->name as an
273 * independent ref to the pack-objects
274 * input, so that the tag is included
275 * in the output; otherwise we would
276 * end up triggering "empty bundle"
279 obj
= parse_object(sha1
);
281 add_pending_object(&revs
, obj
, e
->name
);
288 write_or_die(bundle_fd
, sha1_to_hex(e
->item
->sha1
), 40);
289 write_or_die(bundle_fd
, " ", 1);
290 write_or_die(bundle_fd
, ref
, strlen(ref
));
291 write_or_die(bundle_fd
, "\n", 1);
295 die ("Refusing to create empty bundle.");
298 write_or_die(bundle_fd
, "\n", 1);
301 argv_pack
[0] = "pack-objects";
302 argv_pack
[1] = "--all-progress";
303 argv_pack
[2] = "--stdout";
304 argv_pack
[3] = "--thin";
306 memset(&rls
, 0, sizeof(rls
));
307 rls
.argv
= argv_pack
;
311 if (start_command(&rls
))
312 return error("Could not spawn pack-objects");
313 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
314 struct object
*object
= revs
.pending
.objects
[i
].item
;
315 if (object
->flags
& UNINTERESTING
)
316 write(rls
.in
, "^", 1);
317 write(rls
.in
, sha1_to_hex(object
->sha1
), 40);
318 write(rls
.in
, "\n", 1);
320 if (finish_command(&rls
))
321 return error ("pack-objects died");
323 if (!bundle_to_stdout
)
324 commit_lock_file(&lock
);
328 int unbundle(struct bundle_header
*header
, int bundle_fd
)
330 const char *argv_index_pack
[] = {"index-pack",
331 "--fix-thin", "--stdin", NULL
};
332 struct child_process ip
;
334 if (verify_bundle(header
, 0))
336 memset(&ip
, 0, sizeof(ip
));
337 ip
.argv
= argv_index_pack
;
341 if (run_command(&ip
))
342 return error("index-pack died");