Reimplment git_status_foreach using git diff This is an initial reimplementation of status using diff a la the way that core git does it.
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
diff --git a/include/git2/status.h b/include/git2/status.h
index 5c45dae..4dc80b9 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -31,7 +31,8 @@ GIT_BEGIN_DECL
#define GIT_STATUS_WT_MODIFIED (1 << 4)
#define GIT_STATUS_WT_DELETED (1 << 5)
-#define GIT_STATUS_IGNORED (1 << 6)
+#define GIT_STATUS_IGNORED (1 << 6)
+#define GIT_STATUS_WT_UNTRACKED (1 << 7)
/**
* Gather file statuses and run a callback for each one.
@@ -47,6 +48,71 @@ GIT_BEGIN_DECL
GIT_EXTERN(int) git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload);
/**
+ * Select the files on which to report status.
+ *
+ * - GIT_STATUS_SHOW_INDEX_AND_WORKDIR is the default. This is the
+ * rough equivalent of `git status --porcelain` where each file
+ * will receive a callback indicating its status in the index and
+ * in the workdir.
+ * - GIT_STATUS_SHOW_INDEX_ONLY will only make callbacks for index
+ * side of status. The status of the index contents relative to
+ * the HEAD will be given.
+ * - GIT_STATUS_SHOW_WORKDIR_ONLY will only make callbacks for the
+ * workdir side of status, reporting the status of workdir content
+ * relative to the index.
+ * - GIT_STATUS_SHOW_INDEX_THEN_WORKDIR behaves like index-only
+ * followed by workdir-only, causing two callbacks to be issued
+ * per file (first index then workdir). This is slightly more
+ * efficient than making separate calls. This makes it easier to
+ * emulate the output of a plain `git status`.
+ */
+typedef enum {
+ GIT_STATUS_SHOW_INDEX_AND_WORKDIR = 0,
+ GIT_STATUS_SHOW_INDEX_ONLY = 1,
+ GIT_STATUS_SHOW_WORKDIR_ONLY = 2,
+ GIT_STATUS_SHOW_INDEX_THEN_WORKDIR = 3,
+} git_status_show_t;
+
+/**
+ * Flags to control status callbacks
+ *
+ * - GIT_STATUS_OPT_INCLUDE_UNTRACKED says that callbacks should
+ * be made on untracked files. These will only be made if the
+ * workdir files are included in the status "show" option.
+ * - GIT_STATUS_OPT_INCLUDE_IGNORED says that ignored files should
+ * get callbacks. Again, these callbacks will only be made if
+ * the workdir files are included in the status "show" option.
+ * Right now, there is no option to include all files in
+ * directories that are ignored completely.
+ * - GIT_STATUS_OPT_EXCLUDE_UNMODIFIED indicates that callback
+ * do not need to be made on unmodified files.
+ * - GIT_STATUS_OPT_EXCLUDE_SUBMODULES indicates that directories
+ * which appear to be submodules should just be skipped over.
+ */
+#define GIT_STATUS_OPT_INCLUDE_UNTRACKED (1 << 0)
+#define GIT_STATUS_OPT_INCLUDE_IGNORED (1 << 1)
+#define GIT_STATUS_OPT_EXCLUDE_UNMODIFIED (1 << 2)
+#define GIT_STATUS_OPT_EXCLUDE_SUBMODULES (1 << 3)
+
+/**
+ * Options to control which callbacks will be made by
+ * `git_status_foreach_ext()`
+ */
+typedef struct {
+ git_status_show_t show;
+ unsigned int flags;
+} git_status_options;
+
+/**
+ * Gather file status information and run callbacks as requested.
+ */
+GIT_EXTERN(int) git_status_foreach_ext(
+ git_repository *repo,
+ git_status_options *opts,
+ int (*callback)(const char *, unsigned int, void *),
+ void *payload);
+
+/**
* Get file status for a single file
*
* @param status_flags the status value
diff --git a/src/diff.c b/src/diff.c
index 69c944c..469a6c0 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -538,33 +538,22 @@ int git_diff_merge(
const git_diff_list *from)
{
int error = 0;
- unsigned int i = 0, j = 0;
git_vector onto_new;
- git_diff_delta *delta;
+ git_diff_delta *delta, *o;
+ const git_diff_delta *f;
+ unsigned int i;
if (git_vector_init(&onto_new, onto->deltas.length, diff_delta__cmp) < 0)
return -1;
- while (!error && (i < onto->deltas.length || j < from->deltas.length)) {
- git_diff_delta *o = git_vector_get(&onto->deltas, i);
- const git_diff_delta *f = git_vector_get_const(&from->deltas, j);
- const char *opath = !o ? NULL : o->old.path ? o->old.path : o->new.path;
- const char *fpath = !f ? NULL : f->old.path ? f->old.path : f->new.path;
-
- if (opath && (!fpath || strcmp(opath, fpath) < 0)) {
- delta = diff_delta__dup(o);
- i++;
- } else if (fpath && (!opath || strcmp(opath, fpath) > 0)) {
- delta = diff_delta__dup(f);
- j++;
- } else {
- delta = diff_delta__merge_like_cgit(o, f);
- i++;
- j++;
- }
-
- error = !delta ? -1 : git_vector_insert(&onto_new, delta);
- }
+ GIT_DIFF_COITERATE(
+ onto, from, o, f,
+ delta = diff_delta__dup(o),
+ delta = diff_delta__dup(f),
+ delta = diff_delta__merge_like_cgit(o, f),
+ if ((error = !delta ? -1 : git_vector_insert(&onto_new, delta)) < 0)
+ break;
+ );
if (error == 0) {
git_vector_swap(&onto->deltas, &onto_new);
@@ -577,3 +566,4 @@ int git_diff_merge(
return error;
}
+
diff --git a/src/diff.h b/src/diff.h
index 7d69199..058a1f5 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -21,5 +21,19 @@ struct git_diff_list {
git_iterator_type_t new_src;
};
+/* macro lets you iterate over two diff lists together */
+
+#define GIT_DIFF_COITERATE(A,B,AD,BD,LEFT,RIGHT,BOTH,AFTER) do { \
+ unsigned int _i = 0, _j = 0; int _cmp; \
+ while (((A) && _i < (A)->deltas.length) || ((B) && _j < (B)->deltas.length)) { \
+ (AD) = (A) ? GIT_VECTOR_GET(&(A)->deltas,_i) : NULL; \
+ (BD) = (B) ? GIT_VECTOR_GET(&(B)->deltas,_j) : NULL; \
+ _cmp = !(BD) ? -1 : !(AD) ? 1 : strcmp((AD)->old.path,(BD)->old.path); \
+ if (_cmp < 0) { LEFT; _i++; } \
+ else if (_cmp > 0) { RIGHT; _j++; } \
+ else { BOTH; _i++; _j++; } \
+ AFTER; \
+ } } while (0)
+
#endif
diff --git a/src/status.c b/src/status.c
index 2221db3..eab7c88 100644
--- a/src/status.c
+++ b/src/status.c
@@ -15,6 +15,183 @@
#include "repository.h"
#include "ignore.h"
+#include "git2/diff.h"
+#include "diff.h"
+
+static int resolve_head_to_tree(git_tree **tree, git_repository *repo)
+{
+ git_reference *head = NULL;
+ git_object *obj = NULL;
+
+ if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
+ return -1;
+
+ if (git_reference_oid(head) == NULL) {
+ git_reference *resolved;
+
+ if (git_reference_resolve(&resolved, head) < 0) {
+ /* cannot resolve HEAD - probably brand new repo */
+ giterr_clear();
+ git_reference_free(head);
+ return GIT_ENOTFOUND;
+ }
+
+ git_reference_free(head);
+ head = resolved;
+ }
+
+ if (git_object_lookup(&obj, repo, git_reference_oid(head), GIT_OBJ_ANY) < 0)
+ goto fail;
+
+ switch (git_object_type(obj)) {
+ case GIT_OBJ_TREE:
+ *tree = (git_tree *)obj;
+ break;
+ case GIT_OBJ_COMMIT:
+ if (git_commit_tree(tree, (git_commit *)obj) < 0)
+ goto fail;
+ git_object_free(obj);
+ break;
+ default:
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ git_object_free(obj);
+ git_reference_free(head);
+ return -1;
+}
+
+static unsigned int index_delta2status(git_delta_t index_status)
+{
+ unsigned int st = GIT_STATUS_CURRENT;
+
+ switch (index_status) {
+ case GIT_DELTA_ADDED:
+ case GIT_DELTA_COPIED:
+ case GIT_DELTA_RENAMED:
+ st = GIT_STATUS_INDEX_NEW;
+ break;
+ case GIT_DELTA_DELETED:
+ st = GIT_STATUS_INDEX_DELETED;
+ break;
+ case GIT_DELTA_MODIFIED:
+ st = GIT_STATUS_INDEX_MODIFIED;
+ break;
+ default:
+ break;
+ }
+
+ return st;
+}
+
+static unsigned int workdir_delta2status(git_delta_t workdir_status)
+{
+ unsigned int st = GIT_STATUS_CURRENT;
+
+ switch (workdir_status) {
+ case GIT_DELTA_ADDED:
+ case GIT_DELTA_COPIED:
+ case GIT_DELTA_RENAMED:
+ case GIT_DELTA_UNTRACKED:
+ st = GIT_STATUS_WT_NEW;
+ break;
+ case GIT_DELTA_DELETED:
+ st = GIT_STATUS_WT_DELETED;
+ break;
+ case GIT_DELTA_MODIFIED:
+ st = GIT_STATUS_WT_MODIFIED;
+ break;
+ case GIT_DELTA_IGNORED:
+ st = GIT_STATUS_IGNORED;
+ break;
+ default:
+ break;
+ }
+
+ return st;
+}
+
+int git_status_foreach_ext(
+ git_repository *repo,
+ git_status_options *opts,
+ int (*cb)(const char *, unsigned int, void *),
+ void *cbdata)
+{
+ int err = 0;
+ git_diff_options diffopt;
+ git_diff_list *idx2head = NULL, *wd2idx = NULL;
+ git_tree *head = NULL;
+ git_status_show_t show =
+ opts ? opts->show : GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+ git_diff_delta *i2h, *w2i;
+
+ assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR);
+
+ switch (resolve_head_to_tree(&head, repo)) {
+ case 0: break;
+ case GIT_ENOTFOUND: return 0;
+ default: return -1;
+ }
+
+ memset(&diffopt, 0, sizeof(diffopt));
+ diffopt.flags = GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
+
+ if (show != GIT_STATUS_SHOW_WORKDIR_ONLY &&
+ (err = git_diff_index_to_tree(repo, &diffopt, head, &idx2head)) < 0)
+ goto cleanup;
+
+ if (show != GIT_STATUS_SHOW_INDEX_ONLY &&
+ (err = git_diff_workdir_to_index(repo, &diffopt, &wd2idx)) < 0)
+ goto cleanup;
+
+ if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) {
+ git_diff_list *empty = NULL;
+ GIT_DIFF_COITERATE(
+ idx2head, empty, i2h, w2i,
+ err = cb(i2h->old.path, index_delta2status(i2h->status), cbdata),
+ /* nothing */, /* nothing */, if (err < 0) break);
+
+ git_diff_list_free(idx2head);
+ idx2head = NULL;
+ }
+
+ GIT_DIFF_COITERATE(
+ idx2head, wd2idx, i2h, w2i,
+ err = cb(i2h->old.path, index_delta2status(i2h->status), cbdata),
+ err = cb(w2i->old.path, workdir_delta2status(w2i->status), cbdata),
+ err = cb(i2h->old.path, index_delta2status(i2h->status) |
+ workdir_delta2status(w2i->status), cbdata),
+ if (err < 0) break);
+
+cleanup:
+ git_tree_free(head);
+ git_diff_list_free(idx2head);
+ git_diff_list_free(wd2idx);
+ return err;
+}
+
+int git_status_foreach(
+ git_repository *repo,
+ int (*callback)(const char *, unsigned int, void *),
+ void *payload)
+{
+ git_status_options opts;
+
+ opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+ opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
+ GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
+
+ return git_status_foreach_ext(repo, &opts, callback, payload);
+}
+
+
+/*
+ * the old stuff
+ */
+
struct status_entry {
git_index_time mtime;
@@ -461,7 +638,7 @@ static int status_cmp(const void *a, const void *b)
#define DEFAULT_SIZE 16
-int git_status_foreach(
+int git_status_foreach_old(
git_repository *repo,
int (*callback)(const char *, unsigned int, void *),
void *payload)
diff --git a/src/vector.h b/src/vector.h
index 180edbf..5bc2791 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -44,6 +44,8 @@ GIT_INLINE(const void *) git_vector_get_const(const git_vector *v, unsigned int
return (position < v->length) ? v->contents[position] : NULL;
}
+#define GIT_VECTOR_GET(V,I) ((I) < (V)->length ? (V)->contents[(I)] : NULL)
+
GIT_INLINE(void *) git_vector_last(git_vector *v)
{
return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL;