Commit 56960b8396d3aef0b39f32aa7a9749202f925ada

Vicent Marti 2013-05-28T20:47:55

Liike this

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
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 468d0f9..4d9ec83 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -351,7 +351,9 @@ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2);
  * @param repo the repository
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_reference_iterator_new(git_reference_iterator **out, git_repository *repo);
+GIT_EXTERN(int) git_reference_iterator_new(
+	git_reference_iterator **out,
+	git_repository *repo);
 
 /**
  * Create an iterator for the repo's references that match the
@@ -362,16 +364,19 @@ GIT_EXTERN(int) git_reference_iterator_new(git_reference_iterator **out, git_rep
  * @param glob the glob to match against the reference names
  * @return 0 or an error code
  */
-GIT_EXTERN(int) git_reference_iterator_glob_new(git_reference_iterator **out, git_repository *repo, const char *glob);
+GIT_EXTERN(int) git_reference_iterator_glob_new(
+	git_reference_iterator **out,
+	git_repository *repo,
+	const char *glob);
 
 /**
- * Get the next reference name
+ * Get the next reference
  *
- * @param out pointer in which to store the string
+ * @param out pointer in which to store the reference
  * @param iter the iterator
  * @param 0, GIT_ITEROVER if there are no more; or an error code
  */
-GIT_EXTERN(int) git_reference_next(const char **out, git_reference_iterator *iter);
+GIT_EXTERN(int) git_reference_next(git_reference **out, git_reference_iterator *iter);
 
 /**
  * Free the iterator and its associated resources
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index 548597f..0820cd9 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -85,7 +85,7 @@ struct git_refdb_backend {
 	 * A refdb implementation must provide this function.
 	 */
 	int (*next)(
-		const char **name,
+		git_reference **ref,
 		git_reference_iterator *iter);
 
 	/**
diff --git a/src/branch.c b/src/branch.c
index dd6dc68..84efada 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -131,28 +131,32 @@ int git_branch_foreach(
 	void *payload)
 {
 	git_reference_iterator *iter;
-	const char *name;
+	git_reference *ref;
 	int error;
 
 	if (git_reference_iterator_new(&iter, repo) < 0)
 		return -1;
 
-	while ((error = git_reference_next(&name, iter)) == 0) {
+	while ((error = git_reference_next(&ref, iter)) == 0) {
 		if (list_flags & GIT_BRANCH_LOCAL &&
-		    git__prefixcmp(name, GIT_REFS_HEADS_DIR) == 0) {
-			if (callback(name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, payload)) {
+		    git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR) == 0) {
+			if (callback(ref->name + strlen(GIT_REFS_HEADS_DIR),
+					GIT_BRANCH_LOCAL, payload)) {
 				error = GIT_EUSER;
 				break;
 			}
 		}
 
 		if (list_flags & GIT_BRANCH_REMOTE &&
-		    git__prefixcmp(name, GIT_REFS_REMOTES_DIR) == 0) {
-			if (callback(name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, payload)) {
+		    git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR) == 0) {
+			if (callback(ref->name + strlen(GIT_REFS_REMOTES_DIR),
+					GIT_BRANCH_REMOTE, payload)) {
 				error = GIT_EUSER;
 				break;
 			}
 		}
+
+		git_reference_free(ref);
 	}
 
 	if (error == GIT_ITEROVER)
@@ -160,7 +164,6 @@ int git_branch_foreach(
 
 	git_reference_iterator_free(iter);
 	return error;
-
 }
 
 int git_branch_move(
diff --git a/src/refdb.c b/src/refdb.c
index 9f9037c..6cb8792 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -160,17 +160,26 @@ int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const c
 	return 0;
 }
 
-int git_refdb_next(const char **out, git_reference_iterator *iter)
+int git_refdb_next(git_reference **out, git_reference_iterator *iter)
 {
 	int error;
 
-	if (!iter->glob)
-		return iter->backend->next(out, iter);
+	if (!iter->glob) {
+		if ((error = iter->backend->next(out, iter)) < 0)
+			return error;
+
+		(*out)->db = iter->backend;
+		return 0;
+	}
 
 	/* If the iterator has a glob, we need to filter */
 	while ((error = iter->backend->next(out, iter)) == 0) {
-		if (!p_fnmatch(iter->glob, *out, 0))
-			break;
+		if (!p_fnmatch(iter->glob, (*out)->name, 0)) {
+			(*out)->db = iter->backend;
+			return 0;
+		}
+
+		git_reference_free(*out);
 	}
 
 	return error;
diff --git a/src/refdb.h b/src/refdb.h
index 2edd05d..82522e1 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -28,7 +28,7 @@ int git_refdb_lookup(
 
 int git_refdb_iterator(git_reference_iterator **out, git_refdb *db);
 int git_refdb_iterator_glob(git_reference_iterator **out, git_refdb *db, const char *glob);
-int git_refdb_next(const char **out, git_reference_iterator *iter);
+int git_refdb_next(git_reference **out, git_reference_iterator *iter);
 void git_refdb_iterator_free(git_reference_iterator *iter);
 
 int git_refdb_write(git_refdb *refdb, const git_reference *ref);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 97f0b07..4579645 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -645,16 +645,19 @@ static int iter_load_loose_paths(refdb_fs_iter *iter)
 	return 0;
 }
 
-static int refdb_fs_backend__next(const char **out, git_reference_iterator *_iter)
+static int refdb_fs_backend__next(git_reference **out, git_reference_iterator *_iter)
 {
 	refdb_fs_iter *iter = (refdb_fs_iter *)_iter;
-	refdb_fs_backend *backend = (refdb_fs_backend *) iter->parent.backend;
+	refdb_fs_backend *backend = (refdb_fs_backend *)iter->parent.backend;
 	git_strmap *packfile = backend->refcache.packfile;
 
-	if (iter->loose_pos < iter->loose.length) {
+	while (iter->loose_pos < iter->loose.length) {
 		const char *path = git_vector_get(&iter->loose, iter->loose_pos++);
-		*out = path;
-		return 0;
+
+		if (loose_lookup(out, backend, path) == 0)
+			return 0;
+
+		giterr_clear();
 	}
 
 	if (iter->packed_pos < kh_end(packfile)) {
@@ -671,7 +674,10 @@ static int refdb_fs_backend__next(const char **out, git_reference_iterator *_ite
 			iter->packed_pos++;
 		} while (ref->flags & PACKREF_SHADOWED);
 
-		*out = ref->name;
+		*out = git_reference__alloc(ref->name, &ref->oid, &ref->peel);
+		if (*out == NULL)
+			return -1;
+
 		return 0;
 	}
 
diff --git a/src/refs.c b/src/refs.c
index 9c6c5c6..43231b0 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -664,7 +664,7 @@ int git_reference_iterator_glob_new(git_reference_iterator **out, git_repository
 	return git_refdb_iterator_glob(out, refdb, glob);
 }
 
-int git_reference_next(const char **out, git_reference_iterator *iter)
+int git_reference_next(git_reference **out, git_reference_iterator *iter)
 {
 	return git_refdb_next(out, iter);
 }
diff --git a/src/remote.c b/src/remote.c
index 7a64622..266a3d9 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1239,30 +1239,25 @@ static int update_branch_remote_config_entry(
 }
 
 static int rename_one_remote_reference(
-	git_repository *repo,
-	const char *reference_name,
+	git_reference *reference,
 	const char *old_remote_name,
 	const char *new_remote_name)
 {
 	int error = -1;
 	git_buf new_name = GIT_BUF_INIT;
-	git_reference *reference = NULL;
 	git_reference *newref = NULL;
 
 	if (git_buf_printf(
 		&new_name,
 		GIT_REFS_REMOTES_DIR "%s%s",
 		new_remote_name,
-		reference_name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
+		reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
 			return -1;
 
-	if (git_reference_lookup(&reference, repo, reference_name) < 0)
-		goto cleanup;
-
+	/* TODO: can we make this NULL? */
 	error = git_reference_rename(&newref, reference, git_buf_cstr(&new_name), 0);
 	git_reference_free(reference);
 
-cleanup:
 	git_reference_free(newref);
 	git_buf_free(&new_name);
 	return error;
@@ -1273,46 +1268,28 @@ static int rename_remote_references(
 	const char *old_name,
 	const char *new_name)
 {
-	git_vector refnames;
 	int error = -1;
-	unsigned int i;
-	char *name;
-	const char *refname;
+	git_reference *ref;
 	git_reference_iterator *iter;
 
-	if (git_vector_init(&refnames, 8, NULL) < 0)
-		return -1;
-
 	if (git_reference_iterator_new(&iter, repo) < 0)
-		goto cleanup;
+		return -1;
 
-	while ((error = git_reference_next(&refname, iter)) == 0) {
-		if (git__prefixcmp(refname, GIT_REFS_REMOTES_DIR))
+	while ((error = git_reference_next(&ref, iter)) == 0) {
+		if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR))
 			continue;
 
-		if ((error = git_vector_insert(&refnames, git__strdup(refname))) < 0)
-			break;
-
+		if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) {
+			git_reference_iterator_free(iter);
+			return error;
+		}
 	}
 
 	git_reference_iterator_free(iter);
-	if (error == GIT_ITEROVER)
-		error = 0;
-	else
-		goto cleanup;
 
-	git_vector_foreach(&refnames, i, name) {
-		if ((error = rename_one_remote_reference(repo, name, old_name, new_name)) < 0)
-			goto cleanup;
-	}
-
-	error = 0;
-cleanup:
-	git_vector_foreach(&refnames, i, name) {
-		git__free(name);
-	}
+	if (error == GIT_ITEROVER)
+		return 0;
 
-	git_vector_free(&refnames);
 	return error;
 }
 
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
index 961e18d..c89bcce 100644
--- a/tests-clar/refdb/testdb.c
+++ b/tests-clar/refdb/testdb.c
@@ -134,7 +134,7 @@ static int refdb_test_backend__iterator(git_reference_iterator **out, git_refdb_
 	return 0;
 }
 
-static int refdb_test_backend__next(const char **name, git_reference_iterator *_iter)
+static int refdb_test_backend__next(git_reference **out, git_reference_iterator *_iter)
 {
 	refdb_test_entry *entry;
 	refdb_test_backend *backend = (refdb_test_backend *) _iter->backend;
@@ -144,9 +144,15 @@ static int refdb_test_backend__next(const char **name, git_reference_iterator *_
 	if (!entry)
 		return GIT_ITEROVER;
 
-	*name = entry->name;
-	iter->i++;
+	if (entry->type == GIT_REF_OID) {
+		*out = git_reference__alloc(entry->name, &entry->target.oid, NULL);
+	} else if (entry->type == GIT_REF_SYMBOLIC) {
+		*out = git_reference__alloc_symbolic(entry->name, entry->target.symbolic);
+	} else {
+		return -1;
+	}
 
+	iter->i++;
 	return 0;
 }
 
diff --git a/tests-clar/refs/iterator.c b/tests-clar/refs/iterator.c
index d5555c6..7a96689 100644
--- a/tests-clar/refs/iterator.c
+++ b/tests-clar/refs/iterator.c
@@ -38,40 +38,43 @@ static const char *refnames[] = {
 	"refs/tags/wrapped_tag",
 };
 
+static int refcmp_cb(const void *a, const void *b)
+{
+	const git_reference *refa = (const git_reference *)a;
+	const git_reference *refb = (const git_reference *)b;
+
+	return strcmp(refa->name, refb->name);
+}
+
 void test_refs_iterator__list(void)
 {
 	git_reference_iterator *iter;
 	git_vector output;
-	char *refname;
+	git_reference *ref;
 	int error;
 	size_t i;
 
-	cl_git_pass(git_vector_init(&output, 32, git__strcmp_cb));
+	cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
 	cl_git_pass(git_reference_iterator_new(&iter, repo));
 
 	do {
-		const char *name;
-		error = git_reference_next(&name, iter);
+		error = git_reference_next(&ref, iter);
 		cl_assert(error == 0 || error == GIT_ITEROVER);
 		if (error != GIT_ITEROVER) {
-			char *dup = git__strdup(name);
-			cl_assert(dup != NULL);
-			cl_git_pass(git_vector_insert(&output, dup));
+			cl_git_pass(git_vector_insert(&output, ref));
 		}
 	} while (!error);
 
+	git_reference_iterator_free(iter);
 	cl_assert_equal_i(output.length, ARRAY_SIZE(refnames));
 
 	git_vector_sort(&output);
-	git_vector_foreach(&output, i, refname) {
-		cl_assert_equal_s(refname, refnames[i]);
-	}
-
-	git_reference_iterator_free(iter);
 
-	git_vector_foreach(&output, i, refname) {
-		git__free(refname);
+	git_vector_foreach(&output, i, ref) {
+		cl_assert_equal_s(ref->name, refnames[i]);
+		git_reference_free(ref);
 	}
+
 	git_vector_free(&output);
 }
 
@@ -79,14 +82,14 @@ void test_refs_iterator__empty(void)
 {
 	git_reference_iterator *iter;
 	git_odb *odb;
-	const char *name;
+	git_reference *ref;
 	git_repository *empty;
 
 	cl_git_pass(git_odb_new(&odb));
 	cl_git_pass(git_repository_wrap_odb(&empty, odb));
 
 	cl_git_pass(git_reference_iterator_new(&iter, empty));
-	cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&name, iter));
+	cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&ref, iter));
 
 	git_reference_iterator_free(iter);
 	git_odb_free(odb);