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