Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 2 | #include "pkt-line.h" |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 3 | #include "fetch-pack.h" |
Shawn O. Pearce | fa74052 | 2007-09-19 00:49:35 -0400 | [diff] [blame] | 4 | |
Junio C Hamano | 33b8303 | 2005-08-12 02:08:29 -0700 | [diff] [blame] | 5 | static const char fetch_pack_usage[] = |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 6 | "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] " |
| 7 | "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " |
| 8 | "[--no-progress] [-v] [<host>:]<directory> [<refs>...]"; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 9 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 10 | static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc, |
| 11 | const char *name, int namelen) |
| 12 | { |
| 13 | struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1); |
| 14 | |
| 15 | memcpy(ref->name, name, namelen); |
| 16 | ref->name[namelen] = '\0'; |
| 17 | (*nr)++; |
| 18 | ALLOC_GROW(*sought, *nr, *alloc); |
| 19 | (*sought)[*nr - 1] = ref; |
| 20 | } |
| 21 | |
| 22 | static void add_sought_entry(struct ref ***sought, int *nr, int *alloc, |
| 23 | const char *string) |
| 24 | { |
| 25 | add_sought_entry_mem(sought, nr, alloc, string, strlen(string)); |
| 26 | } |
| 27 | |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 28 | int cmd_fetch_pack(int argc, const char **argv, const char *prefix) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 29 | { |
Michael Haggerty | 57e6fc69 | 2012-05-21 09:59:59 +0200 | [diff] [blame] | 30 | int i, ret; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 31 | struct ref *ref = NULL; |
Michael Haggerty | 9d19c6e | 2012-05-21 09:59:56 +0200 | [diff] [blame] | 32 | const char *dest = NULL; |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 33 | struct ref **sought = NULL; |
| 34 | int nr_sought = 0, alloc_sought = 0; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 35 | int fd[2]; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 36 | char *pack_lockfile = NULL; |
| 37 | char **pack_lockfile_ptr = NULL; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 38 | struct child_process *conn; |
Nguyễn Thái Ngọc Duy | f8eb303 | 2012-10-26 22:53:54 +0700 | [diff] [blame] | 39 | struct fetch_pack_args args; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 40 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 41 | packet_trace_identity("fetch-pack"); |
| 42 | |
Nguyễn Thái Ngọc Duy | f8eb303 | 2012-10-26 22:53:54 +0700 | [diff] [blame] | 43 | memset(&args, 0, sizeof(args)); |
| 44 | args.uploadpack = "git-upload-pack"; |
| 45 | |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 46 | for (i = 1; i < argc && *argv[i] == '-'; i++) { |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 47 | const char *arg = argv[i]; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 48 | |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 49 | if (!prefixcmp(arg, "--upload-pack=")) { |
| 50 | args.uploadpack = arg + 14; |
| 51 | continue; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 52 | } |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 53 | if (!prefixcmp(arg, "--exec=")) { |
| 54 | args.uploadpack = arg + 7; |
| 55 | continue; |
| 56 | } |
| 57 | if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { |
| 58 | args.quiet = 1; |
| 59 | continue; |
| 60 | } |
| 61 | if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { |
| 62 | args.lock_pack = args.keep_pack; |
| 63 | args.keep_pack = 1; |
| 64 | continue; |
| 65 | } |
| 66 | if (!strcmp("--thin", arg)) { |
| 67 | args.use_thin_pack = 1; |
| 68 | continue; |
| 69 | } |
| 70 | if (!strcmp("--include-tag", arg)) { |
| 71 | args.include_tag = 1; |
| 72 | continue; |
| 73 | } |
| 74 | if (!strcmp("--all", arg)) { |
| 75 | args.fetch_all = 1; |
| 76 | continue; |
| 77 | } |
| 78 | if (!strcmp("--stdin", arg)) { |
| 79 | args.stdin_refs = 1; |
| 80 | continue; |
| 81 | } |
| 82 | if (!strcmp("-v", arg)) { |
| 83 | args.verbose = 1; |
| 84 | continue; |
| 85 | } |
| 86 | if (!prefixcmp(arg, "--depth=")) { |
| 87 | args.depth = strtol(arg + 8, NULL, 0); |
| 88 | continue; |
| 89 | } |
| 90 | if (!strcmp("--no-progress", arg)) { |
| 91 | args.no_progress = 1; |
| 92 | continue; |
| 93 | } |
| 94 | if (!strcmp("--stateless-rpc", arg)) { |
| 95 | args.stateless_rpc = 1; |
| 96 | continue; |
| 97 | } |
| 98 | if (!strcmp("--lock-pack", arg)) { |
| 99 | args.lock_pack = 1; |
| 100 | pack_lockfile_ptr = &pack_lockfile; |
| 101 | continue; |
| 102 | } |
| 103 | usage(fetch_pack_usage); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 104 | } |
Michael Haggerty | 4cc00fc | 2012-05-21 09:59:57 +0200 | [diff] [blame] | 105 | |
| 106 | if (i < argc) |
| 107 | dest = argv[i++]; |
| 108 | else |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 109 | usage(fetch_pack_usage); |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 110 | |
Michael Haggerty | 57e6fc69 | 2012-05-21 09:59:59 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Copy refs from cmdline to growable list, then append any |
| 113 | * refs from the standard input: |
| 114 | */ |
Michael Haggerty | 57e6fc69 | 2012-05-21 09:59:59 +0200 | [diff] [blame] | 115 | for (; i < argc; i++) |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 116 | add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]); |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 117 | if (args.stdin_refs) { |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 118 | if (args.stateless_rpc) { |
| 119 | /* in stateless RPC mode we use pkt-line to read |
| 120 | * from stdin, until we get a flush packet |
| 121 | */ |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 122 | for (;;) { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 123 | char *line = packet_read_line(0, NULL); |
| 124 | if (!line) |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 125 | break; |
Junio C Hamano | e013bda | 2013-04-01 08:59:37 -0700 | [diff] [blame] | 126 | add_sought_entry(&sought, &nr_sought, &alloc_sought, line); |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | else { |
| 130 | /* read from stdin one ref per line, until EOF */ |
| 131 | struct strbuf line = STRBUF_INIT; |
Michael Haggerty | 8bee93d | 2012-09-09 08:19:40 +0200 | [diff] [blame] | 132 | while (strbuf_getline(&line, stdin, '\n') != EOF) |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 133 | add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf); |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 134 | strbuf_release(&line); |
| 135 | } |
| 136 | } |
| 137 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 138 | if (args.stateless_rpc) { |
| 139 | conn = NULL; |
| 140 | fd[0] = 0; |
| 141 | fd[1] = 1; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 142 | } else { |
Michael Haggerty | 9d19c6e | 2012-05-21 09:59:56 +0200 | [diff] [blame] | 143 | conn = git_connect(fd, dest, args.uploadpack, |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 144 | args.verbose ? CONNECT_VERBOSE : 0); |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 145 | } |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 146 | |
Jeff King | 85edf4f | 2013-02-20 15:06:45 -0500 | [diff] [blame] | 147 | get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 148 | |
| 149 | ref = fetch_pack(&args, fd, conn, ref, dest, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 150 | sought, nr_sought, pack_lockfile_ptr); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 151 | if (pack_lockfile) { |
| 152 | printf("lock %s\n", pack_lockfile); |
| 153 | fflush(stdout); |
| 154 | } |
| 155 | close(fd[0]); |
| 156 | close(fd[1]); |
| 157 | if (finish_connect(conn)) |
Michael Haggerty | 7418f1a | 2012-09-09 08:19:46 +0200 | [diff] [blame] | 158 | return 1; |
Junio C Hamano | 9e5d2b4 | 2005-11-06 00:09:59 -0800 | [diff] [blame] | 159 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 160 | ret = !ref; |
Michael Haggerty | b285668 | 2012-09-09 08:19:48 +0200 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * If the heads to pull were given, we should have consumed |
| 164 | * all of them by matching the remote. Otherwise, 'git fetch |
| 165 | * remote no-such-ref' would silently succeed without issuing |
| 166 | * an error. |
| 167 | */ |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 168 | for (i = 0; i < nr_sought; i++) { |
| 169 | if (!sought[i] || sought[i]->matched) |
| 170 | continue; |
| 171 | error("no such remote ref %s", sought[i]->name); |
| 172 | ret = 1; |
| 173 | } |
| 174 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 175 | while (ref) { |
| 176 | printf("%s %s\n", |
| 177 | sha1_to_hex(ref->old_sha1), ref->name); |
| 178 | ref = ref->next; |
| 179 | } |
Junio C Hamano | 9e5d2b4 | 2005-11-06 00:09:59 -0800 | [diff] [blame] | 180 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 181 | return ret; |
| 182 | } |