Commit 1ee2ef87ec4c2c63b7c89849eb9daad9e46e6fe7

Edward Thomson 2013-05-21T11:05:21

status access by index, providing more details to callers

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
diff --git a/include/git2/status.h b/include/git2/status.h
index 38b6fa5..ce1d44a 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -174,10 +174,73 @@ typedef struct {
 	git_strarray      pathspec;
 } git_status_options;
 
+/**
+ * A status entry, providing the differences between the file as it exists
+ * in HEAD and the index, and providing the differences between the index
+ * and the working directory.
+ *
+ * The `status` value provides the status flags for this file.
+ *
+ * The `head_to_index` value provides detailed information about the
+ * differences between the file in HEAD and the file in the index.
+ *
+ * The `index_to_workdir` value provides detailed information about the
+ * differences between the file in the index and the file in the
+ * working directory.
+ */
+typedef struct {
+	git_status_t status;
+	git_diff_delta *head_to_index;
+	git_diff_delta *index_to_workdir;
+} git_status_entry;
+
 #define GIT_STATUS_OPTIONS_VERSION 1
 #define GIT_STATUS_OPTIONS_INIT {GIT_STATUS_OPTIONS_VERSION}
 
 /**
+ * Gather file status information and populate the `git_status_list`.
+ *
+ * @param out Pointer to store the status results in
+ * @param repo Repository object
+ * @param opts Status options structure
+ * @return 0 on success or error code
+ */
+GIT_EXTERN(int) git_status_list_new(
+	git_status_list **out,
+	git_repository *repo,
+	const git_status_options *opts);
+
+/**
+ * Gets the count of status entries in this list.
+ *
+ * @param statuslist Existing status list object
+ * @return the number of status entries
+ */
+GIT_EXTERN(size_t) git_status_list_entrycount(
+	git_status_list *statuslist);
+
+/**
+ * Get a pointer to one of the entries in the status list.
+ *
+ * The entry is not modifiable and should not be freed.
+ *
+ * @param statuslist Existing status list object
+ * @param idx Position of the entry
+ * @return Pointer to the entry; NULL if out of bounds
+ */
+GIT_EXTERN(const git_status_entry *) git_status_byindex(
+	git_status_list *statuslist,
+	size_t idx);
+
+/**
+ * Free an existing status list
+ *
+ * @param statuslist Existing status list object
+ */
+GIT_EXTERN(void) git_status_list_free(
+	git_status_list *statuslist);
+
+/**
  * Gather file status information and run callbacks as requested.
  *
  * This is an extended version of the `git_status_foreach()` API that
diff --git a/include/git2/types.h b/include/git2/types.h
index 1bfa73b..dc34407 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -174,6 +174,9 @@ typedef struct git_reference_iterator  git_reference_iterator;
 /** Merge heads, the input to merge */
 typedef struct git_merge_head git_merge_head;
 
+/** Representation of a status collection */
+typedef struct git_status_list git_status_list;
+
 
 /** Basic type of any Git reference. */
 typedef enum {
diff --git a/src/status.c b/src/status.c
index 712e0d5..33e67ef 100644
--- a/src/status.c
+++ b/src/status.c
@@ -11,6 +11,7 @@
 #include "hash.h"
 #include "vector.h"
 #include "tree.h"
+#include "status.h"
 #include "git2/status.h"
 #include "repository.h"
 #include "ignore.h"
@@ -77,71 +78,105 @@ static unsigned int workdir_delta2status(git_delta_t workdir_status)
 	return st;
 }
 
-typedef struct {
-	git_status_cb cb;
-	void *payload;
-	const git_status_options *opts;
-} status_user_callback;
+static bool status_is_included(
+	git_status_list *statuslist,
+	git_diff_delta *head2idx,
+	git_diff_delta *idx2wd)
+{
+	/* if excluding submodules and this is a submodule everywhere */
+	if ((statuslist->opts.flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0) {
+		bool in_tree  = (head2idx && head2idx->status != GIT_DELTA_ADDED);
+		bool in_index = (head2idx && head2idx->status != GIT_DELTA_DELETED);
+		bool in_wd    = (idx2wd && idx2wd->status != GIT_DELTA_DELETED);
+
+		if ((!in_tree || head2idx->old_file.mode == GIT_FILEMODE_COMMIT) &&
+			(!in_index || head2idx->new_file.mode == GIT_FILEMODE_COMMIT) &&
+			(!in_wd || idx2wd->new_file.mode == GIT_FILEMODE_COMMIT))
+			return 0;
+	}
 
-static int status_invoke_cb(
-	git_diff_delta *h2i, git_diff_delta *i2w, void *payload)
+	return 1;
+}
+
+static git_status_t status_compute(
+	git_diff_delta *head2idx,
+	git_diff_delta *idx2wd)
 {
-	status_user_callback *usercb = payload;
-	const char *path = NULL;
-	unsigned int status = 0;
+	git_status_t status = 0;
 
-	if (i2w) {
-		path = i2w->old_file.path;
-		status |= workdir_delta2status(i2w->status);
-	}
-	if (h2i) {
-		path = h2i->old_file.path;
-		status |= index_delta2status(h2i->status);
-	}
+	if (head2idx)
+		status |= index_delta2status(head2idx->status);
 
-	/* if excluding submodules and this is a submodule everywhere */
-	if (usercb->opts &&
-		(usercb->opts->flags & GIT_STATUS_OPT_EXCLUDE_SUBMODULES) != 0)
-	{
-		bool in_tree  = (h2i && h2i->status != GIT_DELTA_ADDED);
-		bool in_index = (h2i && h2i->status != GIT_DELTA_DELETED);
-		bool in_wd    = (i2w && i2w->status != GIT_DELTA_DELETED);
+	if (idx2wd)
+		status |= workdir_delta2status(idx2wd->status);
 
-		if ((!in_tree || h2i->old_file.mode == GIT_FILEMODE_COMMIT) &&
-			(!in_index || h2i->new_file.mode == GIT_FILEMODE_COMMIT) &&
-			(!in_wd || i2w->new_file.mode == GIT_FILEMODE_COMMIT))
-			return 0;
-	}
+	return status;
+}
+
+static int status_collect(
+	git_diff_delta *head2idx,
+	git_diff_delta *idx2wd,
+	void *payload)
+{
+	git_status_list *statuslist = payload;
+	git_status_entry *status_entry;
+	
+	if (!status_is_included(statuslist, head2idx, idx2wd))
+		return 0;
+	
+	status_entry = git__malloc(sizeof(git_status_entry));
+	GITERR_CHECK_ALLOC(status_entry);
+
+	status_entry->status = status_compute(head2idx, idx2wd);
+	status_entry->head_to_index = head2idx;
+	status_entry->index_to_workdir = idx2wd;
+
+	git_vector_insert(&statuslist->paired, status_entry);
 
-	return usercb->cb(path, status, usercb->payload);
+	return 0;
 }
 
-int git_status_foreach_ext(
+git_status_list *git_status_list_alloc(void)
+{
+	git_status_list *statuslist = NULL;
+
+	if ((statuslist = git__calloc(1, sizeof(git_status_list))) == NULL ||
+		git_vector_init(&statuslist->paired, 0, NULL) < 0)
+		return NULL;
+
+	return statuslist;
+}
+
+int git_status_list_new(
+	git_status_list **out,
 	git_repository *repo,
-	const git_status_options *opts,
-	git_status_cb cb,
-	void *payload)
+	const git_status_options *opts)
 {
-	int err = 0;
+	git_status_list *statuslist = NULL;
 	git_diff_options diffopt = GIT_DIFF_OPTIONS_INIT;
-	git_diff_list *head2idx = NULL, *idx2wd = NULL;
 	git_tree *head = NULL;
 	git_status_show_t show =
 		opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
-	status_user_callback usercb;
+	int error = 0;
 
 	assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR);
 
+	*out = NULL;
+
 	GITERR_CHECK_VERSION(opts, GIT_STATUS_OPTIONS_VERSION, "git_status_options");
 
-	if (show != GIT_STATUS_SHOW_INDEX_ONLY &&
-		(err = git_repository__ensure_not_bare(repo, "status")) < 0)
-		return err;
+	if ((error = git_repository__ensure_not_bare(repo, "status")) < 0)
+		return error;
 
 	/* if there is no HEAD, that's okay - we'll make an empty iterator */
-	if (((err = git_repository_head_tree(&head, repo)) < 0) &&
-		!(err == GIT_ENOTFOUND || err == GIT_EORPHANEDHEAD))
-		return err;
+	if (((error = git_repository_head_tree(&head, repo)) < 0) &&
+		!(error == GIT_ENOTFOUND || error == GIT_EORPHANEDHEAD))
+		return error;
+
+	statuslist = git_status_list_alloc();
+	GITERR_CHECK_ALLOC(statuslist);
+
+	memcpy(&statuslist->opts, opts, sizeof(git_status_options));
 
 	memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec));
 
