blob: 4ef7256aa11e8b43691f238970da6c6bf21cd4ee [file] [log] [blame]
Johannes Schindelin30415d52007-09-10 23:03:15 -04001#include "cache.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02002#include "lockfile.h"
Johannes Schindelin30415d52007-09-10 23:03:15 -04003#include "bundle.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Stefan Beller109cd762018-06-28 18:21:51 -07005#include "repository.h"
Johannes Schindelin30415d52007-09-10 23:03:15 -04006#include "object.h"
7#include "commit.h"
8#include "diff.h"
9#include "revision.h"
10#include "list-objects.h"
11#include "run-command.h"
Junio C Hamanofa303832007-11-22 16:51:18 -080012#include "refs.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040013#include "strvec.h"
Derrick Stolee105c6f12022-03-09 16:01:39 +000014#include "list-objects-filter-options.h"
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000015
16static const char v2_bundle_signature[] = "# v2 git bundle\n";
17static const char v3_bundle_signature[] = "# v3 git bundle\n";
18static struct {
19 int version;
20 const char *signature;
21} bundle_sigs[] = {
22 { 2, v2_bundle_signature },
23 { 3, v3_bundle_signature },
24};
Johannes Schindelin30415d52007-09-10 23:03:15 -040025
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +020026void bundle_header_init(struct bundle_header *header)
Johannes Schindelin30415d52007-09-10 23:03:15 -040027{
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +020028 struct bundle_header blank = BUNDLE_HEADER_INIT;
29 memcpy(header, &blank, sizeof(*header));
30}
31
32void bundle_header_release(struct bundle_header *header)
33{
34 string_list_clear(&header->prerequisites, 1);
35 string_list_clear(&header->references, 1);
Derrick Stolee105c6f12022-03-09 16:01:39 +000036 list_objects_filter_release(&header->filter);
Johannes Schindelin30415d52007-09-10 23:03:15 -040037}
38
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000039static int parse_capability(struct bundle_header *header, const char *capability)
brian m. carlson6161ce72020-06-19 17:56:00 +000040{
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000041 const char *arg;
42 if (skip_prefix(capability, "object-format=", &arg)) {
43 int algo = hash_algo_by_name(arg);
44 if (algo == GIT_HASH_UNKNOWN)
45 return error(_("unrecognized bundle hash algorithm: %s"), arg);
46 header->hash_algo = &hash_algos[algo];
47 return 0;
48 }
Derrick Stolee105c6f12022-03-09 16:01:39 +000049 if (skip_prefix(capability, "filter=", &arg)) {
50 parse_list_objects_filter(&header->filter, arg);
51 return 0;
52 }
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000053 return error(_("unknown capability '%s'"), capability);
54}
brian m. carlson6161ce72020-06-19 17:56:00 +000055
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000056static int parse_bundle_signature(struct bundle_header *header, const char *line)
57{
58 int i;
59
60 for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) {
61 if (!strcmp(line, bundle_sigs[i].signature)) {
62 header->version = bundle_sigs[i].version;
63 return 0;
64 }
65 }
66 return -1;
brian m. carlson6161ce72020-06-19 17:56:00 +000067}
68
Ævar Arnfjörð Bjarmason89c6e452022-05-16 20:11:05 +000069int read_bundle_header_fd(int fd, struct bundle_header *header,
70 const char *report_path)
Junio C Hamanof3fa1832007-11-08 15:35:32 -080071{
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070072 struct strbuf buf = STRBUF_INIT;
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070073 int status = 0;
Johannes Schindelin30415d52007-09-10 23:03:15 -040074
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070075 /* The bundle header begins with the signature */
Thomas Rast5e8617f2012-02-22 20:34:22 +010076 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000077 parse_bundle_signature(header, buf.buf)) {
Junio C Hamano2727b712011-10-13 15:19:31 -070078 if (report_path)
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000079 error(_("'%s' does not look like a v2 or v3 bundle file"),
Junio C Hamano2727b712011-10-13 15:19:31 -070080 report_path);
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070081 status = -1;
82 goto abort;
83 }
84
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000085 header->hash_algo = the_hash_algo;
86
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070087 /* The bundle header ends with an empty line */
Thomas Rast5e8617f2012-02-22 20:34:22 +010088 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070089 buf.len && buf.buf[0] != '\n') {
brian m. carlsonb8607f32017-05-01 02:28:59 +000090 struct object_id oid;
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070091 int is_prereq = 0;
brian m. carlsonb8607f32017-05-01 02:28:59 +000092 const char *p;
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070093
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -070094 strbuf_rtrim(&buf);
95
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000096 if (header->version == 3 && *buf.buf == '@') {
97 if (parse_capability(header, buf.buf + 1)) {
brian m. carlson6161ce72020-06-19 17:56:00 +000098 status = -1;
99 break;
100 }
brian m. carlsonc5aecfc2020-07-29 23:14:20 +0000101 continue;
102 }
103
104 if (*buf.buf == '-') {
105 is_prereq = 1;
106 strbuf_remove(&buf, 0, 1);
brian m. carlson6161ce72020-06-19 17:56:00 +0000107 }
108
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -0700109 /*
110 * Tip lines have object name, SP, and refname.
111 * Prerequisites have object name that is optionally
112 * followed by SP and subject line.
113 */
brian m. carlson6161ce72020-06-19 17:56:00 +0000114 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
brian m. carlsonb8607f32017-05-01 02:28:59 +0000115 (*p && !isspace(*p)) ||
116 (!is_prereq && !*p)) {
Junio C Hamano2727b712011-10-13 15:19:31 -0700117 if (report_path)
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700118 error(_("unrecognized header: %s%s (%d)"),
Junio C Hamano2727b712011-10-13 15:19:31 -0700119 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -0700120 status = -1;
121 break;
122 } else {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200123 struct object_id *dup = oiddup(&oid);
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -0700124 if (is_prereq)
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200125 string_list_append(&header->prerequisites, "")->util = dup;
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -0700126 else
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200127 string_list_append(&header->references, p + 1)->util = dup;
Junio C Hamanoe9ee84c2011-10-13 15:12:02 -0700128 }
129 }
130
131 abort:
132 if (status) {
133 close(fd);
134 fd = -1;
135 }
136 strbuf_release(&buf);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400137 return fd;
138}
139
Johannes Schindelin30415d52007-09-10 23:03:15 -0400140int read_bundle_header(const char *path, struct bundle_header *header)
141{
Junio C Hamano2727b712011-10-13 15:19:31 -0700142 int fd = open(path, O_RDONLY);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400143
Johannes Schindelin30415d52007-09-10 23:03:15 -0400144 if (fd < 0)
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700145 return error(_("could not open '%s'"), path);
Ævar Arnfjörð Bjarmason89c6e452022-05-16 20:11:05 +0000146 return read_bundle_header_fd(fd, header, path);
Junio C Hamano2727b712011-10-13 15:19:31 -0700147}
148
149int is_bundle(const char *path, int quiet)
150{
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200151 struct bundle_header header = BUNDLE_HEADER_INIT;
Junio C Hamano2727b712011-10-13 15:19:31 -0700152 int fd = open(path, O_RDONLY);
153
154 if (fd < 0)
155 return 0;
Ævar Arnfjörð Bjarmason89c6e452022-05-16 20:11:05 +0000156 fd = read_bundle_header_fd(fd, &header, quiet ? NULL : path);
Junio C Hamano2727b712011-10-13 15:19:31 -0700157 if (fd >= 0)
158 close(fd);
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200159 bundle_header_release(&header);
Junio C Hamano2727b712011-10-13 15:19:31 -0700160 return (fd >= 0);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400161}
162
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200163static int list_refs(struct string_list *r, int argc, const char **argv)
Johannes Schindelin30415d52007-09-10 23:03:15 -0400164{
165 int i;
166
167 for (i = 0; i < r->nr; i++) {
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200168 struct object_id *oid;
169 const char *name;
170
Johannes Schindelin30415d52007-09-10 23:03:15 -0400171 if (argc > 1) {
172 int j;
173 for (j = 1; j < argc; j++)
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200174 if (!strcmp(r->items[i].string, argv[j]))
Johannes Schindelin30415d52007-09-10 23:03:15 -0400175 break;
176 if (j == argc)
177 continue;
178 }
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200179
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200180 oid = r->items[i].util;
181 name = r->items[i].string;
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200182 printf("%s %s\n", oid_to_hex(oid), name);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400183 }
184 return 0;
185}
186
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +0700187/* Remember to update object flag allocation in object.h */
Johannes Schindelin30415d52007-09-10 23:03:15 -0400188#define PREREQ_MARK (1u<<16)
189
Nguyễn Thái Ngọc Duy74ae4b62018-11-10 06:49:01 +0100190int verify_bundle(struct repository *r,
191 struct bundle_header *header,
Derrick Stolee89bd7fe2022-10-12 12:52:37 +0000192 enum verify_bundle_flags flags)
Johannes Schindelin30415d52007-09-10 23:03:15 -0400193{
194 /*
195 * Do fast check, then if any prereqs are missing then go line by line
196 * to be verbose about the errors
197 */
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200198 struct string_list *p = &header->prerequisites;
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +0200199 struct rev_info revs = REV_INFO_INIT;
Nguyễn Thái Ngọc Duya80aad72009-05-21 19:32:44 +1000200 const char *argv[] = {NULL, "--all", NULL};
Johannes Schindelin30415d52007-09-10 23:03:15 -0400201 struct commit *commit;
202 int i, ret = 0, req_nr;
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700203 const char *message = _("Repository lacks these prerequisite commits:");
Johannes Schindelin30415d52007-09-10 23:03:15 -0400204
Derrick Stoleec96060b2022-10-12 12:52:35 +0000205 if (!r || !r->objects || !r->objects->odb)
206 return error(_("need a repository to verify a bundle"));
Johannes Schindelin3bbbe462019-05-27 12:59:14 -0700207
Nguyễn Thái Ngọc Duy74ae4b62018-11-10 06:49:01 +0100208 repo_init_revisions(r, &revs, NULL);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400209 for (i = 0; i < p->nr; i++) {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200210 struct string_list_item *e = p->items + i;
211 const char *name = e->string;
212 struct object_id *oid = e->util;
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200213 struct object *o = parse_object(r, oid);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400214 if (o) {
215 o->flags |= PREREQ_MARK;
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200216 add_pending_object(&revs, o, name);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400217 continue;
218 }
Derrick Stolee70334fc2022-10-12 12:52:38 +0000219 ret++;
220 if (flags & VERIFY_BUNDLE_QUIET)
221 continue;
222 if (ret == 1)
Daniel Lowe9db56f72008-11-10 16:07:52 -0500223 error("%s", message);
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200224 error("%s %s", oid_to_hex(oid), name);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400225 }
226 if (revs.pending.nr != p->nr)
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +0200227 goto cleanup;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400228 req_nr = revs.pending.nr;
229 setup_revisions(2, argv, &revs, NULL);
230
Derrick Stolee105c6f12022-03-09 16:01:39 +0000231 list_objects_filter_copy(&revs.filter, &header->filter);
232
Martin Koegler3d51e1b2008-02-18 08:31:56 +0100233 if (prepare_revision_walk(&revs))
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700234 die(_("revision walk setup failed"));
Johannes Schindelin30415d52007-09-10 23:03:15 -0400235
236 i = req_nr;
237 while (i && (commit = get_revision(&revs)))
238 if (commit->object.flags & PREREQ_MARK)
239 i--;
240
René Scharfe63647392017-12-25 18:46:14 +0100241 for (i = 0; i < p->nr; i++) {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200242 struct string_list_item *e = p->items + i;
243 const char *name = e->string;
244 const struct object_id *oid = e->util;
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200245 struct object *o = parse_object(r, oid);
René Scharfe63647392017-12-25 18:46:14 +0100246 assert(o); /* otherwise we'd have returned early */
247 if (o->flags & SHOWN)
248 continue;
Derrick Stolee70334fc2022-10-12 12:52:38 +0000249 ret++;
250 if (flags & VERIFY_BUNDLE_QUIET)
251 continue;
252 if (ret == 1)
René Scharfe63647392017-12-25 18:46:14 +0100253 error("%s", message);
Ævar Arnfjörð Bjarmason15e7c7d2021-07-02 11:57:31 +0200254 error("%s %s", oid_to_hex(oid), name);
René Scharfe63647392017-12-25 18:46:14 +0100255 }
Johannes Schindelin30415d52007-09-10 23:03:15 -0400256
Derrick Stolee89bd7fe2022-10-12 12:52:37 +0000257 if (flags & VERIFY_BUNDLE_VERBOSE) {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200258 struct string_list *r;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400259
260 r = &header->references;
Lukas Fleischera02ffe02013-03-08 19:01:26 +0100261 printf_ln(Q_("The bundle contains this ref:",
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +0100262 "The bundle contains these %"PRIuMAX" refs:",
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700263 r->nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +0100264 (uintmax_t)r->nr);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400265 list_refs(r, 0, NULL);
Derrick Stolee105c6f12022-03-09 16:01:39 +0000266
Lukas Fleischer71ba6b12013-03-07 01:56:35 +0100267 r = &header->prerequisites;
Junio C Hamano8c3710f2012-06-04 11:51:13 -0700268 if (!r->nr) {
269 printf_ln(_("The bundle records a complete history."));
270 } else {
Lukas Fleischera02ffe02013-03-08 19:01:26 +0100271 printf_ln(Q_("The bundle requires this ref:",
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +0100272 "The bundle requires these %"PRIuMAX" refs:",
Junio C Hamano8c3710f2012-06-04 11:51:13 -0700273 r->nr),
Ævar Arnfjörð Bjarmason99d60542022-03-07 16:27:08 +0100274 (uintmax_t)r->nr);
Junio C Hamano8c3710f2012-06-04 11:51:13 -0700275 list_refs(r, 0, NULL);
276 }
Derrick Stolee017303e2022-03-22 17:28:38 +0000277
Derrick Stolee8ba221e2022-03-22 17:28:39 +0000278 printf_ln("The bundle uses this hash algorithm: %s",
279 header->hash_algo->name);
Derrick Stolee017303e2022-03-22 17:28:38 +0000280 if (header->filter.choice)
281 printf_ln("The bundle uses this filter: %s",
282 list_objects_filter_spec(&header->filter));
Johannes Schindelin30415d52007-09-10 23:03:15 -0400283 }
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +0200284cleanup:
Derrick Stoleec96060b2022-10-12 12:52:35 +0000285 /* Clean up objects used, as they will be reused. */
286 for (i = 0; i < p->nr; i++) {
287 struct string_list_item *e = p->items + i;
288 struct object_id *oid = e->util;
289 commit = lookup_commit_reference_gently(r, oid, 1);
290 if (commit)
291 clear_commit_marks(commit, ALL_REV_FLAGS | PREREQ_MARK);
292 }
Ævar Arnfjörð Bjarmasonf196c1e2022-04-13 22:01:38 +0200293 release_revisions(&revs);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400294 return ret;
295}
296
297int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
298{
299 return list_refs(&header->references, argc, argv);
300}
301
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100302static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
303{
304 unsigned long size;
305 enum object_type type;
René Scharfe64045942014-10-04 00:40:24 +0200306 char *buf = NULL, *line, *lineend;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200307 timestamp_t date;
René Scharfe64045942014-10-04 00:40:24 +0200308 int result = 1;
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100309
310 if (revs->max_age == -1 && revs->min_age == -1)
René Scharfe64045942014-10-04 00:40:24 +0200311 goto out;
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100312
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000313 buf = read_object_file(&tag->oid, &type, &size);
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100314 if (!buf)
René Scharfe64045942014-10-04 00:40:24 +0200315 goto out;
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100316 line = memmem(buf, size, "\ntagger ", 8);
317 if (!line++)
René Scharfe64045942014-10-04 00:40:24 +0200318 goto out;
Lukas Fleischer2c8544a2014-08-02 10:39:06 +0200319 lineend = memchr(line, '\n', buf + size - line);
320 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100321 if (!line++)
René Scharfe64045942014-10-04 00:40:24 +0200322 goto out;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200323 date = parse_timestamp(line, NULL, 10);
René Scharfe64045942014-10-04 00:40:24 +0200324 result = (revs->max_age == -1 || revs->max_age < date) &&
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100325 (revs->min_age == -1 || revs->min_age > date);
René Scharfe64045942014-10-04 00:40:24 +0200326out:
327 free(buf);
328 return result;
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100329}
330
Michael Haggertye54c3472015-08-10 11:47:37 +0200331
Jeff King2c8ee1f2018-11-16 04:43:59 -0500332/* Write the pack data to bundle_fd */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400333static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
Junio C Hamano5e626b92014-10-30 10:45:41 -0700334{
335 struct child_process pack_objects = CHILD_PROCESS_INIT;
336 int i;
337
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400338 strvec_pushl(&pack_objects.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400339 "pack-objects",
340 "--stdout", "--thin", "--delta-base-offset",
341 NULL);
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400342 strvec_pushv(&pack_objects.args, pack_options->v);
Derrick Stoleef18b5122022-03-09 16:01:41 +0000343 if (revs->filter.choice)
344 strvec_pushf(&pack_objects.args, "--filter=%s",
345 list_objects_filter_spec(&revs->filter));
Junio C Hamano5e626b92014-10-30 10:45:41 -0700346 pack_objects.in = -1;
347 pack_objects.out = bundle_fd;
348 pack_objects.git_cmd = 1;
Jeff King2c8ee1f2018-11-16 04:43:59 -0500349
350 /*
351 * start_command() will close our descriptor if it's >1. Duplicate it
352 * to avoid surprising the caller.
353 */
354 if (pack_objects.out > 1) {
355 pack_objects.out = dup(pack_objects.out);
356 if (pack_objects.out < 0) {
357 error_errno(_("unable to dup bundle descriptor"));
358 child_process_clear(&pack_objects);
359 return -1;
360 }
361 }
362
Junio C Hamano5e626b92014-10-30 10:45:41 -0700363 if (start_command(&pack_objects))
364 return error(_("Could not spawn pack-objects"));
365
Junio C Hamano5e626b92014-10-30 10:45:41 -0700366 for (i = 0; i < revs->pending.nr; i++) {
367 struct object *object = revs->pending.objects[i].item;
368 if (object->flags & UNINTERESTING)
369 write_or_die(pack_objects.in, "^", 1);
brian m. carlson703d2d42019-08-18 20:04:11 +0000370 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
Junio C Hamano5e626b92014-10-30 10:45:41 -0700371 write_or_die(pack_objects.in, "\n", 1);
372 }
373 close(pack_objects.in);
374 if (finish_command(&pack_objects))
375 return error(_("pack-objects died"));
376 return 0;
377}
378
Jeff Kingd9362ef2014-10-30 17:35:24 -0400379/*
380 * Write out bundle refs based on the tips already
381 * parsed into revs.pending. As a side effect, may
382 * manipulate revs.pending to include additional
383 * necessary objects (like tags).
384 *
385 * Returns the number of refs written, or negative
386 * on error.
387 */
388static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
Junio C Hamanoe8eb2512014-10-30 11:01:37 -0700389{
Jeff Kingd9362ef2014-10-30 17:35:24 -0400390 int i;
391 int ref_count = 0;
Junio C Hamanoe8eb2512014-10-30 11:01:37 -0700392
Jeff Kingd9362ef2014-10-30 17:35:24 -0400393 for (i = 0; i < revs->pending.nr; i++) {
394 struct object_array_entry *e = revs->pending.objects + i;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000395 struct object_id oid;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400396 char *ref;
Junio C Hamanofa303832007-11-22 16:51:18 -0800397 const char *display_ref;
398 int flag;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400399
400 if (e->item->flags & UNINTERESTING)
401 continue;
Jonathan Tanf24c30e2020-09-01 15:28:09 -0700402 if (dwim_ref(e->name, strlen(e->name), &oid, &ref, 0) != 1)
Stefan Bellerc8a571d2015-03-10 16:51:48 -0700403 goto skip_write_ref;
brian m. carlson34c290a2017-10-15 22:06:56 +0000404 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
Junio C Hamanofa303832007-11-22 16:51:18 -0800405 flag = 0;
406 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
407
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100408 if (e->item->type == OBJ_TAG &&
Jeff Kingd9362ef2014-10-30 17:35:24 -0400409 !is_tag_in_date_range(e->item, revs)) {
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100410 e->item->flags |= UNINTERESTING;
Stefan Bellerc8a571d2015-03-10 16:51:48 -0700411 goto skip_write_ref;
Johannes Schindelinc9a42c42009-01-02 19:08:46 +0100412 }
413
Johannes Schindelin30415d52007-09-10 23:03:15 -0400414 /*
415 * Make sure the refs we wrote out is correct; --max-count and
416 * other limiting options could have prevented all the tips
417 * from getting output.
418 *
419 * Non commit objects such as tags and blobs do not have
420 * this issue as they are not affected by those extra
421 * constraints.
422 */
423 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700424 warning(_("ref '%s' is excluded by the rev-list options"),
Johannes Schindelin30415d52007-09-10 23:03:15 -0400425 e->name);
Stefan Bellerc8a571d2015-03-10 16:51:48 -0700426 goto skip_write_ref;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400427 }
428 /*
429 * If you run "git bundle create bndl v1.0..v2.0", the
430 * name of the positive ref is "v2.0" but that is the
431 * commit that is referenced by the tag, and not the tag
432 * itself.
433 */
Jeff King9001dc22018-08-28 17:22:48 -0400434 if (!oideq(&oid, &e->item->oid)) {
Johannes Schindelin30415d52007-09-10 23:03:15 -0400435 /*
436 * Is this the positive end of a range expressed
437 * in terms of a tag (e.g. v2.0 from the range
438 * "v1.0..v2.0")?
439 */
Nguyễn Thái Ngọc Duy74ae4b62018-11-10 06:49:01 +0100440 struct commit *one = lookup_commit_reference(revs->repo, &oid);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400441 struct object *obj;
442
443 if (e->item == &(one->object)) {
444 /*
445 * Need to include e->name as an
446 * independent ref to the pack-objects
447 * input, so that the tag is included
448 * in the output; otherwise we would
449 * end up triggering "empty bundle"
450 * error.
451 */
brian m. carlsonc251c832017-05-06 22:10:38 +0000452 obj = parse_object_or_die(&oid, e->name);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400453 obj->flags |= SHOWN;
Jeff Kingd9362ef2014-10-30 17:35:24 -0400454 add_pending_object(revs, obj, e->name);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400455 }
Stefan Bellerc8a571d2015-03-10 16:51:48 -0700456 goto skip_write_ref;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400457 }
458
459 ref_count++;
brian m. carlson703d2d42019-08-18 20:04:11 +0000460 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400461 write_or_die(bundle_fd, " ", 1);
Junio C Hamanofa303832007-11-22 16:51:18 -0800462 write_or_die(bundle_fd, display_ref, strlen(display_ref));
Johannes Schindelin30415d52007-09-10 23:03:15 -0400463 write_or_die(bundle_fd, "\n", 1);
Stefan Bellerc8a571d2015-03-10 16:51:48 -0700464 skip_write_ref:
Johannes Schindelin30415d52007-09-10 23:03:15 -0400465 free(ref);
466 }
Johannes Schindelin30415d52007-09-10 23:03:15 -0400467
468 /* end header */
469 write_or_die(bundle_fd, "\n", 1);
Jeff Kingd9362ef2014-10-30 17:35:24 -0400470 return ref_count;
471}
472
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500473struct bundle_prerequisites_info {
474 struct object_array *pending;
475 int fd;
476};
477
478static void write_bundle_prerequisites(struct commit *commit, void *data)
479{
480 struct bundle_prerequisites_info *bpi = data;
481 struct object *object;
482 struct pretty_print_context ctx = { 0 };
483 struct strbuf buf = STRBUF_INIT;
484
485 if (!(commit->object.flags & BOUNDARY))
486 return;
487 strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid));
488 write_or_die(bpi->fd, buf.buf, buf.len);
489
490 ctx.fmt = CMIT_FMT_ONELINE;
491 ctx.output_encoding = get_log_output_encoding();
492 strbuf_reset(&buf);
493 pretty_print_commit(&ctx, commit, &buf);
494 strbuf_trim(&buf);
495
496 object = (struct object *)commit;
497 object->flags |= UNINTERESTING;
498 add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID,
499 NULL);
500 strbuf_addch(&buf, '\n');
501 write_or_die(bpi->fd, buf.buf, buf.len);
502 strbuf_release(&buf);
503}
504
Jeff Kingfcb133e2019-01-24 08:11:51 -0500505int create_bundle(struct repository *r, const char *path,
Junio C Hamanoe0ad9572020-08-11 18:04:11 -0700506 int argc, const char **argv, struct strvec *pack_options, int version)
Jeff Kingd9362ef2014-10-30 17:35:24 -0400507{
Martin Ågrenb2275862018-05-09 22:55:38 +0200508 struct lock_file lock = LOCK_INIT;
Jeff Kingd9362ef2014-10-30 17:35:24 -0400509 int bundle_fd = -1;
510 int bundle_to_stdout;
511 int ref_count = 0;
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500512 struct rev_info revs, revs_copy;
Derrick Stoleef18b5122022-03-09 16:01:41 +0000513 int min_version = 2;
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500514 struct bundle_prerequisites_info bpi;
515 int i;
Jeff Kingd9362ef2014-10-30 17:35:24 -0400516
Derrick Stoleef18b5122022-03-09 16:01:41 +0000517 /* init revs to list objects for pack-objects later */
518 save_commit_buffer = 0;
519 repo_init_revisions(r, &revs, NULL);
520
521 /*
522 * Pre-initialize the '--objects' flag so we can parse a
523 * --filter option successfully.
524 */
525 revs.tree_objects = revs.blob_objects = 1;
526
527 argc = setup_revisions(argc, argv, &revs, NULL);
528
529 /*
530 * Reasons to require version 3:
531 *
532 * 1. @object-format is required because our hash algorithm is not
533 * SHA1.
534 * 2. @filter is required because we parsed an object filter.
535 */
536 if (the_hash_algo != &hash_algos[GIT_HASH_SHA1] || revs.filter.choice)
537 min_version = 3;
538
539 if (argc > 1) {
540 error(_("unrecognized argument: %s"), argv[1]);
541 goto err;
542 }
543
Jeff Kingd9362ef2014-10-30 17:35:24 -0400544 bundle_to_stdout = !strcmp(path, "-");
545 if (bundle_to_stdout)
546 bundle_fd = 1;
Jeff King2c8ee1f2018-11-16 04:43:59 -0500547 else
Jeff Kingd9362ef2014-10-30 17:35:24 -0400548 bundle_fd = hold_lock_file_for_update(&lock, path,
549 LOCK_DIE_ON_ERROR);
550
brian m. carlsonc5aecfc2020-07-29 23:14:20 +0000551 if (version == -1)
552 version = min_version;
553
554 if (version < 2 || version > 3) {
555 die(_("unsupported bundle version %d"), version);
556 } else if (version < min_version) {
557 die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
558 } else if (version == 2) {
559 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
560 } else {
561 const char *capability = "@object-format=";
562 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
563 write_or_die(bundle_fd, capability, strlen(capability));
564 write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
565 write_or_die(bundle_fd, "\n", 1);
Jeff Kingd9362ef2014-10-30 17:35:24 -0400566
Derrick Stoleef18b5122022-03-09 16:01:41 +0000567 if (revs.filter.choice) {
568 const char *value = expand_list_objects_filter_spec(&revs.filter);
569 capability = "@filter=";
570 write_or_die(bundle_fd, capability, strlen(capability));
571 write_or_die(bundle_fd, value, strlen(value));
572 write_or_die(bundle_fd, "\n", 1);
573 }
Stefan Bellerf5ff5fb2016-03-31 17:35:45 -0700574 }
Jeff Kingd9362ef2014-10-30 17:35:24 -0400575
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500576 /* save revs.pending in revs_copy for later use */
577 memcpy(&revs_copy, &revs, sizeof(revs));
578 revs_copy.pending.nr = 0;
579 revs_copy.pending.alloc = 0;
580 revs_copy.pending.objects = NULL;
581 for (i = 0; i < revs.pending.nr; i++) {
582 struct object_array_entry *e = revs.pending.objects + i;
583 if (e)
584 add_object_array_with_path(e->item, e->name,
585 &revs_copy.pending,
586 e->mode, e->path);
587 }
Jeff Kingd9362ef2014-10-30 17:35:24 -0400588
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500589 /* write prerequisites */
590 revs.boundary = 1;
591 if (prepare_revision_walk(&revs))
592 die("revision walk setup failed");
593 bpi.fd = bundle_fd;
594 bpi.pending = &revs_copy.pending;
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +0000595
Derrick Stoleef18b5122022-03-09 16:01:41 +0000596 /*
597 * Remove any object walking here. We only care about commits and
598 * tags here. The revs_copy has the right instances of these values.
599 */
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +0000600 revs.blob_objects = revs.tree_objects = 0;
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500601 traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi);
602 object_array_remove_duplicates(&revs_copy.pending);
603
604 /* write bundle refs */
605 ref_count = write_bundle_refs(bundle_fd, &revs_copy);
Jeff Kingd9362ef2014-10-30 17:35:24 -0400606 if (!ref_count)
607 die(_("Refusing to create empty bundle."));
608 else if (ref_count < 0)
Stefan Bellerf5ff5fb2016-03-31 17:35:45 -0700609 goto err;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400610
611 /* write pack */
Jiang Xin5bb0fd22021-01-11 21:27:03 -0500612 if (write_pack_data(bundle_fd, &revs_copy, pack_options))
Stefan Bellerf5ff5fb2016-03-31 17:35:45 -0700613 goto err;
Brandon Casey4ed7cd32008-01-16 13:12:46 -0600614
Csaba Henk0f5cdf62010-08-27 20:31:47 +0000615 if (!bundle_to_stdout) {
616 if (commit_lock_file(&lock))
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700617 die_errno(_("cannot create '%s'"), path);
Csaba Henk0f5cdf62010-08-27 20:31:47 +0000618 }
Johannes Sixtc20181e2008-02-21 23:42:56 +0100619 return 0;
Stefan Bellerf5ff5fb2016-03-31 17:35:45 -0700620err:
Jeff King2c8ee1f2018-11-16 04:43:59 -0500621 rollback_lock_file(&lock);
Stefan Bellerf5ff5fb2016-03-31 17:35:45 -0700622 return -1;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400623}
624
Nguyễn Thái Ngọc Duy74ae4b62018-11-10 06:49:01 +0100625int unbundle(struct repository *r, struct bundle_header *header,
Derrick Stolee89bd7fe2022-10-12 12:52:37 +0000626 int bundle_fd, struct strvec *extra_index_pack_args,
627 enum verify_bundle_flags flags)
Johannes Schindelin30415d52007-09-10 23:03:15 -0400628{
René Scharfed3180272014-08-19 21:09:35 +0200629 struct child_process ip = CHILD_PROCESS_INIT;
Ævar Arnfjörð Bjarmason73660962021-09-05 09:34:43 +0200630 strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL);
Johannes Schindelin30415d52007-09-10 23:03:15 -0400631
Derrick Stolee4f39eb02022-03-09 16:01:42 +0000632 /* If there is a filter, then we need to create the promisor pack. */
633 if (header->filter.choice)
634 strvec_push(&ip.args, "--promisor=from-bundle");
635
Ævar Arnfjörð Bjarmason73660962021-09-05 09:34:43 +0200636 if (extra_index_pack_args) {
637 strvec_pushv(&ip.args, extra_index_pack_args->v);
638 strvec_clear(extra_index_pack_args);
639 }
Junio C Hamanobe042af2011-09-18 16:52:32 -0700640
Derrick Stolee89bd7fe2022-10-12 12:52:37 +0000641 if (verify_bundle(r, header, flags))
Johannes Schindelin30415d52007-09-10 23:03:15 -0400642 return -1;
Johannes Schindelin30415d52007-09-10 23:03:15 -0400643 ip.in = bundle_fd;
644 ip.no_stdout = 1;
645 ip.git_cmd = 1;
646 if (run_command(&ip))
Nguyễn Thái Ngọc Duy8a1e7ea2012-04-23 19:30:30 +0700647 return error(_("index-pack died"));
Johannes Schindelin30415d52007-09-10 23:03:15 -0400648 return 0;
649}