Report information on branches from remote.h
This adds full parsing for branch.<name> sections and functions to interpret the results usefully. It incidentally corrects the fetch configuration information for legacy branches/* files with '#' characters in the URLs. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Sep 10, 2007 at 23:02 UTC
cf818348f1ab577d2ecb5d11a00a1d4122435ece
2 files changed
+164
-11
remote.c
+146
-11
@@ -5,6 +5,12 @@
5
static struct remote **remotes;
6
static int allocated_remotes;
7
8
+static struct branch **branches;
9
+static int allocated_branches;
10
+
11
+static struct branch *current_branch;
12
+static const char *default_remote_name;
13
+
14
#define BUF_SIZE (2048)
15
static char buffer[BUF_SIZE];
16
@@ -67,6 +73,54 @@ static struct remote *make_remote(const char *name, int len)
73
return remotes[empty];
74
}
75
76
+static void add_merge(struct branch *branch, const char *name)
77
+{
78
+ int nr = branch->merge_nr + 1;
79
+ branch->merge_name =
80
+ xrealloc(branch->merge_name, nr * sizeof(char *));
81
+ branch->merge_name[nr-1] = name;
82
+ branch->merge_nr = nr;
83
+}
84
+
85
+static struct branch *make_branch(const char *name, int len)
86
+{
87
+ int i, empty = -1;
88
+ char *refname;
89
+
90
+ for (i = 0; i < allocated_branches; i++) {
91
+ if (!branches[i]) {
92
+ if (empty < 0)
93
+ empty = i;
94
+ } else {
95
+ if (len ? (!strncmp(name, branches[i]->name, len) &&
96
+ !branches[i]->name[len]) :
97
+ !strcmp(name, branches[i]->name))
98
+ return branches[i];
99
+ }
100
+ }
101
+
102
+ if (empty < 0) {
103
+ empty = allocated_branches;
104
+ allocated_branches += allocated_branches ? allocated_branches : 1;
105
+ branches = xrealloc(branches,
106
+ sizeof(*branches) * allocated_branches);
107
+ memset(branches + empty, 0,
108
+ (allocated_branches - empty) * sizeof(*branches));
109
+ }
110
+ branches[empty] = xcalloc(1, sizeof(struct branch));
111
+ if (len)
112
+ branches[empty]->name = xstrndup(name, len);
113
+ else
114
+ branches[empty]->name = xstrdup(name);
115
+ refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
116
+ strcpy(refname, "refs/heads/");
117
+ strcpy(refname + strlen("refs/heads/"),
118
+ branches[empty]->name);
119
+ branches[empty]->refname = refname;
120
+
121
+ return branches[empty];
122
+}
123
+
124
static void read_remotes_file(struct remote *remote)
125
{
126
FILE *f = fopen(git_path("remotes/%s", remote->name), "r");
@@ -116,6 +170,8 @@ static void read_remotes_file(struct remote *remote)
170
static void read_branches_file(struct remote *remote)
171
{
172
const char *slash = strchr(remote->name, '/');
173
+ char *frag;
174
+ char *branch;
175
int n = slash ? slash - remote->name : 1000;
176
FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
177
char *s, *p;
@@ -141,23 +197,40 @@ static void read_branches_file(struct remote *remote)
197
strcpy(p, s);
198
if (slash)
199
strcat(p, slash);
200
+ frag = strchr(p, '#');
201
+ if (frag) {
202
+ *(frag++) = '\0';
203
+ branch = xmalloc(strlen(frag) + 12);
204
+ strcpy(branch, "refs/heads/");
205
+ strcat(branch, frag);
206
+ } else {
207
+ branch = "refs/heads/master";
208
+ }
209
add_uri(remote, p);
210
+ add_fetch_refspec(remote, branch);
211
}
212
147
-static char *default_remote_name = NULL;
148
-static const char *current_branch = NULL;
149
-static int current_branch_len = 0;
150
-
213
static int handle_config(const char *key, const char *value)
214
{
215
const char *name;
216
const char *subkey;
217
struct remote *remote;
156
- if (!prefixcmp(key, "branch.") && current_branch &&
157
- !strncmp(key + 7, current_branch, current_branch_len) &&
158
- !strcmp(key + 7 + current_branch_len, ".remote")) {
159
- free(default_remote_name);
160
- default_remote_name = xstrdup(value);
218
+ struct branch *branch;
219
+ if (!prefixcmp(key, "branch.")) {
220
+ name = key + 7;
221
+ subkey = strrchr(name, '.');
222
+ branch = make_branch(name, subkey - name);
223
+ if (!subkey)
224
+ return 0;
225
+ if (!value)
226
+ return 0;
227
+ if (!strcmp(subkey, ".remote")) {
228
+ branch->remote_name = xstrdup(value);
229
+ if (branch == current_branch)
230
+ default_remote_name = branch->remote_name;
231
+ } else if (!strcmp(subkey, ".merge"))
232
+ add_merge(branch, xstrdup(value));
233
+ return 0;
234
}
235
if (prefixcmp(key, "remote."))
236
return 0;
@@ -217,8 +290,8 @@ static void read_config(void)
290
head_ref = resolve_ref("HEAD", sha1, 0, &flag);
291
if (head_ref && (flag & REF_ISSYMREF) &&
292
!prefixcmp(head_ref, "refs/heads/")) {
220
- current_branch = head_ref + strlen("refs/heads/");
221
- current_branch_len = strlen(current_branch);
293
+ current_branch =
294
+ make_branch(head_ref + strlen("refs/heads/"), 0);
295
}
296
git_config(handle_config);
297
}
@@ -313,6 +386,25 @@ int remote_has_uri(struct remote *remote, const char *uri)
386
return 0;
387
}
388
389
+/*
390
+ * Returns true if, under the matching rules for fetching, name is the
391
+ * same as the given full name.
392
+ */
393
+static int ref_matches_abbrev(const char *name, const char *full)
394
+{
395
+ if (!prefixcmp(name, "refs/") || !strcmp(name, "HEAD"))
396
+ return !strcmp(name, full);
397
+ if (prefixcmp(full, "refs/"))
398
+ return 0;
399
+ if (!prefixcmp(name, "heads/") ||
400
+ !prefixcmp(name, "tags/") ||
401
+ !prefixcmp(name, "remotes/"))
402
+ return !strcmp(name, full + 5);
403
+ if (prefixcmp(full + 5, "heads/"))
404
+ return 0;
405
+ return !strcmp(full + 11, name);
406
+}
407
+
408
int remote_find_tracking(struct remote *remote, struct refspec *refspec)
409
{
410
int find_src = refspec->src == NULL;
@@ -636,3 +728,46 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
728
}
729
return 0;
730
}
731
+
732
+struct branch *branch_get(const char *name)
733
+{
734
+ struct branch *ret;
735
+
736
+ read_config();
737
+ if (!name || !*name || !strcmp(name, "HEAD"))
738
+ ret = current_branch;
739
+ else
740
+ ret = make_branch(name, 0);
741
+ if (ret && ret->remote_name) {
742
+ ret->remote = remote_get(ret->remote_name);
743
+ if (ret->merge_nr) {
744
+ int i;
745
+ ret->merge = xcalloc(sizeof(*ret->merge),
746
+ ret->merge_nr);
747
+ for (i = 0; i < ret->merge_nr; i++) {
748
+ ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
749
+ ret->merge[i]->src = xstrdup(ret->merge_name[i]);
750
+ remote_find_tracking(ret->remote,
751
+ ret->merge[i]);
752
+ }
753
+ }
754
+ }
755
+ return ret;
756
+}
757
+
758
+int branch_has_merge_config(struct branch *branch)
759
+{
760
+ return branch && !!branch->merge;
761
+}
762
+
763
+int branch_merges(struct branch *branch, const char *refname)
764
+{
765
+ int i;
766
+ if (!branch)
767
+ return 0;
768
+ for (i = 0; i < branch->merge_nr; i++) {
769
+ if (ref_matches_abbrev(branch->merge[i]->src, refname))
770
+ return 1;
771
+ }
772
+ return 0;
773
+}
remote.h
+18
@@ -49,4 +49,22 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
49
*/
50
int remote_find_tracking(struct remote *remote, struct refspec *refspec);
51
52
+struct branch {
53
+ const char *name;
54
+ const char *refname;
55
+
56
+ const char *remote_name;
57
+ struct remote *remote;
58
+
59
+ const char **merge_name;
60
+ struct refspec **merge;
61
+ int merge_nr;
62
+};
63
+
64
+struct branch *branch_get(const char *name);
65
+
66
+int branch_has_merge_config(struct branch *branch);
67
+
68
+int branch_merges(struct branch *branch, const char *refname);
69
+
70
#endif