Commit 9c82357be74bc5404038ec3c16706b1805843556

Carlos Martín Nieto 2011-06-17T18:13:14

Add a remotes API Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
diff --git a/include/git2.h b/include/git2.h
index ff1c118..e04b0bd 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -54,4 +54,7 @@
 #include "git2/index.h"
 #include "git2/config.h"
 
+#include "git2/remote.h"
+#include "git2/refspec.h"
+
 #endif
diff --git a/include/git2/branch.h b/include/git2/branch.h
new file mode 100644
index 0000000..456b7d1
--- /dev/null
+++ b/include/git2/branch.h
@@ -0,0 +1,9 @@
+#ifndef INCLUDE_branch_h__
+#define INCLUDE_branch_h__
+
+struct git_branch {
+	char *remote; /* TODO: Make this a git_remote */
+	char *merge;
+};
+
+#endif
diff --git a/include/git2/refspec.h b/include/git2/refspec.h
new file mode 100644
index 0000000..d45364f
--- /dev/null
+++ b/include/git2/refspec.h
@@ -0,0 +1,22 @@
+#ifndef INCLUDE_git_refspec_h__
+#define INCLUDE_git_refspec_h__
+
+#include "git2/types.h"
+
+/**
+ * Get the source specifier
+ *
+ * @param refspec the refspec
+ * @return the refspec's source specifier
+ */
+const char *git_refspec_src(const git_refspec *refspec);
+
+/**
+ * Get the destination specifier
+ *
+ * @param refspec the refspec
+ * @return the refspec's destination specifier
+ */
+const char *git_refspec_dst(const git_refspec *refspec);
+
+#endif
diff --git a/include/git2/remote.h b/include/git2/remote.h
new file mode 100644
index 0000000..8edb743
--- /dev/null
+++ b/include/git2/remote.h
@@ -0,0 +1,58 @@
+#ifndef INCLUDE_git_remote_h__
+#define INCLUDE_git_remote_h__
+
+#include "git2/common.h"
+#include "git2/repository.h"
+#include "git2/refspec.h"
+
+/**
+ * Get the information for a particular remote
+ *
+ * @param out pointer to the new remote object
+ * @param cfg the repository's configuration
+ * @param name the remote's name
+ * @return 0 on success; error value otherwise
+ */
+GIT_EXTERN(int) git_remote_get(struct git_remote **out, struct git_config *cfg, const char *name);
+
+/**
+ * Get the remote's name
+ *
+ * @param remote the remote
+ * @return a pointer to the name
+ */
+GIT_EXTERN(const char *) git_remote_name(struct git_remote *remote);
+
+/**
+ * Get the remote's url
+ *
+ * @param remote the remote
+ * @return a pointer to the url
+ */
+GIT_EXTERN(const char *) git_remote_url(struct git_remote *remote);
+
+/**
+ * Get the fetch refspec
+ *
+ * @param remote the remote
+ * @return a pointer to the fetch refspec or NULL if it doesn't exist
+ */
+GIT_EXTERN(const git_refspec *) git_remote_fetchspec(struct git_remote *remote);
+
+/**
+ * Get the push refspec
+ *
+ * @param remote the remote
+ * @return a pointer to the push refspec or NULL if it doesn't exist
+ */
+
+GIT_EXTERN(const git_refspec *) git_remote_fetchspec(struct git_remote *remote);
+
+/**
+ * Free the memory associated with a remote
+ *
+ * @param remote the remote to free
+ */
+GIT_EXTERN(void) git_remote_free(struct git_remote *remote);
+
+#endif
diff --git a/include/git2/types.h b/include/git2/types.h
index 499ba98..963156f 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -167,6 +167,9 @@ typedef enum {
 	GIT_REF_LISTALL = GIT_REF_OID|GIT_REF_SYMBOLIC|GIT_REF_PACKED,
 } git_rtype;
 
+typedef struct git_refspec git_refspec;
+typedef struct git_remote git_remote;
+
 /** @} */
 GIT_END_DECL
 
