ext/rugged/vendor/libgit2-dist/src/status.c in rugged-0.16.0 vs ext/rugged/vendor/libgit2-dist/src/status.c in rugged-0.17.0b1

- old
+ new

@@ -1,7 +1,7 @@ /* - * Copyright (C) 2009-2011 the libgit2 contributors + * Copyright (C) 2009-2012 the libgit2 contributors * * This file is part of libgit2, distributed under the GNU GPL v2 with * a Linking Exception. For full terms see the included COPYING file. */ @@ -11,686 +11,232 @@ #include "hash.h" #include "vector.h" #include "tree.h" #include "git2/status.h" #include "repository.h" +#include "ignore.h" -struct status_entry { - git_index_time mtime; +#include "git2/diff.h" +#include "diff.h" - git_oid head_oid; - git_oid index_oid; - git_oid wt_oid; - - unsigned int status_flags:6; - - char path[GIT_FLEX_ARRAY]; /* more */ -}; - -static struct status_entry *status_entry_new(git_vector *entries, const char *path) +static unsigned int index_delta2status(git_delta_t index_status) { - struct status_entry *e = git__malloc(sizeof(*e) + strlen(path) + 1); - if (e == NULL) - return NULL; + unsigned int st = GIT_STATUS_CURRENT; - memset(e, 0x0, sizeof(*e)); + 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; + } - if (entries != NULL) - git_vector_insert(entries, e); - - strcpy(e->path, path); - - return e; + return st; } -GIT_INLINE(void) status_entry_update_from_tree_entry(struct status_entry *e, const git_tree_entry *tree_entry) +static unsigned int workdir_delta2status(git_delta_t workdir_status) { - assert(e && tree_entry); + unsigned int st = GIT_STATUS_CURRENT; - git_oid_cpy(&e->head_oid, &tree_entry->oid); -} + 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; + } -GIT_INLINE(void) status_entry_update_from_index_entry(struct status_entry *e, const git_index_entry *index_entry) -{ - assert(e && index_entry); - - git_oid_cpy(&e->index_oid, &index_entry->oid); - e->mtime = index_entry->mtime; + return st; } -static void status_entry_update_from_index(struct status_entry *e, git_index *index) +int git_status_foreach_ext( + git_repository *repo, + const git_status_options *opts, + int (*cb)(const char *, unsigned int, void *), + void *cbdata) { - int idx; - git_index_entry *index_entry; + int err = 0, cmp; + 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; + unsigned int i, j, i_max, j_max; - assert(e && index); + assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR); - idx = git_index_find(index, e->path); - if (idx < 0) - return; + if ((err = git_repository_head_tree(&head, repo)) < 0) + return err; - index_entry = git_index_get(index, idx); + memset(&diffopt, 0, sizeof(diffopt)); + memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec)); - status_entry_update_from_index_entry(e, index_entry); -} + if ((opts->flags & GIT_STATUS_OPT_INCLUDE_UNTRACKED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNTRACKED; + if ((opts->flags & GIT_STATUS_OPT_INCLUDE_IGNORED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_IGNORED; + if ((opts->flags & GIT_STATUS_OPT_INCLUDE_UNMODIFIED) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_INCLUDE_UNMODIFIED; + if ((opts->flags & GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS) != 0) + diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS; + /* TODO: support EXCLUDE_SUBMODULES flag */ -static int status_entry_update_from_workdir(struct status_entry *e, char* full_path) -{ - struct stat filest; + if (show != GIT_STATUS_SHOW_WORKDIR_ONLY && + (err = git_diff_index_to_tree(repo, &diffopt, head, &idx2head)) < 0) + goto cleanup; - if (p_stat(full_path, &filest) < GIT_SUCCESS) - return git__throw(GIT_EOSERR, "Failed to determine status of file '%s'. Can't read file", full_path); + if (show != GIT_STATUS_SHOW_INDEX_ONLY && + (err = git_diff_workdir_to_index(repo, &diffopt, &wd2idx)) < 0) + goto cleanup; - if (e->mtime.seconds == (git_time_t)filest.st_mtime) - git_oid_cpy(&e->wt_oid, &e->index_oid); - else - git_odb_hashfile(&e->wt_oid, full_path, GIT_OBJ_BLOB); - - return GIT_SUCCESS; -} - -static int status_entry_update_flags(struct status_entry *e) -{ - git_oid zero; - int head_zero, index_zero, wt_zero; - - memset(&zero, 0x0, sizeof(git_oid)); - - head_zero = git_oid_cmp(&zero, &e->head_oid); - index_zero = git_oid_cmp(&zero, &e->index_oid); - wt_zero = git_oid_cmp(&zero, &e->wt_oid); - - if (head_zero == 0 && index_zero == 0 && wt_zero == 0) - return GIT_ENOTFOUND; - - if (head_zero == 0 && index_zero != 0) - e->status_flags |= GIT_STATUS_INDEX_NEW; - else if (index_zero == 0 && head_zero != 0) - e->status_flags |= GIT_STATUS_INDEX_DELETED; - else if (git_oid_cmp(&e->head_oid, &e->index_oid) != 0) - e->status_flags |= GIT_STATUS_INDEX_MODIFIED; - - if (index_zero == 0 && wt_zero != 0) - e->status_flags |= GIT_STATUS_WT_NEW; - else if (wt_zero == 0 && index_zero != 0) - e->status_flags |= GIT_STATUS_WT_DELETED; - else if (git_oid_cmp(&e->index_oid, &e->wt_oid) != 0) - e->status_flags |= GIT_STATUS_WT_MODIFIED; - - return GIT_SUCCESS; -} - -struct status_st { - git_vector *vector; - git_index *index; - git_tree *tree; - - int workdir_path_len; - char* head_tree_relative_path; - int head_tree_relative_path_len; - unsigned int tree_position; - unsigned int index_position; - int is_dir:1; -}; - -static int retrieve_head_tree(git_tree **tree_out, git_repository *repo) -{ - git_reference *resolved_head_ref; - git_commit *head_commit = NULL; - git_tree *tree; - int error = GIT_SUCCESS; - - *tree_out = NULL; - - error = git_repository_head(&resolved_head_ref, repo); - /* - * We assume that a situation where HEAD exists but can not be resolved is valid. - * A new repository fits this description for instance. - */ - if (error == GIT_ENOTFOUND) - return GIT_SUCCESS; - if (error < GIT_SUCCESS) - return git__rethrow(error, "HEAD can't be resolved"); - - if ((error = git_commit_lookup(&head_commit, repo, git_reference_oid(resolved_head_ref))) < GIT_SUCCESS) - return git__rethrow(error, "The tip of HEAD can't be retrieved"); - - if ((error = git_commit_tree(&tree, head_commit)) < GIT_SUCCESS) { - error = git__rethrow(error, "The tree of HEAD can't be retrieved"); - goto exit; - } - - *tree_out = tree; - -exit: - git_commit_free(head_commit); - return error; -} - -enum path_type { - GIT_STATUS_PATH_NULL, - GIT_STATUS_PATH_IGNORE, - GIT_STATUS_PATH_FILE, - GIT_STATUS_PATH_FOLDER, -}; - -static int dirent_cb(void *state, char *full_path); -static int alphasorted_futils_direach( - char *path, size_t path_sz, - int (*fn)(void *, char *), void *arg); - -static int process_folder(struct status_st *st, const git_tree_entry *tree_entry, char *full_path, enum path_type path_type) -{ - git_object *subtree = NULL; - git_tree *pushed_tree = NULL; - int error, pushed_tree_position = 0; - git_otype tree_entry_type = GIT_OBJ_BAD; - - if (tree_entry != NULL) { - tree_entry_type = git_tree_entry_type(tree_entry); - - switch (tree_entry_type) { - case GIT_OBJ_TREE: - error = git_tree_entry_2object(&subtree, ((git_object *)(st->tree))->repo, tree_entry); - pushed_tree = st->tree; - pushed_tree_position = st->tree_position; - st->tree = (git_tree *)subtree; - st->tree_position = 0; - st->head_tree_relative_path_len += 1 + tree_entry->filename_len; /* path + '/' + name */ - break; - - case GIT_OBJ_BLOB: - /* No op */ - break; - - default: - error = git__throw(GIT_EINVALIDTYPE, "Unexpected tree entry type"); /* TODO: How should we deal with submodules? */ + if (show == GIT_STATUS_SHOW_INDEX_THEN_WORKDIR) { + for (i = 0; !err && i < idx2head->deltas.length; i++) { + i2h = GIT_VECTOR_GET(&idx2head->deltas, i); + err = cb(i2h->old_file.path, index_delta2status(i2h->status), cbdata); } + git_diff_list_free(idx2head); + idx2head = NULL; } - if (full_path != NULL && path_type == GIT_STATUS_PATH_FOLDER) - error = alphasorted_futils_direach(full_path, GIT_PATH_MAX, dirent_cb, st); - else { - error = dirent_cb(st, NULL); - } + i_max = idx2head ? idx2head->deltas.length : 0; + j_max = wd2idx ? wd2idx->deltas.length : 0; - if (tree_entry_type == GIT_OBJ_TREE) { - git_object_free(subtree); - st->head_tree_relative_path_len -= 1 + tree_entry->filename_len; - st->tree = pushed_tree; - st->tree_position = pushed_tree_position; - st->tree_position++; - } + for (i = 0, j = 0; !err && (i < i_max || j < j_max); ) { + i2h = idx2head ? GIT_VECTOR_GET(&idx2head->deltas,i) : NULL; + w2i = wd2idx ? GIT_VECTOR_GET(&wd2idx->deltas,j) : NULL; - return error; -} + cmp = !w2i ? -1 : !i2h ? 1 : strcmp(i2h->old_file.path, w2i->old_file.path); -static int store_if_changed(struct status_st *st, struct status_entry *e) -{ - int error; - if ((error = status_entry_update_flags(e)) < GIT_SUCCESS) - return git__throw(error, "Failed to process the file '%s'. It doesn't exist in the workdir, in the HEAD nor in the index", e->path); - - if (e->status_flags == GIT_STATUS_CURRENT) { - git__free(e); - return GIT_SUCCESS; - } - - return git_vector_insert(st->vector, e); -} - -static int determine_status(struct status_st *st, - int in_head, int in_index, int in_workdir, - const git_tree_entry *tree_entry, - const git_index_entry *index_entry, - char *full_path, - const char *status_path, - enum path_type path_type) -{ - struct status_entry *e; - int error = GIT_SUCCESS; - git_otype tree_entry_type = GIT_OBJ_BAD; - - if (tree_entry != NULL) - tree_entry_type = git_tree_entry_type(tree_entry); - - /* If we're dealing with a directory in the workdir, let's recursively tackle it first */ - if (path_type == GIT_STATUS_PATH_FOLDER) - return process_folder(st, tree_entry, full_path, path_type); - - /* Are we dealing with a file somewhere? */ - if (in_workdir || in_index || (in_head && tree_entry_type == GIT_OBJ_BLOB)) { - e = status_entry_new(NULL, status_path); - - if (in_head && tree_entry_type == GIT_OBJ_BLOB) { - status_entry_update_from_tree_entry(e, tree_entry); - st->tree_position++; + if (cmp < 0) { + err = cb(i2h->old_file.path, index_delta2status(i2h->status), cbdata); + i++; + } else if (cmp > 0) { + err = cb(w2i->old_file.path, workdir_delta2status(w2i->status), cbdata); + j++; + } else { + err = cb(i2h->old_file.path, index_delta2status(i2h->status) | + workdir_delta2status(w2i->status), cbdata); + i++; j++; } - - if (in_index) { - status_entry_update_from_index_entry(e, index_entry); - st->index_position++; - } - - if (in_workdir) - if ((error = status_entry_update_from_workdir(e, full_path)) < GIT_SUCCESS) - return error; /* The callee has already set the error message */ - - return store_if_changed(st, e); } - /* Last option, we're dealing with a leftover folder tree entry */ - assert(in_head && !in_index && !in_workdir && (tree_entry_type == GIT_OBJ_TREE)); - return process_folder(st, tree_entry, full_path, path_type); +cleanup: + git_tree_free(head); + git_diff_list_free(idx2head); + git_diff_list_free(wd2idx); + return err; } -static int path_type_from(char *full_path, int is_dir) +int git_status_foreach( + git_repository *repo, + int (*callback)(const char *, unsigned int, void *), + void *payload) { - if (full_path == NULL) - return GIT_STATUS_PATH_NULL; + git_status_options opts; - if (!is_dir) - return GIT_STATUS_PATH_FILE; + memset(&opts, 0, sizeof(opts)); + opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED | + GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; - if (!git__suffixcmp(full_path, "/" DOT_GIT "/")) - return GIT_STATUS_PATH_IGNORE; - - return GIT_STATUS_PATH_FOLDER; + return git_status_foreach_ext(repo, &opts, callback, payload); } -static const char *status_path(const char *first, const char *second, const char *third) -{ - /* At least one of them can not be NULL */ - assert(first != NULL || second != NULL || third != NULL); +struct status_file_info { + unsigned int count; + unsigned int status; + char *expected; +}; - /* TODO: Fixme. Ensure that when non null, they're all equal */ - if (first != NULL) - return first; - - if (second != NULL) - return second; - - return third; -} - -static int compare(const char *left, const char *right) +static int get_one_status(const char *path, unsigned int status, void *data) { - if (left == NULL && right == NULL) - return 0; + struct status_file_info *sfi = data; - if (left == NULL) - return 1; + sfi->count++; + sfi->status = status; - if (right == NULL) + if (sfi->count > 1 || strcmp(sfi->expected, path) != 0) { + giterr_set(GITERR_INVALID, + "Ambiguous path '%s' given to git_status_file", sfi->expected); return -1; - - return strcmp(left, right); -} - -/* Greatly inspired from JGit IndexTreeWalker */ -/* https://github.com/spearce/jgit/blob/ed47e29c777accfa78c6f50685a5df2b8f5b8ff5/org.spearce.jgit/src/org/spearce/jgit/lib/IndexTreeWalker.java#L88 */ - -static int dirent_cb(void *state, char *a) -{ - const git_tree_entry *m; - const git_index_entry *entry; - enum path_type path_type; - int cmpma, cmpmi, cmpai, error; - const char *pm, *pa, *pi; - const char *m_name, *i_name, *a_name; - - struct status_st *st = (struct status_st *)state; - - path_type = path_type_from(a, st->is_dir); - - if (path_type == GIT_STATUS_PATH_IGNORE) - return GIT_SUCCESS; /* Let's skip the ".git" directory */ - - a_name = (path_type != GIT_STATUS_PATH_NULL) ? a + st->workdir_path_len : NULL; - - while (1) { - if (st->tree == NULL) - m = NULL; - else - m = git_tree_entry_byindex(st->tree, st->tree_position); - - entry = git_index_get(st->index, st->index_position); - - if ((m == NULL) && (a == NULL) && (entry == NULL)) - return GIT_SUCCESS; - - if (m != NULL) { - st->head_tree_relative_path[st->head_tree_relative_path_len] = '\0'; - - /* When the tree entry is a folder, append a forward slash to its name */ - if (git_tree_entry_type(m) == GIT_OBJ_TREE) - git_path_join_n(st->head_tree_relative_path, 3, st->head_tree_relative_path, m->filename, ""); - else - git_path_join(st->head_tree_relative_path, st->head_tree_relative_path, m->filename); - - m_name = st->head_tree_relative_path; - } else - m_name = NULL; - - i_name = (entry != NULL) ? entry->path : NULL; - - cmpma = compare(m_name, a_name); - cmpmi = compare(m_name, i_name); - cmpai = compare(a_name, i_name); - - pm = ((cmpma <= 0) && (cmpmi <= 0)) ? m_name : NULL; - pa = ((cmpma >= 0) && (cmpai <= 0)) ? a_name : NULL; - pi = ((cmpmi >= 0) && (cmpai >= 0)) ? i_name : NULL; - - if((error = determine_status(st, pm != NULL, pi != NULL, pa != NULL, m, entry, a, status_path(pm, pi, pa), path_type)) < GIT_SUCCESS) - return git__rethrow(error, "An error occured while determining the status of '%s'", a); - - if ((pa != NULL) || (path_type == GIT_STATUS_PATH_FOLDER)) - return GIT_SUCCESS; } -} -static int status_cmp(const void *a, const void *b) -{ - const struct status_entry *entry_a = (const struct status_entry *)(a); - const struct status_entry *entry_b = (const struct status_entry *)(b); - - return strcmp(entry_a->path, entry_b->path); + return 0; } -#define DEFAULT_SIZE 16 - -int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsigned int, void *), void *payload) +int git_status_file( + unsigned int *status_flags, + git_repository *repo, + const char *path) { - git_vector entries; - git_index *index = NULL; - char temp_path[GIT_PATH_MAX]; - char tree_path[GIT_PATH_MAX] = ""; - struct status_st dirent_st; - int error = GIT_SUCCESS; - unsigned int i; - git_tree *tree; - struct status_entry *e; - const char *workdir; + int error; + git_status_options opts; + struct status_file_info sfi; - if ((workdir = git_repository_workdir(repo)) == NULL) - return git__throw(GIT_ERROR, - "Cannot retrieve status on a bare repository"); - - if ((error = git_repository_index__weakptr(&index, repo)) < GIT_SUCCESS) { - return git__rethrow(error, - "Failed to determine statuses. Index can't be opened"); - } - - if ((error = retrieve_head_tree(&tree, repo)) < GIT_SUCCESS) { - error = git__rethrow(error, "Failed to determine statuses"); - goto exit; - } - - git_vector_init(&entries, DEFAULT_SIZE, status_cmp); - - dirent_st.workdir_path_len = strlen(workdir); - dirent_st.tree_position = 0; - dirent_st.index_position = 0; - dirent_st.tree = tree; - dirent_st.index = index; - dirent_st.vector = &entries; - dirent_st.head_tree_relative_path = tree_path; - dirent_st.head_tree_relative_path_len = 0; - dirent_st.is_dir = 1; - - strcpy(temp_path, workdir); - - if (git_futils_isdir(temp_path)) { - error = git__throw(GIT_EINVALIDPATH, - "Failed to determine status of file '%s'. " - "The given path doesn't lead to a folder", temp_path); - goto exit; - } - - error = alphasorted_futils_direach( - temp_path, sizeof(temp_path), - dirent_cb, &dirent_st - ); - - if (error < GIT_SUCCESS) - error = git__rethrow(error, - "Failed to determine statuses. " - "An error occured while processing the working directory"); - - if ((error == GIT_SUCCESS) && ((error = dirent_cb(&dirent_st, NULL)) < GIT_SUCCESS)) - error = git__rethrow(error, - "Failed to determine statuses. " - "An error occured while post-processing the HEAD tree and the index"); - - for (i = 0; i < entries.length; ++i) { - e = (struct status_entry *)git_vector_get(&entries, i); - - if (error == GIT_SUCCESS) { - error = callback(e->path, e->status_flags, payload); - if (error < GIT_SUCCESS) - error = git__rethrow(error, - "Failed to determine statuses. User callback failed"); - } - - git__free(e); - } - -exit: - git_vector_free(&entries); - git_tree_free(tree); - return error; -} - -static int recurse_tree_entry(git_tree *tree, struct status_entry *e, const char *path) -{ - char *dir_sep; - const git_tree_entry *tree_entry; - git_tree *subtree; - int error = GIT_SUCCESS; - - dir_sep = strchr(path, '/'); - if (!dir_sep) { - tree_entry = git_tree_entry_byname(tree, path); - if (tree_entry == NULL) - return GIT_SUCCESS; /* The leaf doesn't exist in the tree*/ - - status_entry_update_from_tree_entry(e, tree_entry); - return GIT_SUCCESS; - } - - /* Retrieve subtree name */ - *dir_sep = '\0'; - - tree_entry = git_tree_entry_byname(tree, path); - if (tree_entry == NULL) - return GIT_SUCCESS; /* The subtree doesn't exist in the tree*/ - - *dir_sep = '/'; - - /* Retreive subtree */ - if ((error = git_tree_lookup(&subtree, tree->object.repo, &tree_entry->oid)) < GIT_SUCCESS) - return git__throw(GIT_EOBJCORRUPTED, "Can't find tree object '%s'", tree_entry->filename); - - error = recurse_tree_entry(subtree, e, dir_sep+1); - git_tree_free(subtree); - return error; -} - -int git_status_file(unsigned int *status_flags, git_repository *repo, const char *path) -{ - struct status_entry *e; - git_index *index = NULL; - char temp_path[GIT_PATH_MAX]; - int error = GIT_SUCCESS; - git_tree *tree = NULL; - const char *workdir; - assert(status_flags && repo && path); - if ((workdir = git_repository_workdir(repo)) == NULL) - return git__throw(GIT_ERROR, - "Cannot retrieve status on a bare repository"); + memset(&sfi, 0, sizeof(sfi)); + if ((sfi.expected = git__strdup(path)) == NULL) + return -1; - git_path_join(temp_path, workdir, path); - if (git_futils_isdir(temp_path) == GIT_SUCCESS) - return git__throw(GIT_EINVALIDPATH, - "Failed to determine status of file '%s'. " - "Given path leads to a folder, not a file", path); + memset(&opts, 0, sizeof(opts)); + opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; + opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED | + GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS | + GIT_STATUS_OPT_INCLUDE_UNMODIFIED; + opts.pathspec.count = 1; + opts.pathspec.strings = &sfi.expected; - e = status_entry_new(NULL, path); - if (e == NULL) - return GIT_ENOMEM; + error = git_status_foreach_ext(repo, &opts, get_one_status, &sfi); - /* Find file in Workdir */ - if (git_futils_exists(temp_path) == GIT_SUCCESS) { - if ((error = status_entry_update_from_workdir(e, temp_path)) < GIT_SUCCESS) - goto exit; /* The callee has already set the error message */ + if (!error && !sfi.count) { + giterr_set(GITERR_INVALID, + "Attempt to get status of nonexistent file '%s'", path); + error = GIT_ENOTFOUND; } - /* Find file in Index */ - if ((error = git_repository_index__weakptr(&index, repo)) < GIT_SUCCESS) { - error = git__rethrow(error, - "Failed to determine status of file '%s'." - "Index can't be opened", path); - goto exit; - } + *status_flags = sfi.status; - status_entry_update_from_index(e, index); + git__free(sfi.expected); - if ((error = retrieve_head_tree(&tree, repo)) < GIT_SUCCESS) { - error = git__rethrow(error, - "Failed to determine status of file '%s'", path); - goto exit; - } - - /* If the repository is not empty, try and locate the file in HEAD */ - if (tree != NULL) { - strcpy(temp_path, path); - - error = recurse_tree_entry(tree, e, temp_path); - if (error < GIT_SUCCESS) { - error = git__rethrow(error, - "Failed to determine status of file '%s'. " - "An error occured while processing the tree", path); - goto exit; - } - } - - /* Determine status */ - if ((error = status_entry_update_flags(e)) < GIT_SUCCESS) { - error = git__throw(error, "Nonexistent file"); - goto exit; - } - - *status_flags = e->status_flags; - -exit: - git_tree_free(tree); - git__free(e); return error; } -/* - * git_futils_direach is not supposed to return entries in an ordered manner. - * alphasorted_futils_direach wraps git_futils_direach and invokes the callback - * function by passing it alphabeticcally sorted paths parameters. - * - */ - -struct alphasorted_dirent_info { - int is_dir; - - char path[GIT_FLEX_ARRAY]; /* more */ -}; - -static struct alphasorted_dirent_info *alphasorted_dirent_info_new(const char *path) +int git_status_should_ignore( + int *ignored, + git_repository *repo, + const char *path) { - int is_dir, size; - struct alphasorted_dirent_info *di; + int error; + git_ignores ignores; - is_dir = git_futils_isdir(path) == GIT_SUCCESS ? 1 : 0; - size = sizeof(*di) + (is_dir ? GIT_PATH_MAX : strlen(path)) + 2; + if (git_ignore__for_path(repo, path, &ignores) < 0) + return -1; - di = git__malloc(size); - if (di == NULL) - return NULL; - - memset(di, 0x0, size); - - strcpy(di->path, path); - - if (is_dir) { - di->is_dir = 1; - - /* - * Append a forward slash to the name to force folders - * to be ordered in a similar way than in a tree - * - * The file "subdir" should appear before the file "subdir.txt" - * The folder "subdir" should appear after the file "subdir.txt" - */ - di->path[strlen(path)] = '/'; - } - - return di; -} - -static int alphasorted_dirent_info_cmp(const void *a, const void *b) -{ - struct alphasorted_dirent_info *stra = (struct alphasorted_dirent_info *)a; - struct alphasorted_dirent_info *strb = (struct alphasorted_dirent_info *)b; - - return strcmp(stra->path, strb->path); -} - -static int alphasorted_dirent_cb(void *state, char *full_path) -{ - struct alphasorted_dirent_info *entry; - git_vector *entry_names; - - entry_names = (git_vector *)state; - entry = alphasorted_dirent_info_new(full_path); - - if (entry == NULL) - return GIT_ENOMEM; - - if (git_vector_insert(entry_names, entry) < GIT_SUCCESS) { - git__free(entry); - return GIT_ENOMEM; - } - - return GIT_SUCCESS; -} - -static int alphasorted_futils_direach( - char *path, - size_t path_sz, - int (*fn)(void *, char *), - void *arg) -{ - struct alphasorted_dirent_info *entry; - git_vector entry_names; - unsigned int idx; - int error = GIT_SUCCESS; - - if (git_vector_init(&entry_names, 16, alphasorted_dirent_info_cmp) < GIT_SUCCESS) - return GIT_ENOMEM; - - error = git_futils_direach(path, path_sz, alphasorted_dirent_cb, &entry_names); - - git_vector_sort(&entry_names); - - for (idx = 0; idx < entry_names.length; ++idx) { - entry = (struct alphasorted_dirent_info *)git_vector_get(&entry_names, idx); - - if (error == GIT_SUCCESS) { - ((struct status_st *)arg)->is_dir = entry->is_dir; - error = fn(arg, entry->path); - } - - git__free(entry); - } - - git_vector_free(&entry_names); + error = git_ignore__lookup(&ignores, path, ignored); + git_ignore__free(&ignores); return error; } +