]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch-pack.c
parse_fetch_refspec(): clarify the codeflow a bit
[thirdparty/git.git] / builtin / fetch-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
def88e9a 2#include "pkt-line.h"
2d4177c0 3#include "fetch-pack.h"
fa740529 4
33b83034 5static const char fetch_pack_usage[] =
078b895f
IT
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>...]";
def88e9a 9
50ab5fd3
SP
10int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
11{
57e6fc69 12 int i, ret;
ba227857 13 struct ref *ref = NULL;
9d19c6ea 14 const char *dest = NULL;
8bee93dd 15 struct string_list sought = STRING_LIST_INIT_DUP;
ba227857 16 int fd[2];
249b2004
SP
17 char *pack_lockfile = NULL;
18 char **pack_lockfile_ptr = NULL;
ba227857 19 struct child_process *conn;
f8eb3036 20 struct fetch_pack_args args;
e28714c5 21
bbc30f99
JK
22 packet_trace_identity("fetch-pack");
23
f8eb3036
NTND
24 memset(&args, 0, sizeof(args));
25 args.uploadpack = "git-upload-pack";
26
ff22ff99 27 for (i = 1; i < argc && *argv[i] == '-'; i++) {
2d4177c0 28 const char *arg = argv[i];
def88e9a 29
ff22ff99
MH
30 if (!prefixcmp(arg, "--upload-pack=")) {
31 args.uploadpack = arg + 14;
32 continue;
33 }
34 if (!prefixcmp(arg, "--exec=")) {
35 args.uploadpack = arg + 7;
36 continue;
def88e9a 37 }
ff22ff99
MH
38 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
39 args.quiet = 1;
40 continue;
41 }
42 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
43 args.lock_pack = args.keep_pack;
44 args.keep_pack = 1;
45 continue;
46 }
47 if (!strcmp("--thin", arg)) {
48 args.use_thin_pack = 1;
49 continue;
50 }
51 if (!strcmp("--include-tag", arg)) {
52 args.include_tag = 1;
53 continue;
54 }
55 if (!strcmp("--all", arg)) {
56 args.fetch_all = 1;
57 continue;
def88e9a 58 }
ff22ff99
MH
59 if (!strcmp("--stdin", arg)) {
60 args.stdin_refs = 1;
61 continue;
62 }
63 if (!strcmp("-v", arg)) {
64 args.verbose = 1;
65 continue;
66 }
67 if (!prefixcmp(arg, "--depth=")) {
68 args.depth = strtol(arg + 8, NULL, 0);
69 continue;
70 }
71 if (!strcmp("--no-progress", arg)) {
72 args.no_progress = 1;
73 continue;
74 }
75 if (!strcmp("--stateless-rpc", arg)) {
76 args.stateless_rpc = 1;
77 continue;
78 }
79 if (!strcmp("--lock-pack", arg)) {
80 args.lock_pack = 1;
81 pack_lockfile_ptr = &pack_lockfile;
82 continue;
83 }
84 usage(fetch_pack_usage);
def88e9a 85 }
4cc00fcf
MH
86
87 if (i < argc)
88 dest = argv[i++];
89 else
def88e9a 90 usage(fetch_pack_usage);
2d4177c0 91
57e6fc69
MH
92 /*
93 * Copy refs from cmdline to growable list, then append any
94 * refs from the standard input:
95 */
57e6fc69 96 for (; i < argc; i++)
8bee93dd 97 string_list_append(&sought, xstrdup(argv[i]));
078b895f 98 if (args.stdin_refs) {
078b895f
IT
99 if (args.stateless_rpc) {
100 /* in stateless RPC mode we use pkt-line to read
101 * from stdin, until we get a flush packet
102 */
103 static char line[1000];
104 for (;;) {
105 int n = packet_read_line(0, line, sizeof(line));
106 if (!n)
107 break;
108 if (line[n-1] == '\n')
109 n--;
8bee93dd 110 string_list_append(&sought, xmemdupz(line, n));
078b895f
IT
111 }
112 }
113 else {
114 /* read from stdin one ref per line, until EOF */
115 struct strbuf line = STRBUF_INIT;
8bee93dd
MH
116 while (strbuf_getline(&line, stdin, '\n') != EOF)
117 string_list_append(&sought, strbuf_detach(&line, NULL));
078b895f
IT
118 strbuf_release(&line);
119 }
120 }
121
249b2004
SP
122 if (args.stateless_rpc) {
123 conn = NULL;
124 fd[0] = 0;
125 fd[1] = 1;
ba227857 126 } else {
9d19c6ea 127 conn = git_connect(fd, dest, args.uploadpack,
249b2004
SP
128 args.verbose ? CONNECT_VERBOSE : 0);
129 }
130
afe7c5ff 131 get_remote_heads(fd[0], &ref, 0, NULL);
249b2004
SP
132
133 ref = fetch_pack(&args, fd, conn, ref, dest,
8bee93dd 134 &sought, pack_lockfile_ptr);
249b2004
SP
135 if (pack_lockfile) {
136 printf("lock %s\n", pack_lockfile);
137 fflush(stdout);
ba227857 138 }
249b2004
SP
139 close(fd[0]);
140 close(fd[1]);
141 if (finish_connect(conn))
7418f1a0 142 return 1;
2d4177c0 143
b285668d
MH
144 ret = !ref || sought.nr;
145
146 /*
147 * If the heads to pull were given, we should have consumed
148 * all of them by matching the remote. Otherwise, 'git fetch
149 * remote no-such-ref' would silently succeed without issuing
150 * an error.
151 */
152 for (i = 0; i < sought.nr; i++)
153 error("no such remote ref %s", sought.items[i].string);
2d4177c0
DB
154 while (ref) {
155 printf("%s %s\n",
156 sha1_to_hex(ref->old_sha1), ref->name);
157 ref = ref->next;
158 }
159
160 return ret;
161}