/package.nw/node_modules/git-utils/deps/libgit2/tests/index/bypath.c

https://github.com/cytle/wechat_web_devtools · C · 362 lines · 257 code · 104 blank · 1 comment · 16 complexity · 74cbacdca92ee85aa2c2021d779b1943 MD5 · raw file

  1. #include "clar_libgit2.h"
  2. #include "repository.h"
  3. #include "../submodule/submodule_helpers.h"
  4. static git_repository *g_repo;
  5. static git_index *g_idx;
  6. void test_index_bypath__initialize(void)
  7. {
  8. g_repo = setup_fixture_submod2();
  9. cl_git_pass(git_repository_index__weakptr(&g_idx, g_repo));
  10. }
  11. void test_index_bypath__cleanup(void)
  12. {
  13. g_repo = NULL;
  14. g_idx = NULL;
  15. }
  16. void test_index_bypath__add_directory(void)
  17. {
  18. cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
  19. }
  20. void test_index_bypath__add_submodule(void)
  21. {
  22. unsigned int status;
  23. const char *sm_name = "sm_changed_head";
  24. cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
  25. cl_assert_equal_i(GIT_SUBMODULE_STATUS_WD_MODIFIED, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
  26. cl_git_pass(git_index_add_bypath(g_idx, sm_name));
  27. cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
  28. cl_assert_equal_i(0, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
  29. }
  30. void test_index_bypath__add_submodule_unregistered(void)
  31. {
  32. const char *sm_name = "not-submodule";
  33. const char *sm_head = "68e92c611b80ee1ed8f38314ff9577f0d15b2444";
  34. const git_index_entry *entry;
  35. cl_git_pass(git_index_add_bypath(g_idx, sm_name));
  36. cl_assert(entry = git_index_get_bypath(g_idx, sm_name, 0));
  37. cl_assert_equal_s(sm_head, git_oid_tostr_s(&entry->id));
  38. cl_assert_equal_s(sm_name, entry->path);
  39. }
  40. void test_index_bypath__add_hidden(void)
  41. {
  42. const git_index_entry *entry;
  43. bool hidden;
  44. GIT_UNUSED(entry);
  45. GIT_UNUSED(hidden);
  46. #ifdef GIT_WIN32
  47. cl_git_mkfile("submod2/hidden_file", "you can't see me");
  48. cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
  49. cl_assert(!hidden);
  50. cl_git_pass(git_win32__set_hidden("submod2/hidden_file", true));
  51. cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
  52. cl_assert(hidden);
  53. cl_git_pass(git_index_add_bypath(g_idx, "hidden_file"));
  54. cl_assert(entry = git_index_get_bypath(g_idx, "hidden_file", 0));
  55. cl_assert_equal_i(GIT_FILEMODE_BLOB, entry->mode);
  56. #endif
  57. }
  58. void test_index_bypath__add_keeps_existing_case(void)
  59. {
  60. const git_index_entry *entry;
  61. if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
  62. clar__skip();
  63. cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
  64. cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/file1.txt"));
  65. cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/file1.txt", 0));
  66. cl_assert_equal_s("just_a_dir/file1.txt", entry->path);
  67. cl_git_rewritefile("submod2/just_a_dir/file1.txt", "Updated!");
  68. cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/FILE1.txt"));
  69. cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/FILE1.txt", 0));
  70. cl_assert_equal_s("just_a_dir/file1.txt", entry->path);
  71. }
  72. void test_index_bypath__add_honors_existing_case(void)
  73. {
  74. const git_index_entry *entry;
  75. if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
  76. clar__skip();
  77. cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
  78. cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
  79. cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
  80. cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
  81. cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/File1.txt"));
  82. cl_git_pass(git_index_add_bypath(g_idx, "JUST_A_DIR/file2.txt"));
  83. cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/FILE3.txt"));
  84. cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/File1.txt", 0));
  85. cl_assert_equal_s("just_a_dir/File1.txt", entry->path);
  86. cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/file2.txt", 0));
  87. cl_assert_equal_s("just_a_dir/file2.txt", entry->path);
  88. cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/FILE3.txt", 0));
  89. cl_assert_equal_s("just_a_dir/FILE3.txt", entry->path);
  90. cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
  91. cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/file3.txt"));
  92. cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/file3.txt", 0));
  93. cl_assert_equal_s("just_a_dir/FILE3.txt", entry->path);
  94. }
  95. void test_index_bypath__add_honors_existing_case_2(void)
  96. {
  97. git_index_entry dummy = { { 0 } };
  98. const git_index_entry *entry;
  99. if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
  100. clar__skip();
  101. dummy.mode = GIT_FILEMODE_BLOB;
  102. cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
  103. /* note that `git_index_add` does no checking to canonical directories */
  104. dummy.path = "Just_a_dir/file0.txt";
  105. cl_git_pass(git_index_add(g_idx, &dummy));
  106. dummy.path = "just_a_dir/fileA.txt";
  107. cl_git_pass(git_index_add(g_idx, &dummy));
  108. dummy.path = "Just_A_Dir/fileB.txt";
  109. cl_git_pass(git_index_add(g_idx, &dummy));
  110. dummy.path = "JUST_A_DIR/fileC.txt";
  111. cl_git_pass(git_index_add(g_idx, &dummy));
  112. dummy.path = "just_A_dir/fileD.txt";
  113. cl_git_pass(git_index_add(g_idx, &dummy));
  114. dummy.path = "JUST_a_DIR/fileE.txt";
  115. cl_git_pass(git_index_add(g_idx, &dummy));
  116. cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
  117. cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
  118. cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
  119. cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
  120. cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/File1.txt"));
  121. cl_git_pass(git_index_add_bypath(g_idx, "JUST_A_DIR/file2.txt"));
  122. cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/FILE3.txt"));
  123. cl_git_pass(git_index_add_bypath(g_idx, "JusT_A_DIR/FILE4.txt"));
  124. cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/File1.txt", 0));
  125. cl_assert_equal_s("just_a_dir/File1.txt", entry->path);
  126. cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/file2.txt", 0));
  127. cl_assert_equal_s("JUST_A_DIR/file2.txt", entry->path);
  128. cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/FILE3.txt", 0));
  129. cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry->path);
  130. cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
  131. cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/file3.txt"));
  132. cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/file3.txt", 0));
  133. cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry->path);
  134. }
  135. void test_index_bypath__add_honors_existing_case_3(void)
  136. {
  137. git_index_entry dummy = { { 0 } };
  138. const git_index_entry *entry;
  139. if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
  140. clar__skip();
  141. dummy.mode = GIT_FILEMODE_BLOB;
  142. cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
  143. dummy.path = "just_a_dir/filea.txt";
  144. cl_git_pass(git_index_add(g_idx, &dummy));
  145. dummy.path = "Just_A_Dir/fileB.txt";
  146. cl_git_pass(git_index_add(g_idx, &dummy));
  147. dummy.path = "just_A_DIR/FILEC.txt";
  148. cl_git_pass(git_index_add(g_idx, &dummy));
  149. dummy.path = "Just_a_DIR/FileD.txt";
  150. cl_git_pass(git_index_add(g_idx, &dummy));
  151. cl_git_mkfile("submod2/JuSt_A_DiR/fILEE.txt", "This is a file");
  152. cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/fILEE.txt"));
  153. cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/fILEE.txt", 0));
  154. cl_assert_equal_s("just_a_dir/fILEE.txt", entry->path);
  155. }
  156. void test_index_bypath__add_honors_existing_case_4(void)
  157. {
  158. git_index_entry dummy = { { 0 } };
  159. const git_index_entry *entry;
  160. if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
  161. clar__skip();
  162. dummy.mode = GIT_FILEMODE_BLOB;
  163. cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b"));
  164. dummy.path = "just_a_dir/a/b/c/d/e/file1.txt";
  165. cl_git_pass(git_index_add(g_idx, &dummy));
  166. dummy.path = "just_a_dir/a/B/C/D/E/file2.txt";
  167. cl_git_pass(git_index_add(g_idx, &dummy));
  168. cl_must_pass(p_mkdir("submod2/just_a_dir/a", 0777));
  169. cl_must_pass(p_mkdir("submod2/just_a_dir/a/b", 0777));
  170. cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z", 0777));
  171. cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y", 0777));
  172. cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y/x", 0777));
  173. cl_git_mkfile("submod2/just_a_dir/a/b/z/y/x/FOO.txt", "This is a file");
  174. cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt"));
  175. cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt", 0));
  176. cl_assert_equal_s("just_a_dir/a/b/Z/y/X/foo.txt", entry->path);
  177. }
  178. void test_index_bypath__add_honors_mode(void)
  179. {
  180. const git_index_entry *entry;
  181. git_index_entry new_entry;
  182. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  183. memcpy(&new_entry, entry, sizeof(git_index_entry));
  184. new_entry.path = "README.txt";
  185. new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
  186. cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
  187. cl_git_pass(git_index_add(g_idx, &new_entry));
  188. cl_git_pass(git_index_write(g_idx));
  189. cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
  190. cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
  191. cl_git_pass(git_index_write(g_idx));
  192. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  193. cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, entry->mode);
  194. }
  195. void test_index_bypath__add_honors_conflict_mode(void)
  196. {
  197. const git_index_entry *entry;
  198. git_index_entry new_entry;
  199. int stage = 0;
  200. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  201. memcpy(&new_entry, entry, sizeof(git_index_entry));
  202. new_entry.path = "README.txt";
  203. new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
  204. cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
  205. cl_git_pass(git_index_remove_bypath(g_idx, "README.txt"));
  206. for (stage = 1; stage <= 3; stage++) {
  207. new_entry.flags = stage << GIT_IDXENTRY_STAGESHIFT;
  208. cl_git_pass(git_index_add(g_idx, &new_entry));
  209. }
  210. cl_git_pass(git_index_write(g_idx));
  211. cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
  212. cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
  213. cl_git_pass(git_index_write(g_idx));
  214. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  215. cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, entry->mode);
  216. }
  217. void test_index_bypath__add_honors_conflict_case(void)
  218. {
  219. const git_index_entry *entry;
  220. git_index_entry new_entry;
  221. int stage = 0;
  222. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  223. memcpy(&new_entry, entry, sizeof(git_index_entry));
  224. new_entry.path = "README.txt";
  225. new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
  226. cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
  227. cl_git_pass(git_index_remove_bypath(g_idx, "README.txt"));
  228. for (stage = 1; stage <= 3; stage++) {
  229. new_entry.flags = stage << GIT_IDXENTRY_STAGESHIFT;
  230. cl_git_pass(git_index_add(g_idx, &new_entry));
  231. }
  232. cl_git_pass(git_index_write(g_idx));
  233. cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
  234. cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
  235. cl_git_pass(git_index_write(g_idx));
  236. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  237. cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, entry->mode);
  238. }
  239. void test_index_bypath__add_honors_symlink(void)
  240. {
  241. const git_index_entry *entry;
  242. git_index_entry new_entry;
  243. int symlinks;
  244. cl_git_pass(git_repository__cvar(&symlinks, g_repo, GIT_CVAR_SYMLINKS));
  245. if (symlinks)
  246. cl_skip();
  247. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  248. memcpy(&new_entry, entry, sizeof(git_index_entry));
  249. new_entry.path = "README.txt";
  250. new_entry.mode = GIT_FILEMODE_LINK;
  251. cl_git_pass(git_index_add(g_idx, &new_entry));
  252. cl_git_pass(git_index_write(g_idx));
  253. cl_git_rewritefile("submod2/README.txt", "Modified but still a (fake) symlink");
  254. cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
  255. cl_git_pass(git_index_write(g_idx));
  256. cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
  257. cl_assert_equal_i(GIT_FILEMODE_LINK, entry->mode);
  258. }