t4205 (log-pretty-formats): don't hardcode SHA-1 in expected outputs
[git/debian.git] / send-pack.c
blob7d172ef37f7b185f9586ce9015a08481d0bd259c
1 #include "builtin.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "remote.h"
8 #include "send-pack.h"
9 #include "quote.h"
10 #include "transport.h"
11 #include "version.h"
13 static int feed_object(const unsigned char *sha1, int fd, int negative)
15 char buf[42];
17 if (negative && !has_sha1_file(sha1))
18 return 1;
20 memcpy(buf + negative, sha1_to_hex(sha1), 40);
21 if (negative)
22 buf[0] = '^';
23 buf[40 + negative] = '\n';
24 return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
28 * Make a pack stream and spit it out into file descriptor fd
30 static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
33 * The child becomes pack-objects --revs; we feed
34 * the revision parameters to it via its stdin and
35 * let its stdout go back to the other end.
37 const char *argv[] = {
38 "pack-objects",
39 "--all-progress-implied",
40 "--revs",
41 "--stdout",
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 NULL,
48 struct child_process po;
49 int i;
51 i = 4;
52 if (args->use_thin_pack)
53 argv[i++] = "--thin";
54 if (args->use_ofs_delta)
55 argv[i++] = "--delta-base-offset";
56 if (args->quiet || !args->progress)
57 argv[i++] = "-q";
58 if (args->progress)
59 argv[i++] = "--progress";
60 memset(&po, 0, sizeof(po));
61 po.argv = argv;
62 po.in = -1;
63 po.out = args->stateless_rpc ? -1 : fd;
64 po.git_cmd = 1;
65 if (start_command(&po))
66 die_errno("git pack-objects failed");
69 * We feed the pack-objects we just spawned with revision
70 * parameters by writing to the pipe.
72 for (i = 0; i < extra->nr; i++)
73 if (!feed_object(extra->array[i], po.in, 1))
74 break;
76 while (refs) {
77 if (!is_null_sha1(refs->old_sha1) &&
78 !feed_object(refs->old_sha1, po.in, 1))
79 break;
80 if (!is_null_sha1(refs->new_sha1) &&
81 !feed_object(refs->new_sha1, po.in, 0))
82 break;
83 refs = refs->next;
86 close(po.in);
88 if (args->stateless_rpc) {
89 char *buf = xmalloc(LARGE_PACKET_MAX);
90 while (1) {
91 ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
92 if (n <= 0)
93 break;
94 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
96 free(buf);
97 close(po.out);
98 po.out = -1;
101 if (finish_command(&po))
102 return -1;
103 return 0;
106 static int receive_status(int in, struct ref *refs)
108 struct ref *hint;
109 int ret = 0;
110 char *line = packet_read_line(in, NULL);
111 if (prefixcmp(line, "unpack "))
112 return error("did not receive remote status");
113 if (strcmp(line, "unpack ok")) {
114 error("unpack failed: %s", line + 7);
115 ret = -1;
117 hint = NULL;
118 while (1) {
119 char *refname;
120 char *msg;
121 line = packet_read_line(in, NULL);
122 if (!line)
123 break;
124 if (prefixcmp(line, "ok ") && prefixcmp(line, "ng ")) {
125 error("invalid ref status from remote: %s", line);
126 ret = -1;
127 break;
130 refname = line + 3;
131 msg = strchr(refname, ' ');
132 if (msg)
133 *msg++ = '\0';
135 /* first try searching at our hint, falling back to all refs */
136 if (hint)
137 hint = find_ref_by_name(hint, refname);
138 if (!hint)
139 hint = find_ref_by_name(refs, refname);
140 if (!hint) {
141 warning("remote reported status on unknown ref: %s",
142 refname);
143 continue;
145 if (hint->status != REF_STATUS_EXPECTING_REPORT) {
146 warning("remote reported status on unexpected ref: %s",
147 refname);
148 continue;
151 if (line[0] == 'o' && line[1] == 'k')
152 hint->status = REF_STATUS_OK;
153 else {
154 hint->status = REF_STATUS_REMOTE_REJECT;
155 ret = -1;
157 if (msg)
158 hint->remote_status = xstrdup(msg);
159 /* start our next search from the next ref */
160 hint = hint->next;
162 return ret;
165 static int sideband_demux(int in, int out, void *data)
167 int *fd = data, ret;
168 #ifdef NO_PTHREADS
169 close(fd[1]);
170 #endif
171 ret = recv_sideband("send-pack", fd[0], out);
172 close(out);
173 return ret;
176 int send_pack(struct send_pack_args *args,
177 int fd[], struct child_process *conn,
178 struct ref *remote_refs,
179 struct extra_have_objects *extra_have)
181 int in = fd[0];
182 int out = fd[1];
183 struct strbuf req_buf = STRBUF_INIT;
184 struct ref *ref;
185 int new_refs;
186 int allow_deleting_refs = 0;
187 int status_report = 0;
188 int use_sideband = 0;
189 int quiet_supported = 0;
190 int agent_supported = 0;
191 unsigned cmds_sent = 0;
192 int ret;
193 struct async demux;
195 /* Does the other end support the reporting? */
196 if (server_supports("report-status"))
197 status_report = 1;
198 if (server_supports("delete-refs"))
199 allow_deleting_refs = 1;
200 if (server_supports("ofs-delta"))
201 args->use_ofs_delta = 1;
202 if (server_supports("side-band-64k"))
203 use_sideband = 1;
204 if (server_supports("quiet"))
205 quiet_supported = 1;
206 if (server_supports("agent"))
207 agent_supported = 1;
209 if (!remote_refs) {
210 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
211 "Perhaps you should specify a branch such as 'master'.\n");
212 return 0;
216 * Finally, tell the other end!
218 new_refs = 0;
219 for (ref = remote_refs; ref; ref = ref->next) {
220 if (!ref->peer_ref && !args->send_mirror)
221 continue;
223 /* Check for statuses set by set_ref_status_for_push() */
224 switch (ref->status) {
225 case REF_STATUS_REJECT_NONFASTFORWARD:
226 case REF_STATUS_REJECT_ALREADY_EXISTS:
227 case REF_STATUS_REJECT_FETCH_FIRST:
228 case REF_STATUS_REJECT_NEEDS_FORCE:
229 case REF_STATUS_UPTODATE:
230 continue;
231 default:
232 ; /* do nothing */
235 if (ref->deletion && !allow_deleting_refs) {
236 ref->status = REF_STATUS_REJECT_NODELETE;
237 continue;
240 if (!ref->deletion)
241 new_refs++;
243 if (args->dry_run) {
244 ref->status = REF_STATUS_OK;
245 } else {
246 char *old_hex = sha1_to_hex(ref->old_sha1);
247 char *new_hex = sha1_to_hex(ref->new_sha1);
248 int quiet = quiet_supported && (args->quiet || !args->progress);
250 if (!cmds_sent && (status_report || use_sideband ||
251 quiet || agent_supported)) {
252 packet_buf_write(&req_buf,
253 "%s %s %s%c%s%s%s%s%s",
254 old_hex, new_hex, ref->name, 0,
255 status_report ? " report-status" : "",
256 use_sideband ? " side-band-64k" : "",
257 quiet ? " quiet" : "",
258 agent_supported ? " agent=" : "",
259 agent_supported ? git_user_agent_sanitized() : ""
262 else
263 packet_buf_write(&req_buf, "%s %s %s",
264 old_hex, new_hex, ref->name);
265 ref->status = status_report ?
266 REF_STATUS_EXPECTING_REPORT :
267 REF_STATUS_OK;
268 cmds_sent++;
272 if (args->stateless_rpc) {
273 if (!args->dry_run && cmds_sent) {
274 packet_buf_flush(&req_buf);
275 send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
277 } else {
278 write_or_die(out, req_buf.buf, req_buf.len);
279 packet_flush(out);
281 strbuf_release(&req_buf);
283 if (use_sideband && cmds_sent) {
284 memset(&demux, 0, sizeof(demux));
285 demux.proc = sideband_demux;
286 demux.data = fd;
287 demux.out = -1;
288 if (start_async(&demux))
289 die("send-pack: unable to fork off sideband demultiplexer");
290 in = demux.out;
293 if (new_refs && cmds_sent) {
294 if (pack_objects(out, remote_refs, extra_have, args) < 0) {
295 for (ref = remote_refs; ref; ref = ref->next)
296 ref->status = REF_STATUS_NONE;
297 if (args->stateless_rpc)
298 close(out);
299 if (git_connection_is_socket(conn))
300 shutdown(fd[0], SHUT_WR);
301 if (use_sideband)
302 finish_async(&demux);
303 return -1;
306 if (args->stateless_rpc && cmds_sent)
307 packet_flush(out);
309 if (status_report && cmds_sent)
310 ret = receive_status(in, remote_refs);
311 else
312 ret = 0;
313 if (args->stateless_rpc)
314 packet_flush(out);
316 if (use_sideband && cmds_sent) {
317 if (finish_async(&demux)) {
318 error("error in sideband demultiplexer");
319 ret = -1;
321 close(demux.out);
324 if (ret < 0)
325 return ret;
327 if (args->porcelain)
328 return 0;
330 for (ref = remote_refs; ref; ref = ref->next) {
331 switch (ref->status) {
332 case REF_STATUS_NONE:
333 case REF_STATUS_UPTODATE:
334 case REF_STATUS_OK:
335 break;
336 default:
337 return -1;
340 return 0;