1#include "cache.h"2#include "bundle.h"3#include "object.h"4#include "commit.h"5#include "diff.h"6#include "revision.h"7#include "list-objects.h"8#include "run-command.h"9#include "refs.h"1011static const char bundle_signature[] = "# v2 git bundle\n";1213static void add_to_ref_list(const unsigned char *sha1, const char *name,14struct ref_list *list)15{16if (list->nr + 1 >= list->alloc) {17list->alloc = alloc_nr(list->nr + 1);18list->list = xrealloc(list->list,19list->alloc * sizeof(list->list[0]));20}21memcpy(list->list[list->nr].sha1, sha1, 20);22list->list[list->nr].name = xstrdup(name);23list->nr++;24}2526static int parse_bundle_header(int fd, struct bundle_header *header,27const char *report_path)28{29struct strbuf buf = STRBUF_INIT;30int status = 0;3132/* The bundle header begins with the signature */33if (strbuf_getwholeline_fd(&buf, fd, '\n') ||34strcmp(buf.buf, bundle_signature)) {35if (report_path)36error(_("'%s' does not look like a v2 bundle file"),37report_path);38status = -1;39goto abort;40}4142/* The bundle header ends with an empty line */43while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&44buf.len && buf.buf[0] != '\n') {45unsigned char sha1[20];46int is_prereq = 0;4748if (*buf.buf == '-') {49is_prereq = 1;50strbuf_remove(&buf, 0, 1);51}52strbuf_rtrim(&buf);5354/*55* Tip lines have object name, SP, and refname.56* Prerequisites have object name that is optionally57* followed by SP and subject line.58*/59if (get_sha1_hex(buf.buf, sha1) ||60(buf.len > 40 && !isspace(buf.buf[40])) ||61(!is_prereq && buf.len <= 40)) {62if (report_path)63error(_("unrecognized header: %s%s (%d)"),64(is_prereq ? "-" : ""), buf.buf, (int)buf.len);65status = -1;66break;67} else {68if (is_prereq)69add_to_ref_list(sha1, "", &header->prerequisites);70else71add_to_ref_list(sha1, buf.buf + 41, &header->references);72}73}7475abort:76if (status) {77close(fd);78fd = -1;79}80strbuf_release(&buf);81return fd;82}8384int read_bundle_header(const char *path, struct bundle_header *header)85{86int fd = open(path, O_RDONLY);8788if (fd < 0)89return error(_("could not open '%s'"), path);90return parse_bundle_header(fd, header, path);91}9293int is_bundle(const char *path, int quiet)94{95struct bundle_header header;96int fd = open(path, O_RDONLY);9798if (fd < 0)99return 0;100memset(&header, 0, sizeof(header));101fd = parse_bundle_header(fd, &header, quiet ? NULL : path);102if (fd >= 0)103close(fd);104return (fd >= 0);105}106107static int list_refs(struct ref_list *r, int argc, const char **argv)108{109int i;110111for (i = 0; i < r->nr; i++) {112if (argc > 1) {113int j;114for (j = 1; j < argc; j++)115if (!strcmp(r->list[i].name, argv[j]))116break;117if (j == argc)118continue;119}120printf("%s %s\n", sha1_to_hex(r->list[i].sha1),121r->list[i].name);122}123return 0;124}125126#define PREREQ_MARK (1u<<16)127128int verify_bundle(struct bundle_header *header, int verbose)129{130/*131* Do fast check, then if any prereqs are missing then go line by line132* to be verbose about the errors133*/134struct ref_list *p = &header->prerequisites;135struct rev_info revs;136const char *argv[] = {NULL, "--all", NULL};137struct object_array refs;138struct commit *commit;139int i, ret = 0, req_nr;140const char *message = _("Repository lacks these prerequisite commits:");141142init_revisions(&revs, NULL);143for (i = 0; i < p->nr; i++) {144struct ref_list_entry *e = p->list + i;145struct object *o = parse_object(e->sha1);146if (o) {147o->flags |= PREREQ_MARK;148add_pending_object(&revs, o, e->name);149continue;150}151if (++ret == 1)152error("%s", message);153error("%s %s", sha1_to_hex(e->sha1), e->name);154}155if (revs.pending.nr != p->nr)156return ret;157req_nr = revs.pending.nr;158setup_revisions(2, argv, &revs, NULL);159160refs = revs.pending;161revs.leak_pending = 1;162163if (prepare_revision_walk(&revs))164die(_("revision walk setup failed"));165166i = req_nr;167while (i && (commit = get_revision(&revs)))168if (commit->object.flags & PREREQ_MARK)169i--;170171for (i = 0; i < req_nr; i++)172if (!(refs.objects[i].item->flags & SHOWN)) {173if (++ret == 1)174error("%s", message);175error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),176refs.objects[i].name);177}178179clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);180free(refs.objects);181182if (verbose) {183struct ref_list *r;184185r = &header->references;186printf_ln(Q_("The bundle contains this ref:",187"The bundle contains these %d refs:",188r->nr),189r->nr);190list_refs(r, 0, NULL);191r = &header->prerequisites;192if (!r->nr) {193printf_ln(_("The bundle records a complete history."));194} else {195printf_ln(Q_("The bundle requires this ref:",196"The bundle requires these %d refs:",197r->nr),198r->nr);199list_refs(r, 0, NULL);200}201}202return ret;203}204205int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)206{207return list_refs(&header->references, argc, argv);208}209210static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)211{212unsigned long size;213enum object_type type;214char *buf, *line, *lineend;215unsigned long date;216217if (revs->max_age == -1 && revs->min_age == -1)218return 1;219220buf = read_sha1_file(tag->sha1, &type, &size);221if (!buf)222return 1;223line = memmem(buf, size, "\ntagger ", 8);224if (!line++)225return 1;226lineend = memchr(line, buf + size - line, '\n');227line = memchr(line, lineend ? lineend - line : buf + size - line, '>');228if (!line++)229return 1;230date = strtoul(line, NULL, 10);231free(buf);232return (revs->max_age == -1 || revs->max_age < date) &&233(revs->min_age == -1 || revs->min_age > date);234}235236int create_bundle(struct bundle_header *header, const char *path,237int argc, const char **argv)238{239static struct lock_file lock;240int bundle_fd = -1;241int bundle_to_stdout;242const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));243const char **argv_pack = xmalloc(6 * sizeof(const char *));244int i, ref_count = 0;245struct strbuf buf = STRBUF_INIT;246struct rev_info revs;247struct child_process rls;248FILE *rls_fout;249250bundle_to_stdout = !strcmp(path, "-");251if (bundle_to_stdout)252bundle_fd = 1;253else254bundle_fd = hold_lock_file_for_update(&lock, path,255LOCK_DIE_ON_ERROR);256257/* write signature */258write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));259260/* init revs to list objects for pack-objects later */261save_commit_buffer = 0;262init_revisions(&revs, NULL);263264/* write prerequisites */265memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));266argv_boundary[0] = "rev-list";267argv_boundary[1] = "--boundary";268argv_boundary[2] = "--pretty=oneline";269argv_boundary[argc + 2] = NULL;270memset(&rls, 0, sizeof(rls));271rls.argv = argv_boundary;272rls.out = -1;273rls.git_cmd = 1;274if (start_command(&rls))275return -1;276rls_fout = xfdopen(rls.out, "r");277while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {278unsigned char sha1[20];279if (buf.len > 0 && buf.buf[0] == '-') {280write_or_die(bundle_fd, buf.buf, buf.len);281if (!get_sha1_hex(buf.buf + 1, sha1)) {282struct object *object = parse_object_or_die(sha1, buf.buf);283object->flags |= UNINTERESTING;284add_pending_object(&revs, object, buf.buf);285}286} else if (!get_sha1_hex(buf.buf, sha1)) {287struct object *object = parse_object_or_die(sha1, buf.buf);288object->flags |= SHOWN;289}290}291strbuf_release(&buf);292fclose(rls_fout);293if (finish_command(&rls))294return error(_("rev-list died"));295296/* write references */297argc = setup_revisions(argc, argv, &revs, NULL);298299if (argc > 1)300return error(_("unrecognized argument: %s"), argv[1]);301302object_array_remove_duplicates(&revs.pending);303304for (i = 0; i < revs.pending.nr; i++) {305struct object_array_entry *e = revs.pending.objects + i;306unsigned char sha1[20];307char *ref;308const char *display_ref;309int flag;310311if (e->item->flags & UNINTERESTING)312continue;313if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)314continue;315if (read_ref_full(e->name, sha1, 1, &flag))316flag = 0;317display_ref = (flag & REF_ISSYMREF) ? e->name : ref;318319if (e->item->type == OBJ_TAG &&320!is_tag_in_date_range(e->item, &revs)) {321e->item->flags |= UNINTERESTING;322continue;323}324325/*326* Make sure the refs we wrote out is correct; --max-count and327* other limiting options could have prevented all the tips328* from getting output.329*330* Non commit objects such as tags and blobs do not have331* this issue as they are not affected by those extra332* constraints.333*/334if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {335warning(_("ref '%s' is excluded by the rev-list options"),336e->name);337free(ref);338continue;339}340/*341* If you run "git bundle create bndl v1.0..v2.0", the342* name of the positive ref is "v2.0" but that is the343* commit that is referenced by the tag, and not the tag344* itself.345*/346if (hashcmp(sha1, e->item->sha1)) {347/*348* Is this the positive end of a range expressed349* in terms of a tag (e.g. v2.0 from the range350* "v1.0..v2.0")?351*/352struct commit *one = lookup_commit_reference(sha1);353struct object *obj;354355if (e->item == &(one->object)) {356/*357* Need to include e->name as an358* independent ref to the pack-objects359* input, so that the tag is included360* in the output; otherwise we would361* end up triggering "empty bundle"362* error.363*/364obj = parse_object_or_die(sha1, e->name);365obj->flags |= SHOWN;366add_pending_object(&revs, obj, e->name);367}368free(ref);369continue;370}371372ref_count++;373write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);374write_or_die(bundle_fd, " ", 1);375write_or_die(bundle_fd, display_ref, strlen(display_ref));376write_or_die(bundle_fd, "\n", 1);377free(ref);378}379if (!ref_count)380die(_("Refusing to create empty bundle."));381382/* end header */383write_or_die(bundle_fd, "\n", 1);384385/* write pack */386argv_pack[0] = "pack-objects";387argv_pack[1] = "--all-progress-implied";388argv_pack[2] = "--stdout";389argv_pack[3] = "--thin";390argv_pack[4] = "--delta-base-offset";391argv_pack[5] = NULL;392memset(&rls, 0, sizeof(rls));393rls.argv = argv_pack;394rls.in = -1;395rls.out = bundle_fd;396rls.git_cmd = 1;397if (start_command(&rls))398return error(_("Could not spawn pack-objects"));399400/*401* start_command closed bundle_fd if it was > 1402* so set the lock fd to -1 so commit_lock_file()403* won't fail trying to close it.404*/405lock.fd = -1;406407for (i = 0; i < revs.pending.nr; i++) {408struct object *object = revs.pending.objects[i].item;409if (object->flags & UNINTERESTING)410write_or_die(rls.in, "^", 1);411write_or_die(rls.in, sha1_to_hex(object->sha1), 40);412write_or_die(rls.in, "\n", 1);413}414close(rls.in);415if (finish_command(&rls))416return error(_("pack-objects died"));417if (!bundle_to_stdout) {418if (commit_lock_file(&lock))419die_errno(_("cannot create '%s'"), path);420}421return 0;422}423424int unbundle(struct bundle_header *header, int bundle_fd, int flags)425{426const char *argv_index_pack[] = {"index-pack",427"--fix-thin", "--stdin", NULL, NULL};428struct child_process ip;429430if (flags & BUNDLE_VERBOSE)431argv_index_pack[3] = "-v";432433if (verify_bundle(header, 0))434return -1;435memset(&ip, 0, sizeof(ip));436ip.argv = argv_index_pack;437ip.in = bundle_fd;438ip.no_stdout = 1;439ip.git_cmd = 1;440if (run_command(&ip))441return error(_("index-pack died"));442return 0;443}