]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/send-pack.c
prefix_filename: return newly allocated string
[thirdparty/git.git] / builtin / send-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
2a9c3fe8 2#include "commit.h"
584c6cc9 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
de1a2fdd 5#include "sideband.h"
38b1c662 6#include "run-command.h"
6b62816c 7#include "remote.h"
47a59185 8#include "connect.h"
96249c04 9#include "send-pack.h"
de1a2fdd 10#include "quote.h"
f1863d0d 11#include "transport.h"
ff5effdf 12#include "version.h"
13eb4626 13#include "sha1-array.h"
d830d395 14#include "gpg-interface.h"
068c77a5 15#include "gettext.h"
61221472 16
068c77a5
DB
17static const char * const send_pack_usage[] = {
18 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
19 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
20 "[<host>:]<directory> [<ref>...]\n"
21 " --all and explicit <ref> specification are mutually exclusive."),
22 NULL,
23};
96249c04 24
6828f72f 25static struct send_pack_args args;
61221472 26
de1a2fdd
SP
27static void print_helper_status(struct ref *ref)
28{
29 struct strbuf buf = STRBUF_INIT;
30
31 for (; ref; ref = ref->next) {
32 const char *msg = NULL;
33 const char *res;
34
35 switch(ref->status) {
36 case REF_STATUS_NONE:
37 res = "error";
38 msg = "no match";
39 break;
40
41 case REF_STATUS_OK:
42 res = "ok";
43 break;
44
45 case REF_STATUS_UPTODATE:
46 res = "ok";
47 msg = "up to date";
48 break;
49
50 case REF_STATUS_REJECT_NONFASTFORWARD:
51 res = "error";
52 msg = "non-fast forward";
dbfeddb1
CR
53 break;
54
75e5c0dc
JH
55 case REF_STATUS_REJECT_FETCH_FIRST:
56 res = "error";
57 msg = "fetch first";
58 break;
59
60 case REF_STATUS_REJECT_NEEDS_FORCE:
61 res = "error";
62 msg = "needs force";
63 break;
64
631b5ef2
JH
65 case REF_STATUS_REJECT_STALE:
66 res = "error";
67 msg = "stale info";
68 break;
69
dbfeddb1
CR
70 case REF_STATUS_REJECT_ALREADY_EXISTS:
71 res = "error";
72 msg = "already exists";
de1a2fdd
SP
73 break;
74
75 case REF_STATUS_REJECT_NODELETE:
76 case REF_STATUS_REMOTE_REJECT:
77 res = "error";
78 break;
79
80 case REF_STATUS_EXPECTING_REPORT:
81 default:
82 continue;
83 }
84
85 strbuf_reset(&buf);
86 strbuf_addf(&buf, "%s %s", res, ref->name);
87 if (ref->remote_status)
88 msg = ref->remote_status;
89 if (msg) {
90 strbuf_addch(&buf, ' ');
91 quote_two_c_style(&buf, "", msg, 0);
92 }
93 strbuf_addch(&buf, '\n');
94
cdf4fb8e 95 write_or_die(1, buf.buf, buf.len);
de1a2fdd
SP
96 }
97 strbuf_release(&buf);
98}
99
68c757f2
DB
100static int send_pack_config(const char *k, const char *v, void *cb)
101{
102 git_gpg_config(k, v, NULL);
103
104 if (!strcmp(k, "push.gpgsign")) {
105 const char *value;
106 if (!git_config_get_value("push.gpgsign", &value)) {
107 switch (git_config_maybe_bool("push.gpgsign", value)) {
108 case 0:
109 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
110 break;
111 case 1:
112 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
113 break;
114 default:
115 if (value && !strcasecmp(value, "if-asked"))
116 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
117 else
118 return error("Invalid value for '%s'", k);
119 }
120 }
121 }
122 return 0;
123}
124
96249c04 125int cmd_send_pack(int argc, const char **argv, const char *prefix)
61221472 126{
64fcef2d
DB
127 int i, nr_refspecs = 0;
128 const char **refspecs = NULL;
96249c04 129 const char *remote_name = NULL;
b516968f 130 struct remote *remote = NULL;
96249c04 131 const char *dest = NULL;
64fcef2d
DB
132 int fd[2];
133 struct child_process *conn;
13eb4626 134 struct sha1_array extra_have = SHA1_ARRAY_INIT;
b016918b 135 struct sha1_array shallow = SHA1_ARRAY_INIT;
6d2bf96e 136 struct ref *remote_refs, *local_refs;
64fcef2d 137 int ret;
de1a2fdd 138 int helper_status = 0;
64fcef2d 139 int send_all = 0;
068c77a5 140 int verbose = 0;
64fcef2d 141 const char *receivepack = "git-receive-pack";
068c77a5
DB
142 unsigned dry_run = 0;
143 unsigned send_mirror = 0;
144 unsigned force_update = 0;
145 unsigned quiet = 0;
30261094 146 int push_cert = 0;
068c77a5
DB
147 unsigned use_thin_pack = 0;
148 unsigned atomic = 0;
149 unsigned stateless_rpc = 0;
64fcef2d 150 int flags;
10643d4e 151 unsigned int reject_reasons;
391b1f20 152 int progress = -1;
26be19ba 153 int from_stdin = 0;
28f5d176 154 struct push_cas_option cas = {0};
84a9b58c 155
068c77a5
DB
156 struct option options[] = {
157 OPT__VERBOSITY(&verbose),
158 OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
159 OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
160 OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
161 OPT_BOOL(0, "all", &send_all, N_("push all refs")),
162 OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
163 OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
164 OPT_BOOL('f', "force", &force_update, N_("force updates")),
30261094
DB
165 { OPTION_CALLBACK,
166 0, "signed", &push_cert, "yes|no|if-asked", N_("GPG sign the push"),
167 PARSE_OPT_OPTARG, option_parse_push_signed },
068c77a5
DB
168 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
169 OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
170 OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
171 OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
172 OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
173 OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
174 { OPTION_CALLBACK,
175 0, CAS_OPT_NAME, &cas, N_("refname>:<expect"),
176 N_("require old value of ref to be at this value"),
177 PARSE_OPT_OPTARG, parseopt_push_cas_option },
178 OPT_END()
179 };
61221472 180
68c757f2 181 git_config(send_pack_config, NULL);
068c77a5
DB
182 argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
183 if (argc > 0) {
184 dest = argv[0];
185 refspecs = (const char **)(argv + 1);
186 nr_refspecs = argc - 1;
61221472 187 }
068c77a5 188
61221472 189 if (!dest)
068c77a5
DB
190 usage_with_options(send_pack_usage, options);
191
192 args.verbose = verbose;
193 args.dry_run = dry_run;
194 args.send_mirror = send_mirror;
195 args.force_update = force_update;
196 args.quiet = quiet;
197 args.push_cert = push_cert;
198 args.progress = progress;
199 args.use_thin_pack = use_thin_pack;
200 args.atomic = atomic;
201 args.stateless_rpc = stateless_rpc;
26be19ba
JK
202
203 if (from_stdin) {
204 struct argv_array all_refspecs = ARGV_ARRAY_INIT;
205
206 for (i = 0; i < nr_refspecs; i++)
207 argv_array_push(&all_refspecs, refspecs[i]);
208
209 if (args.stateless_rpc) {
210 const char *buf;
211 while ((buf = packet_read_line(0, NULL)))
212 argv_array_push(&all_refspecs, buf);
213 } else {
214 struct strbuf line = STRBUF_INIT;
933bea92 215 while (strbuf_getline(&line, stdin) != EOF)
26be19ba
JK
216 argv_array_push(&all_refspecs, line.buf);
217 strbuf_release(&line);
218 }
219
220 refspecs = all_refspecs.argv;
221 nr_refspecs = all_refspecs.argc;
222 }
223
28b9d6e5
AW
224 /*
225 * --all and --mirror are incompatible; neither makes sense
226 * with any refspecs.
227 */
c6777563 228 if ((nr_refspecs > 0 && (send_all || args.send_mirror)) ||
64fcef2d 229 (send_all && args.send_mirror))
068c77a5 230 usage_with_options(send_pack_usage, options);
37adac76 231
b516968f
DB
232 if (remote_name) {
233 remote = remote_get(remote_name);
28b91f8a 234 if (!remote_has_url(remote, dest)) {
b516968f
DB
235 die("Destination %s is not a uri for %s",
236 dest, remote_name);
237 }
238 }
239
391b1f20
JK
240 if (progress == -1)
241 progress = !args.quiet && isatty(2);
242 args.progress = progress;
8d32e60d 243
de1a2fdd
SP
244 if (args.stateless_rpc) {
245 conn = NULL;
246 fd[0] = 0;
247 fd[1] = 1;
248 } else {
5a277f3f 249 conn = git_connect(fd, dest, receivepack,
de1a2fdd
SP
250 args.verbose ? CONNECT_VERBOSE : 0);
251 }
96249c04 252
b016918b
NTND
253 get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
254 &extra_have, &shallow);
64fcef2d 255
f1863d0d 256 transport_verify_remote_names(nr_refspecs, refspecs);
64fcef2d
DB
257
258 local_refs = get_local_heads();
96249c04 259
64fcef2d
DB
260 flags = MATCH_REFS_NONE;
261
262 if (send_all)
263 flags |= MATCH_REFS_ALL;
264 if (args.send_mirror)
265 flags |= MATCH_REFS_MIRROR;
266
267 /* match them up */
29753cdd 268 if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
64fcef2d 269 return -1;
96249c04 270
91048a95
JH
271 if (!is_empty_cas(&cas))
272 apply_push_cas(&cas, remote, remote_refs);
273
20e8b465
TRC
274 set_ref_status_for_push(remote_refs, args.send_mirror,
275 args.force_update);
276
64fcef2d 277 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
96249c04 278
de1a2fdd
SP
279 if (helper_status)
280 print_helper_status(remote_refs);
281
64fcef2d 282 close(fd[1]);
7f8e9828 283 close(fd[0]);
64fcef2d 284
98158e9c 285 ret |= finish_connect(conn);
64fcef2d 286
de1a2fdd 287 if (!helper_status)
10643d4e 288 transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
64fcef2d
DB
289
290 if (!args.dry_run && remote) {
291 struct ref *ref;
292 for (ref = remote_refs; ref; ref = ref->next)
f1863d0d 293 transport_update_tracking_ref(remote, ref, args.verbose);
64fcef2d
DB
294 }
295
f1863d0d 296 if (!ret && !transport_refs_pushed(remote_refs))
64fcef2d
DB
297 fprintf(stderr, "Everything up-to-date\n");
298
299 return ret;
61221472 300}