Merge branch 'jk/test-hashmap-updates' into jch
[git] / checkout.c
1 #include "cache.h"
2 #include "remote.h"
3 #include "checkout.h"
4
5 struct tracking_name_data {
6         /* const */ char *src_ref;
7         char *dst_ref;
8         struct object_id *dst_oid;
9         int unique;
10 };
11
12 static int check_tracking_name(struct remote *remote, void *cb_data)
13 {
14         struct tracking_name_data *cb = cb_data;
15         struct refspec query;
16         memset(&query, 0, sizeof(struct refspec));
17         query.src = cb->src_ref;
18         if (remote_find_tracking(remote, &query) ||
19             get_oid(query.dst, cb->dst_oid)) {
20                 free(query.dst);
21                 return 0;
22         }
23         if (cb->dst_ref) {
24                 free(query.dst);
25                 cb->unique = 0;
26                 return 0;
27         }
28         cb->dst_ref = query.dst;
29         return 0;
30 }
31
32 const char *unique_tracking_name(const char *name, struct object_id *oid)
33 {
34         struct tracking_name_data cb_data = { NULL, NULL, NULL, 1 };
35         cb_data.src_ref = xstrfmt("refs/heads/%s", name);
36         cb_data.dst_oid = oid;
37         for_each_remote(check_tracking_name, &cb_data);
38         free(cb_data.src_ref);
39         if (cb_data.unique)
40                 return cb_data.dst_ref;
41         free(cb_data.dst_ref);
42         return NULL;
43 }