Elijah Newren | e93fc5d | 2023-04-11 00:41:50 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 2 | #include "object-name.h" |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 3 | #include "remote.h" |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 4 | #include "refspec.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 5 | #include "repository.h" |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 6 | #include "checkout.h" |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 7 | #include "config.h" |
Elijah Newren | e93fc5d | 2023-04-11 00:41:50 -0700 | [diff] [blame] | 8 | #include "strbuf.h" |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 9 | |
| 10 | struct tracking_name_data { |
| 11 | /* const */ char *src_ref; |
| 12 | char *dst_ref; |
| 13 | struct object_id *dst_oid; |
Ævar Arnfjörð Bjarmason | e4d2d55 | 2018-06-05 14:40:45 +0000 | [diff] [blame] | 14 | int num_matches; |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 15 | const char *default_remote; |
| 16 | char *default_dst_ref; |
| 17 | struct object_id *default_dst_oid; |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
Ævar Arnfjörð Bjarmason | 9865b6e | 2021-09-27 14:54:25 +0200 | [diff] [blame] | 20 | #define TRACKING_NAME_DATA_INIT { 0 } |
Ævar Arnfjörð Bjarmason | e417151 | 2018-06-05 14:40:44 +0000 | [diff] [blame] | 21 | |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 22 | static int check_tracking_name(struct remote *remote, void *cb_data) |
| 23 | { |
| 24 | struct tracking_name_data *cb = cb_data; |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 25 | struct refspec_item query; |
| 26 | memset(&query, 0, sizeof(struct refspec_item)); |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 27 | query.src = cb->src_ref; |
| 28 | if (remote_find_tracking(remote, &query) || |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 29 | repo_get_oid(the_repository, query.dst, cb->dst_oid)) { |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 30 | free(query.dst); |
| 31 | return 0; |
| 32 | } |
Ævar Arnfjörð Bjarmason | e4d2d55 | 2018-06-05 14:40:45 +0000 | [diff] [blame] | 33 | cb->num_matches++; |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 34 | if (cb->default_remote && !strcmp(remote->name, cb->default_remote)) { |
| 35 | struct object_id *dst = xmalloc(sizeof(*cb->default_dst_oid)); |
| 36 | cb->default_dst_ref = xstrdup(query.dst); |
| 37 | oidcpy(dst, cb->dst_oid); |
| 38 | cb->default_dst_oid = dst; |
| 39 | } |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 40 | if (cb->dst_ref) { |
| 41 | free(query.dst); |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | cb->dst_ref = query.dst; |
| 45 | return 0; |
| 46 | } |
| 47 | |
Ævar Arnfjörð Bjarmason | 3c87aa9 | 2018-06-05 14:40:46 +0000 | [diff] [blame] | 48 | const char *unique_tracking_name(const char *name, struct object_id *oid, |
| 49 | int *dwim_remotes_matched) |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 50 | { |
Ævar Arnfjörð Bjarmason | e417151 | 2018-06-05 14:40:44 +0000 | [diff] [blame] | 51 | struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT; |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 52 | const char *default_remote = NULL; |
Jeff King | 9a53219 | 2020-08-17 17:33:11 -0400 | [diff] [blame] | 53 | if (!git_config_get_string_tmp("checkout.defaultremote", &default_remote)) |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 54 | cb_data.default_remote = default_remote; |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 55 | cb_data.src_ref = xstrfmt("refs/heads/%s", name); |
| 56 | cb_data.dst_oid = oid; |
| 57 | for_each_remote(check_tracking_name, &cb_data); |
Ævar Arnfjörð Bjarmason | 3c87aa9 | 2018-06-05 14:40:46 +0000 | [diff] [blame] | 58 | if (dwim_remotes_matched) |
| 59 | *dwim_remotes_matched = cb_data.num_matches; |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 60 | free(cb_data.src_ref); |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 61 | if (cb_data.num_matches == 1) { |
| 62 | free(cb_data.default_dst_ref); |
| 63 | free(cb_data.default_dst_oid); |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 64 | return cb_data.dst_ref; |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 65 | } |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 66 | free(cb_data.dst_ref); |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 67 | if (cb_data.default_dst_ref) { |
| 68 | oidcpy(oid, cb_data.default_dst_oid); |
| 69 | free(cb_data.default_dst_oid); |
| 70 | return cb_data.default_dst_ref; |
| 71 | } |
Thomas Gummerer | 7c85a87 | 2017-11-26 19:43:51 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |