Elijah Newren | eef65c7 | 2023-02-24 00:09:35 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 3 | #include "commit.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 4 | #include "date.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 5 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 6 | #include "hex.h" |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 7 | #include "refs.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 8 | #include "object-store-ll.h" |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 9 | #include "pkt-line.h" |
| 10 | #include "sideband.h" |
| 11 | #include "run-command.h" |
| 12 | #include "remote.h" |
Junio C Hamano | 47a5918 | 2013-07-08 13:56:53 -0700 | [diff] [blame] | 13 | #include "connect.h" |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 14 | #include "send-pack.h" |
| 15 | #include "quote.h" |
| 16 | #include "transport.h" |
| 17 | #include "version.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 18 | #include "oid-array.h" |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 19 | #include "gpg-interface.h" |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 20 | #include "shallow.h" |
SZEDER Gábor | 49fd551 | 2023-03-19 17:27:11 +0100 | [diff] [blame] | 21 | #include "parse-options.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 22 | #include "trace2.h" |
| 23 | #include "write-or-die.h" |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 24 | |
| 25 | int option_parse_push_signed(const struct option *opt, |
| 26 | const char *arg, int unset) |
| 27 | { |
| 28 | if (unset) { |
| 29 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER; |
| 30 | return 0; |
| 31 | } |
| 32 | switch (git_parse_maybe_bool(arg)) { |
| 33 | case 1: |
| 34 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS; |
| 35 | return 0; |
| 36 | case 0: |
| 37 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER; |
| 38 | return 0; |
| 39 | } |
| 40 | if (!strcasecmp("if-asked", arg)) { |
| 41 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED; |
| 42 | return 0; |
| 43 | } |
| 44 | die("bad %s argument: %s", opt->long_name, arg); |
| 45 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 46 | |
brian m. carlson | 246d740 | 2018-03-12 02:27:32 +0000 | [diff] [blame] | 47 | static void feed_object(const struct object_id *oid, FILE *fh, int negative) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 48 | { |
Jonathan Tan | d8bc1a5 | 2019-10-08 11:37:39 -0700 | [diff] [blame] | 49 | if (negative && |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 50 | !repo_has_object_file_with_flags(the_repository, oid, |
| 51 | OBJECT_INFO_SKIP_FETCH_OBJECT | |
| 52 | OBJECT_INFO_QUICK)) |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 53 | return; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 54 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 55 | if (negative) |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 56 | putc('^', fh); |
brian m. carlson | 246d740 | 2018-03-12 02:27:32 +0000 | [diff] [blame] | 57 | fputs(oid_to_hex(oid), fh); |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 58 | putc('\n', fh); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Make a pack stream and spit it out into file descriptor fd |
| 63 | */ |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 64 | static int pack_objects(int fd, struct ref *refs, struct oid_array *advertised, |
| 65 | struct oid_array *negotiated, |
| 66 | struct send_pack_args *args) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 67 | { |
| 68 | /* |
| 69 | * The child becomes pack-objects --revs; we feed |
| 70 | * the revision parameters to it via its stdin and |
| 71 | * let its stdout go back to the other end. |
| 72 | */ |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 73 | struct child_process po = CHILD_PROCESS_INIT; |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 74 | FILE *po_in; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 75 | int i; |
Jeff King | d1a13d3 | 2017-03-07 08:39:48 -0500 | [diff] [blame] | 76 | int rc; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 77 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 78 | strvec_push(&po.args, "pack-objects"); |
| 79 | strvec_push(&po.args, "--all-progress-implied"); |
| 80 | strvec_push(&po.args, "--revs"); |
| 81 | strvec_push(&po.args, "--stdout"); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 82 | if (args->use_thin_pack) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 83 | strvec_push(&po.args, "--thin"); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 84 | if (args->use_ofs_delta) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 85 | strvec_push(&po.args, "--delta-base-offset"); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 86 | if (args->quiet || !args->progress) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 87 | strvec_push(&po.args, "-q"); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 88 | if (args->progress) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 89 | strvec_push(&po.args, "--progress"); |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 90 | if (is_repository_shallow(the_repository)) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 91 | strvec_push(&po.args, "--shallow"); |
Kyle Zhao | 82f67ee | 2022-06-17 19:06:19 +0000 | [diff] [blame] | 92 | if (args->disable_bitmaps) |
| 93 | strvec_push(&po.args, "--no-use-bitmap-index"); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 94 | po.in = -1; |
| 95 | po.out = args->stateless_rpc ? -1 : fd; |
| 96 | po.git_cmd = 1; |
Jeff King | 8b59935 | 2020-11-20 19:29:21 -0500 | [diff] [blame] | 97 | po.clean_on_exit = 1; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 98 | if (start_command(&po)) |
| 99 | die_errno("git pack-objects failed"); |
| 100 | |
| 101 | /* |
| 102 | * We feed the pack-objects we just spawned with revision |
| 103 | * parameters by writing to the pipe. |
| 104 | */ |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 105 | po_in = xfdopen(po.in, "w"); |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 106 | for (i = 0; i < advertised->nr; i++) |
| 107 | feed_object(&advertised->oid[i], po_in, 1); |
| 108 | for (i = 0; i < negotiated->nr; i++) |
| 109 | feed_object(&negotiated->oid[i], po_in, 1); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 110 | |
| 111 | while (refs) { |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 112 | if (!is_null_oid(&refs->old_oid)) |
brian m. carlson | 246d740 | 2018-03-12 02:27:32 +0000 | [diff] [blame] | 113 | feed_object(&refs->old_oid, po_in, 1); |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 114 | if (!is_null_oid(&refs->new_oid)) |
brian m. carlson | 246d740 | 2018-03-12 02:27:32 +0000 | [diff] [blame] | 115 | feed_object(&refs->new_oid, po_in, 0); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 116 | refs = refs->next; |
| 117 | } |
| 118 | |
Jeff King | f0bca72 | 2016-06-08 15:42:16 -0400 | [diff] [blame] | 119 | fflush(po_in); |
| 120 | if (ferror(po_in)) |
| 121 | die_errno("error writing to pack-objects"); |
| 122 | fclose(po_in); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 123 | |
| 124 | if (args->stateless_rpc) { |
| 125 | char *buf = xmalloc(LARGE_PACKET_MAX); |
| 126 | while (1) { |
| 127 | ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX); |
| 128 | if (n <= 0) |
| 129 | break; |
| 130 | send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX); |
| 131 | } |
| 132 | free(buf); |
| 133 | close(po.out); |
| 134 | po.out = -1; |
| 135 | } |
| 136 | |
Jeff King | d1a13d3 | 2017-03-07 08:39:48 -0500 | [diff] [blame] | 137 | rc = finish_command(&po); |
| 138 | if (rc) { |
| 139 | /* |
| 140 | * For a normal non-zero exit, we assume pack-objects wrote |
| 141 | * something useful to stderr. For death by signal, though, |
| 142 | * we should mention it to the user. The exception is SIGPIPE |
Ville Skyttä | 6412757 | 2017-06-25 13:20:41 +0300 | [diff] [blame] | 143 | * (141), because that's a normal occurrence if the remote end |
Jeff King | d1a13d3 | 2017-03-07 08:39:48 -0500 | [diff] [blame] | 144 | * hangs up (and we'll report that by trying to read the unpack |
| 145 | * status). |
| 146 | */ |
| 147 | if (rc > 128 && rc != 141) |
| 148 | error("pack-objects died of signal %d", rc - 128); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 149 | return -1; |
Jeff King | d1a13d3 | 2017-03-07 08:39:48 -0500 | [diff] [blame] | 150 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 154 | static int receive_unpack_status(struct packet_reader *reader) |
Jeff King | 7c39df2 | 2017-03-07 08:35:57 -0500 | [diff] [blame] | 155 | { |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 156 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
Jon Simons | bb1356d | 2018-02-08 13:47:50 -0500 | [diff] [blame] | 157 | return error(_("unexpected flush packet while reading remote unpack status")); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 158 | if (!skip_prefix(reader->line, "unpack ", &reader->line)) |
| 159 | return error(_("unable to parse remote unpack status: %s"), reader->line); |
| 160 | if (strcmp(reader->line, "ok")) |
| 161 | return error(_("remote unpack failed: %s"), reader->line); |
Jeff King | 7c39df2 | 2017-03-07 08:35:57 -0500 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 165 | static int receive_status(struct packet_reader *reader, struct ref *refs) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 166 | { |
| 167 | struct ref *hint; |
Jeff King | 7c39df2 | 2017-03-07 08:35:57 -0500 | [diff] [blame] | 168 | int ret; |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 169 | struct ref_push_report *report = NULL; |
| 170 | int new_report = 0; |
| 171 | int once = 0; |
Jeff King | 7c39df2 | 2017-03-07 08:35:57 -0500 | [diff] [blame] | 172 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 173 | hint = NULL; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 174 | ret = receive_unpack_status(reader); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 175 | while (1) { |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 176 | struct object_id old_oid, new_oid; |
| 177 | const char *head; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 178 | const char *refname; |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 179 | char *p; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 180 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 181 | break; |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 182 | head = reader->line; |
| 183 | p = strchr(head, ' '); |
| 184 | if (!p) { |
| 185 | error("invalid status line from remote: %s", reader->line); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 186 | ret = -1; |
| 187 | break; |
| 188 | } |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 189 | *p++ = '\0'; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 190 | |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 191 | if (!strcmp(head, "option")) { |
| 192 | const char *key, *val; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 193 | |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 194 | if (!hint || !(report || new_report)) { |
| 195 | if (!once++) |
| 196 | error("'option' without a matching 'ok/ng' directive"); |
| 197 | ret = -1; |
| 198 | continue; |
| 199 | } |
| 200 | if (new_report) { |
| 201 | if (!hint->report) { |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 202 | CALLOC_ARRAY(hint->report, 1); |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 203 | report = hint->report; |
| 204 | } else { |
| 205 | report = hint->report; |
| 206 | while (report->next) |
| 207 | report = report->next; |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 208 | CALLOC_ARRAY(report->next, 1); |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 209 | report = report->next; |
| 210 | } |
| 211 | new_report = 0; |
| 212 | } |
| 213 | key = p; |
| 214 | p = strchr(key, ' '); |
| 215 | if (p) |
| 216 | *p++ = '\0'; |
| 217 | val = p; |
| 218 | if (!strcmp(key, "refname")) |
| 219 | report->ref_name = xstrdup_or_null(val); |
| 220 | else if (!strcmp(key, "old-oid") && val && |
| 221 | !parse_oid_hex(val, &old_oid, &val)) |
| 222 | report->old_oid = oiddup(&old_oid); |
| 223 | else if (!strcmp(key, "new-oid") && val && |
| 224 | !parse_oid_hex(val, &new_oid, &val)) |
| 225 | report->new_oid = oiddup(&new_oid); |
| 226 | else if (!strcmp(key, "forced-update")) |
| 227 | report->forced_update = 1; |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | report = NULL; |
| 232 | new_report = 0; |
| 233 | if (strcmp(head, "ok") && strcmp(head, "ng")) { |
| 234 | error("invalid ref status from remote: %s", head); |
| 235 | ret = -1; |
| 236 | break; |
| 237 | } |
| 238 | refname = p; |
| 239 | p = strchr(refname, ' '); |
| 240 | if (p) |
| 241 | *p++ = '\0'; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 242 | /* first try searching at our hint, falling back to all refs */ |
| 243 | if (hint) |
| 244 | hint = find_ref_by_name(hint, refname); |
| 245 | if (!hint) |
| 246 | hint = find_ref_by_name(refs, refname); |
| 247 | if (!hint) { |
| 248 | warning("remote reported status on unknown ref: %s", |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 249 | refname); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 250 | continue; |
| 251 | } |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 252 | if (hint->status != REF_STATUS_EXPECTING_REPORT && |
| 253 | hint->status != REF_STATUS_OK && |
| 254 | hint->status != REF_STATUS_REMOTE_REJECT) { |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 255 | warning("remote reported status on unexpected ref: %s", |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 256 | refname); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 257 | continue; |
| 258 | } |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 259 | if (!strcmp(head, "ng")) { |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 260 | hint->status = REF_STATUS_REMOTE_REJECT; |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 261 | if (p) |
| 262 | hint->remote_status = xstrdup(p); |
| 263 | else |
| 264 | hint->remote_status = "failed"; |
| 265 | } else { |
| 266 | hint->status = REF_STATUS_OK; |
| 267 | hint->remote_status = xstrdup_or_null(p); |
| 268 | new_report = 1; |
| 269 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 270 | } |
| 271 | return ret; |
| 272 | } |
| 273 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 274 | static int sideband_demux(int in UNUSED, int out, void *data) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 275 | { |
| 276 | int *fd = data, ret; |
Nguyễn Thái Ngọc Duy | c0e40a2 | 2018-11-03 09:48:39 +0100 | [diff] [blame] | 277 | if (async_with_fork()) |
| 278 | close(fd[1]); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 279 | ret = recv_sideband("send-pack", fd[0], out); |
| 280 | close(out); |
| 281 | return ret; |
| 282 | } |
| 283 | |
Nguyễn Thái Ngọc Duy | f2c681c | 2013-12-05 20:02:52 +0700 | [diff] [blame] | 284 | static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb) |
| 285 | { |
| 286 | struct strbuf *sb = cb; |
| 287 | if (graft->nr_parent == -1) |
brian m. carlson | 7683e2e | 2015-03-13 23:39:34 +0000 | [diff] [blame] | 288 | packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid)); |
Nguyễn Thái Ngọc Duy | f2c681c | 2013-12-05 20:02:52 +0700 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Ramsay Jones | 16a2743 | 2014-01-05 23:55:01 +0000 | [diff] [blame] | 292 | static void advertise_shallow_grafts_buf(struct strbuf *sb) |
Nguyễn Thái Ngọc Duy | f2c681c | 2013-12-05 20:02:52 +0700 | [diff] [blame] | 293 | { |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 294 | if (!is_repository_shallow(the_repository)) |
Nguyễn Thái Ngọc Duy | f2c681c | 2013-12-05 20:02:52 +0700 | [diff] [blame] | 295 | return; |
| 296 | for_each_commit_graft(advertise_shallow_grafts_cb, sb); |
| 297 | } |
| 298 | |
Stefan Beller | 7582e93 | 2015-01-07 19:23:21 -0800 | [diff] [blame] | 299 | #define CHECK_REF_NO_PUSH -1 |
| 300 | #define CHECK_REF_STATUS_REJECTED -2 |
| 301 | #define CHECK_REF_UPTODATE -3 |
| 302 | static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args) |
Junio C Hamano | e40671a | 2014-08-12 15:40:00 -0700 | [diff] [blame] | 303 | { |
| 304 | if (!ref->peer_ref && !args->send_mirror) |
Stefan Beller | 7582e93 | 2015-01-07 19:23:21 -0800 | [diff] [blame] | 305 | return CHECK_REF_NO_PUSH; |
Junio C Hamano | e40671a | 2014-08-12 15:40:00 -0700 | [diff] [blame] | 306 | |
| 307 | /* Check for statuses set by set_ref_status_for_push() */ |
| 308 | switch (ref->status) { |
| 309 | case REF_STATUS_REJECT_NONFASTFORWARD: |
| 310 | case REF_STATUS_REJECT_ALREADY_EXISTS: |
| 311 | case REF_STATUS_REJECT_FETCH_FIRST: |
| 312 | case REF_STATUS_REJECT_NEEDS_FORCE: |
| 313 | case REF_STATUS_REJECT_STALE: |
Srinidhi Kaushik | 99a1f9a | 2020-10-03 17:40:44 +0530 | [diff] [blame] | 314 | case REF_STATUS_REJECT_REMOTE_UPDATED: |
Junio C Hamano | e40671a | 2014-08-12 15:40:00 -0700 | [diff] [blame] | 315 | case REF_STATUS_REJECT_NODELETE: |
Stefan Beller | 7582e93 | 2015-01-07 19:23:21 -0800 | [diff] [blame] | 316 | return CHECK_REF_STATUS_REJECTED; |
Junio C Hamano | e40671a | 2014-08-12 15:40:00 -0700 | [diff] [blame] | 317 | case REF_STATUS_UPTODATE: |
Stefan Beller | 7582e93 | 2015-01-07 19:23:21 -0800 | [diff] [blame] | 318 | return CHECK_REF_UPTODATE; |
Han Xin | a4f324a | 2020-09-19 22:47:50 +0800 | [diff] [blame] | 319 | |
Junio C Hamano | e40671a | 2014-08-12 15:40:00 -0700 | [diff] [blame] | 320 | default: |
Han Xin | a4f324a | 2020-09-19 22:47:50 +0800 | [diff] [blame] | 321 | case REF_STATUS_EXPECTING_REPORT: |
| 322 | /* already passed checks on the local side */ |
| 323 | case REF_STATUS_OK: |
| 324 | /* of course this is OK */ |
Stefan Beller | 7582e93 | 2015-01-07 19:23:21 -0800 | [diff] [blame] | 325 | return 0; |
Junio C Hamano | e40671a | 2014-08-12 15:40:00 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 329 | /* |
| 330 | * the beginning of the next line, or the end of buffer. |
| 331 | * |
| 332 | * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and |
| 333 | * convert many similar uses found by "git grep -A4 memchr". |
| 334 | */ |
| 335 | static const char *next_line(const char *line, size_t len) |
| 336 | { |
| 337 | const char *nl = memchr(line, '\n', len); |
| 338 | if (!nl) |
| 339 | return line + len; /* incomplete line */ |
| 340 | return nl + 1; |
| 341 | } |
| 342 | |
Junio C Hamano | 20a7558 | 2014-08-18 13:46:58 -0700 | [diff] [blame] | 343 | static int generate_push_cert(struct strbuf *req_buf, |
| 344 | const struct ref *remote_refs, |
| 345 | struct send_pack_args *args, |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 346 | const char *cap_string, |
| 347 | const char *push_cert_nonce) |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 348 | { |
| 349 | const struct ref *ref; |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 350 | struct string_list_item *item; |
Fabian Stelzer | 4838f62 | 2021-09-10 20:07:38 +0000 | [diff] [blame] | 351 | char *signing_key_id = xstrdup(get_signing_key_id()); |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 352 | const char *cp, *np; |
| 353 | struct strbuf cert = STRBUF_INIT; |
| 354 | int update_seen = 0; |
| 355 | |
René Scharfe | 02962d3 | 2016-07-30 19:36:23 +0200 | [diff] [blame] | 356 | strbuf_addstr(&cert, "certificate version 0.1\n"); |
Fabian Stelzer | 4838f62 | 2021-09-10 20:07:38 +0000 | [diff] [blame] | 357 | strbuf_addf(&cert, "pusher %s ", signing_key_id); |
Junio C Hamano | fb06b52 | 2014-10-08 13:05:15 -0700 | [diff] [blame] | 358 | datestamp(&cert); |
| 359 | strbuf_addch(&cert, '\n'); |
Junio C Hamano | 9be8916 | 2014-08-22 18:15:24 -0700 | [diff] [blame] | 360 | if (args->url && *args->url) { |
| 361 | char *anon_url = transport_anonymize_url(args->url); |
| 362 | strbuf_addf(&cert, "pushee %s\n", anon_url); |
| 363 | free(anon_url); |
| 364 | } |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 365 | if (push_cert_nonce[0]) |
| 366 | strbuf_addf(&cert, "nonce %s\n", push_cert_nonce); |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 367 | if (args->push_options) |
| 368 | for_each_string_list_item(item, args->push_options) |
| 369 | strbuf_addf(&cert, "push-option %s\n", item->string); |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 370 | strbuf_addstr(&cert, "\n"); |
| 371 | |
| 372 | for (ref = remote_refs; ref; ref = ref->next) { |
Stefan Beller | 7582e93 | 2015-01-07 19:23:21 -0800 | [diff] [blame] | 373 | if (check_to_send_update(ref, args) < 0) |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 374 | continue; |
| 375 | update_seen = 1; |
| 376 | strbuf_addf(&cert, "%s %s %s\n", |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 377 | oid_to_hex(&ref->old_oid), |
| 378 | oid_to_hex(&ref->new_oid), |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 379 | ref->name); |
| 380 | } |
| 381 | if (!update_seen) |
| 382 | goto free_return; |
| 383 | |
Fabian Stelzer | 4838f62 | 2021-09-10 20:07:38 +0000 | [diff] [blame] | 384 | if (sign_buffer(&cert, &cert, get_signing_key())) |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 385 | die(_("failed to sign the push certificate")); |
| 386 | |
Junio C Hamano | 20a7558 | 2014-08-18 13:46:58 -0700 | [diff] [blame] | 387 | packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string); |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 388 | for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) { |
| 389 | np = next_line(cp, cert.buf + cert.len - cp); |
| 390 | packet_buf_write(req_buf, |
| 391 | "%.*s", (int)(np - cp), cp); |
| 392 | } |
| 393 | packet_buf_write(req_buf, "push-cert-end\n"); |
| 394 | |
| 395 | free_return: |
Fabian Stelzer | 4838f62 | 2021-09-10 20:07:38 +0000 | [diff] [blame] | 396 | free(signing_key_id); |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 397 | strbuf_release(&cert); |
Junio C Hamano | 20a7558 | 2014-08-18 13:46:58 -0700 | [diff] [blame] | 398 | return update_seen; |
Junio C Hamano | a85b377 | 2014-09-12 11:17:07 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Junio C Hamano | afcb6ee | 2015-04-01 18:00:36 -0700 | [diff] [blame] | 401 | #define NONCE_LEN_LIMIT 256 |
| 402 | |
| 403 | static void reject_invalid_nonce(const char *nonce, int len) |
| 404 | { |
| 405 | int i = 0; |
| 406 | |
| 407 | if (NONCE_LEN_LIMIT <= len) |
| 408 | die("the receiving end asked to sign an invalid nonce <%.*s>", |
| 409 | len, nonce); |
| 410 | |
| 411 | for (i = 0; i < len; i++) { |
| 412 | int ch = nonce[i] & 0xFF; |
| 413 | if (isalnum(ch) || |
| 414 | ch == '-' || ch == '.' || |
| 415 | ch == '/' || ch == '+' || |
| 416 | ch == '=' || ch == '_') |
| 417 | continue; |
| 418 | die("the receiving end asked to sign an invalid nonce <%.*s>", |
| 419 | len, nonce); |
| 420 | } |
| 421 | } |
| 422 | |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 423 | static void get_commons_through_negotiation(const char *url, |
| 424 | const struct ref *remote_refs, |
| 425 | struct oid_array *commons) |
| 426 | { |
| 427 | struct child_process child = CHILD_PROCESS_INIT; |
| 428 | const struct ref *ref; |
| 429 | int len = the_hash_algo->hexsz + 1; /* hash + NL */ |
| 430 | |
| 431 | child.git_cmd = 1; |
| 432 | child.no_stdin = 1; |
| 433 | child.out = -1; |
| 434 | strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL); |
Jonathan Tan | 54a03bc | 2021-07-15 10:44:31 -0700 | [diff] [blame] | 435 | for (ref = remote_refs; ref; ref = ref->next) { |
| 436 | if (!is_null_oid(&ref->new_oid)) |
| 437 | strvec_pushf(&child.args, "--negotiation-tip=%s", oid_to_hex(&ref->new_oid)); |
| 438 | } |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 439 | strvec_push(&child.args, url); |
| 440 | |
| 441 | if (start_command(&child)) |
| 442 | die(_("send-pack: unable to fork off fetch subprocess")); |
| 443 | |
| 444 | do { |
| 445 | char hex_hash[GIT_MAX_HEXSZ + 1]; |
| 446 | int read_len = read_in_full(child.out, hex_hash, len); |
| 447 | struct object_id oid; |
| 448 | const char *end; |
| 449 | |
| 450 | if (!read_len) |
| 451 | break; |
| 452 | if (read_len != len) |
| 453 | die("invalid length read %d", read_len); |
| 454 | if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n') |
| 455 | die("invalid hash"); |
| 456 | oid_array_append(commons, &oid); |
| 457 | } while (1); |
| 458 | |
| 459 | if (finish_command(&child)) { |
| 460 | /* |
| 461 | * The information that push negotiation provides is useful but |
| 462 | * not mandatory. |
| 463 | */ |
| 464 | warning(_("push negotiation failed; proceeding anyway with push")); |
| 465 | } |
| 466 | } |
| 467 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 468 | int send_pack(struct send_pack_args *args, |
| 469 | int fd[], struct child_process *conn, |
| 470 | struct ref *remote_refs, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 471 | struct oid_array *extra_have) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 472 | { |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 473 | struct oid_array commons = OID_ARRAY_INIT; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 474 | int in = fd[0]; |
| 475 | int out = fd[1]; |
| 476 | struct strbuf req_buf = STRBUF_INIT; |
Junio C Hamano | 887f353 | 2014-08-15 11:37:01 -0700 | [diff] [blame] | 477 | struct strbuf cap_buf = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 478 | struct ref *ref; |
Junio C Hamano | ab2b0c9 | 2014-08-15 12:23:51 -0700 | [diff] [blame] | 479 | int need_pack_data = 0; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 480 | int allow_deleting_refs = 0; |
| 481 | int status_report = 0; |
| 482 | int use_sideband = 0; |
| 483 | int quiet_supported = 0; |
| 484 | int agent_supported = 0; |
Josh Steadmon | 8c48700 | 2020-11-11 15:29:33 -0800 | [diff] [blame] | 485 | int advertise_sid = 0; |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 486 | int push_negotiate = 0; |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 487 | int use_atomic = 0; |
| 488 | int atomic_supported = 0; |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 489 | int use_push_options = 0; |
| 490 | int push_options_supported = 0; |
brian m. carlson | 82db03a | 2020-05-25 19:58:57 +0000 | [diff] [blame] | 491 | int object_format_supported = 0; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 492 | unsigned cmds_sent = 0; |
| 493 | int ret; |
| 494 | struct async demux; |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 495 | const char *push_cert_nonce = NULL; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 496 | struct packet_reader reader; |
Kyle Zhao | 82f67ee | 2022-06-17 19:06:19 +0000 | [diff] [blame] | 497 | int use_bitmaps; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 498 | |
Ævar Arnfjörð Bjarmason | 1e5b5ea | 2021-06-30 18:38:10 +0200 | [diff] [blame] | 499 | if (!remote_refs) { |
| 500 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" |
| 501 | "Perhaps you should specify a branch.\n"); |
| 502 | return 0; |
| 503 | } |
| 504 | |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 505 | git_config_get_bool("push.negotiate", &push_negotiate); |
| 506 | if (push_negotiate) |
| 507 | get_commons_through_negotiation(args->url, remote_refs, &commons); |
| 508 | |
Kyle Zhao | 82f67ee | 2022-06-17 19:06:19 +0000 | [diff] [blame] | 509 | if (!git_config_get_bool("push.usebitmaps", &use_bitmaps)) |
| 510 | args->disable_bitmaps = !use_bitmaps; |
| 511 | |
Josh Steadmon | 8c48700 | 2020-11-11 15:29:33 -0800 | [diff] [blame] | 512 | git_config_get_bool("transfer.advertisesid", &advertise_sid); |
| 513 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 514 | /* Does the other end support the reporting? */ |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 515 | if (server_supports("report-status-v2")) |
| 516 | status_report = 2; |
| 517 | else if (server_supports("report-status")) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 518 | status_report = 1; |
| 519 | if (server_supports("delete-refs")) |
| 520 | allow_deleting_refs = 1; |
| 521 | if (server_supports("ofs-delta")) |
| 522 | args->use_ofs_delta = 1; |
| 523 | if (server_supports("side-band-64k")) |
| 524 | use_sideband = 1; |
| 525 | if (server_supports("quiet")) |
| 526 | quiet_supported = 1; |
| 527 | if (server_supports("agent")) |
| 528 | agent_supported = 1; |
Josh Steadmon | 8c48700 | 2020-11-11 15:29:33 -0800 | [diff] [blame] | 529 | if (!server_supports("session-id")) |
| 530 | advertise_sid = 0; |
Carlos Martín Nieto | 1ba98a7 | 2013-11-23 17:07:55 +0100 | [diff] [blame] | 531 | if (server_supports("no-thin")) |
| 532 | args->use_thin_pack = 0; |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 533 | if (server_supports("atomic")) |
| 534 | atomic_supported = 1; |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 535 | if (server_supports("push-options")) |
| 536 | push_options_supported = 1; |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 537 | |
brian m. carlson | 82db03a | 2020-05-25 19:58:57 +0000 | [diff] [blame] | 538 | if (!server_supports_hash(the_hash_algo->name, &object_format_supported)) |
| 539 | die(_("the receiving end does not support this repository's hash algorithm")); |
| 540 | |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 541 | if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) { |
Jeff King | 7ce4c8f | 2023-04-14 17:25:20 -0400 | [diff] [blame] | 542 | size_t len; |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 543 | push_cert_nonce = server_feature_value("push-cert", &len); |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 544 | if (push_cert_nonce) { |
| 545 | reject_invalid_nonce(push_cert_nonce, len); |
| 546 | push_cert_nonce = xmemdupz(push_cert_nonce, len); |
| 547 | } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) { |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 548 | die(_("the receiving end does not support --signed push")); |
Dave Borowitz | 3026109 | 2015-08-19 11:26:46 -0400 | [diff] [blame] | 549 | } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) { |
| 550 | warning(_("not sending a push certificate since the" |
| 551 | " receiving end does not support --signed" |
| 552 | " push")); |
| 553 | } |
Junio C Hamano | b89363e | 2014-08-21 16:45:30 -0700 | [diff] [blame] | 554 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 555 | |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 556 | if (args->atomic && !atomic_supported) |
Ralf Thielow | c8b8f22 | 2015-04-02 19:28:48 +0200 | [diff] [blame] | 557 | die(_("the receiving end does not support --atomic push")); |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 558 | |
| 559 | use_atomic = atomic_supported && args->atomic; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 560 | |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 561 | if (args->push_options && !push_options_supported) |
| 562 | die(_("the receiving end does not support push options")); |
| 563 | |
| 564 | use_push_options = push_options_supported && args->push_options; |
| 565 | |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 566 | if (status_report == 1) |
Junio C Hamano | 887f353 | 2014-08-15 11:37:01 -0700 | [diff] [blame] | 567 | strbuf_addstr(&cap_buf, " report-status"); |
Jiang Xin | 63518a5 | 2020-08-27 11:45:46 -0400 | [diff] [blame] | 568 | else if (status_report == 2) |
| 569 | strbuf_addstr(&cap_buf, " report-status-v2"); |
Junio C Hamano | 887f353 | 2014-08-15 11:37:01 -0700 | [diff] [blame] | 570 | if (use_sideband) |
| 571 | strbuf_addstr(&cap_buf, " side-band-64k"); |
| 572 | if (quiet_supported && (args->quiet || !args->progress)) |
| 573 | strbuf_addstr(&cap_buf, " quiet"); |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 574 | if (use_atomic) |
| 575 | strbuf_addstr(&cap_buf, " atomic"); |
Stefan Beller | f6a4e61 | 2016-07-14 14:49:47 -0700 | [diff] [blame] | 576 | if (use_push_options) |
| 577 | strbuf_addstr(&cap_buf, " push-options"); |
brian m. carlson | 82db03a | 2020-05-25 19:58:57 +0000 | [diff] [blame] | 578 | if (object_format_supported) |
| 579 | strbuf_addf(&cap_buf, " object-format=%s", the_hash_algo->name); |
Junio C Hamano | 887f353 | 2014-08-15 11:37:01 -0700 | [diff] [blame] | 580 | if (agent_supported) |
| 581 | strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized()); |
Josh Steadmon | 8c48700 | 2020-11-11 15:29:33 -0800 | [diff] [blame] | 582 | if (advertise_sid) |
| 583 | strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id()); |
Junio C Hamano | 887f353 | 2014-08-15 11:37:01 -0700 | [diff] [blame] | 584 | |
Junio C Hamano | 621b059 | 2014-08-12 15:04:17 -0700 | [diff] [blame] | 585 | /* |
| 586 | * NEEDSWORK: why does delete-refs have to be so specific to |
| 587 | * send-pack machinery that set_ref_status_for_push() cannot |
| 588 | * set this bit for us??? |
| 589 | */ |
| 590 | for (ref = remote_refs; ref; ref = ref->next) |
| 591 | if (ref->deletion && !allow_deleting_refs) |
| 592 | ref->status = REF_STATUS_REJECT_NODELETE; |
| 593 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 594 | /* |
Junio C Hamano | b783aa7 | 2014-08-15 12:29:42 -0700 | [diff] [blame] | 595 | * Clear the status for each ref and see if we need to send |
| 596 | * the pack data. |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 597 | */ |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 598 | for (ref = remote_refs; ref; ref = ref->next) { |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 599 | switch (check_to_send_update(ref, args)) { |
| 600 | case 0: /* no error */ |
| 601 | break; |
| 602 | case CHECK_REF_STATUS_REJECTED: |
| 603 | /* |
| 604 | * When we know the server would reject a ref update if |
| 605 | * we were to send it and we're trying to send the refs |
| 606 | * atomically, abort the whole operation. |
| 607 | */ |
Rene Scharfe | 872d651 | 2017-08-30 20:00:28 +0200 | [diff] [blame] | 608 | if (use_atomic) { |
| 609 | strbuf_release(&req_buf); |
| 610 | strbuf_release(&cap_buf); |
Jiang Xin | dfe1b7f | 2020-04-17 05:45:36 -0400 | [diff] [blame] | 611 | reject_atomic_push(remote_refs, args->send_mirror); |
| 612 | error("atomic push failed for ref %s. status: %d\n", |
| 613 | ref->name, ref->status); |
Jiang Xin | 7dcbeaa | 2020-04-17 05:45:32 -0400 | [diff] [blame] | 614 | return args->porcelain ? 0 : -1; |
Rene Scharfe | 872d651 | 2017-08-30 20:00:28 +0200 | [diff] [blame] | 615 | } |
Jeff King | 1cf01a3 | 2017-09-21 02:25:41 -0400 | [diff] [blame] | 616 | /* else fallthrough */ |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 617 | default: |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 618 | continue; |
Ronnie Sahlberg | 4ff17f1 | 2015-01-07 19:23:22 -0800 | [diff] [blame] | 619 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 620 | if (!ref->deletion) |
Junio C Hamano | ab2b0c9 | 2014-08-15 12:23:51 -0700 | [diff] [blame] | 621 | need_pack_data = 1; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 622 | |
Junio C Hamano | b783aa7 | 2014-08-15 12:29:42 -0700 | [diff] [blame] | 623 | if (args->dry_run || !status_report) |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 624 | ref->status = REF_STATUS_OK; |
Junio C Hamano | b783aa7 | 2014-08-15 12:29:42 -0700 | [diff] [blame] | 625 | else |
| 626 | ref->status = REF_STATUS_EXPECTING_REPORT; |
| 627 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 628 | |
Han Xin | a4f324a | 2020-09-19 22:47:50 +0800 | [diff] [blame] | 629 | if (!args->dry_run) |
| 630 | advertise_shallow_grafts_buf(&req_buf); |
| 631 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 632 | /* |
| 633 | * Finally, tell the other end! |
| 634 | */ |
Han Xin | a4f324a | 2020-09-19 22:47:50 +0800 | [diff] [blame] | 635 | if (!args->dry_run && push_cert_nonce) |
| 636 | cmds_sent = generate_push_cert(&req_buf, remote_refs, args, |
| 637 | cap_buf.buf, push_cert_nonce); |
| 638 | else if (!args->dry_run) |
| 639 | for (ref = remote_refs; ref; ref = ref->next) { |
| 640 | char *old_hex, *new_hex; |
Junio C Hamano | b783aa7 | 2014-08-15 12:29:42 -0700 | [diff] [blame] | 641 | |
Han Xin | a4f324a | 2020-09-19 22:47:50 +0800 | [diff] [blame] | 642 | if (check_to_send_update(ref, args) < 0) |
| 643 | continue; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 644 | |
Han Xin | a4f324a | 2020-09-19 22:47:50 +0800 | [diff] [blame] | 645 | old_hex = oid_to_hex(&ref->old_oid); |
| 646 | new_hex = oid_to_hex(&ref->new_oid); |
| 647 | if (!cmds_sent) { |
| 648 | packet_buf_write(&req_buf, |
| 649 | "%s %s %s%c%s", |
| 650 | old_hex, new_hex, ref->name, 0, |
| 651 | cap_buf.buf); |
| 652 | cmds_sent = 1; |
| 653 | } else { |
| 654 | packet_buf_write(&req_buf, "%s %s %s", |
| 655 | old_hex, new_hex, ref->name); |
| 656 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 657 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 658 | |
Brandon Williams | eb7b974 | 2017-03-22 15:21:59 -0700 | [diff] [blame] | 659 | if (use_push_options) { |
| 660 | struct string_list_item *item; |
| 661 | |
| 662 | packet_buf_flush(&req_buf); |
| 663 | for_each_string_list_item(item, args->push_options) |
| 664 | packet_buf_write(&req_buf, "%s", item->string); |
| 665 | } |
| 666 | |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 667 | if (args->stateless_rpc) { |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 668 | if (!args->dry_run && (cmds_sent || is_repository_shallow(the_repository))) { |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 669 | packet_buf_flush(&req_buf); |
| 670 | send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX); |
| 671 | } |
| 672 | } else { |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 673 | write_or_die(out, req_buf.buf, req_buf.len); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 674 | packet_flush(out); |
| 675 | } |
| 676 | strbuf_release(&req_buf); |
Junio C Hamano | 887f353 | 2014-08-15 11:37:01 -0700 | [diff] [blame] | 677 | strbuf_release(&cap_buf); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 678 | |
| 679 | if (use_sideband && cmds_sent) { |
| 680 | memset(&demux, 0, sizeof(demux)); |
| 681 | demux.proc = sideband_demux; |
| 682 | demux.data = fd; |
| 683 | demux.out = -1; |
Jeff King | 3e8b06d | 2016-04-19 18:50:17 -0400 | [diff] [blame] | 684 | demux.isolate_sigpipe = 1; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 685 | if (start_async(&demux)) |
| 686 | die("send-pack: unable to fork off sideband demultiplexer"); |
| 687 | in = demux.out; |
| 688 | } |
| 689 | |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 690 | packet_reader_init(&reader, in, NULL, 0, |
| 691 | PACKET_READ_CHOMP_NEWLINE | |
| 692 | PACKET_READ_DIE_ON_ERR_PACKET); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 693 | |
Junio C Hamano | ab2b0c9 | 2014-08-15 12:23:51 -0700 | [diff] [blame] | 694 | if (need_pack_data && cmds_sent) { |
Jonathan Tan | 477673d | 2021-05-04 14:16:02 -0700 | [diff] [blame] | 695 | if (pack_objects(out, remote_refs, extra_have, &commons, args) < 0) { |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 696 | if (args->stateless_rpc) |
| 697 | close(out); |
| 698 | if (git_connection_is_socket(conn)) |
| 699 | shutdown(fd[0], SHUT_WR); |
Jeff King | ba69f92 | 2017-03-07 08:38:51 -0500 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * Do not even bother with the return value; we know we |
Jeff King | ad7a403 | 2019-11-12 21:07:19 -0500 | [diff] [blame] | 703 | * are failing, and just want the error() side effects, |
| 704 | * as well as marking refs with their remote status (if |
| 705 | * we get one). |
Jeff King | ba69f92 | 2017-03-07 08:38:51 -0500 | [diff] [blame] | 706 | */ |
| 707 | if (status_report) |
Jeff King | ad7a403 | 2019-11-12 21:07:19 -0500 | [diff] [blame] | 708 | receive_status(&reader, remote_refs); |
Jeff King | ba69f92 | 2017-03-07 08:38:51 -0500 | [diff] [blame] | 709 | |
Jeff King | 739cf49 | 2016-04-19 18:45:17 -0400 | [diff] [blame] | 710 | if (use_sideband) { |
| 711 | close(demux.out); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 712 | finish_async(&demux); |
Jeff King | 739cf49 | 2016-04-19 18:45:17 -0400 | [diff] [blame] | 713 | } |
Jens Lindstrom | 37cb1dd | 2013-10-22 15:36:02 +0200 | [diff] [blame] | 714 | fd[1] = -1; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 715 | return -1; |
| 716 | } |
Jens Lindstrom | 37cb1dd | 2013-10-22 15:36:02 +0200 | [diff] [blame] | 717 | if (!args->stateless_rpc) |
| 718 | /* Closed by pack_objects() via start_command() */ |
| 719 | fd[1] = -1; |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 720 | } |
| 721 | if (args->stateless_rpc && cmds_sent) |
| 722 | packet_flush(out); |
| 723 | |
| 724 | if (status_report && cmds_sent) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 725 | ret = receive_status(&reader, remote_refs); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 726 | else |
| 727 | ret = 0; |
| 728 | if (args->stateless_rpc) |
| 729 | packet_flush(out); |
| 730 | |
| 731 | if (use_sideband && cmds_sent) { |
Jeff King | 739cf49 | 2016-04-19 18:45:17 -0400 | [diff] [blame] | 732 | close(demux.out); |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 733 | if (finish_async(&demux)) { |
| 734 | error("error in sideband demultiplexer"); |
| 735 | ret = -1; |
| 736 | } |
Nguyễn Thái Ngọc Duy | f5d942e | 2012-10-26 22:53:53 +0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | if (ret < 0) |
| 740 | return ret; |
| 741 | |
| 742 | if (args->porcelain) |
| 743 | return 0; |
| 744 | |
| 745 | for (ref = remote_refs; ref; ref = ref->next) { |
| 746 | switch (ref->status) { |
| 747 | case REF_STATUS_NONE: |
| 748 | case REF_STATUS_UPTODATE: |
| 749 | case REF_STATUS_OK: |
| 750 | break; |
| 751 | default: |
| 752 | return -1; |
| 753 | } |
| 754 | } |
| 755 | return 0; |
| 756 | } |