@@ -163,41 +198,106 @@ int git_status_foreach_ext(
 		diffopt.flags = diffopt.flags | GIT_DIFF_IGNORE_SUBMODULES;
 
 	if (show != GIT_STATUS_SHOW_WORKDIR_ONLY) {
-		err = git_diff_tree_to_index(&head2idx, repo, head, NULL, &diffopt);
-		if (err < 0)
-			goto cleanup;
+		error = git_diff_tree_to_index(&statuslist->head2idx, repo, head, NULL, &diffopt);
+
+		if (error < 0)
+			goto on_error;
 	}
 
 	if (show != GIT_STATUS_SHOW_INDEX_ONLY) {
-		err = git_diff_index_to_workdir(&idx2wd, repo, NULL, &diffopt);
-		if (err < 0)
-			goto cleanup;
-	}
+		error = git_diff_index_to_workdir(&statuslist->idx2wd, repo, NULL, &diffopt);
 
-	usercb.cb = cb;
-	usercb.payload = payload;
-	usercb.opts = opts;
+		if (error < 0)
+			goto on_error;
+	}
 
 	if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) {
-		if ((err = git_diff__paired_foreach(
-				 head2idx, NULL, status_invoke_cb, &usercb)) < 0)
-			goto cleanup;
+		if ((error = git_diff__paired_foreach(statuslist->head2idx, NULL, status_collect, statuslist)) < 0)
+			goto on_error;
 
-		git_diff_list_free(head2idx);
-		head2idx = NULL;
+		git_diff_list_free(statuslist->head2idx);
+		statuslist->head2idx = NULL;
 	}
 
-	err = git_diff__paired_foreach(head2idx, idx2wd, status_invoke_cb, &usercb);
+	if ((error = git_diff__paired_foreach(statuslist->head2idx, statuslist->idx2wd, status_collect, statuslist)) < 0)
+		goto on_error;
+
+	*out = statuslist;
+	goto done;
+
+on_error:
+	git_status_list_free(statuslist);
 
-cleanup:
+done:
 	git_tree_free(head);
-	git_diff_list_free(head2idx);
-	git_diff_list_free(idx2wd);
 
-	if (err == GIT_EUSER)
-		giterr_clear();
+	return error;
+}
+
+size_t git_status_list_entrycount(git_status_list *statuslist)
+{
+	assert(statuslist);
+
+	return statuslist->paired.length;
+}
+
+const git_status_entry *git_status_byindex(
+	git_status_list *statuslist, 
+	size_t i)
+{
+	assert(statuslist);
+
+	return git_vector_get(&statuslist->paired, i);
+}
+
+void git_status_list_free(git_status_list *statuslist)
+{
+	git_status_entry *status_entry;
+	size_t i;
+
+	if (statuslist == NULL)
+		return;
+
+	git_diff_list_free(statuslist->head2idx);
+	git_diff_list_free(statuslist->idx2wd);
+
+	git_vector_foreach(&statuslist->paired, i, status_entry)
+		git__free(status_entry);
 
-	return err;
+	git_vector_free(&statuslist->paired);
+
+	git__free(statuslist);
+}
+
+int git_status_foreach_ext(
+	git_repository *repo,
+	const git_status_options *opts,
+	git_status_cb cb,
+	void *payload)
+{
+	git_status_list *statuslist;
+	const git_status_entry *status_entry;
+	size_t i;
+	int error = 0;
+
+	if ((error = git_status_list_new(&statuslist, repo, opts)) < 0)
+		return error;
+
+	git_vector_foreach(&statuslist->paired, i, status_entry) {
+		const char *path = status_entry->head_to_index ?
+			status_entry->head_to_index->old_file.path :
+			status_entry->index_to_workdir->old_file.path;
+
+		if (cb(path, status_entry->status, payload) != 0) {
+			error = GIT_EUSER;
+			giterr_clear();
+			break;
+		}
+	}
+
+	git_status_list_free(statuslist);
+
+	return error;
 }
 
 int git_status_foreach(
diff --git a/src/status.h b/src/status.h
new file mode 100644
index 0000000..b58e0eb
--- /dev/null
+++ b/src/status.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_status_h__
+#define INCLUDE_status_h__
+
+#include "diff.h"
+#include "git2/status.h"
+#include "git2/diff.h"
+
+struct git_status_list {
+	git_status_options opts;
+
+	git_diff_list *head2idx;
+	git_diff_list *idx2wd;
+
+	git_vector paired;
+};
+
+#endif