checkout.c: introduce an *_INIT macro
[git/git.git] / checkout.c
CommitLineData
7c85a87c
TG
1#include "cache.h"
2#include "remote.h"
ec0cb496 3#include "refspec.h"
7c85a87c
TG
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 unique;
11};
12
e417151b
ÆAB
13#define TRACKING_NAME_DATA_INIT { NULL, NULL, NULL, 1 }
14
7c85a87c
TG
15static int check_tracking_name(struct remote *remote, void *cb_data)
16{
17 struct tracking_name_data *cb = cb_data;
0ad4a5ff
BW
18 struct refspec_item query;
19 memset(&query, 0, sizeof(struct refspec_item));
7c85a87c
TG
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 if (cb->dst_ref) {
27 free(query.dst);
28 cb->unique = 0;
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{
e417151b 37 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
7c85a87c
TG
38 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
39 cb_data.dst_oid = oid;
40 for_each_remote(check_tracking_name, &cb_data);
41 free(cb_data.src_ref);
42 if (cb_data.unique)
43 return cb_data.dst_ref;
44 free(cb_data.dst_ref);
45 return NULL;
46}