python_code
stringlengths 0
1.8M
| repo_name
stringclasses 7
values | file_path
stringlengths 5
99
|
---|---|---|
#include "clar.h"
#include "clar_libgit2.h"
#include "commit.h"
#include "diff.h"
#include "diff_generate.h"
static git_repository *repo;
void test_diff_format_email__initialize(void)
{
repo = cl_git_sandbox_init("diff_format_email");
}
void test_diff_format_email__cleanup(void)
{
cl_git_sandbox_cleanup();
}
#ifndef GIT_DEPRECATE_HARD
static void assert_email_match(
const char *expected,
const char *oidstr,
git_diff_format_email_options *opts)
{
git_oid oid;
git_commit *commit = NULL;
git_diff *diff = NULL;
git_buf buf = GIT_BUF_INIT;
git_oid__fromstr(&oid, oidstr, GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
opts->id = git_commit_id(commit);
opts->author = git_commit_author(commit);
if (!opts->summary)
opts->summary = git_commit_summary(commit);
cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
cl_git_pass(git_diff_format_email(&buf, diff, opts));
cl_assert_equal_s(expected, buf.ptr);
git_buf_dispose(&buf);
cl_git_pass(git_diff_commit_as_email(
&buf, repo, commit, 1, 1, opts->flags, NULL));
cl_assert_equal_s(expected, buf.ptr);
git_diff_free(diff);
git_commit_free(commit);
git_buf_dispose(&buf);
}
#endif
void test_diff_format_email__simple(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email =
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
"Subject: [PATCH] Modify some content\n" \
"\n" \
"---\n" \
" file1.txt | 8 +++++---\n" \
" 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
"\n" \
"diff --git a/file1.txt b/file1.txt\n" \
"index 94aaae8..af8f41d 100644\n" \
"--- a/file1.txt\n" \
"+++ b/file1.txt\n" \
"@@ -1,15 +1,17 @@\n" \
" file1.txt\n" \
" file1.txt\n" \
"+_file1.txt_\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
"+\n" \
"+\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"+_file1.txt_\n" \
"+_file1.txt_\n" \
" file1.txt\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
assert_email_match(
email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
#endif
}
void test_diff_format_email__with_message(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
"From: Patrick Steinhardt <[email protected]>\n" \
"Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
"Subject: [PATCH] Modify content with message\n" \
"\n" \
"Modify content of file3.txt by appending a new line. Make this\n" \
"commit message somewhat longer to test behavior with newlines\n" \
"embedded in the message body.\n" \
"\n" \
"Also test if new paragraphs are included correctly.\n" \
"---\n" \
" file3.txt | 1 +\n" \
" 1 file changed, 1 insertion(+)\n" \
"\n" \
"diff --git a/file3.txt b/file3.txt\n" \
"index 9a2d780..7309653 100644\n" \
"--- a/file3.txt\n" \
"+++ b/file3.txt\n" \
"@@ -3,3 +3,4 @@ file3!\n" \
" file3\n" \
" file3\n" \
" file3\n" \
"+file3\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
opts.body = "Modify content of file3.txt by appending a new line. Make this\n" \
"commit message somewhat longer to test behavior with newlines\n" \
"embedded in the message body.\n" \
"\n" \
"Also test if new paragraphs are included correctly.";
assert_email_match(
email, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270", &opts);
#endif
}
void test_diff_format_email__multiple(void)
{
#ifndef GIT_DEPRECATE_HARD
git_oid oid;
git_commit *commit = NULL;
git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
git_buf buf = GIT_BUF_INIT;
const char *email =
"From 10808fe9c9be5a190c0ba68d1a002233fb363508 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Thu, 10 Apr 2014 19:37:05 +0200\n" \
"Subject: [PATCH 1/2] Added file2.txt file3.txt\n" \
"\n" \
"---\n" \
" file2.txt | 5 +++++\n" \
" file3.txt | 5 +++++\n" \
" 2 files changed, 10 insertions(+)\n" \
" create mode 100644 file2.txt\n" \
" create mode 100644 file3.txt\n" \
"\n" \
"diff --git a/file2.txt b/file2.txt\n" \
"new file mode 100644\n" \
"index 0000000..e909123\n" \
"--- /dev/null\n" \
"+++ b/file2.txt\n" \
"@@ -0,0 +1,5 @@\n" \
"+file2\n" \
"+file2\n" \
"+file2\n" \
"+file2\n" \
"+file2\n" \
"diff --git a/file3.txt b/file3.txt\n" \
"new file mode 100644\n" \
"index 0000000..9435022\n" \
"--- /dev/null\n" \
"+++ b/file3.txt\n" \
"@@ -0,0 +1,5 @@\n" \
"+file3\n" \
"+file3\n" \
"+file3\n" \
"+file3\n" \
"+file3\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n" \
"From 873806f6f27e631eb0b23e4b56bea2bfac14a373 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Thu, 10 Apr 2014 19:37:36 +0200\n" \
"Subject: [PATCH 2/2] Modified file2.txt, file3.txt\n" \
"\n" \
"---\n" \
" file2.txt | 2 +-\n" \
" file3.txt | 2 +-\n" \
" 2 files changed, 2 insertions(+), 2 deletions(-)\n" \
"\n" \
"diff --git a/file2.txt b/file2.txt\n" \
"index e909123..7aff11d 100644\n" \
"--- a/file2.txt\n" \
"+++ b/file2.txt\n" \
"@@ -1,5 +1,5 @@\n" \
" file2\n" \
" file2\n" \
" file2\n" \
"-file2\n" \
"+file2!\n" \
" file2\n" \
"diff --git a/file3.txt b/file3.txt\n" \
"index 9435022..9a2d780 100644\n" \
"--- a/file3.txt\n" \
"+++ b/file3.txt\n" \
"@@ -1,5 +1,5 @@\n" \
" file3\n" \
"-file3\n" \
"+file3!\n" \
" file3\n" \
" file3\n" \
" file3\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
git_oid__fromstr(&oid, "10808fe9c9be5a190c0ba68d1a002233fb363508", GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
opts.id = git_commit_id(commit);
opts.author = git_commit_author(commit);
opts.summary = git_commit_summary(commit);
opts.patch_no = 1;
opts.total_patches = 2;
cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
cl_git_pass(git_diff_format_email(&buf, diff, &opts));
git_diff_free(diff);
git_commit_free(commit);
diff = NULL;
commit = NULL;
git_oid__fromstr(&oid, "873806f6f27e631eb0b23e4b56bea2bfac14a373", GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
opts.id = git_commit_id(commit);
opts.author = git_commit_author(commit);
opts.summary = git_commit_summary(commit);
opts.patch_no = 2;
opts.total_patches = 2;
cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
cl_git_pass(git_diff_format_email(&buf, diff, &opts));
cl_assert_equal_s(email, buf.ptr);
git_diff_free(diff);
git_commit_free(commit);
git_buf_dispose(&buf);
#endif
}
void test_diff_format_email__exclude_marker(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email =
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
"Subject: Modify some content\n" \
"\n" \
"---\n" \
" file1.txt | 8 +++++---\n" \
" 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
"\n" \
"diff --git a/file1.txt b/file1.txt\n" \
"index 94aaae8..af8f41d 100644\n" \
"--- a/file1.txt\n" \
"+++ b/file1.txt\n" \
"@@ -1,15 +1,17 @@\n" \
" file1.txt\n" \
" file1.txt\n" \
"+_file1.txt_\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
"+\n" \
"+\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"+_file1.txt_\n" \
"+_file1.txt_\n" \
" file1.txt\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
opts.flags |= GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER;
assert_email_match(
email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
#endif
}
void test_diff_format_email__invalid_no(void)
{
#ifndef GIT_DEPRECATE_HARD
git_oid oid;
git_commit *commit = NULL;
git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
git_buf buf = GIT_BUF_INIT;
git_oid__fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
opts.id = git_commit_id(commit);
opts.author = git_commit_author(commit);
opts.summary = git_commit_summary(commit);
opts.patch_no = 2;
opts.total_patches = 1;
cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
cl_git_fail(git_diff_format_email(&buf, diff, &opts));
cl_git_fail(git_diff_commit_as_email(&buf, repo, commit, 2, 1, 0, NULL));
git_diff_free(diff);
git_commit_free(commit);
git_buf_dispose(&buf);
#endif
}
void test_diff_format_email__mode_change(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email =
"From 7ade76dd34bba4733cf9878079f9fd4a456a9189 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Thu, 10 Apr 2014 10:05:03 +0200\n" \
"Subject: [PATCH] Update permissions\n" \
"\n" \
"---\n" \
" file1.txt.renamed | 0\n" \
" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
" mode change 100644 => 100755 file1.txt.renamed\n" \
"\n" \
"diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
"old mode 100644\n" \
"new mode 100755\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
assert_email_match(
email, "7ade76dd34bba4733cf9878079f9fd4a456a9189", &opts);
#endif
}
void test_diff_format_email__rename_add_remove(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email =
"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
"Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
"\n" \
"---\n" \
" file1.txt | 17 -----------------\n" \
" file1.txt.renamed | 17 +++++++++++++++++\n" \
" 2 files changed, 17 insertions(+), 17 deletions(-)\n" \
" delete mode 100644 file1.txt\n" \
" create mode 100644 file1.txt.renamed\n" \
"\n" \
"diff --git a/file1.txt b/file1.txt\n" \
"deleted file mode 100644\n" \
"index af8f41d..0000000\n" \
"--- a/file1.txt\n" \
"+++ /dev/null\n" \
"@@ -1,17 +0,0 @@\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-_file1.txt_\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-\n" \
"-\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-_file1.txt_\n" \
"-_file1.txt_\n" \
"-file1.txt\n" \
"diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
"new file mode 100644\n" \
"index 0000000..a97157a\n" \
"--- /dev/null\n" \
"+++ b/file1.txt.renamed\n" \
"@@ -0,0 +1,17 @@\n" \
"+file1.txt\n" \
"+file1.txt\n" \
"+_file1.txt_\n" \
"+file1.txt\n" \
"+file1.txt\n" \
"+file1.txt_renamed\n" \
"+file1.txt\n" \
"+\n" \
"+\n" \
"+file1.txt\n" \
"+file1.txt\n" \
"+file1.txt_renamed\n" \
"+file1.txt\n" \
"+file1.txt\n" \
"+_file1.txt_\n" \
"+_file1.txt_\n" \
"+file1.txt\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
assert_email_match(
email, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
#endif
}
void test_diff_format_email__multiline_summary(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email =
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
"Subject: [PATCH] Modify some content\n" \
"\n" \
"---\n" \
" file1.txt | 8 +++++---\n" \
" 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
"\n" \
"diff --git a/file1.txt b/file1.txt\n" \
"index 94aaae8..af8f41d 100644\n" \
"--- a/file1.txt\n" \
"+++ b/file1.txt\n" \
"@@ -1,15 +1,17 @@\n" \
" file1.txt\n" \
" file1.txt\n" \
"+_file1.txt_\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
"+\n" \
"+\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
" file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"-file1.txt\n" \
"+_file1.txt_\n" \
"+_file1.txt_\n" \
" file1.txt\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
opts.summary = "Modify some content\nSome extra stuff here";
assert_email_match(
email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
#endif
}
void test_diff_format_email__binary(void)
{
#ifndef GIT_DEPRECATE_HARD
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email =
"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <[email protected]>\n" \
"Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
"Subject: [PATCH] Modified binary file\n" \
"\n" \
"---\n" \
" binary.bin | Bin 3 -> 5 bytes\n" \
" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
"\n" \
"diff --git a/binary.bin b/binary.bin\n" \
"index bd474b2..9ac35ff 100644\n" \
"Binary files a/binary.bin and b/binary.bin differ\n" \
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
opts.summary = "Modified binary file";
assert_email_match(
email, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
#endif
}
| libgit2-main | tests/libgit2/diff/format_email.c |
#include "clar_libgit2.h"
#include "patch.h"
#include "patch_parse.h"
#include "diff_helpers.h"
#include "../patch/patch_common.h"
void test_diff_parse__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_diff_parse__nonpatches_fail_with_notfound(void)
{
git_diff *diff;
const char *not = PATCH_NOT_A_PATCH;
const char *not_with_leading = "Leading text.\n" PATCH_NOT_A_PATCH;
const char *not_with_trailing = PATCH_NOT_A_PATCH "Trailing text.\n";
const char *not_with_both = "Lead.\n" PATCH_NOT_A_PATCH "Trail.\n";
cl_git_fail_with(GIT_ENOTFOUND,
git_diff_from_buffer(&diff,
not,
strlen(not)));
cl_git_fail_with(GIT_ENOTFOUND,
git_diff_from_buffer(&diff,
not_with_leading,
strlen(not_with_leading)));
cl_git_fail_with(GIT_ENOTFOUND,
git_diff_from_buffer(&diff,
not_with_trailing,
strlen(not_with_trailing)));
cl_git_fail_with(GIT_ENOTFOUND,
git_diff_from_buffer(&diff,
not_with_both,
strlen(not_with_both)));
}
static void test_parse_invalid_diff(const char *invalid_diff)
{
git_diff *diff;
git_str buf = GIT_STR_INIT;
/* throw some random (legitimate) diffs in with the given invalid
* one.
*/
git_str_puts(&buf, PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE);
git_str_puts(&buf, PATCH_BINARY_DELTA);
git_str_puts(&buf, invalid_diff);
git_str_puts(&buf, PATCH_ORIGINAL_TO_CHANGE_MIDDLE);
git_str_puts(&buf, PATCH_BINARY_LITERAL);
cl_git_fail_with(GIT_ERROR,
git_diff_from_buffer(&diff, buf.ptr, buf.size));
git_str_dispose(&buf);
}
void test_diff_parse__exact_rename(void)
{
const char *content =
"---\n"
" old_name.c => new_name.c | 0\n"
" 1 file changed, 0 insertions(+), 0 deletions(-)\n"
" rename old_name.c => new_name.c (100%)\n"
"\n"
"diff --git a/old_name.c b/new_name.c\n"
"similarity index 100%\n"
"rename from old_name.c\n"
"rename to new_name.c\n"
"-- \n"
"2.9.3\n";
git_diff *diff;
cl_git_pass(git_diff_from_buffer(
&diff, content, strlen(content)));
git_diff_free(diff);
}
void test_diff_parse__empty_file(void)
{
const char *content =
"---\n"
" file | 0\n"
" 1 file changed, 0 insertions(+), 0 deletions(-)\n"
" created mode 100644 file\n"
"\n"
"diff --git a/file b/file\n"
"new file mode 100644\n"
"index 0000000..e69de29\n"
"-- \n"
"2.20.1\n";
git_diff *diff;
cl_git_pass(git_diff_from_buffer(
&diff, content, strlen(content)));
git_diff_free(diff);
}
void test_diff_parse__no_extended_headers(void)
{
const char *content = PATCH_NO_EXTENDED_HEADERS;
git_diff *diff;
cl_git_pass(git_diff_from_buffer(
&diff, content, strlen(content)));
git_diff_free(diff);
}
void test_diff_parse__add_delete_no_index(void)
{
const char *content =
"diff --git a/file.txt b/file.txt\n"
"new file mode 100644\n"
"--- /dev/null\n"
"+++ b/file.txt\n"
"@@ -0,0 +1,2 @@\n"
"+one\n"
"+two\n"
"diff --git a/otherfile.txt b/otherfile.txt\n"
"deleted file mode 100644\n"
"--- a/otherfile.txt\n"
"+++ /dev/null\n"
"@@ -1,1 +0,0 @@\n"
"-three\n";
git_diff *diff;
cl_git_pass(git_diff_from_buffer(
&diff, content, strlen(content)));
git_diff_free(diff);
}
void test_diff_parse__invalid_patches_fails(void)
{
test_parse_invalid_diff(PATCH_CORRUPT_MISSING_NEW_FILE);
test_parse_invalid_diff(PATCH_CORRUPT_MISSING_OLD_FILE);
test_parse_invalid_diff(PATCH_CORRUPT_NO_CHANGES);
test_parse_invalid_diff(PATCH_CORRUPT_MISSING_HUNK_HEADER);
}
static void test_tree_to_tree_computed_to_parsed(
const char *sandbox, const char *a_id, const char *b_id,
uint32_t diff_flags, uint32_t find_flags)
{
git_repository *repo;
git_diff *computed, *parsed;
git_tree *a, *b;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
git_buf computed_buf = GIT_BUF_INIT;
repo = cl_git_sandbox_init(sandbox);
opts.id_abbrev = GIT_OID_SHA1_HEXSIZE;
opts.flags = GIT_DIFF_SHOW_BINARY | diff_flags;
findopts.flags = find_flags;
cl_assert((a = resolve_commit_oid_to_tree(repo, a_id)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(repo, b_id)) != NULL);
cl_git_pass(git_diff_tree_to_tree(&computed, repo, a, b, &opts));
if (find_flags)
cl_git_pass(git_diff_find_similar(computed, &findopts));
cl_git_pass(git_diff_to_buf(&computed_buf,
computed, GIT_DIFF_FORMAT_PATCH));
cl_git_pass(git_diff_from_buffer(&parsed,
computed_buf.ptr, computed_buf.size));
diff_assert_equal(computed, parsed);
git_tree_free(a);
git_tree_free(b);
git_diff_free(computed);
git_diff_free(parsed);
git_buf_dispose(&computed_buf);
cl_git_sandbox_cleanup();
}
void test_diff_parse__can_parse_generated_diff(void)
{
test_tree_to_tree_computed_to_parsed(
"diff", "d70d245e", "7a9e0b02", 0, 0);
test_tree_to_tree_computed_to_parsed(
"unsymlinked.git", "806999", "a8595c", 0, 0);
test_tree_to_tree_computed_to_parsed("diff",
"d70d245ed97ed2aa596dd1af6536e4bfdb047b69",
"7a9e0b02e63179929fed24f0a3e0f19168114d10", 0, 0);
test_tree_to_tree_computed_to_parsed(
"unsymlinked.git", "7fccd7", "806999", 0, 0);
test_tree_to_tree_computed_to_parsed(
"unsymlinked.git", "7fccd7", "a8595c", 0, 0);
test_tree_to_tree_computed_to_parsed(
"attr", "605812a", "370fe9ec22", 0, 0);
test_tree_to_tree_computed_to_parsed(
"attr", "f5b0af1fb4f5c", "370fe9ec22", 0, 0);
test_tree_to_tree_computed_to_parsed(
"diff", "d70d245e", "d70d245e", 0, 0);
test_tree_to_tree_computed_to_parsed("diff_format_email",
"873806f6f27e631eb0b23e4b56bea2bfac14a373",
"897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
GIT_DIFF_SHOW_BINARY, 0);
test_tree_to_tree_computed_to_parsed("diff_format_email",
"897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
"873806f6f27e631eb0b23e4b56bea2bfac14a373",
GIT_DIFF_SHOW_BINARY, 0);
test_tree_to_tree_computed_to_parsed("renames",
"31e47d8c1fa36d7f8d537b96158e3f024de0a9f2",
"2bc7f351d20b53f1c72c16c4b036e491c478c49a",
0, GIT_DIFF_FIND_RENAMES);
test_tree_to_tree_computed_to_parsed("renames",
"31e47d8c1fa36d7f8d537b96158e3f024de0a9f2",
"2bc7f351d20b53f1c72c16c4b036e491c478c49a",
GIT_DIFF_INCLUDE_UNMODIFIED,
0);
test_tree_to_tree_computed_to_parsed("renames",
"31e47d8c1fa36d7f8d537b96158e3f024de0a9f2",
"2bc7f351d20b53f1c72c16c4b036e491c478c49a",
GIT_DIFF_INCLUDE_UNMODIFIED,
GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED | GIT_DIFF_FIND_EXACT_MATCH_ONLY);
}
void test_diff_parse__get_patch_from_diff(void)
{
git_repository *repo;
git_diff *computed, *parsed;
git_tree *a, *b;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_buf computed_buf = GIT_BUF_INIT;
git_patch *patch_computed, *patch_parsed;
repo = cl_git_sandbox_init("diff");
opts.flags = GIT_DIFF_SHOW_BINARY;
cl_assert((a = resolve_commit_oid_to_tree(repo,
"d70d245ed97ed2aa596dd1af6536e4bfdb047b69")) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(repo,
"7a9e0b02e63179929fed24f0a3e0f19168114d10")) != NULL);
cl_git_pass(git_diff_tree_to_tree(&computed, repo, a, b, &opts));
cl_git_pass(git_diff_to_buf(&computed_buf,
computed, GIT_DIFF_FORMAT_PATCH));
cl_git_pass(git_patch_from_diff(&patch_computed, computed, 0));
cl_git_pass(git_diff_from_buffer(&parsed,
computed_buf.ptr, computed_buf.size));
cl_git_pass(git_patch_from_diff(&patch_parsed, parsed, 0));
cl_assert_equal_i(
git_patch_num_hunks(patch_computed),
git_patch_num_hunks(patch_parsed));
git_patch_free(patch_computed);
git_patch_free(patch_parsed);
git_tree_free(a);
git_tree_free(b);
git_diff_free(computed);
git_diff_free(parsed);
git_buf_dispose(&computed_buf);
cl_git_sandbox_cleanup();
}
static int file_cb(const git_diff_delta *delta, float progress, void *payload)
{
int *called = (int *) payload;
GIT_UNUSED(delta);
GIT_UNUSED(progress);
(*called)++;
return 0;
}
void test_diff_parse__foreach_works_with_parsed_patch(void)
{
const char patch[] =
"diff --git a/obj1 b/obj2\n"
"index 1234567..7654321 10644\n"
"--- a/obj1\n"
"+++ b/obj2\n"
"@@ -1 +1 @@\n"
"-abcde\n"
"+12345\n";
int called = 0;
git_diff *diff;
cl_git_pass(git_diff_from_buffer(&diff, patch, strlen(patch)));
cl_git_pass(git_diff_foreach(diff, file_cb, NULL, NULL, NULL, &called));
cl_assert_equal_i(called, 1);
git_diff_free(diff);
}
void test_diff_parse__parsing_minimal_patch_succeeds(void)
{
const char patch[] =
"diff --git a/obj1 b/obj2\n"
"index 1234567..7654321 10644\n"
"--- a/obj1\n"
"+++ b/obj2\n"
"@@ -1 +1 @@\n"
"-a\n"
"+\n";
git_buf buf = GIT_BUF_INIT;
git_diff *diff;
cl_git_pass(git_diff_from_buffer(&diff, patch, strlen(patch)));
cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
cl_assert_equal_s(patch, buf.ptr);
git_diff_free(diff);
git_buf_dispose(&buf);
}
void test_diff_parse__patch_roundtrip_succeeds(void)
{
const char buf1[] = "a\n", buf2[] = "b\n";
git_buf patchbuf = GIT_BUF_INIT, diffbuf = GIT_BUF_INIT;
git_patch *patch;
git_diff *diff;
cl_git_pass(git_patch_from_buffers(&patch, buf1, strlen(buf1), "obj1", buf2, strlen(buf2), "obj2", NULL));
cl_git_pass(git_patch_to_buf(&patchbuf, patch));
cl_git_pass(git_diff_from_buffer(&diff, patchbuf.ptr, patchbuf.size));
cl_git_pass(git_diff_to_buf(&diffbuf, diff, GIT_DIFF_FORMAT_PATCH));
cl_assert_equal_s(patchbuf.ptr, diffbuf.ptr);
git_patch_free(patch);
git_diff_free(diff);
git_buf_dispose(&patchbuf);
git_buf_dispose(&diffbuf);
}
#define cl_assert_equal_i_src(i1,i2,file,func,line) clar__assert_equal(file,func,line,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
static void cl_git_assert_lineinfo_(int old_lineno, int new_lineno, int num_lines, git_patch *patch, size_t hunk_idx, size_t line_idx, const char *file, const char *func, int lineno)
{
const git_diff_line *line;
cl_git_expect(git_patch_get_line_in_hunk(&line, patch, hunk_idx, line_idx), 0, file, func, lineno);
cl_assert_equal_i_src(old_lineno, line->old_lineno, file, func, lineno);
cl_assert_equal_i_src(new_lineno, line->new_lineno, file, func, lineno);
cl_assert_equal_i_src(num_lines, line->num_lines, file, func, lineno);
}
#define cl_git_assert_lineinfo(old, new, num, p, h, l) \
cl_git_assert_lineinfo_(old,new,num,p,h,l,__FILE__,__func__,__LINE__)
void test_diff_parse__issue4672(void)
{
const char *text = "diff --git a/a b/a\n"
"index 7f129fd..af431f2 100644\n"
"--- a/a\n"
"+++ b/a\n"
"@@ -3 +3 @@\n"
"-a contents 2\n"
"+a contents\n";
git_diff *diff;
git_patch *patch;
const git_diff_hunk *hunk;
size_t n, l = 0;
cl_git_pass(git_diff_from_buffer(&diff, text, strlen(text)));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_get_hunk(&hunk, &n, patch, 0));
cl_git_assert_lineinfo(3, -1, 1, patch, 0, l++);
cl_git_assert_lineinfo(-1, 3, 1, patch, 0, l++);
cl_assert_equal_i(n, l);
git_patch_free(patch);
git_diff_free(diff);
}
void test_diff_parse__lineinfo(void)
{
const char *text = PATCH_ORIGINAL_TO_CHANGE_MIDDLE;
git_diff *diff;
git_patch *patch;
const git_diff_hunk *hunk;
size_t n, l = 0;
cl_git_pass(git_diff_from_buffer(&diff, text, strlen(text)));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_get_hunk(&hunk, &n, patch, 0));
cl_git_assert_lineinfo(3, 3, 1, patch, 0, l++);
cl_git_assert_lineinfo(4, 4, 1, patch, 0, l++);
cl_git_assert_lineinfo(5, 5, 1, patch, 0, l++);
cl_git_assert_lineinfo(6, -1, 1, patch, 0, l++);
cl_git_assert_lineinfo(-1, 6, 1, patch, 0, l++);
cl_git_assert_lineinfo(7, 7, 1, patch, 0, l++);
cl_git_assert_lineinfo(8, 8, 1, patch, 0, l++);
cl_git_assert_lineinfo(9, 9, 1, patch, 0, l++);
cl_assert_equal_i(n, l);
git_patch_free(patch);
git_diff_free(diff);
}
void test_diff_parse__new_file_with_space(void)
{
const char *content = PATCH_ORIGINAL_NEW_FILE_WITH_SPACE;
git_patch *patch;
git_diff *diff;
cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
cl_git_pass(git_patch_from_diff((git_patch **) &patch, diff, 0));
cl_assert_equal_p(patch->diff_opts.old_prefix, NULL);
cl_assert_equal_p(patch->delta->old_file.path, NULL);
cl_assert_equal_s(patch->diff_opts.new_prefix, "b/");
cl_assert_equal_s(patch->delta->new_file.path, "sp ace.txt");
git_patch_free(patch);
git_diff_free(diff);
}
void test_diff_parse__new_file_with_space_and_regenerate_patch(void)
{
const char *content = PATCH_ORIGINAL_NEW_FILE_WITH_SPACE;
git_diff *diff = NULL;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
git_buf_dispose(&buf);
git_diff_free(diff);
}
void test_diff_parse__delete_file_with_space_and_regenerate_patch(void)
{
const char *content = PATCH_DELETE_FILE_WITH_SPACE;
git_diff *diff = NULL;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
git_buf_dispose(&buf);
git_diff_free(diff);
}
void test_diff_parse__crlf(void)
{
const char *text = PATCH_CRLF;
git_diff *diff;
git_patch *patch;
const git_diff_delta *delta;
cl_git_pass(git_diff_from_buffer(&diff, text, strlen(text)));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
delta = git_patch_get_delta(patch);
cl_assert_equal_s(delta->old_file.path, "test-file");
cl_assert_equal_s(delta->new_file.path, "test-file");
git_patch_free(patch);
git_diff_free(diff);
}
| libgit2-main | tests/libgit2/diff/parse.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
#define BLOB_DIFF \
"diff --git a/file b/file\n" \
"index 45141a7..4d713dc 100644\n" \
"--- a/file\n" \
"+++ b/file\n" \
"@@ -1 +1,6 @@\n" \
" Hello from the root\n" \
"+\n" \
"+Some additional lines\n" \
"+\n" \
"+Down here below\n" \
"+\n"
static git_repository *g_repo = NULL;
static diff_expects expected;
static git_diff_options opts;
static git_blob *d, *alien;
static void quick_diff_blob_to_str(
const git_blob *blob, const char *blob_path,
const char *str, size_t len, const char *str_path)
{
memset(&expected, 0, sizeof(expected));
if (str && !len)
len = strlen(str);
cl_git_pass(git_diff_blob_to_buffer(
blob, blob_path, str, len, str_path,
&opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
}
void test_diff_blob__initialize(void)
{
git_oid oid;
g_repo = cl_git_sandbox_init("attr");
cl_git_pass(git_diff_options_init(&opts, GIT_DIFF_OPTIONS_VERSION));
opts.context_lines = 1;
memset(&expected, 0, sizeof(expected));
/* tests/resources/attr/root_test4.txt */
cl_git_pass(git_oid__fromstrn(&oid, "a0f7217a", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 8));
/* alien.png */
cl_git_pass(git_oid__fromstrn(&oid, "edf3dcee", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 8));
}
void test_diff_blob__cleanup(void)
{
git_blob_free(d);
d = NULL;
git_blob_free(alien);
alien = NULL;
cl_git_sandbox_cleanup();
}
static void assert_one_modified(
int hunks, int lines, int ctxt, int adds, int dels, diff_expects *exp)
{
cl_assert_equal_i(1, exp->files);
cl_assert_equal_i(1, exp->file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp->files_binary);
cl_assert_equal_i(hunks, exp->hunks);
cl_assert_equal_i(lines, exp->lines);
cl_assert_equal_i(ctxt, exp->line_ctxt);
cl_assert_equal_i(adds, exp->line_adds);
cl_assert_equal_i(dels, exp->line_dels);
}
void test_diff_blob__patch_with_freed_blobs(void)
{
git_oid a_oid, b_oid;
git_blob *a, *b;
git_patch *p;
git_buf buf = GIT_BUF_INIT;
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid__fromstrn(&a_oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
/* tests/resources/attr/root_test2 */
cl_git_pass(git_oid__fromstrn(&b_oid, "4d713dc4", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, NULL));
git_blob_free(a);
git_blob_free(b);
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(buf.ptr, BLOB_DIFF);
git_patch_free(p);
git_buf_dispose(&buf);
}
void test_diff_blob__can_compare_text_blobs(void)
{
git_blob *a, *b, *c;
git_oid a_oid, b_oid, c_oid;
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid__fromstrn(&a_oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));
/* tests/resources/attr/root_test2 */
cl_git_pass(git_oid__fromstrn(&b_oid, "4d713dc4", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));
/* tests/resources/attr/root_test3 */
cl_git_pass(git_oid__fromstrn(&c_oid, "c96bbb2c2557a832", 16, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));
/* Doing the equivalent of a `git diff -U1` on these files */
/* diff on tests/resources/attr/root_test1 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
a, NULL, b, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(1, 6, 1, 5, 0, &expected);
/* same diff but use direct buffers */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_buffers(
git_blob_rawcontent(a), (size_t)git_blob_rawsize(a), NULL,
git_blob_rawcontent(b), (size_t)git_blob_rawsize(b), NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(1, 6, 1, 5, 0, &expected);
/* diff on tests/resources/attr/root_test2 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
b, NULL, c, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(1, 15, 3, 9, 3, &expected);
/* diff on tests/resources/attr/root_test3 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
a, NULL, c, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(1, 13, 0, 12, 1, &expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
c, NULL, d, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(2, 14, 4, 6, 4, &expected);
git_blob_free(a);
git_blob_free(b);
git_blob_free(c);
}
static void assert_patch_matches_blobs(
git_patch *p, git_blob *a, git_blob *b,
int hunks, int l0, int l1, int ctxt, int adds, int dels)
{
const git_diff_delta *delta;
size_t tc, ta, td;
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
cl_assert_equal_oid(git_blob_id(a), &delta->old_file.id);
cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size);
cl_assert_equal_oid(git_blob_id(b), &delta->new_file.id);
cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size);
cl_assert_equal_i(hunks, (int)git_patch_num_hunks(p));
if (hunks > 0)
cl_assert_equal_i(l0, git_patch_num_lines_in_hunk(p, 0));
if (hunks > 1)
cl_assert_equal_i(l1, git_patch_num_lines_in_hunk(p, 1));
cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
cl_assert_equal_i(ctxt, (int)tc);
cl_assert_equal_i(adds, (int)ta);
cl_assert_equal_i(dels, (int)td);
}
void test_diff_blob__can_compare_text_blobs_with_patch(void)
{
git_blob *a, *b, *c;
git_oid a_oid, b_oid, c_oid;
git_patch *p;
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid__fromstrn(&a_oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
/* tests/resources/attr/root_test2 */
cl_git_pass(git_oid__fromstrn(&b_oid, "4d713dc4", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 8));
/* tests/resources/attr/root_test3 */
cl_git_pass(git_oid__fromstrn(&c_oid, "c96bbb2c2557a832", 16, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));
/* Doing the equivalent of a `git diff -U1` on these files */
/* diff on tests/resources/attr/root_test1 */
cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts));
assert_patch_matches_blobs(p, a, b, 1, 6, 0, 1, 5, 0);
git_patch_free(p);
/* diff on tests/resources/attr/root_test2 */
cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts));
assert_patch_matches_blobs(p, b, c, 1, 15, 0, 3, 9, 3);
git_patch_free(p);
/* diff on tests/resources/attr/root_test3 */
cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts));
assert_patch_matches_blobs(p, a, c, 1, 13, 0, 0, 12, 1);
git_patch_free(p);
/* one more */
cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts));
assert_patch_matches_blobs(p, c, d, 2, 5, 9, 4, 6, 4);
git_patch_free(p);
git_blob_free(a);
git_blob_free(b);
git_blob_free(c);
}
void test_diff_blob__can_compare_against_null_blobs(void)
{
git_blob *e = NULL;
cl_git_pass(git_diff_blobs(
d, NULL, e, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(14, expected.hunk_old_lines);
cl_assert_equal_i(14, expected.lines);
cl_assert_equal_i(14, expected.line_dels);
opts.flags |= GIT_DIFF_REVERSE;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
d, NULL, e, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expected.files_binary);
cl_assert_equal_i(1, expected.hunks);
cl_assert_equal_i(14, expected.hunk_new_lines);
cl_assert_equal_i(14, expected.lines);
cl_assert_equal_i(14, expected.line_adds);
opts.flags ^= GIT_DIFF_REVERSE;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
alien, NULL, NULL, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.files_binary);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, expected.hunks);
cl_assert_equal_i(0, expected.lines);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
NULL, NULL, alien, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.files_binary);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expected.hunks);
cl_assert_equal_i(0, expected.lines);
}
void test_diff_blob__can_compare_against_null_blobs_with_patch(void)
{
git_blob *e = NULL;
git_patch *p;
const git_diff_delta *delta;
const git_diff_line *line;
int l, max_l;
cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts));
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
cl_assert_equal_oid(git_blob_id(d), &delta->old_file.id);
cl_assert_equal_sz(git_blob_rawsize(d), delta->old_file.size);
cl_assert(git_oid_is_zero(&delta->new_file.id));
cl_assert_equal_sz(0, delta->new_file.size);
cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0));
max_l = git_patch_num_lines_in_hunk(p, 0);
for (l = 0; l < max_l; ++l) {
cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l));
cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin);
}
git_patch_free(p);
opts.flags |= GIT_DIFF_REVERSE;
cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts));
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_ADDED, delta->status);
cl_assert(git_oid_is_zero(&delta->old_file.id));
cl_assert_equal_sz(0, delta->old_file.size);
cl_assert_equal_oid(git_blob_id(d), &delta->new_file.id);
cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size);
cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0));
max_l = git_patch_num_lines_in_hunk(p, 0);
for (l = 0; l < max_l; ++l) {
cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l));
cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin);
}
git_patch_free(p);
opts.flags ^= GIT_DIFF_REVERSE;
cl_git_pass(git_patch_from_blobs(&p, alien, NULL, NULL, NULL, &opts));
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
git_patch_free(p);
cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, alien, NULL, &opts));
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_ADDED, delta->status);
cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
git_patch_free(p);
}
static void assert_identical_blobs_comparison(diff_expects *expected)
{
cl_assert_equal_i(1, expected->files);
cl_assert_equal_i(1, expected->file_status[GIT_DELTA_UNMODIFIED]);
cl_assert_equal_i(0, expected->hunks);
cl_assert_equal_i(0, expected->lines);
}
void test_diff_blob__can_compare_identical_blobs(void)
{
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
cl_git_pass(git_diff_blobs(
d, NULL, d, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(0, expected.files_binary);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
NULL, NULL, NULL, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(0, expected.files_binary);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
alien, NULL, alien, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_identical_blobs_comparison(&expected);
cl_assert(expected.files_binary > 0);
}
void test_diff_blob__can_compare_identical_blobs_with_patch(void)
{
git_patch *p;
const git_diff_delta *delta;
cl_git_pass(git_patch_from_blobs(&p, d, NULL, d, NULL, &opts));
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status);
cl_assert_equal_sz(delta->old_file.size, git_blob_rawsize(d));
cl_assert_equal_oid(git_blob_id(d), &delta->old_file.id);
cl_assert_equal_sz(delta->new_file.size, git_blob_rawsize(d));
cl_assert_equal_oid(git_blob_id(d), &delta->new_file.id);
cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
git_patch_free(p);
cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, NULL, NULL, &opts));
cl_assert(p != NULL);
delta = git_patch_get_delta(p);
cl_assert(delta != NULL);
cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status);
cl_assert_equal_sz(0, delta->old_file.size);
cl_assert(git_oid_is_zero(&delta->old_file.id));
cl_assert_equal_sz(0, delta->new_file.size);
cl_assert(git_oid_is_zero(&delta->new_file.id));
cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
git_patch_free(p);
cl_git_pass(git_patch_from_blobs(&p, alien, NULL, alien, NULL, &opts));
cl_assert(p != NULL);
cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status);
cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
git_patch_free(p);
}
static void assert_binary_blobs_comparison(diff_expects *expected)
{
cl_assert(expected->files_binary > 0);
cl_assert_equal_i(1, expected->files);
cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected->hunks);
cl_assert_equal_i(0, expected->lines);
}
void test_diff_blob__can_compare_two_binary_blobs(void)
{
git_blob *heart;
git_oid h_oid;
/* heart.png */
cl_git_pass(git_oid__fromstrn(&h_oid, "de863bff", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 8));
cl_git_pass(git_diff_blobs(
alien, NULL, heart, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
heart, NULL, alien, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
git_blob_free(heart);
}
void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void)
{
cl_git_pass(git_diff_blobs(
alien, NULL, d, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
d, NULL, alien, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
}
/*
* $ git diff fe773770 a0f7217
* diff --git a/fe773770 b/a0f7217
* index fe77377..a0f7217 100644
* --- a/fe773770
* +++ b/a0f7217
* @@ -1,6 +1,6 @@
* Here is some stuff at the start
*
* -This should go in one hunk
* +This should go in one hunk (first)
*
* Some additional lines
*
* @@ -8,7 +8,7 @@ Down here below the other lines
*
* With even more at the end
*
* -Followed by a second hunk of stuff
* +Followed by a second hunk of stuff (second)
*
* That happens down here
*/
void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void)
{
git_blob *old_d;
git_oid old_d_oid;
opts.context_lines = 3;
/* tests/resources/attr/root_test1 from commit f5b0af1 */
cl_git_pass(git_oid__fromstrn(&old_d_oid, "fe773770", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 8));
/* Test with default inter-hunk-context (not set) => default is 0 */
cl_git_pass(git_diff_blobs(
old_d, NULL, d, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(2, expected.hunks);
/* Test with inter-hunk-context explicitly set to 0 */
opts.interhunk_lines = 0;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
old_d, NULL, d, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(2, expected.hunks);
/* Test with inter-hunk-context explicitly set to 1 */
opts.interhunk_lines = 1;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
old_d, NULL, d, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(1, expected.hunks);
git_blob_free(old_d);
}
void test_diff_blob__checks_options_version_too_low(void)
{
const git_error *err;
opts.version = 0;
cl_git_fail(git_diff_blobs(
d, NULL, alien, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
}
void test_diff_blob__checks_options_version_too_high(void)
{
const git_error *err;
opts.version = 1024;
cl_git_fail(git_diff_blobs(
d, NULL, alien, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
}
void test_diff_blob__can_correctly_detect_a_binary_blob_as_binary(void)
{
/* alien.png */
cl_assert_equal_i(true, git_blob_is_binary(alien));
}
void test_diff_blob__can_correctly_detect_binary_blob_data_as_binary(void)
{
/* alien.png */
const char *content = git_blob_rawcontent(alien);
size_t len = (size_t)git_blob_rawsize(alien);
cl_assert_equal_i(true, git_blob_data_is_binary(content, len));
}
void test_diff_blob__can_correctly_detect_a_textual_blob_as_non_binary(void)
{
/* tests/resources/attr/root_test4.txt */
cl_assert_equal_i(false, git_blob_is_binary(d));
}
void test_diff_blob__can_correctly_detect_textual_blob_data_as_non_binary(void)
{
/* tests/resources/attr/root_test4.txt */
const char *content = git_blob_rawcontent(d);
size_t len = (size_t)git_blob_rawsize(d);
cl_assert_equal_i(false, git_blob_data_is_binary(content, len));
}
/*
* git_diff_blob_to_buffer tests
*/
static void assert_changed_single_one_line_file(
diff_expects *expected, git_delta_t mod)
{
cl_assert_equal_i(1, expected->files);
cl_assert_equal_i(1, expected->file_status[mod]);
cl_assert_equal_i(1, expected->hunks);
cl_assert_equal_i(1, expected->lines);
if (mod == GIT_DELTA_ADDED)
cl_assert_equal_i(1, expected->line_adds);
else if (mod == GIT_DELTA_DELETED)
cl_assert_equal_i(1, expected->line_dels);
}
void test_diff_blob__can_compare_blob_to_buffer(void)
{
git_blob *a;
git_oid a_oid;
const char *a_content = "Hello from the root\n";
const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n";
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid__fromstrn(&a_oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
/* diff from blob a to content of b */
quick_diff_blob_to_str(a, NULL, b_content, 0, NULL);
assert_one_modified(1, 6, 1, 5, 0, &expected);
/* diff from blob a to content of a */
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
quick_diff_blob_to_str(a, NULL, a_content, 0, NULL);
assert_identical_blobs_comparison(&expected);
/* diff from NULL blob to content of a */
memset(&expected, 0, sizeof(expected));
quick_diff_blob_to_str(NULL, NULL, a_content, 0, NULL);
assert_changed_single_one_line_file(&expected, GIT_DELTA_ADDED);
/* diff from blob a to NULL buffer */
memset(&expected, 0, sizeof(expected));
quick_diff_blob_to_str(a, NULL, NULL, 0, NULL);
assert_changed_single_one_line_file(&expected, GIT_DELTA_DELETED);
/* diff with reverse */
opts.flags ^= GIT_DIFF_REVERSE;
memset(&expected, 0, sizeof(expected));
quick_diff_blob_to_str(a, NULL, NULL, 0, NULL);
assert_changed_single_one_line_file(&expected, GIT_DELTA_ADDED);
git_blob_free(a);
}
void test_diff_blob__can_compare_blob_to_buffer_with_patch(void)
{
git_patch *p;
git_blob *a;
git_oid a_oid;
const char *a_content = "Hello from the root\n";
const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n";
size_t tc, ta, td;
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid__fromstrn(&a_oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 8));
/* diff from blob a to content of b */
cl_git_pass(git_patch_from_blob_and_buffer(
&p, a, NULL, b_content, strlen(b_content), NULL, &opts));
cl_assert(p != NULL);
cl_assert_equal_i(GIT_DELTA_MODIFIED, git_patch_get_delta(p)->status);
cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0));
cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
cl_assert_equal_i(1, (int)tc);
cl_assert_equal_i(5, (int)ta);
cl_assert_equal_i(0, (int)td);
git_patch_free(p);
/* diff from blob a to content of a */
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
cl_git_pass(git_patch_from_blob_and_buffer(
&p, a, NULL, a_content, strlen(a_content), NULL, &opts));
cl_assert(p != NULL);
cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status);
cl_assert_equal_i(0, (int)git_patch_num_hunks(p));
git_patch_free(p);
/* diff from NULL blob to content of a */
cl_git_pass(git_patch_from_blob_and_buffer(
&p, NULL, NULL, a_content, strlen(a_content), NULL, &opts));
cl_assert(p != NULL);
cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status);
cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0));
git_patch_free(p);
/* diff from blob a to NULL buffer */
cl_git_pass(git_patch_from_blob_and_buffer(
&p, a, NULL, NULL, 0, NULL, &opts));
cl_assert(p != NULL);
cl_assert_equal_i(GIT_DELTA_DELETED, git_patch_get_delta(p)->status);
cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0));
git_patch_free(p);
/* diff with reverse */
opts.flags ^= GIT_DIFF_REVERSE;
cl_git_pass(git_patch_from_blob_and_buffer(
&p, a, NULL, NULL, 0, NULL, &opts));
cl_assert(p != NULL);
cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status);
cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0));
git_patch_free(p);
git_blob_free(a);
}
static void assert_one_modified_with_lines(diff_expects *expected, int lines)
{
cl_assert_equal_i(1, expected->files);
cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expected->files_binary);
cl_assert_equal_i(lines, expected->lines);
}
void test_diff_blob__binary_data_comparisons(void)
{
git_blob *bin, *nonbin;
git_oid oid;
const char *nonbin_content = "Hello from the root\n";
size_t nonbin_len = 20;
const char *bin_content = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n";
size_t bin_len = 33;
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
cl_git_pass(git_oid__fromstrn(&oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8));
cl_git_pass(git_oid__fromstrn(&oid, "b435cd56", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8));
/* non-binary to reference content */
quick_diff_blob_to_str(nonbin, NULL, nonbin_content, nonbin_len, NULL);
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(0, expected.files_binary);
/* binary to reference content */
quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL);
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(1, expected.files_binary);
/* non-binary to binary content */
quick_diff_blob_to_str(nonbin, NULL, bin_content, bin_len, NULL);
assert_binary_blobs_comparison(&expected);
/* binary to non-binary content */
quick_diff_blob_to_str(bin, NULL, nonbin_content, nonbin_len, NULL);
assert_binary_blobs_comparison(&expected);
/* non-binary to binary blob */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
bin, NULL, nonbin, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_binary_blobs_comparison(&expected);
/*
* repeat with FORCE_TEXT
*/
opts.flags |= GIT_DIFF_FORCE_TEXT;
quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL);
assert_identical_blobs_comparison(&expected);
quick_diff_blob_to_str(nonbin, NULL, bin_content, bin_len, NULL);
assert_one_modified_with_lines(&expected, 4);
quick_diff_blob_to_str(bin, NULL, nonbin_content, nonbin_len, NULL);
assert_one_modified_with_lines(&expected, 4);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
bin, NULL, nonbin, NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified_with_lines(&expected, 4);
/* cleanup */
git_blob_free(bin);
git_blob_free(nonbin);
}
void test_diff_blob__using_path_and_attributes(void)
{
git_config *cfg;
git_blob *bin, *nonbin;
git_oid oid;
const char *nonbin_content = "Hello from the root\n";
const char *bin_content =
"0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n";
size_t bin_len = 33;
const char *changed;
git_patch *p;
git_buf buf = GIT_BUF_INIT;
/* set up custom diff drivers and 'diff' attribute mappings for them */
cl_git_pass(git_repository_config(&cfg, g_repo));
cl_git_pass(git_config_set_bool(cfg, "diff.iam_binary.binary", 1));
cl_git_pass(git_config_set_bool(cfg, "diff.iam_text.binary", 0));
cl_git_pass(git_config_set_string(
cfg, "diff.iam_alphactx.xfuncname", "^[A-Za-z].*$"));
cl_git_pass(git_config_set_bool(cfg, "diff.iam_textalpha.binary", 0));
cl_git_pass(git_config_set_string(
cfg, "diff.iam_textalpha.xfuncname", "^[A-Za-z].*$"));
cl_git_pass(git_config_set_string(
cfg, "diff.iam_numctx.funcname", "^[0-9][0-9]*"));
cl_git_pass(git_config_set_bool(cfg, "diff.iam_textnum.binary", 0));
cl_git_pass(git_config_set_string(
cfg, "diff.iam_textnum.funcname", "^[0-9][0-9]*"));
git_config_free(cfg);
cl_git_append2file(
"attr/.gitattributes",
"\n\n# test_diff_blob__using_path_and_attributes extra\n\n"
"*.binary diff=iam_binary\n"
"*.textary diff=iam_text\n"
"*.alphary diff=iam_alphactx\n"
"*.textalphary diff=iam_textalpha\n"
"*.textnumary diff=iam_textnum\n"
"*.numary diff=iam_numctx\n\n");
opts.context_lines = 0;
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
cl_git_pass(git_oid__fromstrn(&oid, "45141a79", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8));
/* 20b: "Hello from the root\n" */
cl_git_pass(git_oid__fromstrn(&oid, "b435cd56", 8, GIT_OID_SHA1));
cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8));
/* 33b: "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\n0123456789\n" */
/* non-binary to reference content */
quick_diff_blob_to_str(nonbin, NULL, nonbin_content, 0, NULL);
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(0, expected.files_binary);
/* binary to reference content */
quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL);
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(1, expected.files_binary);
/* add some text */
changed = "Hello from the root\nMore lines\nAnd more\nGo here\n";
quick_diff_blob_to_str(nonbin, NULL, changed, 0, NULL);
assert_one_modified(1, 3, 0, 3, 0, &expected);
quick_diff_blob_to_str(nonbin, "foo/bar.binary", changed, 0, NULL);
cl_assert_equal_i(1, expected.files);
cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, expected.files_binary);
cl_assert_equal_i(0, expected.hunks);
cl_assert_equal_i(0, expected.lines);
quick_diff_blob_to_str(nonbin, "foo/bar.textary", changed, 0, NULL);
assert_one_modified(1, 3, 0, 3, 0, &expected);
quick_diff_blob_to_str(nonbin, "foo/bar.alphary", changed, 0, NULL);
assert_one_modified(1, 3, 0, 3, 0, &expected);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.normal b/zzz.normal\n"
"index 45141a7..75b0dbb 100644\n"
"--- a/zzz.normal\n"
"+++ b/zzz.normal\n"
"@@ -1,0 +2,3 @@ Hello from the root\n"
"+More lines\n"
"+And more\n"
"+Go here\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.binary", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.binary b/zzz.binary\n"
"index 45141a7..75b0dbb 100644\n"
"Binary files a/zzz.binary and b/zzz.binary differ\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.alphary", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.alphary b/zzz.alphary\n"
"index 45141a7..75b0dbb 100644\n"
"--- a/zzz.alphary\n"
"+++ b/zzz.alphary\n"
"@@ -1,0 +2,3 @@ Hello from the root\n"
"+More lines\n"
"+And more\n"
"+Go here\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.numary", changed, strlen(changed), NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.numary b/zzz.numary\n"
"index 45141a7..75b0dbb 100644\n"
"--- a/zzz.numary\n"
"+++ b/zzz.numary\n"
"@@ -1,0 +2,3 @@\n"
"+More lines\n"
"+And more\n"
"+Go here\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
/* "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n"
* 33 bytes
*/
changed = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\nreplace a line\n";
cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.normal", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.normal b/zzz.normal\n"
"index b435cd5..1604519 100644\n"
"Binary files a/zzz.normal and b/zzz.normal differ\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.textary", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.textary b/zzz.textary\n"
"index b435cd5..1604519 100644\n"
"--- a/zzz.textary\n"
"+++ b/zzz.textary\n"
"@@ -3 +3 @@\n"
"-0123456789\n"
"+replace a line\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.textalphary", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.textalphary b/zzz.textalphary\n"
"index b435cd5..1604519 100644\n"
"--- a/zzz.textalphary\n"
"+++ b/zzz.textalphary\n"
"@@ -3 +3 @@\n"
"-0123456789\n"
"+replace a line\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, bin, "zzz.textnumary", changed, 37, NULL, &opts));
cl_git_pass(git_patch_to_buf(&buf, p));
cl_assert_equal_s(
"diff --git a/zzz.textnumary b/zzz.textnumary\n"
"index b435cd5..1604519 100644\n"
"--- a/zzz.textnumary\n"
"+++ b/zzz.textnumary\n"
"@@ -3 +3 @@ 0123456789\n"
"-0123456789\n"
"+replace a line\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(p);
git_buf_dispose(&buf);
git_blob_free(nonbin);
git_blob_free(bin);
}
void test_diff_blob__can_compare_buffer_to_buffer(void)
{
const char *a = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n";
const char *b = "a\nB\nc\nd\nE\nF\nh\nj\nk\n";
opts.interhunk_lines = 0;
opts.context_lines = 0;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_buffers(
a, strlen(a), NULL, b, strlen(b), NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(4, 9, 0, 4, 5, &expected);
opts.flags ^= GIT_DIFF_REVERSE;
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_buffers(
a, strlen(a), NULL, b, strlen(b), NULL, &opts,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
assert_one_modified(4, 9, 0, 5, 4, &expected);
}
| libgit2-main | tests/libgit2/diff/blob.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
static git_repository *g_repo = NULL;
void test_diff_notify__initialize(void)
{
}
void test_diff_notify__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static int assert_called_notifications(
const git_diff *diff_so_far,
const git_diff_delta *delta_to_add,
const char *matched_pathspec,
void *payload)
{
bool found = false;
notify_expected *exp = (notify_expected*)payload;
notify_expected *e;
GIT_UNUSED(diff_so_far);
for (e = exp; e->path != NULL; e++) {
if (strcmp(e->path, delta_to_add->new_file.path))
continue;
cl_assert_equal_s(e->matched_pathspec, matched_pathspec);
found = true;
break;
}
cl_assert(found);
return 0;
}
static void test_notify(
char **searched_pathspecs,
int pathspecs_count,
notify_expected *expected_matched_pathspecs,
int expected_diffed_files_count)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
g_repo = cl_git_sandbox_init("status");
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.notify_cb = assert_called_notifications;
opts.pathspec.strings = searched_pathspecs;
opts.pathspec.count = pathspecs_count;
opts.payload = expected_matched_pathspecs;
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(expected_diffed_files_count, exp.files);
git_diff_free(diff);
}
void test_diff_notify__notify_single_pathspec(void)
{
char *searched_pathspecs[] = {
"*_deleted",
};
notify_expected expected_matched_pathspecs[] = {
{ "file_deleted", "*_deleted" },
{ "staged_changes_file_deleted", "*_deleted" },
{ NULL, NULL }
};
test_notify(searched_pathspecs, 1, expected_matched_pathspecs, 2);
}
void test_diff_notify__notify_multiple_pathspec(void)
{
char *searched_pathspecs[] = {
"staged_changes_cant_find_me",
"subdir/modified_cant_find_me",
"subdir/*",
"staged*"
};
notify_expected expected_matched_pathspecs[] = {
{ "staged_changes_file_deleted", "staged*" },
{ "staged_changes_modified_file", "staged*" },
{ "staged_delete_modified_file", "staged*" },
{ "staged_new_file_deleted_file", "staged*" },
{ "staged_new_file_modified_file", "staged*" },
{ "subdir/deleted_file", "subdir/*" },
{ "subdir/modified_file", "subdir/*" },
{ "subdir/new_file", "subdir/*" },
{ NULL, NULL }
};
test_notify(searched_pathspecs, 4, expected_matched_pathspecs, 8);
}
void test_diff_notify__notify_catchall_with_empty_pathspecs(void)
{
char *searched_pathspecs[] = {
"",
""
};
notify_expected expected_matched_pathspecs[] = {
{ "file_deleted", NULL },
{ "ignored_file", NULL },
{ "modified_file", NULL },
{ "new_file", NULL },
{ "\xe8\xbf\x99", NULL },
{ "staged_changes_file_deleted", NULL },
{ "staged_changes_modified_file", NULL },
{ "staged_delete_modified_file", NULL },
{ "staged_new_file_deleted_file", NULL },
{ "staged_new_file_modified_file", NULL },
{ "subdir/deleted_file", NULL },
{ "subdir/modified_file", NULL },
{ "subdir/new_file", NULL },
{ NULL, NULL }
};
test_notify(searched_pathspecs, 1, expected_matched_pathspecs, 13);
}
void test_diff_notify__notify_catchall(void)
{
char *searched_pathspecs[] = {
"*",
};
notify_expected expected_matched_pathspecs[] = {
{ "file_deleted", "*" },
{ "ignored_file", "*" },
{ "modified_file", "*" },
{ "new_file", "*" },
{ "\xe8\xbf\x99", "*" },
{ "staged_changes_file_deleted", "*" },
{ "staged_changes_modified_file", "*" },
{ "staged_delete_modified_file", "*" },
{ "staged_new_file_deleted_file", "*" },
{ "staged_new_file_modified_file", "*" },
{ "subdir/deleted_file", "*" },
{ "subdir/modified_file", "*" },
{ "subdir/new_file", "*" },
{ NULL, NULL }
};
test_notify(searched_pathspecs, 1, expected_matched_pathspecs, 13);
}
static int abort_diff(
const git_diff *diff_so_far,
const git_diff_delta *delta_to_add,
const char *matched_pathspec,
void *payload)
{
GIT_UNUSED(diff_so_far);
GIT_UNUSED(delta_to_add);
GIT_UNUSED(matched_pathspec);
GIT_UNUSED(payload);
return -42;
}
void test_diff_notify__notify_cb_can_abort_diff(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
char *pathspec = NULL;
g_repo = cl_git_sandbox_init("status");
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.notify_cb = abort_diff;
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
pathspec = "file_deleted";
cl_git_fail_with(
git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
pathspec = "staged_changes_modified_file";
cl_git_fail_with(
git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
}
static int filter_all(
const git_diff *diff_so_far,
const git_diff_delta *delta_to_add,
const char *matched_pathspec,
void *payload)
{
GIT_UNUSED(diff_so_far);
GIT_UNUSED(delta_to_add);
GIT_UNUSED(matched_pathspec);
GIT_UNUSED(payload);
return 42;
}
void test_diff_notify__notify_cb_can_be_used_as_filtering_function(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
char *pathspec = NULL;
diff_expects exp;
g_repo = cl_git_sandbox_init("status");
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.notify_cb = filter_all;
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
pathspec = "*_deleted";
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(0, exp.files);
git_diff_free(diff);
}
static int progress_abort_diff(
const git_diff *diff_so_far,
const char *old_path,
const char *new_path,
void *payload)
{
GIT_UNUSED(diff_so_far);
GIT_UNUSED(old_path);
GIT_UNUSED(new_path);
GIT_UNUSED(payload);
return -42;
}
void test_diff_notify__progress_cb_can_abort_diff(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
g_repo = cl_git_sandbox_init("status");
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.progress_cb = progress_abort_diff;
cl_git_fail_with(
git_diff_index_to_workdir(&diff, g_repo, NULL, &opts), -42);
}
| libgit2-main | tests/libgit2/diff/notify.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
#include "git2/sys/diff.h"
git_tree *resolve_commit_oid_to_tree(
git_repository *repo,
const char *partial_oid)
{
size_t len = strlen(partial_oid);
git_oid oid;
git_object *obj = NULL;
git_tree *tree = NULL;
if (git_oid__fromstrn(&oid, partial_oid, len, GIT_OID_SHA1) == 0)
cl_git_pass(git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJECT_ANY));
cl_git_pass(git_object_peel((git_object **) &tree, obj, GIT_OBJECT_TREE));
git_object_free(obj);
return tree;
}
static char diff_pick_suffix(int mode)
{
if (S_ISDIR(mode))
return '/';
else if (GIT_PERMS_IS_EXEC(mode))
return '*';
else
return ' ';
}
static void fprintf_delta(FILE *fp, const git_diff_delta *delta, float progress)
{
char code = git_diff_status_char(delta->status);
char old_suffix = diff_pick_suffix(delta->old_file.mode);
char new_suffix = diff_pick_suffix(delta->new_file.mode);
fprintf(fp, "%c\t%s", code, delta->old_file.path);
if ((delta->old_file.path != delta->new_file.path &&
strcmp(delta->old_file.path, delta->new_file.path) != 0) ||
(delta->old_file.mode != delta->new_file.mode &&
delta->old_file.mode != 0 && delta->new_file.mode != 0))
fprintf(fp, "%c %s%c", old_suffix, delta->new_file.path, new_suffix);
else if (old_suffix != ' ')
fprintf(fp, "%c", old_suffix);
fprintf(fp, "\t[%.2f]\n", progress);
}
int diff_file_cb(
const git_diff_delta *delta,
float progress,
void *payload)
{
diff_expects *e = payload;
if (e->debug)
fprintf_delta(stderr, delta, progress);
if (e->names)
cl_assert_equal_s(e->names[e->files], delta->old_file.path);
if (e->statuses)
cl_assert_equal_i(e->statuses[e->files], (int)delta->status);
e->files++;
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
e->files_binary++;
cl_assert(delta->status <= GIT_DELTA_CONFLICTED);
e->file_status[delta->status] += 1;
return 0;
}
int diff_print_file_cb(
const git_diff_delta *delta,
float progress,
void *payload)
{
if (!payload) {
fprintf_delta(stderr, delta, progress);
return 0;
}
if (!((diff_expects *)payload)->debug)
fprintf_delta(stderr, delta, progress);
return diff_file_cb(delta, progress, payload);
}
int diff_binary_cb(
const git_diff_delta *delta,
const git_diff_binary *binary,
void *payload)
{
GIT_UNUSED(delta);
GIT_UNUSED(binary);
GIT_UNUSED(payload);
return 0;
}
int diff_hunk_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
void *payload)
{
diff_expects *e = payload;
const char *scan = hunk->header, *scan_end = scan + hunk->header_len;
GIT_UNUSED(delta);
/* confirm no NUL bytes in header text */
while (scan < scan_end)
cl_assert('\0' != *scan++);
e->hunks++;
e->hunk_old_lines += hunk->old_lines;
e->hunk_new_lines += hunk->new_lines;
return 0;
}
int diff_line_cb(
const git_diff_delta *delta,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *payload)
{
diff_expects *e = payload;
GIT_UNUSED(delta);
GIT_UNUSED(hunk);
e->lines++;
switch (line->origin) {
case GIT_DIFF_LINE_CONTEXT:
case GIT_DIFF_LINE_CONTEXT_EOFNL: /* techically not a line */
e->line_ctxt++;
break;
case GIT_DIFF_LINE_ADDITION:
case GIT_DIFF_LINE_ADD_EOFNL: /* technically not a line add */
e->line_adds++;
break;
case GIT_DIFF_LINE_DELETION:
case GIT_DIFF_LINE_DEL_EOFNL: /* technically not a line delete */
e->line_dels++;
break;
default:
break;
}
return 0;
}
int diff_foreach_via_iterator(
git_diff *diff,
git_diff_file_cb file_cb,
git_diff_binary_cb binary_cb,
git_diff_hunk_cb hunk_cb,
git_diff_line_cb line_cb,
void *data)
{
size_t d, num_d = git_diff_num_deltas(diff);
GIT_UNUSED(binary_cb);
for (d = 0; d < num_d; ++d) {
git_patch *patch;
const git_diff_delta *delta;
size_t h, num_h;
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert((delta = git_patch_get_delta(patch)) != NULL);
/* call file_cb for this file */
if (file_cb != NULL && file_cb(delta, (float)d / num_d, data) != 0) {
git_patch_free(patch);
goto abort;
}
/* if there are no changes, then the patch will be NULL */
if (!patch) {
cl_assert(delta->status == GIT_DELTA_UNMODIFIED ||
(delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
continue;
}
if (!hunk_cb && !line_cb) {
git_patch_free(patch);
continue;
}
num_h = git_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
const git_diff_hunk *hunk;
size_t l, num_l;
cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
if (hunk_cb && hunk_cb(delta, hunk, data) != 0) {
git_patch_free(patch);
goto abort;
}
for (l = 0; l < num_l; ++l) {
const git_diff_line *line;
cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
if (line_cb &&
line_cb(delta, hunk, line, data) != 0) {
git_patch_free(patch);
goto abort;
}
}
}
git_patch_free(patch);
}
return 0;
abort:
git_error_clear();
return GIT_EUSER;
}
void diff_print(FILE *fp, git_diff *diff)
{
cl_git_pass(
git_diff_print(diff, GIT_DIFF_FORMAT_PATCH,
git_diff_print_callback__to_file_handle, fp ? fp : stderr));
}
void diff_print_raw(FILE *fp, git_diff *diff)
{
cl_git_pass(
git_diff_print(diff, GIT_DIFF_FORMAT_RAW,
git_diff_print_callback__to_file_handle, fp ? fp : stderr));
}
static size_t num_modified_deltas(git_diff *diff)
{
const git_diff_delta *delta;
size_t i, cnt = 0;
for (i = 0; i < git_diff_num_deltas(diff); i++) {
delta = git_diff_get_delta(diff, i);
if (delta->status != GIT_DELTA_UNMODIFIED)
cnt++;
}
return cnt;
}
void diff_assert_equal(git_diff *a, git_diff *b)
{
const git_diff_delta *ad, *bd;
size_t i, j;
assert(a && b);
cl_assert_equal_i(num_modified_deltas(a), num_modified_deltas(b));
for (i = 0, j = 0;
i < git_diff_num_deltas(a) && j < git_diff_num_deltas(b); ) {
ad = git_diff_get_delta(a, i);
bd = git_diff_get_delta(b, j);
if (ad->status == GIT_DELTA_UNMODIFIED) {
i++;
continue;
}
if (bd->status == GIT_DELTA_UNMODIFIED) {
j++;
continue;
}
cl_assert_equal_i(ad->status, bd->status);
cl_assert_equal_i(ad->flags, bd->flags);
cl_assert_equal_i(ad->similarity, bd->similarity);
cl_assert_equal_i(ad->nfiles, bd->nfiles);
/* Don't examine the size or the flags of the deltas;
* computed deltas have sizes (parsed deltas do not) and
* computed deltas will have flags of `VALID_ID` and
* `EXISTS` (parsed deltas will not query the ODB.)
*/
/* an empty id indicates that it wasn't presented, because
* the diff was identical. (eg, pure rename, mode change only, etc)
*/
if (ad->old_file.id_abbrev && bd->old_file.id_abbrev) {
cl_assert_equal_i(ad->old_file.id_abbrev, bd->old_file.id_abbrev);
cl_assert_equal_oid(&ad->old_file.id, &bd->old_file.id);
cl_assert_equal_i(ad->old_file.mode, bd->old_file.mode);
}
cl_assert_equal_s(ad->old_file.path, bd->old_file.path);
if (ad->new_file.id_abbrev && bd->new_file.id_abbrev) {
cl_assert_equal_oid(&ad->new_file.id, &bd->new_file.id);
cl_assert_equal_i(ad->new_file.id_abbrev, bd->new_file.id_abbrev);
cl_assert_equal_i(ad->new_file.mode, bd->new_file.mode);
}
cl_assert_equal_s(ad->new_file.path, bd->new_file.path);
i++;
j++;
}
}
| libgit2-main | tests/libgit2/diff/diff_helpers.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
void test_diff_diffiter__initialize(void)
{
}
void test_diff_diffiter__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_diff_diffiter__create(void)
{
git_repository *repo = cl_git_sandbox_init("attr");
git_diff *diff;
size_t d, num_d;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
const git_diff_delta *delta = git_diff_get_delta(diff, d);
cl_assert(delta != NULL);
}
cl_assert(!git_diff_get_delta(diff, num_d));
git_diff_free(diff);
}
void test_diff_diffiter__iterate_files_1(void)
{
git_repository *repo = cl_git_sandbox_init("attr");
git_diff *diff;
size_t d, num_d;
diff_expects exp = { 0 };
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
const git_diff_delta *delta = git_diff_get_delta(diff, d);
cl_assert(delta != NULL);
diff_file_cb(delta, (float)d / (float)num_d, &exp);
}
cl_assert_equal_sz(6, exp.files);
git_diff_free(diff);
}
void test_diff_diffiter__iterate_files_2(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff *diff;
size_t d, num_d;
int count = 0;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL));
num_d = git_diff_num_deltas(diff);
cl_assert_equal_i(8, (int)num_d);
for (d = 0; d < num_d; ++d) {
const git_diff_delta *delta = git_diff_get_delta(diff, d);
cl_assert(delta != NULL);
count++;
}
cl_assert_equal_i(8, count);
git_diff_free(diff);
}
void test_diff_diffiter__iterate_files_and_hunks(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
size_t d, num_d;
int file_count = 0, hunk_count = 0;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_patch *patch;
size_t h, num_h;
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch);
file_count++;
num_h = git_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
const git_diff_hunk *hunk;
cl_git_pass(git_patch_get_hunk(&hunk, NULL, patch, h));
cl_assert(hunk);
hunk_count++;
}
git_patch_free(patch);
}
cl_assert_equal_i(13, file_count);
cl_assert_equal_i(8, hunk_count);
git_diff_free(diff);
}
void test_diff_diffiter__max_size_threshold(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
int file_count = 0, binary_count = 0, hunk_count = 0;
size_t d, num_d;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_patch *patch;
const git_diff_delta *delta;
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch);
delta = git_patch_get_delta(patch);
cl_assert(delta);
file_count++;
hunk_count += (int)git_patch_num_hunks(patch);
assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
git_patch_free(patch);
}
cl_assert_equal_i(13, file_count);
cl_assert_equal_i(0, binary_count);
cl_assert_equal_i(8, hunk_count);
git_diff_free(diff);
/* try again with low file size threshold */
file_count = binary_count = hunk_count = 0;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.max_size = 50; /* treat anything over 50 bytes as binary! */
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_patch *patch;
const git_diff_delta *delta;
cl_git_pass(git_patch_from_diff(&patch, diff, d));
delta = git_patch_get_delta(patch);
file_count++;
hunk_count += (int)git_patch_num_hunks(patch);
assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
git_patch_free(patch);
}
cl_assert_equal_i(13, file_count);
/* Three files are over the 50 byte threshold:
* - staged_changes_file_deleted
* - staged_changes_modified_file
* - staged_new_file_modified_file
*/
cl_assert_equal_i(3, binary_count);
cl_assert_equal_i(5, hunk_count);
git_diff_free(diff);
}
void test_diff_diffiter__iterate_all(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp = {0};
size_t d, num_d;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_patch *patch;
size_t h, num_h;
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch);
exp.files++;
num_h = git_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
const git_diff_hunk *range;
size_t l, num_l;
cl_git_pass(git_patch_get_hunk(&range, &num_l, patch, h));
cl_assert(range);
exp.hunks++;
for (l = 0; l < num_l; ++l) {
const git_diff_line *line;
cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
cl_assert(line && line->content);
exp.lines++;
}
}
git_patch_free(patch);
}
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(8, exp.hunks);
cl_assert_equal_i(14, exp.lines);
git_diff_free(diff);
}
static void iterate_over_patch(git_patch *patch, diff_expects *exp)
{
size_t h, num_h = git_patch_num_hunks(patch), num_l;
exp->files++;
exp->hunks += (int)num_h;
/* let's iterate in reverse, just because we can! */
for (h = 1, num_l = 0; h <= num_h; ++h)
num_l += git_patch_num_lines_in_hunk(patch, num_h - h);
exp->lines += (int)num_l;
}
#define PATCH_CACHE 5
void test_diff_diffiter__iterate_randomly_while_saving_state(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp = {0};
git_patch *patches[PATCH_CACHE];
size_t p, d, num_d;
memset(patches, 0, sizeof(patches));
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
num_d = git_diff_num_deltas(diff);
/* To make sure that references counts work for diff and patch objects,
* this generates patches and randomly caches them. Only when the patch
* is removed from the cache are hunks and lines counted. At the end,
* there are still patches in the cache, so free the diff and try to
* process remaining patches after the diff is freed.
*/
srand(121212);
p = rand() % PATCH_CACHE;
for (d = 0; d < num_d; ++d) {
/* take old patch */
git_patch *patch = patches[p];
patches[p] = NULL;
/* cache new patch */
cl_git_pass(git_patch_from_diff(&patches[p], diff, d));
cl_assert(patches[p] != NULL);
/* process old patch if non-NULL */
if (patch != NULL) {
iterate_over_patch(patch, &exp);
git_patch_free(patch);
}
p = rand() % PATCH_CACHE;
}
/* free diff list now - refcounts should keep things safe */
git_diff_free(diff);
/* process remaining unprocessed patches */
for (p = 0; p < PATCH_CACHE; p++) {
git_patch *patch = patches[p];
if (patch != NULL) {
iterate_over_patch(patch, &exp);
git_patch_free(patch);
}
}
/* hopefully it all still added up right */
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(8, exp.hunks);
cl_assert_equal_i(14, exp.lines);
}
/* This output is taken directly from `git diff` on the status test data */
static const char *expected_patch_text[8] = {
/* 0 */
"diff --git a/file_deleted b/file_deleted\n"
"deleted file mode 100644\n"
"index 5452d32..0000000\n"
"--- a/file_deleted\n"
"+++ /dev/null\n"
"@@ -1 +0,0 @@\n"
"-file_deleted\n",
/* 1 */
"diff --git a/modified_file b/modified_file\n"
"index 452e424..0a53963 100644\n"
"--- a/modified_file\n"
"+++ b/modified_file\n"
"@@ -1 +1,2 @@\n"
" modified_file\n"
"+modified_file\n",
/* 2 */
"diff --git a/staged_changes_file_deleted b/staged_changes_file_deleted\n"
"deleted file mode 100644\n"
"index a6be623..0000000\n"
"--- a/staged_changes_file_deleted\n"
"+++ /dev/null\n"
"@@ -1,2 +0,0 @@\n"
"-staged_changes_file_deleted\n"
"-staged_changes_file_deleted\n",
/* 3 */
"diff --git a/staged_changes_modified_file b/staged_changes_modified_file\n"
"index 906ee77..011c344 100644\n"
"--- a/staged_changes_modified_file\n"
"+++ b/staged_changes_modified_file\n"
"@@ -1,2 +1,3 @@\n"
" staged_changes_modified_file\n"
" staged_changes_modified_file\n"
"+staged_changes_modified_file\n",
/* 4 */
"diff --git a/staged_new_file_deleted_file b/staged_new_file_deleted_file\n"
"deleted file mode 100644\n"
"index 90b8c29..0000000\n"
"--- a/staged_new_file_deleted_file\n"
"+++ /dev/null\n"
"@@ -1 +0,0 @@\n"
"-staged_new_file_deleted_file\n",
/* 5 */
"diff --git a/staged_new_file_modified_file b/staged_new_file_modified_file\n"
"index ed06290..8b090c0 100644\n"
"--- a/staged_new_file_modified_file\n"
"+++ b/staged_new_file_modified_file\n"
"@@ -1 +1,2 @@\n"
" staged_new_file_modified_file\n"
"+staged_new_file_modified_file\n",
/* 6 */
"diff --git a/subdir/deleted_file b/subdir/deleted_file\n"
"deleted file mode 100644\n"
"index 1888c80..0000000\n"
"--- a/subdir/deleted_file\n"
"+++ /dev/null\n"
"@@ -1 +0,0 @@\n"
"-subdir/deleted_file\n",
/* 7 */
"diff --git a/subdir/modified_file b/subdir/modified_file\n"
"index a619198..57274b7 100644\n"
"--- a/subdir/modified_file\n"
"+++ b/subdir/modified_file\n"
"@@ -1 +1,2 @@\n"
" subdir/modified_file\n"
"+subdir/modified_file\n"
};
void test_diff_diffiter__iterate_and_generate_patch_text(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff *diff;
size_t d, num_d;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL));
num_d = git_diff_num_deltas(diff);
cl_assert_equal_i(8, (int)num_d);
for (d = 0; d < num_d; ++d) {
git_patch *patch;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch != NULL);
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected_patch_text[d], buf.ptr);
git_buf_dispose(&buf);
git_patch_free(patch);
}
git_diff_free(diff);
}
void test_diff_diffiter__checks_options_version(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
const git_error *err;
opts.version = 0;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_fail(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
git_error_clear();
opts.version = 1024;
cl_git_fail(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
}
| libgit2-main | tests/libgit2/diff/diffiter.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
#include "repository.h"
#include "index.h"
#include "git2/sys/diff.h"
#include "../checkout/checkout_helpers.h"
static git_repository *g_repo = NULL;
void test_diff_workdir__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_diff_workdir__to_index(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
int use_iterator;
g_repo = cl_git_sandbox_init("status");
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
/* to generate these values:
* - cd to tests/resources/status,
* - mv .gitted .git
* - git diff --name-status
* - git diff
* - mv .git .gitted
*/
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);
cl_assert_equal_i(8, exp.hunks);
cl_assert_equal_i(14, exp.lines);
cl_assert_equal_i(5, exp.line_ctxt);
cl_assert_equal_i(4, exp.line_adds);
cl_assert_equal_i(5, exp.line_dels);
}
{
git_diff_perfdata perf = GIT_DIFF_PERFDATA_INIT;
cl_git_pass(git_diff_get_perfdata(&perf, diff));
cl_assert_equal_sz(
13 /* in root */ + 3 /* in subdir */, perf.stat_calls);
cl_assert_equal_sz(5, perf.oid_calculations);
}
git_diff_free(diff);
}
void test_diff_workdir__to_index_with_conflicts(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_index *index;
git_index_entry our_entry = {{0}}, their_entry = {{0}};
diff_expects exp = {0};
g_repo = cl_git_sandbox_init("status");
opts.context_lines = 3;
opts.interhunk_lines = 1;
/* Adding an entry that represents a rename gets two files in conflict */
our_entry.path = "subdir/modified_file";
our_entry.mode = 0100644;
git_oid__fromstr(&our_entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
their_entry.path = "subdir/rename_conflict";
their_entry.mode = 0100644;
git_oid__fromstr(&their_entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_conflict_add(index, NULL, &our_entry, &their_entry));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &opts));
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(9, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_CONFLICTED]);
cl_assert_equal_i(7, exp.hunks);
cl_assert_equal_i(12, exp.lines);
cl_assert_equal_i(4, exp.line_ctxt);
cl_assert_equal_i(3, exp.line_adds);
cl_assert_equal_i(5, exp.line_dels);
git_diff_free(diff);
git_index_free(index);
}
void test_diff_workdir__to_index_with_assume_unchanged(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_index *idx = NULL;
diff_expects exp;
const git_index_entry *iep;
git_index_entry ie;
g_repo = cl_git_sandbox_init("status");
/* do initial diff */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(8, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
git_diff_free(diff);
/* mark a couple of entries with ASSUME_UNCHANGED */
cl_git_pass(git_repository_index(&idx, g_repo));
cl_assert((iep = git_index_get_bypath(idx, "modified_file", 0)) != NULL);
memcpy(&ie, iep, sizeof(ie));
ie.flags |= GIT_INDEX_ENTRY_VALID;
cl_git_pass(git_index_add(idx, &ie));
cl_assert((iep = git_index_get_bypath(idx, "file_deleted", 0)) != NULL);
memcpy(&ie, iep, sizeof(ie));
ie.flags |= GIT_INDEX_ENTRY_VALID;
cl_git_pass(git_index_add(idx, &ie));
cl_git_pass(git_index_write(idx));
git_index_free(idx);
/* redo diff and see that entries are skipped */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(6, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
git_diff_free(diff);
}
void test_diff_workdir__to_tree(void)
{
/* grabbed a couple of commit oids from the history of the attr repo */
const char *a_commit = "26a125ee1bf"; /* the current HEAD */
const char *b_commit = "0017bd4ab1ec3"; /* the start */
git_tree *a, *b;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_diff *diff2 = NULL;
diff_expects exp;
int use_iterator;
g_repo = cl_git_sandbox_init("status");
a = resolve_commit_oid_to_tree(g_repo, a_commit);
b = resolve_commit_oid_to_tree(g_repo, b_commit);
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
/* You can't really generate the equivalent of git_diff_tree_to_workdir()
* using C git. It really wants to interpose the index into the diff.
*
* To validate the following results with command line git, I ran the
* following:
* - git ls-tree 26a125
* - find . ! -path ./.git/\* -a -type f | git hash-object --stdin-paths
* The results are documented at the bottom of this file in the
* long comment entitled "PREPARATION OF TEST DATA".
*/
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(14, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(5, exp.file_status[GIT_DELTA_UNTRACKED]);
}
/* Since there is no git diff equivalent, let's just assume that the
* text diffs produced by git_diff_foreach are accurate here. We will
* do more apples-to-apples test comparison below.
*/
git_diff_free(diff);
diff = NULL;
memset(&exp, 0, sizeof(exp));
/* This is a compatible emulation of "git diff <sha>" which looks like
* a workdir to tree diff (even though it is not really). This is what
* you would get from "git diff --name-status 26a125ee1bf"
*/
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts));
cl_git_pass(git_diff_merge(diff, diff2));
git_diff_free(diff2);
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(15, exp.files);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(5, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);
cl_assert_equal_i(11, exp.hunks);
cl_assert_equal_i(17, exp.lines);
cl_assert_equal_i(4, exp.line_ctxt);
cl_assert_equal_i(8, exp.line_adds);
cl_assert_equal_i(5, exp.line_dels);
}
git_diff_free(diff);
diff = NULL;
memset(&exp, 0, sizeof(exp));
/* Again, emulating "git diff <sha>" for testing purposes using
* "git diff --name-status 0017bd4ab1ec3" instead.
*/
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts));
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts));
cl_git_pass(git_diff_merge(diff, diff2));
git_diff_free(diff2);
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(16, exp.files);
cl_assert_equal_i(5, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);
cl_assert_equal_i(12, exp.hunks);
cl_assert_equal_i(19, exp.lines);
cl_assert_equal_i(3, exp.line_ctxt);
cl_assert_equal_i(12, exp.line_adds);
cl_assert_equal_i(4, exp.line_dels);
}
git_diff_free(diff);
/* Let's try that once more with a reversed diff */
opts.flags |= GIT_DIFF_REVERSE;
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts));
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts));
cl_git_pass(git_diff_merge(diff, diff2));
git_diff_free(diff2);
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(16, exp.files);
cl_assert_equal_i(5, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);
cl_assert_equal_i(12, exp.hunks);
cl_assert_equal_i(19, exp.lines);
cl_assert_equal_i(3, exp.line_ctxt);
cl_assert_equal_i(12, exp.line_dels);
cl_assert_equal_i(4, exp.line_adds);
git_diff_free(diff);
/* all done now */
git_tree_free(a);
git_tree_free(b);
}
void test_diff_workdir__to_index_with_pathspec(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
char *pathspec = NULL;
int use_iterator;
g_repo = cl_git_sandbox_init("status");
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
pathspec = "modified_file";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
pathspec = "subdir";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(3, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
pathspec = "*_deleted";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(2, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
}
void test_diff_workdir__to_index_with_pathlist_disabling_fnmatch(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
char *pathspec = NULL;
int use_iterator;
g_repo = cl_git_sandbox_init("status");
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED |
GIT_DIFF_DISABLE_PATHSPEC_MATCH;
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 0;
/* ensure that an empty pathspec list is ignored */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
/* ensure that a single NULL pathspec is filtered out (like when using
* fnmatch filtering)
*/
opts.pathspec.count = 1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
pathspec = "modified_file";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
/* ensure that subdirs can be specified */
pathspec = "subdir";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(3, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
/* ensure that subdirs can be specified with a trailing slash */
pathspec = "subdir/";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(3, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
/* ensure that fnmatching is completely disabled */
pathspec = "subdir/*";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
/* ensure that the prefix matching isn't completely braindead */
pathspec = "subdi";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
/* ensure that fnmatching isn't working at all */
pathspec = "*_deleted";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, NULL, NULL, NULL, &exp));
else
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]);
}
git_diff_free(diff);
}
void test_diff_workdir__filemode_changes(void)
{
git_diff *diff = NULL;
diff_expects exp;
int use_iterator;
if (!cl_is_chmod_supported())
return;
g_repo = cl_git_sandbox_init("issue_592");
cl_repo_set_bool(g_repo, "core.filemode", true);
/* test once with no mods */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.hunks);
}
git_diff_free(diff);
/* chmod file and test again */
cl_assert(cl_toggle_filemode("issue_592/a.txt"));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.hunks);
}
git_diff_free(diff);
cl_assert(cl_toggle_filemode("issue_592/a.txt"));
}
void test_diff_workdir__filemode_changes_with_filemode_false(void)
{
git_diff *diff = NULL;
diff_expects exp;
if (!cl_is_chmod_supported())
return;
g_repo = cl_git_sandbox_init("issue_592");
cl_repo_set_bool(g_repo, "core.filemode", false);
/* test once with no mods */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.hunks);
git_diff_free(diff);
/* chmod file and test again */
cl_assert(cl_toggle_filemode("issue_592/a.txt"));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(diff,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.hunks);
git_diff_free(diff);
cl_assert(cl_toggle_filemode("issue_592/a.txt"));
}
void test_diff_workdir__head_index_and_workdir_all_differ(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff_i2t = NULL, *diff_w2i = NULL;
diff_expects exp;
char *pathspec = "staged_changes_modified_file";
git_tree *tree;
int use_iterator;
/* For this file,
* - head->index diff has 1 line of context, 1 line of diff
* - index->workdir diff has 2 lines of context, 1 line of diff
* but
* - head->workdir diff has 1 line of context, 2 lines of diff
* Let's make sure the right one is returned from each fn.
*/
g_repo = cl_git_sandbox_init("status");
tree = resolve_commit_oid_to_tree(g_repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f");
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
cl_git_pass(git_diff_tree_to_index(&diff_i2t, g_repo, tree, NULL, &opts));
cl_git_pass(git_diff_index_to_workdir(&diff_w2i, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff_i2t, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff_i2t, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.hunks);
cl_assert_equal_i(2, exp.lines);
cl_assert_equal_i(1, exp.line_ctxt);
cl_assert_equal_i(1, exp.line_adds);
cl_assert_equal_i(0, exp.line_dels);
}
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff_w2i, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff_w2i, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.hunks);
cl_assert_equal_i(3, exp.lines);
cl_assert_equal_i(2, exp.line_ctxt);
cl_assert_equal_i(1, exp.line_adds);
cl_assert_equal_i(0, exp.line_dels);
}
cl_git_pass(git_diff_merge(diff_i2t, diff_w2i));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff_i2t, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff_i2t, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.hunks);
cl_assert_equal_i(3, exp.lines);
cl_assert_equal_i(1, exp.line_ctxt);
cl_assert_equal_i(2, exp.line_adds);
cl_assert_equal_i(0, exp.line_dels);
}
git_diff_free(diff_i2t);
git_diff_free(diff_w2i);
git_tree_free(tree);
}
void test_diff_workdir__eof_newline_changes(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
char *pathspec = "current_file";
int use_iterator;
g_repo = cl_git_sandbox_init("status");
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.hunks);
cl_assert_equal_i(0, exp.lines);
cl_assert_equal_i(0, exp.line_ctxt);
cl_assert_equal_i(0, exp.line_adds);
cl_assert_equal_i(0, exp.line_dels);
}
git_diff_free(diff);
cl_git_append2file("status/current_file", "\n");
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.hunks);
cl_assert_equal_i(2, exp.lines);
cl_assert_equal_i(1, exp.line_ctxt);
cl_assert_equal_i(1, exp.line_adds);
cl_assert_equal_i(0, exp.line_dels);
}
git_diff_free(diff);
cl_git_rewritefile("status/current_file", "current_file");
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
for (use_iterator = 0; use_iterator <= 1; use_iterator++) {
memset(&exp, 0, sizeof(exp));
if (use_iterator)
cl_git_pass(diff_foreach_via_iterator(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
else
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.hunks);
cl_assert_equal_i(3, exp.lines);
cl_assert_equal_i(0, exp.line_ctxt);
cl_assert_equal_i(1, exp.line_adds);
cl_assert_equal_i(2, exp.line_dels);
}
git_diff_free(diff);
}
/* PREPARATION OF TEST DATA
*
* Since there is no command line equivalent of git_diff_tree_to_workdir,
* it was a bit of a pain to confirm that I was getting the expected
* results in the first part of this tests. Here is what I ended up
* doing to set my expectation for the file counts and results:
*
* Running "git ls-tree 26a125" and "git ls-tree aa27a6" shows:
*
* A a0de7e0ac200c489c41c59dfa910154a70264e6e current_file
* B 5452d32f1dd538eb0405e8a83cc185f79e25e80f file_deleted
* C 452e4244b5d083ddf0460acf1ecc74db9dcfa11a modified_file
* D 32504b727382542f9f089e24fddac5e78533e96c staged_changes
* E 061d42a44cacde5726057b67558821d95db96f19 staged_changes_file_deleted
* F 70bd9443ada07063e7fbf0b3ff5c13f7494d89c2 staged_changes_modified_file
* G e9b9107f290627c04d097733a10055af941f6bca staged_delete_file_deleted
* H dabc8af9bd6e9f5bbe96a176f1a24baf3d1f8916 staged_delete_modified_file
* I 53ace0d1cc1145a5f4fe4f78a186a60263190733 subdir/current_file
* J 1888c805345ba265b0ee9449b8877b6064592058 subdir/deleted_file
* K a6191982709b746d5650e93c2acf34ef74e11504 subdir/modified_file
* L e8ee89e15bbe9b20137715232387b3de5b28972e subdir.txt
*
* --------
*
* find . ! -path ./.git/\* -a -type f | git hash-object --stdin-paths
*
* A a0de7e0ac200c489c41c59dfa910154a70264e6e current_file
* M 6a79f808a9c6bc9531ac726c184bbcd9351ccf11 ignored_file
* C 0a539630525aca2e7bc84975958f92f10a64c9b6 modified_file
* N d4fa8600b4f37d7516bef4816ae2c64dbf029e3a new_file
* D 55d316c9ba708999f1918e9677d01dfcae69c6b9 staged_changes
* F 011c3440d5c596e21d836aa6d7b10eb581f68c49 staged_changes_modified_file
* H dabc8af9bd6e9f5bbe96a176f1a24baf3d1f8916 staged_delete_modified_file
* O 529a16e8e762d4acb7b9636ff540a00831f9155a staged_new_file
* P 8b090c06d14ffa09c4e880088ebad33893f921d1 staged_new_file_modified_file
* I 53ace0d1cc1145a5f4fe4f78a186a60263190733 subdir/current_file
* K 57274b75eeb5f36fd55527806d567b2240a20c57 subdir/modified_file
* Q 80a86a6931b91bc01c2dbf5ca55bdd24ad1ef466 subdir/new_file
* L e8ee89e15bbe9b20137715232387b3de5b28972e subdir.txt
*
* --------
*
* A - current_file (UNMODIFIED) -> not in results
* B D file_deleted
* M I ignored_file (IGNORED)
* C M modified_file
* N U new_file (UNTRACKED)
* D M staged_changes
* E D staged_changes_file_deleted
* F M staged_changes_modified_file
* G D staged_delete_file_deleted
* H - staged_delete_modified_file (UNMODIFIED) -> not in results
* O U staged_new_file
* P U staged_new_file_modified_file
* I - subdir/current_file (UNMODIFIED) -> not in results
* J D subdir/deleted_file
* K M subdir/modified_file
* Q U subdir/new_file
* L - subdir.txt (UNMODIFIED) -> not in results
*
* Expect 13 files, 0 ADD, 4 DEL, 4 MOD, 1 IGN, 4 UNTR
*/
void test_diff_workdir__larger_hunks(void)
{
const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69";
const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10";
git_tree *a, *b;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
size_t i, d, num_d, h, num_h, l, num_l;
g_repo = cl_git_sandbox_init("diff");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
opts.context_lines = 1;
opts.interhunk_lines = 0;
for (i = 0; i <= 2; ++i) {
git_diff *diff = NULL;
git_patch *patch;
const git_diff_hunk *hunk;
const git_diff_line *line;
/* okay, this is a bit silly, but oh well */
switch (i) {
case 0:
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
break;
case 1:
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts));
break;
case 2:
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, b, &opts));
break;
}
num_d = git_diff_num_deltas(diff);
cl_assert_equal_i(2, (int)num_d);
for (d = 0; d < num_d; ++d) {
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch);
num_h = git_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
for (l = 0; l < num_l; ++l) {
cl_git_pass(
git_patch_get_line_in_hunk(&line, patch, h, l));
cl_assert(line);
}
/* confirm fail after the last item */
cl_git_fail(
git_patch_get_line_in_hunk(&line, patch, h, num_l));
}
/* confirm fail after the last item */
cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h));
git_patch_free(patch);
}
git_diff_free(diff);
}
git_tree_free(a);
git_tree_free(b);
}
/* Set up a test that exercises this code. The easiest test using existing
* test data is probably to create a sandbox of submod2 and then run a
* git_diff_tree_to_workdir against tree
* 873585b94bdeabccea991ea5e3ec1a277895b698. As for what you should actually
* test, you can start by just checking that the number of lines of diff
* content matches the actual output of git diff. That will at least
* demonstrate that the submodule content is being used to generate somewhat
* comparable outputs. It is a test that would fail without this code and
* will succeed with it.
*/
#include "../submodule/submodule_helpers.h"
void test_diff_workdir__submodules(void)
{
const char *a_commit = "873585b94bdeabccea991ea5e3ec1a277895b698";
git_tree *a;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
g_repo = setup_fixture_submod2();
a = resolve_commit_oid_to_tree(g_repo, a_commit);
opts.flags =
GIT_DIFF_INCLUDE_UNTRACKED |
GIT_DIFF_INCLUDE_IGNORED |
GIT_DIFF_RECURSE_UNTRACKED_DIRS |
GIT_DIFF_SHOW_UNTRACKED_CONTENT;
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts));
/* diff_print(stderr, diff); */
/* essentially doing: git diff 873585b94bdeabccea991ea5e3ec1a277895b698 */
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
/* so "git diff 873585" returns:
* M .gitmodules
* A just_a_dir/contents
* A just_a_file
* A sm_added_and_uncommited
* A sm_changed_file
* A sm_changed_head
* A sm_changed_index
* A sm_changed_untracked_file
* M sm_missing_commits
* A sm_unchanged
* which is a little deceptive because of the difference between the
* "git diff <treeish>" results from "git_diff_tree_to_workdir". The
* only significant difference is that those Added items will show up
* as Untracked items in the pure libgit2 diff.
*
* Then add in the two extra untracked items "not" and "not-submodule"
* to get the 12 files reported here.
*/
cl_assert_equal_i(12, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(10, exp.file_status[GIT_DELTA_UNTRACKED]);
/* the following numbers match "git diff 873585" exactly */
cl_assert_equal_i(9, exp.hunks);
cl_assert_equal_i(33, exp.lines);
cl_assert_equal_i(2, exp.line_ctxt);
cl_assert_equal_i(30, exp.line_adds);
cl_assert_equal_i(1, exp.line_dels);
git_diff_free(diff);
git_tree_free(a);
}
void test_diff_workdir__cannot_diff_against_a_bare_repository(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_tree *tree;
g_repo = cl_git_sandbox_init("testrepo.git");
cl_assert_equal_i(
GIT_EBAREREPO, git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_repository_head_tree(&tree, g_repo));
cl_assert_equal_i(
GIT_EBAREREPO, git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
git_tree_free(tree);
}
void test_diff_workdir__to_null_tree(void)
{
git_diff *diff;
diff_expects exp;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
opts.flags = GIT_DIFF_INCLUDE_UNTRACKED |
GIT_DIFF_RECURSE_UNTRACKED_DIRS;
g_repo = cl_git_sandbox_init("status");
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, NULL, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(exp.files, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
}
void test_diff_workdir__checks_options_version(void)
{
git_diff *diff;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
const git_error *err;
g_repo = cl_git_sandbox_init("status");
opts.version = 0;
cl_git_fail(git_diff_tree_to_workdir(&diff, g_repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
git_error_clear();
opts.version = 1024;
cl_git_fail(git_diff_tree_to_workdir(&diff, g_repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
}
void test_diff_workdir__can_diff_empty_file(void)
{
git_diff *diff;
git_tree *tree;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_patch *patch;
struct stat st = {0};
g_repo = cl_git_sandbox_init("attr_index");
tree = resolve_commit_oid_to_tree(g_repo, "3812cfef3661"); /* HEAD */
/* baseline - make sure there are no outstanding diffs */
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
cl_assert_equal_i(2, (int)git_diff_num_deltas(diff));
git_diff_free(diff);
/* empty contents of file */
cl_git_rewritefile("attr_index/README.txt", "");
cl_git_pass(git_fs_path_lstat("attr_index/README.txt", &st));
if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
cl_assert_equal_sz(0, st.st_size);
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
cl_assert_equal_i(3, (int)git_diff_num_deltas(diff));
/* diffs are: .gitattributes, README.txt, sub/sub/.gitattributes */
cl_git_pass(git_patch_from_diff(&patch, diff, 1));
git_patch_free(patch);
git_diff_free(diff);
/* remove a file altogether */
cl_git_pass(p_unlink("attr_index/README.txt"));
cl_assert(!git_fs_path_exists("attr_index/README.txt"));
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
cl_assert_equal_i(3, (int)git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 1));
git_patch_free(patch);
git_diff_free(diff);
git_tree_free(tree);
}
void test_diff_workdir__to_index_issue_1397(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
g_repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
opts.context_lines = 3;
opts.interhunk_lines = 1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.hunks);
cl_assert_equal_i(0, exp.lines);
git_diff_free(diff);
diff = NULL;
cl_git_rewritefile("issue_1397/crlf_file.txt",
"first line\r\nsecond line modified\r\nboth with crlf");
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.hunks);
cl_assert_equal_i(5, exp.lines);
cl_assert_equal_i(3, exp.line_ctxt);
cl_assert_equal_i(1, exp.line_adds);
cl_assert_equal_i(1, exp.line_dels);
git_diff_free(diff);
}
void test_diff_workdir__to_tree_issue_1397(void)
{
const char *a_commit = "7f483a738"; /* the current HEAD */
git_tree *a;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_diff *diff2 = NULL;
diff_expects exp;
g_repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
a = resolve_commit_oid_to_tree(g_repo, a_commit);
opts.context_lines = 3;
opts.interhunk_lines = 1;
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.hunks);
cl_assert_equal_i(0, exp.lines);
git_diff_free(diff);
diff = NULL;
cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts));
cl_git_pass(git_diff_merge(diff, diff2));
git_diff_free(diff2);
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(0, exp.files);
cl_assert_equal_i(0, exp.hunks);
cl_assert_equal_i(0, exp.lines);
git_diff_free(diff);
git_tree_free(a);
}
void test_diff_workdir__untracked_directory_scenarios(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
diff_expects exp;
char *pathspec = NULL;
static const char *files0[] = {
"subdir/deleted_file",
"subdir/modified_file",
"subdir/new_file",
NULL
};
static const char *files1[] = {
"subdir/deleted_file",
"subdir/directory/",
"subdir/modified_file",
"subdir/new_file",
NULL
};
static const char *files2[] = {
"subdir/deleted_file",
"subdir/directory/more/notignored",
"subdir/modified_file",
"subdir/new_file",
NULL
};
g_repo = cl_git_sandbox_init("status");
cl_git_mkfile("status/.gitignore", "ignored\n");
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.pathspec.strings = &pathspec;
opts.pathspec.count = 1;
pathspec = "subdir";
/* baseline for "subdir" pathspec */
memset(&exp, 0, sizeof(exp));
exp.names = files0;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(3, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* empty directory */
cl_git_pass(p_mkdir("status/subdir/directory", 0777));
memset(&exp, 0, sizeof(exp));
exp.names = files1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* empty directory in empty directory */
cl_git_pass(p_mkdir("status/subdir/directory/empty", 0777));
memset(&exp, 0, sizeof(exp));
exp.names = files1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* directory with only ignored files */
cl_git_pass(p_mkdir("status/subdir/directory/deeper", 0777));
cl_git_mkfile("status/subdir/directory/deeper/ignored", "ignore me\n");
cl_git_pass(p_mkdir("status/subdir/directory/another", 0777));
cl_git_mkfile("status/subdir/directory/another/ignored", "ignore me\n");
memset(&exp, 0, sizeof(exp));
exp.names = files1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* directory with ignored directory (contents irrelevant) */
cl_git_pass(p_mkdir("status/subdir/directory/more", 0777));
cl_git_pass(p_mkdir("status/subdir/directory/more/ignored", 0777));
cl_git_mkfile("status/subdir/directory/more/ignored/notignored",
"inside ignored dir\n");
memset(&exp, 0, sizeof(exp));
exp.names = files1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* quick version avoids directory scan */
opts.flags = opts.flags | GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS;
memset(&exp, 0, sizeof(exp));
exp.names = files1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* directory with nested non-ignored content */
opts.flags = opts.flags & ~GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS;
cl_git_mkfile("status/subdir/directory/more/notignored",
"not ignored deep under untracked\n");
memset(&exp, 0, sizeof(exp));
exp.names = files1;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
/* use RECURSE_UNTRACKED_DIRS to get actual untracked files (no ignores) */
opts.flags = opts.flags & ~GIT_DIFF_INCLUDE_IGNORED;
opts.flags = opts.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
memset(&exp, 0, sizeof(exp));
exp.names = files2;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_diff_foreach(diff, diff_file_cb, NULL, NULL, NULL, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]);
git_diff_free(diff);
}
void test_diff_workdir__untracked_directory_comes_last(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
g_repo = cl_git_sandbox_init("renames");
cl_git_mkfile("renames/.gitignore", "*.ign\n");
cl_git_pass(p_mkdir("renames/zzz_untracked", 0777));
cl_git_mkfile("renames/zzz_untracked/an.ign", "ignore me please");
cl_git_mkfile("renames/zzz_untracked/skip.ign", "ignore me really");
cl_git_mkfile("renames/zzz_untracked/test.ign", "ignore me now");
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_assert(diff != NULL);
git_diff_free(diff);
}
void test_diff_workdir__untracked_with_bom(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
const git_diff_delta *delta;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_write2file("empty_standard_repo/bom.txt",
"\xFF\xFE\x31\x00\x32\x00\x33\x00\x34\x00", 10, O_WRONLY|O_CREAT, 0664);
opts.flags =
GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_SHOW_UNTRACKED_CONTENT;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
cl_assert((delta = git_diff_get_delta(diff, 0)) != NULL);
cl_assert_equal_i(GIT_DELTA_UNTRACKED, delta->status);
/* not known at this point
* cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
*/
git_diff_free(diff);
}
void test_diff_workdir__patience_diff(void)
{
git_index *index;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_patch *patch = NULL;
git_buf buf = GIT_BUF_INIT;
const char *expected_normal = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n-how to create\n-a patience diff\n I did not know\n how to create\n+a patience diff\n another problem\n-I did not know\n-how to create\n a minimal diff\n";
const char *expected_patience = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n+I did not know\n how to create\n a patience diff\n-I did not know\n-how to create\n another problem\n-I did not know\n-how to create\n a minimal diff\n";
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_mkfile(
"empty_standard_repo/test.txt",
"When I wrote this\nI did not know\nhow to create\na patience diff\nI did not know\nhow to create\nanother problem\nI did not know\nhow to create\na minimal diff\n");
cl_git_pass(git_index_add_bypath(index, "test.txt"));
cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "Base");
git_index_free(index);
cl_git_rewritefile(
"empty_standard_repo/test.txt",
"When I wrote this\nI did not know\nI did not know\nhow to create\na patience diff\nanother problem\na minimal diff\n");
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected_normal, buf.ptr);
git_buf_dispose(&buf);
git_patch_free(patch);
git_diff_free(diff);
opts.flags |= GIT_DIFF_PATIENCE;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(expected_patience, buf.ptr);
git_buf_dispose(&buf);
git_patch_free(patch);
git_diff_free(diff);
}
void test_diff_workdir__with_stale_index(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_index *idx = NULL;
diff_expects exp;
g_repo = cl_git_sandbox_init("status");
cl_git_pass(git_repository_index(&idx, g_repo));
/* make the in-memory index invalid */
{
git_repository *r2;
git_index *idx2;
cl_git_pass(git_repository_open(&r2, "status"));
cl_git_pass(git_repository_index(&idx2, r2));
cl_git_pass(git_index_add_bypath(idx2, "new_file"));
cl_git_pass(git_index_add_bypath(idx2, "subdir/new_file"));
cl_git_pass(git_index_remove_bypath(idx2, "staged_new_file"));
cl_git_pass(git_index_remove_bypath(idx2, "staged_changes_file_deleted"));
cl_git_pass(git_index_write(idx2));
git_index_free(idx2);
git_repository_free(r2);
}
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_UNMODIFIED;
/* first try with index pointer which should prevent reload */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, idx, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(17, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);
cl_assert_equal_i(5, exp.file_status[GIT_DELTA_UNMODIFIED]);
git_diff_free(diff);
/* now let's try without the index pointer which should trigger reload */
/* two files that were UNTRACKED should have become UNMODIFIED */
/* one file that was UNMODIFIED should now have become UNTRACKED */
/* one file that was DELETED should now be gone completely */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
git_diff_free(diff);
cl_assert_equal_i(16, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);
cl_assert_equal_i(6, exp.file_status[GIT_DELTA_UNMODIFIED]);
git_index_free(idx);
}
static int touch_file(void *payload, git_str *path)
{
struct stat st;
struct p_timeval times[2];
GIT_UNUSED(payload);
if (git_fs_path_isdir(path->ptr))
return 0;
cl_must_pass(p_stat(path->ptr, &st));
times[0].tv_sec = st.st_mtime + 3;
times[0].tv_usec = 0;
times[1].tv_sec = st.st_mtime + 3;
times[1].tv_usec = 0;
cl_must_pass(p_utimes(path->ptr, times));
return 0;
}
static void basic_diff_status(git_diff **out, const git_diff_options *opts)
{
diff_expects exp;
cl_git_pass(git_diff_index_to_workdir(out, g_repo, NULL, opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
*out, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]);
cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]);
}
void test_diff_workdir__can_update_index(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_diff_perfdata perf = GIT_DIFF_PERFDATA_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("status");
/* touch all the files so stat times are different */
{
git_str path = GIT_STR_INIT;
cl_git_pass(git_str_sets(&path, "status"));
cl_git_pass(git_fs_path_direach(&path, 0, touch_file, NULL));
git_str_dispose(&path);
}
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
basic_diff_status(&diff, &opts);
cl_git_pass(git_diff_get_perfdata(&perf, diff));
cl_assert_equal_sz(13 + 3, perf.stat_calls);
cl_assert_equal_sz(5, perf.oid_calculations);
git_diff_free(diff);
/* now allow diff to update stat cache */
opts.flags |= GIT_DIFF_UPDATE_INDEX;
/* advance a tick for the index so we don't re-calculate racily-clean entries */
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
tick_index(index);
basic_diff_status(&diff, &opts);
cl_git_pass(git_diff_get_perfdata(&perf, diff));
cl_assert_equal_sz(13 + 3, perf.stat_calls);
cl_assert_equal_sz(5, perf.oid_calculations);
git_diff_free(diff);
/* now if we do it again, we should see fewer OID calculations */
/* tick again as the index updating from the previous diff might have reset the timestamp */
tick_index(index);
basic_diff_status(&diff, &opts);
cl_git_pass(git_diff_get_perfdata(&perf, diff));
cl_assert_equal_sz(13 + 3, perf.stat_calls);
cl_assert_equal_sz(0, perf.oid_calculations);
git_diff_free(diff);
}
#define STR7 "0123456"
#define STR8 "01234567"
#define STR40 STR8 STR8 STR8 STR8 STR8
#define STR200 STR40 STR40 STR40 STR40 STR40
#define STR999Z STR200 STR200 STR200 STR200 STR40 STR40 STR40 STR40 \
STR8 STR8 STR8 STR8 STR7 "\0"
#define STR1000 STR200 STR200 STR200 STR200 STR200
#define STR3999Z STR1000 STR1000 STR1000 STR999Z
#define STR4000 STR1000 STR1000 STR1000 STR1000
static void assert_delta_binary(git_diff *diff, size_t idx, int is_binary)
{
git_patch *patch;
const git_diff_delta *delta;
cl_git_pass(git_patch_from_diff(&patch, diff, idx));
delta = git_patch_get_delta(patch);
cl_assert_equal_b((delta->flags & GIT_DIFF_FLAG_BINARY), is_binary);
git_patch_free(patch);
}
void test_diff_workdir__binary_detection(void)
{
git_index *idx;
git_diff *diff = NULL;
git_str b = GIT_STR_INIT;
int i;
git_str data[10] = {
{ "1234567890", 0, 10 }, /* 0 - all ascii text control */
{ "\xC3\x85\xC3\xBC\xE2\x80\xA0\x48\xC3\xB8\xCF\x80\xCE\xA9", 0, 14 }, /* 1 - UTF-8 multibyte text */
{ "\xEF\xBB\xBF\xC3\x9C\xE2\xA4\x92\xC6\x92\x38\xC2\xA3\xE2\x82\xAC", 0, 16 }, /* 2 - UTF-8 with BOM */
{ STR999Z, 0, 1000 }, /* 3 - ASCII with NUL at 1000 */
{ STR3999Z, 0, 4000 }, /* 4 - ASCII with NUL at 4000 */
{ STR4000 STR3999Z "x", 0, 8001 }, /* 5 - ASCII with NUL at 8000 */
{ STR4000 STR4000 "\0", 0, 8001 }, /* 6 - ASCII with NUL at 8001 */
{ "\x00\xDC\x00\x6E\x21\x39\xFE\x0E\x00\x63\x00\xF8"
"\x00\x64\x00\x65\x20\x48", 0, 18 }, /* 7 - UTF-16 text */
{ "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d"
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d",
0, 26 }, /* 8 - All non-printable characters (no NUL) */
{ "Hello \x01\x02\x03\x04\x05\x06 World!\x01\x02\x03\x04"
"\x05\x06\x07", 0, 26 }, /* 9 - 50-50 non-printable (no NUL) */
};
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_repository_index(&idx, g_repo));
/* We start with ASCII in index and test data in workdir,
* then we will try with test data in index and ASCII in workdir.
*/
cl_git_pass(git_str_sets(&b, "empty_standard_repo/0"));
for (i = 0; i < 10; ++i) {
b.ptr[b.size - 1] = '0' + i;
cl_git_mkfile(b.ptr, "baseline");
cl_git_pass(git_index_add_bypath(idx, &b.ptr[b.size - 1]));
if (data[i].size == 0)
data[i].size = strlen(data[i].ptr);
cl_git_write2file(
b.ptr, data[i].ptr, data[i].size, O_WRONLY|O_TRUNC, 0664);
}
cl_git_pass(git_index_write(idx));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
cl_assert_equal_i(10, git_diff_num_deltas(diff));
/* using diff binary detection (i.e. looking for NUL byte) */
assert_delta_binary(diff, 0, false);
assert_delta_binary(diff, 1, false);
assert_delta_binary(diff, 2, false);
assert_delta_binary(diff, 3, true);
assert_delta_binary(diff, 4, true);
assert_delta_binary(diff, 5, true);
assert_delta_binary(diff, 6, false);
assert_delta_binary(diff, 7, true);
assert_delta_binary(diff, 8, false);
assert_delta_binary(diff, 9, false);
/* The above have been checked to match command-line Git */
git_diff_free(diff);
cl_git_pass(git_str_sets(&b, "empty_standard_repo/0"));
for (i = 0; i < 10; ++i) {
b.ptr[b.size - 1] = '0' + i;
cl_git_pass(git_index_add_bypath(idx, &b.ptr[b.size - 1]));
cl_git_write2file(b.ptr, "baseline\n", 9, O_WRONLY|O_TRUNC, 0664);
}
cl_git_pass(git_index_write(idx));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
cl_assert_equal_i(10, git_diff_num_deltas(diff));
/* using diff binary detection (i.e. looking for NUL byte) */
assert_delta_binary(diff, 0, false);
assert_delta_binary(diff, 1, false);
assert_delta_binary(diff, 2, false);
assert_delta_binary(diff, 3, true);
assert_delta_binary(diff, 4, true);
assert_delta_binary(diff, 5, true);
assert_delta_binary(diff, 6, false);
assert_delta_binary(diff, 7, true);
assert_delta_binary(diff, 8, false);
assert_delta_binary(diff, 9, false);
git_diff_free(diff);
git_index_free(idx);
git_str_dispose(&b);
}
void test_diff_workdir__to_index_conflicted(void) {
const char *a_commit = "26a125ee1bf"; /* the current HEAD */
git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}};
git_tree *a;
git_index *index;
git_diff *diff1, *diff2;
const git_diff_delta *delta;
g_repo = cl_git_sandbox_init("status");
a = resolve_commit_oid_to_tree(g_repo, a_commit);
cl_git_pass(git_repository_index(&index, g_repo));
ancestor.path = ours.path = theirs.path = "_file";
ancestor.mode = ours.mode = theirs.mode = 0100644;
git_oid__fromstr(&ancestor.id, "d427e0b2e138501a3d15cc376077a3631e15bd46", GIT_OID_SHA1);
git_oid__fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
git_oid__fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
cl_git_pass(git_diff_tree_to_index(&diff1, g_repo, a, index, NULL));
cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, index, NULL));
cl_git_pass(git_diff_merge(diff1, diff2));
cl_assert_equal_i(git_diff_num_deltas(diff1), 12);
delta = git_diff_get_delta(diff1, 0);
cl_assert_equal_s(delta->old_file.path, "_file");
cl_assert_equal_i(delta->nfiles, 1);
cl_assert_equal_i(delta->status, GIT_DELTA_CONFLICTED);
git_diff_free(diff2);
git_diff_free(diff1);
git_index_free(index);
git_tree_free(a);
}
void test_diff_workdir__only_writes_index_when_necessary(void)
{
git_index *index;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_reference *head;
git_object *head_object;
unsigned char initial[GIT_HASH_SHA1_SIZE],
first[GIT_HASH_SHA1_SIZE],
second[GIT_HASH_SHA1_SIZE];
git_str path = GIT_STR_INIT;
struct stat st;
struct p_timeval times[2];
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_UPDATE_INDEX;
g_repo = cl_git_sandbox_init("status");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_repository_head(&head, g_repo));
cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(g_repo, head_object, GIT_RESET_HARD, NULL));
memcpy(initial, git_index__checksum(index), GIT_HASH_SHA1_SIZE);
/* update the index timestamp to avoid raciness */
cl_must_pass(p_stat("status/.git/index", &st));
times[0].tv_sec = st.st_mtime + 5;
times[0].tv_usec = 0;
times[1].tv_sec = st.st_mtime + 5;
times[1].tv_usec = 0;
cl_must_pass(p_utimes("status/.git/index", times));
/* ensure diff doesn't touch the index */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
git_diff_free(diff);
memcpy(first, git_index__checksum(index), GIT_HASH_SHA1_SIZE);
cl_assert(memcmp(initial, first, GIT_HASH_SHA1_SIZE) != 0);
/* touch all the files so stat times are different */
cl_git_pass(git_str_sets(&path, "status"));
cl_git_pass(git_fs_path_direach(&path, 0, touch_file, NULL));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
git_diff_free(diff);
/* ensure the second diff did update the index */
memcpy(second, git_index__checksum(index), GIT_HASH_SHA1_SIZE);
cl_assert(memcmp(first, second, GIT_HASH_SHA1_SIZE) != 0);
git_str_dispose(&path);
git_object_free(head_object);
git_reference_free(head);
git_index_free(index);
}
void test_diff_workdir__to_index_pathlist(void)
{
git_index *index;
git_diff *diff;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_vector pathlist = GIT_VECTOR_INIT;
git_vector_insert(&pathlist, "foobar/asdf");
git_vector_insert(&pathlist, "subdir/asdf");
git_vector_insert(&pathlist, "ignored/asdf");
g_repo = cl_git_sandbox_init("status");
cl_git_mkfile("status/.gitignore", ".gitignore\n" "ignored/\n");
cl_must_pass(p_mkdir("status/foobar", 0777));
cl_git_mkfile("status/foobar/one", "one\n");
cl_must_pass(p_mkdir("status/ignored", 0777));
cl_git_mkfile("status/ignored/one", "one\n");
cl_git_mkfile("status/ignored/two", "two\n");
cl_git_mkfile("status/ignored/three", "three\n");
cl_git_pass(git_repository_index(&index, g_repo));
opts.flags = GIT_DIFF_INCLUDE_IGNORED;
opts.pathspec.strings = (char **)pathlist.contents;
opts.pathspec.count = pathlist.length;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &opts));
cl_assert_equal_i(0, git_diff_num_deltas(diff));
git_diff_free(diff);
opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &opts));
cl_assert_equal_i(0, git_diff_num_deltas(diff));
git_diff_free(diff);
git_index_free(index);
git_vector_free(&pathlist);
}
void test_diff_workdir__symlink_changed_on_non_symlink_platform(void)
{
git_tree *tree;
git_diff *diff;
diff_expects exp = {0};
const git_diff_delta *delta;
const char *commit = "7fccd7";
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_vector pathlist = GIT_VECTOR_INIT;
int symlinks;
g_repo = cl_git_sandbox_init("unsymlinked.git");
cl_git_pass(git_repository__configmap_lookup(&symlinks, g_repo, GIT_CONFIGMAP_SYMLINKS));
if (symlinks)
cl_skip();
cl_git_pass(git_vector_insert(&pathlist, "include/Nu/Nu.h"));
opts.pathspec.strings = (char **)pathlist.contents;
opts.pathspec.count = pathlist.length;
cl_must_pass(p_mkdir("symlink", 0777));
cl_git_pass(git_repository_set_workdir(g_repo, "symlink", false));
cl_assert((tree = resolve_commit_oid_to_tree(g_repo, commit)) != NULL);
/* first, do the diff with the original contents */
cl_git_pass(git_futils_mkpath2file("symlink/include/Nu/Nu.h", 0755));
cl_git_mkfile("symlink/include/Nu/Nu.h", "../../objc/Nu.h");
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
cl_assert_equal_i(0, git_diff_num_deltas(diff));
git_diff_free(diff);
/* now update the contents and expect a difference, but that the file
* mode has persisted as a symbolic link.
*/
cl_git_rewritefile("symlink/include/Nu/Nu.h", "awesome content\n");
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(1, exp.files);
cl_assert_equal_i(1, git_diff_num_deltas(diff));
delta = git_diff_get_delta(diff, 0);
cl_assert_equal_i(GIT_FILEMODE_LINK, delta->old_file.mode);
cl_assert_equal_i(GIT_FILEMODE_LINK, delta->new_file.mode);
git_diff_free(diff);
cl_git_pass(git_futils_rmdir_r("symlink", NULL, GIT_RMDIR_REMOVE_FILES));
git_tree_free(tree);
git_vector_free(&pathlist);
}
void test_diff_workdir__order(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_buf patch = GIT_BUF_INIT;
git_oid tree_oid, blob_oid;
git_treebuilder *builder;
git_tree *tree;
git_diff *diff;
g_repo = cl_git_sandbox_init("empty_standard_repo");
/* Build tree with a single file "abc.txt" */
cl_git_pass(git_blob_create_from_buffer(&blob_oid, g_repo, "foo\n", 4));
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
cl_git_pass(git_treebuilder_insert(NULL, builder, "abc.txt", &blob_oid, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_write(&tree_oid, builder));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
/* Create a directory that sorts before and one that sorts after "abc.txt" */
cl_git_mkfile("empty_standard_repo/abc.txt", "bar\n");
cl_must_pass(p_mkdir("empty_standard_repo/abb", 0777));
cl_must_pass(p_mkdir("empty_standard_repo/abd", 0777));
opts.flags = GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
cl_git_pass(git_diff_to_buf(&patch, diff, GIT_DIFF_FORMAT_PATCH));
cl_assert_equal_s(patch.ptr,
"diff --git a/abc.txt b/abc.txt\n"
"index 257cc56..5716ca5 100644\n"
"--- a/abc.txt\n"
"+++ b/abc.txt\n"
"@@ -1 +1 @@\n"
"-foo\n"
"+bar\n");
git_treebuilder_free(builder);
git_buf_dispose(&patch);
git_diff_free(diff);
git_tree_free(tree);
}
void test_diff_workdir__ignore_blank_lines(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff;
git_patch *patch;
git_buf buf = GIT_BUF_INIT;
g_repo = cl_git_sandbox_init("rebase");
cl_git_rewritefile("rebase/gravy.txt", "GRAVY SOUP.\n\n\nGet eight pounds of coarse lean beef--wash it clean and lay it in your\n\npot, put in the same ingredients as for the shin soup, with the same\nquantity of water, and follow the process directed for that. Strain the\nsoup through a sieve, and serve it up clear, with nothing more than\ntoasted bread in it; two table-spoonsful of mushroom catsup will add a\nfine flavour to the soup!\n");
/* Perform the diff normally */
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s("diff --git a/gravy.txt b/gravy.txt\nindex c4e6cca..3c617e6 100644\n--- a/gravy.txt\n+++ b/gravy.txt\n@@ -1,8 +1,10 @@\n GRAVY SOUP.\n \n+\n Get eight pounds of coarse lean beef--wash it clean and lay it in your\n+\n pot, put in the same ingredients as for the shin soup, with the same\n quantity of water, and follow the process directed for that. Strain the\n soup through a sieve, and serve it up clear, with nothing more than\n toasted bread in it; two table-spoonsful of mushroom catsup will add a\n-fine flavour to the soup.\n+fine flavour to the soup!\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(patch);
git_diff_free(diff);
/* Perform the diff ignoring blank lines */
opts.flags |= GIT_DIFF_IGNORE_BLANK_LINES;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
cl_git_pass(git_patch_from_diff(&patch, diff, 0));
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s("diff --git a/gravy.txt b/gravy.txt\nindex c4e6cca..3c617e6 100644\n--- a/gravy.txt\n+++ b/gravy.txt\n@@ -5,4 +7,4 @@ pot, put in the same ingredients as for the shin soup, with the same\n quantity of water, and follow the process directed for that. Strain the\n soup through a sieve, and serve it up clear, with nothing more than\n toasted bread in it; two table-spoonsful of mushroom catsup will add a\n-fine flavour to the soup.\n+fine flavour to the soup!\n", buf.ptr);
git_buf_dispose(&buf);
git_patch_free(patch);
git_diff_free(diff);
}
| libgit2-main | tests/libgit2/diff/workdir.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
static git_repository *g_repo = NULL;
static git_diff_options opts;
static git_diff *diff;
static git_tree *a, *b;
static diff_expects expect;
void test_diff_tree__initialize(void)
{
cl_git_pass(git_diff_options_init(&opts, GIT_DIFF_OPTIONS_VERSION));
memset(&expect, 0, sizeof(expect));
diff = NULL;
a = NULL;
b = NULL;
}
void test_diff_tree__cleanup(void)
{
git_diff_free(diff);
git_tree_free(a);
git_tree_free(b);
cl_git_sandbox_cleanup();
}
void test_diff_tree__0(void)
{
/* grabbed a couple of commit oids from the history of the attr repo */
const char *a_commit = "605812a";
const char *b_commit = "370fe9ec22";
const char *c_commit = "f5b0af1fb4f5c";
git_tree *c;
g_repo = cl_git_sandbox_init("attr");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
opts.context_lines = 1;
opts.interhunk_lines = 1;
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(5, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(5, expect.hunks);
cl_assert_equal_i(7 + 24 + 1 + 6 + 6, expect.lines);
cl_assert_equal_i(1, expect.line_ctxt);
cl_assert_equal_i(24 + 1 + 5 + 5, expect.line_adds);
cl_assert_equal_i(7 + 1, expect.line_dels);
git_diff_free(diff);
diff = NULL;
memset(&expect, 0, sizeof(expect));
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, b, &opts));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(2, expect.files);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(2, expect.hunks);
cl_assert_equal_i(8 + 15, expect.lines);
cl_assert_equal_i(1, expect.line_ctxt);
cl_assert_equal_i(1, expect.line_adds);
cl_assert_equal_i(7 + 14, expect.line_dels);
git_tree_free(c);
}
#define DIFF_OPTS(FLAGS, CTXT) \
{GIT_DIFF_OPTIONS_VERSION, (FLAGS), GIT_SUBMODULE_IGNORE_UNSPECIFIED, \
{NULL,0}, NULL, NULL, NULL, (CTXT), 1}
void test_diff_tree__options(void)
{
/* grabbed a couple of commit oids from the history of the attr repo */
const char *a_commit = "6bab5c79cd5140d0";
const char *b_commit = "605812ab7fe421fdd";
const char *c_commit = "f5b0af1fb4f5";
const char *d_commit = "a97cc019851";
git_tree *c, *d;
diff_expects actual;
int test_ab_or_cd[] = { 0, 0, 0, 0, 1, 1, 1, 1, 1 };
git_diff_options test_options[] = {
/* a vs b tests */
DIFF_OPTS(GIT_DIFF_NORMAL, 1),
DIFF_OPTS(GIT_DIFF_NORMAL, 3),
DIFF_OPTS(GIT_DIFF_REVERSE, 2),
DIFF_OPTS(GIT_DIFF_FORCE_TEXT, 2),
/* c vs d tests */
DIFF_OPTS(GIT_DIFF_NORMAL, 3),
DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE, 3),
DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3),
DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE_EOL, 3),
DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1),
};
/* to generate these values:
* - cd to tests/resources/attr,
* - mv .gitted .git
* - git diff [options] 6bab5c79cd5140d0 605812ab7fe421fdd
* - mv .git .gitted
*/
#define EXPECT_STATUS_ADM(ADDS,DELS,MODS) { 0, ADDS, DELS, MODS, 0, 0, 0, 0, 0 }
diff_expects test_expects[] = {
/* a vs b tests */
{ 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 51, 2, 46, 3 },
{ 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 53, 4, 46, 3 },
{ 5, 0, EXPECT_STATUS_ADM(0, 3, 2), 4, 0, 0, 52, 3, 3, 46 },
{ 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 5, 0, 0, 54, 3, 47, 4 },
/* c vs d tests */
{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 22, 9, 10, 3 },
{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 19, 12, 7, 0 },
{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 },
{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 },
{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 18, 11, 0, 7 },
{ 0 },
};
diff_expects *expected;
int i, j;
g_repo = cl_git_sandbox_init("attr");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
cl_assert((d = resolve_commit_oid_to_tree(g_repo, d_commit)) != NULL);
for (i = 0; test_expects[i].files > 0; i++) {
memset(&actual, 0, sizeof(actual)); /* clear accumulator */
opts = test_options[i];
if (test_ab_or_cd[i] == 0)
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
else
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, d, &opts));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &actual));
expected = &test_expects[i];
cl_assert_equal_i(actual.files, expected->files);
for (j = GIT_DELTA_UNMODIFIED; j <= GIT_DELTA_TYPECHANGE; ++j)
cl_assert_equal_i(expected->file_status[j], actual.file_status[j]);
cl_assert_equal_i(actual.hunks, expected->hunks);
cl_assert_equal_i(actual.lines, expected->lines);
cl_assert_equal_i(actual.line_ctxt, expected->line_ctxt);
cl_assert_equal_i(actual.line_adds, expected->line_adds);
cl_assert_equal_i(actual.line_dels, expected->line_dels);
git_diff_free(diff);
diff = NULL;
}
git_tree_free(c);
git_tree_free(d);
}
void test_diff_tree__bare(void)
{
const char *a_commit = "8496071c1b46c85";
const char *b_commit = "be3563ae3f79";
g_repo = cl_git_sandbox_init("testrepo.git");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
opts.context_lines = 1;
opts.interhunk_lines = 1;
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(3, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(3, expect.hunks);
cl_assert_equal_i(4, expect.lines);
cl_assert_equal_i(0, expect.line_ctxt);
cl_assert_equal_i(3, expect.line_adds);
cl_assert_equal_i(1, expect.line_dels);
}
void test_diff_tree__merge(void)
{
/* grabbed a couple of commit oids from the history of the attr repo */
const char *a_commit = "605812a";
const char *b_commit = "370fe9ec22";
const char *c_commit = "f5b0af1fb4f5c";
git_tree *c;
git_diff *diff1 = NULL, *diff2 = NULL;
g_repo = cl_git_sandbox_init("attr");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
cl_git_pass(git_diff_tree_to_tree(&diff1, g_repo, a, b, NULL));
cl_git_pass(git_diff_tree_to_tree(&diff2, g_repo, c, b, NULL));
git_tree_free(c);
cl_git_pass(git_diff_merge(diff1, diff2));
git_diff_free(diff2);
cl_git_pass(git_diff_foreach(
diff1, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(6, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(3, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(6, expect.hunks);
cl_assert_equal_i(59, expect.lines);
cl_assert_equal_i(1, expect.line_ctxt);
cl_assert_equal_i(36, expect.line_adds);
cl_assert_equal_i(22, expect.line_dels);
git_diff_free(diff1);
}
void test_diff_tree__larger_hunks(void)
{
const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69";
const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10";
size_t d, num_d, h, num_h, l, num_l;
git_patch *patch;
const git_diff_hunk *hunk;
const git_diff_line *line;
g_repo = cl_git_sandbox_init("diff");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
opts.context_lines = 1;
opts.interhunk_lines = 0;
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
cl_git_pass(git_patch_from_diff(&patch, diff, d));
cl_assert(patch);
num_h = git_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
for (l = 0; l < num_l; ++l) {
cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
cl_assert(line);
}
cl_git_fail(git_patch_get_line_in_hunk(&line, patch, h, num_l));
}
cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h));
git_patch_free(patch);
}
cl_git_fail(git_patch_from_diff(&patch, diff, num_d));
cl_assert_equal_i(2, (int)num_d);
}
void test_diff_tree__checks_options_version(void)
{
const char *a_commit = "8496071c1b46c85";
const char *b_commit = "be3563ae3f79";
const git_error *err;
g_repo = cl_git_sandbox_init("testrepo.git");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
opts.version = 0;
cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
err = git_error_last();
cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
git_error_clear();
opts.version = 1024;
cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
err = git_error_last();
}
static void process_tree_to_tree_diffing(
const char *old_commit,
const char *new_commit)
{
g_repo = cl_git_sandbox_init("unsymlinked.git");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, old_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, new_commit)) != NULL);
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, NULL, NULL, NULL, &expect));
}
void test_diff_tree__symlink_blob_mode_changed_to_regular_file(void)
{
/*
* $ git diff 7fccd7..806999
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
* deleted file mode 120000
* index 19bf568..0000000
* --- a/include/Nu/Nu.h
* +++ /dev/null
* @@ -1 +0,0 @@
* -../../objc/Nu.h
* \ No newline at end of file
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
* new file mode 100644
* index 0000000..f9e6561
* --- /dev/null
* +++ b/include/Nu/Nu.h
* @@ -0,0 +1 @@
* +awesome content
* diff --git a/objc/Nu.h b/objc/Nu.h
* deleted file mode 100644
* index f9e6561..0000000
* --- a/objc/Nu.h
* +++ /dev/null
* @@ -1 +0,0 @@
* -awesome content
*/
process_tree_to_tree_diffing("7fccd7", "806999");
cl_assert_equal_i(3, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]);
}
void test_diff_tree__symlink_blob_mode_changed_to_regular_file_as_typechange(void)
{
/*
* $ git diff 7fccd7..a8595c
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
* deleted file mode 120000
* index 19bf568..0000000
* --- a/include/Nu/Nu.h
* +++ /dev/null
* @@ -1 +0,0 @@
* -../../objc/Nu.h
* \ No newline at end of file
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
* new file mode 100755
* index 0000000..f9e6561
* --- /dev/null
* +++ b/include/Nu/Nu.h
* @@ -0,0 +1 @@
* +awesome content
* diff --git a/objc/Nu.h b/objc/Nu.h
* deleted file mode 100644
* index f9e6561..0000000
* --- a/objc/Nu.h
* +++ /dev/null
* @@ -1 +0,0 @@
* -awesome content
*/
opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
process_tree_to_tree_diffing("7fccd7", "a8595c");
cl_assert_equal_i(2, expect.files);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_TYPECHANGE]);
}
void test_diff_tree__regular_blob_mode_changed_to_executable_file(void)
{
/*
* $ git diff 806999..a8595c
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
* old mode 100644
* new mode 100755
*/
process_tree_to_tree_diffing("806999", "a8595c");
cl_assert_equal_i(1, expect.files);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]);
}
void test_diff_tree__issue_1397(void)
{
/* this test shows that it is not needed */
g_repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_assert((a = resolve_commit_oid_to_tree(g_repo, "8a7ef04")) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, "7f483a7")) != NULL);
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
cl_git_pass(git_diff_foreach(diff,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(1, expect.files);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]);
}
static void set_config_int(git_repository *repo, const char *name, int value)
{
git_config *cfg;
cl_git_pass(git_repository_config(&cfg, repo));
cl_git_pass(git_config_set_int32(cfg, name, value));
git_config_free(cfg);
}
void test_diff_tree__diff_configs(void)
{
const char *a_commit = "d70d245e";
const char *b_commit = "7a9e0b02";
g_repo = cl_git_sandbox_init("diff");
cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));
cl_git_pass(git_diff_foreach(diff,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(2, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(6, expect.hunks);
cl_assert_equal_i(55, expect.lines);
cl_assert_equal_i(33, expect.line_ctxt);
cl_assert_equal_i(7, expect.line_adds);
cl_assert_equal_i(15, expect.line_dels);
git_diff_free(diff);
diff = NULL;
set_config_int(g_repo, "diff.context", 1);
memset(&expect, 0, sizeof(expect));
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));
cl_git_pass(git_diff_foreach(diff,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(2, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(7, expect.hunks);
cl_assert_equal_i(34, expect.lines);
cl_assert_equal_i(12, expect.line_ctxt);
cl_assert_equal_i(7, expect.line_adds);
cl_assert_equal_i(15, expect.line_dels);
git_diff_free(diff);
diff = NULL;
set_config_int(g_repo, "diff.context", 0);
set_config_int(g_repo, "diff.noprefix", 1);
memset(&expect, 0, sizeof(expect));
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));
cl_git_pass(git_diff_foreach(diff,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(2, expect.files);
cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(7, expect.hunks);
cl_assert_equal_i(22, expect.lines);
cl_assert_equal_i(0, expect.line_ctxt);
cl_assert_equal_i(7, expect.line_adds);
cl_assert_equal_i(15, expect.line_dels);
}
void test_diff_tree__diff_tree_with_empty_dir_entry_succeeds(void)
{
const char *content = "This is a blob\n";
const git_diff_delta *delta;
git_oid empty_tree, invalid_tree, blob;
git_buf patch = GIT_BUF_INIT;
git_treebuilder *builder;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_blob_create_from_buffer(&blob, g_repo, content, strlen(content)));
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
cl_git_pass(git_treebuilder_write(&empty_tree, builder));
cl_git_pass(git_treebuilder_insert(NULL, builder, "empty_tree", &empty_tree, GIT_FILEMODE_TREE));
cl_git_pass(git_treebuilder_insert(NULL, builder, "blob", &blob, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_write(&invalid_tree, builder));
cl_git_pass(git_tree_lookup(&a, g_repo, &empty_tree));
cl_git_pass(git_tree_lookup(&b, g_repo, &invalid_tree));
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));
cl_git_pass(git_diff_foreach(diff,
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
cl_assert_equal_i(1, expect.files);
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, expect.hunks);
cl_assert_equal_i(1, expect.lines);
cl_assert_equal_i(0, expect.line_ctxt);
cl_assert_equal_i(1, expect.line_adds);
cl_assert_equal_i(0, expect.line_dels);
cl_git_pass(git_diff_to_buf(&patch, diff, GIT_DIFF_FORMAT_PATCH));
cl_assert_equal_s(patch.ptr,
"diff --git a/blob b/blob\n"
"new file mode 100644\n"
"index 0000000..bbf2e80\n"
"--- /dev/null\n"
"+++ b/blob\n"
"@@ -0,0 +1 @@\n"
"+This is a blob\n");
cl_assert_equal_i(git_diff_num_deltas(diff), 1);
delta = git_diff_get_delta(diff, 0);
cl_assert_equal_s(delta->new_file.path, "blob");
git_treebuilder_free(builder);
git_buf_dispose(&patch);
}
| libgit2-main | tests/libgit2/diff/tree.c |
#include "clar_libgit2.h"
#include "diff_helpers.h"
static git_repository *g_repo = NULL;
void test_diff_pathspec__initialize(void)
{
g_repo = cl_git_sandbox_init("status");
}
void test_diff_pathspec__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_diff_pathspec__0(void)
{
const char *a_commit = "26a125ee"; /* the current HEAD */
const char *b_commit = "0017bd4a"; /* the start */
git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_strarray paths = { NULL, 1 };
char *path;
git_pathspec *ps;
git_pathspec_match_list *matches;
cl_assert(a);
cl_assert(b);
path = "*_file";
paths.strings = &path;
cl_git_pass(git_pathspec_new(&ps, &paths));
cl_git_pass(git_pathspec_match_tree(&matches, a, GIT_PATHSPEC_DEFAULT, ps));
cl_assert_equal_i(7, (int)git_pathspec_match_list_entrycount(matches));
cl_assert_equal_s("current_file", git_pathspec_match_list_entry(matches,0));
cl_assert(git_pathspec_match_list_diff_entry(matches,0) == NULL);
git_pathspec_match_list_free(matches);
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, NULL, a, &opts));
cl_git_pass(git_pathspec_match_diff(
&matches, diff, GIT_PATHSPEC_DEFAULT, ps));
cl_assert_equal_i(7, (int)git_pathspec_match_list_entrycount(matches));
cl_assert(git_pathspec_match_list_diff_entry(matches, 0) != NULL);
cl_assert(git_pathspec_match_list_entry(matches, 0) == NULL);
cl_assert_equal_s("current_file",
git_pathspec_match_list_diff_entry(matches,0)->new_file.path);
cl_assert_equal_i(GIT_DELTA_ADDED,
(int)git_pathspec_match_list_diff_entry(matches,0)->status);
git_pathspec_match_list_free(matches);
git_diff_free(diff);
diff = NULL;
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
cl_git_pass(git_pathspec_match_diff(
&matches, diff, GIT_PATHSPEC_DEFAULT, ps));
cl_assert_equal_i(3, (int)git_pathspec_match_list_entrycount(matches));
cl_assert(git_pathspec_match_list_diff_entry(matches, 0) != NULL);
cl_assert(git_pathspec_match_list_entry(matches, 0) == NULL);
cl_assert_equal_s("subdir/current_file",
git_pathspec_match_list_diff_entry(matches,0)->new_file.path);
cl_assert_equal_i(GIT_DELTA_DELETED,
(int)git_pathspec_match_list_diff_entry(matches,0)->status);
git_pathspec_match_list_free(matches);
git_diff_free(diff);
diff = NULL;
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts));
cl_git_pass(git_pathspec_match_diff(
&matches, diff, GIT_PATHSPEC_DEFAULT, ps));
cl_assert_equal_i(4, (int)git_pathspec_match_list_entrycount(matches));
cl_assert(git_pathspec_match_list_diff_entry(matches, 0) != NULL);
cl_assert(git_pathspec_match_list_entry(matches, 0) == NULL);
cl_assert_equal_s("modified_file",
git_pathspec_match_list_diff_entry(matches,0)->new_file.path);
cl_assert_equal_i(GIT_DELTA_MODIFIED,
(int)git_pathspec_match_list_diff_entry(matches,0)->status);
git_pathspec_match_list_free(matches);
git_diff_free(diff);
diff = NULL;
git_tree_free(a);
git_tree_free(b);
git_pathspec_free(ps);
}
| libgit2-main | tests/libgit2/diff/pathspec.c |
#include "clar_libgit2.h"
#include "status_helpers.h"
int cb_status__normal(
const char *path, unsigned int status_flags, void *payload)
{
status_entry_counts *counts = payload;
if (counts->debug)
cb_status__print(path, status_flags, NULL);
if (counts->entry_count >= counts->expected_entry_count)
counts->wrong_status_flags_count++;
else if (strcmp(path, counts->expected_paths[counts->entry_count]))
counts->wrong_sorted_path++;
else if (status_flags != counts->expected_statuses[counts->entry_count])
counts->wrong_status_flags_count++;
counts->entry_count++;
return 0;
}
int cb_status__count(const char *p, unsigned int s, void *payload)
{
volatile int *count = (int *)payload;
GIT_UNUSED(p);
GIT_UNUSED(s);
(*count)++;
return 0;
}
int cb_status__single(const char *p, unsigned int s, void *payload)
{
status_entry_single *data = (status_entry_single *)payload;
if (data->debug)
fprintf(stderr, "%02d: %s (%04x)\n", data->count, p, s);
data->count++;
data->status = s;
return 0;
}
int cb_status__print(
const char *path, unsigned int status_flags, void *payload)
{
char istatus = ' ', wstatus = ' ';
int icount = 0, wcount = 0;
if (status_flags & GIT_STATUS_INDEX_NEW) {
istatus = 'A'; icount++;
}
if (status_flags & GIT_STATUS_INDEX_MODIFIED) {
istatus = 'M'; icount++;
}
if (status_flags & GIT_STATUS_INDEX_DELETED) {
istatus = 'D'; icount++;
}
if (status_flags & GIT_STATUS_INDEX_RENAMED) {
istatus = 'R'; icount++;
}
if (status_flags & GIT_STATUS_INDEX_TYPECHANGE) {
istatus = 'T'; icount++;
}
if (status_flags & GIT_STATUS_WT_NEW) {
wstatus = 'A'; wcount++;
}
if (status_flags & GIT_STATUS_WT_MODIFIED) {
wstatus = 'M'; wcount++;
}
if (status_flags & GIT_STATUS_WT_DELETED) {
wstatus = 'D'; wcount++;
}
if (status_flags & GIT_STATUS_WT_TYPECHANGE) {
wstatus = 'T'; wcount++;
}
if (status_flags & GIT_STATUS_IGNORED) {
wstatus = 'I'; wcount++;
}
if (status_flags & GIT_STATUS_WT_UNREADABLE) {
wstatus = 'X'; wcount++;
}
fprintf(stderr, "%c%c %s (%d/%d%s)\n",
istatus, wstatus, path, icount, wcount,
(icount > 1 || wcount > 1) ? " INVALID COMBO" : "");
if (payload)
*((int *)payload) += 1;
return 0;
}
| libgit2-main | tests/libgit2/status/status_helpers.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "status_helpers.h"
#include "../submodule/submodule_helpers.h"
static git_repository *g_repo = NULL;
void test_status_submodules__initialize(void)
{
}
void test_status_submodules__cleanup(void)
{
}
void test_status_submodules__api(void)
{
git_submodule *sm;
g_repo = setup_fixture_submodules();
cl_assert(git_submodule_lookup(NULL, g_repo, "nonexistent") == GIT_ENOTFOUND);
cl_assert(git_submodule_lookup(NULL, g_repo, "modified") == GIT_ENOTFOUND);
cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
cl_assert(sm != NULL);
cl_assert_equal_s("testrepo", git_submodule_name(sm));
cl_assert_equal_s("testrepo", git_submodule_path(sm));
git_submodule_free(sm);
}
void test_status_submodules__0(void)
{
int counts = 0;
g_repo = setup_fixture_submodules();
cl_assert(git_fs_path_isdir("submodules/.git"));
cl_assert(git_fs_path_isdir("submodules/testrepo/.git"));
cl_assert(git_fs_path_isfile("submodules/.gitmodules"));
cl_git_pass(
git_status_foreach(g_repo, cb_status__count, &counts)
);
cl_assert_equal_i(6, counts);
}
static const char *expected_files[] = {
".gitmodules",
"added",
"deleted",
"ignored",
"modified",
"untracked"
};
static unsigned int expected_status[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_IGNORED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW
};
static int cb_status__match(const char *p, unsigned int s, void *payload)
{
status_entry_counts *counts = payload;
int idx = counts->entry_count++;
clar__assert_equal(
counts->file, counts->func, counts->line,
"Status path mismatch", 1,
"%s", counts->expected_paths[idx], p);
clar__assert_equal(
counts->file, counts->func, counts->line,
"Status code mismatch", 1,
"%o", counts->expected_statuses[idx], s);
return 0;
}
void test_status_submodules__1(void)
{
status_entry_counts counts;
g_repo = setup_fixture_submodules();
cl_assert(git_fs_path_isdir("submodules/.git"));
cl_assert(git_fs_path_isdir("submodules/testrepo/.git"));
cl_assert(git_fs_path_isfile("submodules/.gitmodules"));
status_counts_init(counts, expected_files, expected_status);
cl_git_pass( git_status_foreach(g_repo, cb_status__match, &counts) );
cl_assert_equal_i(6, counts.entry_count);
}
void test_status_submodules__single_file(void)
{
unsigned int status = 0;
g_repo = setup_fixture_submodules();
cl_git_pass( git_status_file(&status, g_repo, "testrepo") );
cl_assert(!status);
}
void test_status_submodules__moved_head(void)
{
git_submodule *sm;
git_repository *smrepo;
git_oid oid;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts;
static const char *expected_files_with_sub[] = {
".gitmodules",
"added",
"deleted",
"ignored",
"modified",
"testrepo",
"untracked"
};
static unsigned int expected_status_with_sub[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_IGNORED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW
};
g_repo = setup_fixture_submodules();
cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
cl_git_pass(git_submodule_open(&smrepo, sm));
git_submodule_free(sm);
/* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
cl_git_pass(
git_oid__fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1));
cl_git_pass(git_repository_set_head_detached(smrepo, &oid));
/* first do a normal status, which should now include the submodule */
opts.flags = GIT_STATUS_OPT_DEFAULTS;
status_counts_init(
counts, expected_files_with_sub, expected_status_with_sub);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* try again with EXCLUDE_SUBMODULES which should skip it */
opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
status_counts_init(counts, expected_files, expected_status);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
git_repository_free(smrepo);
}
void test_status_submodules__dirty_workdir_only(void)
{
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts;
static const char *expected_files_with_sub[] = {
".gitmodules",
"added",
"deleted",
"ignored",
"modified",
"testrepo",
"untracked"
};
static unsigned int expected_status_with_sub[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_IGNORED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW
};
g_repo = setup_fixture_submodules();
cl_git_rewritefile("submodules/testrepo/README", "heyheyhey");
cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before");
/* first do a normal status, which should now include the submodule */
opts.flags = GIT_STATUS_OPT_DEFAULTS;
status_counts_init(
counts, expected_files_with_sub, expected_status_with_sub);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* try again with EXCLUDE_SUBMODULES which should skip it */
opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
status_counts_init(counts, expected_files, expected_status);
cl_git_pass(
git_status_foreach_ext(g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
}
void test_status_submodules__uninitialized(void)
{
git_repository *cloned_repo;
git_status_list *statuslist;
g_repo = cl_git_sandbox_init("submod2");
cl_git_pass(git_clone(&cloned_repo, "submod2", "submod2-clone", NULL));
cl_git_pass(git_status_list_new(&statuslist, cloned_repo, NULL));
cl_assert_equal_i(0, git_status_list_entrycount(statuslist));
git_status_list_free(statuslist);
git_repository_free(cloned_repo);
cl_git_sandbox_cleanup();
}
void test_status_submodules__contained_untracked_repo(void)
{
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts;
git_repository *contained;
static const char *expected_files_not_ignored[] = {
".gitmodules",
"added",
"deleted",
"modified",
"untracked"
};
static unsigned int expected_status_not_ignored[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW,
};
static const char *expected_files_with_untracked[] = {
".gitmodules",
"added",
"deleted",
"dir/file.md",
"modified",
"untracked"
};
static const char *expected_files_with_untracked_dir[] = {
".gitmodules",
"added",
"deleted",
"dir/",
"modified",
"untracked"
};
static unsigned int expected_status_with_untracked[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_WT_NEW,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW
};
g_repo = setup_fixture_submodules();
/* skip empty directory */
cl_must_pass(p_mkdir("submodules/dir", 0777));
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
status_counts_init(
counts, expected_files_not_ignored, expected_status_not_ignored);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(5, counts.entry_count);
/* still skipping because empty == ignored */
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
status_counts_init(
counts, expected_files_not_ignored, expected_status_not_ignored);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(5, counts.entry_count);
/* find non-ignored contents of directory */
cl_git_mkfile("submodules/dir/file.md", "hello");
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
status_counts_init(
counts, expected_files_with_untracked, expected_status_with_untracked);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
/* but skip if all content is ignored */
cl_git_append2file("submodules/.git/info/exclude", "\n*.md\n\n");
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
status_counts_init(
counts, expected_files_not_ignored, expected_status_not_ignored);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(5, counts.entry_count);
/* same is true if it contains a git link */
cl_git_mkfile("submodules/dir/.git", "gitlink: ../.git");
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
status_counts_init(
counts, expected_files_not_ignored, expected_status_not_ignored);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(5, counts.entry_count);
/* but if it contains tracked files, it should just show up as a
* directory and exclude the files in it
*/
cl_git_mkfile("submodules/dir/another_file", "hello");
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
status_counts_init(
counts, expected_files_with_untracked_dir,
expected_status_with_untracked);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
/* that applies to a git repo with a .git directory too */
cl_must_pass(p_unlink("submodules/dir/.git"));
cl_git_pass(git_repository_init(&contained, "submodules/dir", false));
git_repository_free(contained);
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
status_counts_init(
counts, expected_files_with_untracked_dir,
expected_status_with_untracked);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
/* same result even if we don't recurse into subdirectories */
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
status_counts_init(
counts, expected_files_with_untracked_dir,
expected_status_with_untracked);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(6, counts.entry_count);
/* and if we remove the untracked file, it goes back to ignored */
cl_must_pass(p_unlink("submodules/dir/another_file"));
status_counts_init(
counts, expected_files_not_ignored, expected_status_not_ignored);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(5, counts.entry_count);
}
void test_status_submodules__broken_stuff_that_git_allows(void)
{
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts;
git_repository *contained;
static const char *expected_files_with_broken[] = {
".gitmodules",
"added",
"broken/tracked",
"deleted",
"ignored",
"modified",
"untracked"
};
static unsigned int expected_status_with_broken[] = {
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_NEW,
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_IGNORED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW,
};
g_repo = setup_fixture_submodules();
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
GIT_STATUS_OPT_INCLUDE_IGNORED;
/* make a directory and stick a tracked item into the index */
{
git_index *idx;
cl_must_pass(p_mkdir("submodules/broken", 0777));
cl_git_mkfile("submodules/broken/tracked", "tracked content");
cl_git_pass(git_repository_index(&idx, g_repo));
cl_git_pass(git_index_add_bypath(idx, "broken/tracked"));
cl_git_pass(git_index_write(idx));
git_index_free(idx);
}
status_counts_init(
counts, expected_files_with_broken, expected_status_with_broken);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* directory with tracked items that looks a little bit like a repo */
cl_must_pass(p_mkdir("submodules/broken/.git", 0777));
cl_must_pass(p_mkdir("submodules/broken/.git/info", 0777));
cl_git_mkfile("submodules/broken/.git/info/exclude", "# bogus");
status_counts_init(
counts, expected_files_with_broken, expected_status_with_broken);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* directory with tracked items that is a repo */
cl_git_pass(git_futils_rmdir_r(
"submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_pass(git_repository_init(&contained, "submodules/broken", false));
git_repository_free(contained);
status_counts_init(
counts, expected_files_with_broken, expected_status_with_broken);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
/* directory with tracked items that claims to be a submodule but is not */
cl_git_pass(git_futils_rmdir_r(
"submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_append2file("submodules/.gitmodules",
"\n[submodule \"broken\"]\n"
"\tpath = broken\n"
"\turl = https://github.com/not/used\n\n");
status_counts_init(
counts, expected_files_with_broken, expected_status_with_broken);
cl_git_pass(git_status_foreach_ext(
g_repo, &opts, cb_status__match, &counts));
cl_assert_equal_i(7, counts.entry_count);
}
void test_status_submodules__entry_but_dir_tracked(void)
{
git_repository *repo;
git_status_list *status;
git_diff *diff;
git_index *index;
git_tree *tree;
cl_git_pass(git_repository_init(&repo, "mixed-submodule", 0));
cl_git_mkfile("mixed-submodule/.gitmodules", "[submodule \"sub\"]\n path = sub\n url = ../foo\n");
cl_git_pass(p_mkdir("mixed-submodule/sub", 0777));
cl_git_mkfile("mixed-submodule/sub/file", "");
/* Create the commit with sub/file as a file, and an entry for sub in the modules list */
{
git_oid tree_id, commit_id;
git_signature *sig;
git_reference *ref;
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, ".gitmodules"));
cl_git_pass(git_index_add_bypath(index, "sub/file"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_write_tree(&tree_id, index));
cl_git_pass(git_signature_now(&sig, "Sloppy Submoduler", "[email protected]"));
cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
cl_git_pass(git_commit_create(&commit_id, repo, NULL, sig, sig, NULL, "message", tree, 0, NULL));
cl_git_pass(git_reference_create(&ref, repo, "refs/heads/master", &commit_id, 1, "commit: foo"));
git_reference_free(ref);
git_signature_free(sig);
}
cl_git_pass(git_diff_tree_to_index(&diff, repo, tree, index, NULL));
cl_assert_equal_i(0, git_diff_num_deltas(diff));
git_diff_free(diff);
cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL));
cl_assert_equal_i(0, git_diff_num_deltas(diff));
git_diff_free(diff);
cl_git_pass(git_status_list_new(&status, repo, NULL));
cl_assert_equal_i(0, git_status_list_entrycount(status));
git_status_list_free(status);
git_index_free(index);
git_tree_free(tree);
git_repository_free(repo);
}
void test_status_submodules__mixed_case(void)
{
git_status_list *status;
git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
const git_status_entry *s;
size_t i;
status_opts.flags =
GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR |
GIT_STATUS_OPT_RENAMES_FROM_REWRITES |
GIT_STATUS_OPT_INCLUDE_UNREADABLE |
GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED;
g_repo = setup_fixture_submod3();
cl_git_pass(git_status_list_new(&status, g_repo, &status_opts));
for (i = 0; i < git_status_list_entrycount(status); i++) {
s = git_status_byindex(status, i);
if (s->head_to_index &&
strcmp(s->head_to_index->old_file.path, ".gitmodules") == 0)
continue;
cl_assert_equal_i(0, s->status);
}
git_status_list_free(status);
}
| libgit2-main | tests/libgit2/status/submodules.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "odb.h"
static void
cleanup__remove_file(void *_file)
{
cl_must_pass(p_unlink((char *)_file));
}
/* test retrieving OID from a file apart from the ODB */
void test_status_single__hash_single_file(void)
{
static const char file_name[] = "new_file";
static const char file_contents[] = "new_file\n";
static const char file_hash[] = "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a";
git_oid expected_id, actual_id;
/* initialization */
git_oid__fromstr(&expected_id, file_hash, GIT_OID_SHA1);
cl_git_mkfile(file_name, file_contents);
cl_set_cleanup(&cleanup__remove_file, (void *)file_name);
cl_git_pass(git_odb__hashfile(&actual_id, file_name, GIT_OBJECT_BLOB, GIT_OID_SHA1));
cl_assert_equal_oid(&expected_id, &actual_id);
}
/* test retrieving OID from an empty file apart from the ODB */
void test_status_single__hash_single_empty_file(void)
{
static const char file_name[] = "new_empty_file";
static const char file_contents[] = "";
static const char file_hash[] = "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";
git_oid expected_id, actual_id;
/* initialization */
git_oid__fromstr(&expected_id, file_hash, GIT_OID_SHA1);
cl_git_mkfile(file_name, file_contents);
cl_set_cleanup(&cleanup__remove_file, (void *)file_name);
cl_git_pass(git_odb__hashfile(&actual_id, file_name, GIT_OBJECT_BLOB, GIT_OID_SHA1));
cl_assert_equal_oid(&expected_id, &actual_id);
}
| libgit2-main | tests/libgit2/status/single.c |
#include "clar_libgit2.h"
#include "git2/sys/repository.h"
#include "futils.h"
#include "ignore.h"
#include "status_helpers.h"
#include "posix.h"
#include "util.h"
#include "path.h"
static void cleanup_new_repo(void *path)
{
cl_fixture_cleanup((char *)path);
}
void test_status_worktree_init__cannot_retrieve_the_status_of_a_bare_repository(void)
{
git_repository *repo;
unsigned int status = 0;
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_assert_equal_i(GIT_EBAREREPO, git_status_file(&status, repo, "dummy"));
git_repository_free(repo);
}
void test_status_worktree_init__first_commit_in_progress(void)
{
git_repository *repo;
git_index *index;
status_entry_single result;
cl_set_cleanup(&cleanup_new_repo, "getting_started");
cl_git_pass(git_repository_init(&repo, "getting_started", 0));
cl_git_mkfile("getting_started/testfile.txt", "content\n");
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(1, result.count);
cl_assert(result.status == GIT_STATUS_WT_NEW);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "testfile.txt"));
cl_git_pass(git_index_write(index));
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(1, result.count);
cl_assert(result.status == GIT_STATUS_INDEX_NEW);
git_index_free(index);
git_repository_free(repo);
}
void test_status_worktree_init__status_file_without_index_or_workdir(void)
{
git_repository *repo;
unsigned int status = 0;
git_index *index;
cl_git_pass(p_mkdir("wd", 0777));
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_set_workdir(repo, "wd", false));
cl_git_pass(git_index_open(&index, "empty-index"));
cl_assert_equal_i(0, (int)git_index_entrycount(index));
git_repository_set_index(repo, index);
cl_git_pass(git_status_file(&status, repo, "branch_file.txt"));
cl_assert_equal_i(GIT_STATUS_INDEX_DELETED, status);
git_repository_free(repo);
git_index_free(index);
cl_git_pass(p_rmdir("wd"));
}
static void fill_index_wth_head_entries(git_repository *repo, git_index *index)
{
git_oid oid;
git_commit *commit;
git_tree *tree;
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
cl_git_pass(git_commit_tree(&tree, commit));
cl_git_pass(git_index_read_tree(index, tree));
cl_git_pass(git_index_write(index));
git_tree_free(tree);
git_commit_free(commit);
}
void test_status_worktree_init__status_file_with_clean_index_and_empty_workdir(void)
{
git_repository *repo;
unsigned int status = 0;
git_index *index;
cl_git_pass(p_mkdir("wd", 0777));
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_set_workdir(repo, "wd", false));
cl_git_pass(git_index_open(&index, "my-index"));
fill_index_wth_head_entries(repo, index);
git_repository_set_index(repo, index);
cl_git_pass(git_status_file(&status, repo, "branch_file.txt"));
cl_assert_equal_i(GIT_STATUS_WT_DELETED, status);
git_repository_free(repo);
git_index_free(index);
cl_git_pass(p_rmdir("wd"));
cl_git_pass(p_unlink("my-index"));
}
void test_status_worktree_init__bracket_in_filename(void)
{
git_repository *repo;
git_index *index;
status_entry_single result;
unsigned int status_flags;
#define FILE_WITH_BRACKET "LICENSE[1].md"
#define FILE_WITHOUT_BRACKET "LICENSE1.md"
cl_set_cleanup(&cleanup_new_repo, "with_bracket");
cl_git_pass(git_repository_init(&repo, "with_bracket", 0));
cl_git_mkfile("with_bracket/" FILE_WITH_BRACKET, "I have a bracket in my name\n");
/* file is new to working directory */
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(1, result.count);
cl_assert(result.status == GIT_STATUS_WT_NEW);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_BRACKET));
cl_assert(status_flags == GIT_STATUS_WT_NEW);
/* ignore the file */
cl_git_rewritefile("with_bracket/.gitignore", "*.md\n.gitignore\n");
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(2, result.count);
cl_assert(result.status == GIT_STATUS_IGNORED);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_BRACKET));
cl_assert(status_flags == GIT_STATUS_IGNORED);
/* don't ignore the file */
cl_git_rewritefile("with_bracket/.gitignore", ".gitignore\n");
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(2, result.count);
cl_assert(result.status == GIT_STATUS_WT_NEW);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_BRACKET));
cl_assert(status_flags == GIT_STATUS_WT_NEW);
/* add the file to the index */
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, FILE_WITH_BRACKET));
cl_git_pass(git_index_write(index));
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(2, result.count);
cl_assert(result.status == GIT_STATUS_INDEX_NEW);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_BRACKET));
cl_assert(status_flags == GIT_STATUS_INDEX_NEW);
/* Create file without bracket */
cl_git_mkfile("with_bracket/" FILE_WITHOUT_BRACKET, "I have no bracket in my name!\n");
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITHOUT_BRACKET));
cl_assert(status_flags == GIT_STATUS_WT_NEW);
cl_git_fail_with(git_status_file(&status_flags, repo, "LICENSE\\[1\\].md"), GIT_ENOTFOUND);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_BRACKET));
cl_assert(status_flags == GIT_STATUS_INDEX_NEW);
git_index_free(index);
git_repository_free(repo);
}
void test_status_worktree_init__space_in_filename(void)
{
git_repository *repo;
git_index *index;
status_entry_single result;
unsigned int status_flags;
#define FILE_WITH_SPACE "LICENSE - copy.md"
cl_set_cleanup(&cleanup_new_repo, "with_space");
cl_git_pass(git_repository_init(&repo, "with_space", 0));
cl_git_mkfile("with_space/" FILE_WITH_SPACE, "I have a space in my name\n");
/* file is new to working directory */
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(1, result.count);
cl_assert(result.status == GIT_STATUS_WT_NEW);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_SPACE));
cl_assert(status_flags == GIT_STATUS_WT_NEW);
/* ignore the file */
cl_git_rewritefile("with_space/.gitignore", "*.md\n.gitignore\n");
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(2, result.count);
cl_assert(result.status == GIT_STATUS_IGNORED);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_SPACE));
cl_assert(status_flags == GIT_STATUS_IGNORED);
/* don't ignore the file */
cl_git_rewritefile("with_space/.gitignore", ".gitignore\n");
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(2, result.count);
cl_assert(result.status == GIT_STATUS_WT_NEW);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_SPACE));
cl_assert(status_flags == GIT_STATUS_WT_NEW);
/* add the file to the index */
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, FILE_WITH_SPACE));
cl_git_pass(git_index_write(index));
memset(&result, 0, sizeof(result));
cl_git_pass(git_status_foreach(repo, cb_status__single, &result));
cl_assert_equal_i(2, result.count);
cl_assert(result.status == GIT_STATUS_INDEX_NEW);
cl_git_pass(git_status_file(&status_flags, repo, FILE_WITH_SPACE));
cl_assert(status_flags == GIT_STATUS_INDEX_NEW);
git_index_free(index);
git_repository_free(repo);
}
static int cb_status__expected_path(const char *p, unsigned int s, void *payload)
{
const char *expected_path = (const char *)payload;
GIT_UNUSED(s);
if (payload == NULL)
cl_fail("Unexpected path");
cl_assert_equal_s(expected_path, p);
return 0;
}
void test_status_worktree_init__disable_pathspec_match(void)
{
git_repository *repo;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
char *file_with_bracket = "LICENSE[1].md",
*imaginary_file_with_bracket = "LICENSE[1-2].md";
cl_set_cleanup(&cleanup_new_repo, "pathspec");
cl_git_pass(git_repository_init(&repo, "pathspec", 0));
cl_git_mkfile("pathspec/LICENSE[1].md", "screaming bracket\n");
cl_git_mkfile("pathspec/LICENSE1.md", "no bracket\n");
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
opts.pathspec.count = 1;
opts.pathspec.strings = &file_with_bracket;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__expected_path,
file_with_bracket)
);
/* Test passing a pathspec matching files in the workdir. */
/* Must not match because pathspecs are disabled. */
opts.pathspec.strings = &imaginary_file_with_bracket;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__expected_path, NULL)
);
git_repository_free(repo);
}
void test_status_worktree_init__new_staged_file_must_handle_crlf(void)
{
git_repository *repo;
git_index *index;
unsigned int status;
cl_set_cleanup(&cleanup_new_repo, "getting_started");
cl_git_pass(git_repository_init(&repo, "getting_started", 0));
/* Ensure that repo has core.autocrlf=true */
cl_repo_set_bool(repo, "core.autocrlf", true);
cl_git_mkfile("getting_started/testfile.txt", "content\r\n"); /* Content with CRLF */
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "testfile.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_file(&status, repo, "testfile.txt"));
cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
git_index_free(index);
git_repository_free(repo);
}
| libgit2-main | tests/libgit2/status/worktree_init.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "ignore.h"
#include "status_data.h"
#include "posix.h"
#include "util.h"
#include "path.h"
#include "../diff/diff_helpers.h"
#include "../checkout/checkout_helpers.h"
#include "git2/sys/diff.h"
/**
* Cleanup
*
* This will be called once after each test finishes, even
* if the test failed
*/
void test_status_worktree__cleanup(void)
{
cl_git_sandbox_cleanup();
}
/**
* Tests - Status determination on a working tree
*/
/* this test is equivalent to t18-status.c:statuscb0 */
void test_status_worktree__whole_repository(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_count0;
counts.expected_paths = entry_paths0;
counts.expected_statuses = entry_statuses0;
cl_git_pass(
git_status_foreach(repo, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
static void assert_show(
const int entry_counts,
const char *entry_paths[],
const unsigned int entry_statuses[],
git_repository *repo,
git_status_show_t show,
unsigned int extra_flags)
{
status_entry_counts counts;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_counts;
counts.expected_paths = entry_paths;
counts.expected_statuses = entry_statuses;
opts.flags = GIT_STATUS_OPT_DEFAULTS | extra_flags;
opts.show = show;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
void test_status_worktree__show_index_and_workdir(void)
{
assert_show(entry_count0, entry_paths0, entry_statuses0,
cl_git_sandbox_init("status"), GIT_STATUS_SHOW_INDEX_AND_WORKDIR, 0);
}
void test_status_worktree__show_index_only(void)
{
assert_show(entry_count5, entry_paths5, entry_statuses5,
cl_git_sandbox_init("status"), GIT_STATUS_SHOW_INDEX_ONLY, 0);
}
void test_status_worktree__show_workdir_only(void)
{
assert_show(entry_count6, entry_paths6, entry_statuses6,
cl_git_sandbox_init("status"), GIT_STATUS_SHOW_WORKDIR_ONLY, 0);
}
/* this test is equivalent to t18-status.c:statuscb1 */
void test_status_worktree__empty_repository(void)
{
int count = 0;
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_status_foreach(repo, cb_status__count, &count));
cl_assert_equal_i(0, count);
}
static int remove_file_cb(void *data, git_str *file)
{
const char *filename = git_str_cstr(file);
GIT_UNUSED(data);
if (git__suffixcmp(filename, ".git") == 0)
return 0;
if (git_fs_path_isdir(filename))
cl_git_pass(git_futils_rmdir_r(filename, NULL, GIT_RMDIR_REMOVE_FILES));
else
cl_git_pass(p_unlink(git_str_cstr(file)));
return 0;
}
/* this test is equivalent to t18-status.c:statuscb2 */
void test_status_worktree__purged_worktree(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
git_str workdir = GIT_STR_INIT;
/* first purge the contents of the worktree */
cl_git_pass(git_str_sets(&workdir, git_repository_workdir(repo)));
cl_git_pass(git_fs_path_direach(&workdir, 0, remove_file_cb, NULL));
git_str_dispose(&workdir);
/* now get status */
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_count2;
counts.expected_paths = entry_paths2;
counts.expected_statuses = entry_statuses2;
cl_git_pass(
git_status_foreach(repo, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
/* this test is similar to t18-status.c:statuscb3 */
void test_status_worktree__swap_subdir_and_file(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
git_index *index;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
bool ignore_case;
cl_git_pass(git_repository_index(&index, repo));
ignore_case = (git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0;
git_index_free(index);
/* first alter the contents of the worktree */
cl_git_pass(p_rename("status/current_file", "status/swap"));
cl_git_pass(p_rename("status/subdir", "status/current_file"));
cl_git_pass(p_rename("status/swap", "status/subdir"));
cl_git_mkfile("status/.HEADER", "dummy");
cl_git_mkfile("status/42-is-not-prime.sigh", "dummy");
cl_git_mkfile("status/README.md", "dummy");
/* now get status */
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_count3;
counts.expected_paths = ignore_case ? entry_paths3_icase : entry_paths3;
counts.expected_statuses = ignore_case ? entry_statuses3_icase : entry_statuses3;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_INCLUDE_IGNORED;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
void test_status_worktree__swap_subdir_with_recurse_and_pathspec(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
/* first alter the contents of the worktree */
cl_git_pass(p_rename("status/current_file", "status/swap"));
cl_git_pass(p_rename("status/subdir", "status/current_file"));
cl_git_pass(p_rename("status/swap", "status/subdir"));
cl_git_mkfile("status/.new_file", "dummy");
cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", 0777));
cl_git_mkfile("status/zzz_new_dir/new_file", "dummy");
cl_git_mkfile("status/zzz_new_file", "dummy");
/* now get status */
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_count4;
counts.expected_paths = entry_paths4;
counts.expected_statuses = entry_statuses4;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
/* TODO: set pathspec to "current_file" eventually */
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
static void stage_and_commit(git_repository *repo, const char *path)
{
git_index *index;
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, path));
cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n");
git_index_free(index);
}
void test_status_worktree__within_subdir(void)
{
status_entry_counts counts;
git_repository *repo = cl_git_sandbox_init("status");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
char *paths[] = { "zzz_new_dir" };
git_strarray pathsArray;
/* first alter the contents of the worktree */
cl_git_mkfile("status/.new_file", "dummy");
cl_git_pass(git_futils_mkdir_r("status/zzz_new_dir", 0777));
cl_git_mkfile("status/zzz_new_dir/new_file", "dummy");
cl_git_mkfile("status/zzz_new_file", "dummy");
cl_git_mkfile("status/wut", "dummy");
stage_and_commit(repo, "zzz_new_dir/new_file");
/* now get status */
memset(&counts, 0x0, sizeof(status_entry_counts));
counts.expected_entry_count = entry_count4;
counts.expected_paths = entry_paths4;
counts.expected_statuses = entry_statuses4;
counts.debug = true;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
pathsArray.count = 1;
pathsArray.strings = paths;
opts.pathspec = pathsArray;
/* We committed zzz_new_dir/new_file above. It shouldn't be reported. */
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(0, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
/* this test is equivalent to t18-status.c:singlestatus0 */
void test_status_worktree__single_file(void)
{
int i;
unsigned int status_flags;
git_repository *repo = cl_git_sandbox_init("status");
for (i = 0; i < (int)entry_count0; i++) {
cl_git_pass(
git_status_file(&status_flags, repo, entry_paths0[i])
);
cl_assert(entry_statuses0[i] == status_flags);
}
}
/* this test is equivalent to t18-status.c:singlestatus1 */
void test_status_worktree__single_nonexistent_file(void)
{
int error;
unsigned int status_flags;
git_repository *repo = cl_git_sandbox_init("status");
error = git_status_file(&status_flags, repo, "nonexistent");
cl_git_fail(error);
cl_assert(error == GIT_ENOTFOUND);
}
/* this test is equivalent to t18-status.c:singlestatus2 */
void test_status_worktree__single_nonexistent_file_empty_repo(void)
{
int error;
unsigned int status_flags;
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
error = git_status_file(&status_flags, repo, "nonexistent");
cl_git_fail(error);
cl_assert(error == GIT_ENOTFOUND);
}
/* this test is equivalent to t18-status.c:singlestatus3 */
void test_status_worktree__single_file_empty_repo(void)
{
unsigned int status_flags;
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_mkfile("empty_standard_repo/new_file", "new_file\n");
cl_git_pass(git_status_file(&status_flags, repo, "new_file"));
cl_assert(status_flags == GIT_STATUS_WT_NEW);
}
/* this test is equivalent to t18-status.c:singlestatus4 */
void test_status_worktree__single_folder(void)
{
int error;
unsigned int status_flags;
git_repository *repo = cl_git_sandbox_init("status");
error = git_status_file(&status_flags, repo, "subdir");
cl_git_fail(error);
cl_assert(error != GIT_ENOTFOUND);
}
void test_status_worktree__ignores(void)
{
int i, ignored;
git_repository *repo = cl_git_sandbox_init("status");
for (i = 0; i < (int)entry_count0; i++) {
cl_git_pass(
git_status_should_ignore(&ignored, repo, entry_paths0[i])
);
cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_IGNORED));
}
cl_git_pass(
git_status_should_ignore(&ignored, repo, "nonexistent_file")
);
cl_assert(!ignored);
cl_git_pass(
git_status_should_ignore(&ignored, repo, "ignored_nonexistent_file")
);
cl_assert(ignored);
}
static int cb_status__check_592(const char *p, unsigned int s, void *payload)
{
if (s != GIT_STATUS_WT_DELETED ||
(payload != NULL && strcmp(p, (const char *)payload) != 0))
return -1;
return 0;
}
void test_status_worktree__issue_592(void)
{
git_repository *repo;
git_str path = GIT_STR_INIT;
repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(repo), "l.txt"));
cl_git_pass(p_unlink(git_str_cstr(&path)));
cl_assert(!git_fs_path_exists("issue_592/l.txt"));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "l.txt"));
git_str_dispose(&path);
}
void test_status_worktree__issue_592_2(void)
{
git_repository *repo;
git_str path = GIT_STR_INIT;
repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(repo), "c/a.txt"));
cl_git_pass(p_unlink(git_str_cstr(&path)));
cl_assert(!git_fs_path_exists("issue_592/c/a.txt"));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt"));
git_str_dispose(&path);
}
void test_status_worktree__issue_592_3(void)
{
git_repository *repo;
git_str path = GIT_STR_INIT;
repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(repo), "c"));
cl_git_pass(git_futils_rmdir_r(git_str_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES));
cl_assert(!git_fs_path_exists("issue_592/c/a.txt"));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "c/a.txt"));
git_str_dispose(&path);
}
void test_status_worktree__issue_592_4(void)
{
git_repository *repo;
git_str path = GIT_STR_INIT;
repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(repo), "t/b.txt"));
cl_git_pass(p_unlink(git_str_cstr(&path)));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, "t/b.txt"));
git_str_dispose(&path);
}
void test_status_worktree__issue_592_5(void)
{
git_repository *repo;
git_str path = GIT_STR_INIT;
repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(repo), "t"));
cl_git_pass(git_futils_rmdir_r(git_str_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_pass(p_mkdir(git_str_cstr(&path), 0777));
cl_git_pass(git_status_foreach(repo, cb_status__check_592, NULL));
git_str_dispose(&path);
}
void test_status_worktree__issue_592_ignores_0(void)
{
int count = 0;
status_entry_single st;
git_repository *repo = cl_git_sandbox_init("issue_592");
cl_git_pass(git_status_foreach(repo, cb_status__count, &count));
cl_assert_equal_i(0, count);
cl_git_rewritefile("issue_592/.gitignore",
".gitignore\n*.txt\nc/\n[tT]*/\n");
cl_git_pass(git_status_foreach(repo, cb_status__count, &count));
cl_assert_equal_i(1, count);
/* This is a situation where the behavior of libgit2 is
* different from core git. Core git will show ignored.txt
* in the list of ignored files, even though the directory
* "t" is ignored and the file is untracked because we have
* the explicit "*.txt" ignore rule. Libgit2 just excludes
* all untracked files that are contained within ignored
* directories without explicitly listing them.
*/
cl_git_rewritefile("issue_592/t/ignored.txt", "ping");
memset(&st, 0, sizeof(st));
cl_git_pass(git_status_foreach(repo, cb_status__single, &st));
cl_assert_equal_i(1, st.count);
cl_assert(st.status == GIT_STATUS_IGNORED);
cl_git_rewritefile("issue_592/c/ignored_by_dir", "ping");
memset(&st, 0, sizeof(st));
cl_git_pass(git_status_foreach(repo, cb_status__single, &st));
cl_assert_equal_i(1, st.count);
cl_assert(st.status == GIT_STATUS_IGNORED);
cl_git_rewritefile("issue_592/t/ignored_by_dir_pattern", "ping");
memset(&st, 0, sizeof(st));
cl_git_pass(git_status_foreach(repo, cb_status__single, &st));
cl_assert_equal_i(1, st.count);
cl_assert(st.status == GIT_STATUS_IGNORED);
}
void test_status_worktree__issue_592_ignored_dirs_with_tracked_content(void)
{
int count = 0;
git_repository *repo = cl_git_sandbox_init("issue_592b");
cl_git_pass(git_status_foreach(repo, cb_status__count, &count));
cl_assert_equal_i(1, count);
/* if we are really mimicking core git, then only ignored1.txt
* at the top level will show up in the ignores list here.
* everything else will be unmodified or skipped completely.
*/
}
void test_status_worktree__conflict_with_diff3(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_index *index;
unsigned int status;
git_index_entry ancestor_entry, our_entry, their_entry;
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "modified_file";
ancestor_entry.mode = 0100644;
git_oid__fromstr(&ancestor_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
GIT_OID_SHA1);
our_entry.path = "modified_file";
our_entry.mode = 0100644;
git_oid__fromstr(&our_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
GIT_OID_SHA1);
their_entry.path = "modified_file";
their_entry.mode = 0100644;
git_oid__fromstr(&their_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
GIT_OID_SHA1);
cl_git_pass(git_status_file(&status, repo, "modified_file"));
cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_remove(index, "modified_file", 0));
cl_git_pass(git_index_conflict_add(
index, &ancestor_entry, &our_entry, &their_entry));
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_status_file(&status, repo, "modified_file"));
cl_assert_equal_i(GIT_STATUS_CONFLICTED, status);
}
static const char *filemode_paths[] = {
"exec_off",
"exec_off2on_staged",
"exec_off2on_workdir",
"exec_off_untracked",
"exec_on",
"exec_on2off_staged",
"exec_on2off_workdir",
"exec_on_untracked",
};
static unsigned int filemode_statuses[] = {
GIT_STATUS_CURRENT,
GIT_STATUS_INDEX_MODIFIED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW,
GIT_STATUS_CURRENT,
GIT_STATUS_INDEX_MODIFIED,
GIT_STATUS_WT_MODIFIED,
GIT_STATUS_WT_NEW
};
static const int filemode_count = 8;
void test_status_worktree__filemode_changes(void)
{
git_repository *repo = cl_git_sandbox_init("filemodes");
status_entry_counts counts;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
/* overwrite stored filemode with platform appropriate value */
if (cl_is_chmod_supported())
cl_repo_set_bool(repo, "core.filemode", true);
else {
int i;
cl_repo_set_bool(repo, "core.filemode", false);
/* won't trust filesystem mode diffs, so these will appear unchanged */
for (i = 0; i < filemode_count; ++i)
if (filemode_statuses[i] == GIT_STATUS_WT_MODIFIED)
filemode_statuses[i] = GIT_STATUS_CURRENT;
}
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED;
memset(&counts, 0, sizeof(counts));
counts.expected_entry_count = filemode_count;
counts.expected_paths = filemode_paths;
counts.expected_statuses = filemode_statuses;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
void test_status_worktree__filemode_non755(void)
{
git_repository *repo = cl_git_sandbox_init("filemodes");
status_entry_counts counts;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_str executable_path = GIT_STR_INIT;
git_str nonexecutable_path = GIT_STR_INIT;
if (!cl_is_chmod_supported())
return;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED;
git_str_joinpath(&executable_path, git_repository_workdir(repo), "exec_on");
cl_must_pass(p_chmod(git_str_cstr(&executable_path), 0744));
git_str_dispose(&executable_path);
git_str_joinpath(&nonexecutable_path, git_repository_workdir(repo), "exec_off");
cl_must_pass(p_chmod(git_str_cstr(&nonexecutable_path), 0655));
git_str_dispose(&nonexecutable_path);
memset(&counts, 0, sizeof(counts));
counts.expected_entry_count = filemode_count;
counts.expected_paths = filemode_paths;
counts.expected_statuses = filemode_statuses;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts)
);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
static int cb_status__interrupt(const char *p, unsigned int s, void *payload)
{
volatile int *count = (int *)payload;
GIT_UNUSED(p);
GIT_UNUSED(s);
(*count)++;
return (*count == 8) ? -111 : 0;
}
void test_status_worktree__interruptable_foreach(void)
{
int count = 0;
git_repository *repo = cl_git_sandbox_init("status");
cl_assert_equal_i(
-111, git_status_foreach(repo, cb_status__interrupt, &count)
);
cl_assert_equal_i(8, count);
}
void test_status_worktree__line_endings_dont_count_as_changes_with_autocrlf(void)
{
git_repository *repo = cl_git_sandbox_init("status");
unsigned int status;
cl_repo_set_bool(repo, "core.autocrlf", true);
cl_git_rewritefile("status/current_file", "current_file\r\n");
cl_git_pass(git_status_file(&status, repo, "current_file"));
/* stat data on file should no longer match stat cache, even though
* file diff will be empty because of line-ending conversion - matches
* the Git command-line behavior here.
*/
cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status);
}
void test_status_worktree__line_endings_dont_count_as_changes_with_autocrlf_issue_1397(void)
{
git_repository *repo = cl_git_sandbox_init("issue_1397");
unsigned int status;
cl_repo_set_bool(repo, "core.autocrlf", true);
cl_git_pass(git_status_file(&status, repo, "crlf_file.txt"));
cl_assert_equal_i(GIT_STATUS_CURRENT, status);
}
void test_status_worktree__conflicted_item(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_index *index;
unsigned int status;
git_index_entry ancestor_entry, our_entry, their_entry;
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.mode = 0100644;
ancestor_entry.path = "modified_file";
git_oid__fromstr(&ancestor_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
GIT_OID_SHA1);
our_entry.mode = 0100644;
our_entry.path = "modified_file";
git_oid__fromstr(&our_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
GIT_OID_SHA1);
their_entry.mode = 0100644;
their_entry.path = "modified_file";
git_oid__fromstr(&their_entry.id,
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
GIT_OID_SHA1);
cl_git_pass(git_status_file(&status, repo, "modified_file"));
cl_assert_equal_i(GIT_STATUS_WT_MODIFIED, status);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_conflict_add(index, &ancestor_entry,
&our_entry, &their_entry));
cl_git_pass(git_status_file(&status, repo, "modified_file"));
cl_assert_equal_i(GIT_STATUS_CONFLICTED, status);
git_index_free(index);
}
void test_status_worktree__conflict_has_no_oid(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_index *index;
git_index_entry entry = {{0}};
git_status_list *statuslist;
const git_status_entry *status;
git_oid zero_id = GIT_OID_SHA1_ZERO;
entry.mode = 0100644;
entry.path = "modified_file";
git_oid__fromstr(&entry.id, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a", GIT_OID_SHA1);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_conflict_add(index, &entry, &entry, &entry));
git_status_list_new(&statuslist, repo, NULL);
cl_assert_equal_i(16, git_status_list_entrycount(statuslist));
status = git_status_byindex(statuslist, 2);
cl_assert_equal_i(GIT_STATUS_CONFLICTED, status->status);
cl_assert_equal_s("modified_file", status->head_to_index->old_file.path);
cl_assert(!git_oid_equal(&zero_id, &status->head_to_index->old_file.id));
cl_assert(0 != status->head_to_index->old_file.mode);
cl_assert_equal_s("modified_file", status->head_to_index->new_file.path);
cl_assert_equal_oid(&zero_id, &status->head_to_index->new_file.id);
cl_assert_equal_i(0, status->head_to_index->new_file.mode);
cl_assert_equal_i(0, status->head_to_index->new_file.size);
cl_assert_equal_s("modified_file", status->index_to_workdir->old_file.path);
cl_assert_equal_oid(&zero_id, &status->index_to_workdir->old_file.id);
cl_assert_equal_i(0, status->index_to_workdir->old_file.mode);
cl_assert_equal_i(0, status->index_to_workdir->old_file.size);
cl_assert_equal_s("modified_file", status->index_to_workdir->new_file.path);
cl_assert(
!git_oid_equal(&zero_id, &status->index_to_workdir->new_file.id) ||
!(status->index_to_workdir->new_file.flags & GIT_DIFF_FLAG_VALID_ID));
cl_assert(0 != status->index_to_workdir->new_file.mode);
cl_assert(0 != status->index_to_workdir->new_file.size);
git_index_free(index);
git_status_list_free(statuslist);
}
static void assert_ignore_case(
bool should_ignore_case,
int expected_lower_cased_file_status,
int expected_camel_cased_file_status)
{
unsigned int status;
git_str lower_case_path = GIT_STR_INIT, camel_case_path = GIT_STR_INIT;
git_repository *repo, *repo2;
repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_remove_placeholders(git_repository_path(repo), "dummy-marker.txt");
cl_repo_set_bool(repo, "core.ignorecase", should_ignore_case);
cl_git_pass(git_str_joinpath(&lower_case_path,
git_repository_workdir(repo), "plop"));
cl_git_mkfile(git_str_cstr(&lower_case_path), "");
stage_and_commit(repo, "plop");
cl_git_pass(git_repository_open(&repo2, "./empty_standard_repo"));
cl_git_pass(git_status_file(&status, repo2, "plop"));
cl_assert_equal_i(GIT_STATUS_CURRENT, status);
cl_git_pass(git_str_joinpath(&camel_case_path,
git_repository_workdir(repo), "Plop"));
cl_git_pass(p_rename(git_str_cstr(&lower_case_path), git_str_cstr(&camel_case_path)));
cl_git_pass(git_status_file(&status, repo2, "plop"));
cl_assert_equal_i(expected_lower_cased_file_status, status);
cl_git_pass(git_status_file(&status, repo2, "Plop"));
cl_assert_equal_i(expected_camel_cased_file_status, status);
git_repository_free(repo2);
git_str_dispose(&lower_case_path);
git_str_dispose(&camel_case_path);
}
void test_status_worktree__file_status_honors_core_ignorecase_true(void)
{
assert_ignore_case(true, GIT_STATUS_CURRENT, GIT_STATUS_CURRENT);
}
void test_status_worktree__file_status_honors_core_ignorecase_false(void)
{
assert_ignore_case(false, GIT_STATUS_WT_DELETED, GIT_STATUS_WT_NEW);
}
void test_status_worktree__file_status_honors_case_ignorecase_regarding_untracked_files(void)
{
git_repository *repo = cl_git_sandbox_init("status");
unsigned int status;
git_index *index;
cl_repo_set_bool(repo, "core.ignorecase", false);
repo = cl_git_sandbox_reopen();
/* Actually returns GIT_STATUS_IGNORED on Windows */
cl_git_fail_with(git_status_file(&status, repo, "NEW_FILE"), GIT_ENOTFOUND);
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "new_file"));
cl_git_pass(git_index_write(index));
git_index_free(index);
/* Actually returns GIT_STATUS_IGNORED on Windows */
cl_git_fail_with(git_status_file(&status, repo, "NEW_FILE"), GIT_ENOTFOUND);
}
void test_status_worktree__simple_delete(void)
{
git_repository *repo = cl_git_sandbox_init("renames");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
int count;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH |
GIT_STATUS_OPT_EXCLUDE_SUBMODULES |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
count = 0;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__count, &count) );
cl_assert_equal_i(0, count);
cl_must_pass(p_unlink("renames/untimely.txt"));
count = 0;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__count, &count) );
cl_assert_equal_i(1, count);
}
void test_status_worktree__simple_delete_indexed(void)
{
git_repository *repo = cl_git_sandbox_init("renames");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *status;
opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH |
GIT_STATUS_OPT_EXCLUDE_SUBMODULES |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
cl_git_pass(git_status_list_new(&status, repo, &opts));
cl_assert_equal_sz(0, git_status_list_entrycount(status));
git_status_list_free(status);
cl_must_pass(p_unlink("renames/untimely.txt"));
cl_git_pass(git_status_list_new(&status, repo, &opts));
cl_assert_equal_sz(1, git_status_list_entrycount(status));
cl_assert_equal_i(
GIT_STATUS_WT_DELETED, git_status_byindex(status, 0)->status);
git_status_list_free(status);
}
static const char *icase_paths[] = { "B", "c", "g", "H" };
static unsigned int icase_statuses[] = {
GIT_STATUS_WT_MODIFIED, GIT_STATUS_WT_DELETED,
GIT_STATUS_WT_MODIFIED, GIT_STATUS_WT_DELETED,
};
static const char *case_paths[] = { "B", "H", "c", "g" };
static unsigned int case_statuses[] = {
GIT_STATUS_WT_MODIFIED, GIT_STATUS_WT_DELETED,
GIT_STATUS_WT_DELETED, GIT_STATUS_WT_MODIFIED,
};
void test_status_worktree__sorting_by_case(void)
{
git_repository *repo = cl_git_sandbox_init("icase");
git_index *index;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
bool native_ignore_case;
status_entry_counts counts;
cl_git_pass(git_repository_index(&index, repo));
native_ignore_case =
(git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0;
git_index_free(index);
memset(&counts, 0, sizeof(counts));
counts.expected_entry_count = 0;
counts.expected_paths = NULL;
counts.expected_statuses = NULL;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts));
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
cl_git_rewritefile("icase/B", "new stuff");
cl_must_pass(p_unlink("icase/c"));
cl_git_rewritefile("icase/g", "new stuff");
cl_must_pass(p_unlink("icase/H"));
memset(&counts, 0, sizeof(counts));
counts.expected_entry_count = 4;
if (native_ignore_case) {
counts.expected_paths = icase_paths;
counts.expected_statuses = icase_statuses;
} else {
counts.expected_paths = case_paths;
counts.expected_statuses = case_statuses;
}
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts));
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
opts.flags = GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
memset(&counts, 0, sizeof(counts));
counts.expected_entry_count = 4;
counts.expected_paths = case_paths;
counts.expected_statuses = case_statuses;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts));
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
opts.flags = GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY;
memset(&counts, 0, sizeof(counts));
counts.expected_entry_count = 4;
counts.expected_paths = icase_paths;
counts.expected_statuses = icase_statuses;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts));
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
void test_status_worktree__long_filenames(void)
{
char path[260*4+1] = {0};
const char *expected_paths[] = {path};
const unsigned int expected_statuses[] = {GIT_STATUS_WT_NEW};
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts = {0};
/* Create directory with amazingly long filename */
sprintf(path, "empty_standard_repo/%s", longname);
cl_git_pass(git_futils_mkdir_r(path, 0777));
sprintf(path, "empty_standard_repo/%s/foo", longname);
cl_git_mkfile(path, "dummy");
sprintf(path, "%s/foo", longname);
counts.expected_entry_count = 1;
counts.expected_paths = expected_paths;
counts.expected_statuses = expected_statuses;
opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
opts.flags = GIT_STATUS_OPT_DEFAULTS;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) );
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
/* The update stat cache tests mostly just mirror other tests and try
* to make sure that updating the stat cache doesn't change the results
* while reducing the amount of work that needs to be done
*/
static void check_status0(git_status_list *status)
{
size_t i, max_i = git_status_list_entrycount(status);
cl_assert_equal_sz(entry_count0, max_i);
for (i = 0; i < max_i; ++i) {
const git_status_entry *entry = git_status_byindex(status, i);
cl_assert_equal_i(entry_statuses0[i], entry->status);
}
}
void test_status_worktree__update_stat_cache_0(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *status;
git_diff_perfdata perf = GIT_DIFF_PERFDATA_INIT;
git_index *index;
opts.flags = GIT_STATUS_OPT_DEFAULTS;
cl_git_pass(git_status_list_new(&status, repo, &opts));
check_status0(status);
cl_git_pass(git_status_list_get_perfdata(&perf, status));
cl_assert_equal_sz(13 + 3, perf.stat_calls);
cl_assert_equal_sz(5, perf.oid_calculations);
git_status_list_free(status);
/* tick the index so we avoid recalculating racily-clean entries */
cl_git_pass(git_repository_index__weakptr(&index, repo));
tick_index(index);
opts.flags |= GIT_STATUS_OPT_UPDATE_INDEX;
cl_git_pass(git_status_list_new(&status, repo, &opts));
check_status0(status);
cl_git_pass(git_status_list_get_perfdata(&perf, status));
cl_assert_equal_sz(13 + 3, perf.stat_calls);
cl_assert_equal_sz(5, perf.oid_calculations);
git_status_list_free(status);
opts.flags &= ~GIT_STATUS_OPT_UPDATE_INDEX;
/* tick again as the index updating from the previous diff might have reset the timestamp */
tick_index(index);
cl_git_pass(git_status_list_new(&status, repo, &opts));
check_status0(status);
cl_git_pass(git_status_list_get_perfdata(&perf, status));
cl_assert_equal_sz(13 + 3, perf.stat_calls);
cl_assert_equal_sz(0, perf.oid_calculations);
git_status_list_free(status);
}
void test_status_worktree__unreadable(void)
{
#ifndef GIT_WIN32
const char *expected_paths[] = { "no_permission/foo" };
const unsigned int expected_statuses[] = {GIT_STATUS_WT_UNREADABLE};
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts = {0};
if (geteuid() == 0)
cl_skip();
/* Create directory with no read permission */
cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", 0777));
cl_git_mkfile("empty_standard_repo/no_permission/foo", "dummy");
p_chmod("empty_standard_repo/no_permission", 0644);
counts.expected_entry_count = 1;
counts.expected_paths = expected_paths;
counts.expected_statuses = expected_statuses;
opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_INCLUDE_UNREADABLE;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) );
/* Restore permissions so we can cleanup :) */
p_chmod("empty_standard_repo/no_permission", 0777);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
#else
cl_skip();
#endif
}
void test_status_worktree__unreadable_not_included(void)
{
#ifndef GIT_WIN32
const char *expected_paths[] = { "no_permission/" };
const unsigned int expected_statuses[] = {GIT_STATUS_WT_NEW};
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts = {0};
/* Create directory with no read permission */
cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", 0777));
cl_git_mkfile("empty_standard_repo/no_permission/foo", "dummy");
p_chmod("empty_standard_repo/no_permission", 0644);
counts.expected_entry_count = 1;
counts.expected_paths = expected_paths;
counts.expected_statuses = expected_statuses;
opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
opts.flags = (GIT_STATUS_OPT_INCLUDE_IGNORED | GIT_STATUS_OPT_INCLUDE_UNTRACKED);
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) );
/* Restore permissions so we can cleanup :) */
p_chmod("empty_standard_repo/no_permission", 0777);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
#else
cl_skip();
#endif
}
void test_status_worktree__unreadable_as_untracked(void)
{
const char *expected_paths[] = { "no_permission/foo" };
const unsigned int expected_statuses[] = {GIT_STATUS_WT_NEW};
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts = {0};
/* Create directory with no read permission */
cl_git_pass(git_futils_mkdir_r("empty_standard_repo/no_permission", 0777));
cl_git_mkfile("empty_standard_repo/no_permission/foo", "dummy");
p_chmod("empty_standard_repo/no_permission", 0644);
counts.expected_entry_count = 1;
counts.expected_paths = expected_paths;
counts.expected_statuses = expected_statuses;
opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
opts.flags = GIT_STATUS_OPT_DEFAULTS |
GIT_STATUS_OPT_INCLUDE_UNREADABLE |
GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) );
/* Restore permissions so we can cleanup :) */
p_chmod("empty_standard_repo/no_permission", 0777);
cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
cl_assert_equal_i(0, counts.wrong_status_flags_count);
cl_assert_equal_i(0, counts.wrong_sorted_path);
}
void test_status_worktree__update_index_with_symlink_doesnt_change_mode(void)
{
git_repository *repo = cl_git_sandbox_init("testrepo");
git_reference *head;
git_object *head_object;
git_index *index;
const git_index_entry *idx_entry;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
status_entry_counts counts = {0};
const char *expected_paths[] = { "README" };
const unsigned int expected_statuses[] = {GIT_STATUS_WT_NEW};
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_UPDATE_INDEX;
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL));
cl_git_rewritefile("testrepo/README", "This was rewritten.");
/* this status rewrites the index because we have changed the
* contents of a tracked file
*/
counts.expected_entry_count = 1;
counts.expected_paths = expected_paths;
counts.expected_statuses = expected_statuses;
cl_git_pass(
git_status_foreach_ext(repo, &opts, cb_status__normal, &counts));
cl_assert_equal_i(1, counts.entry_count);
/* now ensure that the status's rewrite of the index did not screw
* up the mode of the symlink `link_to_new.txt`, particularly
* on platforms that don't support symlinks
*/
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_read(index, true));
cl_assert(idx_entry = git_index_get_bypath(index, "link_to_new.txt", 0));
cl_assert(S_ISLNK(idx_entry->mode));
git_index_free(index);
git_object_free(head_object);
git_reference_free(head);
}
static const char *testrepo2_subdir_paths[] = {
"subdir/README",
"subdir/new.txt",
"subdir/subdir2/README",
"subdir/subdir2/new.txt",
};
static const char *testrepo2_subdir_paths_icase[] = {
"subdir/new.txt",
"subdir/README",
"subdir/subdir2/new.txt",
"subdir/subdir2/README"
};
void test_status_worktree__with_directory_in_pathlist(void)
{
git_repository *repo = cl_git_sandbox_init("testrepo2");
git_index *index;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *statuslist;
const git_status_entry *status;
size_t i, entrycount;
bool native_ignore_case;
char *subdir_path = "subdir";
cl_git_pass(git_repository_index(&index, repo));
native_ignore_case =
(git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0;
git_index_free(index);
opts.pathspec.strings = &subdir_path;
opts.pathspec.count = 1;
opts.flags =
GIT_STATUS_OPT_DEFAULTS |
GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;
opts.show = GIT_STATUS_SHOW_WORKDIR_ONLY;
git_status_list_new(&statuslist, repo, &opts);
entrycount = git_status_list_entrycount(statuslist);
cl_assert_equal_i(4, entrycount);
for (i = 0; i < entrycount; i++) {
status = git_status_byindex(statuslist, i);
cl_assert_equal_i(0, status->status);
cl_assert_equal_s(native_ignore_case ?
testrepo2_subdir_paths_icase[i] :
testrepo2_subdir_paths[i],
status->index_to_workdir->old_file.path);
}
git_status_list_free(statuslist);
opts.show = GIT_STATUS_SHOW_INDEX_ONLY;
git_status_list_new(&statuslist, repo, &opts);
entrycount = git_status_list_entrycount(statuslist);
cl_assert_equal_i(4, entrycount);
for (i = 0; i < entrycount; i++) {
status = git_status_byindex(statuslist, i);
cl_assert_equal_i(0, status->status);
cl_assert_equal_s(native_ignore_case ?
testrepo2_subdir_paths_icase[i] :
testrepo2_subdir_paths[i],
status->head_to_index->old_file.path);
}
git_status_list_free(statuslist);
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
git_status_list_new(&statuslist, repo, &opts);
entrycount = git_status_list_entrycount(statuslist);
cl_assert_equal_i(4, entrycount);
for (i = 0; i < entrycount; i++) {
status = git_status_byindex(statuslist, i);
cl_assert_equal_i(0, status->status);
cl_assert_equal_s(native_ignore_case ?
testrepo2_subdir_paths_icase[i] :
testrepo2_subdir_paths[i],
status->index_to_workdir->old_file.path);
}
git_status_list_free(statuslist);
}
void test_status_worktree__at_head_parent(void)
{
git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_status_list *statuslist;
git_tree *parent_tree;
const git_status_entry *status;
cl_git_mkfile("empty_standard_repo/file1", "ping");
stage_and_commit(repo, "file1");
cl_git_pass(git_repository_head_tree(&parent_tree, repo));
cl_git_mkfile("empty_standard_repo/file2", "pong");
stage_and_commit(repo, "file2");
cl_git_rewritefile("empty_standard_repo/file2", "pyng");
opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.baseline = parent_tree;
cl_git_pass(git_status_list_new(&statuslist, repo, &opts));
cl_assert_equal_sz(1, git_status_list_entrycount(statuslist));
status = git_status_byindex(statuslist, 0);
cl_assert(status != NULL);
cl_assert_equal_s("file2", status->index_to_workdir->old_file.path);
cl_assert_equal_i(GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW, status->status);
git_tree_free(parent_tree);
git_status_list_free(statuslist);
}
| libgit2-main | tests/libgit2/status/worktree.c |
#include "clar_libgit2.h"
#include "path.h"
#include "posix.h"
#include "status_helpers.h"
#include "util.h"
#include "status.h"
static git_repository *g_repo = NULL;
void test_status_renames__initialize(void)
{
g_repo = cl_git_sandbox_init("renames");
cl_repo_set_bool(g_repo, "core.autocrlf", false);
}
void test_status_renames__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void _rename_helper(
git_repository *repo, const char *from, const char *to, const char *extra)
{
git_str oldpath = GIT_STR_INIT, newpath = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(
&oldpath, git_repository_workdir(repo), from));
cl_git_pass(git_str_joinpath(
&newpath, git_repository_workdir(repo), to));
cl_git_pass(p_rename(oldpath.ptr, newpath.ptr));
if (extra)
cl_git_append2file(newpath.ptr, extra);
git_str_dispose(&oldpath);
git_str_dispose(&newpath);
}
#define rename_file(R,O,N) _rename_helper((R), (O), (N), NULL)
#define rename_and_edit_file(R,O,N) \
_rename_helper((R), (O), (N), "Added at the end to keep similarity!")
struct status_entry {
git_status_t status;
const char *oldname;
const char *newname;
};
static void check_status(
git_status_list *status_list,
struct status_entry *expected_list,
size_t expected_len)
{
const git_status_entry *actual;
const struct status_entry *expected;
const char *oldname, *newname;
size_t i, files_in_status = git_status_list_entrycount(status_list);
cl_assert_equal_sz(expected_len, files_in_status);
for (i = 0; i < expected_len; i++) {
actual = git_status_byindex(status_list, i);
expected = &expected_list[i];
oldname = actual->head_to_index ? actual->head_to_index->old_file.path :
actual->index_to_workdir ? actual->index_to_workdir->old_file.path : NULL;
newname = actual->index_to_workdir ? actual->index_to_workdir->new_file.path :
actual->head_to_index ? actual->head_to_index->new_file.path : NULL;
cl_assert_equal_i_fmt(expected->status, actual->status, "%04x");
if (expected->oldname) {
cl_assert(oldname != NULL);
cl_assert_equal_s(oldname, expected->oldname);
} else {
cl_assert(oldname == NULL);
}
if (actual->status & (GIT_STATUS_INDEX_RENAMED|GIT_STATUS_WT_RENAMED)) {
if (expected->newname) {
cl_assert(newname != NULL);
cl_assert_equal_s(newname, expected->newname);
} else {
cl_assert(newname == NULL);
}
}
}
}
void test_status_renames__head2index_one(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED, "ikeepsix.txt", "newname.txt" },
};
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "newname.txt");
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "newname.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 1);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__head2index_two(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED,
"sixserving.txt", "aaa.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED,
"untimely.txt", "bbb.txt" },
{ GIT_STATUS_INDEX_RENAMED, "songof7cities.txt", "ccc.txt" },
{ GIT_STATUS_INDEX_RENAMED, "ikeepsix.txt", "ddd.txt" },
};
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "ddd.txt");
rename_and_edit_file(g_repo, "sixserving.txt", "aaa.txt");
rename_file(g_repo, "songof7cities.txt", "ccc.txt");
rename_and_edit_file(g_repo, "untimely.txt", "bbb.txt");
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_remove_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_remove_bypath(index, "songof7cities.txt"));
cl_git_pass(git_index_remove_bypath(index, "untimely.txt"));
cl_git_pass(git_index_add_bypath(index, "ddd.txt"));
cl_git_pass(git_index_add_bypath(index, "aaa.txt"));
cl_git_pass(git_index_add_bypath(index, "ccc.txt"));
cl_git_pass(git_index_add_bypath(index, "bbb.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 4);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__head2index_no_rename_from_rewrite(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_MODIFIED, "ikeepsix.txt", "ikeepsix.txt" },
{ GIT_STATUS_INDEX_MODIFIED, "sixserving.txt", "sixserving.txt" },
};
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "_temp_.txt");
rename_file(g_repo, "sixserving.txt", "ikeepsix.txt");
rename_file(g_repo, "_temp_.txt", "sixserving.txt");
cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 2);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__head2index_rename_from_rewrite(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED, "sixserving.txt", "ikeepsix.txt" },
{ GIT_STATUS_INDEX_RENAMED, "ikeepsix.txt", "sixserving.txt" },
};
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_FROM_REWRITES;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "_temp_.txt");
rename_file(g_repo, "sixserving.txt", "ikeepsix.txt");
rename_file(g_repo, "_temp_.txt", "sixserving.txt");
cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 2);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__index2workdir_one(void)
{
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "newname.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
rename_file(g_repo, "ikeepsix.txt", "newname.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 1);
git_status_list_free(statuslist);
}
void test_status_renames__index2workdir_two(void)
{
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
"sixserving.txt", "aaa.txt" },
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
"untimely.txt", "bbb.txt" },
{ GIT_STATUS_WT_RENAMED, "songof7cities.txt", "ccc.txt" },
{ GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "ddd.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
rename_file(g_repo, "ikeepsix.txt", "ddd.txt");
rename_and_edit_file(g_repo, "sixserving.txt", "aaa.txt");
rename_file(g_repo, "songof7cities.txt", "ccc.txt");
rename_and_edit_file(g_repo, "untimely.txt", "bbb.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 4);
git_status_list_free(statuslist);
}
void test_status_renames__index2workdir_rename_from_rewrite(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_WT_RENAMED, "sixserving.txt", "ikeepsix.txt" },
{ GIT_STATUS_WT_RENAMED, "ikeepsix.txt", "sixserving.txt" },
};
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
opts.flags |= GIT_STATUS_OPT_RENAMES_FROM_REWRITES;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "_temp_.txt");
rename_file(g_repo, "sixserving.txt", "ikeepsix.txt");
rename_file(g_repo, "_temp_.txt", "sixserving.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 2);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__both_one(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"ikeepsix.txt", "newname-workdir.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "newname-index.txt");
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "newname-index.txt"));
cl_git_pass(git_index_write(index));
rename_file(g_repo, "newname-index.txt", "newname-workdir.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 1);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__both_two(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED |
GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
"ikeepsix.txt", "ikeepsix-both.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED,
"sixserving.txt", "sixserving-index.txt" },
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
"songof7cities.txt", "songof7cities-workdir.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"untimely.txt", "untimely-both.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_repository_index(&index, g_repo));
rename_and_edit_file(g_repo, "ikeepsix.txt", "ikeepsix-index.txt");
rename_and_edit_file(g_repo, "sixserving.txt", "sixserving-index.txt");
rename_file(g_repo, "untimely.txt", "untimely-index.txt");
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_remove_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_remove_bypath(index, "untimely.txt"));
cl_git_pass(git_index_add_bypath(index, "ikeepsix-index.txt"));
cl_git_pass(git_index_add_bypath(index, "sixserving-index.txt"));
cl_git_pass(git_index_add_bypath(index, "untimely-index.txt"));
cl_git_pass(git_index_write(index));
rename_and_edit_file(g_repo, "ikeepsix-index.txt", "ikeepsix-both.txt");
rename_and_edit_file(g_repo, "songof7cities.txt", "songof7cities-workdir.txt");
rename_file(g_repo, "untimely-index.txt", "untimely-both.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 4);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__both_rename_from_rewrite(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"songof7cities.txt", "ikeepsix.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"ikeepsix.txt", "sixserving.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"sixserving.txt", "songof7cities.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
opts.flags |= GIT_STATUS_OPT_RENAMES_FROM_REWRITES;
cl_git_pass(git_repository_index(&index, g_repo));
rename_file(g_repo, "ikeepsix.txt", "_temp_.txt");
rename_file(g_repo, "sixserving.txt", "ikeepsix.txt");
rename_file(g_repo, "songof7cities.txt", "sixserving.txt");
rename_file(g_repo, "_temp_.txt", "songof7cities.txt");
cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
cl_git_pass(git_index_write(index));
rename_file(g_repo, "songof7cities.txt", "_temp_.txt");
rename_file(g_repo, "ikeepsix.txt", "songof7cities.txt");
rename_file(g_repo, "sixserving.txt", "ikeepsix.txt");
rename_file(g_repo, "_temp_.txt", "sixserving.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 3);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__rewrites_only_for_renames(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_WT_MODIFIED, "ikeepsix.txt", "ikeepsix.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
opts.flags |= GIT_STATUS_OPT_RENAMES_FROM_REWRITES;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_rewritefile("renames/ikeepsix.txt",
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n" \
"This is enough content for the file to be rewritten.\n");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 1);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__both_casechange_one(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
int index_caps;
struct status_entry expected_icase[] = {
{ GIT_STATUS_INDEX_RENAMED,
"ikeepsix.txt", "IKeepSix.txt" },
};
struct status_entry expected_case[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"ikeepsix.txt", "IKEEPSIX.txt" },
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_repository_index(&index, g_repo));
index_caps = git_index_caps(index);
rename_file(g_repo, "ikeepsix.txt", "IKeepSix.txt");
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, "IKeepSix.txt"));
cl_git_pass(git_index_write(index));
/* on a case-insensitive file system, this change won't matter.
* on a case-sensitive one, it will.
*/
rename_file(g_repo, "IKeepSix.txt", "IKEEPSIX.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, (index_caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) ?
expected_icase : expected_case, 1);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__both_casechange_two(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
int index_caps;
struct status_entry expected_icase[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED |
GIT_STATUS_WT_MODIFIED,
"ikeepsix.txt", "IKeepSix.txt" },
{ GIT_STATUS_INDEX_MODIFIED,
"sixserving.txt", "sixserving.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_MODIFIED,
"songof7cities.txt", "songof7.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"untimely.txt", "untimeliest.txt" }
};
struct status_entry expected_case[] = {
{ GIT_STATUS_INDEX_RENAMED |
GIT_STATUS_WT_MODIFIED | GIT_STATUS_WT_RENAMED,
"songof7cities.txt", "SONGOF7.txt" },
{ GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_RENAMED,
"sixserving.txt", "SixServing.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_INDEX_MODIFIED |
GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED,
"ikeepsix.txt", "ikeepsix.txt" },
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED,
"untimely.txt", "untimeliest.txt" }
};
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_repository_index(&index, g_repo));
index_caps = git_index_caps(index);
rename_and_edit_file(g_repo, "ikeepsix.txt", "IKeepSix.txt");
rename_and_edit_file(g_repo, "sixserving.txt", "sixserving.txt");
rename_file(g_repo, "songof7cities.txt", "songof7.txt");
rename_file(g_repo, "untimely.txt", "untimelier.txt");
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_remove_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_remove_bypath(index, "songof7cities.txt"));
cl_git_pass(git_index_remove_bypath(index, "untimely.txt"));
cl_git_pass(git_index_add_bypath(index, "IKeepSix.txt"));
cl_git_pass(git_index_add_bypath(index, "sixserving.txt"));
cl_git_pass(git_index_add_bypath(index, "songof7.txt"));
cl_git_pass(git_index_add_bypath(index, "untimelier.txt"));
cl_git_pass(git_index_write(index));
rename_and_edit_file(g_repo, "IKeepSix.txt", "ikeepsix.txt");
rename_file(g_repo, "sixserving.txt", "SixServing.txt");
rename_and_edit_file(g_repo, "songof7.txt", "SONGOF7.txt");
rename_file(g_repo, "untimelier.txt", "untimeliest.txt");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, (index_caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) ?
expected_icase : expected_case, 4);
git_status_list_free(statuslist);
git_index_free(index);
}
void test_status_renames__zero_byte_file_does_not_fail(void)
{
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected[] = {
{ GIT_STATUS_WT_DELETED, "ikeepsix.txt", "ikeepsix.txt" },
{ GIT_STATUS_WT_NEW, "zerobyte.txt", "zerobyte.txt" },
};
opts.flags |= GIT_STATUS_OPT_RENAMES_FROM_REWRITES |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR |
GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
GIT_STATUS_SHOW_INDEX_AND_WORKDIR |
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
p_unlink("renames/ikeepsix.txt");
cl_git_mkfile("renames/zerobyte.txt", "");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 2);
git_status_list_free(statuslist);
}
#ifdef GIT_USE_ICONV
static char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
static char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
#endif
/*
* Create a file in NFD (canonically decomposed) format. Ensure
* that when core.precomposeunicode is false that we return paths
* in NFD, but when core.precomposeunicode is true, then we
* return paths precomposed (in NFC).
*/
void test_status_renames__precomposed_unicode_rename(void)
{
#ifdef GIT_USE_ICONV
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected0[] = {
{ GIT_STATUS_WT_NEW, nfd, NULL },
{ GIT_STATUS_WT_DELETED, "sixserving.txt", NULL },
};
struct status_entry expected1[] = {
{ GIT_STATUS_WT_RENAMED, "sixserving.txt", nfd },
};
struct status_entry expected2[] = {
{ GIT_STATUS_WT_DELETED, "sixserving.txt", NULL },
{ GIT_STATUS_WT_NEW, nfc, NULL },
};
struct status_entry expected3[] = {
{ GIT_STATUS_WT_RENAMED, "sixserving.txt", nfc },
};
rename_file(g_repo, "sixserving.txt", nfd);
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
cl_repo_set_bool(g_repo, "core.precomposeunicode", false);
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected0, ARRAY_SIZE(expected0));
git_status_list_free(statuslist);
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected1, ARRAY_SIZE(expected1));
git_status_list_free(statuslist);
cl_repo_set_bool(g_repo, "core.precomposeunicode", true);
opts.flags &= ~GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected2, ARRAY_SIZE(expected2));
git_status_list_free(statuslist);
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected3, ARRAY_SIZE(expected3));
git_status_list_free(statuslist);
#endif
}
void test_status_renames__precomposed_unicode_toggle_is_rename(void)
{
#ifdef GIT_USE_ICONV
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
struct status_entry expected0[] = {
{ GIT_STATUS_INDEX_RENAMED, "ikeepsix.txt", nfd },
};
struct status_entry expected1[] = {
{ GIT_STATUS_WT_RENAMED, nfd, nfc },
};
struct status_entry expected2[] = {
{ GIT_STATUS_INDEX_RENAMED, nfd, nfc },
};
struct status_entry expected3[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_RENAMED, nfd, nfd },
};
cl_repo_set_bool(g_repo, "core.precomposeunicode", false);
rename_file(g_repo, "ikeepsix.txt", nfd);
{
git_index *index;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
cl_git_pass(git_index_add_bypath(index, nfd));
cl_git_pass(git_index_write(index));
git_index_free(index);
}
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected0, ARRAY_SIZE(expected0));
git_status_list_free(statuslist);
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "commit nfd");
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
cl_assert_equal_sz(0, git_status_list_entrycount(statuslist));
git_status_list_free(statuslist);
cl_repo_set_bool(g_repo, "core.precomposeunicode", true);
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected1, ARRAY_SIZE(expected1));
git_status_list_free(statuslist);
{
git_index *index;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_remove_bypath(index, nfd));
cl_git_pass(git_index_add_bypath(index, nfc));
cl_git_pass(git_index_write(index));
git_index_free(index);
}
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected2, ARRAY_SIZE(expected2));
git_status_list_free(statuslist);
cl_repo_set_bool(g_repo, "core.precomposeunicode", false);
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected3, ARRAY_SIZE(expected3));
git_status_list_free(statuslist);
#endif
}
void test_status_renames__rename_threshold(void)
{
git_index *index;
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
_rename_helper(g_repo, "ikeepsix.txt", "newname.txt",
"Line 1\n" \
"Line 2\n" \
"Line 3\n" \
"Line 4\n" \
"Line 5\n" \
"Line 6\n" \
"Line 7\n" \
"Line 8\n" \
"Line 9\n"
);
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
cl_git_pass(git_repository_index(&index, g_repo));
/* Default threshold */
{
struct status_entry expected[] = {
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED, "ikeepsix.txt", "newname.txt" },
};
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 1);
git_status_list_free(statuslist);
}
/* Threshold set to 90 */
{
struct status_entry expected[] = {
{ GIT_STATUS_WT_DELETED, "ikeepsix.txt", NULL },
{ GIT_STATUS_WT_NEW, "newname.txt", NULL }
};
opts.rename_threshold = 90;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 2);
git_status_list_free(statuslist);
}
/* Threshold set to 25 */
{
struct status_entry expected[] = {
{ GIT_STATUS_WT_RENAMED | GIT_STATUS_WT_MODIFIED, "ikeepsix.txt", "newname.txt" },
};
opts.rename_threshold = 25;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 1);
git_status_list_free(statuslist);
}
git_index_free(index);
}
void test_status_renames__case_insensitive_h2i_and_i2wc(void)
{
git_status_list *statuslist;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;
git_reference *head, *test_branch;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
git_str path_to_delete = GIT_STR_INIT;
git_str path_to_edit = GIT_STR_INIT;
git_index *index;
git_strarray paths = { NULL, 0 };
struct status_entry expected[] = {
{ GIT_STATUS_INDEX_RENAMED | GIT_STATUS_WT_MODIFIED, "sixserving.txt", "sixserving-renamed.txt" },
{ GIT_STATUS_INDEX_DELETED, "Wow.txt", "Wow.txt" }
};
/* Checkout the correct branch */
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_git_pass(git_reference_symbolic_set_target(
&test_branch, head, "refs/heads/case-insensitive-status", NULL));
cl_git_pass(git_checkout_head(g_repo, &checkout_opts));
cl_git_pass(git_repository_index(&index, g_repo));
/* Rename sixserving.txt, delete Wow.txt, and stage those changes */
rename_file(g_repo, "sixserving.txt", "sixserving-renamed.txt");
cl_git_pass(git_str_joinpath(
&path_to_delete, git_repository_workdir(g_repo), "Wow.txt"));
cl_git_rmfile(path_to_delete.ptr);
cl_git_pass(git_index_add_all(index, &paths, GIT_INDEX_ADD_FORCE, NULL, NULL));
cl_git_pass(git_index_write(index));
/* Change content of sixserving-renamed.txt */
cl_git_pass(git_str_joinpath(
&path_to_edit, git_repository_workdir(g_repo), "sixserving-renamed.txt"));
cl_git_append2file(path_to_edit.ptr, "New content\n");
/* Run status */
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED;
opts.flags |= GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR;
opts.flags |= GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX;
opts.flags |= GIT_STATUS_OPT_SORT_CASE_INSENSITIVELY;
cl_git_pass(git_status_list_new(&statuslist, g_repo, &opts));
check_status(statuslist, expected, 2);
git_status_list_free(statuslist);
git_index_free(index);
git_str_dispose(&path_to_delete);
git_str_dispose(&path_to_edit);
git_reference_free(head);
git_reference_free(test_branch);
}
| libgit2-main | tests/libgit2/status/renames.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "fetchhead.h"
#include "fetchhead_data.h"
#define DO_LOCAL_TEST 0
static git_repository *g_repo;
void test_fetchhead_nonetwork__initialize(void)
{
g_repo = NULL;
}
static void cleanup_repository(void *path)
{
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
cl_fixture_cleanup((const char *)path);
}
static void populate_fetchhead(git_vector *out, git_repository *repo)
{
git_fetchhead_ref *fetchhead_ref;
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid,
"49322bb17d3acc9146f98c97d078513228bbf3c0",
GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 1,
"refs/heads/master",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid__fromstr(&oid,
"0966a434eb1a025db6b71485ab63a3bfbea520b6",
GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/heads/first-merge",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid__fromstr(&oid,
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",
GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/heads/no-parent",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid__fromstr(&oid,
"d96c4e80345534eccee5ac7b07fc7603b56124cb",
GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/tags/annotated_tag",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid__fromstr(&oid,
"55a1a760df4b86a02094a904dfa511deb5655905",
GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/tags/blob",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid__fromstr(&oid,
"8f50ba15d49353813cc6e20298002c0d17b0a9ee",
GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/tags/commit_tree",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_fetchhead_write(repo, out));
}
void test_fetchhead_nonetwork__write(void)
{
git_vector fetchhead_vector = GIT_VECTOR_INIT;
git_fetchhead_ref *fetchhead_ref;
git_str fetchhead_buf = GIT_STR_INIT;
int equals = 0;
size_t i;
cl_git_pass(git_vector_init(&fetchhead_vector, 6, NULL));
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
populate_fetchhead(&fetchhead_vector, g_repo);
cl_git_pass(git_futils_readbuffer(&fetchhead_buf,
"./test1/.git/FETCH_HEAD"));
equals = (strcmp(fetchhead_buf.ptr, FETCH_HEAD_WILDCARD_DATA_LOCAL) == 0);
git_str_dispose(&fetchhead_buf);
git_vector_foreach(&fetchhead_vector, i, fetchhead_ref) {
git_fetchhead_ref_free(fetchhead_ref);
}
git_vector_free(&fetchhead_vector);
cl_assert(equals);
}
typedef struct {
git_vector *fetchhead_vector;
size_t idx;
} fetchhead_ref_cb_data;
static int fetchhead_ref_cb(const char *name, const char *url,
const git_oid *oid, unsigned int is_merge, void *payload)
{
fetchhead_ref_cb_data *cb_data = payload;
git_fetchhead_ref *expected;
cl_assert(payload);
expected = git_vector_get(cb_data->fetchhead_vector, cb_data->idx);
cl_assert_equal_oid(&expected->oid, oid);
cl_assert(expected->is_merge == is_merge);
if (expected->ref_name)
cl_assert_equal_s(expected->ref_name, name);
else
cl_assert(name == NULL);
if (expected->remote_url)
cl_assert_equal_s(expected->remote_url, url);
else
cl_assert(url == NULL);
cb_data->idx++;
return 0;
}
void test_fetchhead_nonetwork__read(void)
{
git_vector fetchhead_vector = GIT_VECTOR_INIT;
git_fetchhead_ref *fetchhead_ref;
fetchhead_ref_cb_data cb_data;
size_t i;
memset(&cb_data, 0x0, sizeof(fetchhead_ref_cb_data));
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
populate_fetchhead(&fetchhead_vector, g_repo);
cb_data.fetchhead_vector = &fetchhead_vector;
cl_git_pass(git_repository_fetchhead_foreach(g_repo, fetchhead_ref_cb, &cb_data));
git_vector_foreach(&fetchhead_vector, i, fetchhead_ref) {
git_fetchhead_ref_free(fetchhead_ref);
}
git_vector_free(&fetchhead_vector);
}
static int read_old_style_cb(const char *name, const char *url,
const git_oid *oid, unsigned int is_merge, void *payload)
{
git_oid expected;
GIT_UNUSED(payload);
git_oid__fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1);
cl_assert(name == NULL);
cl_assert(url == NULL);
cl_assert_equal_oid(&expected, oid);
cl_assert(is_merge == 1);
return 0;
}
void test_fetchhead_nonetwork__read_old_style(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\n");
cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_old_style_cb, NULL));
}
static int read_type_missing(const char *ref_name, const char *remote_url,
const git_oid *oid, unsigned int is_merge, void *payload)
{
git_oid expected;
GIT_UNUSED(payload);
git_oid__fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1);
cl_assert_equal_s("name", ref_name);
cl_assert_equal_s("remote_url", remote_url);
cl_assert_equal_oid(&expected, oid);
cl_assert(is_merge == 0);
return 0;
}
void test_fetchhead_nonetwork__type_missing(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\t'name' of remote_url\n");
cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_type_missing, NULL));
}
static int read_name_missing(const char *ref_name, const char *remote_url,
const git_oid *oid, unsigned int is_merge, void *payload)
{
git_oid expected;
GIT_UNUSED(payload);
git_oid__fromstr(&expected, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1);
cl_assert(ref_name == NULL);
cl_assert_equal_s("remote_url", remote_url);
cl_assert_equal_oid(&expected, oid);
cl_assert(is_merge == 0);
return 0;
}
void test_fetchhead_nonetwork__name_missing(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tremote_url\n");
cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_name_missing, NULL));
}
static int read_noop(const char *ref_name, const char *remote_url,
const git_oid *oid, unsigned int is_merge, void *payload)
{
GIT_UNUSED(ref_name);
GIT_UNUSED(remote_url);
GIT_UNUSED(oid);
GIT_UNUSED(is_merge);
GIT_UNUSED(payload);
return 0;
}
void test_fetchhead_nonetwork__nonexistent(void)
{
int error;
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_fail((error = git_repository_fetchhead_foreach(g_repo, read_noop, NULL)));
cl_assert(error == GIT_ENOTFOUND);
}
void test_fetchhead_nonetwork__invalid_unterminated_last_line(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "unterminated");
cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
}
void test_fetchhead_nonetwork__invalid_oid(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "shortoid\n");
cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
}
void test_fetchhead_nonetwork__invalid_for_merge(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tinvalid-merge\t\n");
cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
cl_assert(git__prefixcmp(git_error_last()->message, "invalid for-merge") == 0);
}
void test_fetchhead_nonetwork__invalid_description(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\n");
cl_git_fail(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
cl_assert(git__prefixcmp(git_error_last()->message, "invalid description") == 0);
}
static int assert_master_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data)
{
GIT_UNUSED(url);
GIT_UNUSED(id);
GIT_UNUSED(data);
if (!strcmp("refs/heads/master", ref) && !is_merge)
return -1;
return 0;
}
static int assert_none_for_merge(const char *ref, const char *url, const git_oid *id, unsigned int is_merge, void *data)
{
GIT_UNUSED(ref);
GIT_UNUSED(url);
GIT_UNUSED(id);
GIT_UNUSED(data);
return is_merge ? -1 : 0;
}
void test_fetchhead_nonetwork__unborn_with_upstream(void)
{
git_repository *repo;
git_remote *remote;
/* Create an empty repo to clone from */
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_set_cleanup(&cleanup_repository, "./repowithunborn");
cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
/* Simulate someone pushing to it by changing to one that has stuff */
cl_git_pass(git_remote_set_url(repo, "origin", cl_fixture("testrepo.git")));
cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
git_remote_free(remote);
cl_git_pass(git_repository_fetchhead_foreach(repo, assert_master_for_merge, NULL));
git_repository_free(repo);
cl_fixture_cleanup("./repowithunborn");
}
void test_fetchhead_nonetwork__fetch_into_repo_with_symrefs(void)
{
git_repository *repo;
git_remote *remote;
git_reference *symref;
repo = cl_git_sandbox_init("empty_standard_repo");
/*
* Testing for a specific constellation where the repository has at
* least one symbolic reference in its refdb.
*/
cl_git_pass(git_reference_symbolic_create(&symref, repo, "refs/heads/symref", "refs/heads/master", 0, NULL));
cl_git_pass(git_remote_set_url(repo, "origin", cl_fixture("testrepo.git")));
cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
git_remote_free(remote);
git_reference_free(symref);
cl_git_sandbox_cleanup();
}
void test_fetchhead_nonetwork__fetch_into_repo_with_invalid_head(void)
{
git_remote *remote;
char *strings[] = { "refs/heads/*:refs/remotes/origin/*" };
git_strarray refspecs = { strings, 1 };
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
/* HEAD pointing to nonexistent branch */
cl_git_rewritefile("./test1/.git/HEAD", "ref: refs/heads/\n");
cl_git_pass(git_remote_create_anonymous(&remote, g_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
cl_git_pass(git_repository_fetchhead_foreach(g_repo, assert_none_for_merge, NULL));
git_remote_free(remote);
}
void test_fetchhead_nonetwork__quote_in_branch_name(void)
{
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_rewritefile("./test1/.git/FETCH_HEAD", FETCH_HEAD_QUOTE_DATA);
cl_git_pass(git_repository_fetchhead_foreach(g_repo, read_noop, NULL));
}
static bool found_master;
static bool found_haacked;
static bool find_master_haacked_called;
static int find_master_haacked(const char *ref_name, const char *remote_url, const git_oid *oid, unsigned int is_merge, void *payload)
{
GIT_UNUSED(remote_url);
GIT_UNUSED(oid);
GIT_UNUSED(payload);
find_master_haacked_called = true;
if (!strcmp("refs/heads/master", ref_name)) {
cl_assert(is_merge);
found_master = true;
}
if (!strcmp("refs/heads/haacked", ref_name)) {
cl_assert(is_merge);
found_haacked = true;
}
return 0;
}
void test_fetchhead_nonetwork__create_when_refpecs_given(void)
{
git_remote *remote;
git_str path = GIT_STR_INIT;
char *refspec1 = "refs/heads/master";
char *refspec2 = "refs/heads/haacked";
char *refspecs[] = { refspec1, refspec2 };
git_strarray specs = {
refspecs,
2,
};
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), "FETCH_HEAD"));
cl_git_pass(git_remote_create(&remote, g_repo, "origin", cl_fixture("testrepo.git")));
cl_assert(!git_fs_path_exists(path.ptr));
cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL));
cl_assert(git_fs_path_exists(path.ptr));
cl_git_pass(git_repository_fetchhead_foreach(g_repo, find_master_haacked, NULL));
cl_assert(find_master_haacked_called);
cl_assert(found_master);
cl_assert(found_haacked);
git_remote_free(remote);
git_str_dispose(&path);
}
static bool count_refs_called;
struct prefix_count {
const char *prefix;
int count;
int expected;
};
static int count_refs(const char *ref_name, const char *remote_url, const git_oid *oid, unsigned int is_merge, void *payload)
{
int i;
struct prefix_count *prefix_counts = (struct prefix_count *) payload;
GIT_UNUSED(remote_url);
GIT_UNUSED(oid);
GIT_UNUSED(is_merge);
count_refs_called = true;
for (i = 0; prefix_counts[i].prefix; i++) {
if (!git__prefixcmp(ref_name, prefix_counts[i].prefix))
prefix_counts[i].count++;
}
return 0;
}
void test_fetchhead_nonetwork__create_with_multiple_refspecs(void)
{
git_remote *remote;
git_str path = GIT_STR_INIT;
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_git_pass(git_remote_create(&remote, g_repo, "origin", cl_fixture("testrepo.git")));
git_remote_free(remote);
cl_git_pass(git_remote_add_fetch(g_repo, "origin", "+refs/notes/*:refs/origin/notes/*"));
/* Pick up the new refspec */
cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));
cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), "FETCH_HEAD"));
cl_assert(!git_fs_path_exists(path.ptr));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
cl_assert(git_fs_path_exists(path.ptr));
{
int i;
struct prefix_count prefix_counts[] = {
{"refs/notes/", 0, 1},
{"refs/heads/", 0, 13},
{"refs/tags/", 0, 7},
{NULL, 0, 0},
};
cl_git_pass(git_repository_fetchhead_foreach(g_repo, count_refs, &prefix_counts));
cl_assert(count_refs_called);
for (i = 0; prefix_counts[i].prefix; i++)
cl_assert_equal_i(prefix_counts[i].expected, prefix_counts[i].count);
}
git_remote_free(remote);
git_str_dispose(&path);
}
void test_fetchhead_nonetwork__credentials_are_stripped(void)
{
git_fetchhead_ref *ref;
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&ref, &oid, 0,
"refs/tags/commit_tree", "http://foo:[email protected]/libgit2/TestGitRepository"));
cl_assert_equal_s(ref->remote_url, "http://github.com/libgit2/TestGitRepository");
git_fetchhead_ref_free(ref);
cl_git_pass(git_oid__fromstr(&oid, "49322bb17d3acc9146f98c97d078513228bbf3c0", GIT_OID_SHA1));
cl_git_pass(git_fetchhead_ref_create(&ref, &oid, 0,
"refs/tags/commit_tree", "https://foo:[email protected]/libgit2/TestGitRepository"));
cl_assert_equal_s(ref->remote_url, "https://github.com/libgit2/TestGitRepository");
git_fetchhead_ref_free(ref);
}
| libgit2-main | tests/libgit2/fetchhead/nonetwork.c |
#include "clar_libgit2.h"
#include "git2/checkout.h"
#include "refs.h"
#include "path.h"
#include "repository.h"
#ifdef GIT_WIN32
# include <windows.h>
#else
# include <dirent.h>
#endif
static git_repository *repo;
static git_object *obj;
static git_checkout_options checkout_opts;
void test_checkout_icase__initialize(void)
{
git_oid id;
git_config *cfg;
int icase = 0;
repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_config_snapshot(&cfg, repo));
git_config_get_bool(&icase, cfg, "core.ignorecase");
git_config_free(cfg);
if (!icase)
cl_skip();
cl_git_pass(git_reference_name_to_id(&id, repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, repo, &id, GIT_OBJECT_ANY));
git_checkout_options_init(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE;
}
void test_checkout_icase__cleanup(void)
{
git_object_free(obj);
cl_git_sandbox_cleanup();
}
static char *get_filename(const char *in)
{
char *search_dirname, *search_filename, *filename = NULL;
git_str out = GIT_STR_INIT;
DIR *dir;
struct dirent *de;
cl_assert(search_dirname = git_fs_path_dirname(in));
cl_assert(search_filename = git_fs_path_basename(in));
cl_assert(dir = opendir(search_dirname));
while ((de = readdir(dir))) {
if (strcasecmp(de->d_name, search_filename) == 0) {
git_str_join(&out, '/', search_dirname, de->d_name);
filename = git_str_detach(&out);
break;
}
}
closedir(dir);
git__free(search_dirname);
git__free(search_filename);
git_str_dispose(&out);
return filename;
}
static void assert_name_is(const char *expected)
{
char *actual;
size_t actual_len, expected_len, start;
cl_assert(actual = get_filename(expected));
expected_len = strlen(expected);
actual_len = strlen(actual);
cl_assert(actual_len >= expected_len);
start = actual_len - expected_len;
cl_assert_equal_s(expected, actual + start);
if (start)
cl_assert_equal_strn("/", actual + (start - 1), 1);
free(actual);
}
static int symlink_or_fake(git_repository *repo, const char *a, const char *b)
{
int symlinks;
cl_git_pass(git_repository__configmap_lookup(&symlinks, repo, GIT_CONFIGMAP_SYMLINKS));
if (symlinks)
return p_symlink(a, b);
else
return git_futils_fake_symlink(a, b);
}
void test_checkout_icase__refuses_to_overwrite_files_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_git_write2file("testrepo/BRANCH_FILE.txt", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/BRANCH_FILE.txt");
}
void test_checkout_icase__overwrites_files_for_files_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_write2file("testrepo/NEW.txt", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/new.txt");
}
void test_checkout_icase__refuses_to_overwrite_links_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(symlink_or_fake(repo, "../tmp", "testrepo/BRANCH_FILE.txt"));
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_fs_path_exists("tmp"));
assert_name_is("testrepo/BRANCH_FILE.txt");
}
void test_checkout_icase__overwrites_links_for_files_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_must_pass(symlink_or_fake(repo, "../tmp", "testrepo/NEW.txt"));
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_fs_path_exists("tmp"));
assert_name_is("testrepo/new.txt");
}
void test_checkout_icase__overwrites_empty_folders_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/new.txt");
cl_assert(!git_fs_path_isdir("testrepo/new.txt"));
}
void test_checkout_icase__refuses_to_overwrite_populated_folders_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(p_mkdir("testrepo/BRANCH_FILE.txt", 0777));
cl_git_write2file("testrepo/BRANCH_FILE.txt/foobar", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/BRANCH_FILE.txt");
cl_assert(git_fs_path_isdir("testrepo/BRANCH_FILE.txt"));
}
void test_checkout_icase__overwrites_folders_for_files_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
cl_git_write2file("testrepo/NEW.txt/foobar", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/new.txt");
cl_assert(!git_fs_path_isdir("testrepo/new.txt"));
}
void test_checkout_icase__refuses_to_overwrite_files_for_folders(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_git_write2file("testrepo/A", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/A");
cl_assert(!git_fs_path_isdir("testrepo/A"));
}
void test_checkout_icase__overwrites_files_for_folders_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_write2file("testrepo/A", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/a");
cl_assert(git_fs_path_isdir("testrepo/a"));
}
void test_checkout_icase__refuses_to_overwrite_links_for_folders(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(symlink_or_fake(repo, "..", "testrepo/A"));
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_fs_path_exists("b.txt"));
assert_name_is("testrepo/A");
}
void test_checkout_icase__overwrites_links_for_folders_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_must_pass(symlink_or_fake(repo, "..", "testrepo/A"));
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_fs_path_exists("b.txt"));
assert_name_is("testrepo/a");
}
void test_checkout_icase__ignores_unstaged_casechange(void)
{
git_reference *orig_ref, *br2_ref;
git_commit *orig, *br2;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_lookup_resolved(&orig_ref, repo, "HEAD", 100));
cl_git_pass(git_commit_lookup(&orig, repo, git_reference_target(orig_ref)));
cl_git_pass(git_reset(repo, (git_object *)orig, GIT_RESET_HARD, NULL));
cl_rename("testrepo/branch_file.txt", "testrepo/Branch_File.txt");
cl_git_pass(git_reference_lookup_resolved(&br2_ref, repo, "refs/heads/br2", 100));
cl_git_pass(git_commit_lookup(&br2, repo, git_reference_target(br2_ref)));
cl_git_pass(git_checkout_tree(repo, (const git_object *)br2, &checkout_opts));
git_commit_free(orig);
git_commit_free(br2);
git_reference_free(orig_ref);
git_reference_free(br2_ref);
}
void test_checkout_icase__conflicts_with_casechanged_subtrees(void)
{
git_reference *orig_ref;
git_object *orig, *subtrees;
git_oid oid;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_lookup_resolved(&orig_ref, repo, "HEAD", 100));
cl_git_pass(git_object_lookup(&orig, repo, git_reference_target(orig_ref), GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(repo, (git_object *)orig, GIT_RESET_HARD, NULL));
cl_must_pass(p_mkdir("testrepo/AB", 0777));
cl_must_pass(p_mkdir("testrepo/AB/C", 0777));
cl_git_write2file("testrepo/AB/C/3.txt", "Foobar!\n", 8, O_RDWR|O_CREAT, 0666);
cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/subtrees"));
cl_git_pass(git_object_lookup(&subtrees, repo, &oid, GIT_OBJECT_ANY));
cl_git_fail(git_checkout_tree(repo, subtrees, &checkout_opts));
git_object_free(orig);
git_object_free(subtrees);
git_reference_free(orig_ref);
}
| libgit2-main | tests/libgit2/checkout/icase.c |
#include "clar_libgit2.h"
#include "diff_generate.h"
#include "git2/checkout.h"
#include "path.h"
#include "posix.h"
#include "futils.h"
static git_repository *g_repo = NULL;
/*
From the test repo used for this test:
--------------------------------------
This is a test repo for libgit2 where tree entries have type changes
The key types that could be found in tree entries are:
1 - GIT_FILEMODE_NEW = 0000000
2 - GIT_FILEMODE_TREE = 0040000
3 - GIT_FILEMODE_BLOB = 0100644
4 - GIT_FILEMODE_BLOB_EXECUTABLE = 0100755
5 - GIT_FILEMODE_LINK = 0120000
6 - GIT_FILEMODE_COMMIT = 0160000
I will try to have every type of transition somewhere in the history
of this repo.
Commits
-------
Initial commit - a(1) b(1) c(1) d(1) e(1)
Create content - a(1->2) b(1->3) c(1->4) d(1->5) e(1->6)
Changes #1 - a(2->3) b(3->4) c(4->5) d(5->6) e(6->2)
Changes #2 - a(3->5) b(4->6) c(5->2) d(6->3) e(2->4)
Changes #3 - a(5->3) b(6->4) c(2->5) d(3->6) e(4->2)
Changes #4 - a(3->2) b(4->3) c(5->4) d(6->5) e(2->6)
Changes #5 - a(2->1) b(3->1) c(4->1) d(5->1) e(6->1)
*/
static const char *g_typechange_oids[] = {
"79b9f23e85f55ea36a472a902e875bc1121a94cb",
"9bdb75b73836a99e3dbeea640a81de81031fdc29",
"0e7ed140b514b8cae23254cb8656fe1674403aff",
"9d0235c7a7edc0889a18f97a42ee6db9fe688447",
"9b19edf33a03a0c59cdfc113bfa5c06179bf9b1a",
"1b63caae4a5ca96f78e8dfefc376c6a39a142475",
"6eae26c90e8ccc4d16208972119c40635489c6f0",
NULL
};
static bool g_typechange_empty[] = {
true, false, false, false, false, false, true, true
};
static const int g_typechange_expected_conflicts[] = {
1, 2, 3, 3, 2, 3, 2
};
static const int g_typechange_expected_untracked[] = {
6, 4, 3, 2, 3, 2, 5
};
void test_checkout_typechange__initialize(void)
{
g_repo = cl_git_sandbox_init("typechanges");
cl_fixture_sandbox("submod2_target");
p_rename("submod2_target/.gitted", "submod2_target/.git");
}
void test_checkout_typechange__cleanup(void)
{
cl_git_sandbox_cleanup();
cl_fixture_cleanup("submod2_target");
}
static void assert_file_exists(const char *path)
{
cl_assert_(git_fs_path_isfile(path), path);
}
static void assert_dir_exists(const char *path)
{
cl_assert_(git_fs_path_isdir(path), path);
}
static void assert_workdir_matches_tree(
git_repository *repo, const git_oid *id, const char *root, bool recurse)
{
git_object *obj;
git_tree *tree;
size_t i, max_i;
git_str path = GIT_STR_INIT;
if (!root)
root = git_repository_workdir(repo);
cl_assert(root);
cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJECT_ANY));
cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJECT_TREE));
git_object_free(obj);
max_i = git_tree_entrycount(tree);
for (i = 0; i < max_i; ++i) {
const git_tree_entry *te = git_tree_entry_byindex(tree, i);
cl_assert(te);
cl_git_pass(git_str_joinpath(&path, root, git_tree_entry_name(te)));
switch (git_tree_entry_type(te)) {
case GIT_OBJECT_COMMIT:
assert_dir_exists(path.ptr);
break;
case GIT_OBJECT_TREE:
assert_dir_exists(path.ptr);
if (recurse)
assert_workdir_matches_tree(
repo, git_tree_entry_id(te), path.ptr, true);
break;
case GIT_OBJECT_BLOB:
switch (git_tree_entry_filemode(te)) {
case GIT_FILEMODE_BLOB:
case GIT_FILEMODE_BLOB_EXECUTABLE:
assert_file_exists(path.ptr);
/* because of cross-platform, don't confirm exec bit yet */
break;
case GIT_FILEMODE_LINK:
cl_assert_(git_fs_path_exists(path.ptr), path.ptr);
/* because of cross-platform, don't confirm link yet */
break;
default:
cl_assert(false); /* really?! */
}
break;
default:
cl_assert(false); /* really?!! */
}
}
git_tree_free(tree);
git_str_dispose(&path);
}
void test_checkout_typechange__checkout_typechanges_safe(void)
{
int i;
git_object *obj;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
for (i = 0; g_typechange_oids[i] != NULL; ++i) {
cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
opts.checkout_strategy = !i ? GIT_CHECKOUT_FORCE : GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(obj)));
assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
git_object_free(obj);
if (!g_typechange_empty[i]) {
cl_assert(git_fs_path_isdir("typechanges"));
cl_assert(git_fs_path_exists("typechanges/a"));
cl_assert(git_fs_path_exists("typechanges/b"));
cl_assert(git_fs_path_exists("typechanges/c"));
cl_assert(git_fs_path_exists("typechanges/d"));
cl_assert(git_fs_path_exists("typechanges/e"));
} else {
cl_assert(git_fs_path_isdir("typechanges"));
cl_assert(!git_fs_path_exists("typechanges/a"));
cl_assert(!git_fs_path_exists("typechanges/b"));
cl_assert(!git_fs_path_exists("typechanges/c"));
cl_assert(!git_fs_path_exists("typechanges/d"));
cl_assert(!git_fs_path_exists("typechanges/e"));
}
}
}
typedef struct {
int conflicts;
int dirty;
int updates;
int untracked;
int ignored;
} notify_counts;
static int notify_counter(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
notify_counts *cts = payload;
GIT_UNUSED(path);
GIT_UNUSED(baseline);
GIT_UNUSED(target);
GIT_UNUSED(workdir);
switch (why) {
case GIT_CHECKOUT_NOTIFY_CONFLICT: cts->conflicts++; break;
case GIT_CHECKOUT_NOTIFY_DIRTY: cts->dirty++; break;
case GIT_CHECKOUT_NOTIFY_UPDATED: cts->updates++; break;
case GIT_CHECKOUT_NOTIFY_UNTRACKED: cts->untracked++; break;
case GIT_CHECKOUT_NOTIFY_IGNORED: cts->ignored++; break;
default: break;
}
return 0;
}
static void force_create_file(const char *file)
{
int error = git_futils_rmdir_r(file, NULL,
GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
cl_assert(!error || error == GIT_ENOTFOUND);
cl_git_pass(git_futils_mkpath2file(file, 0777));
cl_git_rewritefile(file, "yowza!!");
}
static int make_submodule_dirty(git_submodule *sm, const char *name, void *payload)
{
git_str submodulepath = GIT_STR_INIT;
git_str dirtypath = GIT_STR_INIT;
git_repository *submodule_repo;
GIT_UNUSED(name);
GIT_UNUSED(payload);
/* remove submodule directory in preparation for init and repo_init */
cl_git_pass(git_str_joinpath(
&submodulepath,
git_repository_workdir(g_repo),
git_submodule_path(sm)
));
git_futils_rmdir_r(git_str_cstr(&submodulepath), NULL, GIT_RMDIR_REMOVE_FILES);
/* initialize submodule's repository */
cl_git_pass(git_submodule_repo_init(&submodule_repo, sm, 0));
/* create a file in the submodule workdir to make it dirty */
cl_git_pass(
git_str_joinpath(&dirtypath, git_repository_workdir(submodule_repo), "dirty"));
force_create_file(git_str_cstr(&dirtypath));
git_str_dispose(&dirtypath);
git_str_dispose(&submodulepath);
git_repository_free(submodule_repo);
return 0;
}
void test_checkout_typechange__checkout_with_conflicts(void)
{
int i;
git_object *obj;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
notify_counts cts = {0};
opts.notify_flags =
GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_UNTRACKED;
opts.notify_cb = notify_counter;
opts.notify_payload = &cts;
for (i = 0; g_typechange_oids[i] != NULL; ++i) {
cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
force_create_file("typechanges/a/blocker");
force_create_file("typechanges/b");
force_create_file("typechanges/c/sub/sub/file");
git_futils_rmdir_r("typechanges/d", NULL, GIT_RMDIR_REMOVE_FILES);
p_mkdir("typechanges/d", 0777); /* intentionally empty dir */
force_create_file("typechanges/untracked");
cl_git_pass(git_submodule_foreach(g_repo, make_submodule_dirty, NULL));
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
memset(&cts, 0, sizeof(cts));
cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
cl_assert_equal_i(cts.conflicts, g_typechange_expected_conflicts[i]);
cl_assert_equal_i(cts.untracked, g_typechange_expected_untracked[i]);
cl_assert_equal_i(cts.dirty, 0);
cl_assert_equal_i(cts.updates, 0);
cl_assert_equal_i(cts.ignored, 0);
opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
memset(&cts, 0, sizeof(cts));
cl_assert(git_fs_path_exists("typechanges/untracked"));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_assert_equal_i(0, cts.conflicts);
cl_assert(!git_fs_path_exists("typechanges/untracked"));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(obj)));
assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
git_object_free(obj);
}
}
void test_checkout_typechange__status_char(void)
{
size_t i;
git_oid oid;
git_commit *commit;
git_diff *diff;
const git_diff_delta *delta;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
char expected[8] = {'M', 'M', 'R', 'T', 'D', 'R', 'A', 'R'};
git_oid__fromstr(&oid, "9b19edf33a03a0c59cdfc113bfa5c06179bf9b1a", GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
diffopts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
cl_git_pass(git_diff__commit(&diff, g_repo, commit, &diffopts));
cl_git_pass(git_diff_find_similar(diff, NULL));
for (i = 0; i < git_diff_num_deltas(diff); i++) {
delta = git_diff_get_delta(diff, i);
cl_assert_equal_i(expected[i], git_diff_status_char(delta->status));
}
git_diff_free(diff);
git_commit_free(commit);
}
| libgit2-main | tests/libgit2/checkout/typechange.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
#include "path.h"
#include "futils.h"
#include "odb.h"
static git_repository *g_repo;
void test_checkout_binaryunicode__initialize(void)
{
g_repo = cl_git_sandbox_init("binaryunicode");
}
void test_checkout_binaryunicode__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void execute_test(void)
{
git_oid oid, check;
git_commit *commit;
git_tree *tree;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/branch1"));
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
cl_git_pass(git_commit_tree(&tree, commit));
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, (git_object *)tree, &opts));
git_tree_free(tree);
git_commit_free(commit);
/* Verify that the lenna.jpg file was checked out correctly */
cl_git_pass(git_oid__fromstr(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1", GIT_OID_SHA1));
cl_git_pass(git_odb__hashfile(&oid, "binaryunicode/lenna.jpg", GIT_OBJECT_BLOB, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &check);
/* Verify that the text file was checked out correctly */
cl_git_pass(git_oid__fromstr(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a", GIT_OID_SHA1));
cl_git_pass(git_odb__hashfile(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", GIT_OBJECT_BLOB, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &check);
}
void test_checkout_binaryunicode__noautocrlf(void)
{
cl_repo_set_bool(g_repo, "core.autocrlf", false);
execute_test();
}
void test_checkout_binaryunicode__autocrlf(void)
{
cl_repo_set_bool(g_repo, "core.autocrlf", true);
execute_test();
}
| libgit2-main | tests/libgit2/checkout/binaryunicode.c |
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "repository.h"
#include "futils.h"
static const char *repo_name = "nasty";
static git_repository *repo;
static git_checkout_options checkout_opts;
void test_checkout_nasty__initialize(void)
{
repo = cl_git_sandbox_init(repo_name);
GIT_INIT_STRUCTURE(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
}
void test_checkout_nasty__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void test_checkout_passes(const char *refname, const char *filename)
{
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_str path = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&path, repo_name, filename));
cl_git_pass(git_reference_name_to_id(&commit_id, repo, refname));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE |
GIT_CHECKOUT_DONT_UPDATE_INDEX;
cl_git_pass(git_checkout_tree(repo, (const git_object *)commit, &opts));
cl_assert(!git_fs_path_exists(path.ptr));
git_commit_free(commit);
git_str_dispose(&path);
}
static void test_checkout_fails(const char *refname, const char *filename)
{
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_str path = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&path, repo_name, filename));
cl_git_pass(git_reference_name_to_id(&commit_id, repo, refname));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_fail(git_checkout_tree(repo, (const git_object *)commit, &opts));
cl_assert(!git_fs_path_exists(path.ptr));
git_commit_free(commit);
git_str_dispose(&path);
}
/* A tree that contains ".git" as a tree, with a blob inside
* (".git/foobar").
*/
void test_checkout_nasty__dotgit_tree(void)
{
test_checkout_fails("refs/heads/dotgit_tree", ".git/foobar");
}
/* A tree that contains ".GIT" as a tree, with a blob inside
* (".GIT/foobar").
*/
void test_checkout_nasty__dotcapitalgit_tree(void)
{
test_checkout_fails("refs/heads/dotcapitalgit_tree", ".GIT/foobar");
}
/* A tree that contains a tree ".", with a blob inside ("./foobar").
*/
void test_checkout_nasty__dot_tree(void)
{
test_checkout_fails("refs/heads/dot_tree", "foobar");
}
/* A tree that contains a tree ".", with a tree ".git", with a blob
* inside ("./.git/foobar").
*/
void test_checkout_nasty__dot_dotgit_tree(void)
{
test_checkout_fails("refs/heads/dot_dotgit_tree", ".git/foobar");
}
/* A tree that contains a tree, with a tree "..", with a tree ".git", with a
* blob inside ("foo/../.git/foobar").
*/
void test_checkout_nasty__dotdot_dotgit_tree(void)
{
test_checkout_fails("refs/heads/dotdot_dotgit_tree", ".git/foobar");
}
/* A tree that contains a tree, with a tree "..", with a blob inside
* ("foo/../foobar").
*/
void test_checkout_nasty__dotdot_tree(void)
{
test_checkout_fails("refs/heads/dotdot_tree", "foobar");
}
/* A tree that contains a blob with the rogue name ".git/foobar" */
void test_checkout_nasty__dotgit_path(void)
{
test_checkout_fails("refs/heads/dotgit_path", ".git/foobar");
}
/* A tree that contains a blob with the rogue name ".GIT/foobar" */
void test_checkout_nasty__dotcapitalgit_path(void)
{
test_checkout_fails("refs/heads/dotcapitalgit_path", ".GIT/foobar");
}
/* A tree that contains a blob with the rogue name "./.git/foobar" */
void test_checkout_nasty__dot_dotgit_path(void)
{
test_checkout_fails("refs/heads/dot_dotgit_path", ".git/foobar");
}
/* A tree that contains a blob with the rogue name "./.GIT/foobar" */
void test_checkout_nasty__dot_dotcapitalgit_path(void)
{
test_checkout_fails("refs/heads/dot_dotcapitalgit_path", ".GIT/foobar");
}
/* A tree that contains a blob with the rogue name "foo/../.git/foobar" */
void test_checkout_nasty__dotdot_dotgit_path(void)
{
test_checkout_fails("refs/heads/dotdot_dotgit_path", ".git/foobar");
}
/* A tree that contains a blob with the rogue name "foo/../.GIT/foobar" */
void test_checkout_nasty__dotdot_dotcapitalgit_path(void)
{
test_checkout_fails("refs/heads/dotdot_dotcapitalgit_path", ".GIT/foobar");
}
/* A tree that contains a blob with the rogue name "foo/." */
void test_checkout_nasty__dot_path(void)
{
test_checkout_fails("refs/heads/dot_path", "./foobar");
}
/* A tree that contains a blob with the rogue name "foo/." */
void test_checkout_nasty__dot_path_two(void)
{
test_checkout_fails("refs/heads/dot_path_two", "foo/.");
}
/* A tree that contains a blob with the rogue name "foo/../foobar" */
void test_checkout_nasty__dotdot_path(void)
{
test_checkout_fails("refs/heads/dotdot_path", "foobar");
}
/* A tree that contains an entry with a backslash ".git\foobar" */
void test_checkout_nasty__dotgit_backslash_path(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
#endif
}
/* A tree that contains an entry with a backslash ".GIT\foobar" */
void test_checkout_nasty__dotcapitalgit_backslash_path(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
#endif
}
/* A tree that contains an entry with a backslash ".\.GIT\foobar" */
void test_checkout_nasty__dot_backslash_dotcapitalgit_path(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_backslash_dotcapitalgit_path", ".GIT/foobar");
#endif
}
/* A tree that contains an entry ".git.", because Win32 APIs will drop the
* trailing slash.
*/
void test_checkout_nasty__dot_git_dot(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
#endif
}
/* A tree that contains an entry "git~1", because that is typically the
* short name for ".git".
*/
void test_checkout_nasty__git_tilde1(void)
{
test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
test_checkout_fails("refs/heads/git_tilde1", "git~1/foobar");
}
/* A tree that contains an entry "git~2", when we have forced the short
* name for ".git" into "GIT~2".
*/
void test_checkout_nasty__git_custom_shortname(void)
{
#ifdef GIT_WIN32
if (!cl_sandbox_supports_8dot3())
clar__skip();
cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
test_checkout_fails("refs/heads/git_tilde2", ".git/foobar");
#endif
}
/* A tree that contains an entry "git~3", which should be allowed, since
* it is not the typical short name ("GIT~1") or the actual short name
* ("GIT~2") for ".git".
*/
void test_checkout_nasty__only_looks_like_a_git_shortname(void)
{
#ifdef GIT_WIN32
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
cl_git_pass(git_reference_name_to_id(&commit_id, repo, "refs/heads/git_tilde3"));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(repo, (const git_object *)commit, &opts));
cl_assert(git_fs_path_exists("nasty/git~3/foobar"));
git_commit_free(commit);
#endif
}
/* A tree that contains an entry "git:", because Win32 APIs will reject
* that as looking too similar to a drive letter.
*/
void test_checkout_nasty__dot_git_colon(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_git_colon", ".git/foobar");
#endif
}
/* A tree that contains an entry "git:foo", because Win32 APIs will turn
* that into ".git".
*/
void test_checkout_nasty__dot_git_colon_stuff(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_git_colon_stuff", ".git/foobar");
#endif
}
/* A tree that contains an entry ".git::$INDEX_ALLOCATION" because NTFS
* will interpret that as a synonym to ".git", even when mounted via SMB
* on macOS.
*/
void test_checkout_nasty__dotgit_alternate_data_stream(void)
{
test_checkout_fails("refs/heads/dotgit_alternate_data_stream", ".git/dummy-file");
test_checkout_fails("refs/heads/dotgit_alternate_data_stream", ".git::$INDEX_ALLOCATION/dummy-file");
}
/* Trees that contains entries with a tree ".git" that contain
* byte sequences:
* { 0xe2, 0x80, 0x8c }
* { 0xe2, 0x80, 0x8d }
* { 0xe2, 0x80, 0x8e }
* { 0xe2, 0x80, 0x8f }
* { 0xe2, 0x80, 0xaa }
* { 0xe2, 0x80, 0xab }
* { 0xe2, 0x80, 0xac }
* { 0xe2, 0x80, 0xad }
* { 0xe2, 0x81, 0xae }
* { 0xe2, 0x81, 0xaa }
* { 0xe2, 0x81, 0xab }
* { 0xe2, 0x81, 0xac }
* { 0xe2, 0x81, 0xad }
* { 0xe2, 0x81, 0xae }
* { 0xe2, 0x81, 0xaf }
* { 0xef, 0xbb, 0xbf }
* Because these map to characters that HFS filesystems "ignore". Thus
* ".git<U+200C>" will map to ".git".
*/
void test_checkout_nasty__dot_git_hfs_ignorable(void)
{
#ifdef __APPLE__
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
#endif
}
void test_checkout_nasty__honors_core_protecthfs(void)
{
cl_repo_set_bool(repo, "core.protectHFS", true);
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
}
void test_checkout_nasty__honors_core_protectntfs(void)
{
cl_repo_set_bool(repo, "core.protectNTFS", true);
test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
}
void test_checkout_nasty__symlink1(void)
{
test_checkout_passes("refs/heads/symlink1", ".git/foobar");
}
void test_checkout_nasty__symlink2(void)
{
test_checkout_passes("refs/heads/symlink2", ".git/foobar");
}
void test_checkout_nasty__symlink3(void)
{
test_checkout_passes("refs/heads/symlink3", ".git/foobar");
}
void test_checkout_nasty__gitmodules_symlink(void)
{
cl_repo_set_bool(repo, "core.protectHFS", true);
test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
cl_repo_set_bool(repo, "core.protectHFS", false);
cl_repo_set_bool(repo, "core.protectNTFS", true);
test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
cl_repo_set_bool(repo, "core.protectNTFS", false);
test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
}
| libgit2-main | tests/libgit2/checkout/nasty.c |
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/sys/index.h"
#include "futils.h"
#include "repository.h"
static git_repository *g_repo;
static git_index *g_index;
#define TEST_REPO_PATH "merge-resolve"
#define CONFLICTING_ANCESTOR_OID "d427e0b2e138501a3d15cc376077a3631e15bd46"
#define CONFLICTING_OURS_OID "4e886e602529caa9ab11d71f86634bd1b6e0de10"
#define CONFLICTING_THEIRS_OID "2bd0a343aeef7a2cf0d158478966a6e587ff3863"
#define AUTOMERGEABLE_ANCESTOR_OID "6212c31dab5e482247d7977e4f0dd3601decf13b"
#define AUTOMERGEABLE_OURS_OID "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"
#define AUTOMERGEABLE_THEIRS_OID "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe"
#define LINK_ANCESTOR_OID "1a010b1c0f081b2e8901d55307a15c29ff30af0e"
#define LINK_OURS_OID "72ea499e108df5ff0a4a913e7655bbeeb1fb69f2"
#define LINK_THEIRS_OID "8bfb012a6d809e499bd8d3e194a3929bc8995b93"
#define LINK_ANCESTOR_TARGET "file"
#define LINK_OURS_TARGET "other-file"
#define LINK_THEIRS_TARGET "still-another-file"
#define CONFLICTING_OURS_FILE \
"this file is changed in master and branch\n"
#define CONFLICTING_THEIRS_FILE \
"this file is changed in branch and master\n"
#define CONFLICTING_DIFF3_FILE \
"<<<<<<< ours\n" \
"this file is changed in master and branch\n" \
"=======\n" \
"this file is changed in branch and master\n" \
">>>>>>> theirs\n"
#define AUTOMERGEABLE_MERGED_FILE \
"this file is changed in master\n" \
"this file is automergeable\n" \
"this file is automergeable\n" \
"this file is automergeable\n" \
"this file is automergeable\n" \
"this file is automergeable\n" \
"this file is automergeable\n" \
"this file is automergeable\n" \
"this file is changed in branch\n"
struct checkout_index_entry {
uint16_t mode;
char oid_str[GIT_OID_SHA1_HEXSIZE+1];
int stage;
char path[128];
};
struct checkout_name_entry {
char ancestor[64];
char ours[64];
char theirs[64];
};
void test_checkout_conflict__initialize(void)
{
git_config *cfg;
g_repo = cl_git_sandbox_init(TEST_REPO_PATH);
git_repository_index(&g_index, g_repo);
cl_git_rewritefile(
TEST_REPO_PATH "/.gitattributes",
"* text eol=lf\n");
/* Ensure that the user's merge.conflictstyle doesn't interfere */
cl_git_pass(git_repository_config(&cfg, g_repo));
cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge"));
git_config_free(cfg);
}
void test_checkout_conflict__cleanup(void)
{
git_index_free(g_index);
cl_git_sandbox_cleanup();
}
static void create_index(struct checkout_index_entry *entries, size_t entries_len)
{
git_str path = GIT_STR_INIT;
size_t i;
for (i = 0; i < entries_len; i++) {
git_str_joinpath(&path, TEST_REPO_PATH, entries[i].path);
if (entries[i].stage == 3 && (i == 0 || strcmp(entries[i-1].path, entries[i].path) != 0 || entries[i-1].stage != 2))
p_unlink(git_str_cstr(&path));
cl_git_pass(git_index_remove_bypath(g_index, entries[i].path));
}
for (i = 0; i < entries_len; i++) {
git_index_entry entry;
memset(&entry, 0x0, sizeof(git_index_entry));
entry.mode = entries[i].mode;
GIT_INDEX_ENTRY_STAGE_SET(&entry, entries[i].stage);
git_oid__fromstr(&entry.id, entries[i].oid_str, GIT_OID_SHA1);
entry.path = entries[i].path;
cl_git_pass(git_index_add(g_index, &entry));
}
git_str_dispose(&path);
}
static void create_index_names(struct checkout_name_entry *entries, size_t entries_len)
{
size_t i;
for (i = 0; i < entries_len; i++) {
cl_git_pass(git_index_name_add(g_index,
strlen(entries[i].ancestor) == 0 ? NULL : entries[i].ancestor,
strlen(entries[i].ours) == 0 ? NULL : entries[i].ours,
strlen(entries[i].theirs) == 0 ? NULL : entries[i].theirs));
}
}
static void create_conflicting_index(void)
{
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "conflicting.txt" },
{ 0100644, CONFLICTING_OURS_OID, 2, "conflicting.txt" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "conflicting.txt" },
};
create_index(checkout_index_entries, 3);
cl_git_pass(git_index_write(g_index));
}
static void ensure_workdir_contents(const char *path, const char *contents)
{
git_str fullpath = GIT_STR_INIT, data_buf = GIT_STR_INIT;
cl_git_pass(
git_str_joinpath(&fullpath, git_repository_workdir(g_repo), path));
cl_git_pass(git_futils_readbuffer(&data_buf, git_str_cstr(&fullpath)));
cl_assert(strcmp(git_str_cstr(&data_buf), contents) == 0);
git_str_dispose(&fullpath);
git_str_dispose(&data_buf);
}
static void ensure_workdir_oid(const char *path, const char *oid_str)
{
git_oid expected, actual;
cl_git_pass(git_oid__fromstr(&expected, oid_str, GIT_OID_SHA1));
cl_git_pass(git_repository_hashfile(&actual, g_repo, path, GIT_OBJECT_BLOB, NULL));
cl_assert_equal_oid(&expected, &actual);
}
static void ensure_workdir_mode(const char *path, int mode)
{
#ifdef GIT_WIN32
GIT_UNUSED(path);
GIT_UNUSED(mode);
#else
git_str fullpath = GIT_STR_INIT;
struct stat st;
cl_git_pass(
git_str_joinpath(&fullpath, git_repository_workdir(g_repo), path));
cl_git_pass(p_stat(git_str_cstr(&fullpath), &st));
cl_assert_equal_i((mode & S_IRWXU), (st.st_mode & S_IRWXU));
git_str_dispose(&fullpath);
#endif
}
static void ensure_workdir(const char *path, int mode, const char *oid_str)
{
ensure_workdir_mode(path, mode);
ensure_workdir_oid(path, oid_str);
}
static void ensure_workdir_link(
git_repository *repo,
const char *path,
const char *target)
{
int symlinks;
cl_git_pass(git_repository__configmap_lookup(&symlinks, repo, GIT_CONFIGMAP_SYMLINKS));
if (!symlinks) {
ensure_workdir_contents(path, target);
} else {
git_str fullpath = GIT_STR_INIT;
char actual[1024];
struct stat st;
int len;
cl_git_pass(
git_str_joinpath(&fullpath, git_repository_workdir(g_repo), path));
cl_git_pass(p_lstat(git_str_cstr(&fullpath), &st));
cl_assert(S_ISLNK(st.st_mode));
cl_assert((len = p_readlink(git_str_cstr(&fullpath), actual, 1024)) > 0);
actual[len] = '\0';
cl_assert(strcmp(actual, target) == 0);
git_str_dispose(&fullpath);
}
}
void test_checkout_conflict__ignored(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy |= GIT_CHECKOUT_SKIP_UNMERGED;
create_conflicting_index();
cl_git_pass(p_unlink(TEST_REPO_PATH "/conflicting.txt"));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
cl_assert(!git_fs_path_exists(TEST_REPO_PATH "/conflicting.txt"));
}
void test_checkout_conflict__ours(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy |= GIT_CHECKOUT_USE_OURS;
create_conflicting_index();
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_contents("conflicting.txt", CONFLICTING_OURS_FILE);
}
void test_checkout_conflict__theirs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy |= GIT_CHECKOUT_USE_THEIRS;
create_conflicting_index();
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_contents("conflicting.txt", CONFLICTING_THEIRS_FILE);
}
void test_checkout_conflict__diff3(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
create_conflicting_index();
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_contents("conflicting.txt", CONFLICTING_DIFF3_FILE);
}
void test_checkout_conflict__automerge(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, AUTOMERGEABLE_ANCESTOR_OID, 1, "automergeable.txt" },
{ 0100644, AUTOMERGEABLE_OURS_OID, 2, "automergeable.txt" },
{ 0100644, AUTOMERGEABLE_THEIRS_OID, 3, "automergeable.txt" },
};
create_index(checkout_index_entries, 3);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_contents("automergeable.txt", AUTOMERGEABLE_MERGED_FILE);
}
void test_checkout_conflict__directory_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-1" },
{ 0100644, CONFLICTING_OURS_OID, 2, "df-1" },
{ 0100644, CONFLICTING_THEIRS_OID, 0, "df-1/file" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-2" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "df-2" },
{ 0100644, CONFLICTING_OURS_OID, 0, "df-2/file" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "df-3" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-3/file" },
{ 0100644, CONFLICTING_OURS_OID, 2, "df-3/file" },
{ 0100644, CONFLICTING_OURS_OID, 2, "df-4" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-4/file" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "df-4/file" },
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 12);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_oid("df-1/file", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("df-1~ours", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-2/file", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-2~theirs", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("df-3/file", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-3~theirs", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("df-4~ours", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-4/file", CONFLICTING_THEIRS_OID);
}
void test_checkout_conflict__directory_file_with_custom_labels(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-1" },
{ 0100644, CONFLICTING_OURS_OID, 2, "df-1" },
{ 0100644, CONFLICTING_THEIRS_OID, 0, "df-1/file" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-2" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "df-2" },
{ 0100644, CONFLICTING_OURS_OID, 0, "df-2/file" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "df-3" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-3/file" },
{ 0100644, CONFLICTING_OURS_OID, 2, "df-3/file" },
{ 0100644, CONFLICTING_OURS_OID, 2, "df-4" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "df-4/file" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "df-4/file" },
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
opts.our_label = "HEAD";
opts.their_label = "branch";
create_index(checkout_index_entries, 12);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_oid("df-1/file", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("df-1~HEAD", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-2/file", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-2~branch", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("df-3/file", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-3~branch", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("df-4~HEAD", CONFLICTING_OURS_OID);
ensure_workdir_oid("df-4/file", CONFLICTING_THEIRS_OID);
}
void test_checkout_conflict__link_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "link-1" },
{ 0100644, CONFLICTING_OURS_OID, 2, "link-1" },
{ 0120000, LINK_THEIRS_OID, 3, "link-1" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "link-2" },
{ 0120000, LINK_OURS_OID, 2, "link-2" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "link-2" },
{ 0120000, LINK_ANCESTOR_OID, 1, "link-3" },
{ 0100644, CONFLICTING_OURS_OID, 2, "link-3" },
{ 0120000, LINK_THEIRS_OID, 3, "link-3" },
{ 0120000, LINK_ANCESTOR_OID, 1, "link-4" },
{ 0120000, LINK_OURS_OID, 2, "link-4" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "link-4" },
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 12);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
/* Typechange conflicts always keep the file in the workdir */
ensure_workdir_oid("link-1", CONFLICTING_OURS_OID);
ensure_workdir_oid("link-2", CONFLICTING_THEIRS_OID);
ensure_workdir_oid("link-3", CONFLICTING_OURS_OID);
ensure_workdir_oid("link-4", CONFLICTING_THEIRS_OID);
}
void test_checkout_conflict__links(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0120000, LINK_ANCESTOR_OID, 1, "link-1" },
{ 0120000, LINK_OURS_OID, 2, "link-1" },
{ 0120000, LINK_THEIRS_OID, 3, "link-1" },
{ 0120000, LINK_OURS_OID, 2, "link-2" },
{ 0120000, LINK_THEIRS_OID, 3, "link-2" },
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 5);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
/* Conflicts with links always keep the ours side (even with -Xtheirs) */
ensure_workdir_link(g_repo, "link-1", LINK_OURS_TARGET);
ensure_workdir_link(g_repo, "link-2", LINK_OURS_TARGET);
}
void test_checkout_conflict__add_add(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_OURS_OID, 2, "conflicting.txt" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "conflicting.txt" },
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 2);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
/* Add/add writes diff3 files */
ensure_workdir_contents("conflicting.txt", CONFLICTING_DIFF3_FILE);
}
void test_checkout_conflict__mode_change(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "executable-1" },
{ 0100755, CONFLICTING_ANCESTOR_OID, 2, "executable-1" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "executable-1" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "executable-2" },
{ 0100644, CONFLICTING_OURS_OID, 2, "executable-2" },
{ 0100755, CONFLICTING_ANCESTOR_OID, 3, "executable-2" },
{ 0100755, CONFLICTING_ANCESTOR_OID, 1, "executable-3" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 2, "executable-3" },
{ 0100755, CONFLICTING_THEIRS_OID, 3, "executable-3" },
{ 0100755, CONFLICTING_ANCESTOR_OID, 1, "executable-4" },
{ 0100755, CONFLICTING_OURS_OID, 2, "executable-4" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 3, "executable-4" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "executable-5" },
{ 0100755, CONFLICTING_OURS_OID, 2, "executable-5" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "executable-5" },
{ 0100755, CONFLICTING_ANCESTOR_OID, 1, "executable-6" },
{ 0100644, CONFLICTING_OURS_OID, 2, "executable-6" },
{ 0100755, CONFLICTING_THEIRS_OID, 3, "executable-6" },
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 18);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
/* Keep the modified mode */
ensure_workdir_oid("executable-1", CONFLICTING_THEIRS_OID);
ensure_workdir_mode("executable-1", 0100755);
ensure_workdir_oid("executable-2", CONFLICTING_OURS_OID);
ensure_workdir_mode("executable-2", 0100755);
ensure_workdir_oid("executable-3", CONFLICTING_THEIRS_OID);
ensure_workdir_mode("executable-3", 0100644);
ensure_workdir_oid("executable-4", CONFLICTING_OURS_OID);
ensure_workdir_mode("executable-4", 0100644);
ensure_workdir_contents("executable-5", CONFLICTING_DIFF3_FILE);
ensure_workdir_mode("executable-5", 0100755);
ensure_workdir_contents("executable-6", CONFLICTING_DIFF3_FILE);
ensure_workdir_mode("executable-6", 0100644);
}
void test_checkout_conflict__renames(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" },
{ 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" },
{ 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 1, "0b-rewritten-in-ours.txt" },
{ 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 2, "0b-rewritten-in-ours.txt" },
{ 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 3, "0b-rewritten-in-ours.txt" },
{ 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" },
{ 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 1, "0c-rewritten-in-theirs.txt" },
{ 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 2, "0c-rewritten-in-theirs.txt" },
{ 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 3, "0c-rewritten-in-theirs.txt" },
{ 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" },
{ 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" },
{ 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" },
{ 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" },
{ 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" },
{ 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 2, "3a-newname-in-ours-deleted-in-theirs.txt" },
{ 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 1, "3a-renamed-in-ours-deleted-in-theirs.txt" },
{ 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 3, "3b-newname-in-theirs-deleted-in-ours.txt" },
{ 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 1, "3b-renamed-in-theirs-deleted-in-ours.txt" },
{ 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 2, "4a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 3, "4a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 1, "4a-renamed-in-ours-added-in-theirs.txt" },
{ 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 2, "4b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 3, "4b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 1, "4b-renamed-in-theirs-added-in-ours.txt" },
{ 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 2, "5a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 3, "5a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 1, "5a-renamed-in-ours-added-in-theirs.txt" },
{ 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 3, "5a-renamed-in-ours-added-in-theirs.txt" },
{ 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 2, "5b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 3, "5b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 1, "5b-renamed-in-theirs-added-in-ours.txt" },
{ 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 2, "5b-renamed-in-theirs-added-in-ours.txt" },
{ 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 2, "6-both-renamed-1-to-2-ours.txt" },
{ 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 3, "6-both-renamed-1-to-2-theirs.txt" },
{ 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 1, "6-both-renamed-1-to-2.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "7-both-renamed-side-1.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "7-both-renamed-side-1.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "7-both-renamed-side-2.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "7-both-renamed-side-2.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "7-both-renamed.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" }
};
struct checkout_name_entry checkout_name_entries[] = {
{
"3a-renamed-in-ours-deleted-in-theirs.txt",
"3a-newname-in-ours-deleted-in-theirs.txt",
""
},
{
"3b-renamed-in-theirs-deleted-in-ours.txt",
"",
"3b-newname-in-theirs-deleted-in-ours.txt"
},
{
"4a-renamed-in-ours-added-in-theirs.txt",
"4a-newname-in-ours-added-in-theirs.txt",
""
},
{
"4b-renamed-in-theirs-added-in-ours.txt",
"",
"4b-newname-in-theirs-added-in-ours.txt"
},
{
"5a-renamed-in-ours-added-in-theirs.txt",
"5a-newname-in-ours-added-in-theirs.txt",
"5a-renamed-in-ours-added-in-theirs.txt"
},
{
"5b-renamed-in-theirs-added-in-ours.txt",
"5b-renamed-in-theirs-added-in-ours.txt",
"5b-newname-in-theirs-added-in-ours.txt"
},
{
"6-both-renamed-1-to-2.txt",
"6-both-renamed-1-to-2-ours.txt",
"6-both-renamed-1-to-2-theirs.txt"
},
{
"7-both-renamed-side-1.txt",
"7-both-renamed.txt",
"7-both-renamed-side-1.txt"
},
{
"7-both-renamed-side-2.txt",
"7-both-renamed-side-2.txt",
"7-both-renamed.txt"
}
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 41);
create_index_names(checkout_name_entries, 9);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir("0a-no-change.txt",
0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e");
ensure_workdir("0b-duplicated-in-ours.txt",
0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6");
ensure_workdir("0b-rewritten-in-ours.txt",
0100644, "4c7e515d6d52d820496858f2f059ece69e99e2e3");
ensure_workdir("0c-duplicated-in-theirs.txt",
0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31");
ensure_workdir("0c-rewritten-in-theirs.txt",
0100644, "4648d658682d1155c2a3db5b0c53305e26884ea5");
ensure_workdir("1a-newname-in-ours-edited-in-theirs.txt",
0100644, "0d872f8e871a30208305978ecbf9e66d864f1638");
ensure_workdir("1a-newname-in-ours.txt",
0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb");
ensure_workdir("1b-newname-in-theirs-edited-in-ours.txt",
0100644, "ed9523e62e453e50dd9be1606af19399b96e397a");
ensure_workdir("1b-newname-in-theirs.txt",
0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136");
ensure_workdir("2-newname-in-both.txt",
0100644, "178940b450f238a56c0d75b7955cb57b38191982");
ensure_workdir("3a-newname-in-ours-deleted-in-theirs.txt",
0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9");
ensure_workdir("3b-newname-in-theirs-deleted-in-ours.txt",
0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495");
ensure_workdir("4a-newname-in-ours-added-in-theirs.txt~ours",
0100644, "227792b52aaa0b238bea00ec7e509b02623f168c");
ensure_workdir("4a-newname-in-ours-added-in-theirs.txt~theirs",
0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a");
ensure_workdir("4b-newname-in-theirs-added-in-ours.txt~ours",
0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9");
ensure_workdir("4b-newname-in-theirs-added-in-ours.txt~theirs",
0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db");
ensure_workdir("5a-newname-in-ours-added-in-theirs.txt~ours",
0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436");
ensure_workdir("5a-newname-in-ours-added-in-theirs.txt~theirs",
0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714");
ensure_workdir("5b-newname-in-theirs-added-in-ours.txt~ours",
0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced");
ensure_workdir("5b-newname-in-theirs-added-in-ours.txt~theirs",
0100644, "63247125386de9ec90a27ad36169307bf8a11a38");
ensure_workdir("6-both-renamed-1-to-2-ours.txt",
0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450");
ensure_workdir("6-both-renamed-1-to-2-theirs.txt",
0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450");
ensure_workdir("7-both-renamed.txt~ours",
0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11");
ensure_workdir("7-both-renamed.txt~theirs",
0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07");
}
void test_checkout_conflict__rename_keep_ours(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" },
{ 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" },
{ 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 1, "0b-rewritten-in-ours.txt" },
{ 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 2, "0b-rewritten-in-ours.txt" },
{ 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 3, "0b-rewritten-in-ours.txt" },
{ 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" },
{ 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 1, "0c-rewritten-in-theirs.txt" },
{ 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 2, "0c-rewritten-in-theirs.txt" },
{ 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 3, "0c-rewritten-in-theirs.txt" },
{ 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" },
{ 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" },
{ 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" },
{ 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" },
{ 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" },
{ 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 2, "3a-newname-in-ours-deleted-in-theirs.txt" },
{ 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 1, "3a-renamed-in-ours-deleted-in-theirs.txt" },
{ 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 3, "3b-newname-in-theirs-deleted-in-ours.txt" },
{ 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 1, "3b-renamed-in-theirs-deleted-in-ours.txt" },
{ 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 2, "4a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 3, "4a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 1, "4a-renamed-in-ours-added-in-theirs.txt" },
{ 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 2, "4b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 3, "4b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 1, "4b-renamed-in-theirs-added-in-ours.txt" },
{ 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 2, "5a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 3, "5a-newname-in-ours-added-in-theirs.txt" },
{ 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 1, "5a-renamed-in-ours-added-in-theirs.txt" },
{ 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 3, "5a-renamed-in-ours-added-in-theirs.txt" },
{ 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 2, "5b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 3, "5b-newname-in-theirs-added-in-ours.txt" },
{ 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 1, "5b-renamed-in-theirs-added-in-ours.txt" },
{ 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 2, "5b-renamed-in-theirs-added-in-ours.txt" },
{ 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 2, "6-both-renamed-1-to-2-ours.txt" },
{ 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 3, "6-both-renamed-1-to-2-theirs.txt" },
{ 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 1, "6-both-renamed-1-to-2.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "7-both-renamed-side-1.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "7-both-renamed-side-1.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "7-both-renamed-side-2.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "7-both-renamed-side-2.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "7-both-renamed.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" }
};
struct checkout_name_entry checkout_name_entries[] = {
{
"3a-renamed-in-ours-deleted-in-theirs.txt",
"3a-newname-in-ours-deleted-in-theirs.txt",
""
},
{
"3b-renamed-in-theirs-deleted-in-ours.txt",
"",
"3b-newname-in-theirs-deleted-in-ours.txt"
},
{
"4a-renamed-in-ours-added-in-theirs.txt",
"4a-newname-in-ours-added-in-theirs.txt",
""
},
{
"4b-renamed-in-theirs-added-in-ours.txt",
"",
"4b-newname-in-theirs-added-in-ours.txt"
},
{
"5a-renamed-in-ours-added-in-theirs.txt",
"5a-newname-in-ours-added-in-theirs.txt",
"5a-renamed-in-ours-added-in-theirs.txt"
},
{
"5b-renamed-in-theirs-added-in-ours.txt",
"5b-renamed-in-theirs-added-in-ours.txt",
"5b-newname-in-theirs-added-in-ours.txt"
},
{
"6-both-renamed-1-to-2.txt",
"6-both-renamed-1-to-2-ours.txt",
"6-both-renamed-1-to-2-theirs.txt"
},
{
"7-both-renamed-side-1.txt",
"7-both-renamed.txt",
"7-both-renamed-side-1.txt"
},
{
"7-both-renamed-side-2.txt",
"7-both-renamed-side-2.txt",
"7-both-renamed.txt"
}
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS;
create_index(checkout_index_entries, 41);
create_index_names(checkout_name_entries, 9);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir("0a-no-change.txt",
0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e");
ensure_workdir("0b-duplicated-in-ours.txt",
0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6");
ensure_workdir("0b-rewritten-in-ours.txt",
0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e");
ensure_workdir("0c-duplicated-in-theirs.txt",
0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31");
ensure_workdir("0c-rewritten-in-theirs.txt",
0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09");
ensure_workdir("1a-newname-in-ours-edited-in-theirs.txt",
0100644, "0d872f8e871a30208305978ecbf9e66d864f1638");
ensure_workdir("1a-newname-in-ours.txt",
0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb");
ensure_workdir("1b-newname-in-theirs-edited-in-ours.txt",
0100644, "ed9523e62e453e50dd9be1606af19399b96e397a");
ensure_workdir("1b-newname-in-theirs.txt",
0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136");
ensure_workdir("2-newname-in-both.txt",
0100644, "178940b450f238a56c0d75b7955cb57b38191982");
ensure_workdir("3a-newname-in-ours-deleted-in-theirs.txt",
0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9");
ensure_workdir("3b-newname-in-theirs-deleted-in-ours.txt",
0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495");
ensure_workdir("4a-newname-in-ours-added-in-theirs.txt",
0100644, "227792b52aaa0b238bea00ec7e509b02623f168c");
ensure_workdir("4b-newname-in-theirs-added-in-ours.txt",
0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9");
ensure_workdir("5a-newname-in-ours-added-in-theirs.txt",
0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436");
ensure_workdir("5b-newname-in-theirs-added-in-ours.txt",
0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced");
ensure_workdir("6-both-renamed-1-to-2-ours.txt",
0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450");
ensure_workdir("7-both-renamed.txt",
0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11");
}
void test_checkout_conflict__name_mangled_file_exists_in_workdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "test-one-side-one.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "test-one-side-one.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "test-one-side-two.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "test-one-side-two.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "test-one.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "test-one.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "test-two-side-one.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "test-two-side-one.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "test-two-side-two.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "test-two-side-two.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "test-two.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "test-two.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "test-three-side-one.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "test-three-side-one.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "test-three-side-two.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "test-three-side-two.txt" },
{ 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "test-three.txt" },
{ 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "test-three.txt" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "directory_file-one" },
{ 0100644, CONFLICTING_OURS_OID, 2, "directory_file-one" },
{ 0100644, CONFLICTING_THEIRS_OID, 0, "directory_file-one/file" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "directory_file-two" },
{ 0100644, CONFLICTING_OURS_OID, 0, "directory_file-two/file" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "directory_file-two" },
};
struct checkout_name_entry checkout_name_entries[] = {
{
"test-one-side-one.txt",
"test-one.txt",
"test-one-side-one.txt"
},
{
"test-one-side-two.txt",
"test-one-side-two.txt",
"test-one.txt"
},
{
"test-two-side-one.txt",
"test-two.txt",
"test-two-side-one.txt"
},
{
"test-two-side-two.txt",
"test-two-side-two.txt",
"test-two.txt"
},
{
"test-three-side-one.txt",
"test-three.txt",
"test-three-side-one.txt"
},
{
"test-three-side-two.txt",
"test-three-side-two.txt",
"test-three.txt"
}
};
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
create_index(checkout_index_entries, 24);
create_index_names(checkout_name_entries, 6);
cl_git_pass(git_index_write(g_index));
/* Add some files on disk that conflict with the names that would be chosen
* for the files written for each side. */
cl_git_rewritefile("merge-resolve/test-one.txt~ours",
"Expect index contents to be written to ~ours_0");
cl_git_rewritefile("merge-resolve/test-one.txt~theirs",
"Expect index contents to be written to ~theirs_0");
cl_git_rewritefile("merge-resolve/test-two.txt~ours",
"Expect index contents to be written to ~ours_3");
cl_git_rewritefile("merge-resolve/test-two.txt~theirs",
"Expect index contents to be written to ~theirs_3");
cl_git_rewritefile("merge-resolve/test-two.txt~ours_0",
"Expect index contents to be written to ~ours_3");
cl_git_rewritefile("merge-resolve/test-two.txt~theirs_0",
"Expect index contents to be written to ~theirs_3");
cl_git_rewritefile("merge-resolve/test-two.txt~ours_1",
"Expect index contents to be written to ~ours_3");
cl_git_rewritefile("merge-resolve/test-two.txt~theirs_1",
"Expect index contents to be written to ~theirs_3");
cl_git_rewritefile("merge-resolve/test-two.txt~ours_2",
"Expect index contents to be written to ~ours_3");
cl_git_rewritefile("merge-resolve/test-two.txt~theirs_2",
"Expect index contents to be written to ~theirs_3");
cl_git_rewritefile("merge-resolve/test-three.txt~Ours",
"Expect case insensitive filesystems to create ~ours_0");
cl_git_rewritefile("merge-resolve/test-three.txt~THEIRS",
"Expect case insensitive filesystems to create ~theirs_0");
cl_git_rewritefile("merge-resolve/directory_file-one~ours",
"Index contents written to ~ours_0 in this D/F conflict");
cl_git_rewritefile("merge-resolve/directory_file-two~theirs",
"Index contents written to ~theirs_0 in this D/F conflict");
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir("test-one.txt~ours_0",
0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11");
ensure_workdir("test-one.txt~theirs_0",
0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07");
ensure_workdir("test-two.txt~ours_3",
0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11");
ensure_workdir("test-two.txt~theirs_3",
0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07");
/* Name is mangled on case insensitive only */
#if defined(GIT_WIN32) || defined(__APPLE__)
ensure_workdir("test-three.txt~ours_0",
0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11");
ensure_workdir("test-three.txt~theirs_0",
0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07");
#else
ensure_workdir("test-three.txt~ours",
0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11");
ensure_workdir("test-three.txt~theirs",
0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07");
#endif
ensure_workdir("directory_file-one~ours_0", 0100644, CONFLICTING_OURS_OID);
ensure_workdir("directory_file-two~theirs_0", 0100644, CONFLICTING_THEIRS_OID);
}
void test_checkout_conflict__update_only(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, AUTOMERGEABLE_ANCESTOR_OID, 1, "automergeable.txt" },
{ 0100644, AUTOMERGEABLE_OURS_OID, 2, "automergeable.txt" },
{ 0100644, AUTOMERGEABLE_THEIRS_OID, 3, "automergeable.txt" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "modify-delete" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "modify-delete" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "directory_file-one" },
{ 0100644, CONFLICTING_OURS_OID, 2, "directory_file-one" },
{ 0100644, CONFLICTING_THEIRS_OID, 0, "directory_file-one/file" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "directory_file-two" },
{ 0100644, CONFLICTING_OURS_OID, 0, "directory_file-two/file" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "directory_file-two" },
};
opts.checkout_strategy |= GIT_CHECKOUT_UPDATE_ONLY;
create_index(checkout_index_entries, 3);
cl_git_pass(git_index_write(g_index));
cl_git_pass(p_mkdir("merge-resolve/directory_file-two", 0777));
cl_git_rewritefile("merge-resolve/directory_file-two/file", CONFLICTING_OURS_FILE);
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_contents("automergeable.txt", AUTOMERGEABLE_MERGED_FILE);
ensure_workdir("directory_file-two/file", 0100644, CONFLICTING_OURS_OID);
cl_assert(!git_fs_path_exists("merge-resolve/modify-delete"));
cl_assert(!git_fs_path_exists("merge-resolve/test-one.txt"));
cl_assert(!git_fs_path_exists("merge-resolve/test-one-side-one.txt"));
cl_assert(!git_fs_path_exists("merge-resolve/test-one-side-two.txt"));
cl_assert(!git_fs_path_exists("merge-resolve/test-one.txt~ours"));
cl_assert(!git_fs_path_exists("merge-resolve/test-one.txt~theirs"));
cl_assert(!git_fs_path_exists("merge-resolve/directory_file-one/file"));
cl_assert(!git_fs_path_exists("merge-resolve/directory_file-one~ours"));
cl_assert(!git_fs_path_exists("merge-resolve/directory_file-two~theirs"));
}
void test_checkout_conflict__path_filters(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
char *paths[] = { "conflicting-1.txt", "conflicting-3.txt" };
git_strarray patharray = {0};
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "conflicting-1.txt" },
{ 0100644, CONFLICTING_OURS_OID, 2, "conflicting-1.txt" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "conflicting-1.txt" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "conflicting-2.txt" },
{ 0100644, CONFLICTING_OURS_OID, 2, "conflicting-2.txt" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "conflicting-2.txt" },
{ 0100644, AUTOMERGEABLE_ANCESTOR_OID, 1, "conflicting-3.txt" },
{ 0100644, AUTOMERGEABLE_OURS_OID, 2, "conflicting-3.txt" },
{ 0100644, AUTOMERGEABLE_THEIRS_OID, 3, "conflicting-3.txt" },
{ 0100644, AUTOMERGEABLE_ANCESTOR_OID, 1, "conflicting-4.txt" },
{ 0100644, AUTOMERGEABLE_OURS_OID, 2, "conflicting-4.txt" },
{ 0100644, AUTOMERGEABLE_THEIRS_OID, 3, "conflicting-4.txt" },
};
patharray.count = 2;
patharray.strings = paths;
opts.paths = patharray;
create_index(checkout_index_entries, 12);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
ensure_workdir_contents("conflicting-1.txt", CONFLICTING_DIFF3_FILE);
cl_assert(!git_fs_path_exists("merge-resolve/conflicting-2.txt"));
ensure_workdir_contents("conflicting-3.txt", AUTOMERGEABLE_MERGED_FILE);
cl_assert(!git_fs_path_exists("merge-resolve/conflicting-4.txt"));
}
static void collect_progress(
const char *path,
size_t completed_steps,
size_t total_steps,
void *payload)
{
git_vector *paths = payload;
GIT_UNUSED(completed_steps);
GIT_UNUSED(total_steps);
if (path == NULL)
return;
git_vector_insert(paths, strdup(path));
}
void test_checkout_conflict__report_progress(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_vector paths = GIT_VECTOR_INIT;
char *path;
size_t i;
struct checkout_index_entry checkout_index_entries[] = {
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "conflicting-1.txt" },
{ 0100644, CONFLICTING_OURS_OID, 2, "conflicting-1.txt" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "conflicting-1.txt" },
{ 0100644, CONFLICTING_ANCESTOR_OID, 1, "conflicting-2.txt" },
{ 0100644, CONFLICTING_OURS_OID, 2, "conflicting-2.txt" },
{ 0100644, CONFLICTING_THEIRS_OID, 3, "conflicting-2.txt" },
{ 0100644, AUTOMERGEABLE_ANCESTOR_OID, 1, "conflicting-3.txt" },
{ 0100644, AUTOMERGEABLE_OURS_OID, 2, "conflicting-3.txt" },
{ 0100644, AUTOMERGEABLE_THEIRS_OID, 3, "conflicting-3.txt" },
{ 0100644, AUTOMERGEABLE_ANCESTOR_OID, 1, "conflicting-4.txt" },
{ 0100644, AUTOMERGEABLE_OURS_OID, 2, "conflicting-4.txt" },
{ 0100644, AUTOMERGEABLE_THEIRS_OID, 3, "conflicting-4.txt" },
};
opts.progress_cb = collect_progress;
opts.progress_payload = &paths;
create_index(checkout_index_entries, 12);
cl_git_pass(git_index_write(g_index));
cl_git_pass(git_checkout_index(g_repo, g_index, &opts));
cl_assert_equal_i(4, git_vector_length(&paths));
cl_assert_equal_s("conflicting-1.txt", git_vector_get(&paths, 0));
cl_assert_equal_s("conflicting-2.txt", git_vector_get(&paths, 1));
cl_assert_equal_s("conflicting-3.txt", git_vector_get(&paths, 2));
cl_assert_equal_s("conflicting-4.txt", git_vector_get(&paths, 3));
git_vector_foreach(&paths, i, path)
git__free(path);
git_vector_free(&paths);
}
| libgit2-main | tests/libgit2/checkout/conflict.c |
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "futils.h"
#include "repository.h"
#include "remote.h"
#include "repo/repo_helpers.h"
static git_repository *g_repo;
static git_str g_global_path = GIT_STR_INIT;
void test_checkout_index__initialize(void)
{
git_tree *tree;
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_head_tree(&tree, g_repo));
reset_index_to_treeish((git_object *)tree);
git_tree_free(tree);
cl_git_rewritefile(
"./testrepo/.gitattributes",
"* text eol=lf\n");
git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
&g_global_path);
}
void test_checkout_index__cleanup(void)
{
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
g_global_path.ptr);
git_str_dispose(&g_global_path);
cl_git_sandbox_cleanup();
/* try to remove directories created by tests */
cl_fixture_cleanup("alternative");
cl_fixture_cleanup("symlink");
cl_fixture_cleanup("symlink.git");
cl_fixture_cleanup("tmp_global_path");
}
void test_checkout_index__cannot_checkout_a_bare_repository(void)
{
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo.git");
cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
}
void test_checkout_index__can_create_missing_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/new.txt"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/README", "hey there\n");
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__can_remove_untracked_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_futils_mkdir("./testrepo/dir/subdir/subsubdir", 0755, GIT_MKDIR_PATH);
cl_git_mkfile("./testrepo/dir/one", "one\n");
cl_git_mkfile("./testrepo/dir/subdir/two", "two\n");
cl_assert_equal_i(true, git_fs_path_isdir("./testrepo/dir/subdir/subsubdir"));
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/dir"));
}
void test_checkout_index__can_disable_pathspec_match(void)
{
char *files_to_checkout[] = { "test10.txt", "test11.txt"};
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *objects;
git_index *index;
/* reset to beginning of history (i.e. just a README file) */
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_revparse_single(&objects, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_pass(git_checkout_tree(g_repo, objects, &opts));
cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(objects)));
git_object_free(objects);
cl_git_pass(git_repository_index(&index, g_repo));
/* We create 4 files and commit them */
cl_git_mkfile("testrepo/test9.txt", "original\n");
cl_git_mkfile("testrepo/test10.txt", "original\n");
cl_git_mkfile("testrepo/test11.txt", "original\n");
cl_git_mkfile("testrepo/test12.txt", "original\n");
cl_git_pass(git_index_add_bypath(index, "test9.txt"));
cl_git_pass(git_index_add_bypath(index, "test10.txt"));
cl_git_pass(git_index_add_bypath(index, "test11.txt"));
cl_git_pass(git_index_add_bypath(index, "test12.txt"));
cl_git_pass(git_index_write(index));
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "commit our test files");
/* We modify the content of all 4 of our files */
cl_git_rewritefile("testrepo/test9.txt", "modified\n");
cl_git_rewritefile("testrepo/test10.txt", "modified\n");
cl_git_rewritefile("testrepo/test11.txt", "modified\n");
cl_git_rewritefile("testrepo/test12.txt", "modified\n");
/* We checkout only test10.txt and test11.txt */
opts.checkout_strategy =
GIT_CHECKOUT_FORCE |
GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
opts.paths.strings = files_to_checkout;
opts.paths.count = ARRAY_SIZE(files_to_checkout);
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
/* The only files that have been reverted to their original content
should be test10.txt and test11.txt */
check_file_contents("testrepo/test9.txt", "modified\n");
check_file_contents("testrepo/test10.txt", "original\n");
check_file_contents("testrepo/test11.txt", "original\n");
check_file_contents("testrepo/test12.txt", "modified\n");
git_index_free(index);
}
void test_checkout_index__honor_the_specified_pathspecs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
char *entries[] = { "*.txt" };
opts.paths.strings = entries;
opts.paths.count = 1;
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/new.txt"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/README"));
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__honor_the_gitattributes_directives(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const char *attributes =
"branch_file.txt text eol=crlf\n"
"new.txt text eol=lf\n";
cl_git_mkfile("./testrepo/.gitattributes", attributes);
cl_repo_set_bool(g_repo, "core.autocrlf", false);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/README", "hey there\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
check_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
}
void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
{
#ifdef GIT_WIN32
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const char *expected_readme_text = "hey there\r\n";
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_repo_set_bool(g_repo, "core.autocrlf", true);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/README", expected_readme_text);
#endif
}
static void populate_symlink_workdir(void)
{
git_str path = GIT_STR_INIT;
git_repository *repo;
git_remote *origin;
git_object *target;
const char *url = git_repository_path(g_repo);
cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "symlink.git"));
cl_git_pass(git_repository_init(&repo, path.ptr, true));
cl_git_pass(git_repository_set_workdir(repo, "symlink", 1));
/* Delete the `origin` repo (if it exists) so we can recreate it. */
git_remote_delete(repo, GIT_REMOTE_ORIGIN);
cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
cl_git_pass(git_remote_fetch(origin, NULL, NULL, NULL));
git_remote_free(origin);
cl_git_pass(git_revparse_single(&target, repo, "remotes/origin/master"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
git_object_free(target);
git_repository_free(repo);
git_str_dispose(&path);
}
void test_checkout_index__honor_coresymlinks_default_true(void)
{
char link_data[GIT_PATH_MAX];
int link_size = GIT_PATH_MAX;
cl_must_pass(p_mkdir("symlink", 0777));
if (!git_fs_path_supports_symlinks("symlink/test"))
cl_skip();
#ifdef GIT_WIN32
/*
* Windows explicitly requires the global configuration to have
* core.symlinks=true in addition to actual filesystem support.
*/
create_tmp_global_config("tmp_global_path", "core.symlinks", "true");
#endif
populate_symlink_workdir();
link_size = p_readlink("./symlink/link_to_new.txt", link_data, link_size);
cl_assert(link_size >= 0);
link_data[link_size] = '\0';
cl_assert_equal_i(link_size, strlen("new.txt"));
cl_assert_equal_s(link_data, "new.txt");
check_file_contents("./symlink/link_to_new.txt", "my new file\n");
}
void test_checkout_index__honor_coresymlinks_default_false(void)
{
cl_must_pass(p_mkdir("symlink", 0777));
#ifndef GIT_WIN32
/*
* This test is largely for Windows platforms to ensure that
* we respect an unset core.symlinks even when the platform
* supports symlinks. Bail entirely on POSIX platforms that
* do support symlinks.
*/
if (git_fs_path_supports_symlinks("symlink/test"))
cl_skip();
#endif
populate_symlink_workdir();
check_file_contents("./symlink/link_to_new.txt", "new.txt");
}
void test_checkout_index__coresymlinks_set_to_true_fails_when_unsupported(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
if (git_fs_path_supports_symlinks("testrepo/test")) {
cl_skip();
}
cl_repo_set_bool(g_repo, "core.symlinks", true);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
}
void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
char link_data[GIT_PATH_MAX];
size_t link_size = GIT_PATH_MAX;
if (!git_fs_path_supports_symlinks("testrepo/test")) {
cl_skip();
}
cl_repo_set_bool(g_repo, "core.symlinks", true);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
link_data[link_size] = '\0';
cl_assert_equal_i(link_size, strlen("new.txt"));
cl_assert_equal_s(link_data, "new.txt");
check_file_contents("./testrepo/link_to_new.txt", "my new file\n");
}
void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_repo_set_bool(g_repo, "core.symlinks", false);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/link_to_new.txt", "new.txt");
}
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
/* set this up to not return an error code on conflicts, but it
* still will not have permission to overwrite anything...
*/
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "This isn't what's stored!");
}
void test_checkout_index__can_overwrite_modified_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__options_disable_filters(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.disable_filters = false;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "my new file\r\n");
p_unlink("./testrepo/new.txt");
opts.disable_filters = true;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__options_dir_modes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct stat st;
git_oid oid;
git_commit *commit;
mode_t um;
if (!cl_is_chmod_supported())
return;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
reset_index_to_treeish((git_object *)commit);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.dir_mode = 0701;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
/* umask will influence actual directory creation mode */
(void)p_umask(um = p_umask(022));
cl_git_pass(p_stat("./testrepo/a", &st));
/* Haiku & Hurd use other mode bits, so we must mask them out */
cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), (GIT_FILEMODE_TREE | 0701) & ~um, "%07o");
/* File-mode test, since we're on the 'dir' branch */
cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), GIT_FILEMODE_BLOB_EXECUTABLE & ~um, "%07o");
git_commit_free(commit);
}
void test_checkout_index__options_override_file_modes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct stat st;
if (!cl_is_chmod_supported())
return;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.file_mode = 0700;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(p_stat("./testrepo/new.txt", &st));
cl_assert_equal_i_fmt(st.st_mode & GIT_MODE_PERMS_MASK, 0700, "%07o");
}
void test_checkout_index__options_open_flags(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/new.txt", "hi\n");
opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_REMOVE_EXISTING;
opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
}
struct notify_data {
const char *file;
const char *sha;
};
static int test_checkout_notify_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
struct notify_data *expectations = (struct notify_data *)payload;
GIT_UNUSED(workdir);
cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why);
cl_assert_equal_s(expectations->file, path);
cl_assert_equal_i(0, git_oid_streq(&baseline->id, expectations->sha));
cl_assert_equal_i(0, git_oid_streq(&target->id, expectations->sha));
return 0;
}
void test_checkout_index__can_notify_of_skipped_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct notify_data data;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
/*
* $ git ls-tree HEAD
* 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6 README
* 100644 blob 3697d64be941a53d4ae8f6a271e4e3fa56b022cc branch_file.txt
* 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd new.txt
*/
data.file = "new.txt";
data.sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd";
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_ALLOW_CONFLICTS;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
opts.notify_cb = test_checkout_notify_cb;
opts.notify_payload = &data;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
}
static int dont_notify_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
GIT_UNUSED(why);
GIT_UNUSED(path);
GIT_UNUSED(baseline);
GIT_UNUSED(target);
GIT_UNUSED(workdir);
GIT_UNUSED(payload);
cl_assert(false);
return 0;
}
void test_checkout_index__wont_notify_of_expected_line_ending_changes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_mkfile("./testrepo/new.txt", "my new file\r\n");
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_ALLOW_CONFLICTS;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
opts.notify_cb = dont_notify_cb;
opts.notify_payload = NULL;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
}
static void checkout_progress_counter(
const char *path, size_t cur, size_t tot, void *payload)
{
GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
(*(int *)payload)++;
}
void test_checkout_index__calls_progress_callback(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
int calls = 0;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.progress_cb = checkout_progress_counter;
opts.progress_payload = &calls;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert(calls > 0);
}
void test_checkout_index__can_overcome_name_clashes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(git_repository_index(&index, g_repo));
git_index_clear(index);
cl_git_mkfile("./testrepo/path0", "content\r\n");
cl_git_pass(p_mkdir("./testrepo/path1", 0777));
cl_git_mkfile("./testrepo/path1/file1", "content\r\n");
cl_git_pass(git_index_add_bypath(index, "path0"));
cl_git_pass(git_index_add_bypath(index, "path1/file1"));
cl_git_pass(p_unlink("./testrepo/path0"));
cl_git_pass(git_futils_rmdir_r(
"./testrepo/path1", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_mkfile("./testrepo/path1", "content\r\n");
cl_git_pass(p_mkdir("./testrepo/path0", 0777));
cl_git_mkfile("./testrepo/path0/file0", "content\r\n");
cl_assert(git_fs_path_isfile("./testrepo/path1"));
cl_assert(git_fs_path_isfile("./testrepo/path0/file0"));
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_ALLOW_CONFLICTS;
cl_git_pass(git_checkout_index(g_repo, index, &opts));
cl_assert(git_fs_path_isfile("./testrepo/path1"));
cl_assert(git_fs_path_isfile("./testrepo/path0/file0"));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_index(g_repo, index, &opts));
cl_assert(git_fs_path_isfile("./testrepo/path0"));
cl_assert(git_fs_path_isfile("./testrepo/path1/file1"));
git_index_free(index);
}
void test_checkout_index__validates_struct_version(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const git_error *err;
opts.version = 1024;
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(err->klass, GIT_ERROR_INVALID);
opts.version = 0;
git_error_clear();
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(err->klass, GIT_ERROR_INVALID);
}
void test_checkout_index__can_update_prefixed_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/new.txt"));
cl_git_mkfile("./testrepo/READ", "content\n");
cl_git_mkfile("./testrepo/README.after", "content\n");
cl_git_pass(p_mkdir("./testrepo/branch_file", 0777));
cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777));
cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n");
cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777));
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
/* remove untracked will remove the .gitattributes file before the blobs
* were created, so they will have had crlf filtering applied on Windows
*/
check_file_contents_nocr("./testrepo/README", "hey there\n");
check_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents_nocr("./testrepo/new.txt", "my new file\n");
cl_assert(!git_fs_path_exists("testrepo/READ"));
cl_assert(!git_fs_path_exists("testrepo/README.after"));
cl_assert(!git_fs_path_exists("testrepo/branch_file"));
cl_assert(!git_fs_path_exists("testrepo/branch_file.txt.after"));
}
void test_checkout_index__can_checkout_a_newly_initialized_repository(void)
{
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
}
void test_checkout_index__issue_1397(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
}
void test_checkout_index__target_directory(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_counts cts;
memset(&cts, 0, sizeof(cts));
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING;
opts.target_directory = "alternative";
cl_assert(!git_fs_path_isdir("alternative"));
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &cts;
/* create some files that *would* conflict if we were using the wd */
cl_git_mkfile("testrepo/README", "I'm in the way!\n");
cl_git_mkfile("testrepo/new.txt", "my new file\n");
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(0, cts.n_untracked);
cl_assert_equal_i(0, cts.n_ignored);
cl_assert_equal_i(4, cts.n_updates);
check_file_contents("./alternative/README", "hey there\n");
check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
check_file_contents("./alternative/new.txt", "my new file\n");
cl_git_pass(git_futils_rmdir_r(
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
}
void test_checkout_index__target_directory_from_bare(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
git_object *head = NULL;
checkout_counts cts;
memset(&cts, 0, sizeof(cts));
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo.git");
cl_assert(git_repository_is_bare(g_repo));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_revparse_single(&head, g_repo, "HEAD^{tree}"));
cl_git_pass(git_index_read_tree(index, (const git_tree *)head));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &cts;
/* fail to checkout a bare repo */
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
opts.target_directory = "alternative";
cl_assert(!git_fs_path_isdir("alternative"));
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(0, cts.n_untracked);
cl_assert_equal_i(0, cts.n_ignored);
cl_assert_equal_i(3, cts.n_updates);
/* files will have been filtered if needed, so strip CR */
check_file_contents_nocr("./alternative/README", "hey there\n");
check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
check_file_contents_nocr("./alternative/new.txt", "my new file\n");
cl_git_pass(git_futils_rmdir_r(
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
git_object_free(head);
}
void test_checkout_index__can_get_repo_from_index(void)
{
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_fs_path_isfile("./testrepo/new.txt"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_checkout_index(NULL, index, &opts));
check_file_contents("./testrepo/README", "hey there\n");
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
git_index_free(index);
}
static void add_conflict(git_index *index, const char *path)
{
git_index_entry entry;
memset(&entry, 0, sizeof(git_index_entry));
entry.mode = 0100644;
entry.path = path;
git_oid__fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46", GIT_OID_SHA1);
GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(index, &entry));
git_oid__fromstr(&entry.id, "4e886e602529caa9ab11d71f86634bd1b6e0de10", GIT_OID_SHA1);
GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(index, &entry));
git_oid__fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
cl_git_pass(git_index_add(index, &entry));
}
void test_checkout_index__writes_conflict_file(void)
{
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_str conflicting_buf = GIT_STR_INIT;
cl_git_pass(git_repository_index(&index, g_repo));
add_conflict(index, "conflicting.txt");
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
cl_assert(strcmp(conflicting_buf.ptr,
"<<<<<<< ours\n"
"this file is changed in master and branch\n"
"=======\n"
"this file is changed in branch and master\n"
">>>>>>> theirs\n") == 0);
git_str_dispose(&conflicting_buf);
git_index_free(index);
}
void test_checkout_index__adding_conflict_removes_stage_0(void)
{
git_index *new_index, *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_pass(git_index_new(&new_index));
add_conflict(new_index, "new.txt");
cl_git_pass(git_checkout_index(g_repo, new_index, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert(git_index_get_bypath(index, "new.txt", 0) == NULL);
cl_assert(git_index_get_bypath(index, "new.txt", 1) != NULL);
cl_assert(git_index_get_bypath(index, "new.txt", 2) != NULL);
cl_assert(git_index_get_bypath(index, "new.txt", 3) != NULL);
git_index_free(index);
git_index_free(new_index);
}
void test_checkout_index__conflicts_honor_coreautocrlf(void)
{
#ifdef GIT_WIN32
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_str conflicting_buf = GIT_STR_INIT;
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_repository_index(&index, g_repo));
add_conflict(index, "conflicting.txt");
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
cl_assert(strcmp(conflicting_buf.ptr,
"<<<<<<< ours\r\n"
"this file is changed in master and branch\r\n"
"=======\r\n"
"this file is changed in branch and master\r\n"
">>>>>>> theirs\r\n") == 0);
git_str_dispose(&conflicting_buf);
git_index_free(index);
#endif
}
| libgit2-main | tests/libgit2/checkout/index.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
#include "path.h"
#include "futils.h"
static git_repository *g_repo;
void test_checkout_head__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_checkout_head__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_checkout_head__unborn_head_returns_GIT_EUNBORNBRANCH(void)
{
make_head_unborn(g_repo, NON_EXISTING_HEAD);
cl_assert_equal_i(GIT_EUNBORNBRANCH, git_checkout_head(g_repo, NULL));
}
void test_checkout_head__with_index_only_tree(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
/* let's start by getting things into a known state */
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
/* now let's stage some new stuff including a new directory */
cl_git_pass(git_repository_index(&index, g_repo));
p_mkdir("testrepo/newdir", 0777);
cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
cl_git_pass(git_index_add_bypath(index, "newdir/newfile.txt"));
cl_git_pass(git_index_write(index));
cl_assert(git_fs_path_isfile("testrepo/newdir/newfile.txt"));
cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) != NULL);
git_index_free(index);
/* okay, so now we have staged this new file; let's see if we can remove */
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert(!git_fs_path_isfile("testrepo/newdir/newfile.txt"));
cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) == NULL);
git_index_free(index);
}
void test_checkout_head__do_not_remove_untracked_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/untracked", "untracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isfile("testrepo/tracked/tracked"));
cl_assert(git_fs_path_isfile("testrepo/tracked/untracked"));
}
void test_checkout_head__do_not_remove_untracked_file_in_subdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
cl_git_pass(p_mkdir("testrepo/tracked/subdir", 0755));
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/subdir/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/subdir/untracked", "untracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
cl_git_pass(git_index_add_bypath(index, "tracked/subdir/tracked"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isfile("testrepo/tracked/tracked"));
cl_assert(!git_fs_path_isfile("testrepo/tracked/subdir/tracked"));
cl_assert(git_fs_path_isfile("testrepo/tracked/subdir/untracked"));
}
void test_checkout_head__do_remove_untracked_paths(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
char *paths[] = {"tracked/untracked"};
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
cl_git_pass(p_mkdir("testrepo/tracked/subdir", 0755));
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/untracked", "untracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
opts.paths.strings = paths;
opts.paths.count = 1;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(git_fs_path_isfile("testrepo/tracked/tracked"));
cl_assert(!git_fs_path_isfile("testrepo/tracked/untracked"));
}
void test_checkout_head__do_remove_tracked_subdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(p_mkdir("testrepo/subdir", 0755));
cl_git_pass(p_mkdir("testrepo/subdir/tracked", 0755));
cl_git_mkfile("testrepo/subdir/tracked-file", "tracked\n");
cl_git_mkfile("testrepo/subdir/untracked-file", "untracked\n");
cl_git_mkfile("testrepo/subdir/tracked/tracked1", "tracked\n");
cl_git_mkfile("testrepo/subdir/tracked/tracked2", "tracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "subdir/tracked-file"));
cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked1"));
cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked2"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isdir("testrepo/subdir/tracked"));
cl_assert(!git_fs_path_isfile("testrepo/subdir/tracked-file"));
cl_assert(git_fs_path_isfile("testrepo/subdir/untracked-file"));
}
void test_checkout_head__typechange_workdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target;
struct stat st;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&target, g_repo, "HEAD"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_must_pass(p_chmod("testrepo/new.txt", 0755));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(p_stat("testrepo/new.txt", &st));
cl_assert(!GIT_PERMS_IS_EXEC(st.st_mode));
git_object_free(target);
}
void test_checkout_head__typechange_index_and_workdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target;
git_index *index;
struct stat st;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&target, g_repo, "HEAD"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_must_pass(p_chmod("testrepo/new.txt", 0755));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "new.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(p_stat("testrepo/new.txt", &st));
cl_assert(!GIT_PERMS_IS_EXEC(st.st_mode));
git_object_free(target);
git_index_free(index);
}
void test_checkout_head__workdir_filemode_is_simplified(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target, *branch;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&target, g_repo, "a38d028f71eaa590febb7d716b1ca32350cf70da"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_must_pass(p_chmod("testrepo/branch_file.txt", 0666));
/*
* Checkout should not fail with a conflict; though the file mode
* on disk is literally different to the base (0666 vs 0644), Git
* ignores the actual mode and simply treats both as non-executable.
*/
cl_git_pass(git_revparse_single(&branch, g_repo, "099fabac3a9ea935598528c27f866e34089c2eff"));
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, branch, NULL));
git_object_free(branch);
git_object_free(target);
}
void test_checkout_head__obeys_filemode_true(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target, *branch;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
/* In this commit, `README` is executable */
cl_git_pass(git_revparse_single(&target, g_repo, "f9ed4af42472941da45a3c"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_repo_set_bool(g_repo, "core.filemode", true);
cl_must_pass(p_chmod("testrepo/README", 0644));
/*
* Checkout will fail with a conflict; the file mode is updated in
* the checkout target, but the contents have changed in our branch.
*/
cl_git_pass(git_revparse_single(&branch, g_repo, "099fabac3a9ea935598528c27f866e34089c2eff"));
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, branch, NULL));
git_object_free(branch);
git_object_free(target);
}
void test_checkout_head__obeys_filemode_false(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target, *branch;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
/* In this commit, `README` is executable */
cl_git_pass(git_revparse_single(&target, g_repo, "f9ed4af42472941da45a3c"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_repo_set_bool(g_repo, "core.filemode", false);
cl_must_pass(p_chmod("testrepo/README", 0644));
/*
* Checkout will fail with a conflict; the file contents are updated
* in the checkout target, but the filemode has changed in our branch.
*/
cl_git_pass(git_revparse_single(&branch, g_repo, "099fabac3a9ea935598528c27f866e34089c2eff"));
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, branch, NULL));
git_object_free(branch);
git_object_free(target);
}
| libgit2-main | tests/libgit2/checkout/head.c |
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "refs.h"
#include "futils.h"
#include "index.h"
void assert_on_branch(git_repository *repo, const char *branch)
{
git_reference *head;
git_str bname = GIT_STR_INIT;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert_(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC, branch);
cl_git_pass(git_str_joinpath(&bname, "refs/heads", branch));
cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
git_reference_free(head);
git_str_dispose(&bname);
}
void reset_index_to_treeish(git_object *treeish)
{
git_object *tree;
git_index *index;
git_repository *repo = git_object_owner(treeish);
cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJECT_TREE));
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_read_tree(index, (git_tree *)tree));
cl_git_pass(git_index_write(index));
git_object_free(tree);
git_index_free(index);
}
int checkout_count_callback(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
checkout_counts *ct = payload;
GIT_UNUSED(baseline); GIT_UNUSED(target); GIT_UNUSED(workdir);
if (why & GIT_CHECKOUT_NOTIFY_CONFLICT) {
ct->n_conflicts++;
if (ct->debug) {
if (workdir) {
if (baseline) {
if (target)
fprintf(stderr, "M %s (conflicts with M %s)\n",
workdir->path, target->path);
else
fprintf(stderr, "M %s (conflicts with D %s)\n",
workdir->path, baseline->path);
} else {
if (target)
fprintf(stderr, "Existing %s (conflicts with A %s)\n",
workdir->path, target->path);
else
fprintf(stderr, "How can an untracked file be a conflict (%s)\n", workdir->path);
}
} else {
if (baseline) {
if (target)
fprintf(stderr, "D %s (conflicts with M %s)\n",
target->path, baseline->path);
else
fprintf(stderr, "D %s (conflicts with D %s)\n",
baseline->path, baseline->path);
} else {
if (target)
fprintf(stderr, "How can an added file with no workdir be a conflict (%s)\n", target->path);
else
fprintf(stderr, "How can a nonexistent file be a conflict (%s)\n", path);
}
}
}
}
if (why & GIT_CHECKOUT_NOTIFY_DIRTY) {
ct->n_dirty++;
if (ct->debug) {
if (workdir)
fprintf(stderr, "M %s\n", workdir->path);
else
fprintf(stderr, "D %s\n", baseline->path);
}
}
if (why & GIT_CHECKOUT_NOTIFY_UPDATED) {
ct->n_updates++;
if (ct->debug) {
if (baseline) {
if (target)
fprintf(stderr, "update: M %s\n", path);
else
fprintf(stderr, "update: D %s\n", path);
} else {
if (target)
fprintf(stderr, "update: A %s\n", path);
else
fprintf(stderr, "update: this makes no sense %s\n", path);
}
}
}
if (why & GIT_CHECKOUT_NOTIFY_UNTRACKED) {
ct->n_untracked++;
if (ct->debug)
fprintf(stderr, "? %s\n", path);
}
if (why & GIT_CHECKOUT_NOTIFY_IGNORED) {
ct->n_ignored++;
if (ct->debug)
fprintf(stderr, "I %s\n", path);
}
return 0;
}
void tick_index(git_index *index)
{
struct timespec ts;
struct p_timeval times[2];
cl_assert(index->on_disk);
cl_assert(git_index_path(index));
cl_git_pass(git_index_read(index, true));
ts = index->stamp.mtime;
times[0].tv_sec = ts.tv_sec;
times[0].tv_usec = ts.tv_nsec / 1000;
times[1].tv_sec = ts.tv_sec + 5;
times[1].tv_usec = ts.tv_nsec / 1000;
cl_git_pass(p_utimes(git_index_path(index), times));
cl_git_pass(git_index_read(index, true));
}
| libgit2-main | tests/libgit2/checkout/checkout_helpers.c |
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "../filter/crlf.h"
#include "futils.h"
#include "git2/checkout.h"
#include "repository.h"
#include "index.h"
#include "posix.h"
static git_repository *g_repo;
static const char *systype;
static git_str expected_fixture = GIT_STR_INIT;
static int unlink_file(void *payload, git_str *path)
{
char *fn;
cl_assert(fn = git_fs_path_basename(path->ptr));
GIT_UNUSED(payload);
if (strcmp(fn, ".git"))
cl_must_pass(p_unlink(path->ptr));
git__free(fn);
return 0;
}
void test_checkout_crlf__initialize(void)
{
git_str reponame = GIT_STR_INIT;
g_repo = cl_git_sandbox_init("crlf");
/*
* remove the contents of the working directory so that we can
* check out over it.
*/
cl_git_pass(git_str_puts(&reponame, "crlf"));
cl_git_pass(git_fs_path_direach(&reponame, 0, unlink_file, NULL));
if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
systype = "windows";
else
systype = "posix";
git_str_dispose(&reponame);
}
void test_checkout_crlf__cleanup(void)
{
cl_git_sandbox_cleanup();
if (expected_fixture.size) {
cl_fixture_cleanup(expected_fixture.ptr);
git_str_dispose(&expected_fixture);
}
}
struct compare_data
{
const char *dirname;
const char *autocrlf;
const char *attrs;
};
static int compare_file(void *payload, git_str *actual_path)
{
git_str expected_path = GIT_STR_INIT;
git_str actual_contents = GIT_STR_INIT;
git_str expected_contents = GIT_STR_INIT;
struct compare_data *cd = payload;
bool failed = true;
int cmp_git, cmp_gitattributes;
char *basename;
basename = git_fs_path_basename(actual_path->ptr);
cmp_git = strcmp(basename, ".git");
cmp_gitattributes = strcmp(basename, ".gitattributes");
if (cmp_git == 0 || cmp_gitattributes == 0) {
failed = false;
goto done;
}
cl_git_pass(git_str_joinpath(&expected_path, cd->dirname, basename));
if (!git_fs_path_isfile(expected_path.ptr) ||
!git_fs_path_isfile(actual_path->ptr))
goto done;
if (git_futils_readbuffer(&actual_contents, actual_path->ptr) < 0 ||
git_futils_readbuffer(&expected_contents, expected_path.ptr) < 0)
goto done;
if (actual_contents.size != expected_contents.size)
goto done;
if (memcmp(actual_contents.ptr, expected_contents.ptr, expected_contents.size) != 0)
goto done;
failed = false;
done:
if (failed) {
git_str details = GIT_STR_INIT;
git_str_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}",
git_fs_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs);
clar__fail(__FILE__, __func__, __LINE__,
"checked out contents did not match expected", details.ptr, 0);
git_str_dispose(&details);
}
git__free(basename);
git_str_dispose(&expected_contents);
git_str_dispose(&actual_contents);
git_str_dispose(&expected_path);
return 0;
}
static void test_checkout(const char *autocrlf, const char *attrs)
{
git_str attrbuf = GIT_STR_INIT;
git_str expected_dirname = GIT_STR_INIT;
git_str systype_and_direction = GIT_STR_INIT;
git_str sandboxname = GIT_STR_INIT;
git_str reponame = GIT_STR_INIT;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct compare_data compare_data = { NULL, autocrlf, attrs };
const char *c;
cl_git_pass(git_str_puts(&reponame, "crlf"));
cl_git_pass(git_str_puts(&systype_and_direction, systype));
cl_git_pass(git_str_puts(&systype_and_direction, "_to_workdir"));
cl_git_pass(git_str_puts(&sandboxname, "autocrlf_"));
cl_git_pass(git_str_puts(&sandboxname, autocrlf));
if (*attrs) {
cl_git_pass(git_str_puts(&sandboxname, ","));
for (c = attrs; *c; c++) {
if (*c == ' ')
cl_git_pass(git_str_putc(&sandboxname, ','));
else if (*c == '=')
cl_git_pass(git_str_putc(&sandboxname, '_'));
else
cl_git_pass(git_str_putc(&sandboxname, *c));
}
cl_git_pass(git_str_printf(&attrbuf, "* %s\n", attrs));
cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
}
cl_repo_set_string(g_repo, "core.autocrlf", autocrlf);
cl_git_pass(git_str_joinpath(&expected_dirname, systype_and_direction.ptr, sandboxname.ptr));
cl_git_pass(git_str_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr));
cl_fixture_sandbox(expected_fixture.ptr);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
compare_data.dirname = sandboxname.ptr;
cl_git_pass(git_fs_path_direach(&reponame, 0, compare_file, &compare_data));
cl_fixture_cleanup(expected_fixture.ptr);
git_str_dispose(&expected_fixture);
git_str_dispose(&attrbuf);
git_str_dispose(&expected_fixture);
git_str_dispose(&expected_dirname);
git_str_dispose(&sandboxname);
git_str_dispose(&systype_and_direction);
git_str_dispose(&reponame);
}
static void empty_workdir(const char *name)
{
git_vector contents = GIT_VECTOR_INIT;
char *basename;
int cmp;
size_t i;
const char *fn;
cl_git_pass(git_fs_path_dirload(&contents, name, 0, 0));
git_vector_foreach(&contents, i, fn) {
cl_assert(basename = git_fs_path_basename(fn));
cmp = strncasecmp(basename, ".git", 4);
git__free(basename);
if (cmp)
cl_git_pass(p_unlink(fn));
}
git_vector_free_deep(&contents);
}
void test_checkout_crlf__matches_core_git(void)
{
const char *autocrlf[] = { "true", "false", "input", NULL };
const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
"text", "text eol=crlf", "text eol=lf",
"text=auto", "text=auto eol=crlf", "text=auto eol=lf",
NULL };
const char **a, **b;
for (a = autocrlf; *a; a++) {
for (b = attrs; *b; b++) {
empty_workdir("crlf");
test_checkout(*a, *b);
}
}
}
void test_checkout_crlf__detect_crlf_autocrlf_false(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
git_checkout_head(g_repo, &opts);
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
git_index *index;
const git_index_entry *entry;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_pass(git_repository_index(&index, g_repo));
tick_index(index);
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));
git_index_free(index);
}
void test_checkout_crlf__detect_crlf_autocrlf_true(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
git_checkout_head(g_repo, &opts);
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
git_repository_set_head(g_repo, "refs/heads/master");
git_checkout_head(g_repo, &opts);
check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_CRLF_RAW);
check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_CRLF_RAW);
check_file_contents("./crlf/few-utf8-chars-crlf", FEW_UTF8_CRLF_RAW);
check_file_contents("./crlf/many-utf8-chars-crlf", MANY_UTF8_CRLF_RAW);
}
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
git_index *index;
const git_index_entry *entry;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
git_repository_index(&index, g_repo);
tick_index(index);
git_checkout_head(g_repo, &opts);
cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
git_index_free(index);
}
void test_checkout_crlf__with_ident(void)
{
git_index *index;
const git_index_entry *entry;
git_blob *blob;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("crlf/.gitattributes",
"*.txt text\n*.bin binary\n"
"*.crlf text eol=crlf\n"
"*.lf text eol=lf\n"
"*.ident text ident\n"
"*.identcrlf ident text eol=crlf\n"
"*.identlf ident text eol=lf\n");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
/* add files with $Id$ */
cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW);
cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: $\r\n" MORE_CRLF_TEXT_RAW);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "lf.ident"));
cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");
git_checkout_head(g_repo, &opts);
/* check that blobs have $Id$ */
cl_assert((entry = git_index_get_bypath(index, "lf.ident", 0)));
cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
cl_assert_equal_s(
ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
git_blob_free(blob);
cl_assert((entry = git_index_get_bypath(index, "more2.identcrlf", 0)));
cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
cl_assert_equal_s(
"\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
git_blob_free(blob);
/* check that filesystem is initially untouched - matching core Git */
cl_assert_equal_file(
ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
/* check that forced checkout rewrites correctly */
p_unlink("crlf/lf.ident");
p_unlink("crlf/crlf.ident");
p_unlink("crlf/more1.identlf");
p_unlink("crlf/more2.identcrlf");
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert_equal_file(
ALL_LF_TEXT_AS_CRLF
"\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
0, "crlf/lf.ident");
cl_assert_equal_file(
ALL_CRLF_TEXT_RAW
"\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
0, "crlf/crlf.ident");
cl_assert_equal_file(
"$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
cl_assert_equal_file(
"\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
git_index_free(index);
}
void test_checkout_crlf__autocrlf_false_no_attrs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__autocrlf_true_no_attrs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
}
void test_checkout_crlf__autocrlf_input_no_attrs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_pass(git_checkout_head(g_repo, &opts));
if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
} else {
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
}
void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
}
void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__can_write_empty_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/empty-files"));
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/test1.txt", "");
check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
check_file_contents("./crlf/test3.txt", "");
}
| libgit2-main | tests/libgit2/checkout/crlf.c |
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "repository.h"
#include "futils.h"
static git_repository *g_repo;
static git_checkout_options g_opts;
static git_object *g_object;
static void assert_status_entrycount(git_repository *repo, size_t count)
{
git_status_list *status;
cl_git_pass(git_status_list_new(&status, repo, NULL));
cl_assert_equal_i(count, git_status_list_entrycount(status));
git_status_list_free(status);
}
void test_checkout_tree__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTIONS_VERSION);
g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
}
void test_checkout_tree__cleanup(void)
{
git_object_free(g_object);
g_object = NULL;
cl_git_sandbox_cleanup();
if (git_fs_path_isdir("alternative"))
git_futils_rmdir_r("alternative", NULL, GIT_RMDIR_REMOVE_FILES);
}
void test_checkout_tree__cannot_checkout_a_non_treeish(void)
{
/* blob */
cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
cl_git_fail(git_checkout_tree(g_repo, g_object, NULL));
}
void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
{
char *entries[] = { "ab/de/" };
g_opts.paths.strings = entries;
g_opts.paths.count = 1;
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/fgh/1.txt"));
}
void test_checkout_tree__can_checkout_and_remove_directory(void)
{
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
/* Checkout branch "subtrees" and update HEAD, so that HEAD matches the
* current working tree
*/
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
cl_assert_equal_i(true, git_fs_path_isdir("./testrepo/ab/"));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/fgh/1.txt"));
git_object_free(g_object);
g_object = NULL;
/* Checkout branch "master" and update HEAD, so that HEAD matches the
* current working tree
*/
cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
/* This directory should no longer exist */
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
}
void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
{
char *entries[] = { "de/" };
g_opts.paths.strings = entries;
g_opts.paths.count = 1;
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees:ab"));
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/de/"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/de/2.txt"));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/de/fgh/1.txt"));
}
static void progress(const char *path, size_t cur, size_t tot, void *payload)
{
bool *was_called = (bool*)payload;
GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
*was_called = true;
}
void test_checkout_tree__calls_progress_callback(void)
{
bool was_called = 0;
g_opts.progress_cb = progress;
g_opts.progress_payload = &was_called;
cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert_equal_i(was_called, true);
}
void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
{
git_oid master_oid;
git_oid chomped_oid;
git_commit* p_master_commit;
git_commit* p_chomped_commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid__fromstr(&master_oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
git_oid__fromstr(&chomped_oid, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid));
cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid));
/* GIT_CHECKOUT_NONE should not add any file to the working tree from the
* index as it is supposed to be a dry run.
*/
opts.checkout_strategy = GIT_CHECKOUT_NONE;
git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts);
cl_assert_equal_i(false, git_fs_path_isfile("testrepo/readme.txt"));
git_commit_free(p_master_commit);
git_commit_free(p_chomped_commit);
}
void test_checkout_tree__can_switch_branches(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
assert_on_branch(g_repo, "master");
/* do first checkout with FORCE because we don't know if testrepo
* base data is clean for a checkout or not
*/
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
cl_assert(git_fs_path_isfile("testrepo/README"));
cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
cl_assert(git_fs_path_isfile("testrepo/new.txt"));
cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
cl_assert(!git_fs_path_isdir("testrepo/ab"));
assert_on_branch(g_repo, "dir");
git_object_free(obj);
/* do second checkout safe because we should be clean after first */
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
cl_assert(git_fs_path_isfile("testrepo/README"));
cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
cl_assert(git_fs_path_isfile("testrepo/new.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/c/3.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/de/2.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/de/fgh/1.txt"));
cl_assert(!git_fs_path_isdir("testrepo/a"));
assert_on_branch(g_repo, "subtrees");
git_object_free(obj);
}
void test_checkout_tree__can_remove_untracked(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_mkfile("testrepo/untracked_file", "as you wish");
cl_assert(git_fs_path_isfile("testrepo/untracked_file"));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isfile("testrepo/untracked_file"));
}
void test_checkout_tree__can_remove_ignored(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
int ignored = 0;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_IGNORED;
cl_git_mkfile("testrepo/ignored_file", "as you wish");
cl_git_pass(git_ignore_add_rule(g_repo, "ignored_file\n"));
cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ignored_file"));
cl_assert_equal_i(1, ignored);
cl_assert(git_fs_path_isfile("testrepo/ignored_file"));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isfile("testrepo/ignored_file"));
}
static int checkout_tree_with_blob_ignored_in_workdir(int strategy, bool isdir)
{
git_oid oid;
git_object *obj = NULL;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
int ignored = 0, error;
assert_on_branch(g_repo, "master");
/* do first checkout with FORCE because we don't know if testrepo
* base data is clean for a checkout or not
*/
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
cl_assert(git_fs_path_isfile("testrepo/README"));
cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
cl_assert(git_fs_path_isfile("testrepo/new.txt"));
cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
cl_assert(!git_fs_path_isdir("testrepo/ab"));
assert_on_branch(g_repo, "dir");
git_object_free(obj);
opts.checkout_strategy = strategy;
if (isdir) {
cl_must_pass(p_mkdir("testrepo/ab", 0777));
cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777));
cl_git_mkfile("testrepo/ab/4.txt/file1.txt", "as you wish");
cl_git_mkfile("testrepo/ab/4.txt/file2.txt", "foo bar foo");
cl_git_mkfile("testrepo/ab/4.txt/file3.txt", "inky blinky pinky clyde");
cl_assert(git_fs_path_isdir("testrepo/ab/4.txt"));
} else {
cl_must_pass(p_mkdir("testrepo/ab", 0777));
cl_git_mkfile("testrepo/ab/4.txt", "as you wish");
cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
}
cl_git_pass(git_ignore_add_rule(g_repo, "ab/4.txt\n"));
cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ab/4.txt"));
cl_assert_equal_i(1, ignored);
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
error = git_checkout_tree(g_repo, obj, &opts);
git_object_free(obj);
return error;
}
void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void)
{
int error;
cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir(
GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, false));
cl_assert_equal_i(GIT_ECONFLICT, error);
}
void test_checkout_tree__can_overwrite_ignored_by_default(void)
{
cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, false));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
assert_on_branch(g_repo, "subtrees");
}
void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void)
{
int error;
cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir(
GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, true));
cl_assert_equal_i(GIT_ECONFLICT, error);
}
void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
{
cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, true));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
assert_on_branch(g_repo, "subtrees");
}
void test_checkout_tree__can_update_only(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
/* first let's get things into a known state - by checkout out the HEAD */
assert_on_branch(g_repo, "master");
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isdir("testrepo/a"));
check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
/* now checkout branch but with update only */
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
assert_on_branch(g_repo, "dir");
/* this normally would have been created (which was tested separately in
* the test_checkout_tree__can_switch_branches test), but with
* UPDATE_ONLY it will not have been created.
*/
cl_assert(!git_fs_path_isdir("testrepo/a"));
/* but this file still should have been updated */
check_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
git_object_free(obj);
}
void test_checkout_tree__can_checkout_with_pattern(void)
{
char *entries[] = { "[l-z]*.txt" };
/* reset to beginning of history (i.e. just a README file) */
g_opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(g_object)));
git_object_free(g_object);
g_object = NULL;
cl_assert(git_fs_path_exists("testrepo/README"));
cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
cl_assert(!git_fs_path_exists("testrepo/link_to_new.txt"));
cl_assert(!git_fs_path_exists("testrepo/new.txt"));
/* now to a narrow patterned checkout */
g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
g_opts.paths.strings = entries;
g_opts.paths.count = 1;
cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert(git_fs_path_exists("testrepo/README"));
cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
cl_assert(git_fs_path_exists("testrepo/link_to_new.txt"));
cl_assert(git_fs_path_exists("testrepo/new.txt"));
}
void test_checkout_tree__pathlist_checkout_ignores_non_matches(void)
{
char *entries[] = { "branch_file.txt", "link_to_new.txt" };
/* reset to beginning of history (i.e. just a README file) */
g_opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
cl_assert(git_fs_path_exists("testrepo/README"));
cl_assert(git_fs_path_exists("testrepo/branch_file.txt"));
cl_assert(git_fs_path_exists("testrepo/link_to_new.txt"));
cl_assert(git_fs_path_exists("testrepo/new.txt"));
git_object_free(g_object);
cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
g_opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
g_opts.paths.strings = entries;
g_opts.paths.count = 2;
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert(git_fs_path_exists("testrepo/README"));
cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
cl_assert(!git_fs_path_exists("testrepo/link_to_new.txt"));
cl_assert(git_fs_path_exists("testrepo/new.txt"));
}
void test_checkout_tree__can_disable_pattern_match(void)
{
char *entries[] = { "b*.txt" };
/* reset to beginning of history (i.e. just a README file) */
g_opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(g_object)));
git_object_free(g_object);
g_object = NULL;
cl_assert(!git_fs_path_isfile("testrepo/branch_file.txt"));
/* now to a narrow patterned checkout, but disable pattern */
g_opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
g_opts.paths.strings = entries;
g_opts.paths.count = 1;
cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert(!git_fs_path_isfile("testrepo/branch_file.txt"));
/* let's try that again, but allow the pattern match */
g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
}
static void assert_conflict(
const char *entry_path,
const char *new_content,
const char *parent_sha,
const char *commit_sha)
{
git_index *index;
git_object *hack_tree;
git_reference *branch, *head;
git_str file_path = GIT_STR_INIT;
cl_git_pass(git_repository_index(&index, g_repo));
/* Create a branch pointing at the parent */
cl_git_pass(git_revparse_single(&g_object, g_repo, parent_sha));
cl_git_pass(git_branch_create(&branch, g_repo,
"potential_conflict", (git_commit *)g_object, 0));
/* Make HEAD point to this branch */
cl_git_pass(git_reference_symbolic_create(
&head, g_repo, "HEAD", git_reference_name(branch), 1, NULL));
git_reference_free(head);
git_reference_free(branch);
/* Checkout the parent */
g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
/* Hack-ishy workaround to ensure *all* the index entries
* match the content of the tree
*/
cl_git_pass(git_object_peel(&hack_tree, g_object, GIT_OBJECT_TREE));
cl_git_pass(git_index_read_tree(index, (git_tree *)hack_tree));
cl_git_pass(git_index_write(index));
git_object_free(hack_tree);
git_object_free(g_object);
g_object = NULL;
/* Create a conflicting file */
cl_git_pass(git_str_joinpath(&file_path, "./testrepo", entry_path));
cl_git_mkfile(git_str_cstr(&file_path), new_content);
git_str_dispose(&file_path);
/* Trying to checkout the original commit */
cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha));
g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_assert_equal_i(
GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
/* Stage the conflicting change */
cl_git_pass(git_index_add_bypath(index, entry_path));
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_assert_equal_i(
GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
}
void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(void)
{
/*
* 099faba adds a symlink named 'link_to_new.txt'
* a65fedf is the parent of 099faba
*/
assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba");
}
void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT_2(void)
{
/*
* cf80f8d adds a directory named 'a/'
* a4a7dce is the parent of cf80f8d
*/
assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d");
}
void test_checkout_tree__checking_out_a_conflicting_content_change_returns_ECONFLICT(void)
{
/*
* c47800c adds a symlink named 'branch_file.txt'
* 5b5b025 is the parent of 763d71a
*/
assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c");
}
void test_checkout_tree__donot_update_deleted_file_by_default(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid old_id, new_id;
git_commit *old_commit = NULL, *new_commit = NULL;
git_index *index = NULL;
checkout_counts ct;
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
memset(&ct, 0, sizeof(ct));
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &ct;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_oid__fromstr(&old_id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&old_commit, g_repo, &old_id));
cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD, NULL));
cl_git_pass(p_unlink("testrepo/branch_file.txt"));
cl_git_pass(git_index_remove_bypath(index ,"branch_file.txt"));
cl_git_pass(git_index_write(index));
cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
cl_git_pass(git_oid__fromstr(&new_id, "099fabac3a9ea935598528c27f866e34089c2eff", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&new_commit, g_repo, &new_id));
cl_git_fail(git_checkout_tree(g_repo, (git_object *)new_commit, &opts));
cl_assert_equal_i(1, ct.n_conflicts);
cl_assert_equal_i(1, ct.n_updates);
git_commit_free(old_commit);
git_commit_free(new_commit);
git_index_free(index);
}
struct checkout_cancel_at {
const char *filename;
int error;
int count;
};
static int checkout_cancel_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *b,
const git_diff_file *t,
const git_diff_file *w,
void *payload)
{
struct checkout_cancel_at *ca = payload;
GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
ca->count++;
if (!strcmp(path, ca->filename))
return ca->error;
return 0;
}
void test_checkout_tree__can_cancel_checkout_from_notify(void)
{
struct checkout_cancel_at ca;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
assert_on_branch(g_repo, "master");
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
ca.filename = "new.txt";
ca.error = -5555;
ca.count = 0;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
opts.notify_cb = checkout_cancel_cb;
opts.notify_payload = &ca;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_assert(!git_fs_path_exists("testrepo/new.txt"));
cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), -5555);
cl_assert(!git_fs_path_exists("testrepo/new.txt"));
/* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
/* on case-sensitive FS = README, then above */
if (git_fs_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
cl_assert_equal_i(3, ca.count);
else
cl_assert_equal_i(4, ca.count);
/* and again with a different stopping point and return code */
ca.filename = "README";
ca.error = 123;
ca.count = 0;
cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123);
cl_assert(!git_fs_path_exists("testrepo/new.txt"));
if (git_fs_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
cl_assert_equal_i(4, ca.count);
else
cl_assert_equal_i(1, ca.count);
git_object_free(obj);
}
void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
{
git_index *index = NULL;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid tree_id, commit_id;
git_tree *tree = NULL;
git_commit *commit = NULL;
git_repository_index(&index, g_repo);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&commit_id, g_repo, "refs/heads/master"));
cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
cl_git_pass(git_index_add_bypath(index, "this-is-dir/contained_file"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_write_tree(&tree_id, index));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
opts.checkout_strategy = 1;
git_checkout_tree(g_repo, (git_object *)tree, &opts);
git_tree_free(tree);
git_commit_free(commit);
git_index_free(index);
}
void test_checkout_tree__issue_1397(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const char *partial_oid = "8a7ef04";
git_object *tree = NULL;
test_checkout_tree__cleanup(); /* cleanup default checkout */
g_repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_revparse_single(&tree, g_repo, partial_oid));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(g_repo, tree, &opts));
check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
git_object_free(tree);
}
void test_checkout_tree__can_write_to_empty_dirs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
assert_on_branch(g_repo, "master");
cl_git_pass(p_mkdir("testrepo/a", 0777));
/* do first checkout with FORCE because we don't know if testrepo
* base data is clean for a checkout or not
*/
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
git_object_free(obj);
}
void test_checkout_tree__fails_when_dir_in_use(void)
{
#ifdef GIT_WIN32
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
git_object_free(obj);
cl_git_pass(p_chdir("testrepo/a"));
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(p_chdir("../.."));
cl_assert(git_fs_path_is_empty_dir("testrepo/a"));
git_object_free(obj);
#endif
}
void test_checkout_tree__can_continue_when_dir_in_use(void)
{
#ifdef GIT_WIN32
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
opts.checkout_strategy = GIT_CHECKOUT_FORCE |
GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
git_object_free(obj);
cl_git_pass(p_chdir("testrepo/a"));
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(p_chdir("../.."));
cl_assert(git_fs_path_is_empty_dir("testrepo/a"));
git_object_free(obj);
#endif
}
void test_checkout_tree__target_directory_from_bare(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
checkout_counts cts;
memset(&cts, 0, sizeof(cts));
test_checkout_tree__cleanup(); /* cleanup default checkout */
g_repo = cl_git_sandbox_init("testrepo.git");
cl_assert(git_repository_is_bare(g_repo));
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &cts;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
cl_git_pass(git_object_lookup(&g_object, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_fail(git_checkout_tree(g_repo, g_object, &opts));
opts.target_directory = "alternative";
cl_assert(!git_fs_path_isdir("alternative"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
cl_assert_equal_i(0, cts.n_untracked);
cl_assert_equal_i(0, cts.n_ignored);
cl_assert_equal_i(3, cts.n_updates);
check_file_contents_nocr("./alternative/README", "hey there\n");
check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
check_file_contents_nocr("./alternative/new.txt", "my new file\n");
cl_git_pass(git_futils_rmdir_r(
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
}
void test_checkout_tree__extremely_long_file_name(void)
{
/* A utf-8 string with 83 characters, but 249 bytes. */
const char *longname = "\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97";
char path[1024] = {0};
g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&g_object, g_repo, "long-file-name"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
sprintf(path, "testrepo/%s.txt", longname);
cl_assert(git_fs_path_exists(path));
git_object_free(g_object);
cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
cl_assert(!git_fs_path_exists(path));
}
static void create_conflict(const char *path)
{
git_index *index;
git_index_entry entry;
cl_git_pass(git_repository_index(&index, g_repo));
memset(&entry, 0x0, sizeof(git_index_entry));
entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
git_oid__fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46", GIT_OID_SHA1);
entry.path = path;
cl_git_pass(git_index_add(index, &entry));
GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
git_oid__fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
cl_git_pass(git_index_add(index, &entry));
GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
git_oid__fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
cl_git_pass(git_index_add(index, &entry));
cl_git_pass(git_index_write(index));
git_index_free(index);
}
void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
create_conflict("conflicts.txt");
cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
git_object_free(obj);
}
void test_checkout_tree__filemode_preserved_in_index(void)
{
git_oid executable_oid;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
const git_index_entry *entry;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_repository_index(&index, g_repo));
/* test a freshly added executable */
cl_git_pass(git_oid__fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(entry = git_index_get_bypath(index, "executable.txt", 0));
cl_assert(GIT_PERMS_IS_EXEC(entry->mode));
git_commit_free(commit);
/* Now start with a commit which has a text file */
cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
cl_assert(!GIT_PERMS_IS_EXEC(entry->mode));
git_commit_free(commit);
/* And then check out to a commit which converts the text file to an executable */
cl_git_pass(git_oid__fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
cl_assert(GIT_PERMS_IS_EXEC(entry->mode));
git_commit_free(commit);
/* Finally, check out the text file again and check that the exec bit is cleared */
cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
cl_assert(!GIT_PERMS_IS_EXEC(entry->mode));
git_commit_free(commit);
git_index_free(index);
}
#ifndef GIT_WIN32
static mode_t read_filemode(const char *path)
{
git_str fullpath = GIT_STR_INIT;
struct stat st;
mode_t result;
git_str_joinpath(&fullpath, "testrepo", path);
cl_must_pass(p_stat(fullpath.ptr, &st));
result = GIT_PERMS_IS_EXEC(st.st_mode) ?
GIT_FILEMODE_BLOB_EXECUTABLE : GIT_FILEMODE_BLOB;
git_str_dispose(&fullpath);
return result;
}
#endif
void test_checkout_tree__filemode_preserved_in_workdir(void)
{
#ifndef GIT_WIN32
git_oid executable_oid;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
/* test a freshly added executable */
cl_git_pass(git_oid__fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt")));
git_commit_free(commit);
/* Now start with a commit which has a text file */
cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
git_commit_free(commit);
/* And then check out to a commit which converts the text file to an executable */
cl_git_pass(git_oid__fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
git_commit_free(commit);
/* Finally, check out the text file again and check that the exec bit is cleared */
cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
git_commit_free(commit);
#else
cl_skip();
#endif
}
void test_checkout_tree__removes_conflicts(void)
{
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(git_oid__fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_remove(index, "executable.txt", 0));
create_conflict("executable.txt");
cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
create_conflict("other.txt");
cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 1));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 2));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 3));
cl_assert(!git_fs_path_exists("testrepo/other.txt"));
git_commit_free(commit);
git_index_free(index);
}
void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
{
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
const char *path = "executable.txt";
cl_git_pass(git_oid__fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
opts.paths.count = 1;
opts.paths.strings = (char **)&path;
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_remove(index, "executable.txt", 0));
create_conflict("executable.txt");
cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");
create_conflict("other.txt");
cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));
cl_assert(git_index_get_bypath(index, "other.txt", 1) != NULL);
cl_assert(git_index_get_bypath(index, "other.txt", 2) != NULL);
cl_assert(git_index_get_bypath(index, "other.txt", 3) != NULL);
cl_assert(git_fs_path_exists("testrepo/other.txt"));
git_commit_free(commit);
git_index_free(index);
}
void test_checkout_tree__case_changing_rename(void)
{
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid master_id, dir_commit_id, tree_id, commit_id;
git_commit *master_commit, *dir_commit;
git_tree *tree;
git_signature *signature;
const git_index_entry *index_entry;
bool case_sensitive;
assert_on_branch(g_repo, "master");
cl_git_pass(git_repository_index(&index, g_repo));
/* Switch branches and perform a case-changing rename */
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&dir_commit_id, g_repo, "refs/heads/dir"));
cl_git_pass(git_commit_lookup(&dir_commit, g_repo, &dir_commit_id));
cl_git_pass(git_checkout_tree(g_repo, (git_object *)dir_commit, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
cl_assert(git_fs_path_isfile("testrepo/README"));
case_sensitive = !git_fs_path_isfile("testrepo/readme");
cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
cl_assert_equal_s("README", index_entry->path);
cl_git_pass(git_index_remove_bypath(index, "README"));
cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
cl_git_append2file("testrepo/readme", "An addendum...");
cl_git_pass(git_index_add_bypath(index, "readme"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_write_tree(&tree_id, index));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(git_signature_new(&signature, "Renamer", "[email protected]", time(NULL), 0));
cl_git_pass(git_commit_create(&commit_id, g_repo, "refs/heads/dir", signature, signature, NULL, "case-changing rename", tree, 1, (const git_commit **)&dir_commit));
cl_assert(git_fs_path_isfile("testrepo/readme"));
if (case_sensitive)
cl_assert(!git_fs_path_isfile("testrepo/README"));
cl_assert(index_entry = git_index_get_bypath(index, "readme", 0));
cl_assert_equal_s("readme", index_entry->path);
/* Switching back to master should rename readme -> README */
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_name_to_id(&master_id, g_repo, "refs/heads/master"));
cl_git_pass(git_commit_lookup(&master_commit, g_repo, &master_id));
cl_git_pass(git_checkout_tree(g_repo, (git_object *)master_commit, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
assert_on_branch(g_repo, "master");
cl_assert(git_fs_path_isfile("testrepo/README"));
if (case_sensitive)
cl_assert(!git_fs_path_isfile("testrepo/readme"));
cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
cl_assert_equal_s("README", index_entry->path);
git_index_free(index);
git_signature_free(signature);
git_tree_free(tree);
git_commit_free(dir_commit);
git_commit_free(master_commit);
}
static void perfdata_cb(const git_checkout_perfdata *in, void *payload)
{
memcpy(payload, in, sizeof(git_checkout_perfdata));
}
void test_checkout_tree__can_collect_perfdata(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
git_checkout_perfdata perfdata = {0};
opts.perfdata_cb = perfdata_cb;
opts.perfdata_payload = &perfdata;
assert_on_branch(g_repo, "master");
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_assert(perfdata.mkdir_calls > 0);
cl_assert(perfdata.stat_calls > 0);
git_object_free(obj);
}
static void update_attr_callback(
const char *path,
size_t completed_steps,
size_t total_steps,
void *payload)
{
GIT_UNUSED(completed_steps);
GIT_UNUSED(total_steps);
GIT_UNUSED(payload);
if (path && strcmp(path, "ident1.txt") == 0)
cl_git_write2file("testrepo/.gitattributes",
"*.txt ident\n", 12, O_RDWR|O_CREAT, 0666);
}
void test_checkout_tree__caches_attributes_during_checkout(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
git_str ident1 = GIT_STR_INIT, ident2 = GIT_STR_INIT;
char *ident_paths[] = { "ident1.txt", "ident2.txt" };
opts.progress_cb = update_attr_callback;
assert_on_branch(g_repo, "master");
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
opts.paths.strings = ident_paths;
opts.paths.count = 2;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/ident"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
cl_assert_equal_strn(ident1.ptr, "# $Id$", 6);
cl_assert_equal_strn(ident2.ptr, "# $Id$", 6);
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));
cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7);
cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7);
git_str_dispose(&ident1);
git_str_dispose(&ident2);
git_object_free(obj);
}
void test_checkout_tree__can_not_update_index(void)
{
git_oid oid;
git_object *head;
unsigned int status;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
opts.checkout_strategy |=
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
git_object_free(head);
git_index_free(index);
}
void test_checkout_tree__can_update_but_not_write_index(void)
{
git_oid oid;
git_object *head;
unsigned int status;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
git_repository *other;
opts.checkout_strategy |=
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
cl_git_pass(git_repository_open(&other, "testrepo"));
cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
git_repository_free(other);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_repository_open(&other, "testrepo"));
cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
git_repository_free(other);
git_object_free(head);
git_index_free(index);
}
/* Emulate checking out in a repo created by clone --no-checkout,
* which would not have written an index. */
void test_checkout_tree__safe_proceeds_if_no_index(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
assert_on_branch(g_repo, "master");
cl_must_pass(p_unlink("testrepo/.git/index"));
/* do second checkout safe because we should be clean after first */
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
cl_assert(git_fs_path_isfile("testrepo/README"));
cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
cl_assert(git_fs_path_isfile("testrepo/new.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/c/3.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/de/2.txt"));
cl_assert(git_fs_path_isfile("testrepo/ab/de/fgh/1.txt"));
cl_assert(!git_fs_path_isdir("testrepo/a"));
assert_on_branch(g_repo, "subtrees");
git_object_free(obj);
}
static int checkout_conflict_count_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *b,
const git_diff_file *t,
const git_diff_file *w,
void *payload)
{
size_t *n = payload;
GIT_UNUSED(why);
GIT_UNUSED(path);
GIT_UNUSED(b);
GIT_UNUSED(t);
GIT_UNUSED(w);
(*n)++;
return 0;
}
/* A repo that has a HEAD (even a properly born HEAD that peels to
* a commit) but no index should be treated as if it's an empty baseline
*/
void test_checkout_tree__baseline_is_empty_when_no_index(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_reference *head;
git_object *obj;
size_t conflicts = 0;
assert_on_branch(g_repo, "master");
cl_git_pass(git_repository_head(&head, g_repo));
cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));
cl_must_pass(p_unlink("testrepo/.git/index"));
/* for a safe checkout, we should have checkout conflicts with
* the existing untracked files.
*/
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
opts.notify_cb = checkout_conflict_count_cb;
opts.notify_payload = &conflicts;
cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, obj, &opts));
cl_assert_equal_i(4, conflicts);
/* but force should succeed and update the index */
opts.checkout_strategy |= GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
assert_status_entrycount(g_repo, 0);
git_object_free(obj);
git_reference_free(head);
}
void test_checkout_tree__mode_change_is_force_updated(void)
{
git_index *index;
git_reference *head;
git_object *obj;
if (!cl_is_chmod_supported())
clar__skip();
assert_on_branch(g_repo, "master");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_repository_head(&head, g_repo));
cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));
assert_status_entrycount(g_repo, 0);
/* update the mode on-disk */
cl_must_pass(p_chmod("testrepo/README", 0755));
assert_status_entrycount(g_repo, 1);
cl_git_pass(git_checkout_tree(g_repo, obj, &g_opts));
assert_status_entrycount(g_repo, 0);
/* update the mode on-disk and in the index */
cl_must_pass(p_chmod("testrepo/README", 0755));
cl_must_pass(git_index_add_bypath(index, "README"));
cl_git_pass(git_index_write(index));
assert_status_entrycount(g_repo, 1);
cl_git_pass(git_checkout_tree(g_repo, obj, &g_opts));
cl_git_pass(git_index_write(index));
assert_status_entrycount(g_repo, 0);
git_object_free(obj);
git_reference_free(head);
git_index_free(index);
}
void test_checkout_tree__nullopts(void)
{
cl_git_pass(git_checkout_tree(g_repo, NULL, NULL));
}
static void modify_index_ondisk(void)
{
git_repository *other_repo;
git_index *other_index;
git_index_entry entry = {{0}};
cl_git_pass(git_repository_open(&other_repo, git_repository_workdir(g_repo)));
cl_git_pass(git_repository_index(&other_index, other_repo));
cl_git_pass(git_oid__fromstr(&entry.id, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1));
entry.mode = 0100644;
entry.path = "README";
cl_git_pass(git_index_add(other_index, &entry));
cl_git_pass(git_index_write(other_index));
git_index_free(other_index);
git_repository_free(other_repo);
}
static void modify_index_and_checkout_tree(git_checkout_options *opts)
{
git_index *index;
git_reference *head;
git_object *obj;
/* External changes to the index are maintained by default */
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_repository_head(&head, g_repo));
cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));
assert_status_entrycount(g_repo, 0);
modify_index_ondisk();
/* The file in the index remains modified */
cl_git_pass(git_checkout_tree(g_repo, obj, opts));
git_object_free(obj);
git_reference_free(head);
git_index_free(index);
}
void test_checkout_tree__retains_external_index_changes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
modify_index_and_checkout_tree(&opts);
assert_status_entrycount(g_repo, 1);
}
void test_checkout_tree__no_index_refresh(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_NO_REFRESH;
modify_index_and_checkout_tree(&opts);
assert_status_entrycount(g_repo, 0);
}
void test_checkout_tree__dry_run(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_oid oid;
git_object *obj = NULL;
checkout_counts ct;
/* first let's get things into a known state - by checkout out the HEAD */
assert_on_branch(g_repo, "master");
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_fs_path_isdir("testrepo/a"));
check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
/* now checkout branch but with dry run enabled */
memset(&ct, 0, sizeof(ct));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DRY_RUN;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &ct;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
assert_on_branch(g_repo, "dir");
/* these normally would have been created and updated, but with
* DRY_RUN they will be unchanged.
*/
cl_assert(!git_fs_path_isdir("testrepo/a"));
check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
/* check that notify callback was invoked */
cl_assert_equal_i(ct.n_updates, 2);
git_object_free(obj);
}
| libgit2-main | tests/libgit2/checkout/tree.c |
#include "describe_helpers.h"
#include "wildmatch.h"
void assert_describe(
const char *expected_output,
const char *revparse_spec,
git_repository *repo,
git_describe_options *opts,
git_describe_format_options *fmt_opts)
{
git_object *object;
git_buf label = GIT_BUF_INIT;
git_describe_result *result;
cl_git_pass(git_revparse_single(&object, repo, revparse_spec));
cl_git_pass(git_describe_commit(&result, object, opts));
cl_git_pass(git_describe_format(&label, result, fmt_opts));
cl_must_pass(wildmatch(expected_output, label.ptr, 0));
git_describe_result_free(result);
git_object_free(object);
git_buf_dispose(&label);
}
void assert_describe_workdir(
const char *expected_output,
git_repository *repo,
git_describe_options *opts,
git_describe_format_options *fmt_opts)
{
git_buf label = GIT_BUF_INIT;
git_describe_result *result;
cl_git_pass(git_describe_workdir(&result, repo, opts));
cl_git_pass(git_describe_format(&label, result, fmt_opts));
cl_must_pass(wildmatch(expected_output, label.ptr, 0));
git_describe_result_free(result);
git_buf_dispose(&label);
}
| libgit2-main | tests/libgit2/describe/describe_helpers.c |
#include "clar_libgit2.h"
#include "describe_helpers.h"
#include "repository.h"
/* Ported from https://github.com/git/git/blob/adfc1857bdb090786fd9d22c1acec39371c76048/t/t6120-describe.sh */
static git_repository *repo;
void test_describe_t6120__initialize(void)
{
repo = cl_git_sandbox_init("describe");
}
void test_describe_t6120__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_describe_t6120__default(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
assert_describe("A-*", "HEAD", repo, &opts, &fmt_opts);
assert_describe("A-*", "HEAD^", repo, &opts, &fmt_opts);
assert_describe("R-*", "HEAD^^", repo, &opts, &fmt_opts);
assert_describe("A-*", "HEAD^^2", repo, &opts, &fmt_opts);
assert_describe("B", "HEAD^^2^", repo, &opts, &fmt_opts);
assert_describe("R-*", "HEAD^^^", repo, &opts, &fmt_opts);
}
void test_describe_t6120__tags(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
opts.describe_strategy = GIT_DESCRIBE_TAGS;
assert_describe("c-*", "HEAD", repo, &opts, &fmt_opts);
assert_describe("c-*", "HEAD^", repo, &opts, &fmt_opts);
assert_describe("e-*", "HEAD^^", repo, &opts, &fmt_opts);
assert_describe("c-*", "HEAD^^2", repo, &opts, &fmt_opts);
assert_describe("B", "HEAD^^2^", repo, &opts, &fmt_opts);
assert_describe("e", "HEAD^^^", repo, &opts, &fmt_opts);
}
void test_describe_t6120__all(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
opts.describe_strategy = GIT_DESCRIBE_ALL;
assert_describe("heads/master", "HEAD", repo, &opts, &fmt_opts);
assert_describe("tags/c-*", "HEAD^", repo, &opts, &fmt_opts);
assert_describe("tags/e", "HEAD^^^", repo, &opts, &fmt_opts);
}
void test_describe_t6120__longformat(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
fmt_opts.always_use_long_format = 1;
assert_describe("B-0-*", "HEAD^^2^", repo, &opts, &fmt_opts);
assert_describe("A-3-*", "HEAD^^2", repo, &opts, &fmt_opts);
}
void test_describe_t6120__firstparent(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
opts.describe_strategy = GIT_DESCRIBE_TAGS;
assert_describe("c-7-*", "HEAD", repo, &opts, &fmt_opts);
opts.only_follow_first_parent = 1;
assert_describe("e-3-*", "HEAD", repo, &opts, &fmt_opts);
}
void test_describe_t6120__workdir(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
assert_describe_workdir("A-*[0-9a-f]", repo, &opts, &fmt_opts);
cl_git_mkfile("describe/file", "something different");
fmt_opts.dirty_suffix = "-dirty";
assert_describe_workdir("A-*[0-9a-f]-dirty", repo, &opts, &fmt_opts);
fmt_opts.dirty_suffix = ".mod";
assert_describe_workdir("A-*[0-9a-f].mod", repo, &opts, &fmt_opts);
}
static void commit_and_tag(
git_time_t *time,
const char *commit_msg,
const char *tag_name)
{
git_index *index;
git_oid commit_id;
git_reference *ref;
cl_git_pass(git_repository_index__weakptr(&index, repo));
cl_git_append2file("describe/file", "\n");
cl_git_pass(git_index_add_bypath(index, "file"));
cl_git_pass(git_index_write(index));
*time += 10;
cl_repo_commit_from_index(&commit_id, repo, NULL, *time, commit_msg);
if (tag_name == NULL)
return;
cl_git_pass(git_reference_create(&ref, repo, tag_name, &commit_id, 0, NULL));
git_reference_free(ref);
}
void test_describe_t6120__pattern(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
git_oid tag_id;
git_object *head;
git_signature *tagger;
git_time_t time;
/* set-up matching pattern tests */
cl_git_pass(git_revparse_single(&head, repo, "HEAD"));
time = 1380553019;
cl_git_pass(git_signature_new(&tagger, "tagger", "[email protected]", time, 0));
cl_git_pass(git_tag_create(&tag_id, repo, "test-annotated", head, tagger, "test-annotated", 0));
git_signature_free(tagger);
git_object_free(head);
commit_and_tag(&time, "one more", "refs/tags/test1-lightweight");
commit_and_tag(&time, "yet another", "refs/tags/test2-lightweight");
commit_and_tag(&time, "even more", NULL);
/* Exercize */
opts.pattern = "test-*";
assert_describe("test-annotated-*", "HEAD", repo, &opts, &fmt_opts);
opts.describe_strategy = GIT_DESCRIBE_TAGS;
opts.pattern = "test1-*";
assert_describe("test1-lightweight-*", "HEAD", repo, &opts, &fmt_opts);
opts.pattern = "test2-*";
assert_describe("test2-lightweight-*", "HEAD", repo, &opts, &fmt_opts);
fmt_opts.always_use_long_format = 1;
assert_describe("test2-lightweight-*", "HEAD^", repo, &opts, &fmt_opts);
}
| libgit2-main | tests/libgit2/describe/t6120.c |
#include "clar_libgit2.h"
#include "describe_helpers.h"
void test_describe_describe__can_describe_against_a_bare_repo(void)
{
git_repository *repo;
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
assert_describe("hard_tag", "HEAD", repo, &opts, &fmt_opts);
opts.show_commit_oid_as_fallback = 1;
assert_describe("be3563a*", "HEAD^", repo, &opts, &fmt_opts);
git_repository_free(repo);
}
static int delete_cb(git_reference *ref, void *payload)
{
GIT_UNUSED(payload);
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
return 0;
}
void test_describe_describe__describe_a_repo_with_no_refs(void)
{
git_repository *repo;
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_str buf = GIT_STR_INIT;
git_object *object;
git_describe_result *result = NULL;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_revparse_single(&object, repo, "HEAD"));
cl_git_pass(git_reference_foreach(repo, delete_cb, NULL));
/* Impossible to describe without falling back to OIDs */
cl_git_fail(git_describe_commit(&result, object, &opts));
/* Try again with OID fallbacks */
opts.show_commit_oid_as_fallback = 1;
cl_git_pass(git_describe_commit(&result, object, &opts));
git_describe_result_free(result);
git_object_free(object);
git_str_dispose(&buf);
cl_git_sandbox_cleanup();
}
| libgit2-main | tests/libgit2/describe/describe.c |
#include "clar_libgit2.h"
/*
* a4a7dce [0] Merge branch 'master' into br2
|\
| * 9fd738e [1] a fourth commit
| * 4a202b3 [2] a third commit
* | c47800c [3] branch commit one
|/
* 5b5b025 [5] another commit
* 8496071 [4] testing
*/
static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
static const char *commit_ids[] = {
"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
};
/* Careful: there are two possible topological sorts */
static const int commit_sorting_topo[][6] = {
{0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4}
};
static const int commit_sorting_time[][6] = {
{0, 3, 1, 2, 5, 4}
};
static const int commit_sorting_topo_reverse[][6] = {
{4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
};
static const int commit_sorting_time_reverse[][6] = {
{4, 5, 2, 1, 3, 0}
};
/* This is specified unsorted, so both combinations are possible */
static const int commit_sorting_segment[][6] = {
{1, 2, -1, -1, -1, -1}, {2, 1, -1, -1, -1, -1}
};
#define commit_count 6
static const int result_bytes = 24;
static int get_commit_index(git_oid *raw_oid)
{
int i;
char oid[GIT_OID_SHA1_HEXSIZE];
git_oid_fmt(oid, raw_oid);
for (i = 0; i < commit_count; ++i)
if (memcmp(oid, commit_ids[i], GIT_OID_SHA1_HEXSIZE) == 0)
return i;
return -1;
}
static int test_walk_only(git_revwalk *walk,
const int possible_results[][commit_count], int results_count)
{
git_oid oid;
int i;
int result_array[commit_count];
for (i = 0; i < commit_count; ++i)
result_array[i] = -1;
i = 0;
while (git_revwalk_next(&oid, walk) == 0) {
result_array[i++] = get_commit_index(&oid);
/*{
char str[GIT_OID_SHA1_HEXSIZE+1];
git_oid_fmt(str, &oid);
str[GIT_OID_SHA1_HEXSIZE] = 0;
printf(" %d) %s\n", i, str);
}*/
}
for (i = 0; i < results_count; ++i)
if (memcmp(possible_results[i],
result_array, result_bytes) == 0)
return 0;
return GIT_ERROR;
}
static int test_walk(git_revwalk *walk, const git_oid *root,
int flags, const int possible_results[][6], int results_count)
{
git_revwalk_sorting(walk, flags);
git_revwalk_push(walk, root);
return test_walk_only(walk, possible_results, results_count);
}
static git_repository *_repo = NULL;
static git_revwalk *_walk = NULL;
static const char *_fixture = NULL;
void test_revwalk_basic__initialize(void)
{
}
void test_revwalk_basic__cleanup(void)
{
git_revwalk_free(_walk);
if (_fixture)
cl_git_sandbox_cleanup();
else
git_repository_free(_repo);
_fixture = NULL;
_repo = NULL;
_walk = NULL;
}
static void revwalk_basic_setup_walk(const char *fixture)
{
if (fixture) {
_fixture = fixture;
_repo = cl_git_sandbox_init(fixture);
} else {
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
}
cl_git_pass(git_revwalk_new(&_walk, _repo));
}
void test_revwalk_basic__sorting_modes(void)
{
git_oid id;
revwalk_basic_setup_walk(NULL);
git_oid__fromstr(&id, commit_head, GIT_OID_SHA1);
cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME, commit_sorting_time, 1));
cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2));
cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1));
cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2));
}
void test_revwalk_basic__glob_heads(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_revwalk_push_glob(_walk, "heads"));
while (git_revwalk_next(&oid, _walk) == 0)
i++;
/* git log --branches --oneline | wc -l => 14 */
cl_assert_equal_i(i, 14);
}
void test_revwalk_basic__glob_heads_with_invalid(void)
{
int i;
git_oid oid;
revwalk_basic_setup_walk("testrepo");
cl_git_mkfile("testrepo/.git/refs/heads/garbage", "not-a-ref");
cl_git_pass(git_revwalk_push_glob(_walk, "heads"));
for (i = 0; !git_revwalk_next(&oid, _walk); ++i)
/* walking */;
/* git log --branches --oneline | wc -l => 16 */
cl_assert_equal_i(20, i);
}
void test_revwalk_basic__push_head(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_revwalk_push_head(_walk));
while (git_revwalk_next(&oid, _walk) == 0) {
i++;
}
/* git log HEAD --oneline | wc -l => 7 */
cl_assert_equal_i(i, 7);
}
void test_revwalk_basic__sorted_after_reset(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
git_oid__fromstr(&oid, commit_head, GIT_OID_SHA1);
/* push, sort, and test the walk */
cl_git_pass(git_revwalk_push(_walk, &oid));
git_revwalk_sorting(_walk, GIT_SORT_TIME);
cl_git_pass(test_walk_only(_walk, commit_sorting_time, 2));
/* reset, push, and test again - we should see all entries */
git_revwalk_reset(_walk);
cl_git_pass(git_revwalk_push(_walk, &oid));
while (git_revwalk_next(&oid, _walk) == 0)
i++;
cl_assert_equal_i(i, commit_count);
}
void test_revwalk_basic__push_head_hide_ref(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_revwalk_push_head(_walk));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
while (git_revwalk_next(&oid, _walk) == 0) {
i++;
}
/* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */
cl_assert_equal_i(i, 4);
}
void test_revwalk_basic__push_head_hide_ref_nobase(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_revwalk_push_head(_walk));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed"));
while (git_revwalk_next(&oid, _walk) == 0) {
i++;
}
/* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */
cl_assert_equal_i(i, 7);
}
/*
* $ git rev-list HEAD 5b5b02 ^refs/heads/packed-test
* a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* be3563ae3f795b2b4353bcce3a527ad0a4f7f644
* c47800c7266a2be04c571c04d5a6614691ea99bd
* 9fd738e8f7967c078dceed8190330fc8648ee56a
* $ git log HEAD 5b5b02 --oneline --not refs/heads/packed-test | wc -l => 4
* a65fedf
* be3563a Merge branch 'br2'
* c47800c branch commit one
* 9fd738e a fourth commit
*/
void test_revwalk_basic__multiple_push_1(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_revwalk_push_head(_walk));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
cl_git_pass(git_oid__fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1));
cl_git_pass(git_revwalk_push(_walk, &oid));
while (git_revwalk_next(&oid, _walk) == 0)
i++;
cl_assert_equal_i(i, 4);
}
/*
* Difference between test_revwalk_basic__multiple_push_1 and
* test_revwalk_basic__multiple_push_2 is in the order reference
* refs/heads/packed-test and commit 5b5b02 are pushed.
* revwalk should return same commits in both the tests.
* $ git rev-list 5b5b02 HEAD ^refs/heads/packed-test
* a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* be3563ae3f795b2b4353bcce3a527ad0a4f7f644
* c47800c7266a2be04c571c04d5a6614691ea99bd
* 9fd738e8f7967c078dceed8190330fc8648ee56a
* $ git log 5b5b02 HEAD --oneline --not refs/heads/packed-test | wc -l => 4
* a65fedf
* be3563a Merge branch 'br2'
* c47800c branch commit one
* 9fd738e a fourth commit
*/
void test_revwalk_basic__multiple_push_2(void)
{
int i = 0;
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_oid__fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1));
cl_git_pass(git_revwalk_push(_walk, &oid));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
cl_git_pass(git_revwalk_push_head(_walk));
while (git_revwalk_next(&oid, _walk) == 0)
i++;
cl_assert_equal_i(i, 4);
}
void test_revwalk_basic__disallow_non_commit(void)
{
git_oid oid;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_oid__fromstr(&oid, "521d87c1ec3aef9824daf6d96cc0ae3710766d91", GIT_OID_SHA1));
cl_git_fail(git_revwalk_push(_walk, &oid));
}
void test_revwalk_basic__hide_then_push(void)
{
git_oid oid;
int i = 0;
revwalk_basic_setup_walk(NULL);
cl_git_pass(git_oid__fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1));
cl_git_pass(git_revwalk_hide(_walk, &oid));
cl_git_pass(git_revwalk_push(_walk, &oid));
while (git_revwalk_next(&oid, _walk) == 0)
i++;
cl_assert_equal_i(i, 0);
}
void test_revwalk_basic__topo_crash(void)
{
git_oid oid;
git_oid__fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
revwalk_basic_setup_walk(NULL);
git_revwalk_sorting(_walk, GIT_SORT_TOPOLOGICAL);
cl_git_pass(git_revwalk_push(_walk, &oid));
cl_git_pass(git_revwalk_hide(_walk, &oid));
git_revwalk_next(&oid, _walk);
}
void test_revwalk_basic__from_new_to_old(void)
{
git_oid from_oid, to_oid, oid;
int i = 0;
revwalk_basic_setup_walk(NULL);
git_revwalk_sorting(_walk, GIT_SORT_TIME);
cl_git_pass(git_oid__fromstr(&to_oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&from_oid, "a4a7dce85cf63874e984719f4fdd239f5145052f", GIT_OID_SHA1));
cl_git_pass(git_revwalk_push(_walk, &to_oid));
cl_git_pass(git_revwalk_hide(_walk, &from_oid));
while (git_revwalk_next(&oid, _walk) == 0)
i++;
cl_assert_equal_i(i, 0);
}
void test_revwalk_basic__push_range(void)
{
revwalk_basic_setup_walk(NULL);
git_revwalk_reset(_walk);
git_revwalk_sorting(_walk, 0);
cl_git_pass(git_revwalk_push_range(_walk, "9fd738e~2..9fd738e"));
cl_git_pass(test_walk_only(_walk, commit_sorting_segment, 2));
}
void test_revwalk_basic__push_range_merge_base(void)
{
revwalk_basic_setup_walk(NULL);
git_revwalk_reset(_walk);
git_revwalk_sorting(_walk, 0);
cl_git_fail_with(GIT_EINVALIDSPEC, git_revwalk_push_range(_walk, "HEAD...HEAD~2"));
}
void test_revwalk_basic__push_range_no_range(void)
{
revwalk_basic_setup_walk(NULL);
git_revwalk_reset(_walk);
git_revwalk_sorting(_walk, 0);
cl_git_fail_with(GIT_EINVALIDSPEC, git_revwalk_push_range(_walk, "HEAD"));
}
void test_revwalk_basic__push_mixed(void)
{
git_oid oid;
int i = 0;
revwalk_basic_setup_walk(NULL);
git_revwalk_reset(_walk);
git_revwalk_sorting(_walk, 0);
cl_git_pass(git_revwalk_push_glob(_walk, "tags"));
while (git_revwalk_next(&oid, _walk) == 0) {
i++;
}
/* git rev-list --count --glob=tags #=> 9 */
cl_assert_equal_i(9, i);
}
void test_revwalk_basic__push_all(void)
{
git_oid oid;
int i = 0;
revwalk_basic_setup_walk(NULL);
git_revwalk_reset(_walk);
git_revwalk_sorting(_walk, 0);
cl_git_pass(git_revwalk_push_glob(_walk, "*"));
while (git_revwalk_next(&oid, _walk) == 0) {
i++;
}
/* git rev-list --count --all #=> 15 */
cl_assert_equal_i(15, i);
}
/*
* $ git rev-list br2 master e908
* a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* e90810b8df3e80c413d903f631643c716887138d
* 6dcf9bf7541ee10456529833502442f385010c3d
* a4a7dce85cf63874e984719f4fdd239f5145052f
* be3563ae3f795b2b4353bcce3a527ad0a4f7f644
* c47800c7266a2be04c571c04d5a6614691ea99bd
* 9fd738e8f7967c078dceed8190330fc8648ee56a
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
* 5b5b025afb0b4c913b4c338a42934a3863bf3644
* 8496071c1b46c854b31185ea97743be6a8774479
*/
void test_revwalk_basic__mimic_git_rev_list(void)
{
git_oid oid;
revwalk_basic_setup_walk(NULL);
git_revwalk_sorting(_walk, GIT_SORT_TIME);
cl_git_pass(git_revwalk_push_ref(_walk, "refs/heads/br2"));
cl_git_pass(git_revwalk_push_ref(_walk, "refs/heads/master"));
cl_git_pass(git_oid__fromstr(&oid, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1));
cl_git_pass(git_revwalk_push(_walk, &oid));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "e90810b8df3e80c413d903f631643c716887138d"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "6dcf9bf7541ee10456529833502442f385010c3d"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "8496071c1b46c854b31185ea97743be6a8774479"));
cl_git_fail_with(git_revwalk_next(&oid, _walk), GIT_ITEROVER);
}
void test_revwalk_basic__big_timestamp(void)
{
git_reference *head;
git_commit *tip;
git_signature *sig;
git_tree *tree;
git_oid id;
int error;
revwalk_basic_setup_walk("testrepo.git");
cl_git_pass(git_repository_head(&head, _repo));
cl_git_pass(git_reference_peel((git_object **) &tip, head, GIT_OBJECT_COMMIT));
/* Commit with a far-ahead timestamp, we should be able to parse it in the revwalk */
cl_git_pass(git_signature_new(&sig, "Joe", "[email protected]", INT64_C(2399662595), 0));
cl_git_pass(git_commit_tree(&tree, tip));
cl_git_pass(git_commit_create(&id, _repo, "HEAD", sig, sig, NULL, "some message", tree, 1,
(const git_commit **)&tip));
cl_git_pass(git_revwalk_push_head(_walk));
while ((error = git_revwalk_next(&id, _walk)) == 0) {
/* nothing */
}
cl_assert_equal_i(GIT_ITEROVER, error);
git_tree_free(tree);
git_commit_free(tip);
git_reference_free(head);
git_signature_free(sig);
}
/* Ensure that we correctly hide a commit that is (timewise) older
* than the commits that we are showing.
*
* % git rev-list 8e73b76..bd75801
* bd758010071961f28336333bc41e9c64c9a64866
*/
void test_revwalk_basic__old_hidden_commit_one(void)
{
git_oid new_id, old_id, oid;
revwalk_basic_setup_walk("testrepo.git");
cl_git_pass(git_oid__fromstr(&new_id, "bd758010071961f28336333bc41e9c64c9a64866", GIT_OID_SHA1));
cl_git_pass(git_revwalk_push(_walk, &new_id));
cl_git_pass(git_oid__fromstr(&old_id, "8e73b769e97678d684b809b163bebdae2911720f", GIT_OID_SHA1));
cl_git_pass(git_revwalk_hide(_walk, &old_id));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "bd758010071961f28336333bc41e9c64c9a64866"));
cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid, _walk));
}
/* Ensure that we correctly hide a commit that is (timewise) older
* than the commits that we are showing.
*
* % git rev-list bd75801 ^b91e763
* bd758010071961f28336333bc41e9c64c9a64866
*/
void test_revwalk_basic__old_hidden_commit_two(void)
{
git_oid new_id, old_id, oid;
revwalk_basic_setup_walk("testrepo.git");
cl_git_pass(git_oid__fromstr(&new_id, "bd758010071961f28336333bc41e9c64c9a64866", GIT_OID_SHA1));
cl_git_pass(git_revwalk_push(_walk, &new_id));
cl_git_pass(git_oid__fromstr(&old_id, "b91e763008b10db366442469339f90a2b8400d0a", GIT_OID_SHA1));
cl_git_pass(git_revwalk_hide(_walk, &old_id));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(!git_oid_streq(&oid, "bd758010071961f28336333bc41e9c64c9a64866"));
cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid, _walk));
}
/*
* Ensure that we correctly hide all parent commits of a newer
* commit when first hiding older commits.
*
* % git rev-list D ^B ^A ^E
* 790ba0facf6fd103699a5c40cd19dad277ff49cd
* b82cee5004151ae0c4f82b69fb71b87477664b6f
*/
void test_revwalk_basic__newer_hidden_commit_hides_old_commits(void)
{
git_oid oid;
revwalk_basic_setup_walk("revwalk.git");
cl_git_pass(git_revwalk_push_ref(_walk, "refs/heads/D"));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/B"));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/A"));
cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/E"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(git_oid_streq(&oid, "b82cee5004151ae0c4f82b69fb71b87477664b6f"));
cl_git_pass(git_revwalk_next(&oid, _walk));
cl_assert(git_oid_streq(&oid, "790ba0facf6fd103699a5c40cd19dad277ff49cd"));
cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid, _walk));
}
| libgit2-main | tests/libgit2/revwalk/basic.c |
#include "clar_libgit2.h"
void test_revwalk_simplify__cleanup(void)
{
cl_git_sandbox_cleanup();
}
/*
* a4a7dce [0] Merge branch 'master' into br2
|\
| * 9fd738e [1] a fourth commit
| * 4a202b3 [2] a third commit
* | c47800c [3] branch commit one
|/
* 5b5b025 [5] another commit
* 8496071 [4] testing
*/
static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
static const char *expected_str[] = {
"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 4 */
"8496071c1b46c854b31185ea97743be6a8774479", /* 5 */
};
void test_revwalk_simplify__first_parent(void)
{
git_repository *repo;
git_revwalk *walk;
git_oid id, expected[4];
int i, error;
for (i = 0; i < 4; i++) {
git_oid__fromstr(&expected[i], expected_str[i], GIT_OID_SHA1);
}
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_revwalk_new(&walk, repo));
git_oid__fromstr(&id, commit_head, GIT_OID_SHA1);
cl_git_pass(git_revwalk_push(walk, &id));
git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
git_revwalk_simplify_first_parent(walk);
i = 0;
while ((error = git_revwalk_next(&id, walk)) == 0) {
cl_assert_equal_oid(&expected[i], &id);
i++;
}
cl_assert_equal_i(i, 4);
cl_assert_equal_i(error, GIT_ITEROVER);
git_revwalk_free(walk);
}
| libgit2-main | tests/libgit2/revwalk/simplify.c |
#include "clar_libgit2.h"
/*
* a4a7dce [0] Merge branch 'master' into br2
|\
| * 9fd738e [1] a fourth commit
| * 4a202b3 [2] a third commit
* | c47800c [3] branch commit one
|/
* 5b5b025 [5] another commit
* 8496071 [4] testing
*/
static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
static const char *commit_strs[] = {
"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
};
#define commit_count 6
static git_oid commit_ids[commit_count];
static git_oid _head_id;
static git_repository *_repo;
void test_revwalk_hidecb__initialize(void)
{
int i;
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_oid__fromstr(&_head_id, commit_head, GIT_OID_SHA1));
for (i = 0; i < commit_count; i++)
cl_git_pass(git_oid__fromstr(&commit_ids[i], commit_strs[i], GIT_OID_SHA1));
}
void test_revwalk_hidecb__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
}
/* Hide all commits */
static int hide_every_commit_cb(const git_oid *commit_id, void *data)
{
GIT_UNUSED(commit_id);
GIT_UNUSED(data);
return 1;
}
/* Do not hide anything */
static int hide_none_cb(const git_oid *commit_id, void *data)
{
GIT_UNUSED(commit_id);
GIT_UNUSED(data);
return 0;
}
/* Hide some commits */
static int hide_commit_cb(const git_oid *commit_id, void *data)
{
GIT_UNUSED(commit_id);
GIT_UNUSED(data);
return (git_oid_cmp(commit_id, &commit_ids[5]) == 0);
}
/* In payload data, pointer to a commit id is passed */
static int hide_commit_use_payload_cb(const git_oid *commit_id, void *data)
{
git_oid *hide_commit_id = data;
return (git_oid_cmp(commit_id, hide_commit_id) == 0);
}
void test_revwalk_hidecb__hide_all_cb(void)
{
git_revwalk *walk;
git_oid id;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
cl_git_pass(git_revwalk_push(walk, &_head_id));
/* First call to git_revwalk_next should return GIT_ITEROVER */
cl_assert_equal_i(GIT_ITEROVER, git_revwalk_next(&id, walk));
git_revwalk_free(walk);
}
void test_revwalk_hidecb__hide_none_cb(void)
{
git_revwalk *walk;
int i, error;
git_oid id;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
cl_git_pass(git_revwalk_push(walk, &_head_id));
/* It should return all 6 commits */
i = 0;
while ((error = git_revwalk_next(&id, walk)) == 0)
i++;
cl_assert_equal_i(i, 6);
cl_assert_equal_i(error, GIT_ITEROVER);
git_revwalk_free(walk);
}
void test_revwalk_hidecb__unset_cb_before_walk(void)
{
git_revwalk *walk;
git_oid id;
int i, error;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
cl_git_pass(git_revwalk_add_hide_cb(walk, NULL, NULL));
cl_git_pass(git_revwalk_push(walk, &_head_id));
/* It should return all 6 commits */
i = 0;
while ((error = git_revwalk_next(&id, walk)) == 0)
i++;
cl_assert_equal_i(i, 6);
cl_assert_equal_i(error, GIT_ITEROVER);
git_revwalk_free(walk);
}
void test_revwalk_hidecb__change_cb_before_walk(void)
{
git_revwalk *walk;
git_oid id;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_every_commit_cb, NULL));
cl_git_pass(git_revwalk_push(walk, &_head_id));
/* First call to git_revwalk_next should return GIT_ITEROVER */
cl_assert_equal_i(GIT_ITEROVER, git_revwalk_next(&id, walk));
git_revwalk_free(walk);
}
void test_revwalk_hidecb__add_hide_cb_during_walking(void)
{
git_revwalk *walk;
git_oid id;
int error;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_push(walk, &_head_id));
/* Start walking without adding hide callback */
cl_git_pass(git_revwalk_next(&id, walk));
/* Now add hide callback */
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_none_cb, NULL));
/* walk should be reset */
error = git_revwalk_next(&id, walk);
cl_assert_equal_i(error, GIT_ITEROVER);
git_revwalk_free(walk);
}
void test_revwalk_hidecb__hide_some_commits(void)
{
git_revwalk *walk;
git_oid id;
int i, error;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_push(walk, &_head_id));
git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
/* Add hide callback */
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_cb, NULL));
i = 0;
while ((error = git_revwalk_next(&id, walk)) == 0) {
cl_assert_equal_oid(&commit_ids[i], &id);
i++;
}
cl_assert_equal_i(i, 4);
cl_assert_equal_i(error, GIT_ITEROVER);
git_revwalk_free(walk);
}
void test_revwalk_hidecb__test_payload(void)
{
git_revwalk *walk;
git_oid id;
int i, error;
cl_git_pass(git_revwalk_new(&walk, _repo));
cl_git_pass(git_revwalk_push(walk, &_head_id));
git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL);
/* Add hide callback, pass id of parent of initial commit as payload data */
cl_git_pass(git_revwalk_add_hide_cb(walk, hide_commit_use_payload_cb, &commit_ids[5]));
i = 0;
while ((error = git_revwalk_next(&id, walk)) == 0) {
cl_assert_equal_oid(&commit_ids[i], &id);
i++;
}
/* walker should return four commits */
cl_assert_equal_i(i, 4);
cl_assert_equal_i(error, GIT_ITEROVER);
git_revwalk_free(walk);
}
| libgit2-main | tests/libgit2/revwalk/hidecb.c |
#include "clar_libgit2.h"
static git_repository *_repo;
static git_revwalk *_walk;
void test_revwalk_signatureparsing__initialize(void)
{
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_revwalk_new(&_walk, _repo));
}
void test_revwalk_signatureparsing__cleanup(void)
{
git_revwalk_free(_walk);
_walk = NULL;
git_repository_free(_repo);
_repo = NULL;
}
void test_revwalk_signatureparsing__do_not_choke_when_name_contains_angle_brackets(void)
{
git_reference *ref;
git_oid commit_oid;
git_commit *commit;
const git_signature *signature;
/*
* The branch below points at a commit with angle brackets in the committer/author name
* committer <Yu V. Bin Haacked> <[email protected]> 1323847743 +0100
*/
cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/haacked"));
git_revwalk_push(_walk, git_reference_target(ref));
cl_git_pass(git_revwalk_next(&commit_oid, _walk));
cl_git_pass(git_commit_lookup(&commit, _repo, git_reference_target(ref)));
signature = git_commit_committer(commit);
cl_assert_equal_s("[email protected]", signature->email);
cl_assert_equal_s("Yu V. Bin Haacked", signature->name);
cl_assert_equal_i(1323847743, (int)signature->when.time);
cl_assert_equal_i(60, signature->when.offset);
git_commit_free(commit);
git_reference_free(ref);
}
| libgit2-main | tests/libgit2/revwalk/signatureparsing.c |
#include "clar_libgit2.h"
#include "vector.h"
#include <stdarg.h>
static git_repository *_repo;
static git_repository *_repo2;
void test_revwalk_mergebase__initialize(void)
{
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_open(&_repo2, cl_fixture("twowaymerge.git")));
}
void test_revwalk_mergebase__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
git_repository_free(_repo2);
_repo2 = NULL;
}
void test_revwalk_mergebase__single1(void)
{
git_oid result, one, two, expected;
size_t ahead, behind;
cl_git_pass(git_oid__fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd ", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(ahead, 1);
cl_assert_equal_sz(behind, 2);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
cl_assert_equal_sz(ahead, 2);
cl_assert_equal_sz(behind, 1);
}
void test_revwalk_mergebase__single2(void)
{
git_oid result, one, two, expected;
size_t ahead, behind;
cl_git_pass(git_oid__fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(ahead, 1);
cl_assert_equal_sz(behind, 4);
cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &two, &one));
cl_assert_equal_sz(ahead, 4);
cl_assert_equal_sz(behind, 1);
}
void test_revwalk_mergebase__merged_branch(void)
{
git_oid result, one, two, expected;
size_t ahead, behind;
cl_git_pass(git_oid__fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_merge_base(&result, _repo, &two, &one));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(ahead, 3);
cl_assert_equal_sz(behind, 0);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
cl_assert_equal_sz(ahead, 0);
cl_assert_equal_sz(behind, 3);
}
void test_revwalk_mergebase__two_way_merge(void)
{
git_oid one, two;
size_t ahead, behind;
cl_git_pass(git_oid__fromstr(&one, "9b219343610c88a1187c996d0dc58330b55cee28", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "a953a018c5b10b20c86e69fef55ebc8ad4c5a417", GIT_OID_SHA1));
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &one, &two));
cl_assert_equal_sz(ahead, 8);
cl_assert_equal_sz(behind, 2);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &two, &one));
cl_assert_equal_sz(ahead, 2);
cl_assert_equal_sz(behind, 8);
}
void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
{
git_oid result, one, two;
size_t ahead, behind;
int error;
cl_git_pass(git_oid__fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1));
error = git_merge_base(&result, _repo, &one, &two);
cl_git_fail(error);
cl_assert_equal_i(GIT_ENOTFOUND, error);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(4, ahead);
cl_assert_equal_sz(2, behind);
}
void test_revwalk_mergebase__prefer_youngest_merge_base(void)
{
git_oid result, one, two, expected;
cl_git_pass(git_oid__fromstr(&one, "a4a7dce85cf63874e984719f4fdd239f5145052f", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
}
void test_revwalk_mergebase__multiple_merge_bases(void)
{
git_oid one, two, expected1, expected2;
git_oidarray result = {NULL, 0};
cl_git_pass(git_oid__fromstr(&one, "a4a7dce85cf63874e984719f4fdd239f5145052f", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected1, "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected2, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1));
cl_git_pass(git_merge_bases(&result, _repo, &one, &two));
cl_assert_equal_i(2, result.count);
cl_assert_equal_oid(&expected1, &result.ids[0]);
cl_assert_equal_oid(&expected2, &result.ids[1]);
git_oidarray_dispose(&result);
}
void test_revwalk_mergebase__multiple_merge_bases_many_commits(void)
{
git_oid expected1, expected2;
git_oidarray result = {NULL, 0};
git_oid *input = git__malloc(sizeof(git_oid) * 2);
cl_git_pass(git_oid__fromstr(&input[0], "a4a7dce85cf63874e984719f4fdd239f5145052f", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&input[1], "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected1, "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected2, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1));
cl_git_pass(git_merge_bases_many(&result, _repo, 2, input));
cl_assert_equal_i(2, result.count);
cl_assert_equal_oid(&expected1, &result.ids[0]);
cl_assert_equal_oid(&expected2, &result.ids[1]);
git_oidarray_dispose(&result);
git__free(input);
}
void test_revwalk_mergebase__no_off_by_one_missing(void)
{
git_oid result, one, two;
cl_git_pass(git_oid__fromstr(&one, "1a443023183e3f2bfbef8ac923cd81c1018a18fd", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "9f13f7d0a9402c681f91dc590cf7b5470e6a77d2", GIT_OID_SHA1));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
}
static void assert_mergebase_many(const char *expected_sha, int count, ...)
{
va_list ap;
int i;
git_oid *oids;
git_oid oid, expected;
char *partial_oid;
git_object *object;
oids = git__malloc(count * sizeof(git_oid));
cl_assert(oids != NULL);
memset(oids, 0x0, count * sizeof(git_oid));
va_start(ap, count);
for (i = 0; i < count; ++i) {
partial_oid = va_arg(ap, char *);
cl_git_pass(git_oid__fromstrn(&oid, partial_oid, strlen(partial_oid), GIT_OID_SHA1));
cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJECT_COMMIT));
git_oid_cpy(&oids[i], git_object_id(object));
git_object_free(object);
}
va_end(ap);
if (expected_sha == NULL)
cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_many(&oid, _repo, count, oids));
else {
cl_git_pass(git_merge_base_many(&oid, _repo, count, oids));
cl_git_pass(git_oid__fromstr(&expected, expected_sha, GIT_OID_SHA1));
cl_assert_equal_oid(&expected, &oid);
}
git__free(oids);
}
void test_revwalk_mergebase__many_no_common_ancestor_returns_ENOTFOUND(void)
{
assert_mergebase_many(NULL, 3, "41bc8c", "e90810", "a65fed");
assert_mergebase_many(NULL, 3, "e90810", "41bc8c", "a65fed");
assert_mergebase_many(NULL, 3, "e90810", "a65fed", "41bc8c");
assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c");
assert_mergebase_many(NULL, 3, "a65fed", "41bc8c", "e90810");
assert_mergebase_many(NULL, 3, "e90810", "763d71", "a65fed");
}
void test_revwalk_mergebase__many_merge_branch(void)
{
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "e90810", "a65fed");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "a65fed", "e90810");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "849607", "763d71");
assert_mergebase_many("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71");
assert_mergebase_many("5b5b025afb0b4c913b4c338a42934a3863bf3644", 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
}
static void assert_mergebase_octopus(const char *expected_sha, int count, ...)
{
va_list ap;
int i;
git_oid *oids;
git_oid oid, expected;
char *partial_oid;
git_object *object;
oids = git__malloc(count * sizeof(git_oid));
cl_assert(oids != NULL);
memset(oids, 0x0, count * sizeof(git_oid));
va_start(ap, count);
for (i = 0; i < count; ++i) {
partial_oid = va_arg(ap, char *);
cl_git_pass(git_oid__fromstrn(&oid, partial_oid, strlen(partial_oid), GIT_OID_SHA1));
cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJECT_COMMIT));
git_oid_cpy(&oids[i], git_object_id(object));
git_object_free(object);
}
va_end(ap);
if (expected_sha == NULL)
cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_octopus(&oid, _repo, count, oids));
else {
cl_git_pass(git_merge_base_octopus(&oid, _repo, count, oids));
cl_git_pass(git_oid__fromstr(&expected, expected_sha, GIT_OID_SHA1));
cl_assert_equal_oid(&expected, &oid);
}
git__free(oids);
}
void test_revwalk_mergebase__octopus_no_common_ancestor_returns_ENOTFOUND(void)
{
assert_mergebase_octopus(NULL, 3, "41bc8c", "e90810", "a65fed");
assert_mergebase_octopus(NULL, 3, "e90810", "41bc8c", "a65fed");
assert_mergebase_octopus(NULL, 3, "e90810", "a65fed", "41bc8c");
assert_mergebase_octopus(NULL, 3, "a65fed", "e90810", "41bc8c");
assert_mergebase_octopus(NULL, 3, "a65fed", "41bc8c", "e90810");
assert_mergebase_octopus(NULL, 3, "e90810", "763d71", "a65fed");
assert_mergebase_octopus(NULL, 3, "763d71", "e90810", "a65fed");
assert_mergebase_octopus(NULL, 3, "763d71", "a65fed", "e90810");
assert_mergebase_octopus(NULL, 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
}
void test_revwalk_mergebase__octopus_merge_branch(void)
{
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "849607", "763d71");
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71");
}
/*
* testrepo.git $ git log --graph --all
* * commit 763d71aadf09a7951596c9746c024e7eece7c7af
* | Author: nulltoken <[email protected]>
* | Date: Sun Oct 9 12:54:47 2011 +0200
* |
* | Add some files into subdirectories
* |
* | * commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* | | Author: Scott Chacon <[email protected]>
* | | Date: Tue Aug 9 19:33:46 2011 -0700
* | |
* | * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644
* | |\ Merge: 9fd738e c47800c
* | |/ Author: Scott Chacon <[email protected]>
* |/| Date: Tue May 25 11:58:27 2010 -0700
* | |
* | | Merge branch 'br2'
* | |
* | | * commit e90810b8df3e80c413d903f631643c716887138d
* | | | Author: Vicent Marti <[email protected]>
* | | | Date: Thu Aug 5 18:42:20 2010 +0200
* | | |
* | | | Test commit 2
* | | |
* | | * commit 6dcf9bf7541ee10456529833502442f385010c3d
* | | Author: Vicent Marti <[email protected]>
* | | Date: Thu Aug 5 18:41:33 2010 +0200
* | |
* | | Test commit 1
* | |
* | | * commit a4a7dce85cf63874e984719f4fdd239f5145052f
* | | |\ Merge: c47800c 9fd738e
* | |/ / Author: Scott Chacon <[email protected]>
* |/| / Date: Tue May 25 12:00:23 2010 -0700
* | |/
* | | Merge branch 'master' into br2
* | |
* | * commit 9fd738e8f7967c078dceed8190330fc8648ee56a
* | | Author: Scott Chacon <[email protected]>
* | | Date: Mon May 24 10:19:19 2010 -0700
* | |
* | | a fourth commit
* | |
* | * commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
* | | Author: Scott Chacon <[email protected]>
* | | Date: Mon May 24 10:19:04 2010 -0700
* | |
* | | a third commit
* | |
* * | commit c47800c7266a2be04c571c04d5a6614691ea99bd
* |/ Author: Scott Chacon <[email protected]>
* | Date: Tue May 25 11:58:14 2010 -0700
* |
* | branch commit one
* |
* * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
* | Author: Scott Chacon <[email protected]>
* | Date: Tue May 11 13:38:42 2010 -0700
* |
* | another commit
* |
* * commit 8496071c1b46c854b31185ea97743be6a8774479
* Author: Scott Chacon <[email protected]>
* Date: Sat May 8 16:13:06 2010 -0700
*
* testing
*
* * commit 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9
* | Author: Scott Chacon <[email protected]>
* | Date: Tue May 11 13:40:41 2010 -0700
* |
* | packed commit two
* |
* * commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5
* Author: Scott Chacon <[email protected]>
* Date: Tue May 11 13:40:23 2010 -0700
*
* packed commit one
*/
/*
* twowaymerge.git $ git log --graph --all
* * commit 9b219343610c88a1187c996d0dc58330b55cee28
* |\ Merge: c37a783 2224e19
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:31:04 2012 -0800
* | |
* | | Merge branch 'first-branch' into second-branch
* | |
* | * commit 2224e191514cb4bd8c566d80dac22dfcb1e9bb83
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:28:51 2012 -0800
* | |
* | | j
* | |
* | * commit a41a49f8f5cd9b6cb14a076bf8394881ed0b4d19
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:28:39 2012 -0800
* | |
* | | i
* | |
* | * commit 82bf9a1a10a4b25c1f14c9607b60970705e92545
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:28:28 2012 -0800
* | |
* | | h
* | |
* * | commit c37a783c20d92ac92362a78a32860f7eebf938ef
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:30:57 2012 -0800
* | |
* | | n
* | |
* * | commit 8b82fb1794cb1c8c7f172ec730a4c2db0ae3e650
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:30:43 2012 -0800
* | |
* | | m
* | |
* * | commit 6ab5d28acbf3c3bdff276f7ccfdf29c1520e542f
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:30:38 2012 -0800
* | |
* | | l
* | |
* * | commit 7b8c336c45fc6895c1c60827260fe5d798e5d247
* | | Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 20:30:24 2012 -0800
* | |
* | | k
* | |
* | | * commit 1c30b88f5f3ee66d78df6520a7de9e89b890818b
* | | | Author: Scott J. Goldman <[email protected]>
* | | | Date: Tue Nov 27 20:28:10 2012 -0800
* | | |
* | | | e
* | | |
* | | * commit 42b7311aa626e712891940c1ec5d5cba201946a4
* | | | Author: Scott J. Goldman <[email protected]>
* | | | Date: Tue Nov 27 20:28:06 2012 -0800
* | | |
* | | | d
* | | |
* | | * commit a953a018c5b10b20c86e69fef55ebc8ad4c5a417
* | | |\ Merge: bd1732c cdf97fd
* | | |/ Author: Scott J. Goldman <[email protected]>
* | |/| Date: Tue Nov 27 20:26:43 2012 -0800
* | | |
* | | | Merge branch 'first-branch'
* | | |
* | * | commit cdf97fd3bb48eb3827638bb33d208f5fd32d0aa6
* | | | Author: Scott J. Goldman <[email protected]>
* | | | Date: Tue Nov 27 20:24:46 2012 -0800
* | | |
* | | | g
* | | |
* | * | commit ef0488f0b722f0be8bcb90a7730ac7efafd1d694
* | | | Author: Scott J. Goldman <[email protected]>
* | | | Date: Tue Nov 27 20:24:39 2012 -0800
* | | |
* | | | f
* | | |
* | | * commit bd1732c43c68d712ad09e1d872b9be6d4b9efdc4
* | |/ Author: Scott J. Goldman <[email protected]>
* | | Date: Tue Nov 27 17:43:58 2012 -0800
* | |
* | | c
* | |
* | * commit 0c8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29
* |/ Author: Scott J. Goldman <[email protected]>
* | Date: Tue Nov 27 17:43:48 2012 -0800
* |
* | b
* |
* * commit 1f4c0311a24b63f6fc209a59a1e404942d4a5006
* Author: Scott J. Goldman <[email protected]>
* Date: Tue Nov 27 17:43:41 2012 -0800
*
* a
*/
void test_revwalk_mergebase__remove_redundant(void)
{
git_repository *repo;
git_oid one, two, base;
git_oidarray result = {NULL, 0};
cl_git_pass(git_repository_open(&repo, cl_fixture("redundant.git")));
cl_git_pass(git_oid__fromstr(&one, "d89137c93ba1ee749214ff4ce52ae9137bc833f9", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&two, "91f4b95df4a59504a9813ba66912562931d990e3", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&base, "6cb1f2352d974e1c5a776093017e8772416ac97a", GIT_OID_SHA1));
cl_git_pass(git_merge_bases(&result, repo, &one, &two));
cl_assert_equal_i(1, result.count);
cl_assert_equal_oid(&base, &result.ids[0]);
git_oidarray_dispose(&result);
git_repository_free(repo);
}
| libgit2-main | tests/libgit2/revwalk/mergebase.c |
#include "clar_libgit2.h"
#include "futils.h"
static git_repository *repo;
void test_fetch_local__initialize(void)
{
cl_git_pass(git_repository_init(&repo, "./fetch", 0));
}
void test_fetch_local__cleanup(void)
{
git_repository_free(repo);
repo = NULL;
cl_fixture_cleanup("./fetch");
}
void test_fetch_local__defaults(void)
{
git_remote *remote;
git_object *obj;
git_oid expected_id;
cl_git_pass(git_remote_create(&remote, repo, "test",
cl_fixture("testrepo.git")));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
git_oid__fromstr(&expected_id, "258f0e2a959a364e40ed6603d5d44fbb24765b10", GIT_OID_SHA1);
cl_git_pass(git_revparse_single(&obj, repo, "refs/remotes/test/haacked"));
cl_assert_equal_oid(&expected_id, git_object_id(obj));
git_object_free(obj);
git_remote_free(remote);
}
void test_fetch_local__reachable_commit(void)
{
git_remote *remote;
git_strarray refspecs;
git_object *obj;
git_oid expected_id;
git_str fetchhead = GIT_STR_INIT;
char *refspec = "+5b5b025afb0b4c913b4c338a42934a3863bf3644:refs/success";
refspecs.strings = &refspec;
refspecs.count = 1;
git_oid__fromstr(&expected_id, "5b5b025afb0b4c913b4c338a42934a3863bf3644", GIT_OID_SHA1);
cl_git_pass(git_remote_create(&remote, repo, "test",
cl_fixture("testrepo.git")));
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
cl_git_pass(git_revparse_single(&obj, repo, "refs/success"));
cl_assert_equal_oid(&expected_id, git_object_id(obj));
cl_git_pass(git_futils_readbuffer(&fetchhead, "./fetch/.git/FETCH_HEAD"));
cl_assert_equal_strn(fetchhead.ptr,
"5b5b025afb0b4c913b4c338a42934a3863bf3644\t\t'5b5b025afb0b4c913b4c338a42934a3863bf3644' of ",
strlen("5b5b025afb0b4c913b4c338a42934a3863bf3644\t\t'5b5b025afb0b4c913b4c338a42934a3863bf3644' of "));
git_str_dispose(&fetchhead);
git_object_free(obj);
git_remote_free(remote);
}
| libgit2-main | tests/libgit2/fetch/local.c |
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "futils.h"
#include "iterator_helpers.h"
#include "../submodule/submodule_helpers.h"
#include <stdarg.h>
static git_repository *g_repo;
void test_iterator_index__initialize(void)
{
}
void test_iterator_index__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static void index_iterator_test(
const char *sandbox,
const char *start,
const char *end,
git_iterator_flag_t flags,
int expected_count,
const char **expected_names,
const char **expected_oids)
{
git_index *index;
git_iterator *i;
const git_index_entry *entry;
int error, count = 0, caps;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init(sandbox);
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
iter_opts.flags = flags;
iter_opts.start = start;
iter_opts.end = end;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &iter_opts));
while (!(error = git_iterator_advance(&entry, i))) {
cl_assert(entry);
if (expected_names != NULL)
cl_assert_equal_s(expected_names[count], entry->path);
if (expected_oids != NULL) {
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, expected_oids[count], GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
count++;
}
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert(!entry);
cl_assert_equal_i(expected_count, count);
git_iterator_free(i);
cl_assert(caps == git_index_caps(index));
git_index_free(index);
}
static const char *expected_index_0[] = {
"attr0",
"attr1",
"attr2",
"attr3",
"binfile",
"gitattributes",
"macro_bad",
"macro_test",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
"sub/abc",
"sub/file",
"sub/sub/file",
"sub/sub/subsub.txt",
"sub/subdir_test1",
"sub/subdir_test2.txt",
"subdir/.gitattributes",
"subdir/abc",
"subdir/subdir_test1",
"subdir/subdir_test2.txt",
"subdir2/subdir2_test1",
};
static const char *expected_index_oids_0[] = {
"556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3",
"3b74db7ab381105dc0d28f8295a77f6a82989292",
"2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2",
"c485abe35abd4aa6fd83b076a78bbea9e2e7e06c",
"d800886d9c86731ae5c4a62b0b77c437015e00d2",
"2b40c5aca159b04ea8d20ffe36cdf8b09369b14a",
"5819a185d77b03325aaf87cafc771db36f6ddca7",
"ff69f8639ce2e6010b3f33a74160aad98b48da2b",
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
"a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"9e5bdc47d6a80f2be0ea3049ad74231b94609242",
"e563cf4758f0d646f1b14b76016aa17fa9e549a4",
"fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
"99eae476896f4907224978b88e5ecaa6c5bb67a9",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"e563cf4758f0d646f1b14b76016aa17fa9e549a4",
"fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
"dccada462d3df8ac6de596fb8c896aba9344f941"
};
void test_iterator_index__0(void)
{
index_iterator_test(
"attr", NULL, NULL, 0, ARRAY_SIZE(expected_index_0),
expected_index_0, expected_index_oids_0);
}
static const char *expected_index_1[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_new_file",
"staged_new_file_deleted_file",
"staged_new_file_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
};
static const char* expected_index_oids_1[] = {
"a0de7e0ac200c489c41c59dfa910154a70264e6e",
"5452d32f1dd538eb0405e8a83cc185f79e25e80f",
"452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
"55d316c9ba708999f1918e9677d01dfcae69c6b9",
"a6be623522ce87a1d862128ac42672604f7b468b",
"906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8",
"529a16e8e762d4acb7b9636ff540a00831f9155a",
"90b8c29d8ba39434d1c63e1b093daaa26e5bd972",
"ed062903b8f6f3dccb2fa81117ba6590944ef9bd",
"e8ee89e15bbe9b20137715232387b3de5b28972e",
"53ace0d1cc1145a5f4fe4f78a186a60263190733",
"1888c805345ba265b0ee9449b8877b6064592058",
"a6191982709b746d5650e93c2acf34ef74e11504"
};
void test_iterator_index__1(void)
{
index_iterator_test(
"status", NULL, NULL, 0, ARRAY_SIZE(expected_index_1),
expected_index_1, expected_index_oids_1);
}
static const char *expected_index_range[] = {
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
};
static const char *expected_index_oids_range[] = {
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
"a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
};
void test_iterator_index__range(void)
{
index_iterator_test(
"attr", "root", "root", 0, ARRAY_SIZE(expected_index_range),
expected_index_range, expected_index_oids_range);
}
void test_iterator_index__range_empty_0(void)
{
index_iterator_test(
"attr", "empty", "empty", 0, 0, NULL, NULL);
}
void test_iterator_index__range_empty_1(void)
{
index_iterator_test(
"attr", "z_empty_after", NULL, 0, 0, NULL, NULL);
}
void test_iterator_index__range_empty_2(void)
{
index_iterator_test(
"attr", NULL, ".aaa_empty_before", 0, 0, NULL, NULL);
}
static void check_index_range(
git_repository *repo,
const char *start,
const char *end,
bool ignore_case,
int expected_count)
{
git_index *index;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
int error, count, caps;
bool is_ignoring_case;
cl_git_pass(git_repository_index(&index, repo));
caps = git_index_caps(index);
is_ignoring_case = ((caps & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0);
if (ignore_case != is_ignoring_case)
cl_git_pass(git_index_set_caps(index, caps ^ GIT_INDEX_CAPABILITY_IGNORE_CASE));
i_opts.flags = 0;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_index(&i, repo, index, &i_opts));
cl_assert(git_iterator_ignore_case(i) == ignore_case);
for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count)
/* count em up */;
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert_equal_i(expected_count, count);
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__range_icase(void)
{
git_index *index;
git_tree *head;
g_repo = cl_git_sandbox_init("testrepo");
/* reset index to match HEAD */
cl_git_pass(git_repository_head_tree(&head, g_repo));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_read_tree(index, head));
cl_git_pass(git_index_write(index));
git_tree_free(head);
git_index_free(index);
/* do some ranged iterator checks toggling case sensitivity */
check_index_range(g_repo, "B", "C", false, 0);
check_index_range(g_repo, "B", "C", true, 1);
check_index_range(g_repo, "a", "z", false, 3);
check_index_range(g_repo, "a", "z", true, 4);
}
static const char *expected_index_cs[] = {
"B", "D", "F", "H", "J", "L/1", "L/B", "L/D", "L/a", "L/c",
"a", "c", "e", "g", "i", "k/1", "k/B", "k/D", "k/a", "k/c",
};
static const char *expected_index_ci[] = {
"a", "B", "c", "D", "e", "F", "g", "H", "i", "J",
"k/1", "k/a", "k/B", "k/c", "k/D", "L/1", "L/a", "L/B", "L/c", "L/D",
};
void test_iterator_index__case_folding(void)
{
git_str path = GIT_STR_INIT;
int fs_is_ci = 0;
cl_git_pass(git_str_joinpath(&path, cl_fixture("icase"), ".gitted/CoNfIg"));
fs_is_ci = git_fs_path_exists(path.ptr);
git_str_dispose(&path);
index_iterator_test(
"icase", NULL, NULL, 0, ARRAY_SIZE(expected_index_cs),
fs_is_ci ? expected_index_ci : expected_index_cs, NULL);
cl_git_sandbox_cleanup();
index_iterator_test(
"icase", NULL, NULL, GIT_ITERATOR_IGNORE_CASE,
ARRAY_SIZE(expected_index_ci), expected_index_ci, NULL);
cl_git_sandbox_cleanup();
index_iterator_test(
"icase", NULL, NULL, GIT_ITERATOR_DONT_IGNORE_CASE,
ARRAY_SIZE(expected_index_cs), expected_index_cs, NULL);
}
/* Index contents (including pseudotrees):
*
* 0: a 5: F 10: k/ 16: L/
* 1: B 6: g 11: k/1 17: L/1
* 2: c 7: H 12: k/a 18: L/a
* 3: D 8: i 13: k/B 19: L/B
* 4: e 9: J 14: k/c 20: L/c
* 15: k/D 21: L/D
*
* 0: B 5: L/ 11: a 16: k/
* 1: D 6: L/1 12: c 17: k/1
* 2: F 7: L/B 13: e 18: k/B
* 3: H 8: L/D 14: g 19: k/D
* 4: J 9: L/a 15: i 20: k/a
* 10: L/c 21: k/c
*/
void test_iterator_index__icase_0(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
/* autoexpand with no tree entries for index */
cl_git_pass(git_iterator_for_index(&i, g_repo, index, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__icase_1(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
int caps;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
/* force case sensitivity */
cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEX_CAPABILITY_IGNORE_CASE));
/* autoexpand with no tree entries over range */
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* force case insensitivity */
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEX_CAPABILITY_IGNORE_CASE));
/* autoexpand with no tree entries over range */
i_opts.flags = 0;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
cl_git_pass(git_index_set_caps(index, caps));
git_index_free(index);
}
void test_iterator_index__pathlist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
/* Case sensitive */
{
const char *expected[] = {
"B", "D", "L/1", "a", "c", "e", "k/1", "k/a" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Case INsensitive */
{
const char *expected[] = {
"a", "B", "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case sensitive. */
{
const char *expected[] = { "c", "e", "k/1", "k/a" };
size_t expected_len = 4;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case INsensitive. */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 6;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case sensitive. */
{
const char *expected[] = { "B", "D", "L/1", "a", "c", "e" };
size_t expected_len = 6;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case INsensitive. */
{
const char *expected[] = { "a", "B", "c", "D", "e" };
size_t expected_len = 5;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "c", "e", "k/1" };
size_t expected_len = 3;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "k/1" };
size_t expected_len = 1;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a" };
size_t expected_len = 5;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "k/1", "k/a" };
size_t expected_len = 2;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_with_dirs(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
/* Test that a prefix `k` matches folders, even without trailing slash */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that a `k/` matches a folder */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* When the iterator is case sensitive, ensure we can't lookup the
* directory with the wrong case.
*/
{
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
/* Test that case insensitive matching works. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that case insensitive matching works without trailing slash. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_with_dirs_include_trees(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
const char *expected[] = { "k/", "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 6;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_1(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a ==> 5) vs (c e k/1 ==> 3) */
expect = default_icase ? 5 : 3;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_2(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */
expect = default_icase ? 8 : 5;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_four(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist = GIT_VECTOR_INIT;
int default_icase, expect;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "0"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
/* In this test we DO NOT force a case setting on the index. */
default_icase = ((git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0);
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "b";
i_opts.end = "k/D";
/* (c D e k/1 k/a k/B k/c k/D) vs (c e k/1 k/B k/D) */
expect = default_icase ? 8 : 5;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
int caps;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
caps = git_index_caps(index);
/* force case sensitivity */
cl_git_pass(git_index_set_caps(index, caps & ~GIT_INDEX_CAPABILITY_IGNORE_CASE));
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
/* force case insensitivity */
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEX_CAPABILITY_IGNORE_CASE));
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i);
cl_git_pass(git_index_set_caps(index, caps));
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__pathlist_with_directory(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
git_index *index;
g_repo = cl_git_sandbox_init("testrepo2");
git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
git_index_free(index);
git_tree_free(tree);
git_vector_free(&filelist);
}
static void create_paths(git_index *index, const char *root, int depth)
{
git_str fullpath = GIT_STR_INIT;
git_index_entry entry;
size_t root_len;
int i;
if (root) {
cl_git_pass(git_str_puts(&fullpath, root));
cl_git_pass(git_str_putc(&fullpath, '/'));
}
root_len = fullpath.size;
for (i = 0; i < 8; i++) {
bool file = (depth == 0 || (i % 2) == 0);
git_str_truncate(&fullpath, root_len);
cl_git_pass(git_str_printf(&fullpath, "item%d", i));
if (file) {
memset(&entry, 0, sizeof(git_index_entry));
entry.path = fullpath.ptr;
entry.mode = GIT_FILEMODE_BLOB;
git_oid__fromstr(&entry.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6", GIT_OID_SHA1);
cl_git_pass(git_index_add(index, &entry));
} else if (depth > 0) {
create_paths(index, fullpath.ptr, (depth - 1));
}
}
git_str_dispose(&fullpath);
}
void test_iterator_index__pathlist_for_deeply_nested_item(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
create_paths(index, NULL, 3);
/* Ensure that we find the single path we're interested in */
{
const char *expected[] = { "item1/item3/item5/item7" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
{
const char *expected[] = {
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
};
size_t expected_len = 8;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
{
const char *expected[] = {
"item1/item3/item0",
"item1/item3/item1/item0", "item1/item3/item1/item1",
"item1/item3/item1/item2", "item1/item3/item1/item3",
"item1/item3/item1/item4", "item1/item3/item1/item5",
"item1/item3/item1/item6", "item1/item3/item1/item7",
"item1/item3/item2",
"item1/item3/item3/item0", "item1/item3/item3/item1",
"item1/item3/item3/item2", "item1/item3/item3/item3",
"item1/item3/item3/item4", "item1/item3/item3/item5",
"item1/item3/item3/item6", "item1/item3/item3/item7",
"item1/item3/item4",
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
"item1/item3/item6",
"item1/item3/item7/item0", "item1/item3/item7/item1",
"item1/item3/item7/item2", "item1/item3/item7/item3",
"item1/item3/item7/item4", "item1/item3/item7/item5",
"item1/item3/item7/item6", "item1/item3/item7/item7",
};
size_t expected_len = 36;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item0", "item1/item2", "item5/item7/item4", "item6",
"item7/item3/item1/item6" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6"));
cl_git_pass(git_vector_insert(&filelist, "item6"));
cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4"));
cl_git_pass(git_vector_insert(&filelist, "item1/item2"));
cl_git_pass(git_vector_insert(&filelist, "item0"));
/* also add some things that don't exist or don't match the right type */
cl_git_pass(git_vector_insert(&filelist, "item2/"));
cl_git_pass(git_vector_insert(&filelist, "itemN"));
cl_git_pass(git_vector_insert(&filelist, "item1/itemA"));
cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_index_free(index);
git_vector_free(&filelist);
}
void test_iterator_index__advance_over(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
create_paths(index, NULL, 1);
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item0", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item2", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item3/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item5/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item6", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item7/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__advance_into(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_into(i, "B");
expect_advance_into(i, "D");
expect_advance_into(i, "F");
expect_advance_into(i, "H");
expect_advance_into(i, "J");
expect_advance_into(i, "L/");
expect_advance_into(i, "L/1");
expect_advance_into(i, "L/B");
expect_advance_into(i, "L/D");
expect_advance_into(i, "L/a");
expect_advance_into(i, "L/c");
expect_advance_into(i, "a");
expect_advance_into(i, "c");
expect_advance_into(i, "e");
expect_advance_into(i, "g");
expect_advance_into(i, "i");
expect_advance_into(i, "k/");
expect_advance_into(i, "k/1");
expect_advance_into(i, "k/B");
expect_advance_into(i, "k/D");
expect_advance_into(i, "k/a");
expect_advance_into(i, "k/c");
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
void test_iterator_index__advance_into_and_over(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
g_repo = cl_git_sandbox_init("icase");
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_repository_index(&index, g_repo));
create_paths(index, NULL, 2);
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_into(i, "B");
expect_advance_into(i, "D");
expect_advance_into(i, "F");
expect_advance_into(i, "H");
expect_advance_into(i, "J");
expect_advance_into(i, "L/");
expect_advance_into(i, "L/1");
expect_advance_into(i, "L/B");
expect_advance_into(i, "L/D");
expect_advance_into(i, "L/a");
expect_advance_into(i, "L/c");
expect_advance_into(i, "a");
expect_advance_into(i, "c");
expect_advance_into(i, "e");
expect_advance_into(i, "g");
expect_advance_into(i, "i");
expect_advance_into(i, "item0");
expect_advance_into(i, "item1/");
expect_advance_into(i, "item1/item0");
expect_advance_into(i, "item1/item1/");
expect_advance_into(i, "item1/item1/item0");
expect_advance_into(i, "item1/item1/item1");
expect_advance_into(i, "item1/item1/item2");
expect_advance_into(i, "item1/item1/item3");
expect_advance_into(i, "item1/item1/item4");
expect_advance_into(i, "item1/item1/item5");
expect_advance_into(i, "item1/item1/item6");
expect_advance_into(i, "item1/item1/item7");
expect_advance_into(i, "item1/item2");
expect_advance_over(i, "item1/item3/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item5/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item6", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item1/item7/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_into(i, "item2");
expect_advance_over(i, "item3/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item5/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item6", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "item7/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_into(i, "k/");
expect_advance_into(i, "k/1");
expect_advance_into(i, "k/B");
expect_advance_into(i, "k/D");
expect_advance_into(i, "k/a");
expect_advance_into(i, "k/c");
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
static void add_conflict(
git_index *index,
const char *ancestor_path,
const char *our_path,
const char *their_path)
{
git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}};
ancestor.path = ancestor_path;
ancestor.mode = GIT_FILEMODE_BLOB;
git_oid__fromstr(&ancestor.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6", GIT_OID_SHA1);
GIT_INDEX_ENTRY_STAGE_SET(&ancestor, 1);
ours.path = our_path;
ours.mode = GIT_FILEMODE_BLOB;
git_oid__fromstr(&ours.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6", GIT_OID_SHA1);
GIT_INDEX_ENTRY_STAGE_SET(&ours, 2);
theirs.path = their_path;
theirs.mode = GIT_FILEMODE_BLOB;
git_oid__fromstr(&theirs.id, "d44e18fb93b7107b5cd1b95d601591d77869a1b6", GIT_OID_SHA1);
GIT_INDEX_ENTRY_STAGE_SET(&theirs, 3);
cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
}
void test_iterator_index__include_conflicts(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_index *index;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_index(&index, g_repo));
add_conflict(index, "CONFLICT1", "CONFLICT1" ,"CONFLICT1");
add_conflict(index, "ZZZ-CONFLICT2.ancestor", "ZZZ-CONFLICT2.ours", "ZZZ-CONFLICT2.theirs");
add_conflict(index, "ancestor.conflict3", "ours.conflict3", "theirs.conflict3");
add_conflict(index, "zzz-conflict4", "zzz-conflict4", "zzz-conflict4");
/* Iterate the index, ensuring that conflicts are not included */
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
/* Try again, returning conflicts */
i_opts.flags |= GIT_ITERATOR_INCLUDE_CONFLICTS;
cl_git_pass(git_iterator_for_index(&i, g_repo, index, &i_opts));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "CONFLICT1", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ZZZ-CONFLICT2.ancestor", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ZZZ-CONFLICT2.ours", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ZZZ-CONFLICT2.theirs", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ancestor.conflict3", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "ours.conflict3", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "theirs.conflict3", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "zzz-conflict4", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_index_free(index);
}
| libgit2-main | tests/libgit2/iterator/index.c |
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "futils.h"
#include "iterator_helpers.h"
#include <stdarg.h>
static void assert_at_end(git_iterator *i, bool verbose)
{
const git_index_entry *end;
int error = git_iterator_advance(&end, i);
if (verbose && error != GIT_ITEROVER)
fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path);
cl_git_fail_with(GIT_ITEROVER, error);
}
void expect_iterator_items(
git_iterator *i,
size_t expected_flat,
const char **expected_flat_paths,
size_t expected_total,
const char **expected_total_paths)
{
const git_index_entry *entry;
size_t count;
int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES);
bool v = false;
int error;
if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees");
count = 0;
while (!git_iterator_advance(&entry, i)) {
if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
if (no_trees)
cl_assert(entry->mode != GIT_FILEMODE_TREE);
if (expected_flat_paths) {
const char *expect_path = expected_flat_paths[count];
size_t expect_len = strlen(expect_path);
cl_assert_equal_s(expect_path, entry->path);
if (expect_path[expect_len - 1] == '/')
cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
else
cl_assert(entry->mode != GIT_FILEMODE_TREE);
}
cl_assert(++count <= expected_flat);
}
assert_at_end(i, v);
cl_assert_equal_i(expected_flat, count);
cl_git_pass(git_iterator_reset(i));
count = 0;
cl_git_pass(git_iterator_current(&entry, i));
if (v) fprintf(stderr, "-- %s --\n", no_trees ? "notrees" : "trees");
while (entry != NULL) {
if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
if (no_trees)
cl_assert(entry->mode != GIT_FILEMODE_TREE);
if (expected_total_paths) {
const char *expect_path = expected_total_paths[count];
size_t expect_len = strlen(expect_path);
cl_assert_equal_s(expect_path, entry->path);
if (expect_path[expect_len - 1] == '/')
cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
else
cl_assert(entry->mode != GIT_FILEMODE_TREE);
}
if (entry->mode == GIT_FILEMODE_TREE) {
error = git_iterator_advance_into(&entry, i);
/* could return NOTFOUND if directory is empty */
cl_assert(!error || error == GIT_ENOTFOUND);
if (error == GIT_ENOTFOUND) {
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
} else {
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
if (++count >= expected_total)
break;
}
assert_at_end(i, v);
cl_assert_equal_i(expected_total, count);
}
void expect_advance_over(
git_iterator *i,
const char *expected_path,
git_iterator_status_t expected_status)
{
const git_index_entry *entry;
git_iterator_status_t status;
int error;
cl_git_pass(git_iterator_current(&entry, i));
cl_assert_equal_s(expected_path, entry->path);
error = git_iterator_advance_over(&entry, &status, i);
cl_assert(!error || error == GIT_ITEROVER);
cl_assert_equal_i(expected_status, status);
}
void expect_advance_into(
git_iterator *i,
const char *expected_path)
{
const git_index_entry *entry;
int error;
cl_git_pass(git_iterator_current(&entry, i));
cl_assert_equal_s(expected_path, entry->path);
if (S_ISDIR(entry->mode))
error = git_iterator_advance_into(&entry, i);
else
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
| libgit2-main | tests/libgit2/iterator/iterator_helpers.c |
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "futils.h"
#include "../submodule/submodule_helpers.h"
#include "../merge/merge_helpers.h"
#include "iterator_helpers.h"
#include <stdarg.h>
static git_repository *g_repo;
void test_iterator_workdir__initialize(void)
{
}
void test_iterator_workdir__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static void workdir_iterator_test(
const char *sandbox,
const char *start,
const char *end,
int expected_count,
int expected_ignores,
const char **expected_names,
const char *an_ignored_name)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, count = 0, count_all = 0, count_all_post_reset = 0;
g_repo = cl_git_sandbox_init(sandbox);
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
error = git_iterator_current(&entry, i);
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
while (entry != NULL) {
int ignored = git_iterator_current_is_ignored(i);
if (S_ISDIR(entry->mode)) {
cl_git_pass(git_iterator_advance_into(&entry, i));
continue;
}
if (expected_names != NULL)
cl_assert_equal_s(expected_names[count_all], entry->path);
if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0)
cl_assert(ignored);
if (!ignored)
count++;
count_all++;
error = git_iterator_advance(&entry, i);
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
}
cl_assert_equal_i(expected_count, count);
cl_assert_equal_i(expected_count + expected_ignores, count_all);
cl_git_pass(git_iterator_reset(i));
error = git_iterator_current(&entry, i);
cl_assert((error == 0 && entry != NULL) ||
(error == GIT_ITEROVER && entry == NULL));
while (entry != NULL) {
if (S_ISDIR(entry->mode)) {
cl_git_pass(git_iterator_advance_into(&entry, i));
continue;
}
if (expected_names != NULL)
cl_assert_equal_s(
expected_names[count_all_post_reset], entry->path);
count_all_post_reset++;
error = git_iterator_advance(&entry, i);
cl_assert(error == 0 || error == GIT_ITEROVER);
}
cl_assert_equal_i(count_all, count_all_post_reset);
git_iterator_free(i);
}
void test_iterator_workdir__0(void)
{
workdir_iterator_test("attr", NULL, NULL, 24, 5, NULL, "ign");
}
static const char *status_paths[] = {
"current_file",
"ignored_file",
"modified_file",
"new_file",
"staged_changes",
"staged_changes_modified_file",
"staged_delete_modified_file",
"staged_new_file",
"staged_new_file_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/modified_file",
"subdir/new_file",
"\xe8\xbf\x99",
NULL
};
void test_iterator_workdir__1(void)
{
workdir_iterator_test(
"status", NULL, NULL, 13, 1, status_paths, "ignored_file");
}
static const char *status_paths_range_0[] = {
"staged_changes",
"staged_changes_modified_file",
"staged_delete_modified_file",
"staged_new_file",
"staged_new_file_modified_file",
NULL
};
void test_iterator_workdir__1_ranged_0(void)
{
workdir_iterator_test(
"status", "staged", "staged", 5, 0, status_paths_range_0, NULL);
}
static const char *status_paths_range_1[] = {
"modified_file", NULL
};
void test_iterator_workdir__1_ranged_1(void)
{
workdir_iterator_test(
"status", "modified_file", "modified_file",
1, 0, status_paths_range_1, NULL);
}
static const char *status_paths_range_3[] = {
"subdir.txt",
"subdir/current_file",
"subdir/modified_file",
NULL
};
void test_iterator_workdir__1_ranged_3(void)
{
workdir_iterator_test(
"status", "subdir", "subdir/modified_file",
3, 0, status_paths_range_3, NULL);
}
static const char *status_paths_range_4[] = {
"subdir/current_file",
"subdir/modified_file",
"subdir/new_file",
"\xe8\xbf\x99",
NULL
};
void test_iterator_workdir__1_ranged_4(void)
{
workdir_iterator_test(
"status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL);
}
static const char *status_paths_range_5[] = {
"subdir/modified_file",
NULL
};
void test_iterator_workdir__1_ranged_5(void)
{
workdir_iterator_test(
"status", "subdir/modified_file", "subdir/modified_file",
1, 0, status_paths_range_5, NULL);
}
void test_iterator_workdir__1_ranged_5_1_ranged_empty_0(void)
{
workdir_iterator_test(
"status", "\xff_does_not_exist", NULL,
0, 0, NULL, NULL);
}
void test_iterator_workdir__1_ranged_empty_1(void)
{
workdir_iterator_test(
"status", "empty", "empty",
0, 0, NULL, NULL);
}
void test_iterator_workdir__1_ranged_empty_2(void)
{
workdir_iterator_test(
"status", NULL, "aaaa_empty_before",
0, 0, NULL, NULL);
}
void test_iterator_workdir__builtin_ignores(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int idx;
static struct {
const char *path;
bool ignored;
} expected[] = {
{ "dir/", true },
{ "file", false },
{ "ign", true },
{ "macro_bad", false },
{ "macro_test", false },
{ "root_test1", false },
{ "root_test2", false },
{ "root_test3", false },
{ "root_test4.txt", false },
{ "sub/", false },
{ "sub/.gitattributes", false },
{ "sub/abc", false },
{ "sub/dir/", true },
{ "sub/file", false },
{ "sub/ign/", true },
{ "sub/sub/", false },
{ "sub/sub/.gitattributes", false },
{ "sub/sub/dir", false }, /* file is not actually a dir */
{ "sub/sub/file", false },
{ NULL, false }
};
g_repo = cl_git_sandbox_init("attr");
cl_git_pass(p_mkdir("attr/sub/sub/.git", 0777));
cl_git_mkfile("attr/sub/.git", "whatever");
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "dir";
i_opts.end = "sub/sub/file";
cl_git_pass(git_iterator_for_workdir(
&i, g_repo, NULL, NULL, &i_opts));
cl_git_pass(git_iterator_current(&entry, i));
for (idx = 0; entry != NULL; ++idx) {
int ignored = git_iterator_current_is_ignored(i);
cl_assert_equal_s(expected[idx].path, entry->path);
cl_assert_(ignored == expected[idx].ignored, expected[idx].path);
if (!ignored &&
(entry->mode == GIT_FILEMODE_TREE ||
entry->mode == GIT_FILEMODE_COMMIT))
{
/* it is possible to advance "into" a submodule */
cl_git_pass(git_iterator_advance_into(&entry, i));
} else {
int error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
}
cl_assert(expected[idx].path == NULL);
git_iterator_free(i);
}
static void check_wd_first_through_third_range(
git_repository *repo, const char *start, const char *end)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, idx;
static const char *expected[] = { "FIRST", "second", "THIRD", NULL };
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_iterator_for_workdir(
&i, repo, NULL, NULL, &i_opts));
cl_git_pass(git_iterator_current(&entry, i));
for (idx = 0; entry != NULL; ++idx) {
cl_assert_equal_s(expected[idx], entry->path);
error = git_iterator_advance(&entry, i);
cl_assert(!error || error == GIT_ITEROVER);
}
cl_assert(expected[idx] == NULL);
git_iterator_free(i);
}
void test_iterator_workdir__handles_icase_range(void)
{
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
cl_git_mkfile("empty_standard_repo/before", "whatever\n");
cl_git_mkfile("empty_standard_repo/FIRST", "whatever\n");
cl_git_mkfile("empty_standard_repo/second", "whatever\n");
cl_git_mkfile("empty_standard_repo/THIRD", "whatever\n");
cl_git_mkfile("empty_standard_repo/zafter", "whatever\n");
cl_git_mkfile("empty_standard_repo/Zlast", "whatever\n");
check_wd_first_through_third_range(g_repo, "first", "third");
check_wd_first_through_third_range(g_repo, "FIRST", "THIRD");
check_wd_first_through_third_range(g_repo, "first", "THIRD");
check_wd_first_through_third_range(g_repo, "FIRST", "third");
check_wd_first_through_third_range(g_repo, "FirSt", "tHiRd");
}
/*
* The workdir iterator is like the filesystem iterator, but honors
* special git type constructs (ignores, submodules, etc).
*/
void test_iterator_workdir__icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
}
void test_iterator_workdir__icase_starts_and_ends(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
}
static void build_workdir_tree(const char *root, int dirs, int subs)
{
int i, j;
char buf[64], sub[80];
for (i = 0; i < dirs; ++i) {
if (i % 2 == 0) {
p_snprintf(buf, sizeof(buf), "%s/dir%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
p_snprintf(buf, sizeof(buf), "%s/dir%02d/file", root, i);
cl_git_mkfile(buf, buf);
buf[strlen(buf) - 5] = '\0';
} else {
p_snprintf(buf, sizeof(buf), "%s/DIR%02d", root, i);
cl_git_pass(git_futils_mkdir(buf, 0775, GIT_MKDIR_PATH));
}
for (j = 0; j < subs; ++j) {
switch (j % 4) {
case 0: p_snprintf(sub, sizeof(sub), "%s/sub%02d", buf, j); break;
case 1: p_snprintf(sub, sizeof(sub), "%s/sUB%02d", buf, j); break;
case 2: p_snprintf(sub, sizeof(sub), "%s/Sub%02d", buf, j); break;
case 3: p_snprintf(sub, sizeof(sub), "%s/SUB%02d", buf, j); break;
}
cl_git_pass(git_futils_mkdir(sub, 0775, GIT_MKDIR_PATH));
if (j % 2 == 0) {
size_t sublen = strlen(sub);
memcpy(&sub[sublen], "/file", sizeof("/file"));
cl_git_mkfile(sub, sub);
sub[sublen] = '\0';
}
}
}
}
void test_iterator_workdir__depth(void)
{
git_iterator *iter;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
build_workdir_tree("icase", 10, 10);
build_workdir_tree("icase/DIR01/sUB01", 50, 0);
build_workdir_tree("icase/dir02/sUB01", 50, 0);
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
expect_iterator_items(iter, 125, NULL, 125, NULL);
git_iterator_free(iter);
/* auto expand with tree entries (empty dirs silently skipped) */
iter_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
expect_iterator_items(iter, 337, NULL, 337, NULL);
git_iterator_free(iter);
}
/* The filesystem iterator is a workdir iterator without any special
* workdir handling capabilities (ignores, submodules, etc).
*/
void test_iterator_workdir__filesystem(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
static const char *expect_base[] = {
"DIR01/Sub02/file",
"DIR01/sub00/file",
"current_file",
"dir00/Sub02/file",
"dir00/file",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_trees[] = {
"DIR01/",
"DIR01/SUB03/",
"DIR01/Sub02/",
"DIR01/Sub02/file",
"DIR01/sUB01/",
"DIR01/sub00/",
"DIR01/sub00/file",
"current_file",
"dir00/",
"dir00/SUB03/",
"dir00/Sub02/",
"dir00/Sub02/file",
"dir00/file",
"dir00/sUB01/",
"dir00/sub00/",
"dir00/sub00/file",
"modified_file",
"new_file",
NULL,
};
static const char *expect_noauto[] = {
"DIR01/",
"current_file",
"dir00/",
"modified_file",
"new_file",
NULL,
};
g_repo = cl_git_sandbox_init("status");
build_workdir_tree("status/subdir", 2, 4);
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", NULL));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
git__tsort((void **)expect_base, 8, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_trees, 18, (git__tsort_cmp)git__strcasecmp);
git__tsort((void **)expect_noauto, 5, (git__tsort_cmp)git__strcasecmp);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 8, expect_base, 8, expect_base);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 18, expect_trees, 18, expect_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(&i, "status/subdir", &i_opts));
expect_iterator_items(i, 5, expect_noauto, 18, expect_trees);
git_iterator_free(i);
}
void test_iterator_workdir__filesystem2(void)
{
git_iterator *i;
static const char *expect_base[] = {
"heads/br2",
"heads/dir",
"heads/executable",
"heads/ident",
"heads/long-file-name",
"heads/master",
"heads/merge-conflict",
"heads/packed-test",
"heads/subtrees",
"heads/test",
"heads/testrepo-worktree",
"symref",
"tags/e90810b",
"tags/foo/bar",
"tags/foo/foo/bar",
"tags/point_to_blob",
"tags/test",
NULL,
};
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_iterator_for_filesystem(
&i, "testrepo/.git/refs", NULL));
expect_iterator_items(i, 17, expect_base, 17, expect_base);
git_iterator_free(i);
}
/*
* Lots of empty dirs, or nearly empty ones, make the old workdir
* iterator cry. Also, segfault.
*/
void test_iterator_workdir__filesystem_gunk(void)
{
git_str parent = GIT_STR_INIT;
git_iterator *i;
int n;
if (!cl_is_env_set("GITTEST_INVASIVE_SPEED"))
cl_skip();
g_repo = cl_git_sandbox_init("testrepo");
for (n = 0; n < 100000; n++) {
git_str_clear(&parent);
cl_git_pass(git_str_printf(&parent, "%s/refs/heads/foo/%d/subdir", git_repository_path(g_repo), n));
cl_git_pass(git_futils_mkdir(parent.ptr, 0775, GIT_MKDIR_PATH));
}
cl_git_pass(git_iterator_for_filesystem(&i, "testrepo/.git/refs", NULL));
/*
* Should only have 17 items, since we're not asking for trees to be
* returned. the goal of this test is simply to not crash.
*/
expect_iterator_items(i, 17, NULL, 16, NULL);
git_iterator_free(i);
git_str_dispose(&parent);
}
void test_iterator_workdir__skips_unreadable_dirs(void)
{
git_iterator *i;
const git_index_entry *e;
if (!cl_is_chmod_supported())
return;
#ifndef GIT_WIN32
if (geteuid() == 0)
cl_skip();
#endif
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_must_pass(p_mkdir("empty_standard_repo/r", 0777));
cl_git_mkfile("empty_standard_repo/r/a", "hello");
cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777));
cl_git_mkfile("empty_standard_repo/r/b/problem", "not me");
cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000));
cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777));
cl_git_mkfile("empty_standard_repo/r/c/foo", "aloha");
cl_git_mkfile("empty_standard_repo/r/d", "final");
cl_git_pass(git_iterator_for_filesystem(
&i, "empty_standard_repo/r", NULL));
cl_git_pass(git_iterator_advance(&e, i)); /* a */
cl_assert_equal_s("a", e->path);
cl_git_pass(git_iterator_advance(&e, i)); /* c/foo */
cl_assert_equal_s("c/foo", e->path);
cl_git_pass(git_iterator_advance(&e, i)); /* d */
cl_assert_equal_s("d", e->path);
cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
git_iterator_free(i);
}
void test_iterator_workdir__skips_fifos_and_special_files(void)
{
#ifndef GIT_WIN32
git_iterator *i;
const git_index_entry *e;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_must_pass(p_mkdir("empty_standard_repo/dir", 0777));
cl_git_mkfile("empty_standard_repo/file", "not me");
cl_assert(!mkfifo("empty_standard_repo/fifo", 0777));
cl_assert(!access("empty_standard_repo/fifo", F_OK));
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_filesystem(
&i, "empty_standard_repo", &i_opts));
cl_git_pass(git_iterator_advance(&e, i)); /* .git */
cl_assert(S_ISDIR(e->mode));
cl_git_pass(git_iterator_advance(&e, i)); /* dir */
cl_assert(S_ISDIR(e->mode));
/* skips fifo */
cl_git_pass(git_iterator_advance(&e, i)); /* file */
cl_assert(S_ISREG(e->mode));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
git_iterator_free(i);
#else
cl_skip();
#endif
}
void test_iterator_workdir__pathlist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 100, NULL));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
/* Test iterators without returning tree entries (but autoexpanding.) */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
/* Case sensitive */
{
const char *expected[] = {
"B", "D", "L/1", "a", "c", "e", "k/1", "k/a" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Case INsensitive */
{
const char *expected[] = {
"a", "B", "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 8;
i_opts.start = NULL;
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case sensitive. */
{
const char *expected[] = { "c", "e", "k/1", "k/a" };
size_t expected_len = 4;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set a start, but no end. Case INsensitive. */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a", "L/1" };
size_t expected_len = 6;
i_opts.start = "c";
i_opts.end = NULL;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case sensitive. */
{
const char *expected[] = { "B", "D", "L/1", "a", "c", "e" };
size_t expected_len = 6;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Set no start, but an end. Case INsensitive. */
{
const char *expected[] = { "a", "B", "c", "D", "e" };
size_t expected_len = 5;
i_opts.start = NULL;
i_opts.end = "e";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "c", "e", "k/1" };
size_t expected_len = 3;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case sensitive */
{
const char *expected[] = { "k/1" };
size_t expected_len = 1;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "c", "D", "e", "k/1", "k/a" };
size_t expected_len = 5;
i_opts.start = "c";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Start and an end, case INsensitive */
{
const char *expected[] = { "k/1", "k/a" };
size_t expected_len = 2;
i_opts.start = "k";
i_opts.end = "k/D";
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_vector_free(&filelist);
}
void test_iterator_workdir__pathlist_with_dirs(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
/* Test that a prefix `k` matches folders, even without trailing slash */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that a `k/` matches a folder */
{
const char *expected[] = { "k/1", "k/B", "k/D", "k/a", "k/c" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "k/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* When the iterator is case sensitive, ensure we can't lookup the
* directory with the wrong case.
*/
{
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
/* Test that case insensitive matching works. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that case insensitive matching works without trailing slash. */
{
const char *expected[] = { "k/1", "k/a", "k/B", "k/c", "k/D" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "K"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_vector_free(&filelist);
}
static void create_paths(const char *root, int depth)
{
git_str fullpath = GIT_STR_INIT;
size_t root_len;
int i;
cl_git_pass(git_str_puts(&fullpath, root));
cl_git_pass(git_fs_path_to_dir(&fullpath));
root_len = fullpath.size;
for (i = 0; i < 8; i++) {
bool file = (depth == 0 || (i % 2) == 0);
git_str_truncate(&fullpath, root_len);
cl_git_pass(git_str_printf(&fullpath, "item%d", i));
if (file) {
cl_git_rewritefile(fullpath.ptr, "This is a file!\n");
} else {
cl_must_pass(p_mkdir(fullpath.ptr, 0777));
if (depth > 0)
create_paths(fullpath.ptr, (depth - 1));
}
}
git_str_dispose(&fullpath);
}
void test_iterator_workdir__pathlist_for_deeply_nested_item(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = cl_git_sandbox_init("icase");
create_paths(git_repository_workdir(g_repo), 3);
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = { "item1/item3/item5/item7" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/item7"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(4, i->stat_calls);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
};
size_t expected_len = 8;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/item5/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(11, i->stat_calls);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item1/item3/item0",
"item1/item3/item1/item0", "item1/item3/item1/item1",
"item1/item3/item1/item2", "item1/item3/item1/item3",
"item1/item3/item1/item4", "item1/item3/item1/item5",
"item1/item3/item1/item6", "item1/item3/item1/item7",
"item1/item3/item2",
"item1/item3/item3/item0", "item1/item3/item3/item1",
"item1/item3/item3/item2", "item1/item3/item3/item3",
"item1/item3/item3/item4", "item1/item3/item3/item5",
"item1/item3/item3/item6", "item1/item3/item3/item7",
"item1/item3/item4",
"item1/item3/item5/item0", "item1/item3/item5/item1",
"item1/item3/item5/item2", "item1/item3/item5/item3",
"item1/item3/item5/item4", "item1/item3/item5/item5",
"item1/item3/item5/item6", "item1/item3/item5/item7",
"item1/item3/item6",
"item1/item3/item7/item0", "item1/item3/item7/item1",
"item1/item3/item7/item2", "item1/item3/item7/item3",
"item1/item3/item7/item4", "item1/item3/item7/item5",
"item1/item3/item7/item6", "item1/item3/item7/item7",
};
size_t expected_len = 36;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item1/item3/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(42, i->stat_calls);
git_iterator_free(i);
}
/* Ensure that we find the single path we're interested in, and we find
* it efficiently, and don't stat the entire world to get there.
*/
{
const char *expected[] = {
"item0", "item1/item2", "item5/item7/item4", "item6",
"item7/item3/item1/item6" };
size_t expected_len = 5;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "item7/item3/item1/item6"));
cl_git_pass(git_vector_insert(&filelist, "item6"));
cl_git_pass(git_vector_insert(&filelist, "item5/item7/item4"));
cl_git_pass(git_vector_insert(&filelist, "item1/item2"));
cl_git_pass(git_vector_insert(&filelist, "item0"));
/* also add some things that don't exist or don't match the right type */
cl_git_pass(git_vector_insert(&filelist, "item2/"));
cl_git_pass(git_vector_insert(&filelist, "itemN"));
cl_git_pass(git_vector_insert(&filelist, "item1/itemA"));
cl_git_pass(git_vector_insert(&filelist, "item5/item3/item4/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
cl_assert_equal_i(14, i->stat_calls);
git_iterator_free(i);
}
git_vector_free(&filelist);
}
void test_iterator_workdir__bounded_submodules(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_index *index;
git_tree *head;
cl_git_pass(git_vector_init(&filelist, 5, NULL));
g_repo = setup_fixture_submod2();
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_repository_head_tree(&head, g_repo));
/* Test that a submodule matches */
{
const char *expected[] = { "sm_changed_head" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "sm_changed_head"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that a submodule still matches when suffixed with a '/' */
{
const char *expected[] = { "sm_changed_head" };
size_t expected_len = 1;
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "sm_changed_head/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that start/end work with a submodule */
{
const char *expected[] = { "sm_changed_head", "sm_changed_index" };
size_t expected_len = 2;
i_opts.start = "sm_changed_head";
i_opts.end = "sm_changed_index";
i_opts.pathlist.strings = NULL;
i_opts.pathlist.count = 0;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
/* Test that start and end allow '/' suffixes of submodules */
{
const char *expected[] = { "sm_changed_head", "sm_changed_index" };
size_t expected_len = 2;
i_opts.start = "sm_changed_head";
i_opts.end = "sm_changed_index";
i_opts.pathlist.strings = NULL;
i_opts.pathlist.count = 0;
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, index, head, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
}
git_vector_free(&filelist);
git_index_free(index);
git_tree_free(head);
}
void test_iterator_workdir__advance_over(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
/* create an empty directory */
cl_must_pass(p_mkdir("icase/empty", 0777));
/* create a directory in which all contents are ignored */
cl_must_pass(p_mkdir("icase/all_ignored", 0777));
cl_git_rewritefile("icase/all_ignored/one", "This is ignored\n");
cl_git_rewritefile("icase/all_ignored/two", "This, too, is ignored\n");
cl_git_rewritefile("icase/all_ignored/.gitignore", ".gitignore\none\ntwo\n");
/* create a directory in which not all contents are ignored */
cl_must_pass(p_mkdir("icase/some_ignored", 0777));
cl_git_rewritefile("icase/some_ignored/one", "This is ignored\n");
cl_git_rewritefile("icase/some_ignored/two", "This is not ignored\n");
cl_git_rewritefile("icase/some_ignored/.gitignore", ".gitignore\none\n");
/* create a directory which has some empty children */
cl_must_pass(p_mkdir("icase/empty_children", 0777));
cl_must_pass(p_mkdir("icase/empty_children/empty1", 0777));
cl_must_pass(p_mkdir("icase/empty_children/empty2", 0777));
cl_must_pass(p_mkdir("icase/empty_children/empty3", 0777));
/* create a directory which will disappear! */
cl_must_pass(p_mkdir("icase/missing_directory", 0777));
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
cl_must_pass(p_rmdir("icase/missing_directory"));
expect_advance_over(i, "B", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "D", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "F", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "H", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "J", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "L/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "a", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "all_ignored/", GIT_ITERATOR_STATUS_IGNORED);
expect_advance_over(i, "c", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "e", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "empty/", GIT_ITERATOR_STATUS_EMPTY);
expect_advance_over(i, "empty_children/", GIT_ITERATOR_STATUS_EMPTY);
expect_advance_over(i, "g", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "i", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "k/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "missing_directory/", GIT_ITERATOR_STATUS_EMPTY);
expect_advance_over(i, "some_ignored/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
void test_iterator_workdir__advance_over_with_pathlist(void)
{
git_vector pathlist = GIT_VECTOR_INIT;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector_insert(&pathlist, "dirA/subdir1/subdir2/file");
git_vector_insert(&pathlist, "dirB/subdir1/subdir2");
git_vector_insert(&pathlist, "dirC/subdir1/nonexistent");
git_vector_insert(&pathlist, "dirD/subdir1/nonexistent");
git_vector_insert(&pathlist, "dirD/subdir1/subdir2");
git_vector_insert(&pathlist, "dirD/nonexistent");
i_opts.pathlist.strings = (char **)pathlist.contents;
i_opts.pathlist.count = pathlist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
g_repo = cl_git_sandbox_init("icase");
/* Create a directory that has a file that is included in our pathlist */
cl_must_pass(p_mkdir("icase/dirA", 0777));
cl_must_pass(p_mkdir("icase/dirA/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirA/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirA/subdir1/subdir2/file", "foo!");
/* Create a directory that has a directory that is included in our pathlist */
cl_must_pass(p_mkdir("icase/dirB", 0777));
cl_must_pass(p_mkdir("icase/dirB/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirB/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirB/subdir1/subdir2/file", "foo!");
/* Create a directory that would contain an entry in our pathlist, but
* that entry does not actually exist. We don't know this until we
* advance_over it. We want to distinguish this from an actually empty
* or ignored directory.
*/
cl_must_pass(p_mkdir("icase/dirC", 0777));
cl_must_pass(p_mkdir("icase/dirC/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirC/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirC/subdir1/subdir2/file", "foo!");
/* Create a directory that has a mix of actual and nonexistent paths */
cl_must_pass(p_mkdir("icase/dirD", 0777));
cl_must_pass(p_mkdir("icase/dirD/subdir1", 0777));
cl_must_pass(p_mkdir("icase/dirD/subdir1/subdir2", 0777));
cl_git_rewritefile("icase/dirD/subdir1/subdir2/file", "foo!");
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_advance_over(i, "dirA/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "dirB/", GIT_ITERATOR_STATUS_NORMAL);
expect_advance_over(i, "dirC/", GIT_ITERATOR_STATUS_FILTERED);
expect_advance_over(i, "dirD/", GIT_ITERATOR_STATUS_NORMAL);
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
git_vector_free(&pathlist);
}
void test_iterator_workdir__advance_into(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE |
GIT_ITERATOR_DONT_AUTOEXPAND;
cl_must_pass(p_mkdir("icase/Empty", 0777));
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_advance_into(i, "B");
expect_advance_into(i, "D");
expect_advance_into(i, "Empty/");
expect_advance_into(i, "F");
expect_advance_into(i, "H");
expect_advance_into(i, "J");
expect_advance_into(i, "L/");
expect_advance_into(i, "L/1");
expect_advance_into(i, "L/B");
expect_advance_into(i, "L/D");
expect_advance_into(i, "L/a");
expect_advance_into(i, "L/c");
expect_advance_into(i, "a");
expect_advance_into(i, "c");
expect_advance_into(i, "e");
expect_advance_into(i, "g");
expect_advance_into(i, "i");
expect_advance_into(i, "k/");
expect_advance_into(i, "k/1");
expect_advance_into(i, "k/B");
expect_advance_into(i, "k/D");
expect_advance_into(i, "k/a");
expect_advance_into(i, "k/c");
cl_git_fail_with(GIT_ITEROVER, git_iterator_advance(NULL, i));
git_iterator_free(i);
}
void test_iterator_workdir__pathlist_with_directory(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
const char *expected[] = { "subdir/README", "subdir/new.txt",
"subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len = 4;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir/"));
g_repo = cl_git_sandbox_init("testrepo2");
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_free(&filelist);
}
void test_iterator_workdir__pathlist_with_directory_include_trees(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt",
"subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt", };
size_t expected_len = 6;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir/"));
g_repo = cl_git_sandbox_init("testrepo2");
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_workdir(&i, g_repo, NULL, NULL, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_free(&filelist);
}
void test_iterator_workdir__hash_when_requested(void)
{
git_iterator *iter;
const git_index_entry *entry;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
git_oid expected_id = GIT_OID_SHA1_ZERO;
size_t i;
struct merge_index_entry expected[] = {
{ 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
{ 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
{ 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
{ 0100644, "7c7e08f9559d9e1551b91e1cf68f1d0066109add", 0, "oyster.txt" },
{ 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 0, "veal.txt" },
};
g_repo = cl_git_sandbox_init("merge-recursive");
/* do the iteration normally, ensure there are no hashes */
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
for (i = 0; i < sizeof(expected) / sizeof(struct merge_index_entry); i++) {
cl_git_pass(git_iterator_advance(&entry, iter));
cl_assert_equal_oid(&expected_id, &entry->id);
cl_assert_equal_s(expected[i].path, entry->path);
}
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&entry, iter));
git_iterator_free(iter);
/* do the iteration requesting hashes */
iter_opts.flags |= GIT_ITERATOR_INCLUDE_HASH;
cl_git_pass(git_iterator_for_workdir(&iter, g_repo, NULL, NULL, &iter_opts));
for (i = 0; i < sizeof(expected) / sizeof(struct merge_index_entry); i++) {
cl_git_pass(git_iterator_advance(&entry, iter));
cl_git_pass(git_oid__fromstr(&expected_id, expected[i].oid_str, GIT_OID_SHA1));
cl_assert_equal_oid(&expected_id, &entry->id);
cl_assert_equal_s(expected[i].path, entry->path);
}
cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&entry, iter));
git_iterator_free(iter);
}
| libgit2-main | tests/libgit2/iterator/workdir.c |
#include "clar_libgit2.h"
#include "iterator.h"
#include "repository.h"
#include "futils.h"
#include "tree.h"
#include "../submodule/submodule_helpers.h"
#include "../diff/diff_helpers.h"
#include "iterator_helpers.h"
#include <stdarg.h>
static git_repository *g_repo;
void test_iterator_tree__initialize(void)
{
}
void test_iterator_tree__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static void tree_iterator_test(
const char *sandbox,
const char *treeish,
const char *start,
const char *end,
int expected_count,
const char **expected_values)
{
git_tree *t;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, count = 0, count_post_reset = 0;
g_repo = cl_git_sandbox_init(sandbox);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = start;
i_opts.end = end;
cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish));
cl_git_pass(git_iterator_for_tree(&i, t, &i_opts));
/* test loop */
while (!(error = git_iterator_advance(&entry, i))) {
cl_assert(entry);
if (expected_values != NULL)
cl_assert_equal_s(expected_values[count], entry->path);
count++;
}
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert(!entry);
cl_assert_equal_i(expected_count, count);
/* test reset */
cl_git_pass(git_iterator_reset(i));
while (!(error = git_iterator_advance(&entry, i))) {
cl_assert(entry);
if (expected_values != NULL)
cl_assert_equal_s(expected_values[count_post_reset], entry->path);
count_post_reset++;
}
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert(!entry);
cl_assert_equal_i(count, count_post_reset);
git_iterator_free(i);
git_tree_free(t);
}
/* results of: git ls-tree -r --name-only 605812a */
const char *expected_tree_0[] = {
".gitattributes",
"attr0",
"attr1",
"attr2",
"attr3",
"binfile",
"macro_test",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
"subdir/.gitattributes",
"subdir/abc",
"subdir/subdir_test1",
"subdir/subdir_test2.txt",
"subdir2/subdir2_test1",
NULL
};
void test_iterator_tree__0(void)
{
tree_iterator_test("attr", "605812a", NULL, NULL, 16, expected_tree_0);
}
/* results of: git ls-tree -r --name-only 6bab5c79 */
const char *expected_tree_1[] = {
".gitattributes",
"attr0",
"attr1",
"attr2",
"attr3",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
"subdir/.gitattributes",
"subdir/subdir_test1",
"subdir/subdir_test2.txt",
"subdir2/subdir2_test1",
NULL
};
void test_iterator_tree__1(void)
{
tree_iterator_test("attr", "6bab5c79cd5", NULL, NULL, 13, expected_tree_1);
}
/* results of: git ls-tree -r --name-only 26a125ee1 */
const char *expected_tree_2[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
NULL
};
void test_iterator_tree__2(void)
{
tree_iterator_test("status", "26a125ee1", NULL, NULL, 12, expected_tree_2);
}
/* $ git ls-tree -r --name-only 0017bd4ab1e */
const char *expected_tree_3[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file"
};
void test_iterator_tree__3(void)
{
tree_iterator_test("status", "0017bd4ab1e", NULL, NULL, 8, expected_tree_3);
}
/* $ git ls-tree -r --name-only 24fa9a9fc4e202313e24b648087495441dab432b */
const char *expected_tree_4[] = {
"attr0",
"attr1",
"attr2",
"attr3",
"binfile",
"gitattributes",
"macro_bad",
"macro_test",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
"sub/abc",
"sub/file",
"sub/sub/file",
"sub/sub/subsub.txt",
"sub/subdir_test1",
"sub/subdir_test2.txt",
"subdir/.gitattributes",
"subdir/abc",
"subdir/subdir_test1",
"subdir/subdir_test2.txt",
"subdir2/subdir2_test1",
NULL
};
void test_iterator_tree__4(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b", NULL, NULL,
23, expected_tree_4);
}
void test_iterator_tree__4_ranged(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"sub", "sub",
11, &expected_tree_4[12]);
}
const char *expected_tree_ranged_0[] = {
"gitattributes",
"macro_bad",
"macro_test",
"root_test1",
"root_test2",
"root_test3",
"root_test4.txt",
NULL
};
void test_iterator_tree__ranged_0(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"git", "root",
7, expected_tree_ranged_0);
}
const char *expected_tree_ranged_1[] = {
"sub/subdir_test2.txt",
NULL
};
void test_iterator_tree__ranged_1(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"sub/subdir_test2.txt", "sub/subdir_test2.txt",
1, expected_tree_ranged_1);
}
void test_iterator_tree__range_empty_0(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"empty", "empty", 0, NULL);
}
void test_iterator_tree__range_empty_1(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b",
"z_empty_after", NULL, 0, NULL);
}
void test_iterator_tree__range_empty_2(void)
{
tree_iterator_test(
"attr", "24fa9a9fc4e202313e24b648087495441dab432b",
NULL, ".aaa_empty_before", 0, NULL);
}
static void check_tree_entry(
git_iterator *i,
const char *oid,
const char *oid_p,
const char *oid_pp,
const char *oid_ppp)
{
const git_index_entry *ie;
const git_tree_entry *te;
const git_tree *tree;
cl_git_pass(git_iterator_current_tree_entry(&te, i));
cl_assert(te);
cl_assert(git_oid_streq(&te->oid, oid) == 0);
cl_git_pass(git_iterator_current(&ie, i));
if (oid_p) {
cl_git_pass(git_iterator_current_parent_tree(&tree, i, 0));
cl_assert(tree);
cl_assert(git_oid_streq(git_tree_id(tree), oid_p) == 0);
}
if (oid_pp) {
cl_git_pass(git_iterator_current_parent_tree(&tree, i, 1));
cl_assert(tree);
cl_assert(git_oid_streq(git_tree_id(tree), oid_pp) == 0);
}
if (oid_ppp) {
cl_git_pass(git_iterator_current_parent_tree(&tree, i, 2));
cl_assert(tree);
cl_assert(git_oid_streq(git_tree_id(tree), oid_ppp) == 0);
}
}
void test_iterator_tree__special_functions(void)
{
git_tree *t;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry;
int error, cases = 0;
const char *rootoid = "ce39a97a7fb1fa90bcf5e711249c1e507476ae0e";
g_repo = cl_git_sandbox_init("attr");
t = resolve_commit_oid_to_tree(
g_repo, "24fa9a9fc4e202313e24b648087495441dab432b");
cl_assert(t != NULL);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, t, &i_opts));
while (!(error = git_iterator_advance(&entry, i))) {
cl_assert(entry);
if (strcmp(entry->path, "sub/file") == 0) {
cases++;
check_tree_entry(
i, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"ecb97df2a174987475ac816e3847fc8e9f6c596b",
rootoid, NULL);
}
else if (strcmp(entry->path, "sub/sub/subsub.txt") == 0) {
cases++;
check_tree_entry(
i, "9e5bdc47d6a80f2be0ea3049ad74231b94609242",
"4e49ba8c5b6c32ff28cd9dcb60be34df50fcc485",
"ecb97df2a174987475ac816e3847fc8e9f6c596b", rootoid);
}
else if (strcmp(entry->path, "subdir/.gitattributes") == 0) {
cases++;
check_tree_entry(
i, "99eae476896f4907224978b88e5ecaa6c5bb67a9",
"9fb40b6675dde60b5697afceae91b66d908c02d9",
rootoid, NULL);
}
else if (strcmp(entry->path, "subdir2/subdir2_test1") == 0) {
cases++;
check_tree_entry(
i, "dccada462d3df8ac6de596fb8c896aba9344f941",
"2929de282ce999e95183aedac6451d3384559c4b",
rootoid, NULL);
}
}
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert(!entry);
cl_assert_equal_i(4, cases);
git_iterator_free(i);
git_tree_free(t);
}
static void check_tree_range(
git_repository *repo,
const char *start,
const char *end,
bool ignore_case,
int expected_count)
{
git_tree *head;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
int error, count;
i_opts.flags = ignore_case ? GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.start = start;
i_opts.end = end;
cl_git_pass(git_repository_head_tree(&head, repo));
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
for (count = 0; !(error = git_iterator_advance(NULL, i)); ++count)
/* count em up */;
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert_equal_i(expected_count, count);
git_iterator_free(i);
git_tree_free(head);
}
void test_iterator_tree__range_icase(void)
{
g_repo = cl_git_sandbox_init("testrepo");
check_tree_range(g_repo, "B", "C", false, 0);
check_tree_range(g_repo, "B", "C", true, 1);
check_tree_range(g_repo, "b", "c", false, 1);
check_tree_range(g_repo, "b", "c", true, 1);
check_tree_range(g_repo, "a", "z", false, 3);
check_tree_range(g_repo, "a", "z", true, 4);
check_tree_range(g_repo, "A", "Z", false, 1);
check_tree_range(g_repo, "A", "Z", true, 4);
check_tree_range(g_repo, "a", "Z", false, 0);
check_tree_range(g_repo, "a", "Z", true, 4);
check_tree_range(g_repo, "A", "z", false, 4);
check_tree_range(g_repo, "A", "z", true, 4);
check_tree_range(g_repo, "new.txt", "new.txt", true, 1);
check_tree_range(g_repo, "new.txt", "new.txt", false, 1);
check_tree_range(g_repo, "README", "README", true, 1);
check_tree_range(g_repo, "README", "README", false, 1);
}
void test_iterator_tree__icase_0(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_tree *head;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_head_tree(&head, g_repo));
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_tree(&i, head, NULL));
expect_iterator_items(i, 20, NULL, 20, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 22, NULL, 22, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 12, NULL, 22, NULL);
git_iterator_free(i);
git_tree_free(head);
}
void test_iterator_tree__icase_1(void)
{
git_iterator *i;
git_tree *head;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_repository_head_tree(&head, g_repo));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
/* auto expand with no tree entries */
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 7, NULL, 7, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
/* auto expand with tree entries */
i_opts.start = "c";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 4, NULL, 4, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 5, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 1, NULL, 4, NULL);
git_iterator_free(i);
/* auto expand with no tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 13, NULL, 13, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 14, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 6, NULL, 6, NULL);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_DONT_AUTOEXPAND;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 9, NULL, 14, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
git_tree_free(head);
}
void test_iterator_tree__icase_2(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_tree *head;
static const char *expect_basic[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
NULL,
};
static const char *expect_trees[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/",
"subdir/current_file",
"subdir/deleted_file",
"subdir/modified_file",
NULL,
};
static const char *expect_noauto[] = {
"current_file",
"file_deleted",
"modified_file",
"staged_changes",
"staged_changes_file_deleted",
"staged_changes_modified_file",
"staged_delete_file_deleted",
"staged_delete_modified_file",
"subdir.txt",
"subdir/",
NULL
};
g_repo = cl_git_sandbox_init("status");
cl_git_pass(git_repository_head_tree(&head, g_repo));
/* auto expand with no tree entries */
cl_git_pass(git_iterator_for_tree(&i, head, NULL));
expect_iterator_items(i, 12, expect_basic, 12, expect_basic);
git_iterator_free(i);
/* auto expand with tree entries */
i_opts.flags = GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 13, expect_trees, 13, expect_trees);
git_iterator_free(i);
/* no auto expand (implies trees included) */
i_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_iterator_for_tree(&i, head, &i_opts));
expect_iterator_items(i, 10, expect_noauto, 13, expect_trees);
git_iterator_free(i);
git_tree_free(head);
}
/* "b=name,t=name", blob_id, tree_id */
static void build_test_tree(
git_oid *out, git_repository *repo, const char *fmt, ...)
{
git_oid *id;
git_treebuilder *builder;
const char *scan = fmt, *next;
char type, delimiter;
git_filemode_t mode = GIT_FILEMODE_BLOB;
git_str name = GIT_STR_INIT;
va_list arglist;
cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); /* start builder */
va_start(arglist, fmt);
while (*scan) {
switch (type = *scan++) {
case 't': case 'T': mode = GIT_FILEMODE_TREE; break;
case 'b': case 'B': mode = GIT_FILEMODE_BLOB; break;
default:
cl_assert(type == 't' || type == 'T' || type == 'b' || type == 'B');
}
delimiter = *scan++; /* read and skip delimiter */
for (next = scan; *next && *next != delimiter; ++next)
/* seek end */;
cl_git_pass(git_str_set(&name, scan, (size_t)(next - scan)));
for (scan = next; *scan && (*scan == delimiter || *scan == ','); ++scan)
/* skip delimiter and optional comma */;
id = va_arg(arglist, git_oid *);
cl_git_pass(git_treebuilder_insert(NULL, builder, name.ptr, id, mode));
}
va_end(arglist);
cl_git_pass(git_treebuilder_write(out, builder));
git_treebuilder_free(builder);
git_str_dispose(&name);
}
void test_iterator_tree__case_conflicts_0(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, biga_id, littlea_id, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/1.file", "A/3.file", "a/2.file", "a/4.file" };
const char *expect_ci[] = {
"A/1.file", "a/2.file", "A/3.file", "a/4.file" };
const char *expect_cs_trees[] = {
"A/", "A/1.file", "A/3.file", "a/", "a/2.file", "a/4.file" };
const char *expect_ci_trees[] = {
"A/", "A/1.file", "a/2.file", "A/3.file", "a/4.file" };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid__fromstr(&blob_id, blob_sha, GIT_OID_SHA1)); /* lookup blob */
/* create tree with: A/1.file, A/3.file, a/2.file, a/4.file */
build_test_tree(
&biga_id, g_repo, "b|1.file|,b|3.file|", &blob_id, &blob_id);
build_test_tree(
&littlea_id, g_repo, "b|2.file|,b|4.file|", &blob_id, &blob_id);
build_test_tree(
&tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_cs, 4, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_cs_trees, 6, expect_cs_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 5, expect_ci_trees, 5, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
}
void test_iterator_tree__case_conflicts_1(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, Ab_id, biga_id, littlea_id, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/a", "A/b/1", "A/c", "a/C", "a/a", "a/b" };
const char *expect_ci[] = {
"A/a", "a/b", "A/b/1", "A/c" };
const char *expect_cs_trees[] = {
"A/", "A/a", "A/b/", "A/b/1", "A/c", "a/", "a/C", "a/a", "a/b" };
const char *expect_ci_trees[] = {
"A/", "A/a", "a/b", "A/b/", "A/b/1", "A/c" };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid__fromstr(&blob_id, blob_sha, GIT_OID_SHA1)); /* lookup blob */
/* create: A/a A/b/1 A/c a/a a/b a/C */
build_test_tree(&Ab_id, g_repo, "b|1|", &blob_id);
build_test_tree(
&biga_id, g_repo, "b|a|,t|b|,b|c|", &blob_id, &Ab_id, &blob_id);
build_test_tree(
&littlea_id, g_repo, "b|a|,b|b|,b|C|", &blob_id, &blob_id, &blob_id);
build_test_tree(
&tree_id, g_repo, "t|A|,t|a|", &biga_id, &littlea_id);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_cs, 6, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 4, expect_ci, 4, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 9, expect_cs_trees, 9, expect_cs_trees);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 6, expect_ci_trees, 6, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
}
void test_iterator_tree__case_conflicts_2(void)
{
const char *blob_sha = "d44e18fb93b7107b5cd1b95d601591d77869a1b6";
git_tree *tree;
git_oid blob_id, d1, d2, c1, c2, b1, b2, a1, a2, tree_id;
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
const char *expect_cs[] = {
"A/B/C/D/16", "A/B/C/D/foo", "A/B/C/d/15", "A/B/C/d/FOO",
"A/B/c/D/14", "A/B/c/D/foo", "A/B/c/d/13", "A/B/c/d/FOO",
"A/b/C/D/12", "A/b/C/D/foo", "A/b/C/d/11", "A/b/C/d/FOO",
"A/b/c/D/10", "A/b/c/D/foo", "A/b/c/d/09", "A/b/c/d/FOO",
"a/B/C/D/08", "a/B/C/D/foo", "a/B/C/d/07", "a/B/C/d/FOO",
"a/B/c/D/06", "a/B/c/D/foo", "a/B/c/d/05", "a/B/c/d/FOO",
"a/b/C/D/04", "a/b/C/D/foo", "a/b/C/d/03", "a/b/C/d/FOO",
"a/b/c/D/02", "a/b/c/D/foo", "a/b/c/d/01", "a/b/c/d/FOO", };
const char *expect_ci[] = {
"a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04",
"a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08",
"A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12",
"A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16",
"A/B/C/D/foo", };
const char *expect_ci_trees[] = {
"A/", "A/B/", "A/B/C/", "A/B/C/D/",
"a/b/c/d/01", "a/b/c/D/02", "a/b/C/d/03", "a/b/C/D/04",
"a/B/c/d/05", "a/B/c/D/06", "a/B/C/d/07", "a/B/C/D/08",
"A/b/c/d/09", "A/b/c/D/10", "A/b/C/d/11", "A/b/C/D/12",
"A/B/c/d/13", "A/B/c/D/14", "A/B/C/d/15", "A/B/C/D/16",
"A/B/C/D/foo", };
g_repo = cl_git_sandbox_init("icase");
cl_git_pass(git_oid__fromstr(&blob_id, blob_sha, GIT_OID_SHA1)); /* lookup blob */
build_test_tree(&d1, g_repo, "b|16|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|15|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|14|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|13|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&d1, g_repo, "b|12|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|11|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|10|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|09|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&a1, g_repo, "t|B|,t|b|", &b1, &b2);
build_test_tree(&d1, g_repo, "b|08|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|07|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|06|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|05|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b1, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&d1, g_repo, "b|04|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|03|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c1, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&d1, g_repo, "b|02|,b|foo|", &blob_id, &blob_id);
build_test_tree(&d2, g_repo, "b|01|,b|FOO|", &blob_id, &blob_id);
build_test_tree(&c2, g_repo, "t|D|,t|d|", &d1, &d2);
build_test_tree(&b2, g_repo, "t|C|,t|c|", &c1, &c2);
build_test_tree(&a2, g_repo, "t|B|,t|b|", &b1, &b2);
build_test_tree(&tree_id, g_repo, "t/A/,t/a/", &a1, &a2);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 32, expect_cs, 32, expect_cs);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 17, expect_ci, 17, expect_ci);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 21, expect_ci_trees, 21, expect_ci_trees);
git_iterator_free(i);
git_tree_free(tree);
}
void test_iterator_tree__pathlist(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
bool default_icase;
int expect;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
git_repository_head_tree(&tree, g_repo);
/* All indexfilelist iterator tests are "autoexpand with no tree entries" */
/* In this test we DO NOT force a case on the iterators and verify default behavior. */
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 8, NULL, 8, NULL);
git_iterator_free(i);
i_opts.start = "c";
i_opts.end = NULL;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (c D e k/1 k/a L ==> 6) vs (c e k/1 k/a ==> 4) */
expect = ((default_icase) ? 6 : 4);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
i_opts.start = NULL;
i_opts.end = "e";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
default_icase = git_iterator_ignore_case(i);
/* (a B c D e ==> 5) vs (B D L/1 a c e ==> 6) */
expect = ((default_icase) ? 5 : 6);
expect_iterator_items(i, expect, NULL, expect, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
git_tree_free(tree);
}
void test_iterator_tree__pathlist_icase(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "a"));
cl_git_pass(git_vector_insert(&filelist, "B"));
cl_git_pass(git_vector_insert(&filelist, "c"));
cl_git_pass(git_vector_insert(&filelist, "D"));
cl_git_pass(git_vector_insert(&filelist, "e"));
cl_git_pass(git_vector_insert(&filelist, "k.a"));
cl_git_pass(git_vector_insert(&filelist, "k.b"));
cl_git_pass(git_vector_insert(&filelist, "k/1"));
cl_git_pass(git_vector_insert(&filelist, "k/a"));
cl_git_pass(git_vector_insert(&filelist, "kZZZZ"));
cl_git_pass(git_vector_insert(&filelist, "L/1"));
g_repo = cl_git_sandbox_init("icase");
git_repository_head_tree(&tree, g_repo);
i_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 3, NULL, 3, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 1, NULL, 1, NULL);
git_iterator_free(i);
i_opts.flags = GIT_ITERATOR_IGNORE_CASE;
i_opts.start = "c";
i_opts.end = "k/D";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 5, NULL, 5, NULL);
git_iterator_free(i);
i_opts.start = "k";
i_opts.end = "k/Z";
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, 2, NULL, 2, NULL);
git_iterator_free(i);
git_vector_free(&filelist);
git_tree_free(tree);
}
void test_iterator_tree__pathlist_with_directory(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
const char *expected[] = { "subdir/README", "subdir/new.txt",
"subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len = 4;
const char *expected2[] = { "subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len2 = 2;
g_repo = cl_git_sandbox_init("testrepo2");
git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "subdir/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_vector_clear(&filelist);
cl_git_pass(git_vector_insert(&filelist, "subdir/subdir2"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len2, expected2, expected_len2, expected2);
git_iterator_free(i);
git_tree_free(tree);
git_vector_free(&filelist);
}
void test_iterator_tree__pathlist_with_directory_include_tree_nodes(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
const char *expected[] = { "subdir/", "subdir/README", "subdir/new.txt",
"subdir/subdir2/", "subdir/subdir2/README", "subdir/subdir2/new.txt" };
size_t expected_len = 6;
g_repo = cl_git_sandbox_init("testrepo2");
git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "subdir"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
i_opts.flags |= GIT_ITERATOR_DONT_IGNORE_CASE | GIT_ITERATOR_INCLUDE_TREES;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
expect_iterator_items(i, expected_len, expected, expected_len, expected);
git_iterator_free(i);
git_tree_free(tree);
git_vector_free(&filelist);
}
void test_iterator_tree__pathlist_no_match(void)
{
git_iterator *i;
git_iterator_options i_opts = GIT_ITERATOR_OPTIONS_INIT;
git_vector filelist;
git_tree *tree;
const git_index_entry *entry;
g_repo = cl_git_sandbox_init("testrepo2");
git_repository_head_tree(&tree, g_repo);
cl_git_pass(git_vector_init(&filelist, 100, &git__strcmp_cb));
cl_git_pass(git_vector_insert(&filelist, "nonexistent/"));
i_opts.pathlist.strings = (char **)filelist.contents;
i_opts.pathlist.count = filelist.length;
cl_git_pass(git_iterator_for_tree(&i, tree, &i_opts));
cl_assert_equal_i(GIT_ITEROVER, git_iterator_current(&entry, i));
git_iterator_free(i);
git_tree_free(tree);
git_vector_free(&filelist);
}
| libgit2-main | tests/libgit2/iterator/tree.c |
#include "clar_libgit2.h"
#include "patch.h"
#include "patch_parse.h"
#include "patch_common.h"
/* sanity check the round-trip of patch parsing: ensure that we can parse
* and then print a variety of patch files.
*/
static void patch_print_from_patchfile(const char *data, size_t len)
{
git_patch *patch;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_patch_from_buffer(&patch, data, len, NULL));
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_s(data, buf.ptr);
git_patch_free(patch);
git_buf_dispose(&buf);
}
void test_patch_print__change_middle(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_CHANGE_MIDDLE,
strlen(PATCH_ORIGINAL_TO_CHANGE_MIDDLE));
}
void test_patch_print__change_middle_nocontext(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_CHANGE_MIDDLE_NOCONTEXT,
strlen(PATCH_ORIGINAL_TO_CHANGE_MIDDLE_NOCONTEXT));
}
void test_patch_print__change_firstline(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE,
strlen(PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE));
}
void test_patch_print__change_lastline(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_CHANGE_LASTLINE,
strlen(PATCH_ORIGINAL_TO_CHANGE_LASTLINE));
}
void test_patch_print__prepend(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_PREPEND,
strlen(PATCH_ORIGINAL_TO_PREPEND));
}
void test_patch_print__prepend_nocontext(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT,
strlen(PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT));
}
void test_patch_print__append(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_APPEND,
strlen(PATCH_ORIGINAL_TO_APPEND));
}
void test_patch_print__append_nocontext(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_APPEND_NOCONTEXT,
strlen(PATCH_ORIGINAL_TO_APPEND_NOCONTEXT));
}
void test_patch_print__prepend_and_append(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_PREPEND_AND_APPEND,
strlen(PATCH_ORIGINAL_TO_PREPEND_AND_APPEND));
}
void test_patch_print__to_empty_file(void)
{
patch_print_from_patchfile(PATCH_ORIGINAL_TO_EMPTY_FILE,
strlen(PATCH_ORIGINAL_TO_EMPTY_FILE));
}
void test_patch_print__from_empty_file(void)
{
patch_print_from_patchfile(PATCH_EMPTY_FILE_TO_ORIGINAL,
strlen(PATCH_EMPTY_FILE_TO_ORIGINAL));
}
void test_patch_print__add(void)
{
patch_print_from_patchfile(PATCH_ADD_ORIGINAL,
strlen(PATCH_ADD_ORIGINAL));
}
void test_patch_print__delete(void)
{
patch_print_from_patchfile(PATCH_DELETE_ORIGINAL,
strlen(PATCH_DELETE_ORIGINAL));
}
void test_patch_print__rename_exact(void)
{
patch_print_from_patchfile(PATCH_RENAME_EXACT,
strlen(PATCH_RENAME_EXACT));
}
void test_patch_print__rename_exact_with_mode(void)
{
patch_print_from_patchfile(PATCH_RENAME_EXACT_WITH_MODE,
strlen(PATCH_RENAME_EXACT_WITH_MODE));
}
void test_patch_print__rename_similar(void)
{
patch_print_from_patchfile(PATCH_RENAME_SIMILAR,
strlen(PATCH_RENAME_SIMILAR));
}
void test_patch_print__rename_exact_quotedname(void)
{
patch_print_from_patchfile(PATCH_RENAME_EXACT_QUOTEDNAME,
strlen(PATCH_RENAME_EXACT_QUOTEDNAME));
}
void test_patch_print__rename_similar_quotedname(void)
{
patch_print_from_patchfile(PATCH_RENAME_SIMILAR_QUOTEDNAME,
strlen(PATCH_RENAME_SIMILAR_QUOTEDNAME));
}
void test_patch_print__modechange_unchanged(void)
{
patch_print_from_patchfile(PATCH_MODECHANGE_UNCHANGED,
strlen(PATCH_MODECHANGE_UNCHANGED));
}
void test_patch_print__modechange_modified(void)
{
patch_print_from_patchfile(PATCH_MODECHANGE_MODIFIED,
strlen(PATCH_MODECHANGE_MODIFIED));
}
void test_patch_print__binary_literal(void)
{
patch_print_from_patchfile(PATCH_BINARY_LITERAL,
strlen(PATCH_BINARY_LITERAL));
}
void test_patch_print__binary_delta(void)
{
patch_print_from_patchfile(PATCH_BINARY_DELTA,
strlen(PATCH_BINARY_DELTA));
}
void test_patch_print__binary_add(void)
{
patch_print_from_patchfile(PATCH_BINARY_ADD,
strlen(PATCH_BINARY_ADD));
}
void test_patch_print__binary_delete(void)
{
patch_print_from_patchfile(PATCH_BINARY_DELETE,
strlen(PATCH_BINARY_DELETE));
}
void test_patch_print__not_reversible(void)
{
patch_print_from_patchfile(PATCH_BINARY_NOT_REVERSIBLE,
strlen(PATCH_BINARY_NOT_REVERSIBLE));
}
void test_patch_print__binary_not_shown(void)
{
patch_print_from_patchfile(PATCH_BINARY_NOT_PRINTED,
strlen(PATCH_BINARY_NOT_PRINTED));
}
void test_patch_print__binary_add_not_shown(void)
{
patch_print_from_patchfile(PATCH_ADD_BINARY_NOT_PRINTED,
strlen(PATCH_ADD_BINARY_NOT_PRINTED));
}
| libgit2-main | tests/libgit2/patch/print.c |
#include "clar_libgit2.h"
#include "patch.h"
#include "patch_parse.h"
#include "patch_common.h"
static void ensure_patch_validity(git_patch *patch)
{
const git_diff_delta *delta;
char idstr[GIT_OID_SHA1_HEXSIZE+1] = {0};
cl_assert((delta = git_patch_get_delta(patch)) != NULL);
cl_assert_equal_i(2, delta->nfiles);
cl_assert_equal_s(delta->old_file.path, "file.txt");
cl_assert(delta->old_file.mode == GIT_FILEMODE_BLOB);
cl_assert_equal_i(7, delta->old_file.id_abbrev);
git_oid_nfmt(idstr, delta->old_file.id_abbrev, &delta->old_file.id);
cl_assert_equal_s(idstr, "9432026");
cl_assert_equal_i(0, delta->old_file.size);
cl_assert_equal_s(delta->new_file.path, "file.txt");
cl_assert(delta->new_file.mode == GIT_FILEMODE_BLOB);
cl_assert_equal_i(7, delta->new_file.id_abbrev);
git_oid_nfmt(idstr, delta->new_file.id_abbrev, &delta->new_file.id);
cl_assert_equal_s(idstr, "cd8fd12");
cl_assert_equal_i(0, delta->new_file.size);
}
static void ensure_identical_patch_inout(const char *content)
{
git_buf buf = GIT_BUF_INIT;
git_patch *patch;
cl_git_pass(git_patch_from_buffer(&patch, content, strlen(content), NULL));
cl_git_pass(git_patch_to_buf(&buf, patch));
cl_assert_equal_strn(buf.ptr, content, strlen(content));
git_patch_free(patch);
git_buf_dispose(&buf);
}
void test_patch_parse__original_to_change_middle(void)
{
git_patch *patch;
cl_git_pass(git_patch_from_buffer(
&patch, PATCH_ORIGINAL_TO_CHANGE_MIDDLE,
strlen(PATCH_ORIGINAL_TO_CHANGE_MIDDLE), NULL));
ensure_patch_validity(patch);
git_patch_free(patch);
}
void test_patch_parse__leading_and_trailing_garbage(void)
{
git_patch *patch;
const char *leading = "This is some leading garbage.\n"
"Maybe it's email headers?\n"
"\n"
PATCH_ORIGINAL_TO_CHANGE_MIDDLE;
const char *trailing = PATCH_ORIGINAL_TO_CHANGE_MIDDLE
"\n"
"This is some trailing garbage.\n"
"Maybe it's an email signature?\n";
const char *both = "Here's some leading garbage\n"
PATCH_ORIGINAL_TO_CHANGE_MIDDLE
"And here's some trailing.\n";
cl_git_pass(git_patch_from_buffer(&patch, leading, strlen(leading),
NULL));
ensure_patch_validity(patch);
git_patch_free(patch);
cl_git_pass(git_patch_from_buffer(&patch, trailing, strlen(trailing),
NULL));
ensure_patch_validity(patch);
git_patch_free(patch);
cl_git_pass(git_patch_from_buffer(&patch, both, strlen(both),
NULL));
ensure_patch_validity(patch);
git_patch_free(patch);
}
void test_patch_parse__nonpatches_fail_with_notfound(void)
{
git_patch *patch;
cl_git_fail_with(GIT_ENOTFOUND,
git_patch_from_buffer(&patch, PATCH_NOT_A_PATCH,
strlen(PATCH_NOT_A_PATCH), NULL));
}
void test_patch_parse__invalid_patches_fails(void)
{
git_patch *patch;
cl_git_fail_with(GIT_ERROR,
git_patch_from_buffer(&patch, PATCH_CORRUPT_GIT_HEADER,
strlen(PATCH_CORRUPT_GIT_HEADER), NULL));
cl_git_fail_with(GIT_ERROR,
git_patch_from_buffer(&patch,
PATCH_CORRUPT_MISSING_NEW_FILE,
strlen(PATCH_CORRUPT_MISSING_NEW_FILE), NULL));
cl_git_fail_with(GIT_ERROR,
git_patch_from_buffer(&patch,
PATCH_CORRUPT_MISSING_OLD_FILE,
strlen(PATCH_CORRUPT_MISSING_OLD_FILE), NULL));
cl_git_fail_with(GIT_ERROR,
git_patch_from_buffer(&patch, PATCH_CORRUPT_NO_CHANGES,
strlen(PATCH_CORRUPT_NO_CHANGES), NULL));
cl_git_fail_with(GIT_ERROR,
git_patch_from_buffer(&patch,
PATCH_CORRUPT_MISSING_HUNK_HEADER,
strlen(PATCH_CORRUPT_MISSING_HUNK_HEADER), NULL));
}
void test_patch_parse__no_newline_at_end_of_new_file(void)
{
ensure_identical_patch_inout(PATCH_APPEND_NO_NL);
}
void test_patch_parse__no_newline_at_end_of_old_file(void)
{
ensure_identical_patch_inout(PATCH_APPEND_NO_NL_IN_OLD_FILE);
}
void test_patch_parse__files_with_whitespaces_succeeds(void)
{
ensure_identical_patch_inout(PATCH_NAME_WHITESPACE);
}
void test_patch_parse__lifetime_of_patch_does_not_depend_on_buffer(void)
{
git_str diff = GIT_STR_INIT;
git_buf rendered = GIT_BUF_INIT;
git_patch *patch;
cl_git_pass(git_str_sets(&diff, PATCH_ORIGINAL_TO_CHANGE_MIDDLE));
cl_git_pass(git_patch_from_buffer(&patch, diff.ptr, diff.size, NULL));
git_str_dispose(&diff);
cl_git_pass(git_patch_to_buf(&rendered, patch));
cl_assert_equal_s(PATCH_ORIGINAL_TO_CHANGE_MIDDLE, rendered.ptr);
git_buf_dispose(&rendered);
cl_git_pass(git_patch_to_buf(&rendered, patch));
cl_assert_equal_s(PATCH_ORIGINAL_TO_CHANGE_MIDDLE, rendered.ptr);
git_buf_dispose(&rendered);
git_patch_free(patch);
}
void test_patch_parse__binary_file_with_missing_paths(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_MISSING_PATHS,
strlen(PATCH_BINARY_FILE_WITH_MISSING_PATHS), NULL));
}
void test_patch_parse__binary_file_with_whitespace_paths(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_WHITESPACE_PATHS,
strlen(PATCH_BINARY_FILE_WITH_WHITESPACE_PATHS), NULL));
}
void test_patch_parse__binary_file_with_empty_quoted_paths(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_QUOTED_EMPTY_PATHS,
strlen(PATCH_BINARY_FILE_WITH_QUOTED_EMPTY_PATHS), NULL));
}
void test_patch_parse__binary_file_path_with_spaces(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_PATH_WITH_SPACES,
strlen(PATCH_BINARY_FILE_PATH_WITH_SPACES), NULL));
}
void test_patch_parse__binary_file_path_without_body_paths(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_PATH_WITHOUT_BODY_PATHS,
strlen(PATCH_BINARY_FILE_PATH_WITHOUT_BODY_PATHS), NULL));
}
void test_patch_parse__binary_file_with_truncated_delta(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_TRUNCATED_DELTA,
strlen(PATCH_BINARY_FILE_WITH_TRUNCATED_DELTA), NULL));
cl_assert_equal_s(git_error_last()->message, "truncated binary data at line 5");
}
void test_patch_parse__memory_leak_on_multiple_paths(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_MULTIPLE_OLD_PATHS, strlen(PATCH_MULTIPLE_OLD_PATHS), NULL));
}
void test_patch_parse__truncated_no_newline_at_end_of_file(void)
{
size_t len = strlen(PATCH_APPEND_NO_NL) - strlen("at end of file\n");
const git_diff_line *line;
git_patch *patch;
cl_git_pass(git_patch_from_buffer(&patch, PATCH_APPEND_NO_NL, len, NULL));
cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 4));
cl_assert_equal_s(line->content, "\\ No newline ");
git_patch_free(patch);
}
void test_patch_parse__line_number_overflow(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_buffer(&patch, PATCH_INTMAX_NEW_LINES, strlen(PATCH_INTMAX_NEW_LINES), NULL));
git_patch_free(patch);
}
| libgit2-main | tests/libgit2/patch/parse.c |
#include "clar_libgit2.h"
#include "git2/sys/odb_backend.h"
#include "odb.h"
typedef struct {
git_odb_backend base;
size_t position;
} fake_backend;
static git_odb_backend *new_backend(size_t position)
{
fake_backend *b;
b = git__calloc(1, sizeof(fake_backend));
if (b == NULL)
return NULL;
b->base.free = (void (*)(git_odb_backend *)) git__free;
b->base.version = GIT_ODB_BACKEND_VERSION;
b->position = position;
return (git_odb_backend *)b;
}
static void check_backend_sorting(git_odb *odb)
{
size_t i, max_i = git_odb_num_backends(odb);
fake_backend *internal;
for (i = 0; i < max_i; ++i) {
cl_git_pass(git_odb_get_backend((git_odb_backend **)&internal, odb, i));
cl_assert(internal != NULL);
cl_assert_equal_sz(i, internal->position);
}
}
static git_odb *_odb;
void test_odb_sorting__initialize(void)
{
cl_git_pass(git_odb__new(&_odb, NULL));
}
void test_odb_sorting__cleanup(void)
{
git_odb_free(_odb);
_odb = NULL;
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ODB_LOOSE_PRIORITY,
GIT_ODB_DEFAULT_LOOSE_PRIORITY));
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ODB_PACKED_PRIORITY,
GIT_ODB_DEFAULT_PACKED_PRIORITY));
}
void test_odb_sorting__basic_backends_sorting(void)
{
cl_git_pass(git_odb_add_backend(_odb, new_backend(0), 5));
cl_git_pass(git_odb_add_backend(_odb, new_backend(2), 3));
cl_git_pass(git_odb_add_backend(_odb, new_backend(1), 4));
cl_git_pass(git_odb_add_backend(_odb, new_backend(3), 1));
check_backend_sorting(_odb);
}
void test_odb_sorting__alternate_backends_sorting(void)
{
cl_git_pass(git_odb_add_backend(_odb, new_backend(1), 5));
cl_git_pass(git_odb_add_backend(_odb, new_backend(5), 3));
cl_git_pass(git_odb_add_backend(_odb, new_backend(3), 4));
cl_git_pass(git_odb_add_backend(_odb, new_backend(7), 1));
cl_git_pass(git_odb_add_alternate(_odb, new_backend(0), 5));
cl_git_pass(git_odb_add_alternate(_odb, new_backend(4), 3));
cl_git_pass(git_odb_add_alternate(_odb, new_backend(2), 4));
cl_git_pass(git_odb_add_alternate(_odb, new_backend(6), 1));
check_backend_sorting(_odb);
}
void test_odb_sorting__override_default_backend_priority(void)
{
git_odb *new_odb;
git_odb_backend *loose, *packed, *backend;
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ODB_LOOSE_PRIORITY, 5));
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ODB_PACKED_PRIORITY, 3));
git_odb_backend_pack(&packed, "./testrepo.git/objects");
git_odb__backend_loose(&loose, "./testrepo.git/objects", NULL);
cl_git_pass(git_odb__open(&new_odb, cl_fixture("testrepo.git/objects"), NULL));
cl_assert_equal_sz(2, git_odb_num_backends(new_odb));
cl_git_pass(git_odb_get_backend(&backend, new_odb, 0));
cl_assert_equal_p(loose->read, backend->read);
cl_git_pass(git_odb_get_backend(&backend, new_odb, 1));
cl_assert_equal_p(packed->read, backend->read);
git_odb_free(new_odb);
loose->free(loose);
packed->free(packed);
}
| libgit2-main | tests/libgit2/odb/sorting.c |
#include "clar_libgit2.h"
#include "odb.h"
static git_odb *_odb;
void test_odb_mixed__initialize(void)
{
cl_git_pass(git_odb__open(&_odb, cl_fixture("duplicate.git/objects"), NULL));
}
void test_odb_mixed__cleanup(void)
{
git_odb_free(_odb);
_odb = NULL;
}
void test_odb_mixed__dup_oid(void) {
const char hex[] = "ce013625030ba8dba906f756967f9e9ca394464a";
const char short_hex[] = "ce01362";
git_oid oid;
git_odb_object *obj;
cl_git_pass(git_oid__fromstr(&oid, hex, GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, GIT_OID_SHA1_HEXSIZE));
git_odb_object_free(obj);
cl_git_pass(git_odb_exists_prefix(NULL, _odb, &oid, GIT_OID_SHA1_HEXSIZE));
cl_git_pass(git_oid__fromstrn(&oid, short_hex, sizeof(short_hex) - 1, GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, sizeof(short_hex) - 1));
git_odb_object_free(obj);
cl_git_pass(git_odb_exists_prefix(NULL, _odb, &oid, sizeof(short_hex) - 1));
}
/* some known sha collisions of file content:
* 'aabqhq' and 'aaazvc' with prefix 'dea509d0' (+ '9' and + 'b')
* 'aaeufo' and 'aaaohs' with prefix '81b5bff5' (+ 'f' and + 'b')
* 'aafewy' and 'aaepta' with prefix '739e3c4c'
* 'aahsyn' and 'aadrjg' with prefix '0ddeaded' (+ '9' and + 'e')
*/
void test_odb_mixed__dup_oid_prefix_0(void) {
char hex[10];
git_oid oid, found;
git_odb_object *obj;
/* ambiguous in the same pack file */
strncpy(hex, "dea509d0", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
strncpy(hex, "dea509d09", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
cl_assert_equal_oid(&found, git_odb_object_id(obj));
git_odb_object_free(obj);
strncpy(hex, "dea509d0b", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
git_odb_object_free(obj);
/* ambiguous in different pack files */
strncpy(hex, "81b5bff5", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
strncpy(hex, "81b5bff5b", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
cl_assert_equal_oid(&found, git_odb_object_id(obj));
git_odb_object_free(obj);
strncpy(hex, "81b5bff5f", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
git_odb_object_free(obj);
/* ambiguous in pack file and loose */
strncpy(hex, "0ddeaded", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
strncpy(hex, "0ddeaded9", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
cl_assert_equal_oid(&found, git_odb_object_id(obj));
git_odb_object_free(obj);
strncpy(hex, "0ddeadede", sizeof(hex));
cl_git_pass(git_oid__fromstrn(&oid, hex, strlen(hex), GIT_OID_SHA1));
cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
git_odb_object_free(obj);
}
struct expand_id_test_data {
char *lookup_id;
char *expected_id;
git_object_t expected_type;
};
struct expand_id_test_data expand_id_test_data[] = {
/* some prefixes and their expected values */
{ "dea509d0", NULL, GIT_OBJECT_ANY },
{ "00000000", NULL, GIT_OBJECT_ANY },
{ "dea509d0", NULL, GIT_OBJECT_ANY },
{ "dea509d09", "dea509d097ce692e167dfc6a48a7a280cc5e877e", GIT_OBJECT_BLOB },
{ "dea509d0b", "dea509d0b3cb8ee0650f6ca210bc83f4678851ba", GIT_OBJECT_BLOB },
{ "ce0136250", "ce013625030ba8dba906f756967f9e9ca394464a", GIT_OBJECT_BLOB },
{ "0ddeaded", NULL, GIT_OBJECT_ANY },
{ "4d5979b", "4d5979b468252190cb572ae758aca36928e8a91e", GIT_OBJECT_TREE },
{ "0ddeaded", NULL, GIT_OBJECT_ANY },
{ "0ddeadede", "0ddeadede9e6d6ccddce0ee1e5749eed0485e5ea", GIT_OBJECT_BLOB },
{ "0ddeaded9", "0ddeaded9502971eefe1e41e34d0e536853ae20f", GIT_OBJECT_BLOB },
{ "f00b4e", NULL, GIT_OBJECT_ANY },
/* this OID is too short and should be ambiguous! */
{ "f00", NULL, GIT_OBJECT_ANY },
/* some full-length object ids */
{ "0000000000000000000000000000000000000000", NULL, GIT_OBJECT_ANY },
{
"dea509d097ce692e167dfc6a48a7a280cc5e877e",
"dea509d097ce692e167dfc6a48a7a280cc5e877e",
GIT_OBJECT_BLOB
},
{ "f00f00f00f00f00f00f00f00f00f00f00f00f00f", NULL, GIT_OBJECT_ANY },
{
"4d5979b468252190cb572ae758aca36928e8a91e",
"4d5979b468252190cb572ae758aca36928e8a91e",
GIT_OBJECT_TREE
},
/*
* ensure we're not leaking the return error code for the
* last lookup if the last object is invalid
*/
{ "0ddeadedfff", NULL, GIT_OBJECT_ANY },
};
static void setup_prefix_query(
git_odb_expand_id **out_ids,
size_t *out_num)
{
git_odb_expand_id *ids;
size_t num, i;
num = ARRAY_SIZE(expand_id_test_data);
cl_assert((ids = git__calloc(num, sizeof(git_odb_expand_id))));
for (i = 0; i < num; i++) {
git_odb_expand_id *id = &ids[i];
size_t len = strlen(expand_id_test_data[i].lookup_id);
git_oid__fromstrn(&id->id, expand_id_test_data[i].lookup_id, len, GIT_OID_SHA1);
id->length = (unsigned short)len;
id->type = expand_id_test_data[i].expected_type;
}
*out_ids = ids;
*out_num = num;
}
static void assert_found_objects(git_odb_expand_id *ids)
{
size_t num, i;
num = ARRAY_SIZE(expand_id_test_data);
for (i = 0; i < num; i++) {
git_oid expected_id = GIT_OID_SHA1_ZERO;
size_t expected_len = 0;
git_object_t expected_type = 0;
if (expand_id_test_data[i].expected_id) {
git_oid__fromstr(&expected_id, expand_id_test_data[i].expected_id, GIT_OID_SHA1);
expected_len = GIT_OID_SHA1_HEXSIZE;
expected_type = expand_id_test_data[i].expected_type;
}
cl_assert_equal_oid(&expected_id, &ids[i].id);
cl_assert_equal_i(expected_len, ids[i].length);
cl_assert_equal_i(expected_type, ids[i].type);
}
}
static void assert_notfound_objects(git_odb_expand_id *ids)
{
git_oid expected_id = GIT_OID_SHA1_ZERO;
size_t num, i;
num = ARRAY_SIZE(expand_id_test_data);
for (i = 0; i < num; i++) {
cl_assert_equal_oid(&expected_id, &ids[i].id);
cl_assert_equal_i(0, ids[i].length);
cl_assert_equal_i(0, ids[i].type);
}
}
void test_odb_mixed__expand_ids(void)
{
git_odb_expand_id *ids;
size_t i, num;
/* test looking for the actual (correct) types */
setup_prefix_query(&ids, &num);
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
assert_found_objects(ids);
git__free(ids);
/* test looking for an explicit `type == 0` */
setup_prefix_query(&ids, &num);
for (i = 0; i < num; i++)
ids[i].type = 0;
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
assert_found_objects(ids);
git__free(ids);
/* test looking for an explicit GIT_OBJECT_ANY */
setup_prefix_query(&ids, &num);
for (i = 0; i < num; i++)
ids[i].type = GIT_OBJECT_ANY;
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
assert_found_objects(ids);
git__free(ids);
/* test looking for the completely wrong type */
setup_prefix_query(&ids, &num);
for (i = 0; i < num; i++)
ids[i].type = (ids[i].type == GIT_OBJECT_BLOB) ?
GIT_OBJECT_TREE : GIT_OBJECT_BLOB;
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
assert_notfound_objects(ids);
git__free(ids);
}
void test_odb_mixed__expand_ids_cached(void)
{
git_odb_expand_id *ids;
size_t i, num;
/* test looking for the actual (correct) types after accessing the object */
setup_prefix_query(&ids, &num);
for (i = 0; i < num; i++) {
git_odb_object *obj;
if (ids[i].type == GIT_OBJECT_ANY)
continue;
cl_git_pass(git_odb_read_prefix(&obj, _odb, &ids[i].id, ids[i].length));
git_odb_object_free(obj);
}
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
assert_found_objects(ids);
git__free(ids);
}
| libgit2-main | tests/libgit2/odb/mixed.c |
#include "clar_libgit2.h"
#include "git2/odb_backend.h"
static git_repository *repo;
static git_odb *odb;
static git_odb_stream *stream;
void test_odb_streamwrite__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_odb(&odb, repo));
cl_git_pass(git_odb_open_wstream(&stream, odb, 14, GIT_OBJECT_BLOB));
cl_assert_equal_sz(14, stream->declared_size);
}
void test_odb_streamwrite__cleanup(void)
{
git_odb_stream_free(stream);
git_odb_free(odb);
cl_git_sandbox_cleanup();
}
void test_odb_streamwrite__can_accept_chunks(void)
{
git_oid oid;
cl_git_pass(git_odb_stream_write(stream, "deadbeef", 8));
cl_assert_equal_sz(8, stream->received_bytes);
cl_git_pass(git_odb_stream_write(stream, "deadbeef", 6));
cl_assert_equal_sz(8 + 6, stream->received_bytes);
cl_git_pass(git_odb_stream_finalize_write(&oid, stream));
}
void test_odb_streamwrite__can_detect_missing_bytes(void)
{
git_oid oid;
cl_git_pass(git_odb_stream_write(stream, "deadbeef", 8));
cl_assert_equal_sz(8, stream->received_bytes);
cl_git_pass(git_odb_stream_write(stream, "deadbeef", 4));
cl_assert_equal_sz(8 + 4, stream->received_bytes);
cl_git_fail(git_odb_stream_finalize_write(&oid, stream));
}
void test_odb_streamwrite__can_detect_additional_bytes(void)
{
cl_git_pass(git_odb_stream_write(stream, "deadbeef", 8));
cl_assert_equal_sz(8, stream->received_bytes);
cl_git_fail(git_odb_stream_write(stream, "deadbeef", 7));
}
| libgit2-main | tests/libgit2/odb/streamwrite.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "git2/odb_backend.h"
#include "pack.h"
static git_odb *_odb;
static git_repository *_repo;
void test_odb_foreach__cleanup(void)
{
git_odb_free(_odb);
git_repository_free(_repo);
_odb = NULL;
_repo = NULL;
}
static int foreach_cb(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return 0;
}
/*
* $ git --git-dir tests/resources/testrepo.git count-objects --verbose
* count: 60
* size: 240
* in-pack: 1640
* packs: 3
* size-pack: 425
* prune-packable: 0
* garbage: 0
*/
void test_odb_foreach__foreach(void)
{
int nobj = 0;
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
git_repository_odb(&_odb, _repo);
cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
}
void test_odb_foreach__one_pack(void)
{
git_odb_backend *backend = NULL;
int nobj = 0;
cl_git_pass(git_odb__new(&_odb, NULL));
cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
cl_git_pass(git_odb_add_backend(_odb, backend, 1));
_repo = NULL;
cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
cl_assert(nobj == 1628);
}
static int foreach_stop_cb(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return (*nobj == 1000) ? -321 : 0;
}
static int foreach_stop_first_cb(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return -123;
}
static int foreach_stop_cb_positive_ret(const git_oid *oid, void *data)
{
int *nobj = data;
(*nobj)++;
GIT_UNUSED(oid);
return (*nobj == 1000) ? 321 : 0;
}
void test_odb_foreach__interrupt_foreach(void)
{
int nobj = 0;
git_oid id;
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
git_repository_odb(&_odb, _repo);
cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
cl_assert(nobj == 1000);
nobj = 0;
cl_assert_equal_i(321, git_odb_foreach(_odb, foreach_stop_cb_positive_ret, &nobj));
cl_assert(nobj == 1000);
git_odb_free(_odb);
git_repository_free(_repo);
cl_git_pass(git_repository_init(&_repo, "onlyloose.git", true));
git_repository_odb(&_odb, _repo);
cl_git_pass(git_odb_write(&id, _odb, "", 0, GIT_OBJECT_BLOB));
cl_assert_equal_i(-123, git_odb_foreach(_odb, foreach_stop_first_cb, &nobj));
}
void test_odb_foreach__files_in_objects_dir(void)
{
git_repository *repo;
git_odb *odb;
git_str buf = GIT_STR_INIT;
int nobj = 0;
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_git_pass(git_str_joinpath(&buf, git_repository_path(repo), "objects/somefile"));
cl_git_mkfile(buf.ptr, "");
git_str_dispose(&buf);
cl_git_pass(git_repository_odb(&odb, repo));
cl_git_pass(git_odb_foreach(odb, foreach_cb, &nobj));
cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
git_odb_free(odb);
git_repository_free(repo);
cl_fixture_cleanup("testrepo.git");
}
| libgit2-main | tests/libgit2/odb/foreach.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "posix.h"
static git_repository *repo;
static git_odb *odb;
void test_odb_freshen__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_odb(&odb, repo));
}
void test_odb_freshen__cleanup(void)
{
git_odb_free(odb);
cl_git_sandbox_cleanup();
}
static void set_time_wayback(struct stat *out, const char *fn)
{
git_str fullpath = GIT_STR_INIT;
struct p_timeval old[2];
old[0].tv_sec = 1234567890;
old[0].tv_usec = 0;
old[1].tv_sec = 1234567890;
old[1].tv_usec = 0;
git_str_joinpath(&fullpath, "testrepo.git/objects", fn);
cl_must_pass(p_utimes(git_str_cstr(&fullpath), old));
cl_must_pass(p_lstat(git_str_cstr(&fullpath), out));
git_str_dispose(&fullpath);
}
#define LOOSE_STR "my new file\n"
#define LOOSE_BLOB_ID "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"
#define LOOSE_BLOB_FN "a7/1586c1dfe8a71c6cbf6c129f404c5642ff31bd"
void test_odb_freshen__loose_blob(void)
{
git_oid expected_id, id;
struct stat before, after;
cl_git_pass(git_oid__fromstr(&expected_id, LOOSE_BLOB_ID, GIT_OID_SHA1));
set_time_wayback(&before, LOOSE_BLOB_FN);
/* make sure we freshen a blob */
cl_git_pass(git_blob_create_from_buffer(&id, repo, LOOSE_STR, CONST_STRLEN(LOOSE_STR)));
cl_assert_equal_oid(&expected_id, &id);
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_BLOB_FN, &after));
cl_assert(before.st_atime < after.st_atime);
cl_assert(before.st_mtime < after.st_mtime);
}
#define UNIQUE_STR "doesnt exist in the odb yet\n"
#define UNIQUE_BLOB_ID "78a87d0b8878c5953b9a63015ff4e22a3d898826"
#define UNIQUE_BLOB_FN "78/a87d0b8878c5953b9a63015ff4e22a3d898826"
void test_odb_freshen__readonly_object(void)
{
git_oid expected_id, id;
struct stat before, after;
cl_git_pass(git_oid__fromstr(&expected_id, UNIQUE_BLOB_ID, GIT_OID_SHA1));
cl_git_pass(git_blob_create_from_buffer(&id, repo, UNIQUE_STR, CONST_STRLEN(UNIQUE_STR)));
cl_assert_equal_oid(&expected_id, &id);
set_time_wayback(&before, UNIQUE_BLOB_FN);
cl_assert((before.st_mode & S_IWUSR) == 0);
cl_git_pass(git_blob_create_from_buffer(&id, repo, UNIQUE_STR, CONST_STRLEN(UNIQUE_STR)));
cl_assert_equal_oid(&expected_id, &id);
cl_must_pass(p_lstat("testrepo.git/objects/" UNIQUE_BLOB_FN, &after));
cl_assert(before.st_atime < after.st_atime);
cl_assert(before.st_mtime < after.st_mtime);
}
#define LOOSE_TREE_ID "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
#define LOOSE_TREE_FN "94/4c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
void test_odb_freshen__loose_tree(void)
{
git_oid expected_id, id;
git_tree *tree;
struct stat before, after;
cl_git_pass(git_oid__fromstr(&expected_id, LOOSE_TREE_ID, GIT_OID_SHA1));
set_time_wayback(&before, LOOSE_TREE_FN);
cl_git_pass(git_tree_lookup(&tree, repo, &expected_id));
cl_git_pass(git_tree_create_updated(&id, repo, tree, 0, NULL));
/* make sure we freshen a tree */
cl_assert_equal_oid(&expected_id, &id);
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
cl_assert(before.st_atime < after.st_atime);
cl_assert(before.st_mtime < after.st_mtime);
git_tree_free(tree);
}
void test_odb_freshen__tree_during_commit(void)
{
git_oid tree_id, parent_id, commit_id;
git_tree *tree;
git_commit *parent;
git_signature *signature;
struct stat before, after;
cl_git_pass(git_oid__fromstr(&tree_id, LOOSE_TREE_ID, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
set_time_wayback(&before, LOOSE_TREE_FN);
cl_git_pass(git_oid__fromstr(&parent_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&parent, repo, &parent_id));
cl_git_pass(git_signature_new(&signature,
"Refresher", "[email protected]", 1488547083, 0));
cl_git_pass(git_commit_create(&commit_id, repo, NULL,
signature, signature, NULL, "New commit pointing to old tree",
tree, 1, (const git_commit **)&parent));
/* make sure we freshen the tree the commit points to */
cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_TREE_FN, &after));
cl_assert(before.st_atime < after.st_atime);
cl_assert(before.st_mtime < after.st_mtime);
git_signature_free(signature);
git_commit_free(parent);
git_tree_free(tree);
}
#define PACKED_STR "Testing a readme.txt\n"
#define PACKED_ID "6336846bd5c88d32f93ae57d846683e61ab5c530"
#define PACKED_FN "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.pack"
void test_odb_freshen__packed_object(void)
{
git_oid expected_id, id;
struct stat before, after;
struct p_timeval old_times[2];
cl_git_pass(git_oid__fromstr(&expected_id, PACKED_ID, GIT_OID_SHA1));
old_times[0].tv_sec = 1234567890;
old_times[0].tv_usec = 0;
old_times[1].tv_sec = 1234567890;
old_times[1].tv_usec = 0;
/* set time to way back */
cl_must_pass(p_utimes("testrepo.git/objects/pack/" PACKED_FN, old_times));
cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &before));
/* ensure that packfile is freshened */
cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
CONST_STRLEN(PACKED_STR), GIT_OBJECT_BLOB));
cl_assert_equal_oid(&expected_id, &id);
cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
cl_assert(before.st_atime < after.st_atime);
cl_assert(before.st_mtime < after.st_mtime);
memcpy(&before, &after, sizeof(struct stat));
/* ensure that the pack file is not freshened again immediately */
cl_git_pass(git_odb_write(&id, odb, PACKED_STR,
CONST_STRLEN(PACKED_STR), GIT_OBJECT_BLOB));
cl_assert_equal_oid(&expected_id, &id);
cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after));
cl_assert(before.st_atime == after.st_atime);
cl_assert(before.st_atime_nsec == after.st_atime_nsec);
cl_assert(before.st_mtime == after.st_mtime);
cl_assert(before.st_mtime_nsec == after.st_mtime_nsec);
}
| libgit2-main | tests/libgit2/odb/freshen.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "pack_data.h"
static git_odb *_odb;
void test_odb_packed__initialize(void)
{
cl_git_pass(git_odb__open(&_odb, cl_fixture("testrepo.git/objects"), NULL));
}
void test_odb_packed__cleanup(void)
{
git_odb_free(_odb);
_odb = NULL;
}
void test_odb_packed__mass_read(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
git_oid id;
git_odb_object *obj;
cl_git_pass(git_oid__fromstr(&id, packed_objects[i], GIT_OID_SHA1));
cl_assert(git_odb_exists(_odb, &id) == 1);
cl_git_pass(git_odb_read(&obj, _odb, &id));
git_odb_object_free(obj);
}
}
void test_odb_packed__read_header_0(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
git_oid id;
git_odb_object *obj;
size_t len;
git_object_t type;
cl_git_pass(git_oid__fromstr(&id, packed_objects[i], GIT_OID_SHA1));
cl_git_pass(git_odb_read(&obj, _odb, &id));
cl_git_pass(git_odb_read_header(&len, &type, _odb, &id));
cl_assert(obj->cached.size == len);
cl_assert(obj->cached.type == type);
git_odb_object_free(obj);
}
}
void test_odb_packed__read_header_1(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) {
git_oid id;
git_odb_object *obj;
size_t len;
git_object_t type;
cl_git_pass(git_oid__fromstr(&id, loose_objects[i], GIT_OID_SHA1));
cl_assert(git_odb_exists(_odb, &id) == 1);
cl_git_pass(git_odb_read(&obj, _odb, &id));
cl_git_pass(git_odb_read_header(&len, &type, _odb, &id));
cl_assert(obj->cached.size == len);
cl_assert(obj->cached.type == type);
git_odb_object_free(obj);
}
}
| libgit2-main | tests/libgit2/odb/packed.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "filebuf.h"
#define TEST_REPO_PATH "redundant.git"
git_repository *g_repo;
git_odb *g_odb;
void test_odb_emptyobjects__initialize(void)
{
g_repo = cl_git_sandbox_init(TEST_REPO_PATH);
cl_git_pass(git_repository_odb(&g_odb, g_repo));
}
void test_odb_emptyobjects__cleanup(void)
{
git_odb_free(g_odb);
cl_git_sandbox_cleanup();
}
void test_odb_emptyobjects__blob_notfound(void)
{
git_oid id, written_id;
git_blob *blob;
cl_git_pass(git_oid__fromstr(&id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", GIT_OID_SHA1));
cl_git_fail_with(GIT_ENOTFOUND, git_blob_lookup(&blob, g_repo, &id));
cl_git_pass(git_odb_write(&written_id, g_odb, "", 0, GIT_OBJECT_BLOB));
cl_assert(git_fs_path_exists(TEST_REPO_PATH "/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
}
void test_odb_emptyobjects__read_tree(void)
{
git_oid id;
git_tree *tree;
cl_git_pass(git_oid__fromstr(&id, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_assert_equal_i(GIT_OBJECT_TREE, git_object_type((git_object *) tree));
cl_assert_equal_i(0, git_tree_entrycount(tree));
cl_assert_equal_p(NULL, git_tree_entry_byname(tree, "foo"));
git_tree_free(tree);
}
void test_odb_emptyobjects__read_tree_odb(void)
{
git_oid id;
git_odb_object *tree_odb;
cl_git_pass(git_oid__fromstr(&id, "4b825dc642cb6eb9a060e54bf8d69288fbee4904", GIT_OID_SHA1));
cl_git_pass(git_odb_read(&tree_odb, g_odb, &id));
cl_assert(git_odb_object_data(tree_odb));
cl_assert_equal_s("", git_odb_object_data(tree_odb));
cl_assert_equal_i(0, git_odb_object_size(tree_odb));
git_odb_object_free(tree_odb);
}
| libgit2-main | tests/libgit2/odb/emptyobjects.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "git2/odb_backend.h"
#include "posix.h"
#include "loose_data.h"
#include "repository.h"
#ifdef __ANDROID_API__
# define S_IREAD S_IRUSR
# define S_IWRITE S_IWUSR
#endif
static void write_object_files(object_data *d)
{
int fd;
if (p_mkdir(d->dir, GIT_OBJECT_DIR_MODE) < 0)
cl_assert(errno == EEXIST);
cl_assert((fd = p_creat(d->file, S_IREAD | S_IWRITE)) >= 0);
cl_must_pass(p_write(fd, d->bytes, d->blen));
p_close(fd);
}
static void cmp_objects(git_rawobj *o, object_data *d)
{
cl_assert(o->type == git_object_string2type(d->type));
cl_assert(o->len == d->dlen);
if (o->len > 0)
cl_assert(memcmp(o->data, d->data, o->len) == 0);
}
static void test_read_object(object_data *data)
{
git_oid id;
git_odb_object *obj;
git_odb *odb;
git_rawobj tmp;
git_odb_options opts = GIT_ODB_OPTIONS_INIT;
opts.oid_type = data->id_type;
write_object_files(data);
cl_git_pass(git_odb__open(&odb, "test-objects", &opts));
cl_git_pass(git_oid__fromstr(&id, data->id, data->id_type));
cl_git_pass(git_odb_read(&obj, odb, &id));
tmp.data = obj->buffer;
tmp.len = obj->cached.size;
tmp.type = obj->cached.type;
cmp_objects(&tmp, data);
git_odb_object_free(obj);
git_odb_free(odb);
}
static void test_read_header(object_data *data)
{
git_oid id;
git_odb *odb;
size_t len;
git_object_t type;
git_odb_options opts = GIT_ODB_OPTIONS_INIT;
opts.oid_type = data->id_type;
write_object_files(data);
cl_git_pass(git_odb__open(&odb, "test-objects", &opts));
cl_git_pass(git_oid__fromstr(&id, data->id, data->id_type));
cl_git_pass(git_odb_read_header(&len, &type, odb, &id));
cl_assert_equal_sz(data->dlen, len);
cl_assert_equal_i(git_object_string2type(data->type), type);
git_odb_free(odb);
}
static void test_readstream_object(object_data *data, size_t blocksize)
{
git_oid id;
git_odb *odb;
git_odb_stream *stream;
git_rawobj tmp;
char buf[2048], *ptr = buf;
size_t remain;
int ret;
git_odb_options opts = GIT_ODB_OPTIONS_INIT;
opts.oid_type = data->id_type;
write_object_files(data);
cl_git_pass(git_odb__open(&odb, "test-objects", &opts));
cl_git_pass(git_oid__fromstr(&id, data->id, data->id_type));
cl_git_pass(git_odb_open_rstream(&stream, &tmp.len, &tmp.type, odb, &id));
remain = tmp.len;
while (remain) {
cl_assert((ret = git_odb_stream_read(stream, ptr, blocksize)) >= 0);
if (ret == 0)
break;
cl_assert(remain >= (size_t)ret);
remain -= ret;
ptr += ret;
}
cl_assert(remain == 0);
tmp.data = buf;
cmp_objects(&tmp, data);
git_odb_stream_free(stream);
git_odb_free(odb);
}
void test_odb_loose__initialize(void)
{
p_fsync__cnt = 0;
cl_must_pass(p_mkdir("test-objects", GIT_OBJECT_DIR_MODE));
}
void test_odb_loose__cleanup(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
cl_fixture_cleanup("test-objects");
}
void test_odb_loose__exists_sha1(void)
{
git_oid id, id2;
git_odb *odb;
write_object_files(&one);
cl_git_pass(git_odb__open(&odb, "test-objects", NULL));
cl_git_pass(git_oid__fromstr(&id, one.id, GIT_OID_SHA1));
cl_assert(git_odb_exists(odb, &id));
cl_git_pass(git_oid__fromstrp(&id, "8b137891", GIT_OID_SHA1));
cl_git_pass(git_odb_exists_prefix(&id2, odb, &id, 8));
cl_assert_equal_i(0, git_oid_streq(&id2, one.id));
/* Test for a missing object */
cl_git_pass(git_oid__fromstr(&id, "8b137891791fe96927ad78e64b0aad7bded08baa", GIT_OID_SHA1));
cl_assert(!git_odb_exists(odb, &id));
cl_git_pass(git_oid__fromstrp(&id, "8b13789a", GIT_OID_SHA1));
cl_assert_equal_i(GIT_ENOTFOUND, git_odb_exists_prefix(&id2, odb, &id, 8));
git_odb_free(odb);
}
void test_odb_loose__exists_sha256(void)
{
#ifdef GIT_EXPERIMENTAL_SHA256
git_oid id, id2;
git_odb *odb;
git_odb_options odb_opts = GIT_ODB_OPTIONS_INIT;
odb_opts.oid_type = GIT_OID_SHA256;
write_object_files(&one_sha256);
cl_git_pass(git_odb__open(&odb, "test-objects", &odb_opts));
cl_git_pass(git_oid__fromstr(&id, one_sha256.id, GIT_OID_SHA256));
cl_assert(git_odb_exists(odb, &id));
cl_git_pass(git_oid__fromstrp(&id, "4c0d52d1", GIT_OID_SHA256));
cl_git_pass(git_odb_exists_prefix(&id2, odb, &id, 8));
cl_assert_equal_i(0, git_oid_streq(&id2, one_sha256.id));
/* Test for a missing object */
cl_git_pass(git_oid__fromstr(&id, "4c0d52d180c61d01ce1a91dec5ee58f0cbe65fd59433aea803ab927965493faa", GIT_OID_SHA256));
cl_assert(!git_odb_exists(odb, &id));
cl_git_pass(git_oid__fromstrp(&id, "4c0d52da", GIT_OID_SHA256));
cl_assert_equal_i(GIT_ENOTFOUND, git_odb_exists_prefix(&id2, odb, &id, 8));
git_odb_free(odb);
#endif
}
void test_odb_loose__simple_reads_sha1(void)
{
test_read_object(&commit);
test_read_object(&tree);
test_read_object(&tag);
test_read_object(&zero);
test_read_object(&one);
test_read_object(&two);
test_read_object(&some);
}
void test_odb_loose__simple_reads_sha256(void)
{
#ifdef GIT_EXPERIMENTAL_SHA256
test_read_object(&commit_sha256);
test_read_object(&tree_sha256);
test_read_object(&tag_sha256);
test_read_object(&zero_sha256);
test_read_object(&one_sha256);
test_read_object(&two_sha256);
test_read_object(&some_sha256);
#endif
}
void test_odb_loose__streaming_reads_sha1(void)
{
size_t blocksizes[] = { 1, 2, 4, 16, 99, 1024, 123456789 };
size_t i;
for (i = 0; i < ARRAY_SIZE(blocksizes); i++) {
test_readstream_object(&commit, blocksizes[i]);
test_readstream_object(&tree, blocksizes[i]);
test_readstream_object(&tag, blocksizes[i]);
test_readstream_object(&zero, blocksizes[i]);
test_readstream_object(&one, blocksizes[i]);
test_readstream_object(&two, blocksizes[i]);
test_readstream_object(&some, blocksizes[i]);
}
}
void test_odb_loose__streaming_reads_sha256(void)
{
#ifdef GIT_EXPERIMENTAL_SHA256
size_t blocksizes[] = { 1, 2, 4, 16, 99, 1024, 123456789 };
size_t i;
for (i = 0; i < ARRAY_SIZE(blocksizes); i++) {
test_readstream_object(&commit_sha256, blocksizes[i]);
test_readstream_object(&tree_sha256, blocksizes[i]);
test_readstream_object(&tag_sha256, blocksizes[i]);
test_readstream_object(&zero_sha256, blocksizes[i]);
test_readstream_object(&one_sha256, blocksizes[i]);
test_readstream_object(&two_sha256, blocksizes[i]);
test_readstream_object(&some_sha256, blocksizes[i]);
}
#endif
}
void test_odb_loose__read_header_sha1(void)
{
test_read_header(&commit);
test_read_header(&tree);
test_read_header(&tag);
test_read_header(&zero);
test_read_header(&one);
test_read_header(&two);
test_read_header(&some);
}
void test_odb_loose__read_header_sha256(void)
{
#ifdef GIT_EXPERIMENTAL_SHA256
test_read_header(&commit_sha256);
test_read_header(&tree_sha256);
test_read_header(&tag_sha256);
test_read_header(&zero_sha256);
test_read_header(&one_sha256);
test_read_header(&two_sha256);
test_read_header(&some_sha256);
#endif
}
static void test_write_object_permission(
mode_t dir_mode, mode_t file_mode,
mode_t expected_dir_mode, mode_t expected_file_mode)
{
git_odb *odb;
git_odb_backend *backend;
git_oid oid;
struct stat statbuf;
mode_t mask, os_mask;
git_odb_backend_loose_options opts = GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT;
/* Windows does not return group/user bits from stat,
* files are never executable.
*/
#ifdef GIT_WIN32
os_mask = 0600;
#else
os_mask = 0777;
#endif
mask = p_umask(0);
p_umask(mask);
opts.dir_mode = dir_mode;
opts.file_mode = file_mode;
cl_git_pass(git_odb__new(&odb, NULL));
cl_git_pass(git_odb__backend_loose(&backend, "test-objects", &opts));
cl_git_pass(git_odb_add_backend(odb, backend, 1));
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJECT_BLOB));
cl_git_pass(p_stat("test-objects/67", &statbuf));
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected_dir_mode & ~mask) & os_mask);
cl_git_pass(p_stat("test-objects/67/b808feb36201507a77f85e6d898f0a2836e4a5", &statbuf));
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected_file_mode & ~mask) & os_mask);
git_odb_free(odb);
}
void test_odb_loose__permissions_standard(void)
{
test_write_object_permission(0, 0, GIT_OBJECT_DIR_MODE, GIT_OBJECT_FILE_MODE);
}
void test_odb_loose__permissions_readonly(void)
{
test_write_object_permission(0777, 0444, 0777, 0444);
}
void test_odb_loose__permissions_readwrite(void)
{
test_write_object_permission(0777, 0666, 0777, 0666);
}
static void write_object_to_loose_odb(int fsync)
{
git_odb *odb;
git_odb_backend *backend;
git_oid oid;
git_odb_backend_loose_options opts = GIT_ODB_BACKEND_LOOSE_OPTIONS_INIT;
if (fsync)
opts.flags |= GIT_ODB_BACKEND_LOOSE_FSYNC;
opts.dir_mode = 0777;
opts.file_mode = 0666;
cl_git_pass(git_odb__new(&odb, NULL));
cl_git_pass(git_odb__backend_loose(&backend, "test-objects", &opts));
cl_git_pass(git_odb_add_backend(odb, backend, 1));
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJECT_BLOB));
git_odb_free(odb);
}
void test_odb_loose__does_not_fsync_by_default(void)
{
write_object_to_loose_odb(0);
cl_assert_equal_sz(0, p_fsync__cnt);
}
void test_odb_loose__fsync_obeys_odb_option(void)
{
write_object_to_loose_odb(1);
cl_assert(p_fsync__cnt > 0);
}
void test_odb_loose__fsync_obeys_global_setting(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
write_object_to_loose_odb(0);
cl_assert(p_fsync__cnt > 0);
}
void test_odb_loose__fsync_obeys_repo_setting(void)
{
git_repository *repo;
git_odb *odb;
git_oid oid;
cl_git_pass(git_repository_init(&repo, "test-objects", 1));
cl_git_pass(git_repository_odb__weakptr(&odb, repo));
cl_git_pass(git_odb_write(&oid, odb, "No fsync here\n", 14, GIT_OBJECT_BLOB));
cl_assert(p_fsync__cnt == 0);
git_repository_free(repo);
cl_git_pass(git_repository_open(&repo, "test-objects"));
cl_repo_set_bool(repo, "core.fsyncObjectFiles", true);
cl_git_pass(git_repository_odb__weakptr(&odb, repo));
cl_git_pass(git_odb_write(&oid, odb, "Now fsync\n", 10, GIT_OBJECT_BLOB));
cl_assert(p_fsync__cnt > 0);
git_repository_free(repo);
}
| libgit2-main | tests/libgit2/odb/loose.c |
#include "clar_libgit2.h"
#include "git2/odb_backend.h"
#include "pack_data_one.h"
#include "pack.h"
static git_odb *_odb;
void test_odb_packed_one__initialize(void)
{
git_odb_backend *backend = NULL;
cl_git_pass(git_odb__new(&_odb, NULL));
cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
cl_git_pass(git_odb_add_backend(_odb, backend, 1));
}
void test_odb_packed_one__cleanup(void)
{
git_odb_free(_odb);
_odb = NULL;
}
void test_odb_packed_one__mass_read(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(packed_objects_one); ++i) {
git_oid id;
git_odb_object *obj;
cl_git_pass(git_oid__fromstr(&id, packed_objects_one[i], GIT_OID_SHA1));
cl_assert(git_odb_exists(_odb, &id) == 1);
cl_git_pass(git_odb_read(&obj, _odb, &id));
git_odb_object_free(obj);
}
}
void test_odb_packed_one__read_header_0(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(packed_objects_one); ++i) {
git_oid id;
git_odb_object *obj;
size_t len;
git_object_t type;
cl_git_pass(git_oid__fromstr(&id, packed_objects_one[i], GIT_OID_SHA1));
cl_git_pass(git_odb_read(&obj, _odb, &id));
cl_git_pass(git_odb_read_header(&len, &type, _odb, &id));
cl_assert(obj->cached.size == len);
cl_assert(obj->cached.type == type);
git_odb_object_free(obj);
}
}
| libgit2-main | tests/libgit2/odb/packed_one.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "filebuf.h"
static git_str destpath, filepath;
static const char *paths[] = {
"A.git", "B.git", "C.git", "D.git", "E.git", "F.git", "G.git"
};
static git_filebuf file;
static git_repository *repo;
void test_odb_alternates__cleanup(void)
{
size_t i;
git_str_dispose(&destpath);
git_str_dispose(&filepath);
for (i = 0; i < ARRAY_SIZE(paths); i++)
cl_fixture_cleanup(paths[i]);
}
static void init_linked_repo(const char *path, const char *alternate)
{
git_str_clear(&destpath);
git_str_clear(&filepath);
cl_git_pass(git_repository_init(&repo, path, 1));
cl_git_pass(git_fs_path_prettify(&destpath, alternate, NULL));
cl_git_pass(git_str_joinpath(&destpath, destpath.ptr, "objects"));
cl_git_pass(git_str_joinpath(&filepath, git_repository_path(repo), "objects/info"));
cl_git_pass(git_futils_mkdir(filepath.ptr, 0755, GIT_MKDIR_PATH));
cl_git_pass(git_str_joinpath(&filepath, filepath.ptr , "alternates"));
cl_git_pass(git_filebuf_open(&file, git_str_cstr(&filepath), 0, 0666));
git_filebuf_printf(&file, "%s\n", git_str_cstr(&destpath));
cl_git_pass(git_filebuf_commit(&file));
git_repository_free(repo);
}
void test_odb_alternates__chained(void)
{
git_commit *commit;
git_oid oid;
/* Set the alternate A -> testrepo.git */
init_linked_repo(paths[0], cl_fixture("testrepo.git"));
/* Set the alternate B -> A */
init_linked_repo(paths[1], paths[0]);
/* Now load B and see if we can find an object from testrepo.git */
cl_git_pass(git_repository_open(&repo, paths[1]));
git_oid__fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
git_commit_free(commit);
git_repository_free(repo);
}
void test_odb_alternates__long_chain(void)
{
git_commit *commit;
git_oid oid;
size_t i;
/* Set the alternate A -> testrepo.git */
init_linked_repo(paths[0], cl_fixture("testrepo.git"));
/* Set up the five-element chain */
for (i = 1; i < ARRAY_SIZE(paths); i++) {
init_linked_repo(paths[i], paths[i-1]);
}
/* Now load the last one and see if we can find an object from testrepo.git */
cl_git_pass(git_repository_open(&repo, paths[ARRAY_SIZE(paths)-1]));
git_oid__fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
cl_git_fail(git_commit_lookup(&commit, repo, &oid));
git_repository_free(repo);
}
| libgit2-main | tests/libgit2/odb/alternates.c |
#include "clar_libgit2.h"
#include "git2/odb_backend.h"
#include "hash.h"
#include "odb.h"
#define LARGEFILE_SIZE 5368709122
static git_repository *repo;
static git_odb *odb;
void test_odb_largefiles__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_odb(&odb, repo));
}
void test_odb_largefiles__cleanup(void)
{
git_odb_free(odb);
cl_git_sandbox_cleanup();
}
static void writefile(git_oid *oid)
{
static git_odb_stream *stream;
git_str buf = GIT_STR_INIT;
size_t i;
for (i = 0; i < 3041; i++)
cl_git_pass(git_str_puts(&buf, "Hello, world.\n"));
cl_git_pass(git_odb_open_wstream(&stream, odb, LARGEFILE_SIZE, GIT_OBJECT_BLOB));
for (i = 0; i < 126103; i++)
cl_git_pass(git_odb_stream_write(stream, buf.ptr, buf.size));
cl_git_pass(git_odb_stream_finalize_write(oid, stream));
git_odb_stream_free(stream);
git_str_dispose(&buf);
}
void test_odb_largefiles__write_from_memory(void)
{
git_oid expected, oid;
git_str buf = GIT_STR_INIT;
size_t i;
#ifndef GIT_ARCH_64
cl_skip();
#endif
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
!cl_is_env_set("GITTEST_SLOW"))
cl_skip();
for (i = 0; i < (3041*126103); i++)
cl_git_pass(git_str_puts(&buf, "Hello, world.\n"));
git_oid__fromstr(&expected, "3fb56989cca483b21ba7cb0a6edb229d10e1c26c", GIT_OID_SHA1);
cl_git_pass(git_odb_write(&oid, odb, buf.ptr, buf.size, GIT_OBJECT_BLOB));
cl_assert_equal_oid(&expected, &oid);
}
void test_odb_largefiles__streamwrite(void)
{
git_oid expected, oid;
#ifndef GIT_ARCH_64
cl_skip();
#endif
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
!cl_is_env_set("GITTEST_SLOW"))
cl_skip();
git_oid__fromstr(&expected, "3fb56989cca483b21ba7cb0a6edb229d10e1c26c", GIT_OID_SHA1);
writefile(&oid);
cl_assert_equal_oid(&expected, &oid);
}
void test_odb_largefiles__streamread(void)
{
git_oid oid, read_oid;
git_odb_stream *stream;
char buf[10240];
char hdr[64];
size_t len, hdr_len, total = 0;
git_hash_ctx hash;
git_object_t type;
int ret;
#ifndef GIT_ARCH_64
cl_skip();
#endif
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
!cl_is_env_set("GITTEST_SLOW"))
cl_skip();
writefile(&oid);
cl_git_pass(git_odb_open_rstream(&stream, &len, &type, odb, &oid));
cl_assert_equal_sz(LARGEFILE_SIZE, len);
cl_assert_equal_i(GIT_OBJECT_BLOB, type);
cl_git_pass(git_hash_ctx_init(&hash, GIT_HASH_ALGORITHM_SHA1));
cl_git_pass(git_odb__format_object_header(&hdr_len, hdr, sizeof(hdr), len, type));
cl_git_pass(git_hash_update(&hash, hdr, hdr_len));
while ((ret = git_odb_stream_read(stream, buf, 10240)) > 0) {
cl_git_pass(git_hash_update(&hash, buf, ret));
total += ret;
}
cl_assert_equal_sz(LARGEFILE_SIZE, total);
git_hash_final(read_oid.id, &hash);
cl_assert_equal_oid(&oid, &read_oid);
git_hash_ctx_cleanup(&hash);
git_odb_stream_free(stream);
}
void test_odb_largefiles__read_into_memory(void)
{
git_oid oid;
git_odb_object *obj;
#ifndef GIT_ARCH_64
cl_skip();
#endif
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
!cl_is_env_set("GITTEST_SLOW"))
cl_skip();
writefile(&oid);
cl_git_pass(git_odb_read(&obj, odb, &oid));
git_odb_object_free(obj);
}
void test_odb_largefiles__read_into_memory_rejected_on_32bit(void)
{
git_oid oid;
git_odb_object *obj = NULL;
#ifdef GIT_ARCH_64
cl_skip();
#endif
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
!cl_is_env_set("GITTEST_INVASIVE_MEMORY") ||
!cl_is_env_set("GITTEST_SLOW"))
cl_skip();
writefile(&oid);
cl_git_fail(git_odb_read(&obj, odb, &oid));
git_odb_object_free(obj);
}
void test_odb_largefiles__read_header(void)
{
git_oid oid;
size_t len;
git_object_t type;
#ifndef GIT_ARCH_64
cl_skip();
#endif
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
!cl_is_env_set("GITTEST_SLOW"))
cl_skip();
writefile(&oid);
cl_git_pass(git_odb_read_header(&len, &type, odb, &oid));
cl_assert_equal_sz(LARGEFILE_SIZE, len);
cl_assert_equal_i(GIT_OBJECT_BLOB, type);
}
| libgit2-main | tests/libgit2/odb/largefiles.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"
static git_repository *_repo;
static fake_backend *_fake;
#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
static const fake_object _objects[] = {
{ EXISTING_HASH, "" },
{ NULL, NULL }
};
static git_oid _nonexisting_oid;
static git_oid _existing_oid;
static void setup_repository_and_backend(void)
{
git_odb *odb = NULL;
git_odb_backend *backend = NULL;
_repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(build_fake_backend(&backend, _objects, true));
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, backend, 10));
_fake = (fake_backend *)backend;
}
void test_odb_backend_refreshing__initialize(void)
{
git_oid__fromstr(&_nonexisting_oid, NONEXISTING_HASH, GIT_OID_SHA1);
git_oid__fromstr(&_existing_oid, EXISTING_HASH, GIT_OID_SHA1);
setup_repository_and_backend();
}
void test_odb_backend_refreshing__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_odb_backend_refreshing__exists_is_invoked_twice_on_failure(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid));
cl_assert_equal_i(2, _fake->exists_calls);
cl_assert_equal_i(1, _fake->refresh_calls);
}
void test_odb_backend_refreshing__read_is_invoked_twice_on_failure(void)
{
git_object *obj;
cl_git_fail_with(
git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJECT_ANY),
GIT_ENOTFOUND);
cl_assert_equal_i(2, _fake->read_calls);
cl_assert_equal_i(1, _fake->refresh_calls);
}
void test_odb_backend_refreshing__readprefix_is_invoked_twice_on_failure(void)
{
git_object *obj;
cl_git_fail_with(
git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJECT_ANY),
GIT_ENOTFOUND);
cl_assert_equal_i(2, _fake->read_prefix_calls);
cl_assert_equal_i(1, _fake->refresh_calls);
}
void test_odb_backend_refreshing__readheader_is_invoked_twice_on_failure(void)
{
git_odb *odb;
size_t len;
git_object_t type;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_fail_with(
git_odb_read_header(&len, &type, odb, &_nonexisting_oid),
GIT_ENOTFOUND);
cl_assert_equal_i(2, _fake->read_header_calls);
cl_assert_equal_i(1, _fake->refresh_calls);
}
void test_odb_backend_refreshing__exists_is_invoked_once_on_success(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid));
cl_assert_equal_i(1, _fake->exists_calls);
cl_assert_equal_i(0, _fake->refresh_calls);
}
void test_odb_backend_refreshing__read_is_invoked_once_on_success(void)
{
git_object *obj;
cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJECT_ANY));
cl_assert_equal_i(1, _fake->read_calls);
cl_assert_equal_i(0, _fake->refresh_calls);
git_object_free(obj);
}
void test_odb_backend_refreshing__readprefix_is_invoked_once_on_success(void)
{
git_object *obj;
cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJECT_ANY));
cl_assert_equal_i(1, _fake->read_prefix_calls);
cl_assert_equal_i(0, _fake->refresh_calls);
git_object_free(obj);
}
void test_odb_backend_refreshing__readheader_is_invoked_once_on_success(void)
{
git_odb *odb;
size_t len;
git_object_t type;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid));
cl_assert_equal_i(1, _fake->read_header_calls);
cl_assert_equal_i(0, _fake->refresh_calls);
}
void test_odb_backend_refreshing__read_is_invoked_twice_when_revparsing_a_full_oid(void)
{
git_object *obj;
cl_git_fail_with(
git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
GIT_ENOTFOUND);
cl_assert_equal_i(2, _fake->read_calls);
cl_assert_equal_i(1, _fake->refresh_calls);
}
void test_odb_backend_refreshing__refresh_is_invoked(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_i(0, git_odb_refresh(odb));
cl_assert_equal_i(1, _fake->refresh_calls);
}
void test_odb_backend_refreshing__refresh_suppressed_with_no_refresh(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(false, git_odb_exists_ext(odb, &_nonexisting_oid, GIT_ODB_LOOKUP_NO_REFRESH));
cl_assert_equal_i(0, _fake->refresh_calls);
}
| libgit2-main | tests/libgit2/odb/backend/refreshing.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "odb.h"
#include "backend_helpers.h"
#include "git2/sys/mempack.h"
static git_odb *_odb;
static git_oid _oid;
static git_odb_object *_obj;
static git_repository *_repo;
void test_odb_backend_mempack__initialize(void)
{
git_odb_backend *backend;
cl_git_pass(git_mempack_new(&backend));
cl_git_pass(git_odb__new(&_odb, NULL));
cl_git_pass(git_odb_add_backend(_odb, backend, 10));
cl_git_pass(git_repository_wrap_odb(&_repo, _odb));
}
void test_odb_backend_mempack__cleanup(void)
{
git_odb_object_free(_obj);
git_odb_free(_odb);
git_repository_free(_repo);
}
void test_odb_backend_mempack__write_succeeds(void)
{
const char *data = "data";
cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJECT_BLOB));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_mempack__read_of_missing_object_fails(void)
{
cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));
cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_mempack__exists_of_missing_object_fails(void)
{
cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));
cl_assert(git_odb_exists(_odb, &_oid) == 0);
}
void test_odb_backend_mempack__exists_with_existing_objects_succeeds(void)
{
const char *data = "data";
cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJECT_BLOB));
cl_assert(git_odb_exists(_odb, &_oid) == 1);
}
void test_odb_backend_mempack__blob_create_from_buffer_succeeds(void)
{
const char *data = "data";
cl_git_pass(git_blob_create_from_buffer(&_oid, _repo, data, strlen(data) + 1));
cl_assert(git_odb_exists(_odb, &_oid) == 1);
}
| libgit2-main | tests/libgit2/odb/backend/mempack.c |
#include "clar_libgit2.h"
#include "git2/sys/odb_backend.h"
#include "backend_helpers.h"
static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
{
const fake_object *obj = fake->objects, *found = NULL;
while (obj && obj->oid) {
git_oid current_oid;
git_oid__fromstr(¤t_oid, obj->oid, GIT_OID_SHA1);
if (git_oid_ncmp(¤t_oid, oid, len) == 0) {
if (found)
return GIT_EAMBIGUOUS;
found = obj;
}
obj++;
}
if (found && out)
*out = found;
return found ? GIT_OK : GIT_ENOTFOUND;
}
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
{
fake_backend *fake;
fake = (fake_backend *)backend;
fake->exists_calls++;
return search_object(NULL, fake, oid, GIT_OID_SHA1_HEXSIZE) == GIT_OK;
}
static int fake_backend__exists_prefix(
git_oid *out, git_odb_backend *backend, const git_oid *oid, size_t len)
{
const fake_object *obj;
fake_backend *fake;
int error;
fake = (fake_backend *)backend;
fake->exists_prefix_calls++;
if ((error = search_object(&obj, fake, oid, len)) < 0)
return error;
if (out)
git_oid__fromstr(out, obj->oid, GIT_OID_SHA1);
return 0;
}
static int fake_backend__read(
void **buffer_p, size_t *len_p, git_object_t *type_p,
git_odb_backend *backend, const git_oid *oid)
{
const fake_object *obj;
fake_backend *fake;
int error;
fake = (fake_backend *)backend;
fake->read_calls++;
if ((error = search_object(&obj, fake, oid, GIT_OID_SHA1_HEXSIZE)) < 0)
return error;
*len_p = strlen(obj->content);
*buffer_p = git__strdup(obj->content);
*type_p = GIT_OBJECT_BLOB;
return 0;
}
static int fake_backend__read_header(
size_t *len_p, git_object_t *type_p,
git_odb_backend *backend, const git_oid *oid)
{
const fake_object *obj;
fake_backend *fake;
int error;
fake = (fake_backend *)backend;
fake->read_header_calls++;
if ((error = search_object(&obj, fake, oid, GIT_OID_SHA1_HEXSIZE)) < 0)
return error;
*len_p = strlen(obj->content);
*type_p = GIT_OBJECT_BLOB;
return 0;
}
static int fake_backend__read_prefix(
git_oid *out_oid, void **buffer_p, size_t *len_p, git_object_t *type_p,
git_odb_backend *backend, const git_oid *short_oid, size_t len)
{
const fake_object *obj;
fake_backend *fake;
int error;
fake = (fake_backend *)backend;
fake->read_prefix_calls++;
if ((error = search_object(&obj, fake, short_oid, len)) < 0)
return error;
git_oid__fromstr(out_oid, obj->oid, GIT_OID_SHA1);
*len_p = strlen(obj->content);
*buffer_p = git__strdup(obj->content);
*type_p = GIT_OBJECT_BLOB;
return 0;
}
static int fake_backend__refresh(git_odb_backend *backend)
{
fake_backend *fake;
fake = (fake_backend *)backend;
fake->refresh_calls++;
return 0;
}
static void fake_backend__free(git_odb_backend *_backend)
{
fake_backend *backend;
backend = (fake_backend *)_backend;
git__free(backend);
}
int build_fake_backend(
git_odb_backend **out,
const fake_object *objects,
bool support_refresh)
{
fake_backend *backend;
backend = git__calloc(1, sizeof(fake_backend));
GIT_ERROR_CHECK_ALLOC(backend);
backend->parent.version = GIT_ODB_BACKEND_VERSION;
backend->objects = objects;
backend->parent.read = fake_backend__read;
backend->parent.read_prefix = fake_backend__read_prefix;
backend->parent.read_header = fake_backend__read_header;
backend->parent.refresh = support_refresh ? fake_backend__refresh : NULL;
backend->parent.exists = fake_backend__exists;
backend->parent.exists_prefix = fake_backend__exists_prefix;
backend->parent.free = &fake_backend__free;
*out = (git_odb_backend *)backend;
return 0;
}
| libgit2-main | tests/libgit2/odb/backend/backend_helpers.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"
#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
static git_repository *_repo;
static git_odb *_odb;
static git_odb_object *_obj;
static git_oid _oid;
static void setup_backend(const fake_object *objs)
{
git_odb_backend *backend;
cl_git_pass(build_fake_backend(&backend, objs, false));
cl_git_pass(git_repository_odb__weakptr(&_odb, _repo));
cl_git_pass(git_odb_add_backend(_odb, backend, 10));
}
static void assert_object_contains(git_odb_object *obj, const char *expected)
{
const char *actual = (const char *) git_odb_object_data(obj);
cl_assert_equal_s(actual, expected);
}
void test_odb_backend_simple__initialize(void)
{
_repo = cl_git_sandbox_init("testrepo.git");
_odb = NULL;
_obj = NULL;
}
void test_odb_backend_simple__cleanup(void)
{
git_odb_object_free(_obj);
cl_git_sandbox_cleanup();
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
}
void test_odb_backend_simple__read_of_object_succeeds(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));
cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
{
const fake_object objs[] = {
{ "1234567890123456789012345678901234567890", "nonmatching content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
{
const fake_object objs[] = {
{ "1234567890123456789012345678901234567890", "nonmatching content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__read_prefix_succeeds(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632", GIT_OID_SHA1));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
char *hash = "f6ea0495187600e8";
setup_backend(objs);
cl_git_pass(git_oid__fromstrn(&_oid, hash, strlen(hash), GIT_OID_SHA1));
cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890222222222222222222222222222222", "second content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 7));
}
void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890111111111111111111111111111112", "second content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 39));
cl_git_pass(git_odb_read_prefix(&_obj, _odb, &_oid, 40));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__exists_succeeds(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_assert(git_odb_exists(_odb, &_oid));
}
void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));
cl_assert(git_odb_exists(_odb, &_oid) == 0);
}
void test_odb_backend_simple__exists_prefix_succeeds(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890222222222222222222222222222222", "second content" },
{ NULL, NULL }
};
git_oid found;
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 12));
cl_assert(git_oid_equal(&found, &_oid));
}
void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890222222222222222222222222222222", "second content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(NULL, _odb, &_oid, 7));
}
void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890111111111111111111111111111112", "second content" },
{ NULL, NULL }
};
git_oid found;
setup_backend(objs);
cl_git_pass(git_oid__fromstr(&_oid, objs[0].oid, GIT_OID_SHA1));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &_oid, 39));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 40));
cl_assert(git_oid_equal(&found, &_oid));
}
void test_odb_backend_simple__null_oid_is_ignored(void)
{
const fake_object objs[] = {
{ "0000000000000000000000000000000000000000", "null oid content" },
{ NULL, NULL }
};
git_oid null_oid = GIT_OID_SHA1_ZERO;
git_odb_object *obj;
setup_backend(objs);
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_assert(!git_odb_exists(_odb, &null_oid));
cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&obj, _odb, &null_oid));
cl_assert(git_error_last() && strstr(git_error_last()->message, "null OID"));
}
| libgit2-main | tests/libgit2/odb/backend/simple.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"
#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
static git_repository *_repo;
static git_odb_object *_obj;
static fake_backend *_fake_empty;
static fake_backend *_fake_filled;
static git_oid _existing_oid;
static const fake_object _objects_filled[] = {
{ EXISTING_HASH, "" },
{ NULL, NULL }
};
static const fake_object _objects_empty[] = {
{ NULL, NULL }
};
void test_odb_backend_multiple__initialize(void)
{
git_odb_backend *backend;
git_oid__fromstr(&_existing_oid, EXISTING_HASH, GIT_OID_SHA1);
_obj = NULL;
_repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(build_fake_backend(&backend, _objects_filled, false));
_fake_filled = (fake_backend *)backend;
cl_git_pass(build_fake_backend(&backend, _objects_empty, false));
_fake_empty = (fake_backend *)backend;
}
void test_odb_backend_multiple__cleanup(void)
{
git_odb_object_free(_obj);
cl_git_sandbox_cleanup();
}
void test_odb_backend_multiple__read_with_empty_first_succeeds(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 10));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 50));
cl_git_pass(git_odb_read(&_obj, odb, &_existing_oid));
cl_assert_equal_i(1, _fake_filled->read_calls);
cl_assert_equal_i(1, _fake_empty->read_calls);
}
void test_odb_backend_multiple__read_with_first_matching_stops(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 10));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
cl_git_pass(git_odb_read(&_obj, odb, &_existing_oid));
cl_assert_equal_i(1, _fake_filled->read_calls);
cl_assert_equal_i(0, _fake_empty->read_calls);
}
void test_odb_backend_multiple__read_prefix_with_first_empty_succeeds(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 10));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 50));
cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
}
void test_odb_backend_multiple__read_prefix_with_first_matching_reads_both(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, -10));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
}
void test_odb_backend_multiple__read_prefix_with_first_matching_succeeds_without_hash_verification(void)
{
git_odb *odb;
git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0);
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, -10));
cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
/*
* Both backends should be checked as we have to check
* for collisions
*/
cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1);
}
| libgit2-main | tests/libgit2/odb/backend/multiple.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"
static git_repository *_repo;
static fake_backend *_fake;
#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
static const fake_object _objects[] = {
{ EXISTING_HASH, "" },
{ NULL, NULL }
};
static git_oid _nonexisting_oid;
static git_oid _existing_oid;
static void setup_repository_and_backend(void)
{
git_odb *odb = NULL;
git_odb_backend *backend = NULL;
_repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(build_fake_backend(&backend, _objects, false));
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, backend, 10));
_fake = (fake_backend *)backend;
}
void test_odb_backend_nonrefreshing__initialize(void)
{
git_oid__fromstr(&_nonexisting_oid, NONEXISTING_HASH, GIT_OID_SHA1);
git_oid__fromstr(&_existing_oid, EXISTING_HASH, GIT_OID_SHA1);
setup_repository_and_backend();
}
void test_odb_backend_nonrefreshing__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid));
cl_assert_equal_i(1, _fake->exists_calls);
}
void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void)
{
git_object *obj;
cl_git_fail_with(
git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJECT_ANY),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_calls);
}
void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void)
{
git_object *obj;
cl_git_fail_with(
git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJECT_ANY),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_prefix_calls);
}
void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void)
{
git_odb *odb;
size_t len;
git_object_t type;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_fail_with(
git_odb_read_header(&len, &type, odb, &_nonexisting_oid),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_header_calls);
}
void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void)
{
git_odb *odb;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid));
cl_assert_equal_i(1, _fake->exists_calls);
}
void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void)
{
git_object *obj;
cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJECT_ANY));
cl_assert_equal_i(1, _fake->read_calls);
git_object_free(obj);
}
void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void)
{
git_object *obj;
cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJECT_ANY));
cl_assert_equal_i(1, _fake->read_prefix_calls);
git_object_free(obj);
}
void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void)
{
git_odb *odb;
size_t len;
git_object_t type;
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid));
cl_assert_equal_i(1, _fake->read_header_calls);
}
void test_odb_backend_nonrefreshing__read_is_invoked_once_when_revparsing_a_full_oid(void)
{
git_object *obj;
cl_git_fail_with(
git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_calls);
}
| libgit2-main | tests/libgit2/odb/backend/nonrefreshing.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "odb.h"
#include "git2/sys/repository.h"
static git_repository *_repo;
void test_odb_backend_nobackend__initialize(void)
{
git_config *config;
git_odb *odb;
git_refdb *refdb;
cl_git_pass(git_repository_new(&_repo));
cl_git_pass(git_config_new(&config));
cl_git_pass(git_odb__new(&odb, NULL));
cl_git_pass(git_refdb_new(&refdb, _repo));
git_repository_set_config(_repo, config);
git_repository_set_odb(_repo, odb);
git_repository_set_refdb(_repo, refdb);
/* The set increases the refcount and we don't want them anymore */
git_config_free(config);
git_odb_free(odb);
git_refdb_free(refdb);
}
void test_odb_backend_nobackend__cleanup(void)
{
git_repository_free(_repo);
}
void test_odb_backend_nobackend__write_fails_gracefully(void)
{
git_oid id;
git_odb *odb;
const git_error *err;
git_repository_odb(&odb, _repo);
cl_git_fail(git_odb_write(&id, odb, "Hello world!\n", 13, GIT_OBJECT_BLOB));
err = git_error_last();
cl_assert_equal_s(err->message, "cannot write object - unsupported in the loaded odb backends");
git_odb_free(odb);
}
| libgit2-main | tests/libgit2/odb/backend/nobackend.c |
#include "clar_libgit2.h"
#include "notes.h"
static git_repository *_repo;
static git_note *_note;
static git_signature *_sig;
static git_config *_cfg;
void test_notes_notesref__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&_repo, "testrepo.git"));
}
void test_notes_notesref__cleanup(void)
{
git_note_free(_note);
_note = NULL;
git_signature_free(_sig);
_sig = NULL;
git_config_free(_cfg);
_cfg = NULL;
git_repository_free(_repo);
_repo = NULL;
cl_fixture_cleanup("testrepo.git");
}
void test_notes_notesref__config_corenotesref(void)
{
git_oid oid, note_oid;
git_buf default_ref = GIT_BUF_INIT;
cl_git_pass(git_signature_now(&_sig, "alice", "[email protected]"));
cl_git_pass(git_oid__fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1));
cl_git_pass(git_repository_config(&_cfg, _repo));
cl_git_pass(git_config_set_string(_cfg, "core.notesRef", "refs/notes/mydefaultnotesref"));
cl_git_pass(git_note_create(¬e_oid, _repo, NULL, _sig, _sig, &oid, "test123test\n", 0));
cl_git_pass(git_note_read(&_note, _repo, NULL, &oid));
cl_assert_equal_s("test123test\n", git_note_message(_note));
cl_assert_equal_oid(git_note_id(_note), ¬e_oid);
git_note_free(_note);
cl_git_pass(git_note_read(&_note, _repo, "refs/notes/mydefaultnotesref", &oid));
cl_assert_equal_s("test123test\n", git_note_message(_note));
cl_assert_equal_oid(git_note_id(_note), ¬e_oid);
cl_git_pass(git_note_default_ref(&default_ref, _repo));
cl_assert_equal_s("refs/notes/mydefaultnotesref", default_ref.ptr);
git_buf_dispose(&default_ref);
cl_git_pass(git_config_delete_entry(_cfg, "core.notesRef"));
cl_git_pass(git_note_default_ref(&default_ref, _repo));
cl_assert_equal_s(GIT_NOTES_DEFAULT_REF, default_ref.ptr);
git_buf_dispose(&default_ref);
}
| libgit2-main | tests/libgit2/notes/notesref.c |
#include "clar_libgit2.h"
static git_repository *_repo;
static git_signature *_sig;
void test_notes_notes__initialize(void)
{
_repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_signature_now(&_sig, "alice", "[email protected]"));
}
void test_notes_notes__cleanup(void)
{
git_signature_free(_sig);
_sig = NULL;
cl_git_sandbox_cleanup();
}
static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) {
git_blob *blob;
cl_assert_equal_s(git_note_message(note), message);
cl_assert_equal_oid(git_note_id(note), note_oid);
cl_git_pass(git_blob_lookup(&blob, _repo, note_oid));
cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob));
git_blob_free(blob);
}
static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message)
{
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, target_sha, GIT_OID_SHA1));
cl_git_pass(git_note_create(note_oid, _repo, canonical_namespace, _sig, _sig, &oid, message, 0));
}
static struct {
const char *note_sha;
const char *annotated_object_sha;
}
list_expectations[] = {
{ "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
{ "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "9fd738e8f7967c078dceed8190330fc8648ee56a" },
{ "257b43746b6b46caa4aa788376c647cce0a33e2b", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750" },
{ "1ec1c8e03f461f4f5d3f3702172483662e7223f3", "c47800c7266a2be04c571c04d5a6614691ea99bd" },
{ NULL, NULL }
};
#define EXPECTATIONS_COUNT (sizeof(list_expectations)/sizeof(list_expectations[0])) - 1
static int note_list_cb(
const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload)
{
git_oid expected_note_oid, expected_target_oid;
unsigned int *count = (unsigned int *)payload;
cl_assert(*count < EXPECTATIONS_COUNT);
cl_git_pass(git_oid__fromstr(&expected_note_oid, list_expectations[*count].note_sha, GIT_OID_SHA1));
cl_assert_equal_oid(&expected_note_oid, blob_id);
cl_git_pass(git_oid__fromstr(&expected_target_oid, list_expectations[*count].annotated_object_sha, GIT_OID_SHA1));
cl_assert_equal_oid(&expected_target_oid, annotated_obj_id);
(*count)++;
return 0;
}
struct note_create_payload {
const char *note_oid;
const char *object_oid;
unsigned seen;
};
static int note_list_create_cb(
const git_oid *blob_oid, const git_oid *annotated_obj_id, void *payload)
{
git_oid expected_note_oid, expected_target_oid;
struct note_create_payload *notes = payload;
size_t i;
for (i = 0; notes[i].note_oid != NULL; i++) {
cl_git_pass(git_oid__fromstr(&expected_note_oid, notes[i].note_oid, GIT_OID_SHA1));
if (git_oid_cmp(&expected_note_oid, blob_oid) != 0)
continue;
cl_git_pass(git_oid__fromstr(&expected_target_oid, notes[i].object_oid, GIT_OID_SHA1));
if (git_oid_cmp(&expected_target_oid, annotated_obj_id) != 0)
continue;
notes[i].seen = 1;
return 0;
}
cl_fail("Did not see expected note");
return 0;
}
static void assert_notes_seen(struct note_create_payload payload[], size_t n)
{
size_t seen = 0, i;
for (i = 0; payload[i].note_oid != NULL; i++) {
if (payload[i].seen)
seen++;
}
cl_assert_equal_i(seen, n);
}
void test_notes_notes__can_create_a_note(void)
{
git_oid note_oid;
static struct note_create_payload can_create_a_note[] = {
{ "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
{ NULL, NULL, 0 }
};
create_note(¬e_oid, "refs/notes/i-can-see-dead-notes", can_create_a_note[0].object_oid, "I decorate 4a20\n");
cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note));
assert_notes_seen(can_create_a_note, 1);
}
void test_notes_notes__can_create_a_note_from_commit(void)
{
git_oid oid;
git_oid notes_commit_out;
git_reference *ref;
static struct note_create_payload can_create_a_note_from_commit[] = {
{ "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
{ NULL, NULL, 0 }
};
cl_git_pass(git_oid__fromstr(&oid, can_create_a_note_from_commit[0].object_oid, GIT_OID_SHA1));
cl_git_pass(git_note_commit_create(¬es_commit_out, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
/* create_from_commit will not update any ref,
* so we must manually create the ref, that points to the commit */
cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", ¬es_commit_out, 0, NULL));
cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note_from_commit));
assert_notes_seen(can_create_a_note_from_commit, 1);
git_reference_free(ref);
}
/* Test that we can create a note from a commit, given an existing commit */
void test_notes_notes__can_create_a_note_from_commit_given_an_existing_commit(void)
{
git_oid oid;
git_oid notes_commit_out;
git_commit *existing_notes_commit = NULL;
git_reference *ref;
static struct note_create_payload can_create_a_note_from_commit_given_an_existing_commit[] = {
{ "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
{ "1aaf94147c21f981e0a20bf57b89137c5a6aae52", "9fd738e8f7967c078dceed8190330fc8648ee56a", 0 },
{ NULL, NULL, 0 }
};
cl_git_pass(git_oid__fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", GIT_OID_SHA1));
cl_git_pass(git_note_commit_create(¬es_commit_out, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
cl_git_pass(git_oid__fromstr(&oid, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1));
git_commit_lookup(&existing_notes_commit, _repo, ¬es_commit_out);
cl_assert(existing_notes_commit);
cl_git_pass(git_note_commit_create(¬es_commit_out, NULL, _repo, existing_notes_commit, _sig, _sig, &oid, "I decorate 9fd7\n", 0));
/* create_from_commit will not update any ref,
* so we must manually create the ref, that points to the commit */
cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", ¬es_commit_out, 0, NULL));
cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note_from_commit_given_an_existing_commit));
assert_notes_seen(can_create_a_note_from_commit_given_an_existing_commit, 2);
git_commit_free(existing_notes_commit);
git_reference_free(ref);
}
/*
* $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd
* $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 9fd738e8f7967c078dceed8190330fc8648ee56a
* $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*
* $ git notes --ref i-can-see-dead-notes list
* 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
* 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
* 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
*
* $ git ls-tree refs/notes/i-can-see-dead-notes
* 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
* 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
* 100644 blob 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* 100644 blob 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
*/
void test_notes_notes__can_retrieve_a_list_of_notes_for_a_given_namespace(void)
{
git_oid note_oid1, note_oid2, note_oid3, note_oid4;
unsigned int retrieved_notes = 0;
create_note(¬e_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
create_note(¬e_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
create_note(¬e_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
create_note(¬e_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
cl_git_pass(git_note_foreach
(_repo, "refs/notes/i-can-see-dead-notes", note_list_cb, &retrieved_notes));
cl_assert_equal_i(4, retrieved_notes);
}
static int note_cancel_cb(
const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload)
{
unsigned int *count = (unsigned int *)payload;
GIT_UNUSED(blob_id);
GIT_UNUSED(annotated_obj_id);
(*count)++;
return (*count > 2);
}
void test_notes_notes__can_cancel_foreach(void)
{
git_oid note_oid1, note_oid2, note_oid3, note_oid4;
unsigned int retrieved_notes = 0;
create_note(¬e_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
create_note(¬e_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
create_note(¬e_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
create_note(¬e_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
cl_assert_equal_i(
1,
git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes",
note_cancel_cb, &retrieved_notes));
}
void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_returns_ENOTFOUND(void)
{
int error;
unsigned int retrieved_notes = 0;
error = git_note_foreach(_repo, "refs/notes/i-am-not", note_list_cb, &retrieved_notes);
cl_git_fail(error);
cl_assert_equal_i(GIT_ENOTFOUND, error);
cl_assert_equal_i(0, retrieved_notes);
}
void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void)
{
git_oid note_oid, target_oid;
git_note *note, *default_namespace_note;
git_buf default_ref = GIT_BUF_INIT;
cl_git_pass(git_oid__fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125", GIT_OID_SHA1));
cl_git_pass(git_note_default_ref(&default_ref, _repo));
create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
cl_git_pass(git_note_read(¬e, _repo, NULL, &target_oid));
cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref.ptr, &target_oid));
assert_note_equal(note, "hello world\n", ¬e_oid);
assert_note_equal(default_namespace_note, "hello world\n", ¬e_oid);
git_buf_dispose(&default_ref);
git_note_free(note);
git_note_free(default_namespace_note);
}
void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void)
{
git_oid note_oid, target_oid;
git_note *note;
cl_git_pass(git_oid__fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125", GIT_OID_SHA1));
create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n");
cl_git_pass(git_note_read(¬e, _repo, "refs/notes/some/namespace", &target_oid));
assert_note_equal(note, "hello world on a custom namespace\n", ¬e_oid);
git_note_free(note);
}
/*
* $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
* 08b041783f40edfe12bb406c9c9a8a040177c125
*/
void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void)
{
int error;
git_oid note_oid, target_oid;
cl_git_pass(git_oid__fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125", GIT_OID_SHA1));
create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
error = git_note_create(¬e_oid, _repo, NULL, _sig, _sig, &target_oid, "hello world\n", 0);
cl_git_fail(error);
cl_assert_equal_i(GIT_EEXISTS, error);
create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
error = git_note_create(¬e_oid, _repo, "refs/notes/some/namespace", _sig, _sig, &target_oid, "hello world\n", 0);
cl_git_fail(error);
cl_assert_equal_i(GIT_EEXISTS, error);
}
void test_notes_notes__creating_a_note_on_a_target_can_overwrite_existing_note(void)
{
git_oid note_oid, target_oid;
git_note *note, *namespace_note;
cl_git_pass(git_oid__fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125", GIT_OID_SHA1));
create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n");
cl_git_pass(git_note_create(¬e_oid, _repo, NULL, _sig, _sig, &target_oid, "hello new world\n", 1));
cl_git_pass(git_note_read(¬e, _repo, NULL, &target_oid));
assert_note_equal(note, "hello new world\n", ¬e_oid);
create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n");
cl_git_pass(git_note_create(¬e_oid, _repo, "refs/notes/some/namespace", _sig, _sig, &target_oid, "hello new ref world\n", 1));
cl_git_pass(git_note_read(&namespace_note, _repo, "refs/notes/some/namespace", &target_oid));
assert_note_equal(namespace_note, "hello new ref world\n", ¬e_oid);
git_note_free(note);
git_note_free(namespace_note);
}
static char *messages[] = {
"08c041783f40edfe12bb406c9c9a8a040177c125",
"96c45fbe09ab7445fc7c60fd8d17f32494399343",
"48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1",
"24c3eaafb681c3df668f9df96f58e7b8c756eb04",
"96ca1b6ccc7858ae94684777f85ac0e7447f7040",
"7ac2db4378a08bb244a427c357e0082ee0d57ac6",
"e6cba23dbf4ef84fe35e884f017f4e24dc228572",
"c8cf3462c7d8feba716deeb2ebe6583bd54589e2",
"39c16b9834c2d665ac5f68ad91dc5b933bad8549",
"f3c582b1397df6a664224ebbaf9d4cc952706597",
"29cec67037fe8e89977474988219016ae7f342a6",
"36c4cd238bf8e82e27b740e0741b025f2e8c79ab",
"f1c45a47c02e01d5a9a326f1d9f7f756373387f8",
"4aca84406f5daee34ab513a60717c8d7b1763ead",
"84ce167da452552f63ed8407b55d5ece4901845f",
NULL
};
#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1
/* Test that we can read a note */
void test_notes_notes__can_read_a_note(void)
{
git_oid note_oid, target_oid;
git_note *note;
create_note(¬e_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
cl_git_pass(git_oid__fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", GIT_OID_SHA1));
cl_git_pass(git_note_read(¬e, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
git_note_free(note);
}
/* Test that we can read a note with from commit api */
void test_notes_notes__can_read_a_note_from_a_commit(void)
{
git_oid oid, notes_commit_oid;
git_commit *notes_commit;
git_note *note;
cl_git_pass(git_oid__fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", GIT_OID_SHA1));
cl_git_pass(git_note_commit_create(¬es_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
cl_git_pass(git_commit_lookup(¬es_commit, _repo, ¬es_commit_oid));
cl_assert(notes_commit);
cl_git_pass(git_note_commit_read(¬e, _repo, notes_commit, &oid));
cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
git_commit_free(notes_commit);
git_note_free(note);
}
/* Test that we can read a commit with no note fails */
void test_notes_notes__attempt_to_read_a_note_from_a_commit_with_no_note_fails(void)
{
git_oid oid, notes_commit_oid;
git_commit *notes_commit;
git_note *note;
cl_git_pass(git_oid__fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", GIT_OID_SHA1));
cl_git_pass(git_note_commit_create(¬es_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
git_commit_lookup(¬es_commit, _repo, ¬es_commit_oid);
cl_git_pass(git_note_commit_remove(¬es_commit_oid, _repo, notes_commit, _sig, _sig, &oid));
git_commit_free(notes_commit);
git_commit_lookup(¬es_commit, _repo, ¬es_commit_oid);
cl_assert(notes_commit);
cl_git_fail_with(GIT_ENOTFOUND, git_note_commit_read(¬e, _repo, notes_commit, &oid));
git_commit_free(notes_commit);
}
/*
* $ git ls-tree refs/notes/fanout
* 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84
*
* $ git ls-tree 4b22b35
* 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759 96
*
* $ git ls-tree d71aab4
* 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125 071c1b46c854b31185ea97743be6a8774479
*/
void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void)
{
size_t i;
git_oid note_oid, target_oid;
git_note *_note;
cl_git_pass(git_oid__fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125", GIT_OID_SHA1));
for (i = 0; i < MESSAGES_COUNT; i++) {
cl_git_pass(git_note_create(¬e_oid, _repo, "refs/notes/fanout", _sig, _sig, &target_oid, messages[i], 0));
cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid));
git_note_free(_note);
git_oid_cpy(&target_oid, ¬e_oid);
}
}
/*
* $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
* 08b041783f40edfe12bb406c9c9a8a040177c125
*/
void test_notes_notes__can_read_a_note_in_an_existing_fanout(void)
{
git_oid note_oid, target_oid;
git_note *note;
cl_git_pass(git_oid__fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1));
cl_git_pass(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid));
cl_git_pass(git_oid__fromstr(¬e_oid, "08b041783f40edfe12bb406c9c9a8a040177c125", GIT_OID_SHA1));
cl_assert_equal_oid(git_note_id(note), ¬e_oid);
git_note_free(note);
}
/* Can remove a note */
void test_notes_notes__can_remove_a_note(void)
{
git_oid note_oid, target_oid;
git_note *note;
create_note(¬e_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
cl_git_pass(git_oid__fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", GIT_OID_SHA1));
cl_git_pass(git_note_remove(_repo, "refs/notes/i-can-see-dead-notes", _sig, _sig, &target_oid));
cl_git_fail(git_note_read(¬e, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
}
/* Can remove a note from a commit */
void test_notes_notes__can_remove_a_note_from_commit(void)
{
git_oid oid, notes_commit_oid;
git_note *note = NULL;
git_commit *existing_notes_commit;
git_reference *ref;
cl_git_pass(git_oid__fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", GIT_OID_SHA1));
cl_git_pass(git_note_commit_create(¬es_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
cl_git_pass(git_commit_lookup(&existing_notes_commit, _repo, ¬es_commit_oid));
cl_assert(existing_notes_commit);
cl_git_pass(git_note_commit_remove(¬es_commit_oid, _repo, existing_notes_commit, _sig, _sig, &oid));
/* remove_from_commit will not update any ref,
* so we must manually create the ref, that points to the commit */
cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", ¬es_commit_oid, 0, NULL));
cl_git_fail(git_note_read(¬e, _repo, "refs/notes/i-can-see-dead-notes", &oid));
git_commit_free(existing_notes_commit);
git_reference_free(ref);
git_note_free(note);
}
void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void)
{
git_oid target_oid;
git_note *note;
cl_git_pass(git_oid__fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1));
cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
cl_git_fail(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid));
}
void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void)
{
int error;
git_oid target_oid;
cl_git_pass(git_oid__fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479", GIT_OID_SHA1));
cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid);
cl_git_fail(error);
cl_assert_equal_i(GIT_ENOTFOUND, error);
}
void test_notes_notes__can_iterate_default_namespace(void)
{
git_note_iterator *iter;
git_note *note;
git_oid note_id, annotated_id;
git_oid note_created[2];
const char* note_message[] = {
"I decorate a65f\n",
"I decorate c478\n"
};
int i, err;
create_note(¬e_created[0], "refs/notes/commits",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
create_note(¬e_created[1], "refs/notes/commits",
"c47800c7266a2be04c571c04d5a6614691ea99bd", note_message[1]);
cl_git_pass(git_note_iterator_new(&iter, _repo, NULL));
for (i = 0; (err = git_note_next(¬e_id, &annotated_id, iter)) >= 0; ++i) {
cl_git_pass(git_note_read(¬e, _repo, NULL, &annotated_id));
cl_assert_equal_s(git_note_message(note), note_message[i]);
git_note_free(note);
}
cl_assert_equal_i(GIT_ITEROVER, err);
cl_assert_equal_i(2, i);
git_note_iterator_free(iter);
}
void test_notes_notes__can_iterate_custom_namespace(void)
{
git_note_iterator *iter;
git_note *note;
git_oid note_id, annotated_id;
git_oid note_created[2];
const char* note_message[] = {
"I decorate a65f\n",
"I decorate c478\n"
};
int i, err;
create_note(¬e_created[0], "refs/notes/beer",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
create_note(¬e_created[1], "refs/notes/beer",
"c47800c7266a2be04c571c04d5a6614691ea99bd", note_message[1]);
cl_git_pass(git_note_iterator_new(&iter, _repo, "refs/notes/beer"));
for (i = 0; (err = git_note_next(¬e_id, &annotated_id, iter)) >= 0; ++i) {
cl_git_pass(git_note_read(¬e, _repo, "refs/notes/beer", &annotated_id));
cl_assert_equal_s(git_note_message(note), note_message[i]);
git_note_free(note);
}
cl_assert_equal_i(GIT_ITEROVER, err);
cl_assert_equal_i(2, i);
git_note_iterator_free(iter);
}
void test_notes_notes__empty_iterate(void)
{
git_note_iterator *iter;
cl_git_fail(git_note_iterator_new(&iter, _repo, "refs/notes/commits"));
}
void test_notes_notes__iterate_from_commit(void)
{
git_note_iterator *iter;
git_note *note;
git_oid note_id, annotated_id;
git_oid oids[2];
git_oid notes_commit_oids[2];
git_commit *notes_commits[2];
const char* note_message[] = {
"I decorate a65f\n",
"I decorate c478\n"
};
int i, err;
cl_git_pass(git_oid__fromstr(&(oids[0]), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&(oids[1]), "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1));
cl_git_pass(git_note_commit_create(¬es_commit_oids[0], NULL, _repo, NULL, _sig, _sig, &(oids[0]), note_message[0], 0));
git_commit_lookup(¬es_commits[0], _repo, ¬es_commit_oids[0]);
cl_assert(notes_commits[0]);
cl_git_pass(git_note_commit_create(¬es_commit_oids[1], NULL, _repo, notes_commits[0], _sig, _sig, &(oids[1]), note_message[1], 0));
git_commit_lookup(¬es_commits[1], _repo, ¬es_commit_oids[1]);
cl_assert(notes_commits[1]);
cl_git_pass(git_note_commit_iterator_new(&iter, notes_commits[1]));
for (i = 0; (err = git_note_next(¬e_id, &annotated_id, iter)) >= 0; ++i) {
cl_git_pass(git_note_commit_read(¬e, _repo, notes_commits[1], &annotated_id));
cl_assert_equal_s(git_note_message(note), note_message[i]);
git_note_free(note);
}
cl_assert_equal_i(GIT_ITEROVER, err);
cl_assert_equal_i(2, i);
git_note_iterator_free(iter);
git_commit_free(notes_commits[0]);
git_commit_free(notes_commits[1]);
}
| libgit2-main | tests/libgit2/notes/notes.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "git2/attr.h"
#include "attr.h"
#include "sysdir.h"
#include "attr_expect.h"
#include "git2/sys/repository.h"
static git_repository *g_repo = NULL;
void test_attr_repo__initialize(void)
{
g_repo = cl_git_sandbox_init("attr");
}
void test_attr_repo__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
cl_sandbox_set_search_path_defaults();
}
static struct attr_expected get_one_test_cases[] = {
{ "root_test1", "repoattr", EXPECT_TRUE, NULL },
{ "root_test1", "rootattr", EXPECT_TRUE, NULL },
{ "root_test1", "missingattr", EXPECT_UNDEFINED, NULL },
{ "root_test1", "subattr", EXPECT_UNDEFINED, NULL },
{ "root_test1", "negattr", EXPECT_UNDEFINED, NULL },
{ "root_test2", "repoattr", EXPECT_TRUE, NULL },
{ "root_test2", "rootattr", EXPECT_FALSE, NULL },
{ "root_test2", "missingattr", EXPECT_UNDEFINED, NULL },
{ "root_test2", "multiattr", EXPECT_FALSE, NULL },
{ "root_test3", "repoattr", EXPECT_TRUE, NULL },
{ "root_test3", "rootattr", EXPECT_UNDEFINED, NULL },
{ "root_test3", "multiattr", EXPECT_STRING, "3" },
{ "root_test3", "multi2", EXPECT_UNDEFINED, NULL },
{ "sub/subdir_test1", "repoattr", EXPECT_TRUE, NULL },
{ "sub/subdir_test1", "rootattr", EXPECT_TRUE, NULL },
{ "sub/subdir_test1", "missingattr", EXPECT_UNDEFINED, NULL },
{ "sub/subdir_test1", "subattr", EXPECT_STRING, "yes" },
{ "sub/subdir_test1", "negattr", EXPECT_FALSE, NULL },
{ "sub/subdir_test1", "another", EXPECT_UNDEFINED, NULL },
{ "sub/subdir_test2.txt", "repoattr", EXPECT_TRUE, NULL },
{ "sub/subdir_test2.txt", "rootattr", EXPECT_TRUE, NULL },
{ "sub/subdir_test2.txt", "missingattr", EXPECT_UNDEFINED, NULL },
{ "sub/subdir_test2.txt", "subattr", EXPECT_STRING, "yes" },
{ "sub/subdir_test2.txt", "negattr", EXPECT_FALSE, NULL },
{ "sub/subdir_test2.txt", "another", EXPECT_STRING, "zero" },
{ "sub/subdir_test2.txt", "reposub", EXPECT_TRUE, NULL },
{ "sub/sub/subdir.txt", "another", EXPECT_STRING, "one" },
{ "sub/sub/subdir.txt", "reposubsub", EXPECT_TRUE, NULL },
{ "sub/sub/subdir.txt", "reposub", EXPECT_UNDEFINED, NULL },
{ "does-not-exist", "foo", EXPECT_STRING, "yes" },
{ "sub/deep/file", "deepdeep", EXPECT_TRUE, NULL },
{ "sub/sub/d/no", "test", EXPECT_STRING, "a/b/d/*" },
{ "sub/sub/d/yes", "test", EXPECT_UNDEFINED, NULL },
};
void test_attr_repo__get_one(void)
{
int i;
for (i = 0; i < (int)ARRAY_SIZE(get_one_test_cases); ++i) {
struct attr_expected *scan = &get_one_test_cases[i];
const char *value;
cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr));
attr_check_expected(
scan->expected, scan->expected_str, scan->attr, value);
}
cl_assert(git_attr_cache__is_cached(
g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/attributes"));
cl_assert(git_attr_cache__is_cached(
g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitattributes"));
cl_assert(git_attr_cache__is_cached(
g_repo, GIT_ATTR_FILE_SOURCE_FILE, "sub/.gitattributes"));
}
void test_attr_repo__get_one_start_deep(void)
{
int i;
for (i = (int)ARRAY_SIZE(get_one_test_cases) - 1; i >= 0; --i) {
struct attr_expected *scan = &get_one_test_cases[i];
const char *value;
cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr));
attr_check_expected(
scan->expected, scan->expected_str, scan->attr, value);
}
cl_assert(git_attr_cache__is_cached(
g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/attributes"));
cl_assert(git_attr_cache__is_cached(
g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitattributes"));
cl_assert(git_attr_cache__is_cached(
g_repo, GIT_ATTR_FILE_SOURCE_FILE, "sub/.gitattributes"));
}
void test_attr_repo__get_many(void)
{
const char *names[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
const char *values[4];
cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test1", 4, names));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test2", 4, names));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert(GIT_ATTR_IS_FALSE(values[1]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/subdir_test1", 4, names));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
cl_assert_equal_s("yes", values[3]);
}
void test_attr_repo__get_many_in_place(void)
{
const char *vals[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
/* it should be legal to look up values into the same array that has
* the attribute names, overwriting each name as the value is found.
*/
cl_git_pass(git_attr_get_many(vals, g_repo, 0, "sub/subdir_test1", 4, vals));
cl_assert(GIT_ATTR_IS_TRUE(vals[0]));
cl_assert(GIT_ATTR_IS_TRUE(vals[1]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(vals[2]));
cl_assert_equal_s("yes", vals[3]);
}
static int count_attrs(
const char *name,
const char *value,
void *payload)
{
GIT_UNUSED(name);
GIT_UNUSED(value);
*((int *)payload) += 1;
return 0;
}
#define CANCEL_VALUE 12345
static int cancel_iteration(
const char *name,
const char *value,
void *payload)
{
GIT_UNUSED(name);
GIT_UNUSED(value);
*((int *)payload) -= 1;
if (*((int *)payload) < 0)
return CANCEL_VALUE;
return 0;
}
void test_attr_repo__foreach(void)
{
int count;
count = 0;
cl_git_pass(git_attr_foreach(
g_repo, 0, "root_test1", &count_attrs, &count));
cl_assert(count == 2);
count = 0;
cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test1",
&count_attrs, &count));
cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */
count = 0;
cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test2.txt",
&count_attrs, &count));
cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */
count = 2;
cl_assert_equal_i(
CANCEL_VALUE, git_attr_foreach(
g_repo, 0, "sub/subdir_test1", &cancel_iteration, &count)
);
}
void test_attr_repo__manpage_example(void)
{
const char *value;
cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "foo"));
cl_assert(GIT_ATTR_IS_TRUE(value));
cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "bar"));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "baz"));
cl_assert(GIT_ATTR_IS_FALSE(value));
cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "merge"));
cl_assert_equal_s("filfre", value);
cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "frotz"));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
}
#define CONTENT "I'm going to be dynamically processed\r\n" \
"And my line endings...\r\n" \
"...are going to be\n" \
"normalized!\r\n"
#define GITATTR "* text=auto\n" \
"*.txt text\n" \
"*.data binary\n"
static void add_to_workdir(const char *filename, const char *content)
{
git_str buf = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&buf, "attr", filename));
cl_git_rewritefile(git_str_cstr(&buf), content);
git_str_dispose(&buf);
}
static void assert_proper_normalization(git_index *index, const char *filename, const char *expected_sha)
{
size_t index_pos;
const git_index_entry *entry;
add_to_workdir(filename, CONTENT);
cl_git_pass(git_index_add_bypath(index, filename));
cl_assert(!git_index_find(&index_pos, index, filename));
entry = git_index_get_byindex(index, index_pos);
cl_assert_equal_i(0, git_oid_streq(&entry->id, expected_sha));
}
void test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitattributes_directives(void)
{
git_index* index;
cl_git_pass(git_repository_index(&index, g_repo));
add_to_workdir(".gitattributes", GITATTR);
assert_proper_normalization(index, "text.txt", "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
assert_proper_normalization(index, "huh.dunno", "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
assert_proper_normalization(index, "binary.data", "66eeff1fcbacf589e6d70aa70edd3fce5be2b37c");
git_index_free(index);
}
void test_attr_repo__bare_repo_with_index(void)
{
const char *names[4] = { "test1", "test2", "test3", "test4" };
const char *values[4];
git_index *index;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_mkfile(
"attr/.gitattributes",
"*.txt test1 test2=foobar -test3\n"
"trial.txt -test1 test2=barfoo !test3 test4\n");
cl_git_pass(git_index_add_bypath(index, ".gitattributes"));
git_index_free(index);
cl_must_pass(p_unlink("attr/.gitattributes"));
cl_assert(!git_fs_path_exists("attr/.gitattributes"));
cl_git_pass(git_repository_set_bare(g_repo));
cl_git_pass(git_attr_get_many(values, g_repo, 0, "file.txt", 4, names));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert_equal_s("foobar", values[1]);
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
cl_git_pass(git_attr_get_many(values, g_repo, 0, "trial.txt", 4, names));
cl_assert(GIT_ATTR_IS_FALSE(values[0]));
cl_assert_equal_s("barfoo", values[1]);
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
cl_assert(GIT_ATTR_IS_TRUE(values[3]));
cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/sub/subdir.txt", 4, names));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert_equal_s("foobar", values[1]);
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
}
void test_attr_repo__sysdir(void)
{
git_str sysdir = GIT_STR_INIT;
const char *value;
cl_git_pass(p_mkdir("system", 0777));
cl_git_rewritefile("system/gitattributes", "file merge=foo");
cl_git_pass(git_str_joinpath(&sysdir, clar_sandbox_path(), "system"));
cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
g_repo = cl_git_sandbox_reopen();
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "merge"));
cl_assert_equal_s(value, "foo");
cl_git_pass(p_unlink("system/gitattributes"));
cl_git_pass(p_rmdir("system"));
git_str_dispose(&sysdir);
}
void test_attr_repo__sysdir_with_session(void)
{
const char *values[2], *attrs[2] = { "foo", "bar" };
git_str sysdir = GIT_STR_INIT;
git_attr_session session;
cl_git_pass(p_mkdir("system", 0777));
cl_git_rewritefile("system/gitattributes", "file foo=1 bar=2");
cl_git_pass(git_str_joinpath(&sysdir, clar_sandbox_path(), "system"));
cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
g_repo = cl_git_sandbox_reopen();
cl_git_pass(git_attr_session__init(&session, g_repo));
cl_git_pass(git_attr_get_many_with_session(values, g_repo, &session, NULL, "file", ARRAY_SIZE(attrs), attrs));
cl_assert_equal_s(values[0], "1");
cl_assert_equal_s(values[1], "2");
cl_git_pass(p_unlink("system/gitattributes"));
cl_git_pass(p_rmdir("system"));
git_str_dispose(&sysdir);
git_attr_session__free(&session);
}
void test_attr_repo__rewrite(void)
{
const char *value;
cl_git_rewritefile("attr/.gitattributes", "file.txt foo=first\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
cl_assert_equal_s(value, "first");
cl_git_rewritefile("attr/.gitattributes", "file.txt foo=second\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
cl_assert_equal_s(value, "second");
cl_git_rewritefile("attr/.gitattributes", "file.txt other=value\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
cl_assert_equal_p(value, NULL);
}
void test_attr_repo__rewrite_sysdir(void)
{
git_str sysdir = GIT_STR_INIT;
const char *value;
cl_git_pass(p_mkdir("system", 0777));
cl_git_pass(git_str_joinpath(&sysdir, clar_sandbox_path(), "system"));
cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
g_repo = cl_git_sandbox_reopen();
cl_git_rewritefile("system/gitattributes", "file foo=first");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "foo"));
cl_assert_equal_s(value, "first");
cl_git_rewritefile("system/gitattributes", "file foo=second");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "foo"));
cl_assert_equal_s(value, "second");
git_str_dispose(&sysdir);
}
void test_attr_repo__unlink(void)
{
const char *value;
cl_git_rewritefile("attr/.gitattributes", "file.txt foo=value1\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
cl_assert_equal_s(value, "value1");
cl_git_pass(p_unlink("attr/.gitattributes"));
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
cl_assert_equal_p(value, NULL);
}
| libgit2-main | tests/libgit2/attr/repo.c |
#include "clar_libgit2.h"
#include "attr_file.h"
#include "attr_expect.h"
void test_attr_lookup__simple(void)
{
git_attr_file *file;
git_attr_path path;
const char *value = NULL;
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
cl_assert_equal_s(cl_fixture("attr/attr0"), file->entry->path);
cl_assert(file->rules.length == 1);
cl_git_pass(git_attr_path__init(&path, "test", NULL, GIT_DIR_FLAG_UNKNOWN));
cl_assert_equal_s("test", path.path);
cl_assert_equal_s("test", path.basename);
cl_assert(!path.is_dir);
cl_git_pass(git_attr_file__lookup_one(file,&path,"binary",&value));
cl_assert(GIT_ATTR_IS_TRUE(value));
cl_git_pass(git_attr_file__lookup_one(file,&path,"missing",&value));
cl_assert(!value);
git_attr_path__free(&path);
git_attr_file__free(file);
}
static void run_test_cases(git_attr_file *file, struct attr_expected *cases, int force_dir)
{
git_attr_path path;
const char *value = NULL;
struct attr_expected *c;
int error;
for (c = cases; c->path != NULL; c++) {
cl_git_pass(git_attr_path__init(&path, c->path, NULL, GIT_DIR_FLAG_UNKNOWN));
if (force_dir)
path.is_dir = 1;
error = git_attr_file__lookup_one(file,&path,c->attr,&value);
cl_git_pass(error);
attr_check_expected(c->expected, c->expected_str, c->attr, value);
git_attr_path__free(&path);
}
}
void test_attr_lookup__match_variants(void)
{
git_attr_file *file;
git_attr_path path;
struct attr_expected dir_cases[] = {
{ "pat2", "attr2", EXPECT_TRUE, NULL },
{ "/testing/for/pat2", "attr2", EXPECT_TRUE, NULL },
{ "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL },
{ "/fun/fun/fun/pat4.dir", "attr4", EXPECT_TRUE, NULL },
{ "foo.pat5", "attr5", EXPECT_TRUE, NULL },
{ NULL, NULL, 0, NULL }
};
struct attr_expected cases[] = {
/* pat0 -> simple match */
{ "pat0", "attr0", EXPECT_TRUE, NULL },
{ "/testing/for/pat0", "attr0", EXPECT_TRUE, NULL },
{ "relative/to/pat0", "attr0", EXPECT_TRUE, NULL },
{ "this-contains-pat0-inside", "attr0", EXPECT_UNDEFINED, NULL },
{ "this-aint-right", "attr0", EXPECT_UNDEFINED, NULL },
{ "/this/pat0/dont/match", "attr0", EXPECT_UNDEFINED, NULL },
/* negative match */
{ "pat0", "attr1", EXPECT_TRUE, NULL },
{ "pat1", "attr1", EXPECT_UNDEFINED, NULL },
{ "/testing/for/pat1", "attr1", EXPECT_UNDEFINED, NULL },
{ "/testing/for/pat0", "attr1", EXPECT_TRUE, NULL },
{ "/testing/for/pat1/inside", "attr1", EXPECT_TRUE, NULL },
{ "misc", "attr1", EXPECT_TRUE, NULL },
/* dir match */
{ "pat2", "attr2", EXPECT_UNDEFINED, NULL },
{ "/testing/for/pat2", "attr2", EXPECT_UNDEFINED, NULL },
{ "/not/pat2/yousee", "attr2", EXPECT_UNDEFINED, NULL },
/* path match */
{ "pat3file", "attr3", EXPECT_UNDEFINED, NULL },
{ "/pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
{ "pat3dir/pat3file", "attr3", EXPECT_TRUE, NULL },
/* pattern* match */
{ "pat4.txt", "attr4", EXPECT_TRUE, NULL },
{ "/fun/fun/fun/pat4.c", "attr4", EXPECT_TRUE, NULL },
{ "pat4.", "attr4", EXPECT_TRUE, NULL },
{ "pat4", "attr4", EXPECT_UNDEFINED, NULL },
/* *pattern match */
{ "foo.pat5", "attr5", EXPECT_TRUE, NULL },
{ "/this/is/ok.pat5", "attr5", EXPECT_TRUE, NULL },
{ "/this/is/bad.pat5/yousee.txt", "attr5", EXPECT_UNDEFINED, NULL },
{ "foo.pat5", "attr100", EXPECT_UNDEFINED, NULL },
/* glob match with slashes */
{ "foo.pat6", "attr6", EXPECT_UNDEFINED, NULL },
{ "pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL },
{ "pat6/pat6/.pat6", "attr6", EXPECT_TRUE, NULL },
{ "pat6/pat6/extra/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL },
{ "/prefix/pat6/pat6/foobar.pat6", "attr6", EXPECT_UNDEFINED, NULL },
{ "/pat6/pat6/foobar.pat6", "attr6", EXPECT_TRUE, NULL },
/* complex pattern */
{ "pat7a12z", "attr7", EXPECT_TRUE, NULL },
{ "pat7e__x", "attr7", EXPECT_TRUE, NULL },
{ "pat7b/1y", "attr7", EXPECT_UNDEFINED, NULL }, /* ? does not match / */
{ "pat7e_x", "attr7", EXPECT_UNDEFINED, NULL },
{ "pat7aaaa", "attr7", EXPECT_UNDEFINED, NULL },
{ "pat7zzzz", "attr7", EXPECT_UNDEFINED, NULL },
{ "/this/can/be/anything/pat7a12z", "attr7", EXPECT_TRUE, NULL },
{ "but/it/still/must/match/pat7aaaa", "attr7", EXPECT_UNDEFINED, NULL },
{ "pat7aaay.fail", "attr7", EXPECT_UNDEFINED, NULL },
/* pattern with spaces */
{ "pat8 with spaces", "attr8", EXPECT_TRUE, NULL },
{ "/gotta love/pat8 with spaces", "attr8", EXPECT_TRUE, NULL },
{ "failing pat8 with spaces", "attr8", EXPECT_UNDEFINED, NULL },
{ "spaces", "attr8", EXPECT_UNDEFINED, NULL },
/* pattern at eof */
{ "pat9", "attr9", EXPECT_TRUE, NULL },
{ "/eof/pat9", "attr9", EXPECT_TRUE, NULL },
{ "pat", "attr9", EXPECT_UNDEFINED, NULL },
{ "at9", "attr9", EXPECT_UNDEFINED, NULL },
{ "pat9.fail", "attr9", EXPECT_UNDEFINED, NULL },
/* sentinel at end */
{ NULL, NULL, 0, NULL }
};
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
cl_assert_equal_s(cl_fixture("attr/attr1"), file->entry->path);
cl_assert(file->rules.length == 10);
cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL, GIT_DIR_FLAG_UNKNOWN));
cl_assert_equal_s("pat0", path.basename);
run_test_cases(file, cases, 0);
run_test_cases(file, dir_cases, 1);
git_attr_file__free(file);
git_attr_path__free(&path);
}
void test_attr_lookup__assign_variants(void)
{
git_attr_file *file;
struct attr_expected cases[] = {
/* pat0 -> simple assign */
{ "pat0", "simple", EXPECT_TRUE, NULL },
{ "/testing/pat0", "simple", EXPECT_TRUE, NULL },
{ "pat0", "fail", EXPECT_UNDEFINED, NULL },
{ "/testing/pat0", "fail", EXPECT_UNDEFINED, NULL },
/* negative assign */
{ "pat1", "neg", EXPECT_FALSE, NULL },
{ "/testing/pat1", "neg", EXPECT_FALSE, NULL },
{ "pat1", "fail", EXPECT_UNDEFINED, NULL },
{ "/testing/pat1", "fail", EXPECT_UNDEFINED, NULL },
/* forced undef */
{ "pat1", "notundef", EXPECT_TRUE, NULL },
{ "pat2", "notundef", EXPECT_UNDEFINED, NULL },
{ "/lead/in/pat1", "notundef", EXPECT_TRUE, NULL },
{ "/lead/in/pat2", "notundef", EXPECT_UNDEFINED, NULL },
/* assign value */
{ "pat3", "assigned", EXPECT_STRING, "test-value" },
{ "pat3", "notassigned", EXPECT_UNDEFINED, NULL },
/* assign value */
{ "pat4", "rule-with-more-chars", EXPECT_STRING, "value-with-more-chars" },
{ "pat4", "notassigned-rule-with-more-chars", EXPECT_UNDEFINED, NULL },
/* empty assignments */
{ "pat5", "empty", EXPECT_TRUE, NULL },
{ "pat6", "negempty", EXPECT_FALSE, NULL },
/* multiple assignment */
{ "pat7", "multiple", EXPECT_TRUE, NULL },
{ "pat7", "single", EXPECT_FALSE, NULL },
{ "pat7", "values", EXPECT_STRING, "1" },
{ "pat7", "also", EXPECT_STRING, "a-really-long-value/*" },
{ "pat7", "happy", EXPECT_STRING, "yes!" },
{ "pat8", "again", EXPECT_TRUE, NULL },
{ "pat8", "another", EXPECT_STRING, "12321" },
/* bad assignment */
{ "patbad0", "simple", EXPECT_UNDEFINED, NULL },
{ "patbad0", "notundef", EXPECT_TRUE, NULL },
{ "patbad1", "simple", EXPECT_UNDEFINED, NULL },
/* eof assignment */
{ "pat9", "at-eof", EXPECT_FALSE, NULL },
/* sentinel at end */
{ NULL, NULL, 0, NULL }
};
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
cl_assert(file->rules.length == 11);
run_test_cases(file, cases, 0);
git_attr_file__free(file);
}
void test_attr_lookup__check_attr_examples(void)
{
git_attr_file *file;
struct attr_expected cases[] = {
{ "foo.java", "diff", EXPECT_STRING, "java" },
{ "foo.java", "crlf", EXPECT_FALSE, NULL },
{ "foo.java", "myAttr", EXPECT_TRUE, NULL },
{ "foo.java", "other", EXPECT_UNDEFINED, NULL },
{ "/prefix/dir/foo.java", "diff", EXPECT_STRING, "java" },
{ "/prefix/dir/foo.java", "crlf", EXPECT_FALSE, NULL },
{ "/prefix/dir/foo.java", "myAttr", EXPECT_TRUE, NULL },
{ "/prefix/dir/foo.java", "other", EXPECT_UNDEFINED, NULL },
{ "NoMyAttr.java", "crlf", EXPECT_FALSE, NULL },
{ "NoMyAttr.java", "myAttr", EXPECT_UNDEFINED, NULL },
{ "NoMyAttr.java", "other", EXPECT_UNDEFINED, NULL },
{ "/prefix/dir/NoMyAttr.java", "crlf", EXPECT_FALSE, NULL },
{ "/prefix/dir/NoMyAttr.java", "myAttr", EXPECT_UNDEFINED, NULL },
{ "/prefix/dir/NoMyAttr.java", "other", EXPECT_UNDEFINED, NULL },
{ "README", "caveat", EXPECT_STRING, "unspecified" },
{ "/specific/path/README", "caveat", EXPECT_STRING, "unspecified" },
{ "README", "missing", EXPECT_UNDEFINED, NULL },
{ "/specific/path/README", "missing", EXPECT_UNDEFINED, NULL },
/* sentinel at end */
{ NULL, NULL, 0, NULL }
};
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
cl_assert(file->rules.length == 3);
run_test_cases(file, cases, 0);
git_attr_file__free(file);
}
void test_attr_lookup__from_buffer(void)
{
git_attr_file *file;
git_attr_file_source source = {0};
struct attr_expected cases[] = {
{ "abc", "foo", EXPECT_TRUE, NULL },
{ "abc", "bar", EXPECT_TRUE, NULL },
{ "abc", "baz", EXPECT_TRUE, NULL },
{ "aaa", "foo", EXPECT_TRUE, NULL },
{ "aaa", "bar", EXPECT_UNDEFINED, NULL },
{ "aaa", "baz", EXPECT_TRUE, NULL },
{ "qqq", "foo", EXPECT_UNDEFINED, NULL },
{ "qqq", "bar", EXPECT_UNDEFINED, NULL },
{ "qqq", "baz", EXPECT_TRUE, NULL },
{ NULL, NULL, 0, NULL }
};
cl_git_pass(git_attr_file__new(&file, NULL, &source));
cl_git_pass(git_attr_file__parse_buffer(NULL, file, "a* foo\nabc bar\n* baz", true));
cl_assert(file->rules.length == 3);
run_test_cases(file, cases, 0);
git_attr_file__free(file);
}
| libgit2-main | tests/libgit2/attr/lookup.c |
#include "clar_libgit2.h"
#include "git2/attr.h"
void test_attr_flags__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_attr_flags__bare(void)
{
git_repository *repo = cl_git_sandbox_init("testrepo.git");
const char *value;
cl_assert(git_repository_is_bare(repo));
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM, "README.md", "diff"));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
}
void test_attr_flags__index_vs_workdir(void)
{
git_repository *repo = cl_git_sandbox_init("attr_index");
const char *value;
cl_assert(!git_repository_is_bare(repo));
/* wd then index */
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"README.md", "bar"));
cl_assert(GIT_ATTR_IS_FALSE(value));
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"README.md", "blargh"));
cl_assert_equal_s(value, "goop");
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"README.txt", "foo"));
cl_assert(GIT_ATTR_IS_FALSE(value));
/* index then wd */
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"README.md", "bar"));
cl_assert(GIT_ATTR_IS_TRUE(value));
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"README.md", "blargh"));
cl_assert_equal_s(value, "garble");
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"README.txt", "foo"));
cl_assert(GIT_ATTR_IS_TRUE(value));
}
void test_attr_flags__subdir(void)
{
git_repository *repo = cl_git_sandbox_init("attr_index");
const char *value;
/* wd then index */
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"sub/sub/README.md", "bar"));
cl_assert_equal_s(value, "1234");
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"sub/sub/README.txt", "another"));
cl_assert_equal_s(value, "one");
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"sub/sub/README.txt", "again"));
cl_assert(GIT_ATTR_IS_TRUE(value));
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
"sub/sub/README.txt", "beep"));
cl_assert_equal_s(value, "10");
/* index then wd */
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"sub/sub/README.md", "bar"));
cl_assert_equal_s(value, "1337");
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"sub/sub/README.txt", "another"));
cl_assert_equal_s(value, "one");
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"sub/sub/README.txt", "again"));
cl_assert(GIT_ATTR_IS_TRUE(value));
cl_git_pass(git_attr_get(
&value, repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
"sub/sub/README.txt", "beep"));
cl_assert_equal_s(value, "5");
}
| libgit2-main | tests/libgit2/attr/flags.c |
#include "clar_libgit2.h"
#include "attr_file.h"
#include "attr_expect.h"
#define get_rule(X) ((git_attr_rule *)git_vector_get(&file->rules,(X)))
#define get_assign(R,Y) ((git_attr_assignment *)git_vector_get(&(R)->assigns,(Y)))
void test_attr_file__simple_read(void)
{
git_attr_file *file;
git_attr_assignment *assign;
git_attr_rule *rule;
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
cl_assert_equal_s(cl_fixture("attr/attr0"), file->entry->path);
cl_assert(file->rules.length == 1);
rule = get_rule(0);
cl_assert(rule != NULL);
cl_assert_equal_s("*", rule->match.pattern);
cl_assert(rule->match.length == 1);
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
cl_assert(rule->assigns.length == 1);
assign = get_assign(rule, 0);
cl_assert(assign != NULL);
cl_assert_equal_s("binary", assign->name);
cl_assert(GIT_ATTR_IS_TRUE(assign->value));
git_attr_file__free(file);
}
void test_attr_file__match_variants(void)
{
git_attr_file *file;
git_attr_rule *rule;
git_attr_assignment *assign;
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
cl_assert_equal_s(cl_fixture("attr/attr1"), file->entry->path);
cl_assert(file->rules.length == 10);
/* let's do a thorough check of this rule, then just verify
* the things that are unique for the later rules
*/
rule = get_rule(0);
cl_assert(rule);
cl_assert_equal_s("pat0", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat0"));
cl_assert(rule->assigns.length == 1);
assign = get_assign(rule,0);
cl_assert_equal_s("attr0", assign->name);
cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name));
cl_assert(GIT_ATTR_IS_TRUE(assign->value));
rule = get_rule(1);
cl_assert_equal_s("pat1", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat1"));
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0);
rule = get_rule(2);
cl_assert_equal_s("pat2", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat2"));
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_DIRECTORY) != 0);
rule = get_rule(3);
cl_assert_equal_s("pat3dir/pat3file", rule->match.pattern);
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_FULLPATH) != 0);
rule = get_rule(4);
cl_assert_equal_s("pat4.*", rule->match.pattern);
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
rule = get_rule(5);
cl_assert_equal_s("*.pat5", rule->match.pattern);
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
rule = get_rule(7);
cl_assert_equal_s("pat7[a-e]??[xyz]", rule->match.pattern);
cl_assert(rule->assigns.length == 1);
cl_assert((rule->match.flags & GIT_ATTR_FNMATCH_HASWILD) != 0);
assign = get_assign(rule,0);
cl_assert_equal_s("attr7", assign->name);
cl_assert(GIT_ATTR_IS_TRUE(assign->value));
rule = get_rule(8);
cl_assert_equal_s("pat8 with spaces", rule->match.pattern);
cl_assert(rule->match.length == strlen("pat8 with spaces"));
rule = get_rule(9);
cl_assert_equal_s("pat9", rule->match.pattern);
git_attr_file__free(file);
}
static void check_one_assign(
git_attr_file *file,
int rule_idx,
int assign_idx,
const char *pattern,
const char *name,
enum attr_expect_t expected,
const char *expected_str)
{
git_attr_rule *rule = get_rule(rule_idx);
git_attr_assignment *assign = get_assign(rule, assign_idx);
cl_assert_equal_s(pattern, rule->match.pattern);
cl_assert(rule->assigns.length == 1);
cl_assert_equal_s(name, assign->name);
cl_assert(assign->name_hash == git_attr_file__name_hash(assign->name));
attr_check_expected(expected, expected_str, assign->name, assign->value);
}
void test_attr_file__assign_variants(void)
{
git_attr_file *file;
git_attr_rule *rule;
git_attr_assignment *assign;
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
cl_assert_equal_s(cl_fixture("attr/attr2"), file->entry->path);
cl_assert(file->rules.length == 11);
check_one_assign(file, 0, 0, "pat0", "simple", EXPECT_TRUE, NULL);
check_one_assign(file, 1, 0, "pat1", "neg", EXPECT_FALSE, NULL);
check_one_assign(file, 2, 0, "*", "notundef", EXPECT_TRUE, NULL);
check_one_assign(file, 3, 0, "pat2", "notundef", EXPECT_UNDEFINED, NULL);
check_one_assign(file, 4, 0, "pat3", "assigned", EXPECT_STRING, "test-value");
check_one_assign(file, 5, 0, "pat4", "rule-with-more-chars", EXPECT_STRING, "value-with-more-chars");
check_one_assign(file, 6, 0, "pat5", "empty", EXPECT_TRUE, NULL);
check_one_assign(file, 7, 0, "pat6", "negempty", EXPECT_FALSE, NULL);
rule = get_rule(8);
cl_assert_equal_s("pat7", rule->match.pattern);
cl_assert(rule->assigns.length == 5);
/* assignments will be sorted by hash value, so we have to do
* lookups by search instead of by position
*/
assign = git_attr_rule__lookup_assignment(rule, "multiple");
cl_assert(assign);
cl_assert_equal_s("multiple", assign->name);
cl_assert(GIT_ATTR_IS_TRUE(assign->value));
assign = git_attr_rule__lookup_assignment(rule, "single");
cl_assert(assign);
cl_assert_equal_s("single", assign->name);
cl_assert(GIT_ATTR_IS_FALSE(assign->value));
assign = git_attr_rule__lookup_assignment(rule, "values");
cl_assert(assign);
cl_assert_equal_s("values", assign->name);
cl_assert_equal_s("1", assign->value);
assign = git_attr_rule__lookup_assignment(rule, "also");
cl_assert(assign);
cl_assert_equal_s("also", assign->name);
cl_assert_equal_s("a-really-long-value/*", assign->value);
assign = git_attr_rule__lookup_assignment(rule, "happy");
cl_assert(assign);
cl_assert_equal_s("happy", assign->name);
cl_assert_equal_s("yes!", assign->value);
assign = git_attr_rule__lookup_assignment(rule, "other");
cl_assert(!assign);
rule = get_rule(9);
cl_assert_equal_s("pat8", rule->match.pattern);
cl_assert(rule->assigns.length == 2);
assign = git_attr_rule__lookup_assignment(rule, "again");
cl_assert(assign);
cl_assert_equal_s("again", assign->name);
cl_assert(GIT_ATTR_IS_TRUE(assign->value));
assign = git_attr_rule__lookup_assignment(rule, "another");
cl_assert(assign);
cl_assert_equal_s("another", assign->name);
cl_assert_equal_s("12321", assign->value);
check_one_assign(file, 10, 0, "pat9", "at-eof", EXPECT_FALSE, NULL);
git_attr_file__free(file);
}
static void assert_examples(git_attr_file *file)
{
git_attr_rule *rule;
git_attr_assignment *assign;
rule = get_rule(0);
cl_assert_equal_s("*.java", rule->match.pattern);
cl_assert(rule->assigns.length == 3);
assign = git_attr_rule__lookup_assignment(rule, "diff");
cl_assert_equal_s("diff", assign->name);
cl_assert_equal_s("java", assign->value);
assign = git_attr_rule__lookup_assignment(rule, "crlf");
cl_assert_equal_s("crlf", assign->name);
cl_assert(GIT_ATTR_IS_FALSE(assign->value));
assign = git_attr_rule__lookup_assignment(rule, "myAttr");
cl_assert_equal_s("myAttr", assign->name);
cl_assert(GIT_ATTR_IS_TRUE(assign->value));
assign = git_attr_rule__lookup_assignment(rule, "missing");
cl_assert(assign == NULL);
rule = get_rule(1);
cl_assert_equal_s("NoMyAttr.java", rule->match.pattern);
cl_assert(rule->assigns.length == 1);
assign = get_assign(rule, 0);
cl_assert_equal_s("myAttr", assign->name);
cl_assert(GIT_ATTR_IS_UNSPECIFIED(assign->value));
rule = get_rule(2);
cl_assert_equal_s("README", rule->match.pattern);
cl_assert(rule->assigns.length == 1);
assign = get_assign(rule, 0);
cl_assert_equal_s("caveat", assign->name);
cl_assert_equal_s("unspecified", assign->value);
}
void test_attr_file__check_attr_examples(void)
{
git_attr_file *file;
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
cl_assert_equal_s(cl_fixture("attr/attr3"), file->entry->path);
cl_assert(file->rules.length == 3);
assert_examples(file);
git_attr_file__free(file);
}
void test_attr_file__whitespace(void)
{
git_attr_file *file;
cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr4")));
cl_assert_equal_s(cl_fixture("attr/attr4"), file->entry->path);
cl_assert(file->rules.length == 3);
assert_examples(file);
git_attr_file__free(file);
}
| libgit2-main | tests/libgit2/attr/file.c |
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "clar_libgit2.h"
#include "git2/sys/repository.h"
#include "attr.h"
static git_repository *g_repo = NULL;
void test_attr_macro__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
void test_attr_macro__macros(void)
{
const char *names[7] = { "rootattr", "binary", "diff", "crlf", "merge", "text", "frotz" };
const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
const char *names3[3] = { "macro2", "multi2", "multi3" };
const char *values[7];
g_repo = cl_git_sandbox_init("attr");
cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 7, names));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
cl_assert(GIT_ATTR_IS_FALSE(values[3]));
cl_assert(GIT_ATTR_IS_FALSE(values[4]));
cl_assert(GIT_ATTR_IS_FALSE(values[5]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[6]));
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
cl_assert_equal_s("77", values[4]);
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3));
cl_assert(GIT_ATTR_IS_TRUE(values[0]));
cl_assert(GIT_ATTR_IS_FALSE(values[1]));
cl_assert_equal_s("answer", values[2]);
}
void test_attr_macro__bad_macros(void)
{
const char *names[6] = { "rootattr", "positive", "negative",
"firstmacro", "secondmacro", "thirdmacro" };
const char *values[6];
g_repo = cl_git_sandbox_init("attr");
cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names));
/* these three just confirm that the "mymacro" rule ran */
cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[0]));
cl_assert(GIT_ATTR_IS_TRUE(values[1]));
cl_assert(GIT_ATTR_IS_FALSE(values[2]));
/* file contains:
* # let's try some malicious macro defs
* [attr]firstmacro -thirdmacro -secondmacro
* [attr]secondmacro firstmacro -firstmacro
* [attr]thirdmacro secondmacro=hahaha -firstmacro
* macro_bad firstmacro secondmacro thirdmacro
*
* firstmacro assignment list ends up with:
* -thirdmacro -secondmacro
* secondmacro assignment list expands "firstmacro" and ends up with:
* -thirdmacro -secondmacro -firstmacro
* thirdmacro assignment don't expand so list ends up with:
* secondmacro="hahaha"
*
* macro_bad assignment list ends up with:
* -thirdmacro -secondmacro firstmacro &&
* -thirdmacro -secondmacro -firstmacro secondmacro &&
* secondmacro="hahaha" thirdmacro
*
* so summary results should be:
* -firstmacro secondmacro="hahaha" thirdmacro
*/
cl_assert(GIT_ATTR_IS_FALSE(values[3]));
cl_assert_equal_s("hahaha", values[4]);
cl_assert(GIT_ATTR_IS_TRUE(values[5]));
}
void test_attr_macro__macros_in_root_wd_apply(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(p_mkdir("empty_standard_repo/dir", 0777));
cl_git_rewritefile("empty_standard_repo/.gitattributes", "[attr]customattr key=value\n");
cl_git_rewritefile("empty_standard_repo/dir/.gitattributes", "file customattr\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "dir/file", "key"));
cl_assert_equal_s(value, "value");
}
void test_attr_macro__changing_macro_in_root_wd_updates_attributes(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_rewritefile("empty_standard_repo/.gitattributes",
"[attr]customattr key=first\n"
"file customattr\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "key"));
cl_assert_equal_s(value, "first");
cl_git_rewritefile("empty_standard_repo/.gitattributes",
"[attr]customattr key=second\n"
"file customattr\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "key"));
cl_assert_equal_s(value, "second");
}
void test_attr_macro__macros_in_subdir_do_not_apply(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(p_mkdir("empty_standard_repo/dir", 0777));
cl_git_rewritefile("empty_standard_repo/dir/.gitattributes",
"[attr]customattr key=value\n"
"file customattr\n");
/* This should _not_ pass, as macros in subdirectories shall be ignored */
cl_git_pass(git_attr_get(&value, g_repo, 0, "dir/file", "key"));
cl_assert_equal_p(value, NULL);
}
void test_attr_macro__adding_macro_succeeds(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_attr_add_macro(g_repo, "macro", "key=value"));
cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "key"));
cl_assert_equal_s(value, "value");
}
void test_attr_macro__adding_boolean_macros_succeeds(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_attr_add_macro(g_repo, "macro-pos", "positive"));
cl_git_pass(git_attr_add_macro(g_repo, "macro-neg", "-negative"));
cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro-pos macro-neg\n");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "positive"));
cl_assert(GIT_ATTR_IS_TRUE(value));
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "negative"));
cl_assert(GIT_ATTR_IS_FALSE(value));
}
void test_attr_macro__redefining_macro_succeeds(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_attr_add_macro(g_repo, "macro", "key=value1"));
cl_git_pass(git_attr_add_macro(g_repo, "macro", "key=value2"));
cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "key"));
cl_assert_equal_s(value, "value2");
}
void test_attr_macro__recursive_macro_resolves(void)
{
const char *value;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_attr_add_macro(g_repo, "expandme", "key=value"));
cl_git_pass(git_attr_add_macro(g_repo, "macro", "expandme"));
cl_git_rewritefile("empty_standard_repo/.gitattributes", "file.txt macro");
cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "key"));
cl_assert_equal_s(value, "value");
}
| libgit2-main | tests/libgit2/attr/macro.c |
#include "clar_libgit2.h"
#include "git2/clone.h"
#include "git2/transport.h"
#include "git2/sys/transport.h"
#include "futils.h"
static int custom_transport(
git_transport **out,
git_remote *owner,
void *payload)
{
*((int*)payload) = 1;
return git_transport_local(out, owner, payload);
}
static int custom_transport_remote_create(
git_remote **out,
git_repository *repo,
const char *name,
const char *url,
void *payload)
{
int error;
GIT_UNUSED(payload);
if ((error = git_remote_create(out, repo, name, url)) < 0)
return error;
return 0;
}
void test_clone_transport__custom_transport(void)
{
git_repository *repo;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
int custom_transport_used = 0;
clone_opts.remote_cb = custom_transport_remote_create;
clone_opts.fetch_opts.callbacks.transport = custom_transport;
clone_opts.fetch_opts.callbacks.payload = &custom_transport_used;
cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./custom_transport.git", &clone_opts));
git_repository_free(repo);
cl_git_pass(git_futils_rmdir_r("./custom_transport.git", NULL, GIT_RMDIR_REMOVE_FILES));
cl_assert(custom_transport_used == 1);
}
| libgit2-main | tests/libgit2/clone/transport.c |
#include "clar_libgit2.h"
#include "git2/clone.h"
#include "../submodule/submodule_helpers.h"
#include "remote.h"
#include "futils.h"
#include "repository.h"
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
static git_clone_options g_options;
static git_repository *g_repo;
static git_reference* g_ref;
static git_remote* g_remote;
void test_clone_nonetwork__initialize(void)
{
git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
g_repo = NULL;
memset(&g_options, 0, sizeof(git_clone_options));
g_options.version = GIT_CLONE_OPTIONS_VERSION;
g_options.checkout_opts = dummy_opts;
g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
g_options.fetch_opts = dummy_fetch;
}
void test_clone_nonetwork__cleanup(void)
{
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
if (g_ref) {
git_reference_free(g_ref);
g_ref = NULL;
}
if (g_remote) {
git_remote_free(g_remote);
g_remote = NULL;
}
cl_fixture_cleanup("./foo");
}
void test_clone_nonetwork__bad_urls(void)
{
/* Clone should clean up the mess if the URL isn't a git repository */
cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
cl_assert(!git_fs_path_exists("./foo"));
g_options.bare = true;
cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
cl_assert(!git_fs_path_exists("./foo"));
cl_git_fail(git_clone(&g_repo, "git://example.com:asdf", "./foo", &g_options));
cl_git_fail(git_clone(&g_repo, "https://example.com:asdf/foo", "./foo", &g_options));
cl_git_fail(git_clone(&g_repo, "git://github.com/git://github.com/foo/bar.git.git",
"./foo", &g_options));
cl_git_fail(git_clone(&g_repo, "arrbee:my/bad:[email protected]:1111/strange:words.git",
"./foo", &g_options));
}
void test_clone_nonetwork__do_not_clean_existing_directory(void)
{
/* Clone should not remove the directory if it already exists, but
* Should clean up entries it creates. */
p_mkdir("./foo", GIT_DIR_MODE);
cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
cl_assert(git_fs_path_is_empty_dir("./foo"));
/* Try again with a bare repository. */
g_options.bare = true;
cl_git_fail(git_clone(&g_repo, "not_a_repo", "./foo", &g_options));
cl_assert(git_fs_path_is_empty_dir("./foo"));
}
void test_clone_nonetwork__local(void)
{
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
}
void test_clone_nonetwork__local_absolute_path(void)
{
const char *local_src;
local_src = cl_fixture("testrepo.git");
cl_git_pass(git_clone(&g_repo, local_src, "./foo", &g_options));
}
void test_clone_nonetwork__local_bare(void)
{
g_options.bare = true;
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
}
void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
{
cl_git_mkfile("./foo", "Bar!");
cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
}
void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
{
p_mkdir("./foo", GIT_DIR_MODE);
cl_git_mkfile("./foo/bar", "Baz!");
cl_git_fail(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
}
static int custom_origin_name_remote_create(
git_remote **out,
git_repository *repo,
const char *name,
const char *url,
void *payload)
{
GIT_UNUSED(name);
GIT_UNUSED(payload);
return git_remote_create(out, repo, "my_origin", url);
}
void test_clone_nonetwork__custom_origin_name(void)
{
g_options.remote_cb = custom_origin_name_remote_create;
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
cl_git_pass(git_remote_lookup(&g_remote, g_repo, "my_origin"));
}
void test_clone_nonetwork__defaults(void)
{
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", NULL));
cl_assert(g_repo);
cl_git_pass(git_remote_lookup(&g_remote, g_repo, "origin"));
}
void test_clone_nonetwork__cope_with_already_existing_directory(void)
{
p_mkdir("./foo", GIT_DIR_MODE);
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
}
void test_clone_nonetwork__can_prevent_the_checkout_of_a_standard_repo(void)
{
git_str path = GIT_STR_INIT;
g_options.checkout_opts.checkout_strategy = 0;
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&path)));
git_str_dispose(&path);
}
void test_clone_nonetwork__can_checkout_given_branch(void)
{
git_reference *remote_head;
g_options.checkout_branch = "test";
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
cl_assert_equal_i(0, git_repository_head_unborn(g_repo));
cl_git_pass(git_repository_head(&g_ref, g_repo));
cl_assert_equal_s(git_reference_name(g_ref), "refs/heads/test");
cl_assert(git_fs_path_exists("foo/readme.txt"));
cl_git_pass(git_reference_lookup(&remote_head, g_repo, "refs/remotes/origin/HEAD"));
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(remote_head));
cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head));
git_reference_free(remote_head);
}
static int clone_cancel_fetch_transfer_progress_cb(
const git_indexer_progress *stats, void *data)
{
GIT_UNUSED(stats); GIT_UNUSED(data);
return -54321;
}
void test_clone_nonetwork__can_cancel_clone_in_fetch(void)
{
g_options.checkout_branch = "test";
g_options.fetch_opts.callbacks.transfer_progress =
clone_cancel_fetch_transfer_progress_cb;
cl_git_fail_with(git_clone(
&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
-54321);
cl_assert(!g_repo);
cl_assert(!git_fs_path_exists("foo/readme.txt"));
}
static int clone_cancel_checkout_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *b,
const git_diff_file *t,
const git_diff_file *w,
void *payload)
{
const char *at_file = payload;
GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);
if (!strcmp(path, at_file))
return -12345;
return 0;
}
void test_clone_nonetwork__can_cancel_clone_in_checkout(void)
{
g_options.checkout_branch = "test";
g_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
g_options.checkout_opts.notify_cb = clone_cancel_checkout_cb;
g_options.checkout_opts.notify_payload = "readme.txt";
cl_git_fail_with(git_clone(
&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options),
-12345);
cl_assert(!g_repo);
cl_assert(!git_fs_path_exists("foo/readme.txt"));
}
void test_clone_nonetwork__can_detached_head(void)
{
git_object *obj;
git_repository *cloned;
git_reference *cloned_head;
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
cl_git_pass(git_revparse_single(&obj, g_repo, "master~1"));
cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(obj)));
cl_git_pass(git_clone(&cloned, "./foo", "./foo1", &g_options));
cl_assert(git_repository_head_detached(cloned));
cl_git_pass(git_repository_head(&cloned_head, cloned));
cl_assert_equal_oid(git_object_id(obj), git_reference_target(cloned_head));
git_object_free(obj);
git_reference_free(cloned_head);
git_repository_free(cloned);
cl_fixture_cleanup("./foo1");
}
void test_clone_nonetwork__clone_tag_to_tree(void)
{
git_repository *stage;
git_index_entry entry;
git_index *index;
git_odb *odb;
git_oid tree_id;
git_tree *tree;
git_reference *tag;
git_tree_entry *tentry;
const char *file_path = "some/deep/path.txt";
const char *file_content = "some content\n";
const char *tag_name = "refs/tags/tree-tag";
stage = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_odb(&odb, stage));
cl_git_pass(git_index_new(&index));
memset(&entry, 0, sizeof(git_index_entry));
entry.path = file_path;
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_odb_write(&entry.id, odb, file_content, strlen(file_content), GIT_OBJECT_BLOB));
cl_git_pass(git_index_add(index, &entry));
cl_git_pass(git_index_write_tree_to(&tree_id, index, stage));
cl_git_pass(git_reference_create(&tag, stage, tag_name, &tree_id, 0, NULL));
git_reference_free(tag);
git_odb_free(odb);
git_index_free(index);
g_options.local = GIT_CLONE_NO_LOCAL;
cl_git_pass(git_clone(&g_repo, cl_git_path_url(git_repository_path(stage)), "./foo", &g_options));
git_repository_free(stage);
cl_git_pass(git_reference_lookup(&tag, g_repo, tag_name));
cl_git_pass(git_tree_lookup(&tree, g_repo, git_reference_target(tag)));
git_reference_free(tag);
cl_git_pass(git_tree_entry_bypath(&tentry, tree, file_path));
git_tree_entry_free(tentry);
git_tree_free(tree);
cl_fixture_cleanup("testrepo.git");
}
static void assert_correct_reflog(const char *name)
{
git_reflog *log;
const git_reflog_entry *entry;
git_str expected_message = GIT_STR_INIT;
git_str_printf(&expected_message,
"clone: from %s", cl_git_fixture_url("testrepo.git"));
cl_git_pass(git_reflog_read(&log, g_repo, name));
cl_assert_equal_i(1, git_reflog_entrycount(log));
entry = git_reflog_entry_byindex(log, 0);
cl_assert_equal_s(expected_message.ptr, git_reflog_entry_message(entry));
git_reflog_free(log);
git_str_dispose(&expected_message);
}
void test_clone_nonetwork__clone_updates_reflog_properly(void)
{
cl_git_pass(git_clone(&g_repo, cl_git_fixture_url("testrepo.git"), "./foo", &g_options));
assert_correct_reflog("HEAD");
assert_correct_reflog("refs/heads/master");
}
static void cleanup_repository(void *path)
{
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
cl_fixture_cleanup((const char *)path);
}
void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
{
git_config *config;
git_repository *repo;
const char *str;
/* Create an empty repo to clone from */
cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
cl_set_cleanup(&cleanup_repository, "./repowithunborn");
cl_git_pass(git_clone(&repo, "./test1", "./repowithunborn", NULL));
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s("origin", str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s("refs/heads/master", str);
git_config_free(config);
git_repository_free(repo);
cl_fixture_cleanup("./repowithunborn");
}
| libgit2-main | tests/libgit2/clone/nonetwork.c |
#include "clar_libgit2.h"
#include "git2/clone.h"
#include "clone.h"
#include "path.h"
#include "posix.h"
#include "futils.h"
static int file_url(git_str *buf, const char *host, const char *path)
{
if (path[0] == '/')
path++;
git_str_clear(buf);
return git_str_printf(buf, "file://%s/%s", host, path);
}
#ifdef GIT_WIN32
static int git_style_unc_path(git_str *buf, const char *host, const char *path)
{
git_str_clear(buf);
if (host)
git_str_printf(buf, "//%s/", host);
if (path[0] == '/')
path++;
if (git__isalpha(path[0]) && path[1] == ':' && path[2] == '/') {
git_str_printf(buf, "%c$/", path[0]);
path += 3;
}
git_str_puts(buf, path);
return git_str_oom(buf) ? -1 : 0;
}
static int unc_path(git_str *buf, const char *host, const char *path)
{
char *c;
if (git_style_unc_path(buf, host, path) < 0)
return -1;
for (c = buf->ptr; *c; c++)
if (*c == '/')
*c = '\\';
return 0;
}
#endif
void test_clone_local__should_clone_local(void)
{
git_str buf = GIT_STR_INIT;
/* we use a fixture path because it needs to exist for us to want to clone */
const char *path = cl_fixture("testrepo.git");
cl_git_pass(file_url(&buf, "", path));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
cl_git_pass(file_url(&buf, "localhost", path));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
cl_git_pass(file_url(&buf, "other-host.mycompany.com", path));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
/* Ensure that file:/// urls are percent decoded: .git == %2e%67%69%74 */
cl_git_pass(file_url(&buf, "", path));
git_str_shorten(&buf, 4);
cl_git_pass(git_str_puts(&buf, "%2e%67%69%74"));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_AUTO));
cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL));
cl_assert_equal_i(1, git_clone__should_clone_local(buf.ptr, GIT_CLONE_LOCAL_NO_LINKS));
cl_assert_equal_i(0, git_clone__should_clone_local(buf.ptr, GIT_CLONE_NO_LOCAL));
cl_assert_equal_i(1, git_clone__should_clone_local(path, GIT_CLONE_LOCAL_AUTO));
cl_assert_equal_i(1, git_clone__should_clone_local(path, GIT_CLONE_LOCAL));
cl_assert_equal_i(1, git_clone__should_clone_local(path, GIT_CLONE_LOCAL_NO_LINKS));
cl_assert_equal_i(0, git_clone__should_clone_local(path, GIT_CLONE_NO_LOCAL));
git_str_dispose(&buf);
}
void test_clone_local__hardlinks(void)
{
git_repository *repo;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
git_str buf = GIT_STR_INIT;
struct stat st;
/*
* In this first clone, we just copy over, since the temp dir
* will often be in a different filesystem, so we cannot
* link. It also allows us to control the number of links
*/
opts.bare = true;
opts.local = GIT_CLONE_LOCAL_NO_LINKS;
cl_git_pass(git_clone(&repo, cl_fixture("testrepo.git"), "./clone.git", &opts));
git_repository_free(repo);
/* This second clone is in the same filesystem, so we can hardlink */
opts.local = GIT_CLONE_LOCAL;
cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone2.git", &opts));
#ifndef GIT_WIN32
git_str_clear(&buf);
cl_git_pass(git_str_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
cl_git_pass(p_stat(buf.ptr, &st));
cl_assert_equal_i(2, st.st_nlink);
#endif
git_repository_free(repo);
git_str_clear(&buf);
opts.local = GIT_CLONE_LOCAL_NO_LINKS;
cl_git_pass(git_clone(&repo, cl_git_path_url("clone.git"), "./clone3.git", &opts));
git_str_clear(&buf);
cl_git_pass(git_str_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
cl_git_pass(p_stat(buf.ptr, &st));
cl_assert_equal_i(1, st.st_nlink);
git_repository_free(repo);
/* this one should automatically use links */
cl_git_pass(git_clone(&repo, "./clone.git", "./clone4.git", NULL));
#ifndef GIT_WIN32
git_str_clear(&buf);
cl_git_pass(git_str_join_n(&buf, '/', 4, git_repository_path(repo), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
cl_git_pass(p_stat(buf.ptr, &st));
cl_assert_equal_i(3, st.st_nlink);
#endif
git_str_dispose(&buf);
git_repository_free(repo);
cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_pass(git_futils_rmdir_r("./clone2.git", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_pass(git_futils_rmdir_r("./clone3.git", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_pass(git_futils_rmdir_r("./clone4.git", NULL, GIT_RMDIR_REMOVE_FILES));
}
void test_clone_local__standard_unc_paths_are_written_git_style(void)
{
#ifdef GIT_WIN32
git_repository *repo;
git_remote *remote;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
git_str unc = GIT_STR_INIT, git_unc = GIT_STR_INIT;
/* we use a fixture path because it needs to exist for us to want to clone */
const char *path = cl_fixture("testrepo.git");
cl_git_pass(unc_path(&unc, "localhost", path));
cl_git_pass(git_style_unc_path(&git_unc, "localhost", path));
cl_git_pass(git_clone(&repo, unc.ptr, "./clone.git", &opts));
cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
cl_assert_equal_s(git_unc.ptr, git_remote_url(remote));
git_remote_free(remote);
git_repository_free(repo);
git_str_dispose(&unc);
git_str_dispose(&git_unc);
cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
#endif
}
void test_clone_local__git_style_unc_paths(void)
{
#ifdef GIT_WIN32
git_repository *repo;
git_remote *remote;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
git_str git_unc = GIT_STR_INIT;
/* we use a fixture path because it needs to exist for us to want to clone */
const char *path = cl_fixture("testrepo.git");
cl_git_pass(git_style_unc_path(&git_unc, "localhost", path));
cl_git_pass(git_clone(&repo, git_unc.ptr, "./clone.git", &opts));
cl_git_pass(git_remote_lookup(&remote, repo, "origin"));
cl_assert_equal_s(git_unc.ptr, git_remote_url(remote));
git_remote_free(remote);
git_repository_free(repo);
git_str_dispose(&git_unc);
cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
#endif
}
| libgit2-main | tests/libgit2/clone/local.c |
#include "clar_libgit2.h"
#include "git2/clone.h"
#include "repository.h"
#include "repo/repo_helpers.h"
static git_clone_options g_options;
static git_repository *g_repo;
static git_repository *g_repo_cloned;
void test_clone_empty__initialize(void)
{
git_repository *sandbox = cl_git_sandbox_init("empty_bare.git");
git_fetch_options dummy_options = GIT_FETCH_OPTIONS_INIT;
cl_git_remove_placeholders(git_repository_path(sandbox), "dummy-marker.txt");
g_repo = NULL;
memset(&g_options, 0, sizeof(git_clone_options));
g_options.version = GIT_CLONE_OPTIONS_VERSION;
g_options.fetch_opts = dummy_options;
}
void test_clone_empty__cleanup(void)
{
cl_fixture_cleanup("tmp_global_path");
cl_git_sandbox_cleanup();
}
static void cleanup_repository(void *path)
{
cl_fixture_cleanup((const char *)path);
git_repository_free(g_repo_cloned);
g_repo_cloned = NULL;
}
void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
{
char *local_name = "refs/heads/master";
const char *expected_tracked_branch_name = "refs/remotes/origin/master";
const char *expected_remote_name = "origin";
git_buf buf = GIT_BUF_INIT;
git_reference *ref;
cl_set_cleanup(&cleanup_repository, "./empty");
g_options.bare = true;
cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options));
/* Although the HEAD is unborn... */
cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned, local_name));
/* ...one can still retrieve the name of the remote tracking reference */
cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, local_name));
cl_assert_equal_s(expected_tracked_branch_name, buf.ptr);
git_buf_dispose(&buf);
/* ...and the name of the remote... */
cl_git_pass(git_branch_remote_name(&buf, g_repo_cloned, expected_tracked_branch_name));
cl_assert_equal_s(expected_remote_name, buf.ptr);
git_buf_dispose(&buf);
/* ...even when the remote HEAD is unborn as well */
cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo_cloned,
expected_tracked_branch_name));
}
void test_clone_empty__respects_initialbranch_config(void)
{
git_buf buf = GIT_BUF_INIT;
create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch");
cl_set_cleanup(&cleanup_repository, "./empty");
g_options.bare = true;
cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options));
cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, "refs/heads/my_default_branch"));
cl_assert_equal_s("refs/remotes/origin/my_default_branch", buf.ptr);
git_buf_dispose(&buf);
}
void test_clone_empty__can_clone_an_empty_local_repo(void)
{
cl_set_cleanup(&cleanup_repository, "./empty");
cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options));
}
void test_clone_empty__can_clone_an_empty_standard_repo(void)
{
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
cl_set_cleanup(&cleanup_repository, "./empty");
cl_git_pass(git_clone(&g_repo_cloned, "./empty_standard_repo", "./empty", &g_options));
}
| libgit2-main | tests/libgit2/clone/empty.c |
#include "clar_libgit2.h"
#include "futils.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",
"[email protected]",
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[] = {
"493568b7a2681187aaac8a58d3f1eab1527cba84", NULL };
char *oids_untracked[] = {
"7f89a8b15c878809c5c54d1ff8f8c9674154017b",
"493568b7a2681187aaac8a58d3f1eab1527cba84", NULL };
char *oids_ignored[] = {
"c95599a8fef20a7e57582c6727b1a0d02e0a5828",
"7f89a8b15c878809c5c54d1ff8f8c9674154017b",
"493568b7a2681187aaac8a58d3f1eab1527cba84", 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);
/* ensure stash_foreach operates with INCLUDE_UNTRACKED */
cl_git_pass(git_stash_save(
&stash_tip_oid,
repo,
signature,
NULL,
GIT_STASH_INCLUDE_UNTRACKED));
data.oids = oids_untracked;
data.invokes = 0;
cl_git_pass(git_stash_foreach(repo, callback_cb, &data));
cl_assert_equal_i(2, data.invokes);
/* ensure stash_foreach operates with INCLUDE_IGNORED */
cl_git_pass(git_stash_save(
&stash_tip_oid,
repo,
signature,
NULL,
GIT_STASH_INCLUDE_IGNORED));
data.oids = oids_ignored;
data.invokes = 0;
cl_git_pass(git_stash_foreach(repo, callback_cb, &data));
cl_assert_equal_i(3, data.invokes);
}
| libgit2-main | tests/libgit2/stash/foreach.c |
#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", "[email protected]", 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_submodule_free(sm);
sm = NULL;
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);
}
| libgit2-main | tests/libgit2/stash/submodules.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "stash_helpers.h"
static git_signature *signature;
static git_repository *repo;
static git_index *repo_index;
void test_stash_apply__initialize(void)
{
git_oid oid;
repo = cl_git_sandbox_init_new("stash");
cl_git_pass(git_repository_index(&repo_index, repo));
cl_git_pass(git_signature_new(&signature, "nulltoken", "[email protected]", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
cl_git_mkfile("stash/what", "hello\n");
cl_git_mkfile("stash/how", "small\n");
cl_git_mkfile("stash/who", "world\n");
cl_git_mkfile("stash/where", "meh\n");
cl_git_pass(git_index_add_bypath(repo_index, "what"));
cl_git_pass(git_index_add_bypath(repo_index, "how"));
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
cl_git_rewritefile("stash/what", "goodbye\n");
cl_git_rewritefile("stash/who", "funky world\n");
cl_git_mkfile("stash/when", "tomorrow\n");
cl_git_mkfile("stash/why", "would anybody use stash?\n");
cl_git_mkfile("stash/where", "????\n");
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_git_pass(git_index_add_bypath(repo_index, "why"));
cl_git_pass(git_index_add_bypath(repo_index, "where"));
cl_git_pass(git_index_write(repo_index));
cl_git_rewritefile("stash/where", "....\n");
/* Pre-stash state */
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
assert_status(repo, "where", GIT_STATUS_INDEX_NEW|GIT_STATUS_WT_MODIFIED);
cl_git_pass(git_stash_save(&oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
/* Post-stash state */
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, "why", GIT_ENOTFOUND);
assert_status(repo, "where", GIT_ENOTFOUND);
}
void test_stash_apply__cleanup(void)
{
git_signature_free(signature);
signature = NULL;
git_index_free(repo_index);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
void test_stash_apply__with_default(void)
{
git_str where = GIT_STR_INIT;
cl_git_pass(git_stash_apply(repo, 0, NULL));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
assert_status(repo, "where", GIT_STATUS_INDEX_NEW);
cl_git_pass(git_futils_readbuffer(&where, "stash/where"));
cl_assert_equal_s("....\n", where.ptr);
git_str_dispose(&where);
}
void test_stash_apply__with_existing_file(void)
{
cl_git_mkfile("stash/where", "oops!\n");
cl_git_fail(git_stash_apply(repo, 0, NULL));
}
void test_stash_apply__merges_new_file(void)
{
const git_index_entry *ancestor, *our, *their;
cl_git_mkfile("stash/where", "committed before stash\n");
cl_git_pass(git_index_add_bypath(repo_index, "where"));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
cl_git_pass(git_stash_apply(repo, 0, NULL));
cl_assert_equal_i(1, git_index_has_conflicts(repo_index));
assert_status(repo, "what", GIT_STATUS_INDEX_MODIFIED);
cl_git_pass(git_index_conflict_get(&ancestor, &our, &their, repo_index, "where")); /* unmerged */
assert_status(repo, "who", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
}
void test_stash_apply__with_reinstate_index(void)
{
git_str where = GIT_STR_INIT;
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
opts.flags = GIT_STASH_APPLY_REINSTATE_INDEX;
cl_git_pass(git_stash_apply(repo, 0, &opts));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
assert_status(repo, "where", GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED);
cl_git_pass(git_futils_readbuffer(&where, "stash/where"));
cl_assert_equal_s("....\n", where.ptr);
git_str_dispose(&where);
}
void test_stash_apply__conflict_index_with_default(void)
{
const git_index_entry *ancestor;
const git_index_entry *our;
const git_index_entry *their;
cl_git_rewritefile("stash/who", "nothing\n");
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_git_pass(git_index_write(repo_index));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
cl_git_pass(git_stash_apply(repo, 0, NULL));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 1);
assert_status(repo, "what", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "how", GIT_STATUS_CURRENT);
cl_git_pass(git_index_conflict_get(&ancestor, &our, &their, repo_index, "who")); /* unmerged */
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
}
void test_stash_apply__conflict_index_with_reinstate_index(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
opts.flags = GIT_STASH_APPLY_REINSTATE_INDEX;
cl_git_rewritefile("stash/who", "nothing\n");
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_git_pass(git_index_write(repo_index));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
cl_git_fail_with(git_stash_apply(repo, 0, &opts), GIT_ECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
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, "why", GIT_ENOTFOUND);
}
void test_stash_apply__conflict_untracked_with_default(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
cl_git_mkfile("stash/when", "nothing\n");
cl_git_fail_with(git_stash_apply(repo, 0, &opts), GIT_ECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
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, "why", GIT_ENOTFOUND);
}
void test_stash_apply__conflict_untracked_with_reinstate_index(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
opts.flags = GIT_STASH_APPLY_REINSTATE_INDEX;
cl_git_mkfile("stash/when", "nothing\n");
cl_git_fail_with(git_stash_apply(repo, 0, &opts), GIT_ECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
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, "why", GIT_ENOTFOUND);
}
void test_stash_apply__conflict_workdir_with_default(void)
{
cl_git_rewritefile("stash/what", "ciao\n");
cl_git_fail_with(git_stash_apply(repo, 0, NULL), GIT_ECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
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, "why", GIT_ENOTFOUND);
}
void test_stash_apply__conflict_workdir_with_reinstate_index(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
opts.flags = GIT_STASH_APPLY_REINSTATE_INDEX;
cl_git_rewritefile("stash/what", "ciao\n");
cl_git_fail_with(git_stash_apply(repo, 0, &opts), GIT_ECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
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, "why", GIT_ENOTFOUND);
}
void test_stash_apply__conflict_commit_with_default(void)
{
const git_index_entry *ancestor;
const git_index_entry *our;
const git_index_entry *their;
cl_git_rewritefile("stash/what", "ciao\n");
cl_git_pass(git_index_add_bypath(repo_index, "what"));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
cl_git_pass(git_stash_apply(repo, 0, NULL));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 1);
cl_git_pass(git_index_conflict_get(&ancestor, &our, &their, repo_index, "what")); /* unmerged */
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
}
void test_stash_apply__conflict_commit_with_reinstate_index(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
const git_index_entry *ancestor;
const git_index_entry *our;
const git_index_entry *their;
opts.flags = GIT_STASH_APPLY_REINSTATE_INDEX;
cl_git_rewritefile("stash/what", "ciao\n");
cl_git_pass(git_index_add_bypath(repo_index, "what"));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
cl_git_pass(git_stash_apply(repo, 0, &opts));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 1);
cl_git_pass(git_index_conflict_get(&ancestor, &our, &their, repo_index, "what")); /* unmerged */
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
}
void test_stash_apply__fails_with_uncommitted_changes_in_index(void)
{
cl_git_rewritefile("stash/who", "nothing\n");
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_git_pass(git_index_write(repo_index));
cl_git_fail_with(git_stash_apply(repo, 0, NULL), GIT_EUNCOMMITTED);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_CURRENT);
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_INDEX_MODIFIED);
assert_status(repo, "when", GIT_ENOTFOUND);
assert_status(repo, "why", GIT_ENOTFOUND);
}
void test_stash_apply__pop(void)
{
cl_git_pass(git_stash_pop(repo, 0, NULL));
cl_git_fail_with(git_stash_pop(repo, 0, NULL), GIT_ENOTFOUND);
}
struct seen_paths {
bool what;
bool how;
bool who;
bool when;
};
static int checkout_notify(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
struct seen_paths *seen_paths = (struct seen_paths *)payload;
GIT_UNUSED(why);
GIT_UNUSED(baseline);
GIT_UNUSED(target);
GIT_UNUSED(workdir);
if (strcmp(path, "what") == 0)
seen_paths->what = 1;
else if (strcmp(path, "how") == 0)
seen_paths->how = 1;
else if (strcmp(path, "who") == 0)
seen_paths->who = 1;
else if (strcmp(path, "when") == 0)
seen_paths->when = 1;
return 0;
}
void test_stash_apply__executes_notify_cb(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
struct seen_paths seen_paths = {0};
opts.checkout_options.notify_cb = checkout_notify;
opts.checkout_options.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.checkout_options.notify_payload = &seen_paths;
cl_git_pass(git_stash_apply(repo, 0, &opts));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
assert_status(repo, "where", GIT_STATUS_INDEX_NEW);
cl_assert_equal_b(true, seen_paths.what);
cl_assert_equal_b(false, seen_paths.how);
cl_assert_equal_b(true, seen_paths.who);
cl_assert_equal_b(true, seen_paths.when);
}
static int progress_cb(
git_stash_apply_progress_t progress,
void *payload)
{
git_stash_apply_progress_t *p = (git_stash_apply_progress_t *)payload;
cl_assert_equal_i((*p)+1, progress);
*p = progress;
return 0;
}
void test_stash_apply__calls_progress_cb(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
git_stash_apply_progress_t progress = GIT_STASH_APPLY_PROGRESS_NONE;
opts.progress_cb = progress_cb;
opts.progress_payload = &progress;
cl_git_pass(git_stash_apply(repo, 0, &opts));
cl_assert_equal_i(progress, GIT_STASH_APPLY_PROGRESS_DONE);
}
static int aborting_progress_cb(
git_stash_apply_progress_t progress,
void *payload)
{
GIT_UNUSED(payload);
if (progress == GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED)
return -44;
return 0;
}
void test_stash_apply__progress_cb_can_abort(void)
{
git_stash_apply_options opts = GIT_STASH_APPLY_OPTIONS_INIT;
opts.progress_cb = aborting_progress_cb;
cl_git_fail_with(-44, git_stash_apply(repo, 0, &opts));
}
void test_stash_apply__uses_reflog_like_indices_1(void)
{
git_oid oid;
cl_git_mkfile("stash/untracked", "untracked\n");
cl_git_pass(git_stash_save(&oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
assert_status(repo, "untracked", GIT_ENOTFOUND);
/* stash@{1} is the oldest (first) stash we made */
cl_git_pass(git_stash_apply(repo, 1, NULL));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "how", GIT_STATUS_CURRENT);
assert_status(repo, "who", GIT_STATUS_WT_MODIFIED);
assert_status(repo, "when", GIT_STATUS_WT_NEW);
assert_status(repo, "why", GIT_STATUS_INDEX_NEW);
assert_status(repo, "where", GIT_STATUS_INDEX_NEW);
}
void test_stash_apply__uses_reflog_like_indices_2(void)
{
git_oid oid;
cl_git_mkfile("stash/untracked", "untracked\n");
cl_git_pass(git_stash_save(&oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
assert_status(repo, "untracked", GIT_ENOTFOUND);
/* stash@{0} is the newest stash we made immediately above */
cl_git_pass(git_stash_apply(repo, 0, NULL));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "untracked", GIT_STATUS_WT_NEW);
}
| libgit2-main | tests/libgit2/stash/apply.c |
#include "clar_libgit2.h"
#include "futils.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", "[email protected]", 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_fs_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_fs_path_exists("stash/one.txt"));
cl_assert(git_fs_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_fs_path_exists("stash/two.txt"));
cl_assert(git_fs_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_fs_path_exists("stash/three.txt"));
cl_assert(git_fs_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_oid(&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);
}
static 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_oid(out, git_object_id(top_stash));
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)));
cl_git_pass(git_stash_drop(repo, 0));
retrieve_top_stash_id(&oid);
cl_assert_equal_oid(&oid, git_object_id(next_top_stash));
git_object_free(next_top_stash);
}
| libgit2-main | tests/libgit2/stash/drop.c |
#include "clar_libgit2.h"
#include "futils.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_mkfile("stash/why", "would anybody use stash?\n"); /* 88c2533e21f098b89c91a431d8075cbde422a51 */
cl_git_mkfile("stash/where", "????\n"); /* e08f7fbb9a42a0c5367cf8b349f1f08c3d56bd72 */
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, "why"));
cl_git_pass(git_index_add_bypath(index, "where"));
cl_git_pass(git_index_write(index));
cl_git_rewritefile("stash/what", "see you later\n"); /* bc99dc98b3eba0e9157e94769cd4d49cb49de449 */
cl_git_mkfile("stash/where", "....\n"); /* e3d6434ec12eb76af8dfa843a64ba6ab91014a0b */
git_index_free(index);
}
void assert_status(
git_repository *repo,
const char *path,
int status_flags)
{
unsigned int status;
if (status_flags < 0)
cl_assert_equal_i(status_flags, git_status_file(&status, repo, path));
else {
cl_git_pass(git_status_file(&status, repo, path));
cl_assert_equal_i((unsigned int)status_flags, status);
}
}
| libgit2-main | tests/libgit2/stash/stash_helpers.c |
#include "clar_libgit2.h"
#include "futils.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", "[email protected]", 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_object_t 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_OBJECT_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:why", "88c2533e21f098b89c91a431d8075cbdbe422a51"); /* would anybody use stash? */
assert_blob_oid("refs/stash:where", "e3d6434ec12eb76af8dfa843a64ba6ab91014a0b"); /* .... */
assert_blob_oid("refs/stash:.gitignore", "ac4d88de61733173d9959e4b77c69b9f17a00980");
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:why", "88c2533e21f098b89c91a431d8075cbdbe422a51"); /* would anybody use stash? */
assert_blob_oid("refs/stash^2:where", "e08f7fbb9a42a0c5367cf8b349f1f08c3d56bd72"); /* ???? */
assert_blob_oid("refs/stash^2:.gitignore", "ac4d88de61733173d9959e4b77c69b9f17a00980");
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__untracked_skips_ignored(void)
{
cl_git_append2file("stash/.gitignore", "bundle/vendor/\n");
cl_must_pass(p_mkdir("stash/bundle", 0777));
cl_must_pass(p_mkdir("stash/bundle/vendor", 0777));
cl_git_mkfile("stash/bundle/vendor/blah", "contents\n");
cl_assert(git_fs_path_exists("stash/when")); /* untracked */
cl_assert(git_fs_path_exists("stash/just.ignore")); /* ignored */
cl_assert(git_fs_path_exists("stash/bundle/vendor/blah")); /* ignored */
cl_git_pass(git_stash_save(
&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
cl_assert(!git_fs_path_exists("stash/when"));
cl_assert(git_fs_path_exists("stash/bundle/vendor/blah"));
cl_assert(git_fs_path_exists("stash/just.ignore"));
}
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");
cl_assert(!git_fs_path_exists("stash/just.ignore"));
}
/*
* Note: this test was flaky prior to fixing #4101 -- run it several
* times to get a failure. The issues is that whether the fast
* (stat-only) codepath is used inside stash's diff operation depends
* on whether files are "racily clean", and there doesn't seem to be
* an easy way to force the exact required state.
*/
void test_stash_save__untracked_regression(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const char *paths[] = {"what", "where", "how", "why"};
git_reference *head;
git_commit *head_commit;
git_str untracked_dir;
const char* workdir = git_repository_workdir(repo);
git_str_init(&untracked_dir, 0);
git_str_printf(&untracked_dir, "%sz", workdir);
cl_assert(!p_mkdir(untracked_dir.ptr, 0777));
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_peel((git_object **)&head_commit, head, GIT_OBJECT_COMMIT));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
opts.paths.strings = (char **)paths;
opts.paths.count = 4;
cl_git_pass(git_checkout_tree(repo, (git_object*)head_commit, &opts));
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
assert_commit_message_contains("refs/stash", "WIP on master");
git_reference_free(head);
git_commit_free(head_commit);
git_str_dispose(&untracked_dir);
}
#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, NULL));
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)
{
assert_object_oid("refs/stash@{0}", NULL, GIT_OBJECT_COMMIT);
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
assert_object_oid("refs/stash@{0}", git_oid_tostr_s(&stash_tip_oid), GIT_OBJECT_COMMIT);
assert_object_oid("refs/stash@{1}", NULL, GIT_OBJECT_COMMIT);
}
void test_stash_save__multiline_message(void)
{
const char *msg = "This\n\nis a multiline message\n";
const git_reflog_entry *entry;
git_reflog *reflog;
assert_object_oid("refs/stash@{0}", NULL, GIT_OBJECT_COMMIT);
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, msg, GIT_STASH_DEFAULT));
cl_git_pass(git_reflog_read(&reflog, repo, "refs/stash"));
cl_assert(entry = git_reflog_entry_byindex(reflog, 0));
cl_assert_equal_s(git_reflog_entry_message(entry), "On master: This is a multiline message");
assert_object_oid("refs/stash@{0}", git_oid_tostr_s(&stash_tip_oid), GIT_OBJECT_COMMIT);
assert_commit_message_contains("refs/stash@{0}", msg);
git_reflog_free(reflog);
}
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', 'where' and 'who' are being committed.
* 'when' remains untracked.
*/
cl_git_pass(git_index_add_bypath(index, "what"));
cl_git_pass(git_index_add_bypath(index, "where"));
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_OBJECT_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_must_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_OBJECT_TREE);
}
void test_stash_save__ignored_directory(void)
{
cl_git_pass(p_mkdir("stash/ignored_directory", 0777));
cl_git_pass(p_mkdir("stash/ignored_directory/sub", 0777));
cl_git_mkfile("stash/ignored_directory/sub/some_file", "stuff");
assert_status(repo, "ignored_directory/sub/some_file", GIT_STATUS_WT_NEW);
cl_git_pass(git_ignore_add_rule(repo, "ignored_directory/"));
assert_status(repo, "ignored_directory/sub/some_file", GIT_STATUS_IGNORED);
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED));
cl_assert(!git_fs_path_exists("stash/ignored_directory/sub/some_file"));
cl_assert(!git_fs_path_exists("stash/ignored_directory/sub"));
cl_assert(!git_fs_path_exists("stash/ignored_directory"));
}
void test_stash_save__skip_submodules(void)
{
git_repository *untracked_repo;
cl_git_pass(git_repository_init(&untracked_repo, "stash/untracked_repo", false));
cl_git_mkfile("stash/untracked_repo/content", "stuff");
git_repository_free(untracked_repo);
assert_status(repo, "untracked_repo/", GIT_STATUS_WT_NEW);
cl_git_pass(git_stash_save(
&stash_tip_oid, repo, signature, NULL, GIT_STASH_INCLUDE_UNTRACKED));
assert_status(repo, "untracked_repo/", GIT_STATUS_WT_NEW);
}
void test_stash_save__deleted_in_index_modified_in_workdir(void)
{
git_index *index;
git_repository_index(&index, repo);
cl_git_pass(git_index_remove_bypath(index, "who"));
cl_git_pass(git_index_write(index));
assert_status(repo, "who", GIT_STATUS_WT_NEW | GIT_STATUS_INDEX_DELETED);
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));
assert_blob_oid("stash@{0}^0:who", "a0400d4954659306a976567af43125a0b1aa8595");
assert_blob_oid("stash@{0}^2:who", NULL);
git_index_free(index);
}
| libgit2-main | tests/libgit2/stash/save.c |
#include "clar_libgit2.h"
#define VALID_COMMIT "tree bdd24e358576f1baa275df98cdcaf3ac9a3f4233\n" \
"parent d6d956f1d66210bfcd0484166befab33b5987a39\n" \
"author Edward Thomson <[email protected]> 1638286404 -0500\n" \
"committer Edward Thomson <[email protected]> 1638324642 -0500\n" \
"\n" \
"commit go here.\n"
#define VALID_TREE "100644 HEADER\0\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42"
#define INVALID_COMMIT "tree bdd24e358576f1baa275df98cdcaf3ac9a3f4233\n" \
"parent d6d956f1d66210bfcd0484166befab33b5987a39\n" \
"committer Edward Thomson <[email protected]> 1638324642 -0500\n" \
"\n" \
"commit go here.\n"
#define INVALID_TREE "100644 HEADER \x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42"
void test_object_validate__valid(void)
{
int valid;
cl_git_pass(git_object_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_BLOB));
cl_assert_equal_i(1, valid);
cl_git_pass(git_object_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_BLOB));
cl_assert_equal_i(1, valid);
cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_COMMIT, CONST_STRLEN(VALID_COMMIT), GIT_OBJECT_COMMIT));
cl_assert_equal_i(1, valid);
cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_TREE, CONST_STRLEN(VALID_TREE), GIT_OBJECT_TREE));
cl_assert_equal_i(1, valid);
}
void test_object_validate__invalid(void)
{
int valid;
cl_git_pass(git_object_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_COMMIT));
cl_assert_equal_i(0, valid);
cl_git_pass(git_object_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_COMMIT));
cl_assert_equal_i(0, valid);
cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_COMMIT, CONST_STRLEN(INVALID_COMMIT), GIT_OBJECT_COMMIT));
cl_assert_equal_i(0, valid);
cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_TREE, CONST_STRLEN(INVALID_TREE), GIT_OBJECT_TREE));
cl_assert_equal_i(0, valid);
}
| libgit2-main | tests/libgit2/object/validate.c |
#include "clar_libgit2.h"
#include "repository.h"
static git_repository *g_repo;
void test_object_lookup__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
}
void test_object_lookup__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
{
const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
git_oid oid;
git_object *object;
cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
cl_assert_equal_i(
GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
}
void test_object_lookup__lookup_nonexisting_returns_enotfound(void)
{
const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
git_oid oid;
git_object *object;
cl_git_pass(git_oid__fromstr(&oid, unknown, GIT_OID_SHA1));
cl_assert_equal_i(
GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_ANY));
}
void test_object_lookup__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void)
{
const char *commit = "e90810b";
git_oid oid;
git_object *object;
cl_git_pass(git_oid__fromstrn(&oid, commit, strlen(commit), GIT_OID_SHA1));
cl_assert_equal_i(
GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJECT_TAG));
}
void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
{
const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
git_oid oid;
git_object *object;
cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
git_object_free(object);
cl_assert_equal_i(
GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
}
void test_object_lookup__lookup_corrupt_object_returns_error(void)
{
const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
*file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
git_str path = GIT_STR_INIT, contents = GIT_STR_INIT;
git_oid oid;
git_object *object;
size_t i;
cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), file));
cl_git_pass(git_futils_readbuffer(&contents, path.ptr));
/* Corrupt and try to read the object */
for (i = 0; i < contents.size; i++) {
contents.ptr[i] ^= 0x1;
cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
contents.ptr[i] ^= 0x1;
}
/* Restore original content and assert we can read the object */
cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
git_object_free(object);
git_str_dispose(&path);
git_str_dispose(&contents);
}
void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
{
const char *oldloose = "objects/8e/73b769e97678d684b809b163bebdae2911720f",
*newloose = "objects/8e/73b769e97678d684b809b163bebdae2911720e",
*commit = "8e73b769e97678d684b809b163bebdae2911720e";
git_str oldpath = GIT_STR_INIT, newpath = GIT_STR_INIT;
git_object *object;
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
/* Copy object to another location with wrong hash */
cl_git_pass(git_str_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
cl_git_pass(git_str_joinpath(&newpath, git_repository_path(g_repo), newloose));
cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644));
/* Verify that lookup fails due to a hashsum mismatch */
cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
/* Disable verification and try again */
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
git_object_free(object);
git_str_dispose(&oldpath);
git_str_dispose(&newpath);
}
| libgit2-main | tests/libgit2/object/lookup.c |
#include "clar_libgit2.h"
static git_repository *g_repo;
void test_object_peel__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
}
void test_object_peel__cleanup(void)
{
git_repository_free(g_repo);
g_repo = NULL;
}
static void assert_peel(
const char *sha,
git_object_t requested_type,
const char* expected_sha,
git_object_t expected_type)
{
git_oid oid, expected_oid;
git_object *obj;
git_object *peeled;
cl_git_pass(git_oid__fromstr(&oid, sha, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_object_peel(&peeled, obj, requested_type));
cl_git_pass(git_oid__fromstr(&expected_oid, expected_sha, GIT_OID_SHA1));
cl_assert_equal_oid(&expected_oid, git_object_id(peeled));
cl_assert_equal_i(expected_type, git_object_type(peeled));
git_object_free(peeled);
git_object_free(obj);
}
static void assert_peel_error(int error, const char *sha, git_object_t requested_type)
{
git_oid oid;
git_object *obj;
git_object *peeled;
cl_git_pass(git_oid__fromstr(&oid, sha, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_assert_equal_i(error, git_object_peel(&peeled, obj, requested_type));
git_object_free(obj);
}
void test_object_peel__peeling_an_object_into_its_own_type_returns_another_instance_of_it(void)
{
assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_TAG,
"7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_TAG);
assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB,
"0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB);
}
void test_object_peel__tag(void)
{
assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_COMMIT,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_TREE,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
assert_peel_error(GIT_EPEEL, "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_BLOB);
assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_ANY,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
}
void test_object_peel__commit(void)
{
assert_peel_error(GIT_EINVALIDSPEC, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_BLOB);
assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_TREE,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
assert_peel_error(GIT_EINVALIDSPEC, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_TAG);
assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_ANY,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
}
void test_object_peel__tree(void)
{
assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_BLOB);
assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_COMMIT);
assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TAG);
assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_ANY);
}
void test_object_peel__blob(void)
{
assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB,
"0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB);
assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_TREE);
assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_COMMIT);
assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_TAG);
assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_ANY);
}
void test_object_peel__target_any_object_for_type_change(void)
{
/* tag to commit */
assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_ANY,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
/* commit to tree */
assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_ANY,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
}
| libgit2-main | tests/libgit2/object/peel.c |
#include "clar_libgit2.h"
#include "repository.h"
static git_repository *g_repo;
static git_tree *g_root_tree;
static git_commit *g_head_commit;
static git_object *g_expectedobject,
*g_actualobject;
void test_object_lookupbypath__initialize(void)
{
git_reference *head;
git_tree_entry *tree_entry;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("attr/.gitted")));
cl_git_pass(git_repository_head(&head, g_repo));
cl_git_pass(git_reference_peel((git_object**)&g_head_commit, head, GIT_OBJECT_COMMIT));
cl_git_pass(git_commit_tree(&g_root_tree, g_head_commit));
cl_git_pass(git_tree_entry_bypath(&tree_entry, g_root_tree, "subdir/subdir_test2.txt"));
cl_git_pass(git_object_lookup(&g_expectedobject, g_repo, git_tree_entry_id(tree_entry),
GIT_OBJECT_ANY));
git_tree_entry_free(tree_entry);
git_reference_free(head);
g_actualobject = NULL;
}
void test_object_lookupbypath__cleanup(void)
{
git_object_free(g_actualobject);
git_object_free(g_expectedobject);
git_tree_free(g_root_tree);
git_commit_free(g_head_commit);
g_expectedobject = NULL;
git_repository_free(g_repo);
g_repo = NULL;
}
void test_object_lookupbypath__errors(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
"subdir/subdir_test2.txt", GIT_OBJECT_TREE)); /* It's not a tree */
cl_assert_equal_i(GIT_ENOTFOUND,
git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
"file/doesnt/exist", GIT_OBJECT_ANY));
}
void test_object_lookupbypath__from_root_tree(void)
{
cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree,
"subdir/subdir_test2.txt", GIT_OBJECT_BLOB));
cl_assert_equal_oid(git_object_id(g_expectedobject),
git_object_id(g_actualobject));
}
void test_object_lookupbypath__from_head_commit(void)
{
cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_head_commit,
"subdir/subdir_test2.txt", GIT_OBJECT_BLOB));
cl_assert_equal_oid(git_object_id(g_expectedobject),
git_object_id(g_actualobject));
}
void test_object_lookupbypath__from_subdir_tree(void)
{
git_tree_entry *entry = NULL;
git_tree *tree = NULL;
cl_git_pass(git_tree_entry_bypath(&entry, g_root_tree, "subdir"));
cl_git_pass(git_tree_lookup(&tree, g_repo, git_tree_entry_id(entry)));
cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)tree,
"subdir_test2.txt", GIT_OBJECT_BLOB));
cl_assert_equal_oid(git_object_id(g_expectedobject),
git_object_id(g_actualobject));
git_tree_entry_free(entry);
git_tree_free(tree);
}
| libgit2-main | tests/libgit2/object/lookupbypath.c |
#include "clar_libgit2.h"
#include "repository.h"
static git_repository *g_repo;
static size_t cache_limit;
static int object_type;
void test_object_cache__initialize_cache_no_blobs(void)
{
g_repo = NULL;
object_type = GIT_OBJECT_BLOB;
cache_limit = 0;
}
void test_object_cache__initialize_cache_tiny_blobs(void)
{
g_repo = NULL;
object_type = GIT_OBJECT_BLOB;
cache_limit = 10;
}
void test_object_cache__initialize_cache_all_blobs(void)
{
g_repo = NULL;
object_type = GIT_OBJECT_BLOB;
cache_limit = 32767;
}
void test_object_cache__initialize_cache_no_trees(void)
{
g_repo = NULL;
object_type = GIT_OBJECT_TREE;
cache_limit = 0;
}
void test_object_cache__cleanup(void)
{
git_repository_free(g_repo);
g_repo = NULL;
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)0);
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_TREE, (size_t)4096);
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_COMMIT, (size_t)4096);
}
static struct {
git_object_t type;
const char *sha;
size_t size;
} g_data[] = {
/* HEAD */
{ GIT_OBJECT_BLOB, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", 10 }, /* README */
{ GIT_OBJECT_BLOB, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc", 8 }, /* branch_file.txt */
{ GIT_OBJECT_BLOB, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", 12 }, /* new.txt */
/* refs/heads/subtrees */
{ GIT_OBJECT_BLOB, "1385f264afb75a56a5bec74243be9b367ba4ca08", 4 }, /* README */
{ GIT_OBJECT_TREE, "f1425cef211cc08caa31e7b545ffb232acb098c3", 90 }, /* ab */
{ GIT_OBJECT_BLOB, "d6c93164c249c8000205dd4ec5cbca1b516d487f", 6 }, /* ab/4.txt */
{ GIT_OBJECT_TREE, "9a03079b8a8ee85a0bee58bf9be3da8b62414ed4", 33 }, /* ab/c */
{ GIT_OBJECT_BLOB, "270b8ea76056d5cad83af921837702d3e3c2924d", 6 }, /* ab/c/3.txt */
{ GIT_OBJECT_TREE, "b6361fc6a97178d8fc8639fdeed71c775ab52593", 63 }, /* ab/de */
{ GIT_OBJECT_BLOB, "e7b4ad382349ff96dd8199000580b9b1e2042eb0", 6 }, /* ab/de/2.txt */
{ GIT_OBJECT_TREE, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54", 33 }, /* ab/de/fgh */
{ GIT_OBJECT_BLOB, "1f67fc4386b2d171e0d21be1c447e12660561f9b", 6 }, /* ab/de/fgh/1.txt */
{ GIT_OBJECT_BLOB, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", 3 }, /* branch_file.txt */
{ GIT_OBJECT_BLOB, "fa49b077972391ad58037050f2a75f74e3671e92", 9 }, /* new.txt */
/* refs/heads/chomped */
{ GIT_OBJECT_BLOB, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", 51 }, /* readme.txt */
{ 0, NULL, 0 },
{ 0, NULL, 0 }
};
void test_object_cache__cache_counts(void)
{
int i, start, nonmatching = 0;
git_oid oid;
git_odb_object *odb_obj;
git_object *obj;
git_odb *odb;
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, cache_limit);
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_odb(&odb, g_repo));
start = (int)git_cache_size(&g_repo->objects);
for (i = 0; g_data[i].sha != NULL; ++i) {
int count = (int)git_cache_size(&g_repo->objects);
cl_git_pass(git_oid__fromstr(&oid, g_data[i].sha, GIT_OID_SHA1));
/* alternate between loading raw and parsed objects */
if ((i & 1) == 0) {
cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
git_odb_object_free(odb_obj);
} else {
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_assert(g_data[i].type == git_object_type(obj));
git_object_free(obj);
}
if ((g_data[i].type == object_type && g_data[i].size >= cache_limit) ||
(g_data[i].type != object_type && g_data[i].type == GIT_OBJECT_BLOB))
cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
else {
cl_assert_equal_i(count + 1, (int)git_cache_size(&g_repo->objects));
nonmatching++;
}
}
cl_assert_equal_i(nonmatching, (int)git_cache_size(&g_repo->objects) - start);
for (i = 0; g_data[i].sha != NULL; ++i) {
int count = (int)git_cache_size(&g_repo->objects);
cl_git_pass(git_oid__fromstr(&oid, g_data[i].sha, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_assert(g_data[i].type == git_object_type(obj));
git_object_free(obj);
cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
}
git_odb_free(odb);
}
static void *cache_parsed(void *arg)
{
int i;
git_oid oid;
git_object *obj;
for (i = ((int *)arg)[1]; g_data[i].sha != NULL; i += 2) {
cl_git_pass(git_oid__fromstr(&oid, g_data[i].sha, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_assert(g_data[i].type == git_object_type(obj));
git_object_free(obj);
}
for (i = 0; i < ((int *)arg)[1]; i += 2) {
cl_git_pass(git_oid__fromstr(&oid, g_data[i].sha, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_assert(g_data[i].type == git_object_type(obj));
git_object_free(obj);
}
return arg;
}
static void *cache_raw(void *arg)
{
int i;
git_oid oid;
git_odb *odb;
git_odb_object *odb_obj;
cl_git_pass(git_repository_odb(&odb, g_repo));
for (i = ((int *)arg)[1]; g_data[i].sha != NULL; i += 2) {
cl_git_pass(git_oid__fromstr(&oid, g_data[i].sha, GIT_OID_SHA1));
cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
git_odb_object_free(odb_obj);
}
for (i = 0; i < ((int *)arg)[1]; i += 2) {
cl_git_pass(git_oid__fromstr(&oid, g_data[i].sha, GIT_OID_SHA1));
cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
git_odb_object_free(odb_obj);
}
git_odb_free(odb);
return arg;
}
#define REPEAT 20
#define THREADCOUNT 50
void test_object_cache__threadmania(void)
{
int try, th, max_i;
void *data;
void *(*fn)(void *);
#ifdef GIT_THREADS
git_thread t[THREADCOUNT];
#endif
for (max_i = 0; g_data[max_i].sha != NULL; ++max_i)
/* count up */;
for (try = 0; try < REPEAT; ++try) {
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
for (th = 0; th < THREADCOUNT; ++th) {
data = git__malloc(2 * sizeof(int));
((int *)data)[0] = th;
((int *)data)[1] = th % max_i;
fn = (th & 1) ? cache_parsed : cache_raw;
#ifdef GIT_THREADS
cl_git_pass(git_thread_create(&t[th], fn, data));
#else
cl_assert(fn(data) == data);
git__free(data);
#endif
}
#ifdef GIT_THREADS
for (th = 0; th < THREADCOUNT; ++th) {
cl_git_pass(git_thread_join(&t[th], &data));
cl_assert_equal_i(th, ((int *)data)[0]);
git__free(data);
}
#endif
git_repository_free(g_repo);
g_repo = NULL;
}
}
static void *cache_quick(void *arg)
{
git_oid oid;
git_object *obj;
cl_git_pass(git_oid__fromstr(&oid, g_data[4].sha, GIT_OID_SHA1));
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
cl_assert(g_data[4].type == git_object_type(obj));
git_object_free(obj);
return arg;
}
void test_object_cache__fast_thread_rush(void)
{
int try, th, data[THREADCOUNT];
#ifdef GIT_THREADS
git_thread t[THREADCOUNT];
#endif
for (try = 0; try < REPEAT; ++try) {
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
for (th = 0; th < THREADCOUNT; ++th) {
data[th] = th;
#ifdef GIT_THREADS
cl_git_pass(
git_thread_create(&t[th], cache_quick, &data[th]));
#else
cl_assert(cache_quick(&data[th]) == &data[th]);
#endif
}
#ifdef GIT_THREADS
for (th = 0; th < THREADCOUNT; ++th) {
void *rval;
cl_git_pass(git_thread_join(&t[th], &rval));
cl_assert_equal_i(th, *((int *)rval));
}
#endif
git_repository_free(g_repo);
g_repo = NULL;
}
}
| libgit2-main | tests/libgit2/object/cache.c |
#include "clar_libgit2.h"
git_repository *_repo;
void test_object_shortid__initialize(void)
{
cl_git_pass(git_repository_open(&_repo, cl_fixture("duplicate.git")));
}
void test_object_shortid__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
}
void test_object_shortid__select(void)
{
git_oid full;
git_object *obj;
git_buf shorty = {0};
git_oid__fromstr(&full, "ce013625030ba8dba906f756967f9e9ca394464a", GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&obj, _repo, &full, GIT_OBJECT_ANY));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(7, shorty.size);
cl_assert_equal_s("ce01362", shorty.ptr);
git_object_free(obj);
git_oid__fromstr(&full, "038d718da6a1ebbc6a7780a96ed75a70cc2ad6e2", GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&obj, _repo, &full, GIT_OBJECT_ANY));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(7, shorty.size);
cl_assert_equal_s("038d718", shorty.ptr);
git_object_free(obj);
git_oid__fromstr(&full, "dea509d097ce692e167dfc6a48a7a280cc5e877e", GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&obj, _repo, &full, GIT_OBJECT_ANY));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(9, shorty.size);
cl_assert_equal_s("dea509d09", shorty.ptr);
git_object_free(obj);
git_oid__fromstr(&full, "dea509d0b3cb8ee0650f6ca210bc83f4678851ba", GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&obj, _repo, &full, GIT_OBJECT_ANY));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(9, shorty.size);
cl_assert_equal_s("dea509d0b", shorty.ptr);
git_object_free(obj);
git_buf_dispose(&shorty);
}
| libgit2-main | tests/libgit2/object/shortid.c |
#include "clar_libgit2.h"
static void assert_message_prettifying(char *expected_output, char *input, int strip_comments)
{
git_buf prettified_message = GIT_BUF_INIT;
git_message_prettify(&prettified_message, input, strip_comments, '#');
cl_assert_equal_s(expected_output, prettified_message.ptr);
git_buf_dispose(&prettified_message);
}
#define t40 "A quick brown fox jumps over the lazy do"
#define s40 " "
#define sss s40 s40 s40 s40 s40 s40 s40 s40 s40 s40 /* # 400 */
#define ttt t40 t40 t40 t40 t40 t40 t40 t40 t40 t40 /* # 400 */
/* Ported from git.git */
/* see https://github.com/git/git/blob/master/t/t0030-stripspace.sh */
void test_object_message__long_lines_without_spaces_should_be_unchanged(void)
{
assert_message_prettifying(ttt "\n", ttt, 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt, 0);
assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0);
assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0);
}
void test_object_message__lines_with_spaces_at_the_beginning_should_be_unchanged(void)
{
assert_message_prettifying(sss ttt "\n", sss ttt, 0);
assert_message_prettifying(sss sss ttt "\n", sss sss ttt, 0);
assert_message_prettifying(sss sss sss ttt "\n", sss sss sss ttt, 0);
}
void test_object_message__lines_with_intermediate_spaces_should_be_unchanged(void)
{
assert_message_prettifying(ttt sss ttt "\n", ttt sss ttt, 0);
assert_message_prettifying(ttt sss sss ttt "\n", ttt sss sss ttt, 0);
}
void test_object_message__consecutive_blank_lines_should_be_unified(void)
{
assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\n\n\n\n" ttt "\n", 0);
assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\n\n\n\n" ttt "\n", 0);
assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\n\n\n\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt "\n", 0);
assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\n\n\n\n" ttt ttt ttt "\n", 0);
assert_message_prettifying(ttt "\n\n" ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0);
assert_message_prettifying(ttt ttt "\n\n" ttt "\n", ttt ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0);
assert_message_prettifying(ttt ttt ttt "\n\n" ttt "\n", ttt ttt ttt "\n\t\n \n\n \t\t\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n\n" ttt ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt ttt "\n", 0);
assert_message_prettifying(ttt "\n\n" ttt ttt ttt "\n", ttt "\n\t\n \n\n \t\t\n" ttt ttt ttt "\n", 0);
}
void test_object_message__only_consecutive_blank_lines_should_be_completely_removed(void)
{
assert_message_prettifying("", "\n", 0);
assert_message_prettifying("", "\n\n\n", 0);
assert_message_prettifying("", sss "\n" sss "\n" sss "\n", 0);
assert_message_prettifying("", sss sss "\n" sss "\n\n", 0);
}
void test_object_message__consecutive_blank_lines_at_the_beginning_should_be_removed(void)
{
assert_message_prettifying(ttt "\n", "\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n", "\n\n\n" ttt "\n", 0);
assert_message_prettifying(ttt ttt "\n", "\n\n\n" ttt ttt "\n", 0);
assert_message_prettifying(ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt "\n", 0);
assert_message_prettifying(ttt ttt ttt ttt "\n", "\n\n\n" ttt ttt ttt ttt "\n", 0);
assert_message_prettifying(ttt "\n", sss "\n" sss "\n" sss "\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n", "\n" sss "\n" sss sss "\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n", sss sss "\n" sss "\n\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n", sss sss sss "\n\n\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n", "\n" sss sss sss "\n\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n", "\n\n" sss sss sss "\n" ttt "\n", 0);
}
void test_object_message__consecutive_blank_lines_at_the_end_should_be_removed(void)
{
assert_message_prettifying(ttt "\n", ttt "\n\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n\n\n\n", 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt "\n\n\n\n", 0);
assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt "\n\n\n\n", 0);
assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt "\n\n\n\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n" sss "\n" sss "\n" sss "\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n\n" sss "\n" sss sss "\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n" sss sss "\n" sss "\n\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n" sss sss sss "\n\n\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n\n" sss sss sss "\n\n", 0);
assert_message_prettifying(ttt "\n", ttt "\n\n\n" sss sss sss "\n\n", 0);
}
void test_object_message__text_without_newline_at_end_should_end_with_newline(void)
{
assert_message_prettifying(ttt "\n", ttt, 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt, 0);
assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt, 0);
assert_message_prettifying(ttt ttt ttt ttt "\n", ttt ttt ttt ttt, 0);
}
void test_object_message__text_plus_spaces_without_newline_should_not_show_spaces_and_end_with_newline(void)
{
assert_message_prettifying(ttt "\n", ttt sss, 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt sss, 0);
assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss, 0);
assert_message_prettifying(ttt "\n", ttt sss sss, 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss, 0);
assert_message_prettifying(ttt "\n", ttt sss sss sss, 0);
}
void test_object_message__text_plus_spaces_ending_with_newline_should_be_cleaned_and_newline_must_remain(void){
assert_message_prettifying(ttt "\n", ttt sss "\n", 0);
assert_message_prettifying(ttt "\n", ttt sss sss "\n", 0);
assert_message_prettifying(ttt "\n", ttt sss sss sss "\n", 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt sss "\n", 0);
assert_message_prettifying(ttt ttt "\n", ttt ttt sss sss "\n", 0);
assert_message_prettifying(ttt ttt ttt "\n", ttt ttt ttt sss "\n", 0);
}
void test_object_message__spaces_with_newline_at_end_should_be_replaced_with_empty_string(void)
{
assert_message_prettifying("", sss "\n", 0);
assert_message_prettifying("", sss sss "\n", 0);
assert_message_prettifying("", sss sss sss "\n", 0);
assert_message_prettifying("", sss sss sss sss "\n", 0);
}
void test_object_message__spaces_without_newline_at_end_should_be_replaced_with_empty_string(void)
{
assert_message_prettifying("", "", 0);
assert_message_prettifying("", sss sss, 0);
assert_message_prettifying("", sss sss sss, 0);
assert_message_prettifying("", sss sss sss sss, 0);
}
void test_object_message__consecutive_text_lines_should_be_unchanged(void)
{
assert_message_prettifying(ttt ttt "\n" ttt "\n", ttt ttt "\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt ttt "\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", ttt "\n" ttt "\n" ttt "\n" ttt ttt "\n", 0);
assert_message_prettifying(ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", ttt "\n" ttt "\n\n" ttt ttt "\n" ttt "\n", 0);
assert_message_prettifying(ttt ttt "\n\n" ttt "\n" ttt ttt "\n", ttt ttt "\n\n" ttt "\n" ttt ttt "\n", 0);
assert_message_prettifying(ttt "\n" ttt ttt "\n\n" ttt "\n", ttt "\n" ttt ttt "\n\n" ttt "\n", 0);
}
void test_object_message__strip_comments(void)
{
assert_message_prettifying("", "# comment", 1);
assert_message_prettifying("", "# comment\n", 1);
assert_message_prettifying("", "# comment \n", 1);
assert_message_prettifying(ttt "\n", ttt "\n" "# comment\n", 1);
assert_message_prettifying(ttt "\n", "# comment\n" ttt "\n", 1);
assert_message_prettifying(ttt "\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 1);
}
void test_object_message__keep_comments(void)
{
assert_message_prettifying("# comment\n", "# comment", 0);
assert_message_prettifying("# comment\n", "# comment\n", 0);
assert_message_prettifying("# comment\n", "# comment \n", 0);
assert_message_prettifying(ttt "\n" "# comment\n", ttt "\n" "# comment\n", 0);
assert_message_prettifying("# comment\n" ttt "\n", "# comment\n" ttt "\n", 0);
assert_message_prettifying(ttt "\n" "# comment\n" ttt "\n", ttt "\n" "# comment\n" ttt "\n", 0);
}
void test_object_message__message_prettify(void)
{
git_buf buffer;
memset(&buffer, 0, sizeof(buffer));
cl_git_pass(git_message_prettify(&buffer, "", 0, '#'));
cl_assert_equal_s(buffer.ptr, "");
git_buf_dispose(&buffer);
cl_git_pass(git_message_prettify(&buffer, "", 1, '#'));
cl_assert_equal_s(buffer.ptr, "");
git_buf_dispose(&buffer);
cl_git_pass(git_message_prettify(&buffer, "Short", 0, '#'));
cl_assert_equal_s("Short\n", buffer.ptr);
git_buf_dispose(&buffer);
cl_git_pass(git_message_prettify(&buffer, "Short", 1, '#'));
cl_assert_equal_s("Short\n", buffer.ptr);
git_buf_dispose(&buffer);
cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 0, '#'));
cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n# with some comments still in\n");
git_buf_dispose(&buffer);
cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 1, '#'));
cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n");
git_buf_dispose(&buffer);
}
| libgit2-main | tests/libgit2/object/message.c |
#include "clar_libgit2.h"
#include "posix.h"
static git_repository *repo;
void test_object_commit_commitstagedfile__initialize(void)
{
cl_fixture("treebuilder");
cl_git_pass(git_repository_init(&repo, "treebuilder/", 0));
cl_assert(repo != NULL);
}
void test_object_commit_commitstagedfile__cleanup(void)
{
git_repository_free(repo);
repo = NULL;
cl_fixture_cleanup("treebuilder");
}
void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
{
git_index *index;
const git_index_entry *entry;
git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid;
git_signature *signature;
git_tree *tree;
git_buf buffer = GIT_BUF_INIT;
/*
* The test below replicates the following git scenario
*
* $ echo "test" > test.txt
* $ git hash-object test.txt
* 9daeafb9864cf43055ae93beb0afd6c7d144bfa4
*
* $ git add .
* $ git commit -m "Initial commit"
*
* $ git log
* commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
* Author: nulltoken <[email protected]>
* Date: Wed Dec 14 08:29:03 2011 +0100
*
* Initial commit
*
* $ git show 1fe3 --format=raw
* commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
* tree 2b297e643c551e76cfa1f93810c50811382f9117
* author nulltoken <[email protected]> 1323847743 +0100
* committer nulltoken <[email protected]> 1323847743 +0100
*
* Initial commit
*
* diff --git a/test.txt b/test.txt
* new file mode 100644
* index 0000000..9daeafb
* --- /dev/null
* +++ b/test.txt
* @@ -0,0 +1 @@
* +test
*
* $ git ls-tree 2b297
* 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt
*/
cl_git_pass(git_oid__fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117", GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4", GIT_OID_SHA1));
/*
* Add a new file to the index
*/
cl_git_mkfile("treebuilder/test.txt", "test\n");
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "test.txt"));
entry = git_index_get_byindex(index, 0);
cl_assert(git_oid_cmp(&expected_blob_oid, &entry->id) == 0);
/*
* Information about index entry should match test file
*/
{
struct stat st;
cl_must_pass(p_lstat("treebuilder/test.txt", &st));
cl_assert(entry->file_size == st.st_size);
#ifndef _WIN32
/*
* Windows doesn't populate these fields, and the signage is
* wrong in the Windows version of the struct, so lets avoid
* the "comparing signed and unsigned" compilation warning in
* that case.
*/
cl_assert(entry->uid == st.st_uid);
cl_assert(entry->gid == st.st_gid);
#endif
}
/*
* Build the tree from the index
*/
cl_git_pass(git_index_write_tree(&tree_oid, index));
cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0);
/*
* Commit the staged file
*/
cl_git_pass(git_signature_new(&signature, "nulltoken", "[email protected]", 1323847743, 60));
cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
cl_git_pass(git_message_prettify(&buffer, "Initial commit", 0, '#'));
cl_git_pass(git_commit_create_v(
&commit_oid,
repo,
"HEAD",
signature,
signature,
NULL,
buffer.ptr,
tree,
0));
cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0);
git_buf_dispose(&buffer);
git_signature_free(signature);
git_tree_free(tree);
git_index_free(index);
}
static void assert_commit_tree_has_n_entries(git_commit *c, int count)
{
git_tree *tree;
cl_git_pass(git_commit_tree(&tree, c));
cl_assert_equal_i(count, git_tree_entrycount(tree));
git_tree_free(tree);
}
static void assert_commit_is_head_(git_commit *c, const char *file, const char *func, int line)
{
git_commit *head;
cl_git_pass(git_revparse_single((git_object **)&head, repo, "HEAD"));
clar__assert(git_oid_equal(git_commit_id(c), git_commit_id(head)), file, func, line, "Commit is not the HEAD", NULL, 1);
git_commit_free(head);
}
#define assert_commit_is_head(C) assert_commit_is_head_((C),__FILE__,__func__,__LINE__)
void test_object_commit_commitstagedfile__amend_commit(void)
{
git_index *index;
git_oid old_oid, new_oid, tree_oid;
git_commit *old_commit, *new_commit;
git_tree *tree;
/* make a commit */
cl_git_mkfile("treebuilder/myfile", "This is a file\n");
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "myfile"));
cl_repo_commit_from_index(&old_oid, repo, NULL, 0, "first commit");
cl_git_pass(git_commit_lookup(&old_commit, repo, &old_oid));
cl_assert_equal_i(0, git_commit_parentcount(old_commit));
assert_commit_tree_has_n_entries(old_commit, 1);
assert_commit_is_head(old_commit);
/* let's amend the message of the HEAD commit */
cl_git_pass(git_commit_amend(
&new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL));
/* fail because the commit isn't the tip of the branch anymore */
cl_git_fail(git_commit_amend(
&new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL));
cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid));
cl_assert_equal_i(0, git_commit_parentcount(new_commit));
assert_commit_tree_has_n_entries(new_commit, 1);
assert_commit_is_head(new_commit);
git_commit_free(old_commit);
old_commit = new_commit;
/* let's amend the tree of that last commit */
cl_git_mkfile("treebuilder/anotherfile", "This is another file\n");
cl_git_pass(git_index_add_bypath(index, "anotherfile"));
cl_git_pass(git_index_write_tree(&tree_oid, index));
cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
cl_assert_equal_i(2, git_tree_entrycount(tree));
/* fail to amend on a ref which does not exist */
cl_git_fail_with(GIT_ENOTFOUND, git_commit_amend(
&new_oid, old_commit, "refs/heads/nope", NULL, NULL, NULL, "Initial commit", tree));
cl_git_pass(git_commit_amend(
&new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", tree));
git_tree_free(tree);
cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid));
cl_assert_equal_i(0, git_commit_parentcount(new_commit));
assert_commit_tree_has_n_entries(new_commit, 2);
assert_commit_is_head(new_commit);
/* cleanup */
git_commit_free(old_commit);
git_commit_free(new_commit);
git_index_free(index);
}
| libgit2-main | tests/libgit2/object/commit/commitstagedfile.c |
#include "clar_libgit2.h"
#include "commit.h"
#include "object.h"
#include "signature.h"
static void assert_commit_parses(const char *data, size_t datalen,
const char *expected_treeid,
const char *expected_author,
const char *expected_committer,
const char *expected_encoding,
const char *expected_message,
size_t expected_parents)
{
git_commit *commit;
if (!datalen)
datalen = strlen(data);
cl_git_pass(git_object__from_raw((git_object **) &commit, data, datalen, GIT_OBJECT_COMMIT));
if (expected_author) {
git_signature *author;
cl_git_pass(git_signature_from_buffer(&author, expected_author));
cl_assert(git_signature__equal(author, commit->author));
cl_assert_equal_s(author->name, commit->author->name);
cl_assert_equal_s(author->email, commit->author->email);
cl_assert_equal_i(author->when.time, commit->author->when.time);
cl_assert_equal_i(author->when.offset, commit->author->when.offset);
cl_assert_equal_i(author->when.sign, commit->author->when.sign);
git_signature_free(author);
}
if (expected_committer) {
git_signature *committer;
cl_git_pass(git_signature_from_buffer(&committer, expected_committer));
cl_assert_equal_s(committer->name, commit->committer->name);
cl_assert_equal_s(committer->email, commit->committer->email);
cl_assert_equal_i(committer->when.time, commit->committer->when.time);
cl_assert_equal_i(committer->when.offset, commit->committer->when.offset);
cl_assert_equal_i(committer->when.sign, commit->committer->when.sign);
git_signature_free(committer);
}
if (expected_encoding)
cl_assert_equal_s(commit->message_encoding, expected_encoding);
else
cl_assert_equal_p(commit->message_encoding, NULL);
if (expected_message)
cl_assert_equal_s(commit->raw_message, expected_message);
else
cl_assert_equal_p(commit->message_encoding, NULL);
if (expected_treeid) {
git_oid tree_oid;
cl_git_pass(git_oid__fromstr(&tree_oid, expected_treeid, GIT_OID_SHA1));
cl_assert_equal_oid(&tree_oid, &commit->tree_id);
}
cl_assert_equal_i(commit->parent_ids.size, expected_parents);
git_object__free(&commit->object);
}
static void assert_commit_fails(const char *data, size_t datalen)
{
git_object *object;
if (!datalen)
datalen = strlen(data);
cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_COMMIT));
}
void test_object_commit_parse__parsing_commit_succeeds(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"committer Committer <[email protected]>\n"
"encoding Encoding\n"
"\n"
"Message";
assert_commit_parses(commit, 0,
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"Author <[email protected]>",
"Committer <[email protected]>",
"Encoding",
"Message", 0);
}
void test_object_commit_parse__parsing_commit_without_encoding_succeeds(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"committer Committer <[email protected]>\n"
"\n"
"Message";
assert_commit_parses(commit, 0,
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"Author <[email protected]>",
"Committer <[email protected]>",
NULL,
"Message", 0);
}
void test_object_commit_parse__parsing_commit_with_multiple_authors_succeeds(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author1 <[email protected]>\n"
"author Author2 <[email protected]>\n"
"author Author3 <[email protected]>\n"
"author Author4 <[email protected]>\n"
"committer Committer <[email protected]>\n"
"\n"
"Message";
assert_commit_parses(commit, 0,
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"Author1 <[email protected]>",
"Committer <[email protected]>",
NULL,
"Message", 0);
}
void test_object_commit_parse__parsing_commit_with_multiple_committers_succeeds(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"committer Committer1 <[email protected]>\n"
"committer Committer2 <[email protected]>\n"
"committer Committer3 <[email protected]>\n"
"committer Committer4 <[email protected]>\n"
"\n"
"Message";
assert_commit_parses(commit, 0,
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"Author <[email protected]>",
"Committer1 <[email protected]>",
NULL,
"Message", 0);
}
void test_object_commit_parse__parsing_commit_without_message_succeeds(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"committer Committer <[email protected]>\n";
assert_commit_parses(commit, 0,
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"Author <[email protected]>",
"Committer <[email protected]>",
NULL,
"", 0);
}
void test_object_commit_parse__parsing_commit_with_unknown_fields_succeeds(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"committer Committer <[email protected]>\n"
"foo bar\n"
"more garbage\n"
"\n"
"Message";
assert_commit_parses(commit, 0,
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"Author <[email protected]>",
"Committer <[email protected]>",
NULL,
"Message", 0);
}
void test_object_commit_parse__parsing_commit_with_invalid_tree_fails(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1xxx5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"committer Committer <[email protected]>\n"
"\n"
"Message";
assert_commit_fails(commit, 0);
}
void test_object_commit_parse__parsing_commit_without_tree_fails(void)
{
const char *commit =
"author Author <[email protected]>\n"
"committer Committer <[email protected]>\n"
"\n"
"Message";
assert_commit_fails(commit, 0);
}
void test_object_commit_parse__parsing_commit_without_author_fails(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"committer Committer <[email protected]>\n"
"\n"
"Message";
assert_commit_fails(commit, 0);
}
void test_object_commit_parse__parsing_commit_without_committer_fails(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author Author <[email protected]>\n"
"\n"
"Message";
assert_commit_fails(commit, 0);
}
void test_object_commit_parse__parsing_encoding_will_not_cause_oob_read(void)
{
const char *commit =
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
"author <>\n"
"committer <>\n"
"encoding foo\n";
/*
* As we ignore unknown fields, the cut-off encoding field will be
* parsed just fine.
*/
assert_commit_parses(commit, strlen(commit) - strlen("ncoding foo\n"),
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
"<>",
"<>",
NULL,
"", 0);
}
| libgit2-main | tests/libgit2/object/commit/parse.c |
#include "clar_libgit2.h"
#include "tree.h"
static git_repository *_repo;
void test_object_tree_duplicateentries__initialize(void) {
_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_duplicateentries__cleanup(void) {
cl_git_sandbox_cleanup();
}
/*
* $ git show --format=raw refs/heads/dir
* commit 144344043ba4d4a405da03de3844aa829ae8be0e
* tree d52a8fe84ceedf260afe4f0287bbfca04a117e83
* parent cf80f8de9f1185bf3a05f993f6121880dd0cfbc9
* author Ben Straub <[email protected]> 1343755506 -0700
* committer Ben Straub <[email protected]> 1343755506 -0700
*
* Change a file mode
*
* diff --git a/a/b.txt b/a/b.txt
* old mode 100644
* new mode 100755
*
* $ git ls-tree d52a8fe84ceedf260afe4f0287bbfca04a117e83
* 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6 README
* 040000 tree 4e0883eeeeebc1fb1735161cea82f7cb5fab7e63 a
* 100644 blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057 branch_file.txt
* 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd new.txt
*/
static void tree_checker(
git_oid *tid,
const char *expected_sha,
git_filemode_t expected_filemode)
{
git_tree *tree;
const git_tree_entry *entry;
git_oid oid;
cl_git_pass(git_tree_lookup(&tree, _repo, tid));
cl_assert_equal_i(1, (int)git_tree_entrycount(tree));
entry = git_tree_entry_byindex(tree, 0);
cl_git_pass(git_oid__fromstr(&oid, expected_sha, GIT_OID_SHA1));
cl_assert_equal_i(0, git_oid_cmp(&oid, git_tree_entry_id(entry)));
cl_assert_equal_i(expected_filemode, git_tree_entry_filemode(entry));
git_tree_free(tree);
}
static void tree_creator(git_oid *out, void (*fn)(git_treebuilder *))
{
git_treebuilder *builder;
cl_git_pass(git_treebuilder_new(&builder, _repo, NULL));
fn(builder);
cl_git_pass(git_treebuilder_write(out, builder));
git_treebuilder_free(builder);
}
static void two_blobs(git_treebuilder *bld)
{
git_oid oid;
const git_tree_entry *entry;
cl_git_pass(git_oid__fromstr(&oid,
"a8233120f6ad708f843d861ce2b7228ec4e3dec6",
GIT_OID_SHA1)); /* blob oid (README) */
cl_git_pass(git_treebuilder_insert(
&entry, bld, "duplicate", &oid,
GIT_FILEMODE_BLOB));
cl_git_pass(git_oid__fromstr(&oid,
"a71586c1dfe8a71c6cbf6c129f404c5642ff31bd",
GIT_OID_SHA1)); /* blob oid (new.txt) */
cl_git_pass(git_treebuilder_insert(
&entry, bld, "duplicate", &oid,
GIT_FILEMODE_BLOB));
}
static void one_blob_and_one_tree(git_treebuilder *bld)
{
git_oid oid;
const git_tree_entry *entry;
cl_git_pass(git_oid__fromstr(&oid,
"a8233120f6ad708f843d861ce2b7228ec4e3dec6",
GIT_OID_SHA1)); /* blob oid (README) */
cl_git_pass(git_treebuilder_insert(
&entry, bld, "duplicate", &oid,
GIT_FILEMODE_BLOB));
cl_git_pass(git_oid__fromstr(&oid,
"4e0883eeeeebc1fb1735161cea82f7cb5fab7e63",
GIT_OID_SHA1)); /* tree oid (a) */
cl_git_pass(git_treebuilder_insert(
&entry, bld, "duplicate", &oid,
GIT_FILEMODE_TREE));
}
void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_through_the_treebuilder(void)
{
git_oid tid;
tree_creator(&tid, two_blobs);
tree_checker(&tid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", GIT_FILEMODE_BLOB);
tree_creator(&tid, one_blob_and_one_tree);
tree_checker(&tid, "4e0883eeeeebc1fb1735161cea82f7cb5fab7e63", GIT_FILEMODE_TREE);
}
static void add_fake_conflicts(git_index *index)
{
git_index_entry ancestor_entry, our_entry, their_entry;
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
ancestor_entry.path = "duplicate";
ancestor_entry.mode = GIT_FILEMODE_BLOB;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid__fromstr(&ancestor_entry.id, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", GIT_OID_SHA1);
our_entry.path = "duplicate";
our_entry.mode = GIT_FILEMODE_BLOB;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 2);
git_oid__fromstr(&our_entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", GIT_OID_SHA1);
their_entry.path = "duplicate";
their_entry.mode = GIT_FILEMODE_BLOB;
GIT_INDEX_ENTRY_STAGE_SET(&their_entry, 3);
git_oid__fromstr(&their_entry.id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(index, &ancestor_entry, &our_entry, &their_entry));
}
void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_building_a_tree_from_a_index_with_conflicts(void)
{
git_index *index;
git_oid tid;
cl_git_pass(git_repository_index(&index, _repo));
add_fake_conflicts(index);
cl_assert_equal_i(GIT_EUNMERGED, git_index_write_tree(&tid, index));
git_index_free(index);
}
| libgit2-main | tests/libgit2/object/tree/duplicateentries.c |
#include "clar_libgit2.h"
#include "tree.h"
static git_repository *g_repo;
void test_object_tree_update__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo2");
}
void test_object_tree_update__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tree_update__remove_blob(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path = "README";
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_REMOVE, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB /* ignored */, path},
};
cl_git_pass(git_oid__fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
cl_git_pass(git_index_remove(idx, path, 0));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__remove_blob_deeper(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path = "subdir/README";
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_REMOVE, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB /* ignored */, path},
};
cl_git_pass(git_oid__fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
cl_git_pass(git_index_remove(idx, path, 0));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__remove_all_entries(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path1 = "subdir/subdir2/README";
const char *path2 = "subdir/subdir2/new.txt";
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_REMOVE, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB /* ignored */, path1},
{ GIT_TREE_UPDATE_REMOVE, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB /* ignored */, path2},
};
cl_git_pass(git_oid__fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
cl_git_pass(git_index_remove(idx, path1, 0));
cl_git_pass(git_index_remove(idx, path2, 0));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 2, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__replace_blob(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
const char *path = "README";
git_index_entry entry = { {0} };
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, path},
};
cl_git_pass(git_oid__fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_index_read_tree(idx, base_tree));
entry.path = path;
cl_git_pass(git_oid__fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add(idx, &entry));
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operation via the tree updater */
cl_git_pass(git_oid__fromstr(&updates[0].id, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 1, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
git_tree_free(base_tree);
}
void test_object_tree_update__add_blobs(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
git_index_entry entry = { {0} };
int i;
const char *paths[] = {
"some/deep/path",
"some/other/path",
"a/path/elsewhere",
};
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, paths[0]},
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, paths[1]},
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, paths[2]},
};
cl_git_pass(git_oid__fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", GIT_OID_SHA1));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
for (i = 0; i < 3; i++) {
cl_git_pass(git_oid__fromstr(&updates[i].id, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
}
for (i = 0; i < 2; i++) {
int j;
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
base_tree = NULL;
if (i == 1) {
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
cl_git_pass(git_index_read_tree(idx, base_tree));
}
for (j = 0; j < 3; j++) {
entry.path = paths[j];
cl_git_pass(git_index_add(idx, &entry));
}
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operations via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 3, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
}
git_tree_free(base_tree);
}
void test_object_tree_update__add_blobs_unsorted(void)
{
git_oid tree_index_id, tree_updater_id, base_id;
git_tree *base_tree;
git_index *idx;
git_index_entry entry = { {0} };
int i;
const char *paths[] = {
"some/deep/path",
"a/path/elsewhere",
"some/other/path",
};
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, paths[0]},
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, paths[1]},
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, paths[2]},
};
cl_git_pass(git_oid__fromstr(&base_id, "c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", GIT_OID_SHA1));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&entry.id, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
for (i = 0; i < 3; i++) {
cl_git_pass(git_oid__fromstr(&updates[i].id, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
}
for (i = 0; i < 2; i++) {
int j;
/* Create it with an index */
cl_git_pass(git_index_new(&idx));
base_tree = NULL;
if (i == 1) {
cl_git_pass(git_tree_lookup(&base_tree, g_repo, &base_id));
cl_git_pass(git_index_read_tree(idx, base_tree));
}
for (j = 0; j < 3; j++) {
entry.path = paths[j];
cl_git_pass(git_index_add(idx, &entry));
}
cl_git_pass(git_index_write_tree_to(&tree_index_id, idx, g_repo));
git_index_free(idx);
/* Perform the same operations via the tree updater */
cl_git_pass(git_tree_create_updated(&tree_updater_id, g_repo, base_tree, 3, updates));
cl_assert_equal_oid(&tree_index_id, &tree_updater_id);
}
git_tree_free(base_tree);
}
void test_object_tree_update__add_conflict(void)
{
int i;
git_oid tree_updater_id;
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, "a/dir/blob"},
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, "a/dir"},
};
for (i = 0; i < 2; i++) {
cl_git_pass(git_oid__fromstr(&updates[i].id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", GIT_OID_SHA1));
}
cl_git_fail(git_tree_create_updated(&tree_updater_id, g_repo, NULL, 2, updates));
}
void test_object_tree_update__add_conflict2(void)
{
int i;
git_oid tree_updater_id;
git_tree_update updates[] = {
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, "a/dir/blob"},
{ GIT_TREE_UPDATE_UPSERT, GIT_OID_SHA1_ZERO, GIT_FILEMODE_TREE, "a/dir/blob"},
};
for (i = 0; i < 2; i++) {
cl_git_pass(git_oid__fromstr(&updates[i].id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", GIT_OID_SHA1));
}
cl_git_fail(git_tree_create_updated(&tree_updater_id, g_repo, NULL, 2, updates));
}
void test_object_tree_update__remove_invalid_submodule(void)
{
git_tree *baseline;
git_oid updated_tree_id, baseline_id;
git_tree_update updates[] = {
{GIT_TREE_UPDATE_REMOVE, GIT_OID_SHA1_ZERO, GIT_FILEMODE_BLOB, "submodule"},
};
/* This tree contains a submodule with an all-zero commit for a submodule named 'submodule' */
cl_git_pass(git_oid__fromstr(&baseline_id, "396c7f1adb7925f51ba13a75f48252f44c5a14a2", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&baseline, g_repo, &baseline_id));
cl_git_pass(git_tree_create_updated(&updated_tree_id, g_repo, baseline, 1, updates));
git_tree_free(baseline);
}
| libgit2-main | tests/libgit2/object/tree/update.c |
#include "clar_libgit2.h"
#include "tree.h"
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static git_repository *g_repo;
/* Fixture setup and teardown */
void test_object_tree_read__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_read__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tree_read__loaded(void)
{
/* access randomly the entries on a loaded tree */
git_oid id;
git_tree *tree;
git_oid__fromstr(&id, tree_oid, GIT_OID_SHA1);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_assert(git_tree_entry_byname(tree, "README") != NULL);
cl_assert(git_tree_entry_byname(tree, "NOTEXISTS") == NULL);
cl_assert(git_tree_entry_byname(tree, "") == NULL);
cl_assert(git_tree_entry_byindex(tree, 0) != NULL);
cl_assert(git_tree_entry_byindex(tree, 2) != NULL);
cl_assert(git_tree_entry_byindex(tree, 3) == NULL);
cl_assert(git_tree_entry_byindex(tree, (unsigned int)-1) == NULL);
git_tree_free(tree);
}
void test_object_tree_read__two(void)
{
/* read a tree from the repository */
git_oid id;
git_tree *tree;
const git_tree_entry *entry;
git_object *obj;
git_oid__fromstr(&id, tree_oid, GIT_OID_SHA1);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_assert(git_tree_entrycount(tree) == 3);
/* GH-86: git_object_lookup() should also check the type if the object comes from the cache */
cl_assert(git_object_lookup(&obj, g_repo, &id, GIT_OBJECT_TREE) == 0);
cl_assert(obj != NULL);
git_object_free(obj);
obj = NULL;
cl_git_fail(git_object_lookup(&obj, g_repo, &id, GIT_OBJECT_BLOB));
cl_assert(obj == NULL);
entry = git_tree_entry_byname(tree, "README");
cl_assert(entry != NULL);
cl_assert_equal_s(git_tree_entry_name(entry), "README");
cl_git_pass(git_tree_entry_to_object(&obj, g_repo, entry));
cl_assert(obj != NULL);
git_object_free(obj);
git_tree_free(tree);
}
#define BIGFILE "bigfile"
#ifdef GIT_ARCH_64
#define BIGFILE_SIZE (off_t)4294967296
#else
# define BIGFILE_SIZE SIZE_MAX
#endif
void test_object_tree_read__largefile(void)
{
const git_tree_entry *entry;
git_index_entry ie;
git_commit *commit;
git_object *object;
git_index *index;
git_tree *tree;
git_oid oid;
char *buf;
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE"))
cl_skip();
cl_assert(buf = git__calloc(1, BIGFILE_SIZE));
memset(&ie, 0, sizeof(ie));
ie.mode = GIT_FILEMODE_BLOB;
ie.path = BIGFILE;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_from_buffer(index, &ie, buf, BIGFILE_SIZE));
cl_repo_commit_from_index(&oid, g_repo, NULL, 0, BIGFILE);
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
cl_git_pass(git_commit_tree(&tree, commit));
cl_assert(entry = git_tree_entry_byname(tree, BIGFILE));
cl_git_pass(git_tree_entry_to_object(&object, g_repo, entry));
git_object_free(object);
git_tree_free(tree);
git_index_free(index);
git_commit_free(commit);
git__free(buf);
}
| libgit2-main | tests/libgit2/object/tree/read.c |
#include "clar_libgit2.h"
#include "tree.h"
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static git_repository *g_repo;
void test_object_tree_walk__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_walk__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static int treewalk_count_cb(
const char *root, const git_tree_entry *entry, void *payload)
{
int *count = payload;
GIT_UNUSED(root);
GIT_UNUSED(entry);
(*count) += 1;
return 0;
}
void test_object_tree_walk__0(void)
{
git_oid id;
git_tree *tree;
int ct;
git_oid__fromstr(&id, tree_oid, GIT_OID_SHA1);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
ct = 0;
cl_git_pass(git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_count_cb, &ct));
cl_assert_equal_i(3, ct);
ct = 0;
cl_git_pass(git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_count_cb, &ct));
cl_assert_equal_i(3, ct);
git_tree_free(tree);
}
static int treewalk_stop_cb(
const char *root, const git_tree_entry *entry, void *payload)
{
int *count = payload;
GIT_UNUSED(root);
GIT_UNUSED(entry);
(*count) += 1;
return (*count == 2) ? -123 : 0;
}
static int treewalk_stop_immediately_cb(
const char *root, const git_tree_entry *entry, void *payload)
{
GIT_UNUSED(root);
GIT_UNUSED(entry);
GIT_UNUSED(payload);
return -100;
}
void test_object_tree_walk__1(void)
{
git_oid id;
git_tree *tree;
int ct;
git_oid__fromstr(&id, tree_oid, GIT_OID_SHA1);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
ct = 0;
cl_assert_equal_i(
-123, git_tree_walk(tree, GIT_TREEWALK_PRE, treewalk_stop_cb, &ct));
cl_assert_equal_i(2, ct);
ct = 0;
cl_assert_equal_i(
-123, git_tree_walk(tree, GIT_TREEWALK_POST, treewalk_stop_cb, &ct));
cl_assert_equal_i(2, ct);
cl_assert_equal_i(
-100, git_tree_walk(
tree, GIT_TREEWALK_PRE, treewalk_stop_immediately_cb, NULL));
cl_assert_equal_i(
-100, git_tree_walk(
tree, GIT_TREEWALK_POST, treewalk_stop_immediately_cb, NULL));
git_tree_free(tree);
}
struct treewalk_skip_data {
int files;
int dirs;
const char *skip;
const char *stop;
};
static int treewalk_skip_de_cb(
const char *root, const git_tree_entry *entry, void *payload)
{
struct treewalk_skip_data *data = payload;
const char *name = git_tree_entry_name(entry);
GIT_UNUSED(root);
if (git_tree_entry_type(entry) == GIT_OBJECT_TREE)
data->dirs++;
else
data->files++;
if (data->skip && !strcmp(name, data->skip))
return 1;
else if (data->stop && !strcmp(name, data->stop))
return -1;
else
return 0;
}
void test_object_tree_walk__2(void)
{
git_oid id;
git_tree *tree;
struct treewalk_skip_data data;
/* look up a deep tree */
git_oid__fromstr(&id, "ae90f12eea699729ed24555e40b9fd669da12a12", GIT_OID_SHA1);
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
memset(&data, 0, sizeof(data));
data.skip = "de";
cl_assert_equal_i(0, git_tree_walk(
tree, GIT_TREEWALK_PRE, treewalk_skip_de_cb, &data));
cl_assert_equal_i(5, data.files);
cl_assert_equal_i(3, data.dirs);
memset(&data, 0, sizeof(data));
data.stop = "3.txt";
cl_assert_equal_i(-1, git_tree_walk(
tree, GIT_TREEWALK_PRE, treewalk_skip_de_cb, &data));
cl_assert_equal_i(3, data.files);
cl_assert_equal_i(2, data.dirs);
memset(&data, 0, sizeof(data));
data.skip = "new.txt";
cl_assert_equal_i(0, git_tree_walk(
tree, GIT_TREEWALK_PRE, treewalk_skip_de_cb, &data));
cl_assert_equal_i(7, data.files);
cl_assert_equal_i(4, data.dirs);
memset(&data, 0, sizeof(data));
data.stop = "new.txt";
cl_assert_equal_i(-1, git_tree_walk(
tree, GIT_TREEWALK_PRE, treewalk_skip_de_cb, &data));
cl_assert_equal_i(7, data.files);
cl_assert_equal_i(4, data.dirs);
git_tree_free(tree);
}
| libgit2-main | tests/libgit2/object/tree/walk.c |
#include "clar_libgit2.h"
static git_repository *repo;
static git_tree *tree;
void test_object_tree_frompath__initialize(void)
{
git_oid id;
const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_assert(repo != NULL);
cl_git_pass(git_oid__fromstr(&id, tree_with_subtrees_oid, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, repo, &id));
cl_assert(tree != NULL);
}
void test_object_tree_frompath__cleanup(void)
{
git_tree_free(tree);
tree = NULL;
git_repository_free(repo);
repo = NULL;
}
static void assert_tree_from_path(
git_tree *root,
const char *path,
const char *expected_entry_name)
{
git_tree_entry *entry;
cl_git_pass(git_tree_entry_bypath(&entry, root, path));
cl_assert_equal_s(git_tree_entry_name(entry), expected_entry_name);
git_tree_entry_free(entry);
}
void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void)
{
git_tree_entry *e;
assert_tree_from_path(tree, "README", "README");
assert_tree_from_path(tree, "ab/de/fgh/1.txt", "1.txt");
assert_tree_from_path(tree, "ab/de/fgh", "fgh");
assert_tree_from_path(tree, "ab/de/fgh/", "fgh");
assert_tree_from_path(tree, "ab/de", "de");
assert_tree_from_path(tree, "ab/", "ab");
assert_tree_from_path(tree, "ab/de/", "de");
cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt"));
cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "README/"));
cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt"));
cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "nope/de/fgh/1.txt"));
cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt"));
cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt/"));
}
void test_object_tree_frompath__fail_when_processing_an_invalid_path(void)
{
git_tree_entry *e;
cl_must_fail(git_tree_entry_bypath(&e, tree, "/"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab/de"));
cl_must_fail(git_tree_entry_bypath(&e, tree, "ab//de"));
}
| libgit2-main | tests/libgit2/object/tree/frompath.c |
#include "clar_libgit2.h"
#include "tree.h"
#include "object.h"
#define OID1_HEX \
"\xae\x90\xf1\x2e\xea\x69\x97\x29\xed\x24" \
"\x55\x5e\x40\xb9\xfd\x66\x9d\xa1\x2a\x12"
#define OID1_STR "ae90f12eea699729ed24555e40b9fd669da12a12"
#define OID2_HEX \
"\xe8\xbf\xe5\xaf\x39\x57\x9a\x7e\x48\x98" \
"\xbb\x23\xf3\xa7\x6a\x72\xc3\x68\xce\xe6"
#define OID2_STR "e8bfe5af39579a7e4898bb23f3a76a72c368cee6"
typedef struct {
const char *filename;
uint16_t attr;
const char *oid;
} expected_entry;
static void assert_tree_parses(const char *data, size_t datalen,
expected_entry *expected_entries, size_t expected_nentries)
{
git_tree *tree;
size_t n;
if (!datalen)
datalen = strlen(data);
cl_git_pass(git_object__from_raw((git_object **) &tree, data, datalen, GIT_OBJECT_TREE));
cl_assert_equal_i(git_tree_entrycount(tree), expected_nentries);
for (n = 0; n < expected_nentries; n++) {
expected_entry *expected = expected_entries + n;
const git_tree_entry *entry;
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, expected->oid, GIT_OID_SHA1));
cl_assert(entry = git_tree_entry_byname(tree, expected->filename));
cl_assert_equal_s(expected->filename, entry->filename);
cl_assert_equal_i(expected->attr, entry->attr);
cl_assert_equal_oid(&oid, &entry->oid);
}
git_object_free(&tree->object);
}
static void assert_tree_fails(const char *data, size_t datalen)
{
git_object *object;
if (!datalen)
datalen = strlen(data);
cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TREE));
}
void test_object_tree_parse__single_blob_parses(void)
{
expected_entry entries[] = {
{ "foo", 0100644, OID1_STR },
};
const char data[] = "100644 foo\x00" OID1_HEX;
assert_tree_parses(data, ARRAY_SIZE(data) - 1, entries, ARRAY_SIZE(entries));
}
void test_object_tree_parse__single_tree_parses(void)
{
expected_entry entries[] = {
{ "foo", 040000, OID1_STR },
};
const char data[] = "040000 foo\x00" OID1_HEX;
assert_tree_parses(data, ARRAY_SIZE(data) - 1, entries, ARRAY_SIZE(entries));
}
void test_object_tree_parse__leading_filename_spaces_parse(void)
{
expected_entry entries[] = {
{ " bar", 0100644, OID1_STR },
};
const char data[] = "100644 bar\x00" OID1_HEX;
assert_tree_parses(data, ARRAY_SIZE(data) - 1, entries, ARRAY_SIZE(entries));
}
void test_object_tree_parse__multiple_entries_parse(void)
{
expected_entry entries[] = {
{ "bar", 0100644, OID1_STR },
{ "foo", 040000, OID2_STR },
};
const char data[] =
"100644 bar\x00" OID1_HEX
"040000 foo\x00" OID2_HEX;
assert_tree_parses(data, ARRAY_SIZE(data) - 1, entries, ARRAY_SIZE(entries));
}
void test_object_tree_parse__invalid_mode_fails(void)
{
const char data[] = "10x644 bar\x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__missing_mode_fails(void)
{
const char data[] = " bar\x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__mode_doesnt_cause_oob_read(void)
{
const char data[] = "100644 bar\x00" OID1_HEX;
assert_tree_fails(data, 2);
/*
* An oob-read would correctly parse the filename and
* later fail to parse the OID with a different error
* message
*/
cl_assert_equal_s(git_error_last()->message, "failed to parse tree: missing space after filemode");
}
void test_object_tree_parse__unreasonably_large_mode_fails(void)
{
const char data[] = "10000000000000000000000000 bar\x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__missing_filename_separator_fails(void)
{
const char data[] = "100644bar\x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__missing_filename_terminator_fails(void)
{
const char data[] = "100644 bar" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__empty_filename_fails(void)
{
const char data[] = "100644 \x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__trailing_garbage_fails(void)
{
const char data[] = "100644 bar\x00" OID1_HEX "x";
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__leading_space_fails(void)
{
const char data[] = " 100644 bar\x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 1);
}
void test_object_tree_parse__truncated_oid_fails(void)
{
const char data[] = " 100644 bar\x00" OID1_HEX;
assert_tree_fails(data, ARRAY_SIZE(data) - 2);
}
| libgit2-main | tests/libgit2/object/tree/parse.c |
#include "clar_libgit2.h"
#include "tree.h"
static const char *blob_oid = "fa49b077972391ad58037050f2a75f74e3671e92";
static const char *first_tree = "181037049a54a1eb5fab404658a3a250b44335d7";
static const char *second_tree = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
static git_repository *g_repo;
/* Fixture setup and teardown */
void test_object_tree_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tree_write__cleanup(void)
{
cl_git_sandbox_cleanup();
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
}
void test_object_tree_write__from_memory(void)
{
/* write a tree from a memory */
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, rid, id2;
git_oid__fromstr(&id, first_tree, GIT_OID_SHA1);
git_oid__fromstr(&id2, second_tree, GIT_OID_SHA1);
git_oid__fromstr(&bid, blob_oid, GIT_OID_SHA1);
/* create a second tree from first tree using `git_treebuilder_insert`
* on REPOSITORY_FOLDER.
*/
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_new(&builder, g_repo, tree));
cl_git_fail(git_treebuilder_insert(NULL, builder, "",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "/",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "..",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".",
&bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "folder/new.txt",
&bid, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_write(&rid, builder));
cl_assert(git_oid_cmp(&rid, &id2) == 0);
git_treebuilder_free(builder);
git_tree_free(tree);
}
void test_object_tree_write__subtree(void)
{
/* write a hierarchical tree from a memory */
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, subtree_id, id2, id3;
git_oid id_hiearar;
git_oid__fromstr(&id, first_tree, GIT_OID_SHA1);
git_oid__fromstr(&id2, second_tree, GIT_OID_SHA1);
git_oid__fromstr(&id3, third_tree, GIT_OID_SHA1);
git_oid__fromstr(&bid, blob_oid, GIT_OID_SHA1);
/* create subtree */
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); /* -V536 */
cl_git_pass(git_treebuilder_write(&subtree_id, builder));
git_treebuilder_free(builder);
/* create parent tree */
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_new(&builder, g_repo, tree));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); /* -V536 */
cl_git_pass(git_treebuilder_write(&id_hiearar, builder));
git_treebuilder_free(builder);
git_tree_free(tree);
cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0);
/* check data is correct */
cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar));
cl_assert(2 == git_tree_entrycount(tree));
git_tree_free(tree);
}
/*
* And the Lord said: Is this tree properly sorted?
*/
void test_object_tree_write__sorted_subtrees(void)
{
git_treebuilder *builder;
git_tree *tree;
unsigned int i;
int position_c = -1, position_cake = -1, position_config = -1;
struct {
unsigned int attr;
const char *filename;
} entries[] = {
{ GIT_FILEMODE_BLOB, ".gitattributes" },
{ GIT_FILEMODE_BLOB, ".gitignore" },
{ GIT_FILEMODE_BLOB, ".htaccess" },
{ GIT_FILEMODE_BLOB, "Capfile" },
{ GIT_FILEMODE_BLOB, "Makefile"},
{ GIT_FILEMODE_BLOB, "README"},
{ GIT_FILEMODE_TREE, "app"},
{ GIT_FILEMODE_TREE, "cake"},
{ GIT_FILEMODE_TREE, "config"},
{ GIT_FILEMODE_BLOB, "c"},
{ GIT_FILEMODE_BLOB, "git_test.txt"},
{ GIT_FILEMODE_BLOB, "htaccess.htaccess"},
{ GIT_FILEMODE_BLOB, "index.php"},
{ GIT_FILEMODE_TREE, "plugins"},
{ GIT_FILEMODE_TREE, "schemas"},
{ GIT_FILEMODE_TREE, "ssl-certs"},
{ GIT_FILEMODE_TREE, "vendors"}
};
git_oid bid, tid, tree_oid;
cl_git_pass(git_oid__fromstr(&bid, blob_oid, GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&tid, first_tree, GIT_OID_SHA1));
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
for (i = 0; i < ARRAY_SIZE(entries); ++i) {
git_oid *id = entries[i].attr == GIT_FILEMODE_TREE ? &tid : &bid;
cl_git_pass(git_treebuilder_insert(NULL,
builder, entries[i].filename, id, entries[i].attr));
}
cl_git_pass(git_treebuilder_write(&tree_oid, builder));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
for (i = 0; i < git_tree_entrycount(tree); i++) {
const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
if (strcmp(entry->filename, "c") == 0)
position_c = i;
if (strcmp(entry->filename, "cake") == 0)
position_cake = i;
if (strcmp(entry->filename, "config") == 0)
position_config = i;
}
git_tree_free(tree);
cl_assert(position_c != -1);
cl_assert(position_cake != -1);
cl_assert(position_config != -1);
cl_assert(position_c < position_cake);
cl_assert(position_cake < position_config);
git_treebuilder_free(builder);
}
static struct {
unsigned int attr;
const char *filename;
} _entries[] = {
{ GIT_FILEMODE_BLOB, "aardvark" },
{ GIT_FILEMODE_BLOB, ".first" },
{ GIT_FILEMODE_BLOB, "apple" },
{ GIT_FILEMODE_BLOB, "last"},
{ GIT_FILEMODE_BLOB, "apple_after"},
{ GIT_FILEMODE_BLOB, "after_aardvark"},
{ 0, NULL },
};
void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
{
git_treebuilder *builder;
int i, aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i;
git_oid entry_oid, tree_oid;
git_tree *tree;
cl_git_pass(git_oid__fromstr(&entry_oid, blob_oid, GIT_OID_SHA1));
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
cl_assert_equal_i(0, (int)git_treebuilder_entrycount(builder));
for (i = 0; _entries[i].filename; ++i)
cl_git_pass(git_treebuilder_insert(NULL,
builder, _entries[i].filename, &entry_oid, _entries[i].attr));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_remove(builder, "apple"));
cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_remove(builder, "apple_after"));
cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "before_last", &entry_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder));
/* reinsert apple_after */
cl_git_pass(git_treebuilder_insert(
NULL, builder, "apple_after", &entry_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_remove(builder, "last"));
cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder));
/* reinsert last */
cl_git_pass(git_treebuilder_insert(
NULL, builder, "last", &entry_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_insert(
NULL, builder, "apple_extra", &entry_oid, GIT_FILEMODE_BLOB));
cl_assert_equal_i(7, (int)git_treebuilder_entrycount(builder));
cl_git_pass(git_treebuilder_write(&tree_oid, builder));
git_treebuilder_free(builder);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
cl_assert_equal_i(7, (int)git_tree_entrycount(tree));
cl_assert(git_tree_entry_byname(tree, ".first") != NULL);
cl_assert(git_tree_entry_byname(tree, "apple") == NULL);
cl_assert(git_tree_entry_byname(tree, "apple_after") != NULL);
cl_assert(git_tree_entry_byname(tree, "apple_extra") != NULL);
cl_assert(git_tree_entry_byname(tree, "last") != NULL);
aardvark_i = apple_i = apple_after_i = apple_extra_i = last_i = -1;
for (i = 0; i < 7; ++i) {
const git_tree_entry *entry = git_tree_entry_byindex(tree, i);
if (!strcmp(entry->filename, "aardvark"))
aardvark_i = i;
else if (!strcmp(entry->filename, "apple"))
apple_i = i;
else if (!strcmp(entry->filename, "apple_after"))
apple_after_i = i;
else if (!strcmp(entry->filename, "apple_extra"))
apple_extra_i = i;
else if (!strcmp(entry->filename, "last"))
last_i = i;
}
cl_assert_equal_i(-1, apple_i);
cl_assert_equal_i(6, last_i);
cl_assert(aardvark_i < apple_after_i);
cl_assert(apple_after_i < apple_extra_i);
git_tree_free(tree);
}
static int treebuilder_filter_prefixed(
const git_tree_entry *entry, void *payload)
{
return !git__prefixcmp(git_tree_entry_name(entry), payload);
}
void test_object_tree_write__filtering(void)
{
git_treebuilder *builder;
int i;
git_oid entry_oid, tree_oid;
git_tree *tree;
git_oid__fromstr(&entry_oid, blob_oid, GIT_OID_SHA1);
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
for (i = 0; _entries[i].filename; ++i)
cl_git_pass(git_treebuilder_insert(NULL,
builder, _entries[i].filename, &entry_oid, _entries[i].attr));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
cl_assert(git_treebuilder_get(builder, "apple") != NULL);
cl_assert(git_treebuilder_get(builder, "aardvark") != NULL);
cl_assert(git_treebuilder_get(builder, "last") != NULL);
git_treebuilder_filter(builder, treebuilder_filter_prefixed, "apple");
cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder));
cl_assert(git_treebuilder_get(builder, "apple") == NULL);
cl_assert(git_treebuilder_get(builder, "aardvark") != NULL);
cl_assert(git_treebuilder_get(builder, "last") != NULL);
git_treebuilder_filter(builder, treebuilder_filter_prefixed, "a");
cl_assert_equal_i(2, (int)git_treebuilder_entrycount(builder));
cl_assert(git_treebuilder_get(builder, "aardvark") == NULL);
cl_assert(git_treebuilder_get(builder, "last") != NULL);
cl_git_pass(git_treebuilder_write(&tree_oid, builder));
git_treebuilder_free(builder);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
cl_assert_equal_i(2, (int)git_tree_entrycount(tree));
git_tree_free(tree);
}
void test_object_tree_write__cruel_paths(void)
{
static const char *the_paths[] = {
"C:\\",
" : * ? \" \n < > |",
"a\\b",
"\\\\b\a",
":\\",
"COM1",
"foo.aux",
REP1024("1234"), /* 4096 char string */
REP1024("12345678"), /* 8192 char string */
"\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD", /* Ūnĭcōde̽ */
NULL
};
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, subid;
const char **scan;
int count = 0, i, j;
git_tree_entry *te;
git_oid__fromstr(&bid, blob_oid, GIT_OID_SHA1);
/* create tree */
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
for (scan = the_paths; *scan; ++scan) {
cl_git_pass(git_treebuilder_insert(
NULL, builder, *scan, &bid, GIT_FILEMODE_BLOB));
count++;
}
cl_git_pass(git_treebuilder_write(&id, builder));
git_treebuilder_free(builder);
/* check data is correct */
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_assert_equal_i(count, git_tree_entrycount(tree));
for (scan = the_paths; *scan; ++scan) {
const git_tree_entry *cte = git_tree_entry_byname(tree, *scan);
cl_assert(cte != NULL);
cl_assert_equal_s(*scan, git_tree_entry_name(cte));
}
for (scan = the_paths; *scan; ++scan) {
cl_git_pass(git_tree_entry_bypath(&te, tree, *scan));
cl_assert_equal_s(*scan, git_tree_entry_name(te));
git_tree_entry_free(te);
}
git_tree_free(tree);
/* let's try longer paths */
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
for (scan = the_paths; *scan; ++scan) {
cl_git_pass(git_treebuilder_insert(
NULL, builder, *scan, &id, GIT_FILEMODE_TREE));
}
cl_git_pass(git_treebuilder_write(&subid, builder));
git_treebuilder_free(builder);
/* check data is correct */
cl_git_pass(git_tree_lookup(&tree, g_repo, &subid));
cl_assert_equal_i(count, git_tree_entrycount(tree));
for (i = 0; i < count; ++i) {
for (j = 0; j < count; ++j) {
git_str b = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&b, the_paths[i], the_paths[j]));
cl_git_pass(git_tree_entry_bypath(&te, tree, b.ptr));
cl_assert_equal_s(the_paths[j], git_tree_entry_name(te));
git_tree_entry_free(te);
git_str_dispose(&b);
}
}
git_tree_free(tree);
}
void test_object_tree_write__protect_filesystems(void)
{
git_treebuilder *builder;
git_oid bid;
cl_git_pass(git_oid__fromstr(&bid, "fa49b077972391ad58037050f2a75f74e3671e92", GIT_OID_SHA1));
/* Ensure that (by default) we can write objects with funny names on
* platforms that are not affected.
*/
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git.", &bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "git~1", &bid, GIT_FILEMODE_BLOB));
#ifndef __APPLE__
cl_git_pass(git_treebuilder_insert(NULL, builder, ".git\xef\xbb\xbf", &bid, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_insert(NULL, builder, ".git\xe2\x80\xad", &bid, GIT_FILEMODE_BLOB));
#endif
git_treebuilder_free(builder);
/* Now turn on core.protectHFS and core.protectNTFS and validate that these
* paths are rejected.
*/
cl_repo_set_bool(g_repo, "core.protectHFS", true);
cl_repo_set_bool(g_repo, "core.protectNTFS", true);
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git.", &bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, "git~1", &bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git\xef\xbb\xbf", &bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git\xe2\x80\xad", &bid, GIT_FILEMODE_BLOB));
cl_git_fail(git_treebuilder_insert(NULL, builder, ".git::$INDEX_ALLOCATION/dummy-file", &bid, GIT_FILEMODE_BLOB));
git_treebuilder_free(builder);
}
static void test_invalid_objects(bool should_allow_invalid)
{
git_treebuilder *builder;
git_oid valid_blob_id, invalid_blob_id, valid_tree_id, invalid_tree_id;
#define assert_allowed(expr) \
clar__assert(!(expr) == should_allow_invalid, \
__FILE__, __func__, __LINE__, \
(should_allow_invalid ? \
"Expected function call to succeed: " #expr : \
"Expected function call to fail: " #expr), \
NULL, 1)
cl_git_pass(git_oid__fromstr(&valid_blob_id, blob_oid, GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&invalid_blob_id,
"1234567890123456789012345678901234567890",
GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&valid_tree_id, first_tree, GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&invalid_tree_id,
"0000000000111111111122222222223333333333",
GIT_OID_SHA1));
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
/* test valid blobs and trees (these should always pass) */
cl_git_pass(git_treebuilder_insert(NULL, builder, "file.txt", &valid_blob_id, GIT_FILEMODE_BLOB));
cl_git_pass(git_treebuilder_insert(NULL, builder, "folder", &valid_tree_id, GIT_FILEMODE_TREE));
/* replace valid files and folders with invalid ones */
assert_allowed(git_treebuilder_insert(NULL, builder, "file.txt", &invalid_blob_id, GIT_FILEMODE_BLOB));
assert_allowed(git_treebuilder_insert(NULL, builder, "folder", &invalid_blob_id, GIT_FILEMODE_BLOB));
/* insert new invalid files and folders */
assert_allowed(git_treebuilder_insert(NULL, builder, "invalid_file.txt", &invalid_blob_id, GIT_FILEMODE_BLOB));
assert_allowed(git_treebuilder_insert(NULL, builder, "invalid_folder", &invalid_blob_id, GIT_FILEMODE_BLOB));
/* insert valid blobs as trees and trees as blobs */
assert_allowed(git_treebuilder_insert(NULL, builder, "file_as_folder", &valid_blob_id, GIT_FILEMODE_TREE));
assert_allowed(git_treebuilder_insert(NULL, builder, "folder_as_file.txt", &valid_tree_id, GIT_FILEMODE_BLOB));
#undef assert_allowed
git_treebuilder_free(builder);
}
static void test_inserting_submodule(void)
{
git_treebuilder *bld;
git_oid sm_id;
cl_git_pass(git_oid__fromstr(&sm_id, "da39a3ee5e6b4b0d3255bfef95601890afd80709", GIT_OID_SHA1));
cl_git_pass(git_treebuilder_new(&bld, g_repo, NULL));
cl_git_pass(git_treebuilder_insert(NULL, bld, "sm", &sm_id, GIT_FILEMODE_COMMIT));
git_treebuilder_free(bld);
}
void test_object_tree_write__object_validity(void)
{
/* Ensure that we cannot add invalid objects by default */
test_invalid_objects(false);
test_inserting_submodule();
/* Ensure that we can turn off validation */
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
test_invalid_objects(true);
test_inserting_submodule();
}
void test_object_tree_write__invalid_null_oid(void)
{
git_treebuilder *bld;
git_oid null_oid = GIT_OID_SHA1_ZERO;
cl_git_pass(git_treebuilder_new(&bld, g_repo, NULL));
cl_git_fail(git_treebuilder_insert(NULL, bld, "null_oid_file", &null_oid, GIT_FILEMODE_BLOB));
cl_assert(git_error_last() && strstr(git_error_last()->message, "null OID") != NULL);
git_treebuilder_free(bld);
}
| libgit2-main | tests/libgit2/object/tree/write.c |
#include "clar_libgit2.h"
#include "tree.h"
static git_repository *repo;
static const char *blob_oid = "3d0970ec547fc41ef8a5882dde99c6adce65b021";
static const char *tree_oid = "1b05fdaa881ee45b48cbaa5e9b037d667a47745e";
void test_object_tree_attributes__initialize(void)
{
repo = cl_git_sandbox_init("deprecated-mode.git");
}
void test_object_tree_attributes__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tree_attributes__ensure_correctness_of_attributes_on_insertion(void)
{
git_treebuilder *builder;
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, blob_oid, GIT_OID_SHA1));
cl_git_pass(git_treebuilder_new(&builder, repo, NULL));
cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, (git_filemode_t)0777777));
cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, (git_filemode_t)0100666));
cl_git_fail(git_treebuilder_insert(NULL, builder, "one.txt", &oid, (git_filemode_t)0000001));
git_treebuilder_free(builder);
}
void test_object_tree_attributes__group_writable_tree_entries_created_with_an_antique_git_version_can_still_be_accessed(void)
{
git_oid tid;
git_tree *tree;
const git_tree_entry *entry;
cl_git_pass(git_oid__fromstr(&tid, tree_oid, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, repo, &tid));
entry = git_tree_entry_byname(tree, "old_mode.txt");
cl_assert_equal_i(
GIT_FILEMODE_BLOB,
git_tree_entry_filemode(entry));
git_tree_free(tree);
}
void test_object_tree_attributes__treebuilder_reject_invalid_filemode(void)
{
git_treebuilder *builder;
git_oid bid;
const git_tree_entry *entry;
cl_git_pass(git_oid__fromstr(&bid, blob_oid, GIT_OID_SHA1));
cl_git_pass(git_treebuilder_new(&builder, repo, NULL));
cl_git_fail(git_treebuilder_insert(
&entry,
builder,
"normalized.txt",
&bid,
GIT_FILEMODE_BLOB_GROUP_WRITABLE));
git_treebuilder_free(builder);
}
void test_object_tree_attributes__normalize_attributes_when_creating_a_tree_from_an_existing_one(void)
{
git_treebuilder *builder;
git_oid tid, tid2;
git_tree *tree;
const git_tree_entry *entry;
cl_git_pass(git_oid__fromstr(&tid, tree_oid, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, repo, &tid));
cl_git_pass(git_treebuilder_new(&builder, repo, tree));
entry = git_treebuilder_get(builder, "old_mode.txt");
cl_assert(entry != NULL);
cl_assert_equal_i(
GIT_FILEMODE_BLOB,
git_tree_entry_filemode(entry));
cl_git_pass(git_treebuilder_write(&tid2, builder));
git_treebuilder_free(builder);
git_tree_free(tree);
cl_git_pass(git_tree_lookup(&tree, repo, &tid2));
entry = git_tree_entry_byname(tree, "old_mode.txt");
cl_assert(entry != NULL);
cl_assert_equal_i(
GIT_FILEMODE_BLOB,
git_tree_entry_filemode(entry));
git_tree_free(tree);
}
void test_object_tree_attributes__normalize_600(void)
{
git_oid id;
git_tree *tree;
const git_tree_entry *entry;
git_oid__fromstr(&id, "0810fb7818088ff5ac41ee49199b51473b1bd6c7", GIT_OID_SHA1);
cl_git_pass(git_tree_lookup(&tree, repo, &id));
entry = git_tree_entry_byname(tree, "ListaTeste.xml");
cl_assert_equal_i(git_tree_entry_filemode(entry), GIT_FILEMODE_BLOB);
cl_assert_equal_i(git_tree_entry_filemode_raw(entry), 0100600);
git_tree_free(tree);
}
| libgit2-main | tests/libgit2/object/tree/attributes.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "futils.h"
static git_repository *repo;
static char textual_content[] = "libgit2\n\r\n\0";
void test_object_blob_fromstream__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
}
void test_object_blob_fromstream__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_blob_fromstream__multiple_write(void)
{
git_oid expected_id, id;
git_object *blob;
git_writestream *stream;
int i, howmany = 6;
cl_git_pass(git_oid__fromstr(&expected_id, "321cbdf08803c744082332332838df6bd160f8f9", GIT_OID_SHA1));
cl_git_fail_with(GIT_ENOTFOUND,
git_object_lookup(&blob, repo, &expected_id, GIT_OBJECT_ANY));
cl_git_pass(git_blob_create_from_stream(&stream, repo, NULL));
for (i = 0; i < howmany; i++)
cl_git_pass(stream->write(stream, textual_content, strlen(textual_content)));
cl_git_pass(git_blob_create_from_stream_commit(&id, stream));
cl_assert_equal_oid(&expected_id, &id);
cl_git_pass(git_object_lookup(&blob, repo, &expected_id, GIT_OBJECT_BLOB));
git_object_free(blob);
}
#define GITATTR "* text=auto\n" \
"*.txt text\n" \
"*.data binary\n"
static void write_attributes(git_repository *repo)
{
git_str buf = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&buf, git_repository_path(repo), "info"));
cl_git_pass(git_str_joinpath(&buf, git_str_cstr(&buf), "attributes"));
cl_git_pass(git_futils_mkpath2file(git_str_cstr(&buf), 0777));
cl_git_rewritefile(git_str_cstr(&buf), GITATTR);
git_str_dispose(&buf);
}
static void assert_named_chunked_blob(const char *expected_sha, const char *fake_name)
{
git_oid expected_id, id;
git_writestream *stream;
int i, howmany = 6;
cl_git_pass(git_oid__fromstr(&expected_id, expected_sha, GIT_OID_SHA1));
cl_git_pass(git_blob_create_from_stream(&stream, repo, fake_name));
for (i = 0; i < howmany; i++)
cl_git_pass(stream->write(stream, textual_content, strlen(textual_content)));
cl_git_pass(git_blob_create_from_stream_commit(&id, stream));
cl_assert_equal_oid(&expected_id, &id);
}
void test_object_blob_fromstream__creating_a_blob_from_chunks_honors_the_attributes_directives(void)
{
write_attributes(repo);
assert_named_chunked_blob("321cbdf08803c744082332332838df6bd160f8f9", "dummy.data");
assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.txt");
assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.dunno");
}
| libgit2-main | tests/libgit2/object/blob/fromstream.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
static git_repository *g_repo = NULL;
#define CRLF_NUM_TEST_OBJECTS 9
static const char *g_crlf_raw[CRLF_NUM_TEST_OBJECTS] = {
"",
"foo\nbar\n",
"foo\rbar\r",
"foo\r\nbar\r\n",
"foo\nbar\rboth\r\nreversed\n\ragain\nproblems\r",
"123\n\000\001\002\003\004abc\255\254\253\r\n",
"\xEF\xBB\xBFThis is UTF-8\n",
"\xEF\xBB\xBF\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\r\n\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\r\n",
"\xFE\xFF\x00T\x00h\x00i\x00s\x00!"
};
static off64_t g_crlf_raw_len[CRLF_NUM_TEST_OBJECTS] = {
-1, -1, -1, -1, -1, 17, -1, -1, 12
};
static git_oid g_crlf_oids[CRLF_NUM_TEST_OBJECTS];
static git_str g_crlf_filtered[CRLF_NUM_TEST_OBJECTS] = {
{ "", 0, 0 },
{ "foo\nbar\n", 0, 8 },
{ "foo\rbar\r", 0, 8 },
{ "foo\nbar\n", 0, 8 },
{ "foo\nbar\rboth\nreversed\n\ragain\nproblems\r", 0, 38 },
{ "123\n\000\001\002\003\004abc\255\254\253\n", 0, 16 },
{ "\xEF\xBB\xBFThis is UTF-8\n", 0, 17 },
{ "\xEF\xBB\xBF\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\n\xE3\x81\xBB\xE3\x81\x92\xE3\x81\xBB\xE3\x81\x92\n", 0, 29 },
{ "\xFE\xFF\x00T\x00h\x00i\x00s\x00!", 0, 12 }
};
static git_str_text_stats g_crlf_filtered_stats[CRLF_NUM_TEST_OBJECTS] = {
{ 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 2, 0, 6, 0 },
{ 0, 0, 2, 0, 0, 6, 0 },
{ 0, 0, 2, 2, 2, 6, 0 },
{ 0, 0, 4, 4, 1, 31, 0 },
{ 0, 1, 1, 2, 1, 9, 5 },
{ GIT_STR_BOM_UTF8, 0, 0, 1, 0, 16, 0 },
{ GIT_STR_BOM_UTF8, 0, 2, 2, 2, 27, 0 },
{ GIT_STR_BOM_UTF16_BE, 5, 0, 0, 0, 7, 5 },
};
void test_object_blob_filter__initialize(void)
{
int i;
g_repo = cl_git_sandbox_init("empty_standard_repo");
for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
if (g_crlf_raw_len[i] < 0)
g_crlf_raw_len[i] = strlen(g_crlf_raw[i]);
cl_git_pass(git_blob_create_from_buffer(
&g_crlf_oids[i], g_repo, g_crlf_raw[i], (size_t)g_crlf_raw_len[i]));
}
}
void test_object_blob_filter__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_blob_filter__unfiltered(void)
{
int i;
git_blob *blob;
for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
size_t raw_len = (size_t)g_crlf_raw_len[i];
cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
cl_assert_equal_sz(raw_len, (size_t)git_blob_rawsize(blob));
cl_assert_equal_i(
0, memcmp(g_crlf_raw[i], git_blob_rawcontent(blob), raw_len));
git_blob_free(blob);
}
}
void test_object_blob_filter__stats(void)
{
int i;
git_blob *blob;
git_str buf = GIT_STR_INIT;
git_str_text_stats stats;
for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
cl_git_pass(git_blob__getbuf(&buf, blob));
git_str_gather_text_stats(&stats, &buf, false);
cl_assert_equal_i(
0, memcmp(&g_crlf_filtered_stats[i], &stats, sizeof(stats)));
git_blob_free(blob);
}
git_str_dispose(&buf);
}
void test_object_blob_filter__to_odb(void)
{
git_filter_list *fl = NULL;
git_config *cfg;
int i;
git_blob *blob;
git_buf out = GIT_BUF_INIT, zeroed;
cl_git_pass(git_repository_config(&cfg, g_repo));
cl_assert(cfg);
git_attr_cache_flush(g_repo);
cl_git_append2file("empty_standard_repo/.gitattributes", "*.txt text\n");
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "filename.txt", GIT_FILTER_TO_ODB, 0));
cl_assert(fl != NULL);
for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));
/* try once with allocated blob */
cl_git_pass(git_filter_list_apply_to_blob(&out, fl, blob));
cl_assert_equal_sz(g_crlf_filtered[i].size, out.size);
cl_assert_equal_i(
0, memcmp(out.ptr, g_crlf_filtered[i].ptr, out.size));
/* try again with zeroed blob */
memset(&zeroed, 0, sizeof(zeroed));
cl_git_pass(git_filter_list_apply_to_blob(&zeroed, fl, blob));
cl_assert_equal_sz(g_crlf_filtered[i].size, zeroed.size);
cl_assert_equal_i(
0, memcmp(zeroed.ptr, g_crlf_filtered[i].ptr, zeroed.size));
git_buf_dispose(&zeroed);
git_blob_free(blob);
}
git_filter_list_free(fl);
git_buf_dispose(&out);
git_config_free(cfg);
}
| libgit2-main | tests/libgit2/object/blob/filter.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "futils.h"
static git_repository *repo;
#define WORKDIR "empty_standard_repo"
#define BARE_REPO "testrepo.git"
#define ELSEWHERE "elsewhere"
typedef int (*blob_creator_fn)(
git_oid *,
git_repository *,
const char *);
void test_object_blob_write__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void assert_blob_creation(const char *path_to_file, const char *blob_from_path, blob_creator_fn creator)
{
git_oid oid;
cl_git_mkfile(path_to_file, "1..2...3... Can you hear me?\n");
cl_must_pass(creator(&oid, repo, blob_from_path));
cl_assert(git_oid_streq(&oid, "da5e4f20c91c81b44a7e298f3d3fb3fe2f178e32") == 0);
}
void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_file_located_in_the_working_directory(void)
{
repo = cl_git_sandbox_init(WORKDIR);
assert_blob_creation(WORKDIR "/test.txt", "test.txt", &git_blob_create_from_workdir);
}
void test_object_blob_write__can_create_a_blob_in_a_standard_repo_from_a_absolute_filepath_pointing_outside_of_the_working_directory(void)
{
git_str full_path = GIT_STR_INIT;
repo = cl_git_sandbox_init(WORKDIR);
cl_must_pass(p_mkdir(ELSEWHERE, 0777));
cl_must_pass(git_fs_path_prettify_dir(&full_path, ELSEWHERE, NULL));
cl_must_pass(git_str_puts(&full_path, "test.txt"));
assert_blob_creation(ELSEWHERE "/test.txt", git_str_cstr(&full_path), &git_blob_create_from_disk);
git_str_dispose(&full_path);
cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
}
void test_object_blob_write__can_create_a_blob_in_a_bare_repo_from_a_absolute_filepath(void)
{
git_str full_path = GIT_STR_INIT;
repo = cl_git_sandbox_init(BARE_REPO);
cl_must_pass(p_mkdir(ELSEWHERE, 0777));
cl_must_pass(git_fs_path_prettify_dir(&full_path, ELSEWHERE, NULL));
cl_must_pass(git_str_puts(&full_path, "test.txt"));
assert_blob_creation(ELSEWHERE "/test.txt", git_str_cstr(&full_path), &git_blob_create_from_disk);
git_str_dispose(&full_path);
cl_must_pass(git_futils_rmdir_r(ELSEWHERE, NULL, GIT_RMDIR_REMOVE_FILES));
}
| libgit2-main | tests/libgit2/object/blob/write.c |
#include "clar_libgit2.h"
#include "tag.h"
static git_repository *g_repo;
#define MAX_USED_TAGS 6
struct pattern_match_t
{
const char* pattern;
const size_t expected_matches;
const char* expected_results[MAX_USED_TAGS];
};
/* Helpers */
static void ensure_tag_pattern_match(git_repository *repo,
const struct pattern_match_t* data)
{
int already_found[MAX_USED_TAGS] = { 0 };
git_strarray tag_list;
int error = 0;
size_t successfully_found = 0;
size_t i, j;
cl_assert(data->expected_matches <= MAX_USED_TAGS);
if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0)
goto exit;
if (tag_list.count != data->expected_matches)
{
error = GIT_ERROR;
goto exit;
}
/* we have to be prepared that tags come in any order. */
for (i = 0; i < tag_list.count; i++)
{
for (j = 0; j < data->expected_matches; j++)
{
if (!already_found[j] && !strcmp(data->expected_results[j], tag_list.strings[i]))
{
already_found[j] = 1;
successfully_found++;
break;
}
}
}
cl_assert_equal_i((int)successfully_found, (int)data->expected_matches);
exit:
git_strarray_dispose(&tag_list);
cl_git_pass(error);
}
/* Fixture setup and teardown */
void test_object_tag_list__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tag_list__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tag_list__list_all(void)
{
/* list all tag names from the repository */
git_strarray tag_list;
cl_git_pass(git_tag_list(&tag_list, g_repo));
cl_assert_equal_i((int)tag_list.count, 6);
git_strarray_dispose(&tag_list);
}
static const struct pattern_match_t matches[] = {
/* All tags, including a packed one and two namespaced ones. */
{ "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } },
/* beginning with */
{ "t*", 1, { "test" } },
/* ending with */
{ "*b", 2, { "e90810b", "point_to_blob" } },
/* exact match */
{ "e", 0 },
{ "e90810b", 1, { "e90810b" } },
/* either or */
{ "e90810[ab]", 1, { "e90810b" } },
/* glob in the middle */
{ "foo/*/bar", 1, { "foo/foo/bar" } },
/*
* The matching of '*' is based on plain string matching analog to the regular expression ".*"
* => a '/' in the tag name has no special meaning.
* Compare to `git tag -l "*bar"`
*/
{ "*bar", 2, { "foo/bar", "foo/foo/bar" } },
/* End of list */
{ NULL }
};
void test_object_tag_list__list_by_pattern(void)
{
/* list all tag names from the repository matching a specified pattern */
size_t i = 0;
while (matches[i].pattern)
ensure_tag_pattern_match(g_repo, &matches[i++]);
}
| libgit2-main | tests/libgit2/object/tag/list.c |
#include "clar_libgit2.h"
#include "tag.h"
static git_repository *repo;
static git_tag *tag;
static git_object *target;
void test_object_tag_peel__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
}
void test_object_tag_peel__cleanup(void)
{
git_tag_free(tag);
tag = NULL;
git_object_free(target);
target = NULL;
git_repository_free(repo);
repo = NULL;
cl_fixture_cleanup("testrepo.git");
}
static void retrieve_tag_from_oid(git_tag **tag_out, git_repository *repo, const char *sha)
{
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, sha, GIT_OID_SHA1));
cl_git_pass(git_tag_lookup(tag_out, repo, &oid));
}
void test_object_tag_peel__can_peel_to_a_commit(void)
{
retrieve_tag_from_oid(&tag, repo, "7b4384978d2493e851f9cca7858815fac9b10980");
cl_git_pass(git_tag_peel(&target, tag));
cl_assert(git_object_type(target) == GIT_OBJECT_COMMIT);
cl_git_pass(git_oid_streq(git_object_id(target), "e90810b8df3e80c413d903f631643c716887138d"));
}
void test_object_tag_peel__can_peel_several_nested_tags_to_a_commit(void)
{
retrieve_tag_from_oid(&tag, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
cl_git_pass(git_tag_peel(&target, tag));
cl_assert(git_object_type(target) == GIT_OBJECT_COMMIT);
cl_git_pass(git_oid_streq(git_object_id(target), "e90810b8df3e80c413d903f631643c716887138d"));
}
void test_object_tag_peel__can_peel_to_a_non_commit(void)
{
retrieve_tag_from_oid(&tag, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
cl_git_pass(git_tag_peel(&target, tag));
cl_assert(git_object_type(target) == GIT_OBJECT_BLOB);
cl_git_pass(git_oid_streq(git_object_id(target), "1385f264afb75a56a5bec74243be9b367ba4ca08"));
}
| libgit2-main | tests/libgit2/object/tag/peel.c |
#include "clar_libgit2.h"
#include "tag.h"
static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static const char *bad_tag_id = "eda9f45a2a98d4c17a09d681d88569fa4ea91755";
static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static const char *short_tag_id = "5da7760512a953e3c7c4e47e4392c7a4338fb729";
static const char *short_tagged_commit = "4a5ed60bafcf4638b7c8356bd4ce1916bfede93c";
static const char *taggerless = "4a23e2e65ad4e31c4c9db7dc746650bfad082679";
static git_repository *g_repo;
/* Fixture setup and teardown */
void test_object_tag_read__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tag_read__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tag_read__parse(void)
{
/* read and parse a tag from the repository */
git_tag *tag1, *tag2;
git_commit *commit;
git_oid id1, id2, id_commit;
git_oid__fromstr(&id1, tag1_id, GIT_OID_SHA1);
git_oid__fromstr(&id2, tag2_id, GIT_OID_SHA1);
git_oid__fromstr(&id_commit, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1));
cl_assert_equal_s(git_tag_name(tag1), "test");
cl_assert(git_tag_target_type(tag1) == GIT_OBJECT_TAG);
cl_git_pass(git_tag_target((git_object **)&tag2, tag1));
cl_assert(tag2 != NULL);
cl_assert(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
cl_git_pass(git_tag_target((git_object **)&commit, tag2));
cl_assert(commit != NULL);
cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
git_tag_free(tag1);
git_tag_free(tag2);
git_commit_free(commit);
}
void test_object_tag_read__parse_without_tagger(void)
{
/* read and parse a tag without a tagger field */
git_repository *bad_tag_repo;
git_tag *bad_tag;
git_commit *commit;
git_oid id, id_commit;
/* TODO: This is a little messy */
cl_git_pass(git_repository_open(&bad_tag_repo, cl_fixture("bad_tag.git")));
git_oid__fromstr(&id, bad_tag_id, GIT_OID_SHA1);
git_oid__fromstr(&id_commit, badly_tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_tag_lookup(&bad_tag, bad_tag_repo, &id));
cl_assert(bad_tag != NULL);
cl_assert_equal_s(git_tag_name(bad_tag), "e90810b");
cl_assert(git_oid_cmp(&id, git_tag_id(bad_tag)) == 0);
cl_assert(bad_tag->tagger == NULL);
cl_git_pass(git_tag_target((git_object **)&commit, bad_tag));
cl_assert(commit != NULL);
cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
git_tag_free(bad_tag);
git_commit_free(commit);
git_repository_free(bad_tag_repo);
}
void test_object_tag_read__parse_without_message(void)
{
/* read and parse a tag without a message field */
git_repository *short_tag_repo;
git_tag *short_tag;
git_commit *commit;
git_oid id, id_commit;
/* TODO: This is a little messy */
cl_git_pass(git_repository_open(&short_tag_repo, cl_fixture("short_tag.git")));
git_oid__fromstr(&id, short_tag_id, GIT_OID_SHA1);
git_oid__fromstr(&id_commit, short_tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_tag_lookup(&short_tag, short_tag_repo, &id));
cl_assert(short_tag != NULL);
cl_assert_equal_s(git_tag_name(short_tag), "no_description");
cl_assert(git_oid_cmp(&id, git_tag_id(short_tag)) == 0);
cl_assert(short_tag->message == NULL);
cl_git_pass(git_tag_target((git_object **)&commit, short_tag));
cl_assert(commit != NULL);
cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
git_tag_free(short_tag);
git_commit_free(commit);
git_repository_free(short_tag_repo);
}
void test_object_tag_read__without_tagger_nor_message(void)
{
git_tag *tag;
git_oid id;
git_repository *repo;
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_git_pass(git_oid__fromstr(&id, taggerless, GIT_OID_SHA1));
cl_git_pass(git_tag_lookup(&tag, repo, &id));
cl_assert_equal_s(git_tag_name(tag), "taggerless");
cl_assert(git_tag_target_type(tag) == GIT_OBJECT_COMMIT);
cl_assert(tag->message == NULL);
cl_assert(tag->tagger == NULL);
git_tag_free(tag);
git_repository_free(repo);
}
static const char *silly_tag = "object c054ccaefbf2da31c3b19178f9e3ef20a3867924\n\
type commit\n\
tag v1_0_1\n\
tagger Jamis Buck <[email protected]> 1107717917\n\
diff --git a/lib/sqlite3/version.rb b/lib/sqlite3/version.rb\n\
index 0b3bf69..4ee8fc2 100644\n\
--- a/lib/sqlite3/version.rb\n\
+++ b/lib/sqlite3/version.rb\n\
@@ -36,7 +36,7 @@ module SQLite3\n\
\n\
MAJOR = 1\n\
MINOR = 0\n\
- TINY = 0\n\
+ TINY = 1\n\
\n\
STRING = [ MAJOR, MINOR, TINY ].join( \".\" )\n\
\n\
-0600\n\
\n\
v1_0_1 release\n";
void test_object_tag_read__extra_header_fields(void)
{
git_tag *tag;
git_odb *odb;
git_oid id;
cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
cl_git_pass(git_odb_write(&id, odb, silly_tag, strlen(silly_tag), GIT_OBJECT_TAG));
cl_git_pass(git_tag_lookup(&tag, g_repo, &id));
cl_assert_equal_s("v1_0_1 release\n", git_tag_message(tag));
git_tag_free(tag);
}
| libgit2-main | tests/libgit2/object/tag/read.c |
#include "clar_libgit2.h"
#include "object.h"
#include "signature.h"
#include "tag.h"
static void assert_tag_parses(const char *data, size_t datalen,
const char *expected_oid,
const char *expected_name,
const char *expected_tagger,
const char *expected_message)
{
git_tag *tag;
if (!datalen)
datalen = strlen(data);
cl_git_pass(git_object__from_raw((git_object **) &tag, data, datalen, GIT_OBJECT_TAG));
cl_assert_equal_i(tag->type, GIT_OBJECT_TAG);
if (expected_oid) {
git_oid oid;
cl_git_pass(git_oid__fromstr(&oid, expected_oid, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &tag->target);
}
if (expected_name)
cl_assert_equal_s(expected_name, tag->tag_name);
else
cl_assert_equal_s(tag->message, NULL);
if (expected_tagger) {
git_signature *tagger;
cl_git_pass(git_signature_from_buffer(&tagger, expected_tagger));
cl_assert_equal_s(tagger->name, tag->tagger->name);
cl_assert_equal_s(tagger->email, tag->tagger->email);
cl_assert_equal_i(tagger->when.time, tag->tagger->when.time);
cl_assert_equal_i(tagger->when.offset, tag->tagger->when.offset);
cl_assert_equal_i(tagger->when.sign, tag->tagger->when.sign);
git_signature_free(tagger);
} else {
cl_assert_equal_s(tag->tagger, NULL);
}
if (expected_message)
cl_assert_equal_s(expected_message, tag->message);
else
cl_assert_equal_s(tag->message, NULL);
git_object__free(&tag->object);
}
static void assert_tag_fails(const char *data, size_t datalen)
{
git_object *object;
if (!datalen)
datalen = strlen(data);
cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TAG));
}
void test_object_tag_parse__valid_tag_parses(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"\n"
"Message";
assert_tag_parses(tag, 0,
"a8d447f68076d1520f69649bb52629941be7031f",
"tagname",
"Taggy Mr. Taggart <[email protected]>",
"Message");
}
void test_object_tag_parse__missing_tagger_parses(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag tagname\n"
"\n"
"Message";
assert_tag_parses(tag, 0,
"a8d447f68076d1520f69649bb52629941be7031f",
"tagname",
NULL,
"Message");
}
void test_object_tag_parse__missing_message_parses(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n";
assert_tag_parses(tag, 0,
"a8d447f68076d1520f69649bb52629941be7031f",
"tagname",
"Taggy Mr. Taggart <[email protected]>",
NULL);
}
void test_object_tag_parse__unknown_field_parses(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"foo bar\n"
"frubble frabble\n"
"\n"
"Message";
assert_tag_parses(tag, 0,
"a8d447f68076d1520f69649bb52629941be7031f",
"tagname",
"Taggy Mr. Taggart <[email protected]>",
"Message");
}
void test_object_tag_parse__missing_object_fails(void)
{
const char *tag =
"type tag\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"\n"
"Message";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__malformatted_object_fails(void)
{
const char *tag =
"object a8d447f68076d15xxxxxxxxxxxxxxxx41be7031f\n"
"type tag\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"\n"
"Message";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__missing_type_fails(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"\n"
"Message";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__invalid_type_fails(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type garbage\n"
"tag tagname\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"\n"
"Message";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__missing_tagname_fails(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tagger Taggy Mr. Taggart <[email protected]>\n"
"\n"
"Message";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__misformatted_tagger_fails(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag Tag\n"
"tagger [email protected]>\n"
"\n"
"Message";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__missing_message_fails(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag Tag\n"
"tagger [email protected]>\n";
assert_tag_fails(tag, 0);
}
void test_object_tag_parse__no_oob_read_when_searching_message(void)
{
const char *tag =
"object a8d447f68076d1520f69649bb52629941be7031f\n"
"type tag\n"
"tag \n"
"tagger <>\n"
" \n\n"
"Message";
/*
* The OOB read previously resulted in an OOM error. We
* thus want to make sure that the resulting error is the
* expected one.
*/
assert_tag_fails(tag, strlen(tag) - strlen("\n\nMessage"));
cl_assert(strstr(git_error_last()->message, "tag contains no message"));
}
| libgit2-main | tests/libgit2/object/tag/parse.c |