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 | |
Heikki Orsila | 34baebc | 2008-08-30 14:12:53 +0300 | [diff] [blame] | 12 | static const char *bundle_usage="git bundle (create <bundle> <git rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )"; |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 13 | |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 14 | int cmd_bundle(int argc, const char **argv, const char *prefix) |
| 15 | { |
| 16 | struct bundle_header header; |
SZEDER Gábor | af05d67 | 2008-03-25 22:06:26 +0100 | [diff] [blame] | 17 | int nongit; |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 18 | const char *cmd, *bundle_file; |
| 19 | int bundle_fd = -1; |
| 20 | char buffer[PATH_MAX]; |
| 21 | |
| 22 | if (argc < 3) |
| 23 | usage(bundle_usage); |
| 24 | |
| 25 | cmd = argv[1]; |
| 26 | bundle_file = argv[2]; |
| 27 | argc -= 2; |
| 28 | argv += 2; |
| 29 | |
| 30 | prefix = setup_git_directory_gently(&nongit); |
| 31 | if (prefix && bundle_file[0] != '/') { |
| 32 | snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file); |
| 33 | bundle_file = buffer; |
| 34 | } |
| 35 | |
| 36 | memset(&header, 0, sizeof(header)); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 37 | if (strcmp(cmd, "create") && (bundle_fd = |
| 38 | read_bundle_header(bundle_file, &header)) < 0) |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 39 | return 1; |
| 40 | |
| 41 | if (!strcmp(cmd, "verify")) { |
| 42 | close(bundle_fd); |
Junio C Hamano | 80e25ce | 2007-03-05 16:17:27 -0800 | [diff] [blame] | 43 | if (verify_bundle(&header, 1)) |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 44 | return 1; |
| 45 | fprintf(stderr, "%s is okay\n", bundle_file); |
| 46 | return 0; |
| 47 | } |
| 48 | if (!strcmp(cmd, "list-heads")) { |
| 49 | close(bundle_fd); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 50 | return !!list_bundle_refs(&header, argc, argv); |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 51 | } |
| 52 | if (!strcmp(cmd, "create")) { |
| 53 | if (nongit) |
| 54 | die("Need a repository to create a bundle."); |
| 55 | return !!create_bundle(&header, bundle_file, argc, argv); |
| 56 | } else if (!strcmp(cmd, "unbundle")) { |
| 57 | if (nongit) |
| 58 | die("Need a repository to unbundle."); |
Johannes Schindelin | 30415d5 | 2007-09-10 23:03:15 -0400 | [diff] [blame] | 59 | return !!unbundle(&header, bundle_fd) || |
| 60 | list_bundle_refs(&header, argc, argv); |
Johannes Schindelin | 2e0afaf | 2007-02-22 01:59:14 +0100 | [diff] [blame] | 61 | } else |
| 62 | usage(bundle_usage); |
| 63 | } |