status: nonexistent file with git_status_file() Throws GIT_ENOTFOUND error if given a filename that is not in HEAD, index, nor the work tree.
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
diff --git a/src/status.c b/src/status.c
index e421842..9f826e0 100644
--- a/src/status.c
+++ b/src/status.c
@@ -258,6 +258,9 @@ static int set_status_flags(struct status_entry *e)
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)
@@ -345,7 +348,7 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
git_index *index;
git_index_entry *index_entry;
char temp_path[GIT_PATH_MAX];
- int idx;
+ int idx, error;
git_tree *tree;
git_reference *head_ref, *resolved_head_ref;
git_commit *head_commit;
@@ -377,7 +380,9 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
strcpy(temp_path, repo->path_workdir);
git_futils_direach(temp_path, GIT_PATH_MAX, single_dirent_cb, &e);
- set_status_flags(e);
+ if ((error = set_status_flags(e)) < GIT_SUCCESS)
+ return git__throw(error, "Nonexistent file");
+
*status_flags = e->status_flags;
free(e);
diff --git a/tests/t18-status.c b/tests/t18-status.c
index dfd87b3..a20d97b 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -184,9 +184,30 @@ BEGIN_TEST(singlestatus0, "test retrieving status for single file")
git_futils_rmdir_r(TEMP_STATUS_FOLDER, 1);
END_TEST
+BEGIN_TEST(singlestatus1, "test retrieving status for nonexistent file")
+ char path_statusfiles[GIT_PATH_MAX];
+ char temp_path[GIT_PATH_MAX];
+ git_repository *repo;
+ unsigned int status_flags;
+ int error;
+
+ must_pass(copy_status_repo(path_statusfiles, temp_path));
+
+ must_pass(git_repository_open(&repo, temp_path));
+
+ // "nonexistent" does not exist in HEAD, Index or the worktree
+ error = git_status_file(&status_flags, repo, "nonexistent");
+ must_be_true(error == GIT_ENOTFOUND);
+
+ git_repository_free(repo);
+
+ git_futils_rmdir_r(TEMP_STATUS_FOLDER, 1);
+END_TEST
+
BEGIN_SUITE(status)
ADD_TEST(file0);
ADD_TEST(statuscb0);
ADD_TEST(singlestatus0);
+ ADD_TEST(singlestatus1);
END_SUITE