ee3a7e9c052ab5936d99e739fd18b0ede4799454
1#include "cache.h"
2#include "remote.h"
3#include "refspec.h"
4#include "checkout.h"
5
6struct tracking_name_data {
7 /* const */ char *src_ref;
8 char *dst_ref;
9 struct object_id *dst_oid;
10 int num_matches;
11};
12
13#define TRACKING_NAME_DATA_INIT { NULL, NULL, NULL, 0 }
14
15static int check_tracking_name(struct remote *remote, void *cb_data)
16{
17 struct tracking_name_data *cb = cb_data;
18 struct refspec_item query;
19 memset(&query, 0, sizeof(struct refspec_item));
20 query.src = cb->src_ref;
21 if (remote_find_tracking(remote, &query) ||
22 get_oid(query.dst, cb->dst_oid)) {
23 free(query.dst);
24 return 0;
25 }
26 cb->num_matches++;
27 if (cb->dst_ref) {
28 free(query.dst);
29 return 0;
30 }
31 cb->dst_ref = query.dst;
32 return 0;
33}
34
35const char *unique_tracking_name(const char *name, struct object_id *oid,
36 int *dwim_remotes_matched)
37{
38 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
39 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
40 cb_data.dst_oid = oid;
41 for_each_remote(check_tracking_name, &cb_data);
42 if (dwim_remotes_matched)
43 *dwim_remotes_matched = cb_data.num_matches;
44 free(cb_data.src_ref);
45 if (cb_data.num_matches == 1)
46 return cb_data.dst_ref;
47 free(cb_data.dst_ref);
48 return NULL;
49}