diff options
author | Vicent Marti <tanoku@gmail.com> | 2013-11-20 15:54:24 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2013-11-20 15:54:24 +0400 |
commit | 4b0a36e881506a02b43a4ae3c19c93c919b36eeb (patch) | |
tree | 026182fa30273a4c1649928b6db3fc5335bd1ea4 /tests/stash | |
parent | 29d7242b1dcd1f09a63417abd648a6217b85d301 (diff) | |
parent | 43cb8b32428b1b29994874349ec22eb5372e152c (diff) |
Merge branch 'development'
Diffstat (limited to 'tests/stash')
-rw-r--r-- | tests/stash/drop.c | 174 | ||||
-rw-r--r-- | tests/stash/foreach.c | 124 | ||||
-rw-r--r-- | tests/stash/save.c | 354 | ||||
-rw-r--r-- | tests/stash/stash_helpers.c | 56 | ||||
-rw-r--r-- | tests/stash/stash_helpers.h | 8 | ||||
-rw-r--r-- | tests/stash/submodules.c | 80 |
6 files changed, 796 insertions, 0 deletions
diff --git a/tests/stash/drop.c b/tests/stash/drop.c new file mode 100644 index 000000000..63ff0377c --- /dev/null +++ b/tests/stash/drop.c @@ -0,0 +1,174 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "stash_helpers.h" +#include "refs.h" + +static git_repository *repo; +static git_signature *signature; + +void test_stash_drop__initialize(void) +{ + cl_git_pass(git_repository_init(&repo, "stash", 0)); + cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ +} + +void test_stash_drop__cleanup(void) +{ + git_signature_free(signature); + signature = NULL; + + git_repository_free(repo); + repo = NULL; + + cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES)); +} + +void test_stash_drop__cannot_drop_from_an_empty_stash(void) +{ + cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND); +} + +static void push_three_states(void) +{ + git_oid oid; + git_index *index; + + cl_git_mkfile("stash/zero.txt", "content\n"); + cl_git_pass(git_repository_index(&index, repo)); + cl_git_pass(git_index_add_bypath(index, "zero.txt")); + cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); + cl_assert(git_path_exists("stash/zero.txt")); + git_index_free(index); + + cl_git_mkfile("stash/one.txt", "content\n"); + cl_git_pass(git_stash_save( + &oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED)); + cl_assert(!git_path_exists("stash/one.txt")); + cl_assert(git_path_exists("stash/zero.txt")); + + cl_git_mkfile("stash/two.txt", "content\n"); + cl_git_pass(git_stash_save( + &oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED)); + cl_assert(!git_path_exists("stash/two.txt")); + cl_assert(git_path_exists("stash/zero.txt")); + + cl_git_mkfile("stash/three.txt", "content\n"); + cl_git_pass(git_stash_save( + &oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED)); + cl_assert(!git_path_exists("stash/three.txt")); + cl_assert(git_path_exists("stash/zero.txt")); +} + +void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void) +{ + push_three_states(); + + cl_git_fail_with(git_stash_drop(repo, 666), GIT_ENOTFOUND); + cl_git_fail_with(git_stash_drop(repo, 42), GIT_ENOTFOUND); + cl_git_fail_with(git_stash_drop(repo, 3), GIT_ENOTFOUND); +} + +void test_stash_drop__can_purge_the_stash_from_the_top(void) +{ + push_three_states(); + + cl_git_pass(git_stash_drop(repo, 0)); + cl_git_pass(git_stash_drop(repo, 0)); + cl_git_pass(git_stash_drop(repo, 0)); + + cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND); +} + +void test_stash_drop__can_purge_the_stash_from_the_bottom(void) +{ + push_three_states(); + + cl_git_pass(git_stash_drop(repo, 2)); + cl_git_pass(git_stash_drop(repo, 1)); + cl_git_pass(git_stash_drop(repo, 0)); + + cl_git_fail_with(git_stash_drop(repo, 0), GIT_ENOTFOUND); +} + +void test_stash_drop__dropping_an_entry_rewrites_reflog_history(void) +{ + git_reference *stash; + git_reflog *reflog; + const git_reflog_entry *entry; + git_oid oid; + size_t count; + + push_three_states(); + + cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)); + + cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)); + entry = git_reflog_entry_byindex(reflog, 1); + + git_oid_cpy(&oid, git_reflog_entry_id_old(entry)); + count = git_reflog_entrycount(reflog); + + git_reflog_free(reflog); + + cl_git_pass(git_stash_drop(repo, 1)); + + cl_git_pass(git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)); + entry = git_reflog_entry_byindex(reflog, 0); + + cl_assert_equal_i(0, git_oid_cmp(&oid, git_reflog_entry_id_old(entry))); + cl_assert_equal_sz(count - 1, git_reflog_entrycount(reflog)); + + git_reflog_free(reflog); + + git_reference_free(stash); +} + +void test_stash_drop__dropping_the_last_entry_removes_the_stash(void) +{ + git_reference *stash; + + push_three_states(); + + cl_git_pass(git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)); + git_reference_free(stash); + + cl_git_pass(git_stash_drop(repo, 0)); + cl_git_pass(git_stash_drop(repo, 0)); + cl_git_pass(git_stash_drop(repo, 0)); + + cl_git_fail_with( + git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE), GIT_ENOTFOUND); +} + +void retrieve_top_stash_id(git_oid *out) +{ + git_object *top_stash; + + cl_git_pass(git_revparse_single(&top_stash, repo, "stash@{0}")); + cl_git_pass(git_reference_name_to_id(out, repo, GIT_REFS_STASH_FILE)); + + cl_assert_equal_i(true, git_oid_cmp(out, git_object_id(top_stash)) == 0); + + git_object_free(top_stash); +} + +void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void) +{ + git_object *next_top_stash; + git_oid oid; + + push_three_states(); + + retrieve_top_stash_id(&oid); + + cl_git_pass(git_revparse_single(&next_top_stash, repo, "stash@{1}")); + cl_assert(git_oid_cmp(&oid, git_object_id(next_top_stash)) != 0); + + cl_git_pass(git_stash_drop(repo, 0)); + + retrieve_top_stash_id(&oid); + + cl_git_pass(git_oid_cmp(&oid, git_object_id(next_top_stash))); + + git_object_free(next_top_stash); +} diff --git a/tests/stash/foreach.c b/tests/stash/foreach.c new file mode 100644 index 000000000..f1983625f --- /dev/null +++ b/tests/stash/foreach.c @@ -0,0 +1,124 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "stash_helpers.h" + +struct callback_data +{ + char **oids; + int invokes; +}; + +static git_repository *repo; +static git_signature *signature; +static git_oid stash_tip_oid; +struct callback_data data; + +#define REPO_NAME "stash" + +void test_stash_foreach__initialize(void) +{ + cl_git_pass(git_signature_new( + &signature, + "nulltoken", + "emeric.fermas@gmail.com", + 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ + + memset(&data, 0, sizeof(struct callback_data)); +} + +void test_stash_foreach__cleanup(void) +{ + git_signature_free(signature); + signature = NULL; + + git_repository_free(repo); + repo = NULL; + + cl_git_pass(git_futils_rmdir_r(REPO_NAME, NULL, GIT_RMDIR_REMOVE_FILES)); +} + +static int callback_cb( + size_t index, + const char* message, + const git_oid *stash_oid, + void *payload) +{ + struct callback_data *data = (struct callback_data *)payload; + + GIT_UNUSED(index); + GIT_UNUSED(message); + + cl_assert_equal_i(0, git_oid_streq(stash_oid, data->oids[data->invokes++])); + + return 0; +} + +void test_stash_foreach__enumerating_a_empty_repository_doesnt_fail(void) +{ + char *oids[] = { NULL }; + + data.oids = oids; + + cl_git_pass(git_repository_init(&repo, REPO_NAME, 0)); + + cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); + + cl_assert_equal_i(0, data.invokes); +} + +void test_stash_foreach__can_enumerate_a_repository(void) +{ + char *oids_default[] = { + "1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL }; + + char *oids_untracked[] = { + "7f89a8b15c878809c5c54d1ff8f8c9674154017b", + "1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL }; + + char *oids_ignored[] = { + "c95599a8fef20a7e57582c6727b1a0d02e0a5828", + "7f89a8b15c878809c5c54d1ff8f8c9674154017b", + "1d91c842a7cdfc25872b3a763e5c31add8816c25", NULL }; + + cl_git_pass(git_repository_init(&repo, REPO_NAME, 0)); + + setup_stash(repo, signature); + + cl_git_pass(git_stash_save( + &stash_tip_oid, + repo, + signature, + NULL, + GIT_STASH_DEFAULT)); + + data.oids = oids_default; + + cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); + cl_assert_equal_i(1, data.invokes); + + data.oids = oids_untracked; + data.invokes = 0; + + cl_git_pass(git_stash_save( + &stash_tip_oid, + repo, + signature, + NULL, + GIT_STASH_INCLUDE_UNTRACKED)); + + cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); + cl_assert_equal_i(2, data.invokes); + + data.oids = oids_ignored; + data.invokes = 0; + + cl_git_pass(git_stash_save( + &stash_tip_oid, + repo, + signature, + NULL, + GIT_STASH_INCLUDE_IGNORED)); + + cl_git_pass(git_stash_foreach(repo, callback_cb, &data)); + cl_assert_equal_i(3, data.invokes); +} diff --git a/tests/stash/save.c b/tests/stash/save.c new file mode 100644 index 000000000..3d92b26bd --- /dev/null +++ b/tests/stash/save.c @@ -0,0 +1,354 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "stash_helpers.h" + +static git_repository *repo; +static git_signature *signature; +static git_oid stash_tip_oid; + +/* + * Friendly reminder, in order to ease the reading of the following tests: + * + * "stash" points to the worktree commit + * "stash^1" points to the base commit (HEAD when the stash was created) + * "stash^2" points to the index commit + * "stash^3" points to the untracked commit + */ + +void test_stash_save__initialize(void) +{ + cl_git_pass(git_repository_init(&repo, "stash", 0)); + cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ + + setup_stash(repo, signature); +} + +void test_stash_save__cleanup(void) +{ + git_signature_free(signature); + signature = NULL; + + git_repository_free(repo); + repo = NULL; + + cl_git_pass(git_futils_rmdir_r("stash", NULL, GIT_RMDIR_REMOVE_FILES)); + cl_fixture_cleanup("sorry-it-is-a-non-bare-only-party"); +} + +static void assert_object_oid(const char* revision, const char* expected_oid, git_otype type) +{ + int result; + git_object *obj; + + result = git_revparse_single(&obj, repo, revision); + + if (!expected_oid) { + cl_assert_equal_i(GIT_ENOTFOUND, result); + return; + } else + cl_assert_equal_i(0, result); + + cl_git_pass(git_oid_streq(git_object_id(obj), expected_oid)); + cl_assert_equal_i(type, git_object_type(obj)); + git_object_free(obj); +} + +static void assert_blob_oid(const char* revision, const char* expected_oid) +{ + assert_object_oid(revision, expected_oid, GIT_OBJ_BLOB); +} + +void test_stash_save__does_not_keep_index_by_default(void) +{ +/* +$ git stash + +$ git show refs/stash:what +see you later + +$ git show refs/stash:how +not so small and + +$ git show refs/stash:who +funky world + +$ git show refs/stash:when +fatal: Path 'when' exists on disk, but not in 'stash'. + +$ git show refs/stash^2:what +goodbye + +$ git show refs/stash^2:how +not so small and + +$ git show refs/stash^2:who +world + +$ git show refs/stash^2:when +fatal: Path 'when' exists on disk, but not in 'stash^2'. + +$ git status --short +?? when + +*/ + unsigned int status; + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + cl_git_pass(git_status_file(&status, repo, "when")); + + assert_blob_oid("refs/stash:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449"); /* see you later */ + assert_blob_oid("refs/stash:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ + assert_blob_oid("refs/stash:who", "a0400d4954659306a976567af43125a0b1aa8595"); /* funky world */ + assert_blob_oid("refs/stash:when", NULL); + assert_blob_oid("refs/stash:just.ignore", NULL); + + assert_blob_oid("refs/stash^2:what", "dd7e1c6f0fefe118f0b63d9f10908c460aa317a6"); /* goodbye */ + assert_blob_oid("refs/stash^2:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ + assert_blob_oid("refs/stash^2:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ + assert_blob_oid("refs/stash^2:when", NULL); + assert_blob_oid("refs/stash^2:just.ignore", NULL); + + assert_blob_oid("refs/stash^3", NULL); + + cl_assert_equal_i(GIT_STATUS_WT_NEW, status); +} + +void test_stash_save__can_keep_index(void) +{ + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_KEEP_INDEX)); + + assert_status(repo, "what", GIT_STATUS_INDEX_MODIFIED); + assert_status(repo, "how", GIT_STATUS_INDEX_MODIFIED); + assert_status(repo, "who", GIT_STATUS_CURRENT); + assert_status(repo, "when", GIT_STATUS_WT_NEW); + assert_status(repo, "just.ignore", GIT_STATUS_IGNORED); +} + +static void assert_commit_message_contains(const char *revision, const char *fragment) +{ + git_commit *commit; + + cl_git_pass(git_revparse_single((git_object**)&commit, repo, revision)); + + cl_assert(strstr(git_commit_message(commit), fragment) != NULL); + + git_commit_free(commit); +} + +void test_stash_save__can_include_untracked_files(void) +{ + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); + + assert_commit_message_contains("refs/stash^3", "untracked files on master: "); + + assert_blob_oid("refs/stash^3:what", NULL); + assert_blob_oid("refs/stash^3:how", NULL); + assert_blob_oid("refs/stash^3:who", NULL); + assert_blob_oid("refs/stash^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b"); + assert_blob_oid("refs/stash^3:just.ignore", NULL); +} + +void test_stash_save__can_include_untracked_and_ignored_files(void) +{ + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)); + + assert_commit_message_contains("refs/stash^3", "untracked files on master: "); + + assert_blob_oid("refs/stash^3:what", NULL); + assert_blob_oid("refs/stash^3:how", NULL); + assert_blob_oid("refs/stash^3:who", NULL); + assert_blob_oid("refs/stash^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b"); + assert_blob_oid("refs/stash^3:just.ignore", "78925fb1236b98b37a35e9723033e627f97aa88b"); +} + +#define MESSAGE "Look Ma! I'm on TV!" +void test_stash_save__can_accept_a_message(void) +{ + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, MESSAGE, GIT_STASH_DEFAULT)); + + assert_commit_message_contains("refs/stash^2", "index on master: "); + assert_commit_message_contains("refs/stash", "On master: " MESSAGE); +} + +void test_stash_save__cannot_stash_against_an_unborn_branch(void) +{ + git_reference *head; + + cl_git_pass(git_reference_symbolic_create(&head, repo, "HEAD", "refs/heads/unborn", 1)); + + cl_assert_equal_i(GIT_EUNBORNBRANCH, + git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + + git_reference_free(head); +} + +void test_stash_save__cannot_stash_against_a_bare_repository(void) +{ + git_repository *local; + + cl_git_pass(git_repository_init(&local, "sorry-it-is-a-non-bare-only-party", 1)); + + cl_assert_equal_i(GIT_EBAREREPO, + git_stash_save(&stash_tip_oid, local, signature, NULL, GIT_STASH_DEFAULT)); + + git_repository_free(local); +} + +void test_stash_save__can_stash_against_a_detached_head(void) +{ + git_repository_detach_head(repo); + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + + assert_commit_message_contains("refs/stash^2", "index on (no branch): "); + assert_commit_message_contains("refs/stash", "WIP on (no branch): "); +} + +void test_stash_save__stashing_updates_the_reflog(void) +{ + char *sha; + + assert_object_oid("refs/stash@{0}", NULL, GIT_OBJ_COMMIT); + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + + sha = git_oid_allocfmt(&stash_tip_oid); + + assert_object_oid("refs/stash@{0}", sha, GIT_OBJ_COMMIT); + assert_object_oid("refs/stash@{1}", NULL, GIT_OBJ_COMMIT); + + git__free(sha); +} + +void test_stash_save__cannot_stash_when_there_are_no_local_change(void) +{ + git_index *index; + git_oid stash_tip_oid; + + cl_git_pass(git_repository_index(&index, repo)); + + /* + * 'what' and 'who' are being committed. + * 'when' remain untracked. + */ + cl_git_pass(git_index_add_bypath(index, "what")); + cl_git_pass(git_index_add_bypath(index, "who")); + cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); + git_index_free(index); + + cl_assert_equal_i(GIT_ENOTFOUND, + git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + + p_unlink("stash/when"); + cl_assert_equal_i(GIT_ENOTFOUND, + git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); +} + +void test_stash_save__can_stage_normal_then_stage_untracked(void) +{ + /* + * $ git ls-tree stash@{1}^0 + * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore + * 100644 blob e6d64adb2c7f3eb8feb493b556cc8070dca379a3 how + * 100644 blob bc99dc98b3eba0e9157e94769cd4d49cb49de449 what + * 100644 blob a0400d4954659306a976567af43125a0b1aa8595 who + * + * $ git ls-tree stash@{1}^1 + * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore + * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how + * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what + * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who + * + * $ git ls-tree stash@{1}^2 + * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore + * 100644 blob e6d64adb2c7f3eb8feb493b556cc8070dca379a3 how + * 100644 blob dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 what + * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who + * + * $ git ls-tree stash@{1}^3 + * fatal: Not a valid object name stash@{1}^3 + * + * $ git ls-tree stash@{0}^0 + * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore + * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how + * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what + * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who + * + * $ git ls-tree stash@{0}^1 + * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore + * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how + * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what + * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who + * + * $ git ls-tree stash@{0}^2 + * 100644 blob ac4d88de61733173d9959e4b77c69b9f17a00980 .gitignore + * 100644 blob ac790413e2d7a26c3767e78c57bb28716686eebc how + * 100644 blob ce013625030ba8dba906f756967f9e9ca394464a what + * 100644 blob cc628ccd10742baea8241c5924df992b5c019f71 who + * + * $ git ls-tree stash@{0}^3 + * 100644 blob b6ed15e81e2593d7bb6265eb4a991d29dc3e628b when + */ + + assert_status(repo, "what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED); + assert_status(repo, "how", GIT_STATUS_INDEX_MODIFIED); + assert_status(repo, "who", GIT_STATUS_WT_MODIFIED); + assert_status(repo, "when", GIT_STATUS_WT_NEW); + assert_status(repo, "just.ignore", GIT_STATUS_IGNORED); + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + assert_status(repo, "what", GIT_STATUS_CURRENT); + assert_status(repo, "how", GIT_STATUS_CURRENT); + assert_status(repo, "who", GIT_STATUS_CURRENT); + assert_status(repo, "when", GIT_STATUS_WT_NEW); + assert_status(repo, "just.ignore", GIT_STATUS_IGNORED); + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); + assert_status(repo, "what", GIT_STATUS_CURRENT); + assert_status(repo, "how", GIT_STATUS_CURRENT); + assert_status(repo, "who", GIT_STATUS_CURRENT); + assert_status(repo, "when", GIT_ENOTFOUND); + assert_status(repo, "just.ignore", GIT_STATUS_IGNORED); + + + assert_blob_oid("stash@{1}^0:what", "bc99dc98b3eba0e9157e94769cd4d49cb49de449"); /* see you later */ + assert_blob_oid("stash@{1}^0:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ + assert_blob_oid("stash@{1}^0:who", "a0400d4954659306a976567af43125a0b1aa8595"); /* funky world */ + assert_blob_oid("stash@{1}^0:when", NULL); + + assert_blob_oid("stash@{1}^2:what", "dd7e1c6f0fefe118f0b63d9f10908c460aa317a6"); /* goodbye */ + assert_blob_oid("stash@{1}^2:how", "e6d64adb2c7f3eb8feb493b556cc8070dca379a3"); /* not so small and */ + assert_blob_oid("stash@{1}^2:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ + assert_blob_oid("stash@{1}^2:when", NULL); + + assert_object_oid("stash@{1}^3", NULL, GIT_OBJ_COMMIT); + + assert_blob_oid("stash@{0}^0:what", "ce013625030ba8dba906f756967f9e9ca394464a"); /* hello */ + assert_blob_oid("stash@{0}^0:how", "ac790413e2d7a26c3767e78c57bb28716686eebc"); /* small */ + assert_blob_oid("stash@{0}^0:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ + assert_blob_oid("stash@{0}^0:when", NULL); + + assert_blob_oid("stash@{0}^2:what", "ce013625030ba8dba906f756967f9e9ca394464a"); /* hello */ + assert_blob_oid("stash@{0}^2:how", "ac790413e2d7a26c3767e78c57bb28716686eebc"); /* small */ + assert_blob_oid("stash@{0}^2:who", "cc628ccd10742baea8241c5924df992b5c019f71"); /* world */ + assert_blob_oid("stash@{0}^2:when", NULL); + + assert_blob_oid("stash@{0}^3:when", "b6ed15e81e2593d7bb6265eb4a991d29dc3e628b"); /* now */ +} + +#define EMPTY_TREE "4b825dc642cb6eb9a060e54bf8d69288fbee4904" + +void test_stash_save__including_untracked_without_any_untracked_file_creates_an_empty_tree(void) +{ + cl_git_pass(p_unlink("stash/when")); + + assert_status(repo, "what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED); + assert_status(repo, "how", GIT_STATUS_INDEX_MODIFIED); + assert_status(repo, "who", GIT_STATUS_WT_MODIFIED); + assert_status(repo, "when", GIT_ENOTFOUND); + assert_status(repo, "just.ignore", GIT_STATUS_IGNORED); + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED)); + + assert_object_oid("stash^3^{tree}", EMPTY_TREE, GIT_OBJ_TREE); +} diff --git a/tests/stash/stash_helpers.c b/tests/stash/stash_helpers.c new file mode 100644 index 000000000..8b7d685f8 --- /dev/null +++ b/tests/stash/stash_helpers.c @@ -0,0 +1,56 @@ +#include "clar_libgit2.h" +#include "fileops.h" +#include "stash_helpers.h" + +void setup_stash(git_repository *repo, git_signature *signature) +{ + git_index *index; + + cl_git_pass(git_repository_index(&index, repo)); + + cl_git_mkfile("stash/what", "hello\n"); /* ce013625030ba8dba906f756967f9e9ca394464a */ + cl_git_mkfile("stash/how", "small\n"); /* ac790413e2d7a26c3767e78c57bb28716686eebc */ + cl_git_mkfile("stash/who", "world\n"); /* cc628ccd10742baea8241c5924df992b5c019f71 */ + cl_git_mkfile("stash/when", "now\n"); /* b6ed15e81e2593d7bb6265eb4a991d29dc3e628b */ + cl_git_mkfile("stash/just.ignore", "me\n"); /* 78925fb1236b98b37a35e9723033e627f97aa88b */ + + cl_git_mkfile("stash/.gitignore", "*.ignore\n"); + + cl_git_pass(git_index_add_bypath(index, "what")); + cl_git_pass(git_index_add_bypath(index, "how")); + cl_git_pass(git_index_add_bypath(index, "who")); + cl_git_pass(git_index_add_bypath(index, ".gitignore")); + + cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit"); + + cl_git_rewritefile("stash/what", "goodbye\n"); /* dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 */ + cl_git_rewritefile("stash/how", "not so small and\n"); /* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */ + cl_git_rewritefile("stash/who", "funky world\n"); /* a0400d4954659306a976567af43125a0b1aa8595 */ + + cl_git_pass(git_index_add_bypath(index, "what")); + cl_git_pass(git_index_add_bypath(index, "how")); + cl_git_pass(git_index_write(index)); + + cl_git_rewritefile("stash/what", "see you later\n"); /* bc99dc98b3eba0e9157e94769cd4d49cb49de449 */ + + git_index_free(index); +} + +void assert_status( + git_repository *repo, + const char *path, + int status_flags) +{ + unsigned int status; + int error; + + error = git_status_file(&status, repo, path); + + if (status_flags < 0) { + cl_assert_equal_i(status_flags, error); + return; + } + + cl_assert_equal_i(0, error); + cl_assert_equal_i((unsigned int)status_flags, status); +} diff --git a/tests/stash/stash_helpers.h b/tests/stash/stash_helpers.h new file mode 100644 index 000000000..66d758fe2 --- /dev/null +++ b/tests/stash/stash_helpers.h @@ -0,0 +1,8 @@ +void setup_stash( + git_repository *repo, + git_signature *signature); + +void assert_status( + git_repository *repo, + const char *path, + int status_flags); diff --git a/tests/stash/submodules.c b/tests/stash/submodules.c new file mode 100644 index 000000000..137c4408c --- /dev/null +++ b/tests/stash/submodules.c @@ -0,0 +1,80 @@ +#include "clar_libgit2.h" +#include "stash_helpers.h" +#include "../submodule/submodule_helpers.h" + +static git_repository *repo; +static git_signature *signature; +static git_oid stash_tip_oid; + +static git_submodule *sm; + +void test_stash_submodules__initialize(void) +{ + cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */ + + repo = setup_fixture_submodules(); + + cl_git_pass(git_submodule_lookup(&sm, repo, "testrepo")); +} + +void test_stash_submodules__cleanup(void) +{ + git_signature_free(signature); + signature = NULL; +} + +void test_stash_submodules__does_not_stash_modified_submodules(void) +{ + static git_index *smindex; + static git_repository *smrepo; + + assert_status(repo, "modified", GIT_STATUS_WT_MODIFIED); + + /* modify file in submodule */ + cl_git_rewritefile("submodules/testrepo/README", "heyheyhey"); + assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED); + + /* add file to index in submodule */ + cl_git_pass(git_submodule_open(&smrepo, sm)); + cl_git_pass(git_repository_index(&smindex, smrepo)); + cl_git_pass(git_index_add_bypath(smindex, "README")); + + /* commit changed index of submodule */ + cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Modify it"); + assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED); + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + + assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED); + assert_status(repo, "modified", GIT_STATUS_CURRENT); + + git_index_free(smindex); + git_repository_free(smrepo); +} + +void test_stash_submodules__stash_is_empty_with_modified_submodules(void) +{ + static git_index *smindex; + static git_repository *smrepo; + + cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT)); + assert_status(repo, "modified", GIT_STATUS_CURRENT); + + /* modify file in submodule */ + cl_git_rewritefile("submodules/testrepo/README", "heyheyhey"); + assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED); + + /* add file to index in submodule */ + cl_git_pass(git_submodule_open(&smrepo, sm)); + cl_git_pass(git_repository_index(&smindex, smrepo)); + cl_git_pass(git_index_add_bypath(smindex, "README")); + + /* commit changed index of submodule */ + cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Modify it"); + assert_status(repo, "testrepo", GIT_STATUS_WT_MODIFIED); + + cl_git_fail_with(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT), GIT_ENOTFOUND); + + git_index_free(smindex); + git_repository_free(smrepo); +} |