diff --git a/src/refspec.c b/src/refspec.c
new file mode 100644
index 0000000..7a85259
--- /dev/null
+++ b/src/refspec.c
@@ -0,0 +1,66 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file.  (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "common.h"
+#include "refspec.h"
+
+int git_refspec_parse(git_refspec *refspec, const char *str)
+{
+	char *delim;
+
+	memset(refspec, 0x0, sizeof(git_refspec));
+
+	if (*str == '+') {
+		refspec->force = 1;
+		str++;
+	}
+
+	delim = strchr(str, ':');
+	if (delim == NULL)
+		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse refspec. No ':'");
+
+	refspec->src = git__strndup(str, delim - str);
+	if (refspec->src == NULL)
+		return GIT_ENOMEM;
+
+	refspec->dst = git__strdup(delim + 1);
+	if (refspec->dst == NULL) {
+		free(refspec->src);
+		refspec->src = NULL;
+		return GIT_ENOMEM;
+	}
+
+	return GIT_SUCCESS;
+}
+
+const char *git_refspec_src(const git_refspec *refspec)
+{
+	return refspec->src;
+}
+
+const char *git_refspec_dst(const git_refspec *refspec)
+{
+	return refspec->dst;
+}
diff --git a/src/refspec.h b/src/refspec.h
new file mode 100644
index 0000000..230135a
--- /dev/null
+++ b/src/refspec.h
@@ -0,0 +1,14 @@
+#ifndef INCLUDE_refspec_h__
+#define INCLUDE_refspec_h__
+
+#include "git2/refspec.h"
+
+struct git_refspec {
+	int force;
+	char *src;
+	char *dst;
+};
+
+int git_refspec_parse(struct git_refspec *refspec, const char *str);
+
+#endif
diff --git a/src/remote.c b/src/remote.c
new file mode 100644
index 0000000..7da6e65
--- /dev/null
+++ b/src/remote.c
@@ -0,0 +1,186 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file.  (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "git2/remote.h"
+#include "git2/config.h"
+#include "git2/types.h"
+
+#include "config.h"
+#include "repository.h"
+#include "remote.h"
+
+static int refspec_parse(git_refspec *refspec, const char *str)
+{
+	char *delim;
+
+	memset(refspec, 0x0, sizeof(git_refspec));
+
+	if (*str == '+') {
+		refspec->force = 1;
+		str++;
+	}
+
+	delim = strchr(str, ':');
+	if (delim == NULL)
+		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse refspec. No ':'");
+
+	refspec->src = git__strndup(str, delim - str);
+	if (refspec->src == NULL)
+		return GIT_ENOMEM;
+
+	refspec->dst = git__strdup(delim + 1);
+	if (refspec->dst == NULL) {
+		free(refspec->src);
+		refspec->src = NULL;
+		return GIT_ENOMEM;
+	}
+
+	return GIT_SUCCESS;
+}
+
+static int parse_remote_refspec(git_config *cfg, git_refspec *refspec, const char *var)
+{
+	const char *val;
+	int error;
+
+	error = git_config_get_string(cfg, var, &val);
+	if (error < GIT_SUCCESS)
+		return error;
+
+	return git_refspec_parse(refspec, val);
+}
+
+int git_remote_get(git_remote **out, git_config *cfg, const char *name)
+{
+	git_remote *remote;
+	char *buf = NULL;
+	const char *val;
+	int ret, error, buf_len;
+
+	remote = git__malloc(sizeof(git_remote));
+	if (remote == NULL)
+		return GIT_ENOMEM;
+
+	memset(remote, 0x0, sizeof(git_remote));
+	remote->name = git__strdup(name);
+	if (remote->name == NULL) {
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+	/* "fetch" is the longest var name we're interested in */
+	buf_len = STRLEN("remote.") + STRLEN(".fetch") + strlen(name) + 1;
+	buf = git__malloc(buf_len);
+	if (buf == NULL) {
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+	ret = snprintf(buf, buf_len, "%s.%s.%s", "remote", name, "url");
+	if (ret < 0) {
+		error = git__throw(GIT_EOSERR, "Failed to build config var name");
+		goto cleanup;
+	}
+
+	error = git_config_get_string(cfg, buf, &val);
+	if (error < GIT_SUCCESS) {
+		error = git__rethrow(error,  "Remote's url doesn't exist");
+		goto cleanup;
+	}
+
+	remote->url = git__strdup(val);
+	if (remote->url == NULL) {
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+	ret = snprintf(buf, buf_len, "%s.%s.%s", "remote", name, "fetch");
+	if (ret < 0) {
+		error = git__throw(GIT_EOSERR, "Failed to build config var name");
+		goto cleanup;
+	}
+
+	error = git_config_get_string(cfg, buf, &val);
+	if (error < GIT_SUCCESS)
+		goto cleanup;
+
+	error = refspec_parse(&remote->fetch, val);
+	if (error < GIT_SUCCESS)
+		goto cleanup;
+
+	ret = snprintf(buf, buf_len, "%s.%s.%s", "remote", name, "push");
+	if (ret < 0) {
+		error = git__throw(GIT_EOSERR, "Failed to build config var name");
+		goto cleanup;
+	}
+
+	error = git_config_get_string(cfg, buf, &val);
+	if (error < GIT_SUCCESS)
+		goto cleanup;
+
+	error = refspec_parse(&remote->push, val);
+	if (error < GIT_SUCCESS)
+		goto cleanup;
+
+	*out = remote;
+
+cleanup:
+	free(buf);
+	if (error < GIT_SUCCESS)
+		git_remote_free(remote);
+
+	return error;
+}
+
+const char *git_remote_name(struct git_remote *remote)
+{
+	return remote->name;
+}
+
+const char *git_remote_url(struct git_remote *remote)
+{
+	return remote->url;
+}
+
+const git_refspec *git_remote_fetchspec(struct git_remote *remote)
+{
+	return &remote->fetch;
+}
+
+const git_refspec *git_remote_pushspec(struct git_remote *remote)
+{
+	return &remote->push;
+}
+
+void git_remote_free(git_remote *remote)
+{
+	free(remote->fetch.src);
+	free(remote->fetch.dst);
+	free(remote->push.src);
+	free(remote->push.dst);
+	free(remote->url);
+	free(remote->name);
+	free(remote);
+}
diff --git a/src/remote.h b/src/remote.h
new file mode 100644
index 0000000..afd2d1b
--- /dev/null
+++ b/src/remote.h
@@ -0,0 +1,14 @@
+#ifndef INCLUDE_remote_h__
+#define INCLUDE_remote_h__
+
+#include "remote.h"
+#include "refspec.h"
+
+struct git_remote {
+	char *name;
+	char *url;
+	struct git_refspec fetch;
+	struct git_refspec push;
+};
+
+#endif