git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / checkout.c
| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "object-name.h" |
| 5 | #include "remote.h" |
| 6 | #include "refspec.h" |
| 7 | #include "repository.h" |
| 8 | #include "checkout.h" |
| 9 | #include "config.h" |
| 10 | #include "strbuf.h" |
| 11 | #include "string-list.h" |
| 12 | |
| 13 | struct tracking_name_data { |
| 14 | /* const */ char *src_ref; |
| 15 | char *dst_ref; |
| 16 | struct object_id *dst_oid; |
| 17 | int num_matches; |
| 18 | const char *default_remote; |
| 19 | char *default_dst_ref; |
| 20 | struct object_id *default_dst_oid; |
| 21 | struct string_list *remote_names; |
| 22 | }; |
| 23 | |
| 24 | #define TRACKING_NAME_DATA_INIT { 0 } |
| 25 | |
| 26 | static int check_tracking_name(struct remote *remote, void *cb_data) |
| 27 | { |
| 28 | struct tracking_name_data *cb = cb_data; |
| 29 | struct refspec_item query; |
| 30 | memset(&query, 0, sizeof(struct refspec_item)); |
| 31 | query.src = cb->src_ref; |
| 32 | if (remote_find_tracking(remote, &query) || |
| 33 | repo_get_oid(the_repository, query.dst, cb->dst_oid)) { |
| 34 | free(query.dst); |
| 35 | return 0; |
| 36 | } |
| 37 | cb->num_matches++; |
| 38 | if (cb->default_remote && !strcmp(remote->name, cb->default_remote)) { |
| 39 | struct object_id *dst = xmalloc(sizeof(*cb->default_dst_oid)); |
| 40 | cb->default_dst_ref = xstrdup(query.dst); |
| 41 | oidcpy(dst, cb->dst_oid); |
| 42 | cb->default_dst_oid = dst; |
| 43 | } |
| 44 | if (cb->remote_names) |
| 45 | string_list_append(cb->remote_names, remote->name); |
| 46 | if (cb->dst_ref) { |
| 47 | free(query.dst); |
| 48 | return 0; |
| 49 | } |
| 50 | cb->dst_ref = query.dst; |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | char *unique_tracking_name(const char *name, struct object_id *oid, |
| 55 | int *dwim_remotes_matched, |
| 56 | struct string_list *dwim_remote_names) |
| 57 | { |
| 58 | struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT; |
| 59 | const char *default_remote = NULL; |
| 60 | |
| 61 | if (!repo_config_get_string_tmp(the_repository, |
| 62 | "checkout.defaultremote", |
| 63 | &default_remote)) |
| 64 | cb_data.default_remote = default_remote; |
| 65 | cb_data.src_ref = xstrfmt("refs/heads/%s", name); |
| 66 | cb_data.dst_oid = oid; |
| 67 | cb_data.remote_names = dwim_remote_names; |
| 68 | for_each_remote(check_tracking_name, &cb_data); |
| 69 | if (dwim_remotes_matched) |
| 70 | *dwim_remotes_matched = cb_data.num_matches; |
| 71 | free(cb_data.src_ref); |
| 72 | if (cb_data.num_matches == 1) { |
| 73 | free(cb_data.default_dst_ref); |
| 74 | free(cb_data.default_dst_oid); |
| 75 | return cb_data.dst_ref; |
| 76 | } |
| 77 | free(cb_data.dst_ref); |
| 78 | if (cb_data.default_dst_ref) { |
| 79 | oidcpy(oid, cb_data.default_dst_oid); |
| 80 | free(cb_data.default_dst_oid); |
| 81 | return cb_data.default_dst_ref; |
| 82 | } |
| 83 | return NULL; |
| 84 | } |