]>
Commit | Line | Data |
---|---|---|
e93fc5d7 | 1 | #include "git-compat-util.h" |
dabab1d6 | 2 | #include "object-name.h" |
7c85a87c | 3 | #include "remote.h" |
ec0cb496 | 4 | #include "refspec.h" |
d1cbe1e6 | 5 | #include "repository.h" |
7c85a87c | 6 | #include "checkout.h" |
8d7b558b | 7 | #include "config.h" |
e93fc5d7 | 8 | #include "strbuf.h" |
7c85a87c TG |
9 | |
10 | struct tracking_name_data { | |
11 | /* const */ char *src_ref; | |
12 | char *dst_ref; | |
13 | struct object_id *dst_oid; | |
e4d2d55a | 14 | int num_matches; |
8d7b558b ÆAB |
15 | const char *default_remote; |
16 | char *default_dst_ref; | |
17 | struct object_id *default_dst_oid; | |
7c85a87c TG |
18 | }; |
19 | ||
9865b6e6 | 20 | #define TRACKING_NAME_DATA_INIT { 0 } |
e417151b | 21 | |
7c85a87c TG |
22 | static int check_tracking_name(struct remote *remote, void *cb_data) |
23 | { | |
24 | struct tracking_name_data *cb = cb_data; | |
0ad4a5ff BW |
25 | struct refspec_item query; |
26 | memset(&query, 0, sizeof(struct refspec_item)); | |
7c85a87c TG |
27 | query.src = cb->src_ref; |
28 | if (remote_find_tracking(remote, &query) || | |
d850b7a5 | 29 | repo_get_oid(the_repository, query.dst, cb->dst_oid)) { |
7c85a87c TG |
30 | free(query.dst); |
31 | return 0; | |
32 | } | |
e4d2d55a | 33 | cb->num_matches++; |
8d7b558b ÆAB |
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 | } | |
7c85a87c TG |
40 | if (cb->dst_ref) { |
41 | free(query.dst); | |
7c85a87c TG |
42 | return 0; |
43 | } | |
44 | cb->dst_ref = query.dst; | |
45 | return 0; | |
46 | } | |
47 | ||
3c87aa94 ÆAB |
48 | const char *unique_tracking_name(const char *name, struct object_id *oid, |
49 | int *dwim_remotes_matched) | |
7c85a87c | 50 | { |
e417151b | 51 | struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT; |
8d7b558b | 52 | const char *default_remote = NULL; |
9a53219f | 53 | if (!git_config_get_string_tmp("checkout.defaultremote", &default_remote)) |
8d7b558b | 54 | cb_data.default_remote = default_remote; |
7c85a87c TG |
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); | |
3c87aa94 ÆAB |
58 | if (dwim_remotes_matched) |
59 | *dwim_remotes_matched = cb_data.num_matches; | |
7c85a87c | 60 | free(cb_data.src_ref); |
8d7b558b ÆAB |
61 | if (cb_data.num_matches == 1) { |
62 | free(cb_data.default_dst_ref); | |
63 | free(cb_data.default_dst_oid); | |
7c85a87c | 64 | return cb_data.dst_ref; |
8d7b558b | 65 | } |
7c85a87c | 66 | free(cb_data.dst_ref); |
8d7b558b ÆAB |
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 | } | |
7c85a87c TG |
72 | return NULL; |
73 | } |