Peter Hagervall | baffc0e | 2007-07-15 01:14:45 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 2 | #include "cache.h" |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 3 | #include "bundle.h" |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 4 | |
| 5 | /* |
| 6 | * Basic handler for bundle files to connect repositories via sneakernet. |
| 7 | * Invocation must include action. |
| 8 | * This function can create a bundle or provide information on an existing |
Heikki Orsila | 34baebc | 2008-08-30 14:12:53 +0300 | [diff] [blame] | 9 | * bundle supporting "fetch", "pull", and "ls-remote". |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
Thiago Farina | 1f986c4 | 2009-09-17 13:20:53 -0400 | [diff] [blame] | 12 | static const char builtin_bundle_usage[] = |
| 13 | "git bundle create <file> <git-rev-list args>\n" |
| 14 | " or: git bundle verify <file>\n" |
| 15 | " or: git bundle list-heads <file> [refname...]\n" |
| 16 | " or: git bundle unbundle <file> [refname...]"; |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 17 | |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 18 | int cmd_bundle(int argc, const char **argv, const char *prefix) |
| 19 | { |
| 20 | struct bundle_header header; |
SZEDER Gábor | af05d67 | 2008-03-25 22:06:26 +0100 | [diff] [blame] | 21 | int nongit; |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 22 | const char *cmd, *bundle_file; |
| 23 | int bundle_fd = -1; |
| 24 | char buffer[PATH_MAX]; |
| 25 | |
| 26 | if (argc < 3) |
Thiago Farina | 1f986c4 | 2009-09-17 13:20:53 -0400 | [diff] [blame] | 27 | usage(builtin_bundle_usage); |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 28 | |
| 29 | cmd = argv[1]; |
| 30 | bundle_file = argv[2]; |
| 31 | argc -= 2; |
| 32 | argv += 2; |
| 33 | |
| 34 | prefix = setup_git_directory_gently(&nongit); |
| 35 | if (prefix && bundle_file[0] != '/') { |
| 36 | snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file); |
| 37 | bundle_file = buffer; |
| 38 | } |
| 39 | |
| 40 | memset(&header, 0, sizeof(header)); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 41 | if (strcmp(cmd, "create") && (bundle_fd = |
| 42 | read_bundle_header(bundle_file, &header)) < 0) |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 43 | return 1; |
| 44 | |
| 45 | if (!strcmp(cmd, "verify")) { |
| 46 | close(bundle_fd); |
Junio C Hamano | 80e25ce | 2007-03-05 16:17:27 -0800 | [diff] [blame] | 47 | if (verify_bundle(&header, 1)) |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 48 | return 1; |
| 49 | fprintf(stderr, "%s is okay\n", bundle_file); |
| 50 | return 0; |
| 51 | } |
| 52 | if (!strcmp(cmd, "list-heads")) { |
| 53 | close(bundle_fd); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 54 | return !!list_bundle_refs(&header, argc, argv); |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 55 | } |
| 56 | if (!strcmp(cmd, "create")) { |
| 57 | if (nongit) |
| 58 | die("Need a repository to create a bundle."); |
| 59 | return !!create_bundle(&header, bundle_file, argc, argv); |
| 60 | } else if (!strcmp(cmd, "unbundle")) { |
| 61 | if (nongit) |
| 62 | die("Need a repository to unbundle."); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 63 | return !!unbundle(&header, bundle_fd) || |
| 64 | list_bundle_refs(&header, argc, argv); |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 65 | } else |
Thiago Farina | 1f986c4 | 2009-09-17 13:20:53 -0400 | [diff] [blame] | 66 | usage(builtin_bundle_usage); |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 67 | } |