| 1 | #ifndef REMOTE_H |
| 2 | #define REMOTE_H |
| 3 | |
| 4 | #include "hash.h" |
| 5 | #include "hashmap.h" |
| 6 | #include "refspec.h" |
| 7 | #include "string-list.h" |
| 8 | #include "strvec.h" |
| 9 | |
| 10 | struct option; |
| 11 | struct transport_ls_refs_options; |
| 12 | struct repository; |
| 13 | |
| 14 | /** |
| 15 | * The API gives access to the configuration related to remotes. It handles |
| 16 | * all three configuration mechanisms historically and currently used by Git, |
| 17 | * and presents the information in a uniform fashion. Note that the code also |
| 18 | * handles plain URLs without any configuration, giving them just the default |
| 19 | * information. |
| 20 | */ |
| 21 | |
| 22 | enum { |
| 23 | REMOTE_UNCONFIGURED = 0, |
| 24 | REMOTE_CONFIG, |
| 25 | #ifndef WITH_BREAKING_CHANGES |
| 26 | REMOTE_REMOTES, |
| 27 | REMOTE_BRANCHES |
| 28 | #endif /* WITH_BREAKING_CHANGES */ |
| 29 | }; |
| 30 | |
| 31 | struct rewrite { |
| 32 | const char *base; |
| 33 | size_t baselen; |
| 34 | struct counted_string *instead_of; |
| 35 | int instead_of_nr; |
| 36 | int instead_of_alloc; |
| 37 | }; |
| 38 | |
| 39 | struct rewrites { |
| 40 | struct rewrite **rewrite; |
| 41 | int rewrite_alloc; |
| 42 | int rewrite_nr; |
| 43 | }; |
| 44 | |
| 45 | struct remote_state { |
| 46 | struct remote **remotes; |
| 47 | int remotes_alloc; |
| 48 | int remotes_nr; |
| 49 | struct hashmap remotes_hash; |
| 50 | |
| 51 | struct hashmap branches_hash; |
| 52 | |
| 53 | struct branch *current_branch; |
| 54 | char *pushremote_name; |
| 55 | |
| 56 | struct rewrites rewrites; |
| 57 | struct rewrites rewrites_push; |
| 58 | |
| 59 | int initialized; |
| 60 | }; |
| 61 | |
| 62 | void remote_state_clear(struct remote_state *remote_state); |
| 63 | struct remote_state *remote_state_new(void); |
| 64 | |
| 65 | enum follow_remote_head_settings { |
| 66 | FOLLOW_REMOTE_NEVER = -1, |
| 67 | FOLLOW_REMOTE_CREATE = 0, |
| 68 | FOLLOW_REMOTE_WARN = 1, |
| 69 | FOLLOW_REMOTE_ALWAYS = 2, |
| 70 | }; |
| 71 | |
| 72 | struct remote { |
| 73 | struct hashmap_entry ent; |
| 74 | |
| 75 | /* The user's nickname for the remote */ |
| 76 | const char *name; |
| 77 | |
| 78 | int origin, configured_in_repo; |
| 79 | |
| 80 | char *foreign_vcs; |
| 81 | |
| 82 | /* An array of all of the url_nr URLs configured for the remote */ |
| 83 | struct strvec url; |
| 84 | /* An array of all of the pushurl_nr push URLs configured for the remote */ |
| 85 | struct strvec pushurl; |
| 86 | |
| 87 | struct refspec push; |
| 88 | |
| 89 | struct refspec fetch; |
| 90 | |
| 91 | /* |
| 92 | * The setting for whether to fetch tags (as a separate rule from the |
| 93 | * configured refspecs); |
| 94 | * -1 to never fetch tags |
| 95 | * 0 to auto-follow tags on heuristic (default) |
| 96 | * 1 to always auto-follow tags |
| 97 | * 2 to always fetch tags |
| 98 | */ |
| 99 | int fetch_tags; |
| 100 | |
| 101 | int skip_default_update; |
| 102 | int mirror; |
| 103 | int prune; |
| 104 | int prune_tags; |
| 105 | |
| 106 | /** |
| 107 | * The configured helper programs to run on the remote side, for |
| 108 | * Git-native protocols. |
| 109 | */ |
| 110 | const char *receivepack; |
| 111 | const char *uploadpack; |
| 112 | |
| 113 | /* The proxy to use for curl (http, https, ftp, etc.) URLs. */ |
| 114 | char *http_proxy; |
| 115 | |
| 116 | /* The method used for authenticating against `http_proxy`. */ |
| 117 | char *http_proxy_authmethod; |
| 118 | |
| 119 | struct string_list server_options; |
| 120 | struct string_list negotiation_restrict; |
| 121 | struct string_list negotiation_include; |
| 122 | |
| 123 | enum follow_remote_head_settings follow_remote_head; |
| 124 | const char *no_warn_branch; |
| 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * struct remotes can be found by name with remote_get(). |
| 129 | * remote_get(NULL) will return the default remote, given the current branch |
| 130 | * and configuration. |
| 131 | */ |
| 132 | struct remote *remote_get(const char *name); |
| 133 | struct remote *remote_get_early(const char *name); |
| 134 | |
| 135 | struct remote *pushremote_get(const char *name); |
| 136 | int remote_is_configured(struct remote *remote, int in_repo); |
| 137 | |
| 138 | typedef int each_remote_fn(struct remote *remote, void *priv); |
| 139 | |
| 140 | /* iterate through struct remotes */ |
| 141 | int for_each_remote(each_remote_fn fn, void *priv); |
| 142 | |
| 143 | int remote_has_url(struct remote *remote, const char *url); |
| 144 | struct strvec *push_url_of_remote(struct remote *remote); |
| 145 | |
| 146 | struct ref_push_report { |
| 147 | char *ref_name; |
| 148 | struct object_id *old_oid; |
| 149 | struct object_id *new_oid; |
| 150 | unsigned int forced_update:1; |
| 151 | struct ref_push_report *next; |
| 152 | }; |
| 153 | |
| 154 | void ref_push_report_free(struct ref_push_report *); |
| 155 | |
| 156 | struct ref { |
| 157 | struct ref *next; |
| 158 | struct object_id old_oid; |
| 159 | struct object_id new_oid; |
| 160 | struct object_id old_oid_expect; /* used by expect-old */ |
| 161 | char *symref; |
| 162 | char *tracking_ref; |
| 163 | unsigned int |
| 164 | force:1, |
| 165 | forced_update:1, |
| 166 | expect_old_sha1:1, |
| 167 | exact_oid:1, |
| 168 | deletion:1, |
| 169 | /* Need to check if local reflog reaches the remote tip. */ |
| 170 | check_reachable:1, |
| 171 | /* |
| 172 | * Store the result of the check enabled by "check_reachable"; |
| 173 | * implies the local reflog does not reach the remote tip. |
| 174 | */ |
| 175 | unreachable:1; |
| 176 | |
| 177 | enum { |
| 178 | REF_NOT_MATCHED = 0, /* initial value */ |
| 179 | REF_MATCHED, |
| 180 | REF_UNADVERTISED_NOT_ALLOWED |
| 181 | } match_status; |
| 182 | |
| 183 | /* |
| 184 | * Order is important here, as we write to FETCH_HEAD |
| 185 | * in numeric order. And the default NOT_FOR_MERGE |
| 186 | * should be 0, so that xcalloc'd structures get it |
| 187 | * by default. |
| 188 | */ |
| 189 | enum fetch_head_status { |
| 190 | FETCH_HEAD_MERGE = -1, |
| 191 | FETCH_HEAD_NOT_FOR_MERGE = 0, |
| 192 | FETCH_HEAD_IGNORE = 1 |
| 193 | } fetch_head_status; |
| 194 | |
| 195 | enum { |
| 196 | REF_STATUS_NONE = 0, |
| 197 | REF_STATUS_OK, |
| 198 | REF_STATUS_REJECT_NONFASTFORWARD, |
| 199 | REF_STATUS_REJECT_ALREADY_EXISTS, |
| 200 | REF_STATUS_REJECT_NODELETE, |
| 201 | REF_STATUS_REJECT_FETCH_FIRST, |
| 202 | REF_STATUS_REJECT_NEEDS_FORCE, |
| 203 | REF_STATUS_REJECT_STALE, |
| 204 | REF_STATUS_REJECT_SHALLOW, |
| 205 | REF_STATUS_REJECT_REMOTE_UPDATED, |
| 206 | REF_STATUS_UPTODATE, |
| 207 | REF_STATUS_REMOTE_REJECT, |
| 208 | REF_STATUS_EXPECTING_REPORT, |
| 209 | REF_STATUS_ATOMIC_PUSH_FAILED |
| 210 | } status; |
| 211 | char *remote_status; |
| 212 | struct ref_push_report *report; |
| 213 | struct ref *peer_ref; /* when renaming */ |
| 214 | char name[FLEX_ARRAY]; /* more */ |
| 215 | }; |
| 216 | |
| 217 | #define REF_NORMAL (1u << 0) |
| 218 | #define REF_BRANCHES (1u << 1) |
| 219 | #define REF_TAGS (1u << 2) |
| 220 | |
| 221 | struct ref *find_ref_by_name(const struct ref *list, const char *name); |
| 222 | |
| 223 | struct ref *alloc_ref(const char *name); |
| 224 | struct ref *copy_ref(const struct ref *ref); |
| 225 | struct ref *copy_ref_list(const struct ref *ref); |
| 226 | int count_refspec_match(const char *, struct ref *refs, struct ref **matched_ref); |
| 227 | /* |
| 228 | * Put a ref in the tail and prepare tail for adding another one. |
| 229 | * *tail is the pointer to the tail of the list of refs. |
| 230 | */ |
| 231 | void tail_link_ref(struct ref *ref, struct ref ***tail); |
| 232 | |
| 233 | int check_ref_type(const struct ref *ref, int flags); |
| 234 | |
| 235 | /* |
| 236 | * Free a single ref and its peer, or an entire list of refs and their peers, |
| 237 | * respectively. |
| 238 | */ |
| 239 | void free_one_ref(struct ref *ref); |
| 240 | void free_refs(struct ref *ref); |
| 241 | |
| 242 | struct oid_array; |
| 243 | struct packet_reader; |
| 244 | struct strvec; |
| 245 | struct string_list; |
| 246 | struct ref **get_remote_heads(struct packet_reader *reader, |
| 247 | struct ref **list, unsigned int flags, |
| 248 | struct oid_array *extra_have, |
| 249 | struct oid_array *shallow_points); |
| 250 | |
| 251 | /* Used for protocol v2 in order to retrieve refs from a remote */ |
| 252 | struct ref **get_remote_refs(int fd_out, struct packet_reader *reader, |
| 253 | struct ref **list, int for_push, |
| 254 | struct transport_ls_refs_options *transport_options, |
| 255 | const struct string_list *server_options, |
| 256 | int stateless_rpc); |
| 257 | |
| 258 | /* Used for protocol v2 in order to retrieve refs from a remote */ |
| 259 | struct bundle_list; |
| 260 | int get_remote_bundle_uri(int fd_out, struct packet_reader *reader, |
| 261 | struct bundle_list *bundles, int stateless_rpc); |
| 262 | |
| 263 | int resolve_remote_symref(struct ref *ref, struct ref *list); |
| 264 | |
| 265 | /* |
| 266 | * Remove and free all but the first of any entries in the input list |
| 267 | * that map the same remote reference to the same local reference. If |
| 268 | * there are two entries that map different remote references to the |
| 269 | * same local reference, emit an error message and die. Return a |
| 270 | * pointer to the head of the resulting list. |
| 271 | */ |
| 272 | struct ref *ref_remove_duplicates(struct ref *ref_map); |
| 273 | |
| 274 | int check_push_refs(struct ref *src, struct refspec *rs); |
| 275 | int match_push_refs(struct ref *src, struct ref **dst, |
| 276 | struct refspec *rs, int flags); |
| 277 | void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, |
| 278 | int force_update); |
| 279 | |
| 280 | /* |
| 281 | * Given a list of the remote refs and the specification of things to |
| 282 | * fetch, makes a (separate) list of the refs to fetch and the local |
| 283 | * refs to store into. Note that negative refspecs are ignored here, and |
| 284 | * should be handled separately. |
| 285 | * |
| 286 | * *tail is the pointer to the tail pointer of the list of results |
| 287 | * beforehand, and will be set to the tail pointer of the list of |
| 288 | * results afterward. |
| 289 | * |
| 290 | * missing_ok is usually false, but when we are adding branch.$name.merge |
| 291 | * it is Ok if the branch is not at the remote anymore. |
| 292 | */ |
| 293 | int get_fetch_map(const struct ref *remote_refs, const struct refspec_item *refspec, |
| 294 | struct ref ***tail, int missing_ok); |
| 295 | |
| 296 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name); |
| 297 | |
| 298 | /* |
| 299 | * For the given remote, reads the refspec's src and sets the other fields. |
| 300 | */ |
| 301 | int remote_find_tracking(struct remote *remote, struct refspec_item *refspec); |
| 302 | |
| 303 | /** |
| 304 | * struct branch holds the configuration for a branch. It can be looked up with |
| 305 | * branch_get(name) for "refs/heads/{name}", or with branch_get(NULL) for HEAD. |
| 306 | */ |
| 307 | struct branch { |
| 308 | struct hashmap_entry ent; |
| 309 | |
| 310 | /* The short name of the branch. */ |
| 311 | const char *name; |
| 312 | |
| 313 | /* The full path for the branch ref. */ |
| 314 | const char *refname; |
| 315 | |
| 316 | /* The name of the remote listed in the configuration. */ |
| 317 | char *remote_name; |
| 318 | |
| 319 | char *pushremote_name; |
| 320 | |
| 321 | /* True if set_merge() has been called to finalize the merge array */ |
| 322 | int set_merge; |
| 323 | |
| 324 | /** |
| 325 | * An array of the struct refspecs used for the merge lines. That is, |
| 326 | * merge[i]->dst is a local tracking ref which should be merged into this |
| 327 | * branch by default. |
| 328 | */ |
| 329 | struct refspec_item **merge; |
| 330 | |
| 331 | /* The number of merge configurations */ |
| 332 | int merge_nr; |
| 333 | |
| 334 | int merge_alloc; |
| 335 | |
| 336 | char *push_tracking_ref; |
| 337 | }; |
| 338 | |
| 339 | struct branch *branch_get(const char *name); |
| 340 | const char *remote_for_branch(struct branch *branch, int *explicit); |
| 341 | const char *pushremote_for_branch(struct branch *branch, int *explicit); |
| 342 | char *remote_ref_for_branch(struct branch *branch, int for_push); |
| 343 | |
| 344 | const char *repo_default_remote(struct repository *repo); |
| 345 | const char *repo_remote_from_url(struct repository *repo, const char *url); |
| 346 | |
| 347 | /* returns true if the given branch has merge configuration given. */ |
| 348 | int branch_has_merge_config(struct branch *branch); |
| 349 | |
| 350 | int branch_merge_matches(struct branch *, int n, const char *); |
| 351 | |
| 352 | /* list of the remote in a group as configured */ |
| 353 | struct remote_group_data { |
| 354 | const char *name; |
| 355 | struct string_list *list; |
| 356 | }; |
| 357 | |
| 358 | int get_remote_group(const char *key, const char *value, |
| 359 | const struct config_context *ctx, |
| 360 | void *priv); |
| 361 | |
| 362 | int add_remote_or_group(const char *name, struct string_list *list); |
| 363 | |
| 364 | /** |
| 365 | * Return the fully-qualified refname of the tracking branch for `branch`. |
| 366 | * I.e., what "branch@{upstream}" would give you. Returns NULL if no |
| 367 | * upstream is defined. |
| 368 | * |
| 369 | * If `err` is not NULL and no upstream is defined, a more specific error |
| 370 | * message is recorded there (if the function does not return NULL, then |
| 371 | * `err` is not touched). |
| 372 | */ |
| 373 | const char *branch_get_upstream(struct branch *branch, struct strbuf *err); |
| 374 | |
| 375 | /** |
| 376 | * Return the tracking branch that corresponds to the ref we would push to |
| 377 | * given a bare `git push` while `branch` is checked out. |
| 378 | * |
| 379 | * The return value and `err` conventions match those of `branch_get_upstream`. |
| 380 | */ |
| 381 | const char *branch_get_push(struct branch *branch, struct strbuf *err); |
| 382 | |
| 383 | /* Flags to match_refs. */ |
| 384 | enum match_refs_flags { |
| 385 | MATCH_REFS_NONE = 0, |
| 386 | MATCH_REFS_ALL = (1 << 0), |
| 387 | MATCH_REFS_MIRROR = (1 << 1), |
| 388 | MATCH_REFS_PRUNE = (1 << 2), |
| 389 | MATCH_REFS_FOLLOW_TAGS = (1 << 3) |
| 390 | }; |
| 391 | |
| 392 | /* Flags for --ahead-behind option. */ |
| 393 | enum ahead_behind_flags { |
| 394 | AHEAD_BEHIND_UNSPECIFIED = -1, |
| 395 | AHEAD_BEHIND_QUICK = 0, /* just eq/neq reporting */ |
| 396 | AHEAD_BEHIND_FULL = 1, /* traditional a/b reporting */ |
| 397 | }; |
| 398 | |
| 399 | /* Reporting of tracking info */ |
| 400 | int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, |
| 401 | const char **upstream_name, int for_push, |
| 402 | enum ahead_behind_flags abf); |
| 403 | int format_tracking_info(struct branch *branch, struct strbuf *sb, |
| 404 | enum ahead_behind_flags abf, |
| 405 | int show_divergence_advice); |
| 406 | |
| 407 | struct ref *get_local_heads(void); |
| 408 | |
| 409 | /* |
| 410 | * Find refs from a list which are likely to be pointed to by the given HEAD |
| 411 | * ref. If REMOTE_GUESS_HEAD_ALL is set, return a list of all candidate refs; |
| 412 | * otherwise, return the most likely ref. If no match is found (or 'head' is |
| 413 | * NULL), returns NULL. All returns are newly allocated and should be freed. |
| 414 | */ |
| 415 | #define REMOTE_GUESS_HEAD_ALL (1 << 0) |
| 416 | #define REMOTE_GUESS_HEAD_QUIET (1 << 1) |
| 417 | struct ref *guess_remote_head(const struct ref *head, |
| 418 | const struct ref *refs, |
| 419 | unsigned flags); |
| 420 | |
| 421 | /* Return refs which no longer exist on remote */ |
| 422 | struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map); |
| 423 | |
| 424 | /* |
| 425 | * Compare-and-swap |
| 426 | */ |
| 427 | struct push_cas_option { |
| 428 | unsigned use_tracking_for_rest:1; |
| 429 | unsigned use_force_if_includes:1; |
| 430 | struct push_cas { |
| 431 | struct object_id expect; |
| 432 | unsigned use_tracking:1; |
| 433 | char *refname; |
| 434 | } *entry; |
| 435 | size_t nr; |
| 436 | size_t alloc; |
| 437 | }; |
| 438 | |
| 439 | int parseopt_push_cas_option(const struct option *, const char *arg, int unset); |
| 440 | void clear_cas_option(struct push_cas_option *); |
| 441 | |
| 442 | int is_empty_cas(const struct push_cas_option *); |
| 443 | void apply_push_cas(struct push_cas_option *, struct remote *, struct ref *); |
| 444 | |
| 445 | /* |
| 446 | * The `url` argument is the URL that navigates to the submodule origin |
| 447 | * repo. When relative, this URL is relative to the superproject origin |
| 448 | * URL repo. The `up_path` argument, if specified, is the relative |
| 449 | * path that navigates from the submodule working tree to the superproject |
| 450 | * working tree. Returns the origin URL of the submodule. |
| 451 | * |
| 452 | * Return either an absolute URL or filesystem path (if the superproject |
| 453 | * origin URL is an absolute URL or filesystem path, respectively) or a |
| 454 | * relative file system path (if the superproject origin URL is a relative |
| 455 | * file system path). |
| 456 | * |
| 457 | * When the output is a relative file system path, the path is either |
| 458 | * relative to the submodule working tree, if up_path is specified, or to |
| 459 | * the superproject working tree otherwise. |
| 460 | * |
| 461 | * NEEDSWORK: This works incorrectly on the domain and protocol part. |
| 462 | * remote_url url outcome expectation |
| 463 | * http://a.com/b ../c http://a.com/c as is |
| 464 | * http://a.com/b/ ../c http://a.com/c same as previous line, but |
| 465 | * ignore trailing slash in url |
| 466 | * http://a.com/b ../../c http://c error out |
| 467 | * http://a.com/b ../../../c http:/c error out |
| 468 | * http://a.com/b ../../../../c http:c error out |
| 469 | * http://a.com/b ../../../../../c .:c error out |
| 470 | * http://a.com/b http://d.org/e http://d.org/e as is |
| 471 | * NEEDSWORK: Given how chop_last_dir() works, this function is broken |
| 472 | * when a local part has a colon in its path component, too. |
| 473 | */ |
| 474 | char *relative_url(const char *remote_url, const char *url, |
| 475 | const char *up_path); |
| 476 | |
| 477 | int valid_remote_name(const char *name); |
| 478 | |
| 479 | #endif |