7 #include "run-command.h"
10 #include "send-pack.h"
12 #include "transport.h"
14 #include "sha1-array.h"
15 #include "gpg-interface.h"
18 static const char * const send_pack_usage
[] = {
19 N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
20 "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
21 "[<host>:]<directory> [<ref>...]\n"
22 " --all and explicit <ref> specification are mutually exclusive."),
26 static struct send_pack_args args
;
28 static void print_helper_status(struct ref
*ref
)
30 struct strbuf buf
= STRBUF_INIT
;
32 for (; ref
; ref
= ref
->next
) {
33 const char *msg
= NULL
;
46 case REF_STATUS_UPTODATE
:
51 case REF_STATUS_REJECT_NONFASTFORWARD
:
53 msg
= "non-fast forward";
56 case REF_STATUS_REJECT_FETCH_FIRST
:
61 case REF_STATUS_REJECT_NEEDS_FORCE
:
66 case REF_STATUS_REJECT_STALE
:
71 case REF_STATUS_REJECT_ALREADY_EXISTS
:
73 msg
= "already exists";
76 case REF_STATUS_REJECT_NODELETE
:
77 case REF_STATUS_REMOTE_REJECT
:
81 case REF_STATUS_EXPECTING_REPORT
:
87 strbuf_addf(&buf
, "%s %s", res
, ref
->name
);
88 if (ref
->remote_status
)
89 msg
= ref
->remote_status
;
91 strbuf_addch(&buf
, ' ');
92 quote_two_c_style(&buf
, "", msg
, 0);
94 strbuf_addch(&buf
, '\n');
96 write_or_die(1, buf
.buf
, buf
.len
);
101 static int send_pack_config(const char *k
, const char *v
, void *cb
)
103 git_gpg_config(k
, v
, NULL
);
105 if (!strcmp(k
, "push.gpgsign")) {
107 if (!git_config_get_value("push.gpgsign", &value
)) {
108 switch (git_config_maybe_bool("push.gpgsign", value
)) {
110 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
113 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
116 if (value
&& !strcasecmp(value
, "if-asked"))
117 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
119 return error("Invalid value for '%s'", k
);
126 int cmd_send_pack(int argc
, const char **argv
, const char *prefix
)
128 int i
, nr_refspecs
= 0;
129 const char **refspecs
= NULL
;
130 const char *remote_name
= NULL
;
131 struct remote
*remote
= NULL
;
132 const char *dest
= NULL
;
134 struct child_process
*conn
;
135 struct oid_array extra_have
= OID_ARRAY_INIT
;
136 struct oid_array shallow
= OID_ARRAY_INIT
;
137 struct ref
*remote_refs
, *local_refs
;
139 int helper_status
= 0;
142 const char *receivepack
= "git-receive-pack";
143 unsigned dry_run
= 0;
144 unsigned send_mirror
= 0;
145 unsigned force_update
= 0;
148 struct string_list push_options
= STRING_LIST_INIT_NODUP
;
149 unsigned use_thin_pack
= 0;
151 unsigned stateless_rpc
= 0;
153 unsigned int reject_reasons
;
156 struct push_cas_option cas
= {0};
158 struct option options
[] = {
159 OPT__VERBOSITY(&verbose
),
160 OPT_STRING(0, "receive-pack", &receivepack
, "receive-pack", N_("receive pack program")),
161 OPT_STRING(0, "exec", &receivepack
, "receive-pack", N_("receive pack program")),
162 OPT_STRING(0, "remote", &remote_name
, "remote", N_("remote name")),
163 OPT_BOOL(0, "all", &send_all
, N_("push all refs")),
164 OPT_BOOL('n' , "dry-run", &dry_run
, N_("dry run")),
165 OPT_BOOL(0, "mirror", &send_mirror
, N_("mirror all refs")),
166 OPT_BOOL('f', "force", &force_update
, N_("force updates")),
168 0, "signed", &push_cert
, "yes|no|if-asked", N_("GPG sign the push"),
169 PARSE_OPT_OPTARG
, option_parse_push_signed
},
170 OPT_STRING_LIST(0, "push-option", &push_options
,
171 N_("server-specific"),
172 N_("option to transmit")),
173 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
174 OPT_BOOL(0, "thin", &use_thin_pack
, N_("use thin pack")),
175 OPT_BOOL(0, "atomic", &atomic
, N_("request atomic transaction on remote side")),
176 OPT_BOOL(0, "stateless-rpc", &stateless_rpc
, N_("use stateless RPC protocol")),
177 OPT_BOOL(0, "stdin", &from_stdin
, N_("read refs from stdin")),
178 OPT_BOOL(0, "helper-status", &helper_status
, N_("print status from remote helper")),
180 0, CAS_OPT_NAME
, &cas
, N_("refname>:<expect"),
181 N_("require old value of ref to be at this value"),
182 PARSE_OPT_OPTARG
, parseopt_push_cas_option
},
186 git_config(send_pack_config
, NULL
);
187 argc
= parse_options(argc
, argv
, prefix
, options
, send_pack_usage
, 0);
190 refspecs
= (const char **)(argv
+ 1);
191 nr_refspecs
= argc
- 1;
195 usage_with_options(send_pack_usage
, options
);
197 args
.verbose
= verbose
;
198 args
.dry_run
= dry_run
;
199 args
.send_mirror
= send_mirror
;
200 args
.force_update
= force_update
;
202 args
.push_cert
= push_cert
;
203 args
.progress
= progress
;
204 args
.use_thin_pack
= use_thin_pack
;
205 args
.atomic
= atomic
;
206 args
.stateless_rpc
= stateless_rpc
;
207 args
.push_options
= push_options
.nr
? &push_options
: NULL
;
210 struct argv_array all_refspecs
= ARGV_ARRAY_INIT
;
212 for (i
= 0; i
< nr_refspecs
; i
++)
213 argv_array_push(&all_refspecs
, refspecs
[i
]);
215 if (args
.stateless_rpc
) {
217 while ((buf
= packet_read_line(0, NULL
)))
218 argv_array_push(&all_refspecs
, buf
);
220 struct strbuf line
= STRBUF_INIT
;
221 while (strbuf_getline(&line
, stdin
) != EOF
)
222 argv_array_push(&all_refspecs
, line
.buf
);
223 strbuf_release(&line
);
226 refspecs
= all_refspecs
.argv
;
227 nr_refspecs
= all_refspecs
.argc
;
231 * --all and --mirror are incompatible; neither makes sense
234 if ((nr_refspecs
> 0 && (send_all
|| args
.send_mirror
)) ||
235 (send_all
&& args
.send_mirror
))
236 usage_with_options(send_pack_usage
, options
);
239 remote
= remote_get(remote_name
);
240 if (!remote_has_url(remote
, dest
)) {
241 die("Destination %s is not a uri for %s",
247 progress
= !args
.quiet
&& isatty(2);
248 args
.progress
= progress
;
250 if (args
.stateless_rpc
) {
255 conn
= git_connect(fd
, dest
, receivepack
,
256 args
.verbose
? CONNECT_VERBOSE
: 0);
259 get_remote_heads(fd
[0], NULL
, 0, &remote_refs
, REF_NORMAL
,
260 &extra_have
, &shallow
);
262 transport_verify_remote_names(nr_refspecs
, refspecs
);
264 local_refs
= get_local_heads();
266 flags
= MATCH_REFS_NONE
;
269 flags
|= MATCH_REFS_ALL
;
270 if (args
.send_mirror
)
271 flags
|= MATCH_REFS_MIRROR
;
274 if (match_push_refs(local_refs
, &remote_refs
, nr_refspecs
, refspecs
, flags
))
277 if (!is_empty_cas(&cas
))
278 apply_push_cas(&cas
, remote
, remote_refs
);
280 set_ref_status_for_push(remote_refs
, args
.send_mirror
,
283 ret
= send_pack(&args
, fd
, conn
, remote_refs
, &extra_have
);
286 print_helper_status(remote_refs
);
291 ret
|= finish_connect(conn
);
294 transport_print_push_status(dest
, remote_refs
, args
.verbose
, 0, &reject_reasons
);
296 if (!args
.dry_run
&& remote
) {
298 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
299 transport_update_tracking_ref(remote
, ref
, args
.verbose
);
302 if (!ret
&& !transport_refs_pushed(remote_refs
))
303 fprintf(stderr
, "Everything up-to-date\n");