blob: 4256e71a7c41cd4fdfbcd832727c21548b52b740 [file] [log] [blame]
Elijah Newrene93fc5d2023-04-11 00:41:50 -07001#include "git-compat-util.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07002#include "object-name.h"
Thomas Gummerer7c85a872017-11-26 19:43:51 +00003#include "remote.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -07004#include "refspec.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00005#include "repository.h"
Thomas Gummerer7c85a872017-11-26 19:43:51 +00006#include "checkout.h"
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +00007#include "config.h"
Elijah Newrene93fc5d2023-04-11 00:41:50 -07008#include "strbuf.h"
Thomas Gummerer7c85a872017-11-26 19:43:51 +00009
10struct tracking_name_data {
11 /* const */ char *src_ref;
12 char *dst_ref;
13 struct object_id *dst_oid;
Ævar Arnfjörð Bjarmasone4d2d552018-06-05 14:40:45 +000014 int num_matches;
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000015 const char *default_remote;
16 char *default_dst_ref;
17 struct object_id *default_dst_oid;
Thomas Gummerer7c85a872017-11-26 19:43:51 +000018};
19
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +020020#define TRACKING_NAME_DATA_INIT { 0 }
Ævar Arnfjörð Bjarmasone4171512018-06-05 14:40:44 +000021
Thomas Gummerer7c85a872017-11-26 19:43:51 +000022static int check_tracking_name(struct remote *remote, void *cb_data)
23{
24 struct tracking_name_data *cb = cb_data;
Brandon Williams0ad4a5f2018-05-16 15:57:49 -070025 struct refspec_item query;
26 memset(&query, 0, sizeof(struct refspec_item));
Thomas Gummerer7c85a872017-11-26 19:43:51 +000027 query.src = cb->src_ref;
28 if (remote_find_tracking(remote, &query) ||
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +020029 repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
Thomas Gummerer7c85a872017-11-26 19:43:51 +000030 free(query.dst);
31 return 0;
32 }
Ævar Arnfjörð Bjarmasone4d2d552018-06-05 14:40:45 +000033 cb->num_matches++;
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000034 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 Gummerer7c85a872017-11-26 19:43:51 +000040 if (cb->dst_ref) {
41 free(query.dst);
Thomas Gummerer7c85a872017-11-26 19:43:51 +000042 return 0;
43 }
44 cb->dst_ref = query.dst;
45 return 0;
46}
47
Ævar Arnfjörð Bjarmason3c87aa92018-06-05 14:40:46 +000048const char *unique_tracking_name(const char *name, struct object_id *oid,
49 int *dwim_remotes_matched)
Thomas Gummerer7c85a872017-11-26 19:43:51 +000050{
Ævar Arnfjörð Bjarmasone4171512018-06-05 14:40:44 +000051 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000052 const char *default_remote = NULL;
Jeff King9a532192020-08-17 17:33:11 -040053 if (!git_config_get_string_tmp("checkout.defaultremote", &default_remote))
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000054 cb_data.default_remote = default_remote;
Thomas Gummerer7c85a872017-11-26 19:43:51 +000055 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ð Bjarmason3c87aa92018-06-05 14:40:46 +000058 if (dwim_remotes_matched)
59 *dwim_remotes_matched = cb_data.num_matches;
Thomas Gummerer7c85a872017-11-26 19:43:51 +000060 free(cb_data.src_ref);
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000061 if (cb_data.num_matches == 1) {
62 free(cb_data.default_dst_ref);
63 free(cb_data.default_dst_oid);
Thomas Gummerer7c85a872017-11-26 19:43:51 +000064 return cb_data.dst_ref;
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000065 }
Thomas Gummerer7c85a872017-11-26 19:43:51 +000066 free(cb_data.dst_ref);
Ævar Arnfjörð Bjarmason8d7b5582018-06-05 14:40:49 +000067 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 Gummerer7c85a872017-11-26 19:43:51 +000072 return NULL;
73}