python_code
stringlengths 0
1.8M
| repo_name
stringclasses 7
values | file_path
stringlengths 5
99
|
---|---|---|
#include "clar_libgit2.h"
static const char* tagger_name = "Vicent Marti";
static const char* tagger_email = "[email protected]";
static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n";
static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static git_repository *g_repo;
/* Fixture setup and teardown */
void test_object_tag_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_object_tag_write__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_object_tag_write__basic(void)
{
/* write a tag to the repository and read it again */
git_tag *tag;
git_oid target_id, tag_id;
git_signature *tagger;
const git_signature *tagger1;
git_reference *ref_tag;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
/* create signature */
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_git_pass(
git_tag_create(&tag_id, g_repo,
"the-tag", target, tagger, tagger_message, 0)
);
git_object_free(target);
git_signature_free(tagger);
cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
cl_assert(git_oid_cmp(git_tag_target_id(tag), &target_id) == 0);
/* Check attributes were set correctly */
tagger1 = git_tag_tagger(tag);
cl_assert(tagger1 != NULL);
cl_assert_equal_s(tagger1->name, tagger_name);
cl_assert_equal_s(tagger1->email, tagger_email);
cl_assert(tagger1->when.time == 123456789);
cl_assert(tagger1->when.offset == 60);
cl_assert_equal_s(git_tag_message(tag), tagger_message);
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
cl_git_pass(git_reference_delete(ref_tag));
git_reference_free(ref_tag);
git_tag_free(tag);
}
void test_object_tag_write__overwrite(void)
{
/* Attempt to write a tag bearing the same name than an already existing tag */
git_oid target_id, tag_id;
git_signature *tagger;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
/* create signature */
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_assert_equal_i(GIT_EEXISTS, git_tag_create(
&tag_id, /* out id */
g_repo,
"e90810b",
target,
tagger,
tagger_message,
0));
git_object_free(target);
git_signature_free(tagger);
}
void test_object_tag_write__replace(void)
{
/* Replace an already existing tag */
git_oid target_id, tag_id, old_tag_id;
git_signature *tagger;
git_reference *ref_tag;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
git_oid_cpy(&old_tag_id, git_reference_target(ref_tag));
git_reference_free(ref_tag);
/* create signature */
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_git_pass(git_tag_create(
&tag_id, /* out id */
g_repo,
"e90810b",
target,
tagger,
tagger_message,
1));
git_object_free(target);
git_signature_free(tagger);
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
cl_assert(git_oid_cmp(git_reference_target(ref_tag), &old_tag_id) != 0);
git_reference_free(ref_tag);
}
void test_object_tag_write__lightweight(void)
{
/* write a lightweight tag to the repository and read it again */
git_oid target_id, object_id;
git_reference *ref_tag;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_tag_create_lightweight(
&object_id,
g_repo,
"light-tag",
target,
0));
git_object_free(target);
cl_assert(git_oid_cmp(&object_id, &target_id) == 0);
cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag"));
cl_assert(git_oid_cmp(git_reference_target(ref_tag), &target_id) == 0);
cl_git_pass(git_tag_delete(g_repo, "light-tag"));
git_reference_free(ref_tag);
}
void test_object_tag_write__lightweight_over_existing(void)
{
/* Attempt to write a lightweight tag bearing the same name than an already existing tag */
git_oid target_id, object_id, existing_object_id;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
cl_assert_equal_i(GIT_EEXISTS, git_tag_create_lightweight(
&object_id,
g_repo,
"e90810b",
target,
0));
git_oid__fromstr(&existing_object_id, tag2_id, GIT_OID_SHA1);
cl_assert(git_oid_cmp(&object_id, &existing_object_id) == 0);
git_object_free(target);
}
void test_object_tag_write__delete(void)
{
/* Delete an already existing tag */
git_reference *ref_tag;
cl_git_pass(git_tag_delete(g_repo, "e90810b"));
cl_git_fail(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
git_reference_free(ref_tag);
}
void test_object_tag_write__creating_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
git_oid target_id, tag_id;
git_signature *tagger;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_tag_create(&tag_id, g_repo,
"Inv@{id", target, tagger, tagger_message, 0)
);
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_tag_create_lightweight(&tag_id, g_repo,
"Inv@{id", target, 0)
);
git_object_free(target);
git_signature_free(tagger);
}
void test_object_tag_write__deleting_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC, git_tag_delete(g_repo, "Inv@{id"));
}
static void create_annotation(git_oid *tag_id, const char *name)
{
git_object *target;
git_oid target_id;
git_signature *tagger;
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_tag_annotation_create(tag_id, g_repo, name, target, tagger, "boom!"));
git_object_free(target);
git_signature_free(tagger);
}
void test_object_tag_write__creating_an_annotation_stores_the_new_object_in_the_odb(void)
{
git_oid tag_id;
git_tag *tag;
create_annotation(&tag_id, "new_tag");
cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
cl_assert_equal_s("new_tag", git_tag_name(tag));
git_tag_free(tag);
}
void test_object_tag_write__creating_an_annotation_does_not_create_a_reference(void)
{
git_oid tag_id;
git_reference *tag_ref;
create_annotation(&tag_id, "new_tag");
cl_git_fail_with(git_reference_lookup(&tag_ref, g_repo, "refs/tags/new_tag"), GIT_ENOTFOUND);
}
void test_object_tag_write__error_when_create_tag_with_invalid_name(void)
{
git_oid target_id, tag_id;
git_signature *tagger;
git_object *target;
git_oid__fromstr(&target_id, tagged_commit, GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
cl_git_fail(
git_tag_create(&tag_id, g_repo,
"-dash", target, tagger, tagger_message, 0)
);
git_object_free(target);
git_signature_free(tagger);
}
| libgit2-main | tests/libgit2/object/tag/write.c |
#include "clar_libgit2.h"
#include "odb.h"
void test_object_raw_compare__succeed_on_copy_oid(void)
{
git_oid a, b;
unsigned char exp[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
memset(&b, 0, sizeof(b));
git_oid__fromraw(&a, exp, GIT_OID_SHA1);
git_oid_cpy(&b, &a);
cl_git_pass(memcmp(a.id, exp, GIT_OID_SHA1_SIZE));
}
void test_object_raw_compare__succeed_on_oid_comparison_lesser(void)
{
git_oid a, b;
unsigned char a_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
unsigned char b_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xf0,
};
git_oid__fromraw(&a, a_in, GIT_OID_SHA1);
git_oid__fromraw(&b, b_in, GIT_OID_SHA1);
cl_assert(git_oid_cmp(&a, &b) < 0);
}
void test_object_raw_compare__succeed_on_oid_comparison_equal(void)
{
git_oid a, b;
unsigned char a_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
git_oid__fromraw(&a, a_in, GIT_OID_SHA1);
git_oid__fromraw(&b, a_in, GIT_OID_SHA1);
cl_assert(git_oid_cmp(&a, &b) == 0);
}
void test_object_raw_compare__succeed_on_oid_comparison_greater(void)
{
git_oid a, b;
unsigned char a_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
unsigned char b_in[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xd0,
};
git_oid__fromraw(&a, a_in, GIT_OID_SHA1);
git_oid__fromraw(&b, b_in, GIT_OID_SHA1);
cl_assert(git_oid_cmp(&a, &b) > 0);
}
void test_object_raw_compare__compare_fmt_oids(void)
{
const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
git_oid in;
char out[GIT_OID_SHA1_HEXSIZE + 1];
cl_git_pass(git_oid__fromstr(&in, exp, GIT_OID_SHA1));
/* Format doesn't touch the last byte */
out[GIT_OID_SHA1_HEXSIZE] = 'Z';
git_oid_fmt(out, &in);
cl_assert(out[GIT_OID_SHA1_HEXSIZE] == 'Z');
/* Format produced the right result */
out[GIT_OID_SHA1_HEXSIZE] = '\0';
cl_assert_equal_s(exp, out);
}
void test_object_raw_compare__compare_static_oids(void)
{
const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
git_oid in;
char *out;
cl_git_pass(git_oid__fromstr(&in, exp, GIT_OID_SHA1));
out = git_oid_tostr_s(&in);
cl_assert(out);
cl_assert_equal_s(exp, out);
}
void test_object_raw_compare__compare_pathfmt_oids(void)
{
const char *exp1 = "16a0123456789abcdef4b775213c23a8bd74f5e0";
const char *exp2 = "16/a0123456789abcdef4b775213c23a8bd74f5e0";
git_oid in;
char out[GIT_OID_SHA1_HEXSIZE + 2];
cl_git_pass(git_oid__fromstr(&in, exp1, GIT_OID_SHA1));
/* Format doesn't touch the last byte */
out[GIT_OID_SHA1_HEXSIZE + 1] = 'Z';
git_oid_pathfmt(out, &in);
cl_assert(out[GIT_OID_SHA1_HEXSIZE + 1] == 'Z');
/* Format produced the right result */
out[GIT_OID_SHA1_HEXSIZE + 1] = '\0';
cl_assert_equal_s(exp2, out);
}
| libgit2-main | tests/libgit2/object/raw/compare.c |
#include "clar_libgit2.h"
#include "odb.h"
void test_object_raw_convert__succeed_on_oid_to_string_conversion(void)
{
const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
git_oid in;
char out[GIT_OID_SHA1_HEXSIZE + 1];
char *str;
int i;
cl_git_pass(git_oid__fromstr(&in, exp, GIT_OID_SHA1));
/* NULL buffer pointer, returns static empty string */
str = git_oid_tostr(NULL, sizeof(out), &in);
cl_assert(str && *str == '\0' && str != out);
/* zero buffer size, returns static empty string */
str = git_oid_tostr(out, 0, &in);
cl_assert(str && *str == '\0' && str != out);
/* NULL oid pointer, sets existing buffer to empty string */
str = git_oid_tostr(out, sizeof(out), NULL);
cl_assert(str && *str == '\0' && str == out);
/* n == 1, returns out as an empty string */
str = git_oid_tostr(out, 1, &in);
cl_assert(str && *str == '\0' && str == out);
for (i = 1; i < GIT_OID_SHA1_HEXSIZE; i++) {
out[i+1] = 'Z';
str = git_oid_tostr(out, i+1, &in);
/* returns out containing c-string */
cl_assert(str && str == out);
/* must be '\0' terminated */
cl_assert(*(str+i) == '\0');
/* must not touch bytes past end of string */
cl_assert(*(str+(i+1)) == 'Z');
/* i == n-1 characters of string */
cl_git_pass(strncmp(exp, out, i));
}
/* returns out as hex formatted c-string */
str = git_oid_tostr(out, sizeof(out), &in);
cl_assert(str && str == out && *(str+GIT_OID_SHA1_HEXSIZE) == '\0');
cl_assert_equal_s(exp, out);
}
void test_object_raw_convert__succeed_on_oid_to_string_conversion_big(void)
{
const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
git_oid in;
char big[GIT_OID_SHA1_HEXSIZE + 1 + 3]; /* note + 4 => big buffer */
char *str;
cl_git_pass(git_oid__fromstr(&in, exp, GIT_OID_SHA1));
/* place some tail material */
big[GIT_OID_SHA1_HEXSIZE+0] = 'W'; /* should be '\0' afterwards */
big[GIT_OID_SHA1_HEXSIZE+1] = 'X'; /* should remain untouched */
big[GIT_OID_SHA1_HEXSIZE+2] = 'Y'; /* ditto */
big[GIT_OID_SHA1_HEXSIZE+3] = 'Z'; /* ditto */
/* returns big as hex formatted c-string */
str = git_oid_tostr(big, sizeof(big), &in);
cl_assert(str && str == big && *(str+GIT_OID_SHA1_HEXSIZE) == '\0');
cl_assert_equal_s(exp, big);
/* check tail material is untouched */
cl_assert(str && str == big && *(str+GIT_OID_SHA1_HEXSIZE+1) == 'X');
cl_assert(str && str == big && *(str+GIT_OID_SHA1_HEXSIZE+2) == 'Y');
cl_assert(str && str == big && *(str+GIT_OID_SHA1_HEXSIZE+3) == 'Z');
}
static void check_partial_oid(
char *buffer, size_t count, const git_oid *oid, const char *expected)
{
git_oid_nfmt(buffer, count, oid);
buffer[count] = '\0';
cl_assert_equal_s(expected, buffer);
}
void test_object_raw_convert__convert_oid_partially(void)
{
const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0";
git_oid in;
char big[GIT_OID_SHA1_HEXSIZE + 1 + 3]; /* note + 4 => big buffer */
cl_git_pass(git_oid__fromstr(&in, exp, GIT_OID_SHA1));
git_oid_nfmt(big, sizeof(big), &in);
cl_assert_equal_s(exp, big);
git_oid_nfmt(big, GIT_OID_SHA1_HEXSIZE + 1, &in);
cl_assert_equal_s(exp, big);
check_partial_oid(big, 1, &in, "1");
check_partial_oid(big, 2, &in, "16");
check_partial_oid(big, 3, &in, "16a");
check_partial_oid(big, 4, &in, "16a0");
check_partial_oid(big, 5, &in, "16a01");
check_partial_oid(big, GIT_OID_SHA1_HEXSIZE, &in, exp);
check_partial_oid(
big, GIT_OID_SHA1_HEXSIZE - 1, &in, "16a0123456789abcdef4b775213c23a8bd74f5e");
check_partial_oid(
big, GIT_OID_SHA1_HEXSIZE - 2, &in, "16a0123456789abcdef4b775213c23a8bd74f5");
check_partial_oid(
big, GIT_OID_SHA1_HEXSIZE - 3, &in, "16a0123456789abcdef4b775213c23a8bd74f");
}
| libgit2-main | tests/libgit2/object/raw/convert.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "hash.h"
void test_object_raw_type2string__convert_type_to_string(void)
{
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_INVALID), "");
cl_assert_equal_s(git_object_type2string(0), ""); /* EXT1 */
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_COMMIT), "commit");
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_TREE), "tree");
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_BLOB), "blob");
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_TAG), "tag");
cl_assert_equal_s(git_object_type2string(5), ""); /* EXT2 */
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_OFS_DELTA), "OFS_DELTA");
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_REF_DELTA), "REF_DELTA");
cl_assert_equal_s(git_object_type2string(-2), "");
cl_assert_equal_s(git_object_type2string(8), "");
cl_assert_equal_s(git_object_type2string(1234), "");
}
void test_object_raw_type2string__convert_string_to_type(void)
{
cl_assert(git_object_string2type(NULL) == GIT_OBJECT_INVALID);
cl_assert(git_object_string2type("") == GIT_OBJECT_INVALID);
cl_assert(git_object_string2type("commit") == GIT_OBJECT_COMMIT);
cl_assert(git_object_string2type("tree") == GIT_OBJECT_TREE);
cl_assert(git_object_string2type("blob") == GIT_OBJECT_BLOB);
cl_assert(git_object_string2type("tag") == GIT_OBJECT_TAG);
cl_assert(git_object_string2type("OFS_DELTA") == GIT_OBJECT_OFS_DELTA);
cl_assert(git_object_string2type("REF_DELTA") == GIT_OBJECT_REF_DELTA);
cl_assert(git_object_string2type("CoMmIt") == GIT_OBJECT_INVALID);
cl_assert(git_object_string2type("hohoho") == GIT_OBJECT_INVALID);
}
void test_object_raw_type2string__check_type_is_loose(void)
{
cl_assert(git_object_typeisloose(GIT_OBJECT_INVALID) == 0);
cl_assert(git_object_typeisloose(0) == 0); /* EXT1 */
cl_assert(git_object_typeisloose(GIT_OBJECT_COMMIT) == 1);
cl_assert(git_object_typeisloose(GIT_OBJECT_TREE) == 1);
cl_assert(git_object_typeisloose(GIT_OBJECT_BLOB) == 1);
cl_assert(git_object_typeisloose(GIT_OBJECT_TAG) == 1);
cl_assert(git_object_typeisloose(5) == 0); /* EXT2 */
cl_assert(git_object_typeisloose(GIT_OBJECT_OFS_DELTA) == 0);
cl_assert(git_object_typeisloose(GIT_OBJECT_REF_DELTA) == 0);
cl_assert(git_object_typeisloose(-2) == 0);
cl_assert(git_object_typeisloose(8) == 0);
cl_assert(git_object_typeisloose(1234) == 0);
}
| libgit2-main | tests/libgit2/object/raw/type2string.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "hash.h"
#include "data.h"
static void hash_object_pass(git_oid *oid, git_rawobj *obj)
{
cl_git_pass(git_odb__hash(oid, obj->data, obj->len, obj->type, GIT_OID_SHA1));
}
static void hash_object_fail(git_oid *oid, git_rawobj *obj)
{
cl_git_fail(git_odb__hash(oid, obj->data, obj->len, obj->type, GIT_OID_SHA1));
}
static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511";
static char *hello_text = "hello world\n";
static char *bye_id = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
static char *bye_text = "bye world\n";
void test_object_raw_hash__hash_by_blocks(void)
{
git_hash_ctx ctx;
unsigned char hash[GIT_HASH_SHA1_SIZE];
git_oid id1, id2;
cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
/* should already be init'd */
cl_git_pass(git_hash_update(&ctx, hello_text, strlen(hello_text)));
cl_git_pass(git_hash_final(hash, &ctx));
cl_git_pass(git_oid__fromraw(&id2, hash, GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&id1, hello_id, GIT_OID_SHA1));
cl_assert(git_oid_cmp(&id1, &id2) == 0);
/* reinit should permit reuse */
cl_git_pass(git_hash_init(&ctx));
cl_git_pass(git_hash_update(&ctx, bye_text, strlen(bye_text)));
cl_git_pass(git_hash_final(hash, &ctx));
cl_git_pass(git_oid__fromraw(&id2, hash, GIT_OID_SHA1));
cl_git_pass(git_oid__fromstr(&id1, bye_id, GIT_OID_SHA1));
cl_assert(git_oid_cmp(&id1, &id2) == 0);
git_hash_ctx_cleanup(&ctx);
}
void test_object_raw_hash__hash_buffer_in_single_call(void)
{
git_oid id1, id2;
unsigned char hash[GIT_HASH_SHA1_SIZE];
cl_git_pass(git_oid__fromstr(&id1, hello_id, GIT_OID_SHA1));
cl_git_pass(git_hash_buf(hash, hello_text, strlen(hello_text), GIT_HASH_ALGORITHM_SHA1));
cl_git_pass(git_oid__fromraw(&id2, hash, GIT_OID_SHA1));
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_vector(void)
{
git_oid id1, id2;
git_str_vec vec[2];
unsigned char hash[GIT_HASH_SHA1_SIZE];
cl_git_pass(git_oid__fromstr(&id1, hello_id, GIT_OID_SHA1));
vec[0].data = hello_text;
vec[0].len = 4;
vec[1].data = hello_text+4;
vec[1].len = strlen(hello_text)-4;
git_hash_vec(hash, vec, 2, GIT_HASH_ALGORITHM_SHA1);
git_oid__fromraw(&id2, hash, GIT_OID_SHA1);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_junk_data(void)
{
git_oid id, id_zero;
cl_git_pass(git_oid__fromstr(&id_zero, zero_id, GIT_OID_SHA1));
/* invalid types: */
junk_obj.data = some_data;
hash_object_fail(&id, &junk_obj);
junk_obj.type = 0; /* EXT1 */
hash_object_fail(&id, &junk_obj);
junk_obj.type = 5; /* EXT2 */
hash_object_fail(&id, &junk_obj);
junk_obj.type = GIT_OBJECT_OFS_DELTA;
hash_object_fail(&id, &junk_obj);
junk_obj.type = GIT_OBJECT_REF_DELTA;
hash_object_fail(&id, &junk_obj);
junk_obj.type = 42;
hash_object_fail(&id, &junk_obj);
/* data can be NULL only if len is zero: */
junk_obj.type = GIT_OBJECT_BLOB;
junk_obj.data = NULL;
hash_object_pass(&id, &junk_obj);
cl_assert(git_oid_cmp(&id, &id_zero) == 0);
junk_obj.len = 1;
hash_object_fail(&id, &junk_obj);
}
void test_object_raw_hash__hash_commit_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, commit_id, GIT_OID_SHA1));
hash_object_pass(&id2, &commit_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_tree_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, tree_id, GIT_OID_SHA1));
hash_object_pass(&id2, &tree_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_tag_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, tag_id, GIT_OID_SHA1));
hash_object_pass(&id2, &tag_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_zero_length_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, zero_id, GIT_OID_SHA1));
hash_object_pass(&id2, &zero_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_one_byte_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, one_id, GIT_OID_SHA1));
hash_object_pass(&id2, &one_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_two_byte_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, two_id, GIT_OID_SHA1));
hash_object_pass(&id2, &two_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
void test_object_raw_hash__hash_multi_byte_object(void)
{
git_oid id1, id2;
cl_git_pass(git_oid__fromstr(&id1, some_id, GIT_OID_SHA1));
hash_object_pass(&id2, &some_obj);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
}
| libgit2-main | tests/libgit2/object/raw/hash.c |
#include "clar_libgit2.h"
#include "odb.h"
void test_object_raw_chars__find_invalid_chars_in_oid(void)
{
git_oid out;
unsigned char exp[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
char in[] = "16a67770b7d8d72317c4b775213c23a8bd74f5e0";
unsigned int i;
for (i = 0; i < 256; i++) {
in[38] = (char)i;
if (git__fromhex(i) >= 0) {
exp[19] = (unsigned char)(git__fromhex(i) << 4);
cl_git_pass(git_oid__fromstr(&out, in, GIT_OID_SHA1));
cl_assert(memcmp(out.id, exp, GIT_OID_SHA1_SIZE) == 0);
} else {
cl_git_fail(git_oid__fromstr(&out, in, GIT_OID_SHA1));
}
}
}
void test_object_raw_chars__build_valid_oid_from_raw_bytes(void)
{
git_oid out;
unsigned char exp[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
git_oid__fromraw(&out, exp, GIT_OID_SHA1);
cl_git_pass(memcmp(out.id, exp, GIT_OID_SHA1_SIZE));
}
| libgit2-main | tests/libgit2/object/raw/chars.c |
#include "clar_libgit2.h"
#include "odb.h"
void test_object_raw_fromstr__fail_on_invalid_oid_string(void)
{
git_oid out;
cl_git_fail(git_oid__fromstr(&out, "", GIT_OID_SHA1));
cl_git_fail(git_oid__fromstr(&out, "moo", GIT_OID_SHA1));
cl_git_fail(git_oid__fromstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5ez", GIT_OID_SHA1));
}
void test_object_raw_fromstr__succeed_on_valid_oid_string(void)
{
git_oid out;
unsigned char exp[] = {
0x16, 0xa6, 0x77, 0x70, 0xb7,
0xd8, 0xd7, 0x23, 0x17, 0xc4,
0xb7, 0x75, 0x21, 0x3c, 0x23,
0xa8, 0xbd, 0x74, 0xf5, 0xe0,
};
cl_git_pass(git_oid__fromstr(&out, "16a67770b7d8d72317c4b775213c23a8bd74f5e0", GIT_OID_SHA1));
cl_git_pass(memcmp(out.id, exp, GIT_OID_SHA1_SIZE));
cl_git_pass(git_oid__fromstr(&out, "16A67770B7D8D72317C4b775213C23A8BD74F5E0", GIT_OID_SHA1));
cl_git_pass(memcmp(out.id, exp, GIT_OID_SHA1_SIZE));
}
| libgit2-main | tests/libgit2/object/raw/fromstr.c |
#include "clar_libgit2.h"
#include "odb.h"
void test_object_raw_size__validate_oid_size(void)
{
git_oid out;
cl_assert(20 == GIT_OID_SHA1_SIZE);
cl_assert(40 == GIT_OID_SHA1_HEXSIZE);
cl_assert(sizeof(out.id) == GIT_OID_MAX_SIZE);
}
| libgit2-main | tests/libgit2/object/raw/size.c |
#include "clar_libgit2.h"
#include "odb.h"
#include "hash.h"
void test_object_raw_short__oid_shortener_no_duplicates(void)
{
git_oid_shorten *os;
int min_len;
os = git_oid_shorten_new(0);
cl_assert(os != NULL);
git_oid_shorten_add(os, "22596363b3de40b06f981fb85d82312e8c0ed511");
git_oid_shorten_add(os, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
git_oid_shorten_add(os, "16a0123456789abcdef4b775213c23a8bd74f5e0");
min_len = git_oid_shorten_add(os, "ce08fe4884650f067bd5703b6a59a8b3b3c99a09");
cl_assert(min_len == GIT_OID_SHA1_HEXSIZE + 1);
git_oid_shorten_free(os);
}
static int insert_sequential_oids(
char ***out, git_oid_shorten *os, int n, int fail)
{
int i, min_len = 0;
char numbuf[16];
git_oid oid;
unsigned char hashbuf[GIT_HASH_SHA1_SIZE];
char **oids = git__calloc(n, sizeof(char *));
cl_assert(oids != NULL);
for (i = 0; i < n; ++i) {
p_snprintf(numbuf, sizeof(numbuf), "%u", (unsigned int)i);
git_hash_buf(hashbuf, numbuf, strlen(numbuf), GIT_HASH_ALGORITHM_SHA1);
git_oid__fromraw(&oid, hashbuf, GIT_OID_SHA1);
oids[i] = git__malloc(GIT_OID_SHA1_HEXSIZE + 1);
cl_assert(oids[i]);
git_oid_nfmt(oids[i], GIT_OID_SHA1_HEXSIZE + 1, &oid);
min_len = git_oid_shorten_add(os, oids[i]);
/* After "fail", we expect git_oid_shorten_add to fail */
if (fail >= 0 && i >= fail)
cl_assert(min_len < 0);
else
cl_assert(min_len >= 0);
}
*out = oids;
return min_len;
}
static void free_oids(int n, char **oids)
{
int i;
for (i = 0; i < n; ++i) {
git__free(oids[i]);
}
git__free(oids);
}
void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
{
#define MAX_OIDS 1000
git_oid_shorten *os;
size_t i, j;
int min_len = 0, found_collision;
char **oids;
os = git_oid_shorten_new(0);
cl_assert(os != NULL);
/*
* Insert in the shortener 1000 unique SHA1 ids
*/
min_len = insert_sequential_oids(&oids, os, MAX_OIDS, MAX_OIDS);
cl_assert(min_len > 0);
/*
* Compare the first `min_char - 1` characters of each
* SHA1 OID. If the minimizer worked, we should find at
* least one collision
*/
found_collision = 0;
for (i = 0; i < MAX_OIDS; ++i) {
for (j = i + 1; j < MAX_OIDS; ++j) {
if (memcmp(oids[i], oids[j], min_len - 1) == 0)
found_collision = 1;
}
}
cl_assert_equal_b(true, found_collision);
/*
* Compare the first `min_char` characters of each
* SHA1 OID. If the minimizer worked, every single preffix
* should be unique.
*/
found_collision = 0;
for (i = 0; i < MAX_OIDS; ++i) {
for (j = i + 1; j < MAX_OIDS; ++j) {
if (memcmp(oids[i], oids[j], min_len) == 0)
found_collision = 1;
}
}
cl_assert_equal_b(false, found_collision);
/* cleanup */
free_oids(MAX_OIDS, oids);
git_oid_shorten_free(os);
#undef MAX_OIDS
}
void test_object_raw_short__oid_shortener_too_much_oids(void)
{
/* The magic number of oids at which an oid_shortener will fail.
* This was experimentally established. */
#define MAX_OIDS 24556
git_oid_shorten *os;
char **oids;
os = git_oid_shorten_new(0);
cl_assert(os != NULL);
cl_assert(insert_sequential_oids(&oids, os, MAX_OIDS, MAX_OIDS - 1) < 0);
free_oids(MAX_OIDS, oids);
git_oid_shorten_free(os);
#undef MAX_OIDS
}
| libgit2-main | tests/libgit2/object/raw/short.c |
#include "clar_libgit2.h"
#include "git2/odb_backend.h"
#include "futils.h"
#include "odb.h"
typedef struct object_data {
char *id; /* object id (sha1) */
char *dir; /* object store (fan-out) directory name */
char *file; /* object store filename */
} object_data;
static const char *odb_dir = "test-objects";
void test_body(object_data *d, git_rawobj *o);
/* Helpers */
static void remove_object_files(object_data *d)
{
cl_git_pass(p_unlink(d->file));
cl_git_pass(p_rmdir(d->dir));
cl_assert(errno != ENOTEMPTY);
cl_git_pass(p_rmdir(odb_dir) < 0);
}
static void streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw)
{
git_odb_stream *stream;
int error;
cl_git_pass(git_odb_open_wstream(&stream, odb, raw->len, raw->type));
git_odb_stream_write(stream, raw->data, raw->len);
error = git_odb_stream_finalize_write(oid, stream);
git_odb_stream_free(stream);
cl_git_pass(error);
}
static void check_object_files(object_data *d)
{
cl_assert(git_fs_path_exists(d->dir));
cl_assert(git_fs_path_exists(d->file));
}
static void cmp_objects(git_rawobj *o1, git_rawobj *o2)
{
cl_assert(o1->type == o2->type);
cl_assert(o1->len == o2->len);
if (o1->len > 0)
cl_assert(memcmp(o1->data, o2->data, o1->len) == 0);
}
static void make_odb_dir(void)
{
cl_git_pass(p_mkdir(odb_dir, GIT_OBJECT_DIR_MODE));
}
/* Standard test form */
void test_body(object_data *d, git_rawobj *o)
{
git_odb *db;
git_oid id1, id2;
git_odb_object *obj;
git_rawobj tmp;
make_odb_dir();
cl_git_pass(git_odb__open(&db, odb_dir, NULL));
cl_git_pass(git_oid__fromstr(&id1, d->id, GIT_OID_SHA1));
streaming_write(&id2, db, o);
cl_assert(git_oid_cmp(&id1, &id2) == 0);
check_object_files(d);
cl_git_pass(git_odb_read(&obj, db, &id1));
tmp.data = obj->buffer;
tmp.len = obj->cached.size;
tmp.type = obj->cached.type;
cmp_objects(&tmp, o);
git_odb_object_free(obj);
git_odb_free(db);
remove_object_files(d);
}
void test_object_raw_write__loose_object(void)
{
object_data commit = {
"3d7f8a6af076c8c3f20071a8935cdbe8228594d1",
"test-objects/3d",
"test-objects/3d/7f8a6af076c8c3f20071a8935cdbe8228594d1",
};
unsigned char commit_data[] = {
0x74, 0x72, 0x65, 0x65, 0x20, 0x64, 0x66, 0x66,
0x32, 0x64, 0x61, 0x39, 0x30, 0x62, 0x32, 0x35,
0x34, 0x65, 0x31, 0x62, 0x65, 0x62, 0x38, 0x38,
0x39, 0x64, 0x31, 0x66, 0x31, 0x66, 0x31, 0x32,
0x38, 0x38, 0x62, 0x65, 0x31, 0x38, 0x30, 0x33,
0x37, 0x38, 0x32, 0x64, 0x66, 0x0a, 0x61, 0x75,
0x74, 0x68, 0x6f, 0x72, 0x20, 0x41, 0x20, 0x55,
0x20, 0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61,
0x75, 0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
0x6d, 0x3e, 0x20, 0x31, 0x32, 0x32, 0x37, 0x38,
0x31, 0x34, 0x32, 0x39, 0x37, 0x20, 0x2b, 0x30,
0x30, 0x30, 0x30, 0x0a, 0x63, 0x6f, 0x6d, 0x6d,
0x69, 0x74, 0x74, 0x65, 0x72, 0x20, 0x43, 0x20,
0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72,
0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e,
0x20, 0x31, 0x32, 0x32, 0x37, 0x38, 0x31, 0x34,
0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, 0x30, 0x30,
0x30, 0x0a, 0x0a, 0x41, 0x20, 0x6f, 0x6e, 0x65,
0x2d, 0x6c, 0x69, 0x6e, 0x65, 0x20, 0x63, 0x6f,
0x6d, 0x6d, 0x69, 0x74, 0x20, 0x73, 0x75, 0x6d,
0x6d, 0x61, 0x72, 0x79, 0x0a, 0x0a, 0x54, 0x68,
0x65, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x6f,
0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f,
0x6d, 0x6d, 0x69, 0x74, 0x20, 0x6d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x2c, 0x20, 0x63, 0x6f,
0x6e, 0x74, 0x61, 0x69, 0x6e, 0x69, 0x6e, 0x67,
0x20, 0x66, 0x75, 0x72, 0x74, 0x68, 0x65, 0x72,
0x20, 0x65, 0x78, 0x70, 0x6c, 0x61, 0x6e, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x6f, 0x66, 0x20,
0x74, 0x68, 0x65, 0x20, 0x70, 0x75, 0x72, 0x70,
0x6f, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x74,
0x68, 0x65, 0x20, 0x63, 0x68, 0x61, 0x6e, 0x67,
0x65, 0x73, 0x20, 0x69, 0x6e, 0x74, 0x72, 0x6f,
0x64, 0x75, 0x63, 0x65, 0x64, 0x20, 0x62, 0x79,
0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6d,
0x6d, 0x69, 0x74, 0x2e, 0x0a, 0x0a, 0x53, 0x69,
0x67, 0x6e, 0x65, 0x64, 0x2d, 0x6f, 0x66, 0x2d,
0x62, 0x79, 0x3a, 0x20, 0x41, 0x20, 0x55, 0x20,
0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, 0x75,
0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, 0x61,
0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
0x3e, 0x0a,
};
git_rawobj commit_obj = {
commit_data,
sizeof(commit_data),
GIT_OBJECT_COMMIT
};
test_body(&commit, &commit_obj);
}
void test_object_raw_write__loose_tree(void)
{
static object_data tree = {
"dff2da90b254e1beb889d1f1f1288be1803782df",
"test-objects/df",
"test-objects/df/f2da90b254e1beb889d1f1f1288be1803782df",
};
static unsigned char tree_data[] = {
0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x6f,
0x6e, 0x65, 0x00, 0x8b, 0x13, 0x78, 0x91, 0x79,
0x1f, 0xe9, 0x69, 0x27, 0xad, 0x78, 0xe6, 0x4b,
0x0a, 0xad, 0x7b, 0xde, 0xd0, 0x8b, 0xdc, 0x31,
0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x73, 0x6f,
0x6d, 0x65, 0x00, 0xfd, 0x84, 0x30, 0xbc, 0x86,
0x4c, 0xfc, 0xd5, 0xf1, 0x0e, 0x55, 0x90, 0xf8,
0xa4, 0x47, 0xe0, 0x1b, 0x94, 0x2b, 0xfe, 0x31,
0x30, 0x30, 0x36, 0x34, 0x34, 0x20, 0x74, 0x77,
0x6f, 0x00, 0x78, 0x98, 0x19, 0x22, 0x61, 0x3b,
0x2a, 0xfb, 0x60, 0x25, 0x04, 0x2f, 0xf6, 0xbd,
0x87, 0x8a, 0xc1, 0x99, 0x4e, 0x85, 0x31, 0x30,
0x30, 0x36, 0x34, 0x34, 0x20, 0x7a, 0x65, 0x72,
0x6f, 0x00, 0xe6, 0x9d, 0xe2, 0x9b, 0xb2, 0xd1,
0xd6, 0x43, 0x4b, 0x8b, 0x29, 0xae, 0x77, 0x5a,
0xd8, 0xc2, 0xe4, 0x8c, 0x53, 0x91,
};
static git_rawobj tree_obj = {
tree_data,
sizeof(tree_data),
GIT_OBJECT_TREE
};
test_body(&tree, &tree_obj);
}
void test_object_raw_write__loose_tag(void)
{
static object_data tag = {
"09d373e1dfdc16b129ceec6dd649739911541e05",
"test-objects/09",
"test-objects/09/d373e1dfdc16b129ceec6dd649739911541e05",
};
static unsigned char tag_data[] = {
0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x20, 0x33,
0x64, 0x37, 0x66, 0x38, 0x61, 0x36, 0x61, 0x66,
0x30, 0x37, 0x36, 0x63, 0x38, 0x63, 0x33, 0x66,
0x32, 0x30, 0x30, 0x37, 0x31, 0x61, 0x38, 0x39,
0x33, 0x35, 0x63, 0x64, 0x62, 0x65, 0x38, 0x32,
0x32, 0x38, 0x35, 0x39, 0x34, 0x64, 0x31, 0x0a,
0x74, 0x79, 0x70, 0x65, 0x20, 0x63, 0x6f, 0x6d,
0x6d, 0x69, 0x74, 0x0a, 0x74, 0x61, 0x67, 0x20,
0x76, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x0a, 0x74,
0x61, 0x67, 0x67, 0x65, 0x72, 0x20, 0x43, 0x20,
0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72,
0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e,
0x20, 0x31, 0x32, 0x32, 0x37, 0x38, 0x31, 0x34,
0x32, 0x39, 0x37, 0x20, 0x2b, 0x30, 0x30, 0x30,
0x30, 0x0a, 0x0a, 0x54, 0x68, 0x69, 0x73, 0x20,
0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74,
0x61, 0x67, 0x20, 0x6f, 0x62, 0x6a, 0x65, 0x63,
0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x72, 0x65,
0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x76, 0x30,
0x2e, 0x30, 0x2e, 0x31, 0x0a,
};
static git_rawobj tag_obj = {
tag_data,
sizeof(tag_data),
GIT_OBJECT_TAG
};
test_body(&tag, &tag_obj);
}
void test_object_raw_write__zero_length(void)
{
static object_data zero = {
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"test-objects/e6",
"test-objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391",
};
static unsigned char zero_data[] = {
0x00 /* dummy data */
};
static git_rawobj zero_obj = {
zero_data,
0,
GIT_OBJECT_BLOB
};
test_body(&zero, &zero_obj);
}
void test_object_raw_write__one_byte(void)
{
static object_data one = {
"8b137891791fe96927ad78e64b0aad7bded08bdc",
"test-objects/8b",
"test-objects/8b/137891791fe96927ad78e64b0aad7bded08bdc",
};
static unsigned char one_data[] = {
0x0a,
};
static git_rawobj one_obj = {
one_data,
sizeof(one_data),
GIT_OBJECT_BLOB
};
test_body(&one, &one_obj);
}
void test_object_raw_write__two_byte(void)
{
static object_data two = {
"78981922613b2afb6025042ff6bd878ac1994e85",
"test-objects/78",
"test-objects/78/981922613b2afb6025042ff6bd878ac1994e85",
};
static unsigned char two_data[] = {
0x61, 0x0a,
};
static git_rawobj two_obj = {
two_data,
sizeof(two_data),
GIT_OBJECT_BLOB
};
test_body(&two, &two_obj);
}
void test_object_raw_write__several_bytes(void)
{
static object_data some = {
"fd8430bc864cfcd5f10e5590f8a447e01b942bfe",
"test-objects/fd",
"test-objects/fd/8430bc864cfcd5f10e5590f8a447e01b942bfe",
};
static unsigned char some_data[] = {
0x2f, 0x2a, 0x0a, 0x20, 0x2a, 0x20, 0x54, 0x68,
0x69, 0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20,
0x69, 0x73, 0x20, 0x66, 0x72, 0x65, 0x65, 0x20,
0x73, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65,
0x3b, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x63, 0x61,
0x6e, 0x20, 0x72, 0x65, 0x64, 0x69, 0x73, 0x74,
0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x69,
0x74, 0x20, 0x61, 0x6e, 0x64, 0x2f, 0x6f, 0x72,
0x20, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x79, 0x0a,
0x20, 0x2a, 0x20, 0x69, 0x74, 0x20, 0x75, 0x6e,
0x64, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20,
0x74, 0x65, 0x72, 0x6d, 0x73, 0x20, 0x6f, 0x66,
0x20, 0x74, 0x68, 0x65, 0x20, 0x47, 0x4e, 0x55,
0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x2c,
0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
0x20, 0x32, 0x2c, 0x0a, 0x20, 0x2a, 0x20, 0x61,
0x73, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x73,
0x68, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74,
0x68, 0x65, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20,
0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65,
0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x2e, 0x0a, 0x20, 0x2a, 0x0a,
0x20, 0x2a, 0x20, 0x49, 0x6e, 0x20, 0x61, 0x64,
0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x74,
0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x70, 0x65,
0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e,
0x73, 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65,
0x20, 0x47, 0x4e, 0x55, 0x20, 0x47, 0x65, 0x6e,
0x65, 0x72, 0x61, 0x6c, 0x20, 0x50, 0x75, 0x62,
0x6c, 0x69, 0x63, 0x20, 0x4c, 0x69, 0x63, 0x65,
0x6e, 0x73, 0x65, 0x2c, 0x0a, 0x20, 0x2a, 0x20,
0x74, 0x68, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68,
0x6f, 0x72, 0x73, 0x20, 0x67, 0x69, 0x76, 0x65,
0x20, 0x79, 0x6f, 0x75, 0x20, 0x75, 0x6e, 0x6c,
0x69, 0x6d, 0x69, 0x74, 0x65, 0x64, 0x20, 0x70,
0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f,
0x6e, 0x20, 0x74, 0x6f, 0x20, 0x6c, 0x69, 0x6e,
0x6b, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f,
0x6d, 0x70, 0x69, 0x6c, 0x65, 0x64, 0x0a, 0x20,
0x2a, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f,
0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x69,
0x73, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x20, 0x69,
0x6e, 0x74, 0x6f, 0x20, 0x63, 0x6f, 0x6d, 0x62,
0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73,
0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x6f, 0x74,
0x68, 0x65, 0x72, 0x20, 0x70, 0x72, 0x6f, 0x67,
0x72, 0x61, 0x6d, 0x73, 0x2c, 0x0a, 0x20, 0x2a,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x6f, 0x20,
0x64, 0x69, 0x73, 0x74, 0x72, 0x69, 0x62, 0x75,
0x74, 0x65, 0x20, 0x74, 0x68, 0x6f, 0x73, 0x65,
0x20, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x77, 0x69,
0x74, 0x68, 0x6f, 0x75, 0x74, 0x20, 0x61, 0x6e,
0x79, 0x20, 0x72, 0x65, 0x73, 0x74, 0x72, 0x69,
0x63, 0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x20, 0x2a,
0x20, 0x63, 0x6f, 0x6d, 0x69, 0x6e, 0x67, 0x20,
0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65,
0x20, 0x75, 0x73, 0x65, 0x20, 0x6f, 0x66, 0x20,
0x74, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c,
0x65, 0x2e, 0x20, 0x20, 0x28, 0x54, 0x68, 0x65,
0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x0a,
0x20, 0x2a, 0x20, 0x72, 0x65, 0x73, 0x74, 0x72,
0x69, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20,
0x64, 0x6f, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x79,
0x20, 0x69, 0x6e, 0x20, 0x6f, 0x74, 0x68, 0x65,
0x72, 0x20, 0x72, 0x65, 0x73, 0x70, 0x65, 0x63,
0x74, 0x73, 0x3b, 0x20, 0x66, 0x6f, 0x72, 0x20,
0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2c,
0x20, 0x74, 0x68, 0x65, 0x79, 0x20, 0x63, 0x6f,
0x76, 0x65, 0x72, 0x0a, 0x20, 0x2a, 0x20, 0x6d,
0x6f, 0x64, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74,
0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x2c,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x64, 0x69, 0x73,
0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x69, 0x6f,
0x6e, 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x6e,
0x6f, 0x74, 0x20, 0x6c, 0x69, 0x6e, 0x6b, 0x65,
0x64, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x0a, 0x20,
0x2a, 0x20, 0x61, 0x20, 0x63, 0x6f, 0x6d, 0x62,
0x69, 0x6e, 0x65, 0x64, 0x20, 0x65, 0x78, 0x65,
0x63, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2e,
0x29, 0x0a, 0x20, 0x2a, 0x0a, 0x20, 0x2a, 0x20,
0x54, 0x68, 0x69, 0x73, 0x20, 0x66, 0x69, 0x6c,
0x65, 0x20, 0x69, 0x73, 0x20, 0x64, 0x69, 0x73,
0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x64,
0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20,
0x68, 0x6f, 0x70, 0x65, 0x20, 0x74, 0x68, 0x61,
0x74, 0x20, 0x69, 0x74, 0x20, 0x77, 0x69, 0x6c,
0x6c, 0x20, 0x62, 0x65, 0x20, 0x75, 0x73, 0x65,
0x66, 0x75, 0x6c, 0x2c, 0x20, 0x62, 0x75, 0x74,
0x0a, 0x20, 0x2a, 0x20, 0x57, 0x49, 0x54, 0x48,
0x4f, 0x55, 0x54, 0x20, 0x41, 0x4e, 0x59, 0x20,
0x57, 0x41, 0x52, 0x52, 0x41, 0x4e, 0x54, 0x59,
0x3b, 0x20, 0x77, 0x69, 0x74, 0x68, 0x6f, 0x75,
0x74, 0x20, 0x65, 0x76, 0x65, 0x6e, 0x20, 0x74,
0x68, 0x65, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x69,
0x65, 0x64, 0x20, 0x77, 0x61, 0x72, 0x72, 0x61,
0x6e, 0x74, 0x79, 0x20, 0x6f, 0x66, 0x0a, 0x20,
0x2a, 0x20, 0x4d, 0x45, 0x52, 0x43, 0x48, 0x41,
0x4e, 0x54, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54,
0x59, 0x20, 0x6f, 0x72, 0x20, 0x46, 0x49, 0x54,
0x4e, 0x45, 0x53, 0x53, 0x20, 0x46, 0x4f, 0x52,
0x20, 0x41, 0x20, 0x50, 0x41, 0x52, 0x54, 0x49,
0x43, 0x55, 0x4c, 0x41, 0x52, 0x20, 0x50, 0x55,
0x52, 0x50, 0x4f, 0x53, 0x45, 0x2e, 0x20, 0x20,
0x53, 0x65, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20,
0x47, 0x4e, 0x55, 0x0a, 0x20, 0x2a, 0x20, 0x47,
0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x50,
0x75, 0x62, 0x6c, 0x69, 0x63, 0x20, 0x4c, 0x69,
0x63, 0x65, 0x6e, 0x73, 0x65, 0x20, 0x66, 0x6f,
0x72, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x64,
0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x2e, 0x0a,
0x20, 0x2a, 0x0a, 0x20, 0x2a, 0x20, 0x59, 0x6f,
0x75, 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64,
0x20, 0x68, 0x61, 0x76, 0x65, 0x20, 0x72, 0x65,
0x63, 0x65, 0x69, 0x76, 0x65, 0x64, 0x20, 0x61,
0x20, 0x63, 0x6f, 0x70, 0x79, 0x20, 0x6f, 0x66,
0x20, 0x74, 0x68, 0x65, 0x20, 0x47, 0x4e, 0x55,
0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
0x4c, 0x69, 0x63, 0x65, 0x6e, 0x73, 0x65, 0x0a,
0x20, 0x2a, 0x20, 0x61, 0x6c, 0x6f, 0x6e, 0x67,
0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x74, 0x68,
0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
0x61, 0x6d, 0x3b, 0x20, 0x73, 0x65, 0x65, 0x20,
0x74, 0x68, 0x65, 0x20, 0x66, 0x69, 0x6c, 0x65,
0x20, 0x43, 0x4f, 0x50, 0x59, 0x49, 0x4e, 0x47,
0x2e, 0x20, 0x20, 0x49, 0x66, 0x20, 0x6e, 0x6f,
0x74, 0x2c, 0x20, 0x77, 0x72, 0x69, 0x74, 0x65,
0x20, 0x74, 0x6f, 0x0a, 0x20, 0x2a, 0x20, 0x74,
0x68, 0x65, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20,
0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65,
0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x35, 0x31, 0x20,
0x46, 0x72, 0x61, 0x6e, 0x6b, 0x6c, 0x69, 0x6e,
0x20, 0x53, 0x74, 0x72, 0x65, 0x65, 0x74, 0x2c,
0x20, 0x46, 0x69, 0x66, 0x74, 0x68, 0x20, 0x46,
0x6c, 0x6f, 0x6f, 0x72, 0x2c, 0x0a, 0x20, 0x2a,
0x20, 0x42, 0x6f, 0x73, 0x74, 0x6f, 0x6e, 0x2c,
0x20, 0x4d, 0x41, 0x20, 0x30, 0x32, 0x31, 0x31,
0x30, 0x2d, 0x31, 0x33, 0x30, 0x31, 0x2c, 0x20,
0x55, 0x53, 0x41, 0x2e, 0x0a, 0x20, 0x2a, 0x2f,
0x0a,
};
static git_rawobj some_obj = {
some_data,
sizeof(some_data),
GIT_OBJECT_BLOB
};
test_body(&some, &some_obj);
}
| libgit2-main | tests/libgit2/object/raw/write.c |
#include "clar_libgit2.h"
#include "thread_helpers.h"
void run_in_parallel(
int repeats,
int threads,
void *(*func)(void *),
void (*before_test)(void),
void (*after_test)(void))
{
int r, t, *id = git__calloc(threads, sizeof(int));
#ifdef GIT_THREADS
git_thread *th = git__calloc(threads, sizeof(git_thread));
cl_assert(th != NULL);
#else
void *th = NULL;
#endif
cl_assert(id != NULL);
for (r = 0; r < repeats; ++r) {
if (before_test) before_test();
for (t = 0; t < threads; ++t) {
id[t] = t;
#ifdef GIT_THREADS
cl_git_pass(git_thread_create(&th[t], func, &id[t]));
#else
cl_assert(func(&id[t]) == &id[t]);
#endif
}
#ifdef GIT_THREADS
for (t = 0; t < threads; ++t)
cl_git_pass(git_thread_join(&th[t], NULL));
memset(th, 0, threads * sizeof(git_thread));
#endif
if (after_test) after_test();
}
git__free(id);
git__free(th);
}
| libgit2-main | tests/libgit2/threads/thread_helpers.c |
#include "clar_libgit2.h"
#include "thread_helpers.h"
#ifdef GIT_THREADS
# if defined(GIT_WIN32)
# define git_thread_yield() Sleep(0)
# elif defined(__FreeBSD__) || defined(__MidnightBSD__) || defined(__DragonFly__)
# define git_thread_yield() pthread_yield()
# else
# define git_thread_yield() sched_yield()
# endif
#else
# define git_thread_yield() (void)0
#endif
static git_repository *_repo;
static git_tree *_a, *_b;
static git_atomic32 _counts[4];
static int _check_counts;
#ifdef GIT_WIN32
static int _retries;
#endif
#define THREADS 20
void test_threads_diff__initialize(void)
{
#ifdef GIT_WIN32
_retries = git_win32__retries;
git_win32__retries = 1;
#endif
}
void test_threads_diff__cleanup(void)
{
cl_git_sandbox_cleanup();
#ifdef GIT_WIN32
git_win32__retries = _retries;
#endif
}
static void setup_trees(void)
{
git_index *idx;
_repo = cl_git_sandbox_reopen(); /* reopen sandbox to flush caches */
/* avoid competing to load initial index */
cl_git_pass(git_repository_index(&idx, _repo));
git_index_free(idx);
cl_git_pass(git_revparse_single(
(git_object **)&_a, _repo, "0017bd4ab1^{tree}"));
cl_git_pass(git_revparse_single(
(git_object **)&_b, _repo, "26a125ee1b^{tree}"));
memset(_counts, 0, sizeof(_counts));
}
static void free_trees(void)
{
git_tree_free(_a); _a = NULL;
git_tree_free(_b); _b = NULL;
if (_check_counts) {
cl_assert_equal_i(288, git_atomic32_get(&_counts[0]));
cl_assert_equal_i(112, git_atomic32_get(&_counts[1]));
cl_assert_equal_i( 80, git_atomic32_get(&_counts[2]));
cl_assert_equal_i( 96, git_atomic32_get(&_counts[3]));
}
}
static void *run_index_diffs(void *arg)
{
int thread = *(int *)arg;
git_repository *repo;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
size_t i;
int exp[4] = { 0, 0, 0, 0 };
cl_git_pass(git_repository_open(&repo, git_repository_path(_repo)));
switch (thread & 0x03) {
case 0: /* diff index to workdir */;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts));
break;
case 1: /* diff tree 'a' to index */;
cl_git_pass(git_diff_tree_to_index(&diff, repo, _a, NULL, &opts));
break;
case 2: /* diff tree 'b' to index */;
cl_git_pass(git_diff_tree_to_index(&diff, repo, _b, NULL, &opts));
break;
case 3: /* diff index to workdir (explicit index) */;
{
git_index *idx;
cl_git_pass(git_repository_index(&idx, repo));
cl_git_pass(git_diff_index_to_workdir(&diff, repo, idx, &opts));
git_index_free(idx);
break;
}
}
/* keep some diff stats to make sure results are as expected */
i = git_diff_num_deltas(diff);
git_atomic32_add(&_counts[0], (int32_t)i);
exp[0] = (int)i;
while (i > 0) {
switch (git_diff_get_delta(diff, --i)->status) {
case GIT_DELTA_MODIFIED: exp[1]++; git_atomic32_inc(&_counts[1]); break;
case GIT_DELTA_ADDED: exp[2]++; git_atomic32_inc(&_counts[2]); break;
case GIT_DELTA_DELETED: exp[3]++; git_atomic32_inc(&_counts[3]); break;
default: break;
}
}
switch (thread & 0x03) {
case 0: case 3:
cl_assert_equal_i(8, exp[0]); cl_assert_equal_i(4, exp[1]);
cl_assert_equal_i(0, exp[2]); cl_assert_equal_i(4, exp[3]);
break;
case 1:
cl_assert_equal_i(12, exp[0]); cl_assert_equal_i(3, exp[1]);
cl_assert_equal_i(7, exp[2]); cl_assert_equal_i(2, exp[3]);
break;
case 2:
cl_assert_equal_i(8, exp[0]); cl_assert_equal_i(3, exp[1]);
cl_assert_equal_i(3, exp[2]); cl_assert_equal_i(2, exp[3]);
break;
}
git_diff_free(diff);
git_repository_free(repo);
git_error_clear();
return arg;
}
void test_threads_diff__concurrent_diffs(void)
{
_repo = cl_git_sandbox_init("status");
_check_counts = 1;
run_in_parallel(
5, 32, run_index_diffs, setup_trees, free_trees);
}
static void *run_index_diffs_with_modifier(void *arg)
{
int thread = *(int *)arg;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff = NULL;
git_index *idx = NULL;
git_repository *repo;
cl_git_pass(git_repository_open(&repo, git_repository_path(_repo)));
cl_git_pass(git_repository_index(&idx, repo));
/* have first thread altering the index as we go */
if (thread == 0) {
int i;
for (i = 0; i < 300; ++i) {
switch (i & 0x03) {
case 0: (void)git_index_add_bypath(idx, "new_file"); break;
case 1: (void)git_index_remove_bypath(idx, "modified_file"); break;
case 2: (void)git_index_remove_bypath(idx, "new_file"); break;
case 3: (void)git_index_add_bypath(idx, "modified_file"); break;
}
git_thread_yield();
}
goto done;
}
/* only use explicit index in this test to prevent reloading */
switch (thread & 0x03) {
case 0: /* diff index to workdir */;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, idx, &opts));
break;
case 1: /* diff tree 'a' to index */;
cl_git_pass(git_diff_tree_to_index(&diff, repo, _a, idx, &opts));
break;
case 2: /* diff tree 'b' to index */;
cl_git_pass(git_diff_tree_to_index(&diff, repo, _b, idx, &opts));
break;
case 3: /* diff index to workdir reversed */;
opts.flags |= GIT_DIFF_REVERSE;
cl_git_pass(git_diff_index_to_workdir(&diff, repo, idx, &opts));
break;
}
/* results will be unpredictable with index modifier thread running */
git_diff_free(diff);
done:
git_index_free(idx);
git_repository_free(repo);
git_error_clear();
return arg;
}
void test_threads_diff__with_concurrent_index_modified(void)
{
_repo = cl_git_sandbox_init("status");
_check_counts = 0;
run_in_parallel(
5, 16, run_index_diffs_with_modifier, setup_trees, free_trees);
}
| libgit2-main | tests/libgit2/threads/diff.c |
#include "clar_libgit2.h"
#include "thread_helpers.h"
#include "cache.h"
static git_repository *g_repo;
void test_threads_basic__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_threads_basic__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_threads_basic__cache(void)
{
/* run several threads polling the cache at the same time */
cl_assert(1 == 1);
}
void test_threads_basic__multiple_init(void)
{
git_repository *nested_repo;
git_libgit2_init();
cl_git_pass(git_repository_open(&nested_repo, cl_fixture("testrepo.git")));
git_repository_free(nested_repo);
git_libgit2_shutdown();
cl_git_pass(git_repository_open(&nested_repo, cl_fixture("testrepo.git")));
git_repository_free(nested_repo);
}
static void *set_error(void *dummy)
{
git_error_set(GIT_ERROR_INVALID, "oh no, something happened!\n");
return dummy;
}
/* Set errors so we can check that we free it */
void test_threads_basic__set_error(void)
{
run_in_parallel(1, 4, set_error, NULL, NULL);
}
#ifdef GIT_THREADS
static void *return_normally(void *param)
{
return param;
}
static void *exit_abruptly(void *param)
{
git_thread_exit(param);
return NULL;
}
#endif
void test_threads_basic__exit(void)
{
#ifndef GIT_THREADS
clar__skip();
#else
git_thread thread;
void *result;
/* Ensure that the return value of the threadproc is returned. */
cl_git_pass(git_thread_create(&thread, return_normally, (void *)424242));
cl_git_pass(git_thread_join(&thread, &result));
cl_assert_equal_sz(424242, (size_t)result);
/* Ensure that the return value of `git_thread_exit` is returned. */
cl_git_pass(git_thread_create(&thread, exit_abruptly, (void *)232323));
cl_git_pass(git_thread_join(&thread, &result));
cl_assert_equal_sz(232323, (size_t)result);
#endif
}
| libgit2-main | tests/libgit2/threads/basic.c |
#include "clar_libgit2.h"
#include "thread_helpers.h"
void test_threads_tlsdata__can_set_and_get(void)
{
git_tlsdata_key key_one, key_two, key_three;
cl_git_pass(git_tlsdata_init(&key_one, NULL));
cl_git_pass(git_tlsdata_init(&key_two, NULL));
cl_git_pass(git_tlsdata_init(&key_three, NULL));
cl_git_pass(git_tlsdata_set(key_one, (void *)(size_t)42424242));
cl_git_pass(git_tlsdata_set(key_two, (void *)(size_t)0xdeadbeef));
cl_git_pass(git_tlsdata_set(key_three, (void *)(size_t)98761234));
cl_assert_equal_sz((size_t)42424242, git_tlsdata_get(key_one));
cl_assert_equal_sz((size_t)0xdeadbeef, git_tlsdata_get(key_two));
cl_assert_equal_sz((size_t)98761234, git_tlsdata_get(key_three));
cl_git_pass(git_tlsdata_dispose(key_one));
cl_git_pass(git_tlsdata_dispose(key_two));
cl_git_pass(git_tlsdata_dispose(key_three));
}
#ifdef GIT_THREADS
static void *set_and_get(void *param)
{
git_tlsdata_key *tlsdata_key = (git_tlsdata_key *)param;
int val;
if (git_tlsdata_set(*tlsdata_key, &val) != 0 ||
git_tlsdata_get(*tlsdata_key) != &val)
return (void *)0;
return (void *)1;
}
#endif
#define THREAD_COUNT 10
void test_threads_tlsdata__threads(void)
{
#ifdef GIT_THREADS
git_thread thread[THREAD_COUNT];
git_tlsdata_key tlsdata;
int i;
cl_git_pass(git_tlsdata_init(&tlsdata, NULL));
for (i = 0; i < THREAD_COUNT; i++)
cl_git_pass(git_thread_create(&thread[i], set_and_get, &tlsdata));
for (i = 0; i < THREAD_COUNT; i++) {
void *result;
cl_git_pass(git_thread_join(&thread[i], &result));
cl_assert_equal_sz(1, (size_t)result);
}
cl_git_pass(git_tlsdata_dispose(tlsdata));
#endif
}
| libgit2-main | tests/libgit2/threads/tlsdata.c |
#include "clar_libgit2.h"
#include "git2/refdb.h"
#include "refdb.h"
static git_repository *g_repo;
static int g_expected = 0;
#ifdef GIT_WIN32
static bool concurrent_compress = false;
#else
static bool concurrent_compress = true;
#endif
void test_threads_refdb__initialize(void)
{
g_repo = NULL;
}
void test_threads_refdb__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
#define REPEAT 20
#define THREADS 20
/* Number of references to create or delete in each thread */
#define NREFS 10
struct th_data {
cl_git_thread_err error;
int id;
const char *path;
};
static void *iterate_refs(void *arg)
{
struct th_data *data = (struct th_data *) arg;
git_reference_iterator *i;
git_reference *ref;
int count = 0, error;
git_repository *repo;
cl_git_thread_pass(data, git_repository_open(&repo, data->path));
do {
error = git_reference_iterator_new(&i, repo);
} while (error == GIT_ELOCKED);
cl_git_thread_pass(data, error);
for (count = 0; !git_reference_next(&ref, i); ++count) {
cl_assert(ref != NULL);
git_reference_free(ref);
}
if (g_expected > 0)
cl_assert_equal_i(g_expected, count);
git_reference_iterator_free(i);
git_repository_free(repo);
git_error_clear();
return arg;
}
static void *create_refs(void *arg)
{
int i, error;
struct th_data *data = (struct th_data *) arg;
git_oid head;
char name[128];
git_reference *ref[NREFS];
git_repository *repo;
cl_git_thread_pass(data, git_repository_open(&repo, data->path));
do {
error = git_reference_name_to_id(&head, repo, "HEAD");
} while (error == GIT_ELOCKED);
cl_git_thread_pass(data, error);
for (i = 0; i < NREFS; ++i) {
p_snprintf(name, sizeof(name), "refs/heads/thread-%03d-%02d", data->id, i);
do {
error = git_reference_create(&ref[i], repo, name, &head, 0, NULL);
} while (error == GIT_ELOCKED);
cl_git_thread_pass(data, error);
if (concurrent_compress && i == NREFS/2) {
git_refdb *refdb;
cl_git_thread_pass(data, git_repository_refdb(&refdb, repo));
do {
error = git_refdb_compress(refdb);
} while (error == GIT_ELOCKED);
cl_git_thread_pass(data, error);
git_refdb_free(refdb);
}
}
for (i = 0; i < NREFS; ++i)
git_reference_free(ref[i]);
git_repository_free(repo);
git_error_clear();
return arg;
}
static void *delete_refs(void *arg)
{
int i, error;
struct th_data *data = (struct th_data *) arg;
git_reference *ref;
char name[128];
git_repository *repo;
cl_git_thread_pass(data, git_repository_open(&repo, data->path));
for (i = 0; i < NREFS; ++i) {
p_snprintf(
name, sizeof(name), "refs/heads/thread-%03d-%02d", (data->id) & ~0x3, i);
if (!git_reference_lookup(&ref, repo, name)) {
do {
error = git_reference_delete(ref);
} while (error == GIT_ELOCKED);
/* Sometimes we race with other deleter threads */
if (error == GIT_ENOTFOUND)
error = 0;
cl_git_thread_pass(data, error);
git_reference_free(ref);
}
if (concurrent_compress && i == NREFS/2) {
git_refdb *refdb;
cl_git_thread_pass(data, git_repository_refdb(&refdb, repo));
do {
error = git_refdb_compress(refdb);
} while (error == GIT_ELOCKED);
cl_git_thread_pass(data, error);
git_refdb_free(refdb);
}
}
git_repository_free(repo);
git_error_clear();
return arg;
}
void test_threads_refdb__edit_while_iterate(void)
{
int r, t;
struct th_data th_data[THREADS];
git_oid head;
git_reference *ref;
char name[128];
git_refdb *refdb;
#ifdef GIT_THREADS
git_thread th[THREADS];
#endif
g_repo = cl_git_sandbox_init("testrepo2");
cl_git_pass(git_reference_name_to_id(&head, g_repo, "HEAD"));
/* make a bunch of references */
for (r = 0; r < 50; ++r) {
p_snprintf(name, sizeof(name), "refs/heads/starter-%03d", r);
cl_git_pass(git_reference_create(&ref, g_repo, name, &head, 0, NULL));
git_reference_free(ref);
}
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
git_refdb_free(refdb);
g_expected = -1;
g_repo = cl_git_sandbox_reopen(); /* reopen to flush caches */
for (t = 0; t < THREADS; ++t) {
void *(*fn)(void *arg);
switch (t & 0x3) {
case 0: fn = create_refs; break;
case 1: fn = delete_refs; break;
default: fn = iterate_refs; break;
}
th_data[t].id = t;
th_data[t].path = git_repository_path(g_repo);
#ifdef GIT_THREADS
cl_git_pass(git_thread_create(&th[t], fn, &th_data[t]));
#else
fn(&th_data[t]);
#endif
}
#ifdef GIT_THREADS
for (t = 0; t < THREADS; ++t) {
cl_git_pass(git_thread_join(&th[t], NULL));
cl_git_thread_check(&th_data[t]);
}
memset(th, 0, sizeof(th));
for (t = 0; t < THREADS; ++t) {
th_data[t].id = t;
cl_git_pass(git_thread_create(&th[t], iterate_refs, &th_data[t]));
}
for (t = 0; t < THREADS; ++t) {
cl_git_pass(git_thread_join(&th[t], NULL));
cl_git_thread_check(&th_data[t]);
}
#endif
}
| libgit2-main | tests/libgit2/threads/refdb.c |
#include "clar_libgit2.h"
void test_threads_atomic__atomic32_set(void)
{
git_atomic32 v = {0};
git_atomic32_set(&v, 1);
cl_assert_equal_i(v.val, 1);
}
void test_threads_atomic__atomic32_get(void)
{
git_atomic32 v = {1};
cl_assert_equal_i(git_atomic32_get(&v), 1);
}
void test_threads_atomic__atomic32_inc(void)
{
git_atomic32 v = {0};
cl_assert_equal_i(git_atomic32_inc(&v), 1);
cl_assert_equal_i(v.val, 1);
}
void test_threads_atomic__atomic32_add(void)
{
git_atomic32 v = {0};
cl_assert_equal_i(git_atomic32_add(&v, 1), 1);
cl_assert_equal_i(v.val, 1);
}
void test_threads_atomic__atomic32_dec(void)
{
git_atomic32 v = {1};
cl_assert_equal_i(git_atomic32_dec(&v), 0);
cl_assert_equal_i(v.val, 0);
}
void test_threads_atomic__atomic64_set(void)
{
#ifndef GIT_ARCH_64
cl_skip();
#else
git_atomic64 v = {0};
git_atomic64_set(&v, 1);
cl_assert_equal_i(v.val, 1);
#endif
}
void test_threads_atomic__atomic64_get(void)
{
#ifndef GIT_ARCH_64
cl_skip();
#else
git_atomic64 v = {1};
cl_assert_equal_i(git_atomic64_get(&v), 1);
#endif
}
void test_threads_atomic__atomic64_add(void)
{
#ifndef GIT_ARCH_64
cl_skip();
#else
git_atomic64 v = {0};
cl_assert_equal_i(git_atomic64_add(&v, 1), 1);
cl_assert_equal_i(v.val, 1);
#endif
}
void test_threads_atomic__cas_pointer(void)
{
int *value = NULL;
int newvalue1 = 1, newvalue2 = 2;
/* value is updated */
cl_assert_equal_p(git_atomic_compare_and_swap(&value, NULL, &newvalue1), NULL);
cl_assert_equal_p(value, &newvalue1);
/* value is not updated */
cl_assert_equal_p(git_atomic_compare_and_swap(&value, NULL, &newvalue2), &newvalue1);
cl_assert_equal_p(value, &newvalue1);
}
void test_threads_atomic__cas_intptr(void)
{
intptr_t value = 0;
intptr_t oldvalue;
intptr_t newvalue;
/* value is updated */
oldvalue = 0;
newvalue = 1;
cl_assert_equal_i((intptr_t)git_atomic_compare_and_swap(&value, (void *)oldvalue, (void *)newvalue), 0);
cl_assert_equal_i(value, 1);
/* value is not updated */
oldvalue = 0;
newvalue = 2;
cl_assert_equal_i((intptr_t)git_atomic_compare_and_swap(&value, (void *)oldvalue, (void *)newvalue), 1);
cl_assert_equal_i(value, 1);
}
void test_threads_atomic__swap(void)
{
int *value = NULL;
int newvalue = 1;
cl_assert_equal_p(git_atomic_swap(value, &newvalue), NULL);
cl_assert_equal_p(value, &newvalue);
cl_assert_equal_p(git_atomic_swap(value, NULL), &newvalue);
cl_assert_equal_p(value, NULL);
}
void test_threads_atomic__load_ptr(void)
{
int value = 1;
int *ptr = &value;
cl_assert_equal_p(git_atomic_load(ptr), &value);
}
void test_threads_atomic__load_intptr(void)
{
intptr_t value = 1;
cl_assert_equal_i((intptr_t)git_atomic_load(value), 1);
}
| libgit2-main | tests/libgit2/threads/atomic.c |
#include "clar_libgit2.h"
#include "thread_helpers.h"
#include "iterator.h"
static git_repository *_repo;
void test_threads_iterator__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void *run_workdir_iterator(void *arg)
{
int error = 0;
git_repository *repo;
git_iterator *iter;
git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
const git_index_entry *entry = NULL;
iter_opts.flags = GIT_ITERATOR_DONT_AUTOEXPAND;
cl_git_pass(git_repository_open(&repo, git_repository_path(_repo)));
cl_git_pass(git_iterator_for_workdir(
&iter, repo, NULL, NULL, &iter_opts));
while (!error) {
if (entry && entry->mode == GIT_FILEMODE_TREE) {
error = git_iterator_advance_into(&entry, iter);
if (error == GIT_ENOTFOUND)
error = git_iterator_advance(&entry, iter);
} else {
error = git_iterator_advance(&entry, iter);
}
if (!error)
(void)git_iterator_current_is_ignored(iter);
}
cl_assert_equal_i(GIT_ITEROVER, error);
git_iterator_free(iter);
git_repository_free(repo);
git_error_clear();
return arg;
}
void test_threads_iterator__workdir(void)
{
_repo = cl_git_sandbox_init("status");
run_in_parallel(
1, 20, run_workdir_iterator, NULL, NULL);
}
| libgit2-main | tests/libgit2/threads/iterator.c |
#include "clar_libgit2.h"
#include "blame.h"
git_blame *g_blame;
void test_blame_getters__initialize(void)
{
size_t i;
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
git_blame_hunk hunks[] = {
{ 3, GIT_OID_SHA1_ZERO, 1, NULL, GIT_OID_SHA1_ZERO, "a", 0},
{ 3, GIT_OID_SHA1_ZERO, 4, NULL, GIT_OID_SHA1_ZERO, "b", 0},
{ 3, GIT_OID_SHA1_ZERO, 7, NULL, GIT_OID_SHA1_ZERO, "c", 0},
{ 3, GIT_OID_SHA1_ZERO, 10, NULL, GIT_OID_SHA1_ZERO, "d", 0},
{ 3, GIT_OID_SHA1_ZERO, 13, NULL, GIT_OID_SHA1_ZERO, "e", 0},
};
g_blame = git_blame__alloc(NULL, opts, "");
for (i=0; i<5; i++) {
git_blame_hunk *h = git__calloc(1, sizeof(git_blame_hunk));
h->final_start_line_number = hunks[i].final_start_line_number;
h->orig_path = git__strdup(hunks[i].orig_path);
h->lines_in_hunk = hunks[i].lines_in_hunk;
git_vector_insert(&g_blame->hunks, h);
}
}
void test_blame_getters__cleanup(void)
{
git_blame_free(g_blame);
}
void test_blame_getters__byindex(void)
{
const git_blame_hunk *h = git_blame_get_hunk_byindex(g_blame, 2);
cl_assert(h);
cl_assert_equal_s(h->orig_path, "c");
h = git_blame_get_hunk_byindex(g_blame, 95);
cl_assert_equal_p(h, NULL);
}
void test_blame_getters__byline(void)
{
const git_blame_hunk *h = git_blame_get_hunk_byline(g_blame, 5);
cl_assert(h);
cl_assert_equal_s(h->orig_path, "b");
h = git_blame_get_hunk_byline(g_blame, 95);
cl_assert_equal_p(h, NULL);
}
| libgit2-main | tests/libgit2/blame/getters.c |
#include "blame_helpers.h"
static git_repository *g_repo;
static git_blame *g_fileblame, *g_bufferblame;
void test_blame_buffer__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
cl_git_pass(git_blame_file(&g_fileblame, g_repo, "b.txt", NULL));
g_bufferblame = NULL;
}
void test_blame_buffer__cleanup(void)
{
git_blame_free(g_fileblame);
git_blame_free(g_bufferblame);
git_repository_free(g_repo);
}
void test_blame_buffer__index(void)
{
const git_blame_hunk *hunk;
const char *buffer = "Hello\nWorld!";
/*
* We need to open a different file from the ones used in other tests. Close
* the one opened in test_blame_buffer__initialize() to avoid a leak.
*/
git_blame_free(g_fileblame);
g_fileblame = NULL;
cl_git_pass(git_blame_file(&g_fileblame, g_repo, "file.txt", NULL));
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
cl_assert_equal_i(2, git_blame_get_hunk_count(g_bufferblame));
check_blame_hunk_index(g_repo, g_bufferblame, 0, 1, 1, 0, "836bc00b", "file.txt");
hunk = git_blame_get_hunk_byline(g_bufferblame, 1);
cl_assert(hunk);
cl_assert_equal_s("lhchavez", hunk->final_signature->name);
check_blame_hunk_index(g_repo, g_bufferblame, 1, 2, 1, 0, "00000000", "file.txt");
hunk = git_blame_get_hunk_byline(g_bufferblame, 2);
cl_assert(hunk);
cl_assert(hunk->final_signature == NULL);
}
void test_blame_buffer__added_line(void)
{
const git_blame_hunk *hunk;
const char *buffer = "\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
\n\
abcdefg\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n";
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame));
check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "000000", "b.txt");
hunk = git_blame_get_hunk_byline(g_bufferblame, 16);
cl_assert(hunk);
cl_assert_equal_s("Ben Straub", hunk->final_signature->name);
}
void test_blame_buffer__deleted_line(void)
{
const char *buffer = "\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n";
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 3, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 3, 9, 1, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 4, 10, 5, 0, "aa06ecca", "b.txt");
}
void test_blame_buffer__add_splits_hunk(void)
{
const char *buffer = "\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
abc\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n";
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 2, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 3, 8, 1, 0, "00000000", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 4, 9, 3, 0, "63d671eb", "b.txt");
}
void test_blame_buffer__delete_crosses_hunk_boundary(void)
{
const char *buffer = "\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n";
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 2, 0, "aa06ecca", "b.txt");
}
void test_blame_buffer__replace_line(void)
{
const char *buffer = "\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
abc\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n";
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 1, 0, "00000000", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 4, 8, 3, 0, "63d671eb", "b.txt");
}
void test_blame_buffer__add_lines_at_end(void)
{
const char *buffer = "\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\
\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\
\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\
\n\
abc\n\
def\n";
cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer)));
cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame));
check_blame_hunk_index(g_repo, g_bufferblame, 0, 1, 4, 0, "da237394", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 1, 5, 1, 1, "b99f7ac0", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 5, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 3, 11, 5, 0, "aa06ecca", "b.txt");
check_blame_hunk_index(g_repo, g_bufferblame, 4, 16, 2, 0, "00000000", "b.txt");
}
| libgit2-main | tests/libgit2/blame/buffer.c |
#include "blame_helpers.h"
void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...)
{
va_list arglist;
printf("Hunk %"PRIuZ" (line %"PRIuZ" +%"PRIuZ"): ", idx,
hunk->final_start_line_number, hunk->lines_in_hunk-1);
va_start(arglist, fmt);
vprintf(fmt, arglist);
va_end(arglist);
printf("\n");
}
void check_blame_hunk_index(git_repository *repo, git_blame *blame, int idx,
size_t start_line, size_t len, char boundary, const char *commit_id, const char *orig_path)
{
char expected[GIT_OID_SHA1_HEXSIZE+1] = {0}, actual[GIT_OID_SHA1_HEXSIZE+1] = {0};
const git_blame_hunk *hunk = git_blame_get_hunk_byindex(blame, idx);
cl_assert(hunk);
if (!strncmp(commit_id, "0000", 4)) {
strcpy(expected, "0000000000000000000000000000000000000000");
} else {
git_object *obj;
cl_git_pass(git_revparse_single(&obj, repo, commit_id));
git_oid_fmt(expected, git_object_id(obj));
git_object_free(obj);
}
if (hunk->final_start_line_number != start_line) {
hunk_message(idx, hunk, "mismatched start line number: expected %"PRIuZ", got %"PRIuZ,
start_line, hunk->final_start_line_number);
}
cl_assert_equal_i(hunk->final_start_line_number, start_line);
if (hunk->lines_in_hunk != len) {
hunk_message(idx, hunk, "mismatched line count: expected %"PRIuZ", got %"PRIuZ,
len, hunk->lines_in_hunk);
}
cl_assert_equal_i(hunk->lines_in_hunk, len);
git_oid_fmt(actual, &hunk->final_commit_id);
if (strcmp(expected, actual)) {
hunk_message(idx, hunk, "has mismatched original id (got %s, expected %s)\n",
actual, expected);
}
cl_assert_equal_s(actual, expected);
cl_assert_equal_oid(&hunk->final_commit_id, &hunk->orig_commit_id);
if (strcmp(hunk->orig_path, orig_path)) {
hunk_message(idx, hunk, "has mismatched original path (got '%s', expected '%s')\n",
hunk->orig_path, orig_path);
}
cl_assert_equal_s(hunk->orig_path, orig_path);
if (hunk->boundary != boundary) {
hunk_message(idx, hunk, "doesn't match boundary flag (got %d, expected %d)\n",
hunk->boundary, boundary);
}
cl_assert_equal_i(boundary, hunk->boundary);
}
| libgit2-main | tests/libgit2/blame/blame_helpers.c |
#include "blame_helpers.h"
static git_repository *g_repo;
static git_blame *g_blame;
void test_blame_simple__initialize(void)
{
g_repo = NULL;
g_blame = NULL;
}
void test_blame_simple__cleanup(void)
{
git_blame_free(g_blame);
git_repository_free(g_repo);
}
/*
* $ git blame -s branch_file.txt
* orig line no final line no
* commit V author timestamp V
* c47800c7 1 (Scott Chacon 2010-05-25 11:58:14 -0700 1
* a65fedf3 2 (Scott Chacon 2011-08-09 19:33:46 -0700 2
*/
void test_blame_simple__trivial_testrepo(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo/.gitted")));
cl_git_pass(git_blame_file(&g_blame, g_repo, "branch_file.txt", NULL));
cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 0, "c47800c7", "branch_file.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "a65fedf3", "branch_file.txt");
}
/*
* $ git blame -n b.txt
* orig line no final line no
* commit V author timestamp V
* da237394 1 (Ben Straub 2013-02-12 15:11:30 -0800 1
* da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2
* da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3
* da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4
* ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5
* 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6
* 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7
* 63d671eb 8 (Ben Straub 2013-02-12 15:13:04 -0800 8
* 63d671eb 9 (Ben Straub 2013-02-12 15:13:04 -0800 9
* 63d671eb 10 (Ben Straub 2013-02-12 15:13:04 -0800 10
* aa06ecca 6 (Ben Straub 2013-02-12 15:14:46 -0800 11
* aa06ecca 7 (Ben Straub 2013-02-12 15:14:46 -0800 12
* aa06ecca 8 (Ben Straub 2013-02-12 15:14:46 -0800 13
* aa06ecca 9 (Ben Straub 2013-02-12 15:14:46 -0800 14
* aa06ecca 10 (Ben Straub 2013-02-12 15:14:46 -0800 15
*/
void test_blame_simple__trivial_blamerepo(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", NULL));
cl_assert_equal_i(4, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, 0, "aa06ecca", "b.txt");
}
/*
* $ git blame -n 359fc2d -- include/git2.h
* orig line no final line no
* commit orig path V author timestamp V
* d12299fe src/git.h 1 (Vicent Martí 2010-12-03 22:22:10 +0200 1
* 359fc2d2 include/git2.h 2 (Edward Thomson 2013-01-08 17:07:25 -0600 2
* d12299fe src/git.h 5 (Vicent Martí 2010-12-03 22:22:10 +0200 3
* bb742ede include/git2.h 4 (Vicent Martí 2011-09-19 01:54:32 +0300 4
* bb742ede include/git2.h 5 (Vicent Martí 2011-09-19 01:54:32 +0300 5
* d12299fe src/git.h 24 (Vicent Martí 2010-12-03 22:22:10 +0200 6
* d12299fe src/git.h 25 (Vicent Martí 2010-12-03 22:22:10 +0200 7
* d12299fe src/git.h 26 (Vicent Martí 2010-12-03 22:22:10 +0200 8
* d12299fe src/git.h 27 (Vicent Martí 2010-12-03 22:22:10 +0200 9
* d12299fe src/git.h 28 (Vicent Martí 2010-12-03 22:22:10 +0200 10
* 96fab093 include/git2.h 11 (Sven Strickroth 2011-10-09 18:37:41 +0200 11
* 9d1dcca2 src/git2.h 33 (Vicent Martí 2011-02-07 10:35:58 +0200 12
* 44908fe7 src/git2.h 29 (Vicent Martí 2010-12-06 23:03:16 +0200 13
* a15c550d include/git2.h 14 (Vicent Martí 2011-11-16 14:09:44 +0100 14
* 44908fe7 src/git2.h 30 (Vicent Martí 2010-12-06 23:03:16 +0200 15
* d12299fe src/git.h 32 (Vicent Martí 2010-12-03 22:22:10 +0200 16
* 44908fe7 src/git2.h 33 (Vicent Martí 2010-12-06 23:03:16 +0200 17
* d12299fe src/git.h 34 (Vicent Martí 2010-12-03 22:22:10 +0200 18
* 44908fe7 src/git2.h 35 (Vicent Martí 2010-12-06 23:03:16 +0200 19
* 638c2ca4 src/git2.h 36 (Vicent Martí 2010-12-18 02:10:25 +0200 20
* 44908fe7 src/git2.h 36 (Vicent Martí 2010-12-06 23:03:16 +0200 21
* d12299fe src/git.h 37 (Vicent Martí 2010-12-03 22:22:10 +0200 22
* 44908fe7 src/git2.h 38 (Vicent Martí 2010-12-06 23:03:16 +0200 23
* 44908fe7 src/git2.h 39 (Vicent Martí 2010-12-06 23:03:16 +0200 24
* bf787bd8 include/git2.h 25 (Carlos Martín Nieto 2012-04-08 18:56:50 +0200 25
* 0984c876 include/git2.h 26 (Scott J. Goldman 2012-11-28 18:27:43 -0800 26
* 2f8a8ab2 src/git2.h 41 (Vicent Martí 2011-01-29 01:56:25 +0200 27
* 27df4275 include/git2.h 47 (Michael Schubert 2011-06-28 14:13:12 +0200 28
* a346992f include/git2.h 28 (Ben Straub 2012-05-10 09:47:14 -0700 29
* d12299fe src/git.h 40 (Vicent Martí 2010-12-03 22:22:10 +0200 30
* 44908fe7 src/git2.h 41 (Vicent Martí 2010-12-06 23:03:16 +0200 31
* 44908fe7 src/git2.h 42 (Vicent Martí 2010-12-06 23:03:16 +0200 32
* 44908fe7 src/git2.h 43 (Vicent Martí 2010-12-06 23:03:16 +0200 33
* 44908fe7 src/git2.h 44 (Vicent Martí 2010-12-06 23:03:16 +0200 34
* 44908fe7 src/git2.h 45 (Vicent Martí 2010-12-06 23:03:16 +0200 35
* 65b09b1d include/git2.h 33 (Russell Belfer 2012-02-02 18:03:43 -0800 36
* d12299fe src/git.h 46 (Vicent Martí 2010-12-03 22:22:10 +0200 37
* 44908fe7 src/git2.h 47 (Vicent Martí 2010-12-06 23:03:16 +0200 38
* 5d4cd003 include/git2.h 55 (Carlos Martín Nieto 2011-03-28 17:02:45 +0200 39
* 41fb1ca0 include/git2.h 39 (Philip Kelley 2012-10-29 13:41:14 -0400 40
* 2dc31040 include/git2.h 56 (Carlos Martín Nieto 2011-06-20 18:58:57 +0200 41
* 764df57e include/git2.h 40 (Ben Straub 2012-06-15 13:14:43 -0700 42
* 5280f4e6 include/git2.h 41 (Ben Straub 2012-07-31 19:39:06 -0700 43
* 613d5eb9 include/git2.h 43 (Philip Kelley 2012-11-28 11:42:37 -0500 44
* d12299fe src/git.h 48 (Vicent Martí 2010-12-03 22:22:10 +0200 45
* 111ee3fe include/git2.h 41 (Vicent Martí 2012-07-11 14:37:26 +0200 46
* f004c4a8 include/git2.h 44 (Russell Belfer 2012-08-21 17:26:39 -0700 47
* 111ee3fe include/git2.h 42 (Vicent Martí 2012-07-11 14:37:26 +0200 48
* 9c82357b include/git2.h 58 (Carlos Martín Nieto 2011-06-17 18:13:14 +0200 49
* d6258deb include/git2.h 61 (Carlos Martín Nieto 2011-06-25 15:10:09 +0200 50
* b311e313 include/git2.h 63 (Julien Miotte 2011-07-27 18:31:13 +0200 51
* 3412391d include/git2.h 63 (Carlos Martín Nieto 2011-07-07 11:47:31 +0200 52
* bfc9ca59 include/git2.h 43 (Russell Belfer 2012-03-28 16:45:36 -0700 53
* bf477ed4 include/git2.h 44 (Michael Schubert 2012-02-15 00:33:38 +0100 54
* edebceff include/git2.h 46 (nulltoken 2012-05-01 13:57:45 +0200 55
* 743a4b3b include/git2.h 48 (nulltoken 2012-06-15 22:24:59 +0200 56
* 0a32dca5 include/git2.h 54 (Michael Schubert 2012-08-19 22:26:32 +0200 57
* 590fb68b include/git2.h 55 (nulltoken 2012-10-04 13:47:45 +0200 58
* bf477ed4 include/git2.h 45 (Michael Schubert 2012-02-15 00:33:38 +0100 59
* d12299fe src/git.h 49 (Vicent Martí 2010-12-03 22:22:10 +0200 60
*/
void test_blame_simple__trivial_libgit2(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
git_object *obj;
/* If we can't open the libgit2 repo or if it isn't a full repo
* with proper history, just skip this test */
if (git_repository_open(&g_repo, cl_fixture("../..")) < 0)
cl_skip();
if (git_repository_is_shallow(g_repo))
cl_skip();
if (git_revparse_single(&obj, g_repo, "359fc2d") < 0)
cl_skip();
git_oid_cpy(&opts.newest_commit, git_object_id(obj));
git_object_free(obj);
cl_git_pass(git_blame_file(&g_blame, g_repo, "include/git2.h", &opts));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "359fc2d2", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 2, 3, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 3, 4, 2, 0, "bb742ede", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 4, 6, 5, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 5, 11, 1, 0, "96fab093", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 6, 12, 1, 0, "9d1dcca2", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 7, 13, 1, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 8, 14, 1, 0, "a15c550d", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 9, 15, 1, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 10, 16, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 11, 17, 1, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 12, 18, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 13, 19, 1, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 14, 20, 1, 0, "638c2ca4", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 15, 21, 1, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 16, 22, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 17, 23, 2, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 18, 25, 1, 0, "bf787bd8", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 19, 26, 1, 0, "0984c876", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 20, 27, 1, 0, "2f8a8ab2", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 21, 28, 1, 0, "27df4275", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 22, 29, 1, 0, "a346992f", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 23, 30, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 24, 31, 5, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 25, 36, 1, 0, "65b09b1d", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 26, 37, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 27, 38, 1, 0, "44908fe7", "src/git2.h");
check_blame_hunk_index(g_repo, g_blame, 28, 39, 1, 0, "5d4cd003", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 29, 40, 1, 0, "41fb1ca0", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 30, 41, 1, 0, "2dc31040", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 31, 42, 1, 0, "764df57e", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 32, 43, 1, 0, "5280f4e6", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 33, 44, 1, 0, "613d5eb9", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 34, 45, 1, 0, "d12299fe", "src/git.h");
check_blame_hunk_index(g_repo, g_blame, 35, 46, 1, 0, "111ee3fe", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 36, 47, 1, 0, "f004c4a8", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 37, 48, 1, 0, "111ee3fe", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 38, 49, 1, 0, "9c82357b", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 39, 50, 1, 0, "d6258deb", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 40, 51, 1, 0, "b311e313", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 41, 52, 1, 0, "3412391d", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 42, 53, 1, 0, "bfc9ca59", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 43, 54, 1, 0, "bf477ed4", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 44, 55, 1, 0, "edebceff", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 45, 56, 1, 0, "743a4b3b", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 46, 57, 1, 0, "0a32dca5", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 47, 58, 1, 0, "590fb68b", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 48, 59, 1, 0, "bf477ed4", "include/git2.h");
check_blame_hunk_index(g_repo, g_blame, 49, 60, 1, 0, "d12299fe", "src/git.h");
}
/* This was leading to segfaults on some systems during cache eviction. */
void test_blame_simple__trivial_libgit2_under_cache_pressure(void)
{
ssize_t old_max_storage = git_cache__max_storage;
git_cache__max_storage = 1024 * 1024;
test_blame_simple__trivial_libgit2();
git_cache__max_storage = old_max_storage;
}
/*
* $ git blame -n b.txt -L 8
* orig line no final line no
* commit V author timestamp V
* 63d671eb 8 (Ben Straub 2013-02-12 15:13:04 -0800 8
* 63d671eb 9 (Ben Straub 2013-02-12 15:13:04 -0800 9
* 63d671eb 10 (Ben Straub 2013-02-12 15:13:04 -0800 10
* aa06ecca 6 (Ben Straub 2013-02-12 15:14:46 -0800 11
* aa06ecca 7 (Ben Straub 2013-02-12 15:14:46 -0800 12
* aa06ecca 8 (Ben Straub 2013-02-12 15:14:46 -0800 13
* aa06ecca 9 (Ben Straub 2013-02-12 15:14:46 -0800 14
* aa06ecca 10 (Ben Straub 2013-02-12 15:14:46 -0800 15
*/
void test_blame_simple__can_restrict_lines_min(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
opts.min_line = 8;
cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts));
cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 8, 3, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 11, 5, 0, "aa06ecca", "b.txt");
}
/*
* $ git blame -n c.txt
* orig line no final line no
* commit V author timestamp V
* 702c7aa5 1 (Carl Schwan 2020-01-29 01:52:31 +0100 4
*/
void test_blame_simple__can_ignore_whitespace_change(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
opts.flags |= GIT_BLAME_IGNORE_WHITESPACE;
cl_git_pass(git_blame_file(&g_blame, g_repo, "c.txt", &opts));
cl_assert_equal_i(1, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "702c7aa5", "c.txt");
}
/*
* $ git blame -n b.txt -L ,6
* orig line no final line no
* commit V author timestamp V
* da237394 1 (Ben Straub 2013-02-12 15:11:30 -0800 1
* da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2
* da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3
* da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4
* ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5
* 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6
*/
void test_blame_simple__can_restrict_lines_max(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
opts.max_line = 6;
cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts));
cl_assert_equal_i(3, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 2, 6, 1, 0, "63d671eb", "b.txt");
}
/*
* $ git blame -n b.txt -L 2,7
* orig line no final line no
* commit V author timestamp V
* da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2
* da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3
* da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4
* ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5
* 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6
* 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7
*/
void test_blame_simple__can_restrict_lines_both(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
opts.min_line = 2;
opts.max_line = 7;
cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts));
cl_assert_equal_i(3, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 2, 3, 0, "da237394", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 2, 6, 2, 0, "63d671eb", "b.txt");
}
void test_blame_simple__can_blame_huge_file(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
cl_git_pass(git_blame_file(&g_blame, g_repo, "huge.txt", &opts));
cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 65536, 0, "4eecfea", "huge.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 65537, 1, 0, "6653ff4", "huge.txt");
}
/*
* $ git blame -n branch_file.txt be3563a..HEAD
* orig line no final line no
* commit V author timestamp V
* ^be3563a 1 (Scott Chacon 2010-05-25 11:58:27 -0700 1) hi
* a65fedf3 2 (Scott Chacon 2011-08-09 19:33:46 -0700 2) bye!
*/
void test_blame_simple__can_restrict_to_newish_commits(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
{
git_object *obj;
cl_git_pass(git_revparse_single(&obj, g_repo, "be3563a"));
git_oid_cpy(&opts.oldest_commit, git_object_id(obj));
git_object_free(obj);
}
cl_git_pass(git_blame_file(&g_blame, g_repo, "branch_file.txt", &opts));
cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 1, "be3563a", "branch_file.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "a65fedf", "branch_file.txt");
}
void test_blame_simple__can_restrict_to_first_parent_commits(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
opts.flags |= GIT_BLAME_FIRST_PARENT;
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts));
cl_assert_equal_i(4, git_blame_get_hunk_count(g_blame));
check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, 0, "63d671eb", "b.txt");
check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, 0, "bc7c5ac2", "b.txt");
}
| libgit2-main | tests/libgit2/blame/simple.c |
#include "clar_libgit2.h"
#include "blame.h"
/**
* The test repo has a history that looks like this:
*
* * (A) bc7c5ac
* |\
* | * (B) aa06ecc
* * | (C) 63d671e
* |/
* * (D) da23739
* * (E) b99f7ac
*
*/
static git_repository *g_repo = NULL;
void test_blame_harder__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
}
void test_blame_harder__cleanup(void)
{
git_repository_free(g_repo);
g_repo = NULL;
}
void test_blame_harder__m(void)
{
/* TODO */
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
GIT_UNUSED(opts);
opts.flags = GIT_BLAME_TRACK_COPIES_SAME_FILE;
}
void test_blame_harder__c(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
GIT_UNUSED(opts);
/* Attribute the first hunk in b.txt to (E), since it was cut/pasted from
* a.txt in (D).
*/
opts.flags = GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
}
void test_blame_harder__cc(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
GIT_UNUSED(opts);
/* Attribute the second hunk in b.txt to (E), since it was copy/pasted from
* a.txt in (C).
*/
opts.flags = GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
}
void test_blame_harder__ccc(void)
{
git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
GIT_UNUSED(opts);
/* Attribute the third hunk in b.txt to (E). This hunk was deleted from
* a.txt in (D), but reintroduced in (B).
*/
opts.flags = GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES;
}
| libgit2-main | tests/libgit2/blame/harder.c |
#include "clar_libgit2.h"
#include "repository.h"
static git_repository *g_repo;
void test_refs_namespaces__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_namespaces__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_namespaces__get_and_set(void)
{
cl_assert_equal_s(NULL, git_repository_get_namespace(g_repo));
cl_git_pass(git_repository_set_namespace(g_repo, "namespace"));
cl_assert_equal_s("namespace", git_repository_get_namespace(g_repo));
cl_git_pass(git_repository_set_namespace(g_repo, NULL));
cl_assert_equal_s(NULL, git_repository_get_namespace(g_repo));
}
void test_refs_namespaces__namespace_doesnt_show_normal_refs(void)
{
static git_strarray ref_list;
cl_git_pass(git_repository_set_namespace(g_repo, "namespace"));
cl_git_pass(git_reference_list(&ref_list, g_repo));
cl_assert_equal_i(0, ref_list.count);
git_strarray_dispose(&ref_list);
}
| libgit2-main | tests/libgit2/refs/namespaces.c |
#include "clar_libgit2.h"
static git_repository *repo;
void test_refs_unicode__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_unicode__cleanup(void)
{
cl_git_sandbox_cleanup();
repo = NULL;
}
void test_refs_unicode__create_and_lookup(void)
{
git_reference *ref0, *ref1, *ref2;
git_repository *repo2;
const char *REFNAME = "refs/heads/" "\303\205" "ngstr" "\303\266" "m";
const char *master = "refs/heads/master";
/* Create the reference */
cl_git_pass(git_reference_lookup(&ref0, repo, master));
cl_git_pass(git_reference_create(
&ref1, repo, REFNAME, git_reference_target(ref0), 0, NULL));
cl_assert_equal_s(REFNAME, git_reference_name(ref1));
git_reference_free(ref0);
/* Lookup the reference in a different instance of the repository */
cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME));
cl_assert_equal_oid(git_reference_target(ref1), git_reference_target(ref2));
cl_assert_equal_s(REFNAME, git_reference_name(ref2));
git_reference_free(ref2);
#if GIT_USE_ICONV
/* Lookup reference by decomposed unicode name */
#define REFNAME_DECOMPOSED "refs/heads/" "A" "\314\212" "ngstro" "\314\210" "m"
cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME_DECOMPOSED));
cl_assert_equal_oid(git_reference_target(ref1), git_reference_target(ref2));
cl_assert_equal_s(REFNAME, git_reference_name(ref2));
git_reference_free(ref2);
#endif
/* Cleanup */
git_reference_free(ref1);
git_repository_free(repo2);
}
| libgit2-main | tests/libgit2/refs/unicode.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static git_repository *g_repo;
void test_refs_list__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_list__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_list__all(void)
{
/* try to list all the references in our test repo */
git_strarray ref_list;
cl_git_pass(git_reference_list(&ref_list, g_repo));
/*{
unsigned short i;
for (i = 0; i < ref_list.count; ++i)
printf("# %s\n", ref_list.strings[i]);
}*/
/* We have exactly 12 refs in total if we include the packed ones:
* there is a reference that exists both in the packfile and as
* loose, but we only list it once */
cl_assert_equal_i((int)ref_list.count, 19);
git_strarray_dispose(&ref_list);
}
void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_extension(void)
{
git_strarray ref_list;
/* Create a fake locked reference */
cl_git_mkfile(
"./testrepo/.git/refs/heads/hanwen.lock",
"144344043ba4d4a405da03de3844aa829ae8be0e\n");
cl_git_pass(git_reference_list(&ref_list, g_repo));
cl_assert_equal_i((int)ref_list.count, 19);
git_strarray_dispose(&ref_list);
}
| libgit2-main | tests/libgit2/refs/list.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "refs.h"
#include "ref_helpers.h"
static git_repository *g_repo;
static const char *loose_tag_ref_name = "refs/tags/e90810b";
void test_refs_basic__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_set_ident(g_repo, "me", "[email protected]"));
}
void test_refs_basic__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_basic__reference_realloc(void)
{
git_reference *ref;
git_reference *new_ref;
const char *new_name = "refs/tags/awful/name-which-is/clearly/really-that-much/longer-than/the-old-one";
/* Retrieval of the reference to rename */
cl_git_pass(git_reference_lookup(&ref, g_repo, loose_tag_ref_name));
new_ref = git_reference__realloc(&ref, new_name);
cl_assert(new_ref != NULL);
git_reference_free(new_ref);
git_reference_free(ref);
/* Reload, so we restore the value */
cl_git_pass(git_reference_lookup(&ref, g_repo, loose_tag_ref_name));
cl_git_pass(git_reference_rename(&new_ref, ref, new_name, 1, "log message"));
cl_assert(ref != NULL);
cl_assert(new_ref != NULL);
git_reference_free(new_ref);
git_reference_free(ref);
}
void test_refs_basic__longpaths(void)
{
#ifdef GIT_WIN32
const char *base;
size_t base_len, extra_len;
ssize_t remain_len, i;
git_str refname = GIT_STR_INIT;
git_reference *one = NULL, *two = NULL;
git_oid id;
cl_git_pass(git_oid__fromstr(&id, "099fabac3a9ea935598528c27f866e34089c2eff", GIT_OID_SHA1));
base = git_repository_path(g_repo);
base_len = git_utf8_char_length(base, strlen(base));
extra_len = CONST_STRLEN("logs/refs/heads/") + CONST_STRLEN(".lock");
remain_len = (ssize_t)MAX_PATH - (base_len + extra_len);
cl_assert(remain_len > 0);
cl_git_pass(git_str_puts(&refname, "refs/heads/"));
for (i = 0; i < remain_len; i++) {
cl_git_pass(git_str_putc(&refname, 'a'));
}
/*
* The full path to the reflog lockfile is 260 characters,
* this is permitted.
*/
cl_git_pass(git_reference_create(&one, g_repo, refname.ptr, &id, 0, NULL));
/* Adding one more character gives us a path that is too long. */
cl_git_pass(git_str_putc(&refname, 'z'));
cl_git_fail(git_reference_create(&two, g_repo, refname.ptr, &id, 0, NULL));
git_reference_free(one);
git_reference_free(two);
git_str_dispose(&refname);
#endif
}
| libgit2-main | tests/libgit2/refs/basic.c |
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *g_repo;
void test_refs_lookup__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_lookup__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_lookup__with_resolve(void)
{
git_reference *a, *b, *temp;
cl_git_pass(git_reference_lookup(&temp, g_repo, "HEAD"));
cl_git_pass(git_reference_resolve(&a, temp));
git_reference_free(temp);
cl_git_pass(git_reference_lookup_resolved(&b, g_repo, "HEAD", 5));
cl_assert(git_reference_cmp(a, b) == 0);
git_reference_free(b);
cl_git_pass(git_reference_lookup_resolved(&b, g_repo, "HEAD_TRACKER", 5));
cl_assert(git_reference_cmp(a, b) == 0);
git_reference_free(b);
git_reference_free(a);
}
void test_refs_lookup__invalid_name(void)
{
git_oid oid;
cl_git_fail(git_reference_name_to_id(&oid, g_repo, "/refs/tags/point_to_blob"));
}
void test_refs_lookup__oid(void)
{
git_oid tag, expected;
cl_git_pass(git_reference_name_to_id(&tag, g_repo, "refs/tags/point_to_blob"));
cl_git_pass(git_oid__fromstr(&expected, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1));
cl_assert_equal_oid(&expected, &tag);
}
void test_refs_lookup__namespace(void)
{
int error;
git_reference *ref;
error = git_reference_lookup(&ref, g_repo, "refs/heads");
cl_assert_equal_i(error, GIT_ENOTFOUND);
error = git_reference_lookup(&ref, g_repo, "refs/heads/");
cl_assert_equal_i(error, GIT_EINVALIDSPEC);
}
void test_refs_lookup__dwim_notfound(void)
{
git_reference *ref;
cl_git_fail_with(GIT_ENOTFOUND, git_reference_dwim(&ref, g_repo, "idontexist"));
cl_assert_equal_s("no reference found for shorthand 'idontexist'", git_error_last()->message);
}
| libgit2-main | tests/libgit2/refs/lookup.c |
#include "clar_libgit2.h"
static git_repository *g_repo;
static git_repository *g_peel_repo;
void test_refs_peel__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_open(&g_peel_repo, cl_fixture("peeled.git")));
}
void test_refs_peel__cleanup(void)
{
git_repository_free(g_repo);
g_repo = NULL;
git_repository_free(g_peel_repo);
g_peel_repo = NULL;
}
static void assert_peel_generic(
git_repository *repo,
const char *ref_name,
git_object_t requested_type,
const char* expected_sha,
git_object_t expected_type)
{
git_oid expected_oid;
git_reference *ref;
git_object *peeled;
cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
cl_git_pass(git_reference_peel(&peeled, ref, 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_reference_free(ref);
}
static void assert_peel(
const char *ref_name,
git_object_t requested_type,
const char* expected_sha,
git_object_t expected_type)
{
assert_peel_generic(g_repo, ref_name, requested_type,
expected_sha, expected_type);
}
static void assert_peel_error(int error, const char *ref_name, git_object_t requested_type)
{
git_reference *ref;
git_object *peeled;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert_equal_i(error, git_reference_peel(&peeled, ref, requested_type));
git_reference_free(ref);
}
void test_refs_peel__can_peel_a_tag(void)
{
assert_peel("refs/tags/test", GIT_OBJECT_TAG,
"b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OBJECT_TAG);
assert_peel("refs/tags/test", GIT_OBJECT_COMMIT,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
assert_peel("refs/tags/test", GIT_OBJECT_TREE,
"53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
assert_peel("refs/tags/point_to_blob", GIT_OBJECT_BLOB,
"1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJECT_BLOB);
}
void test_refs_peel__can_peel_a_branch(void)
{
assert_peel("refs/heads/master", GIT_OBJECT_COMMIT,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
assert_peel("refs/heads/master", GIT_OBJECT_TREE,
"944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJECT_TREE);
}
void test_refs_peel__can_peel_a_symbolic_reference(void)
{
assert_peel("HEAD", GIT_OBJECT_COMMIT,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
assert_peel("HEAD", GIT_OBJECT_TREE,
"944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJECT_TREE);
}
void test_refs_peel__cannot_peel_into_a_non_existing_target(void)
{
assert_peel_error(GIT_EINVALIDSPEC, "refs/tags/point_to_blob", GIT_OBJECT_TAG);
}
void test_refs_peel__can_peel_into_any_non_tag_object(void)
{
assert_peel("refs/heads/master", GIT_OBJECT_ANY,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
assert_peel("refs/tags/point_to_blob", GIT_OBJECT_ANY,
"1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJECT_BLOB);
assert_peel("refs/tags/test", GIT_OBJECT_ANY,
"e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
}
void test_refs_peel__can_peel_fully_peeled_packed_refs(void)
{
assert_peel_generic(g_peel_repo,
"refs/tags/tag-inside-tags", GIT_OBJECT_ANY,
"0df1a5865c8abfc09f1f2182e6a31be550e99f07",
GIT_OBJECT_COMMIT);
assert_peel_generic(g_peel_repo,
"refs/foo/tag-outside-tags", GIT_OBJECT_ANY,
"0df1a5865c8abfc09f1f2182e6a31be550e99f07",
GIT_OBJECT_COMMIT);
}
void test_refs_peel__can_peel_fully_peeled_tag_to_tag(void)
{
assert_peel_generic(g_peel_repo,
"refs/tags/tag-inside-tags", GIT_OBJECT_TAG,
"c2596aa0151888587ec5c0187f261e63412d9e11",
GIT_OBJECT_TAG);
assert_peel_generic(g_peel_repo,
"refs/foo/tag-outside-tags", GIT_OBJECT_TAG,
"c2596aa0151888587ec5c0187f261e63412d9e11",
GIT_OBJECT_TAG);
}
| libgit2-main | tests/libgit2/refs/peel.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "git2/refs.h"
static const char *ref_name = "refs/heads/other";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_test_name = "refs/heads/test";
static git_repository *g_repo;
void test_refs_setter__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_setter__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_setter__update_direct(void)
{
git_reference *ref, *test_ref, *new_ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) == GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
cl_assert(git_reference_type(test_ref) == GIT_REFERENCE_DIRECT);
cl_git_pass(git_reference_set_target(&new_ref, test_ref, &id, NULL));
git_reference_free(test_ref);
git_reference_free(new_ref);
cl_git_pass(git_reference_lookup(&test_ref, g_repo, ref_test_name));
cl_assert(git_reference_type(test_ref) == GIT_REFERENCE_DIRECT);
cl_assert_equal_oid(&id, git_reference_target(test_ref));
git_reference_free(test_ref);
}
void test_refs_setter__update_symbolic(void)
{
git_reference *head, *new_head;
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_assert(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC);
cl_assert(strcmp(git_reference_symbolic_target(head), ref_master_name) == 0);
cl_git_pass(git_reference_symbolic_set_target(&new_head, head, ref_test_name, NULL));
git_reference_free(new_head);
git_reference_free(head);
cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
cl_assert(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC);
cl_assert(strcmp(git_reference_symbolic_target(head), ref_test_name) == 0);
git_reference_free(head);
}
void test_refs_setter__cant_update_direct_with_symbolic(void)
{
/* Overwrite an existing object id reference with a symbolic one */
git_reference *ref, *new;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) == GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
cl_git_fail(git_reference_symbolic_set_target(&new, ref, ref_name, NULL));
git_reference_free(ref);
}
void test_refs_setter__cant_update_symbolic_with_direct(void)
{
/* Overwrite an existing symbolic reference with an object id one */
git_reference *ref, *new;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) == GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
/* Create the symbolic ref */
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0, NULL));
/* Can't set an OID on a direct ref */
cl_git_fail(git_reference_set_target(&new, ref, &id, NULL));
git_reference_free(ref);
}
| libgit2-main | tests/libgit2/refs/setter.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "refs.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *packed_head_name = "refs/heads/packed";
static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *ref_one_name = "refs/heads/one/branch";
static const char *ref_one_name_new = "refs/heads/two/branch";
static const char *ref_two_name = "refs/heads/two";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_two_name_new = "refs/heads/two/two";
static git_repository *g_repo;
void test_refs_rename__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_set_ident(g_repo, "me", "[email protected]"));
}
void test_refs_rename__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_rename__loose(void)
{
/* rename a loose reference */
git_reference *looked_up_ref, *new_ref, *another_looked_up_ref;
git_str temp_path = GIT_STR_INIT;
const char *new_name = "refs/tags/Nemo/knows/refs.kung-fu";
/* Ensure the ref doesn't exist on the file system */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), new_name));
cl_assert(!git_fs_path_exists(temp_path.ptr));
/* Retrieval of the reference to rename */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, loose_tag_ref_name));
/* ... which is indeed loose */
cl_assert(reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is renamed... */
cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, new_name, 0, NULL));
cl_assert_equal_s(new_ref->name, new_name);
git_reference_free(looked_up_ref);
/* ...It can't be looked-up with the old name... */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, loose_tag_ref_name));
/* ...but the new name works ok... */
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, new_name));
cl_assert_equal_s(new_ref->name, new_name);
/* .. the new ref is loose... */
cl_assert(reference_is_packed(another_looked_up_ref) == 0);
cl_assert(reference_is_packed(new_ref) == 0);
/* ...and the ref can be found in the file system */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), new_name));
cl_assert(git_fs_path_exists(temp_path.ptr));
git_reference_free(new_ref);
git_reference_free(another_looked_up_ref);
git_str_dispose(&temp_path);
}
void test_refs_rename__packed(void)
{
/* rename a packed reference (should make it loose) */
git_reference *looked_up_ref, *new_ref, *another_looked_up_ref;
git_str temp_path = GIT_STR_INIT;
const char *brand_new_name = "refs/heads/brand_new_name";
/* Ensure the ref doesn't exist on the file system */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), packed_head_name));
cl_assert(!git_fs_path_exists(temp_path.ptr));
/* The reference can however be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
/* .. and it's packed */
cl_assert(reference_is_packed(looked_up_ref) != 0);
/* Now that the reference is renamed... */
cl_git_pass(git_reference_rename(&new_ref, looked_up_ref, brand_new_name, 0, NULL));
cl_assert_equal_s(new_ref->name, brand_new_name);
git_reference_free(looked_up_ref);
/* ...It can't be looked-up with the old name... */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_head_name));
/* ...but the new name works ok... */
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, brand_new_name));
cl_assert_equal_s(another_looked_up_ref->name, brand_new_name);
/* .. the ref is no longer packed... */
cl_assert(reference_is_packed(another_looked_up_ref) == 0);
cl_assert(reference_is_packed(new_ref) == 0);
/* ...and the ref now happily lives in the file system */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), brand_new_name));
cl_assert(git_fs_path_exists(temp_path.ptr));
git_reference_free(new_ref);
git_reference_free(another_looked_up_ref);
git_str_dispose(&temp_path);
}
void test_refs_rename__packed_doesnt_pack_others(void)
{
/* renaming a packed reference does not pack another reference which happens to be in both loose and pack state */
git_reference *looked_up_ref, *another_looked_up_ref, *renamed_ref;
git_str temp_path = GIT_STR_INIT;
const char *brand_new_name = "refs/heads/brand_new_name";
/* Ensure the other reference exists on the file system */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name));
cl_assert(git_fs_path_exists(temp_path.ptr));
/* Lookup the other reference */
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's loose */
cl_assert(reference_is_packed(another_looked_up_ref) == 0);
git_reference_free(another_looked_up_ref);
/* Lookup the reference to rename */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
/* Ensure it's packed */
cl_assert(reference_is_packed(looked_up_ref) != 0);
/* Now that the reference is renamed... */
cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, brand_new_name, 0, NULL));
git_reference_free(looked_up_ref);
/* Lookup the other reference */
cl_git_pass(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's loose */
cl_assert(reference_is_packed(another_looked_up_ref) == 0);
/* Ensure the other ref still exists on the file system */
cl_assert(git_fs_path_exists(temp_path.ptr));
git_reference_free(renamed_ref);
git_reference_free(another_looked_up_ref);
git_str_dispose(&temp_path);
}
void test_refs_rename__name_collision(void)
{
/* can not rename a reference with the name of an existing reference */
git_reference *looked_up_ref, *renamed_ref;
/* An existing reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
/* Can not be renamed to the name of another existing reference. */
cl_git_fail(git_reference_rename(&renamed_ref, looked_up_ref, packed_test_head_name, 0, NULL));
git_reference_free(looked_up_ref);
/* Failure to rename it hasn't corrupted its state */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
cl_assert_equal_s(looked_up_ref->name, packed_head_name);
git_reference_free(looked_up_ref);
}
void test_refs_rename__invalid_name(void)
{
/* can not rename a reference with an invalid name */
git_reference *looked_up_ref, *renamed_ref;
/* An existing oid reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
/* Can not be renamed with an invalid name. */
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_reference_rename(&renamed_ref, looked_up_ref, "Hello! I'm a very invalid name.", 0, NULL));
/* Can not be renamed outside of the refs hierarchy
* unless it's ALL_CAPS_AND_UNDERSCORES.
*/
cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_rename(&renamed_ref, looked_up_ref, "i-will-sudo-you", 0, NULL));
/* Failure to rename it hasn't corrupted its state */
git_reference_free(looked_up_ref);
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
cl_assert_equal_s(looked_up_ref->name, packed_test_head_name);
git_reference_free(looked_up_ref);
}
void test_refs_rename__force_loose_packed(void)
{
/* can force-rename a packed reference with the name of an existing loose and packed reference */
git_reference *looked_up_ref, *renamed_ref;
git_oid oid;
/* An existing reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
git_oid_cpy(&oid, git_reference_target(looked_up_ref));
/* Can be force-renamed to the name of another existing reference. */
cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, packed_test_head_name, 1, NULL));
git_reference_free(looked_up_ref);
git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
cl_assert_equal_s(looked_up_ref->name, packed_test_head_name);
cl_assert_equal_oid(&oid, git_reference_target(looked_up_ref));
git_reference_free(looked_up_ref);
/* And that the previous one doesn't exist any longer */
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, packed_head_name));
}
void test_refs_rename__force_loose(void)
{
/* can force-rename a loose reference with the name of an existing loose reference */
git_reference *looked_up_ref, *renamed_ref;
git_oid oid;
/* An existing reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/br2"));
git_oid_cpy(&oid, git_reference_target(looked_up_ref));
/* Can be force-renamed to the name of another existing reference. */
cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, "refs/heads/test", 1, NULL));
git_reference_free(looked_up_ref);
git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/test"));
cl_assert_equal_s(looked_up_ref->name, "refs/heads/test");
cl_assert_equal_oid(&oid, git_reference_target(looked_up_ref));
git_reference_free(looked_up_ref);
/* And that the previous one doesn't exist any longer */
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, "refs/heads/br2"));
git_reference_free(looked_up_ref);
}
void test_refs_rename__overwrite(void)
{
/* can not overwrite name of existing reference */
git_reference *ref, *ref_one, *ref_one_new, *ref_two;
git_refdb *refdb;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
/* Create loose references */
cl_git_pass(git_reference_create(&ref_one, g_repo, ref_one_name, &id, 0, NULL));
cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0, NULL));
/* Pack everything */
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
/* Attempt to create illegal reference */
cl_git_fail(git_reference_create(&ref_one_new, g_repo, ref_one_name_new, &id, 0, NULL));
/* Illegal reference couldn't be created so this is supposed to fail */
cl_git_fail(git_reference_lookup(&ref_one_new, g_repo, ref_one_name_new));
git_reference_free(ref);
git_reference_free(ref_one);
git_reference_free(ref_one_new);
git_reference_free(ref_two);
git_refdb_free(refdb);
}
void test_refs_rename__prefix(void)
{
/* can be renamed to a new name prefixed with the old name */
git_reference *ref, *ref_two, *looked_up_ref, *renamed_ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
/* Create loose references */
cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name, &id, 0, NULL));
/* An existing reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));
/* Can be rename to a new name starting with the old name. */
cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, ref_two_name_new, 0, NULL));
git_reference_free(looked_up_ref);
git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
cl_assert_equal_s(looked_up_ref->name, ref_two_name_new);
git_reference_free(looked_up_ref);
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));
git_reference_free(ref);
git_reference_free(ref_two);
git_reference_free(looked_up_ref);
}
void test_refs_rename__move_up(void)
{
/* can move a reference to a upper reference hierarchy */
git_reference *ref, *ref_two, *looked_up_ref, *renamed_ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
/* Create loose references */
cl_git_pass(git_reference_create(&ref_two, g_repo, ref_two_name_new, &id, 0, NULL));
git_reference_free(ref_two);
/* An existing reference... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
/* Can be renamed upward the reference tree. */
cl_git_pass(git_reference_rename(&renamed_ref, looked_up_ref, ref_two_name, 0, NULL));
git_reference_free(looked_up_ref);
git_reference_free(renamed_ref);
/* Check we actually renamed it */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name));
cl_assert_equal_s(looked_up_ref->name, ref_two_name);
git_reference_free(looked_up_ref);
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, ref_two_name_new));
git_reference_free(ref);
git_reference_free(looked_up_ref);
}
void test_refs_rename__propagate_eexists(void)
{
git_reference *ref, *new_ref;
cl_git_pass(git_reference_lookup(&ref, g_repo, packed_head_name));
cl_assert_equal_i(GIT_EEXISTS, git_reference_rename(&new_ref, ref, packed_test_head_name, 0, NULL));
git_reference_free(ref);
}
| libgit2-main | tests/libgit2/refs/rename.c |
#include "clar_libgit2.h"
static bool is_valid_name(const char *name)
{
int valid;
cl_git_pass(git_reference_name_is_valid(&valid, name));
return valid;
}
void test_refs_isvalidname__can_detect_invalid_formats(void)
{
cl_assert_equal_i(false, is_valid_name("refs/tags/0.17.0^{}"));
cl_assert_equal_i(false, is_valid_name("TWO/LEVELS"));
cl_assert_equal_i(false, is_valid_name("ONE.LEVEL"));
cl_assert_equal_i(false, is_valid_name("HEAD/"));
cl_assert_equal_i(false, is_valid_name("NO_TRAILING_UNDERSCORE_"));
cl_assert_equal_i(false, is_valid_name("_NO_LEADING_UNDERSCORE"));
cl_assert_equal_i(false, is_valid_name("HEAD/aa"));
cl_assert_equal_i(false, is_valid_name("lower_case"));
cl_assert_equal_i(false, is_valid_name("/stupid/name/master"));
cl_assert_equal_i(false, is_valid_name("/"));
cl_assert_equal_i(false, is_valid_name("//"));
cl_assert_equal_i(false, is_valid_name(""));
cl_assert_equal_i(false, is_valid_name("refs/heads/sub.lock/webmatrix"));
}
void test_refs_isvalidname__wont_hopefully_choke_on_valid_formats(void)
{
cl_assert_equal_i(true, is_valid_name("refs/tags/0.17.0"));
cl_assert_equal_i(true, is_valid_name("refs/LEVELS"));
cl_assert_equal_i(true, is_valid_name("HEAD"));
cl_assert_equal_i(true, is_valid_name("ONE_LEVEL"));
cl_assert_equal_i(true, is_valid_name("refs/stash"));
cl_assert_equal_i(true, is_valid_name("refs/remotes/origin/bim_with_3d@11296"));
cl_assert_equal_i(true, is_valid_name("refs/master{yesterday"));
cl_assert_equal_i(true, is_valid_name("refs/master}yesterday"));
cl_assert_equal_i(true, is_valid_name("refs/master{yesterday}"));
}
| libgit2-main | tests/libgit2/refs/isvalidname.c |
#include "clar_libgit2.h"
#include "repository.h"
static void assert_shorthand(git_repository *repo, const char *refname, const char *shorthand)
{
git_reference *ref;
cl_git_pass(git_reference_lookup(&ref, repo, refname));
cl_assert_equal_s(git_reference_shorthand(ref), shorthand);
git_reference_free(ref);
}
void test_refs_shorthand__0(void)
{
git_repository *repo;
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
assert_shorthand(repo, "refs/heads/master", "master");
assert_shorthand(repo, "refs/tags/test", "test");
assert_shorthand(repo, "refs/remotes/test/master", "test/master");
assert_shorthand(repo, "refs/notes/fanout", "notes/fanout");
git_repository_free(repo);
}
| libgit2-main | tests/libgit2/refs/shorthand.c |
#include "clar_libgit2.h"
#include "git2/revparse.h"
#include "refs.h"
#include "path.h"
static git_repository *g_repo;
static git_object *g_obj;
/* Helpers */
static void test_object_and_ref_inrepo(
const char *spec,
const char *expected_oid,
const char *expected_refname,
git_repository *repo,
bool assert_reference_retrieval)
{
char objstr[64] = {0};
git_object *obj = NULL;
git_reference *ref = NULL;
int error;
error = git_revparse_ext(&obj, &ref, repo, spec);
if (expected_oid != NULL) {
cl_git_pass(error);
git_oid_fmt(objstr, git_object_id(obj));
cl_assert_equal_s(objstr, expected_oid);
} else
cl_git_fail(error);
if (assert_reference_retrieval) {
if (expected_refname == NULL)
cl_assert(NULL == ref);
else
cl_assert_equal_s(expected_refname, git_reference_name(ref));
}
git_object_free(obj);
git_reference_free(ref);
}
static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo)
{
test_object_and_ref_inrepo(spec, expected_oid, NULL, repo, false);
}
static void test_id_inrepo(
const char *spec,
const char *expected_left,
const char *expected_right,
git_revspec_t expected_flags,
git_repository *repo)
{
git_revspec revspec;
int error = git_revparse(&revspec, repo, spec);
if (expected_left) {
char str[64] = {0};
cl_assert_equal_i(0, error);
git_oid_fmt(str, git_object_id(revspec.from));
cl_assert_equal_s(str, expected_left);
git_object_free(revspec.from);
} else {
cl_assert_equal_i(GIT_ENOTFOUND, error);
}
if (expected_right) {
char str[64] = {0};
git_oid_fmt(str, git_object_id(revspec.to));
cl_assert_equal_s(str, expected_right);
git_object_free(revspec.to);
}
if (expected_flags)
cl_assert_equal_i(expected_flags, revspec.flags);
}
static void test_object(const char *spec, const char *expected_oid)
{
test_object_inrepo(spec, expected_oid, g_repo);
}
static void test_object_and_ref(const char *spec, const char *expected_oid, const char *expected_refname)
{
test_object_and_ref_inrepo(spec, expected_oid, expected_refname, g_repo, true);
}
static void test_rangelike(const char *rangelike,
const char *expected_left,
const char *expected_right,
git_revspec_t expected_revparseflags)
{
char objstr[64] = {0};
git_revspec revspec;
int error;
error = git_revparse(&revspec, g_repo, rangelike);
if (expected_left != NULL) {
cl_assert_equal_i(0, error);
cl_assert_equal_i(revspec.flags, expected_revparseflags);
git_oid_fmt(objstr, git_object_id(revspec.from));
cl_assert_equal_s(objstr, expected_left);
git_oid_fmt(objstr, git_object_id(revspec.to));
cl_assert_equal_s(objstr, expected_right);
} else
cl_assert(error != 0);
git_object_free(revspec.from);
git_object_free(revspec.to);
}
static void test_id(
const char *spec,
const char *expected_left,
const char *expected_right,
git_revspec_t expected_flags)
{
test_id_inrepo(spec, expected_left, expected_right, expected_flags, g_repo);
}
static void test_invalid_revspec(const char* invalid_spec)
{
git_revspec revspec;
cl_assert_equal_i(
GIT_EINVALIDSPEC, git_revparse(&revspec, g_repo, invalid_spec));
}
void test_refs_revparse__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
}
void test_refs_revparse__cleanup(void)
{
git_repository_free(g_repo);
}
void test_refs_revparse__nonexistant_object(void)
{
test_object("this-does-not-exist", NULL);
test_object("this-does-not-exist^1", NULL);
test_object("this-does-not-exist~2", NULL);
}
static void assert_invalid_single_spec(const char *invalid_spec)
{
cl_assert_equal_i(
GIT_EINVALIDSPEC, git_revparse_single(&g_obj, g_repo, invalid_spec));
}
void test_refs_revparse__invalid_reference_name(void)
{
assert_invalid_single_spec("this doesn't make sense");
assert_invalid_single_spec("Inv@{id");
assert_invalid_single_spec("");
}
void test_refs_revparse__shas(void)
{
test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
}
void test_refs_revparse__head(void)
{
test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD^0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
void test_refs_revparse__full_refs(void)
{
test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d");
test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
}
void test_refs_revparse__partial_refs(void)
{
test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f");
}
void test_refs_revparse__describe_output(void)
{
test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
void test_refs_revparse__nth_parent(void)
{
assert_invalid_single_spec("be3563a^-1");
assert_invalid_single_spec("^");
assert_invalid_single_spec("be3563a^{tree}^");
assert_invalid_single_spec("point_to_blob^{blob}^");
assert_invalid_single_spec("this doesn't make sense^1");
test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("be3563a^^", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("be3563a^{commit}^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("be3563a^42", NULL);
}
void test_refs_revparse__not_tag(void)
{
test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master^{tree}^{}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
test_object("e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
test_object("tags/e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
test_object("e908^{}", "e90810b8df3e80c413d903f631643c716887138d");
}
void test_refs_revparse__to_type(void)
{
assert_invalid_single_spec("wrapped_tag^{trip}");
test_object("point_to_blob^{commit}", NULL);
cl_assert_equal_i(
GIT_EPEEL, git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}"));
test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
test_object("master^{commit}^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
void test_refs_revparse__linear_history(void)
{
assert_invalid_single_spec("~");
test_object("foo~bar", NULL);
assert_invalid_single_spec("master~bar");
assert_invalid_single_spec("master~-1");
assert_invalid_single_spec("master~0bar");
assert_invalid_single_spec("this doesn't make sense~2");
assert_invalid_single_spec("be3563a^{tree}~");
assert_invalid_single_spec("point_to_blob^{blob}~");
test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("master~~", "9fd738e8f7967c078dceed8190330fc8648ee56a");
}
void test_refs_revparse__chaining(void)
{
assert_invalid_single_spec("master@{0}@{0}");
assert_invalid_single_spec("@{u}@{-1}");
assert_invalid_single_spec("@{-1}@{-1}");
assert_invalid_single_spec("@{-3}@{0}");
test_object("master@{0}~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("@{u}@{0}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("@{-1}@{0}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
test_object("@{-4}@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("master^^2^", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479");
test_object("master^^1^2^1", NULL);
}
void test_refs_revparse__upstream(void)
{
assert_invalid_single_spec("e90810b@{u}");
assert_invalid_single_spec("refs/tags/e90810b@{u}");
test_object("refs/heads/e90810b@{u}", NULL);
test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("refs/heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
}
void test_refs_revparse__ordinal(void)
{
assert_invalid_single_spec("master@{-2}");
/* TODO: make the test below actually fail
* cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{1a}"));
*/
test_object("nope@{0}", NULL);
test_object("master@{31415}", NULL);
test_object("@{1000}", NULL);
test_object("@{2}", NULL);
test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("refs/heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
}
void test_refs_revparse__previous_head(void)
{
assert_invalid_single_spec("@{-xyz}");
assert_invalid_single_spec("@{-0}");
assert_invalid_single_spec("@{-1b}");
test_object("@{-42}", NULL);
test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
}
static void create_fake_stash_reference_and_reflog(git_repository *repo)
{
git_reference *master, *new_master;
git_str log_path = GIT_STR_INIT;
git_str_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash");
cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&log_path)));
cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
cl_git_pass(git_reference_rename(&new_master, master, "refs/fakestash", 0, NULL));
git_reference_free(master);
cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&log_path)));
git_str_dispose(&log_path);
git_reference_free(new_master);
}
void test_refs_revparse__reflog_of_a_ref_under_refs(void)
{
git_repository *repo = cl_git_sandbox_init("testrepo.git");
test_object_inrepo("refs/fakestash", NULL, repo);
create_fake_stash_reference_and_reflog(repo);
/*
* $ git reflog -1 refs/fakestash
* a65fedf refs/fakestash@{0}: commit: checking in
*
* $ git reflog -1 refs/fakestash@{0}
* a65fedf refs/fakestash@{0}: commit: checking in
*
* $ git reflog -1 fakestash
* a65fedf fakestash@{0}: commit: checking in
*
* $ git reflog -1 fakestash@{0}
* a65fedf fakestash@{0}: commit: checking in
*/
test_object_inrepo("refs/fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
test_object_inrepo("refs/fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
test_object_inrepo("fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
test_object_inrepo("fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
cl_git_sandbox_cleanup();
}
void test_refs_revparse__revwalk(void)
{
test_object("master^{/not found in any commit}", NULL);
test_object("master^{/merge}", NULL);
assert_invalid_single_spec("master^{/((}");
test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a");
}
void test_refs_revparse__date(void)
{
/*
* $ git reflog HEAD --date=iso
* a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master
* a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in
* c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2
* a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit:
* be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes
*
* $ git reflog HEAD --date=raw
* a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master
* a4a7dce HEAD@{1335806617 -0900}: commit: checking in
* c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2
* a65fedf HEAD@{1335806603 -0900}: commit:
* be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour
*/
test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
/*
* $ git reflog master --date=iso
* a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in
* be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src...
*
* $ git reflog master --date=raw
* a65fedf master@{1335806603 -0800}: commit: checking in
* be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso
*/
/*
* $ git rev-parse "master@{2012-04-30 17:22:42 +0000}"
* warning: log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800
* be3563ae3f795b2b4353bcce3a527ad0a4f7f644
*/
test_object("master@{2012-04-30 17:22:42 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master@{2012-04-30 09:22:42 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
/*
* $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}"
* be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg
*/
test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
/*
* $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}"
* a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
*/
test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
/*
* $ git reflog -1 master@{2012-05-03}
* a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
*/
test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
/*
* $ git reflog -1 "master@{1335806603}"
* a65fedf
*
* $ git reflog -1 "master@{1335806602}"
* be3563a
*/
test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
/*
* $ git rev-parse "with-empty-log@{2 days ago}" --
* fatal: log for refs/heads/with-empty-log is empty
*/
test_object("with-empty-log@{2 days ago}", NULL);
}
void test_refs_revparse__invalid_date(void)
{
/*
* $ git rev-parse HEAD@{} --
* fatal: bad revision 'HEAD@{}'
*
* $ git rev-parse HEAD@{NEITHER_INTEGER_NOR_DATETIME} --
* fatal: bad revision 'HEAD@{NEITHER_INTEGER_NOR_DATETIME}'
*/
test_object("HEAD@{}", NULL);
test_object("HEAD@{NEITHER_INTEGER_NOR_DATETIME}", NULL);
}
void test_refs_revparse__colon(void)
{
assert_invalid_single_spec(":/");
assert_invalid_single_spec("point_to_blob:readme.txt");
cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); /* Not implemented */
test_object(":/not found in any commit", NULL);
test_object("subtrees:ab/42.txt", NULL);
test_object("subtrees:ab/4.txt/nope", NULL);
test_object("subtrees:nope", NULL);
test_object("test/master^1:branch_file.txt", NULL);
/* From tags */
test_object("test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
test_object("tags/test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
test_object("e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
test_object("tags/e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
/* From commits */
test_object("a65f:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
/* From trees */
test_object("a65f^{tree}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
test_object("944c:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
/* Retrieving trees */
test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12");
test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3");
test_object("subtrees:ab/", "f1425cef211cc08caa31e7b545ffb232acb098c3");
/* Retrieving blobs */
test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f");
test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b");
test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f");
test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9");
test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
test_object("test/master@{1}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
}
void test_refs_revparse__disambiguation(void)
{
/*
* $ git show e90810b
* tag e90810b
* Tagger: Vicent Marti <[email protected]>
* Date: Thu Aug 12 03:59:17 2010 +0200
*
* This is a very simple tag.
*
* commit e90810b8df3e80c413d903f631643c716887138d
* Author: Vicent Marti <[email protected]>
* Date: Thu Aug 5 18:42:20 2010 +0200
*
* Test commit 2
*
* diff --git a/readme.txt b/readme.txt
* index 6336846..0266163 100644
* --- a/readme.txt
* +++ b/readme.txt
* @@ -1 +1,2 @@
* Testing a readme.txt
* +Now we add a single line here
*
* $ git show-ref e90810b
* 7b4384978d2493e851f9cca7858815fac9b10980 refs/tags/e90810b
*
*/
test_object("e90810b", "7b4384978d2493e851f9cca7858815fac9b10980");
/*
* $ git show e90810
* commit e90810b8df3e80c413d903f631643c716887138d
* Author: Vicent Marti <[email protected]>
* Date: Thu Aug 5 18:42:20 2010 +0200
*
* Test commit 2
*
* diff --git a/readme.txt b/readme.txt
* index 6336846..0266163 100644
* --- a/readme.txt
* +++ b/readme.txt
* @@ -1 +1,2 @@
* Testing a readme.txt
* +Now we add a single line here
*/
test_object("e90810", "e90810b8df3e80c413d903f631643c716887138d");
}
void test_refs_revparse__a_too_short_objectid_returns_EAMBIGUOUS(void)
{
cl_assert_equal_i(
GIT_EAMBIGUOUS, git_revparse_single(&g_obj, g_repo, "e90"));
}
/*
* $ echo "aabqhq" | git hash-object -t blob --stdin
* dea509d0b3cb8ee0650f6ca210bc83f4678851ba
*
* $ echo "aaazvc" | git hash-object -t blob --stdin
* dea509d097ce692e167dfc6a48a7a280cc5e877e
*/
void test_refs_revparse__a_not_precise_enough_objectid_returns_EAMBIGUOUS(void)
{
git_repository *repo;
git_index *index;
git_object *obj;
repo = cl_git_sandbox_init("testrepo");
cl_git_mkfile("testrepo/one.txt", "aabqhq\n");
cl_git_mkfile("testrepo/two.txt", "aaazvc\n");
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_add_bypath(index, "one.txt"));
cl_git_pass(git_index_add_bypath(index, "two.txt"));
cl_git_fail_with(git_revparse_single(&obj, repo, "dea509d0"), GIT_EAMBIGUOUS);
cl_git_pass(git_revparse_single(&obj, repo, "dea509d09"));
git_object_free(obj);
git_index_free(index);
cl_git_sandbox_cleanup();
}
void test_refs_revparse__issue_994(void)
{
git_repository *repo;
git_reference *head, *with_at;
git_object *target;
repo = cl_git_sandbox_init("testrepo.git");
cl_assert_equal_i(GIT_ENOTFOUND,
git_revparse_single(&target, repo, "origin/bim_with_3d@11296"));
cl_assert_equal_i(GIT_ENOTFOUND,
git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296"));
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_create(
&with_at,
repo,
"refs/remotes/origin/bim_with_3d@11296",
git_reference_target(head),
0,
NULL));
cl_git_pass(git_revparse_single(&target, repo, "origin/bim_with_3d@11296"));
git_object_free(target);
cl_git_pass(git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296"));
git_object_free(target);
git_reference_free(with_at);
git_reference_free(head);
cl_git_sandbox_cleanup();
}
/**
* $ git rev-parse blah-7-gc47800c
* c47800c7266a2be04c571c04d5a6614691ea99bd
*
* $ git rev-parse HEAD~3
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*
* $ git branch blah-7-gc47800c HEAD~3
*
* $ git rev-parse blah-7-gc47800c
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*/
void test_refs_revparse__try_to_retrieve_branch_before_described_tag(void)
{
git_repository *repo;
git_reference *branch;
git_object *target;
char sha[GIT_OID_SHA1_HEXSIZE + 1];
repo = cl_git_sandbox_init("testrepo.git");
test_object_inrepo("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd", repo);
cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
cl_git_pass(git_branch_create(&branch, repo, "blah-7-gc47800c", (git_commit *)target, 0));
git_oid_tostr(sha, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target));
test_object_inrepo("blah-7-gc47800c", sha, repo);
git_reference_free(branch);
git_object_free(target);
cl_git_sandbox_cleanup();
}
/**
* $ git rev-parse a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* a65fedf39aefe402d3bb6e24df4d4f5fe4547750
*
* $ git rev-parse HEAD~3
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*
* $ git branch a65fedf39aefe402d3bb6e24df4d4f5fe4547750 HEAD~3
*
* $ git rev-parse a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* a65fedf39aefe402d3bb6e24df4d4f5fe4547750
*
* $ git rev-parse heads/a65fedf39aefe402d3bb6e24df4d4f5fe4547750
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*/
void test_refs_revparse__try_to_retrieve_sha_before_branch(void)
{
git_repository *repo;
git_reference *branch;
git_object *target;
char sha[GIT_OID_SHA1_HEXSIZE + 1];
repo = cl_git_sandbox_init("testrepo.git");
test_object_inrepo("a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
cl_git_pass(git_branch_create(&branch, repo, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", (git_commit *)target, 0));
git_oid_tostr(sha, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target));
test_object_inrepo("a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
test_object_inrepo("heads/a65fedf39aefe402d3bb6e24df4d4f5fe4547750", sha, repo);
git_reference_free(branch);
git_object_free(target);
cl_git_sandbox_cleanup();
}
/**
* $ git rev-parse c47800
* c47800c7266a2be04c571c04d5a6614691ea99bd
*
* $ git rev-parse HEAD~3
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*
* $ git branch c47800 HEAD~3
*
* $ git rev-parse c47800
* 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
*/
void test_refs_revparse__try_to_retrieve_branch_before_abbrev_sha(void)
{
git_repository *repo;
git_reference *branch;
git_object *target;
char sha[GIT_OID_SHA1_HEXSIZE + 1];
repo = cl_git_sandbox_init("testrepo.git");
test_object_inrepo("c47800", "c47800c7266a2be04c571c04d5a6614691ea99bd", repo);
cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
cl_git_pass(git_branch_create(&branch, repo, "c47800", (git_commit *)target, 0));
git_oid_tostr(sha, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target));
test_object_inrepo("c47800", sha, repo);
git_reference_free(branch);
git_object_free(target);
cl_git_sandbox_cleanup();
}
void test_refs_revparse__range(void)
{
assert_invalid_single_spec("be3563a^1..be3563a");
test_rangelike("be3563a^1..be3563a",
"9fd738e8f7967c078dceed8190330fc8648ee56a",
"be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
GIT_REVSPEC_RANGE);
test_rangelike("be3563a^1...be3563a",
"9fd738e8f7967c078dceed8190330fc8648ee56a",
"be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
test_rangelike("be3563a^1.be3563a", NULL, NULL, 0);
}
void test_refs_revparse__parses_range_operator(void)
{
test_id("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
test_id("HEAD~3..HEAD",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
GIT_REVSPEC_RANGE);
test_id("HEAD~3...HEAD",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
test_id("HEAD~3..",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
GIT_REVSPEC_RANGE);
test_id("HEAD~3...",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
test_id("..HEAD~3",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
GIT_REVSPEC_RANGE);
test_id("...HEAD~3",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
test_id("...",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
test_invalid_revspec("..");
}
void test_refs_revparse__ext_retrieves_both_the_reference_and_its_target(void)
{
test_object_and_ref(
"master@{upstream}",
"be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
"refs/remotes/test/master");
test_object_and_ref(
"@{-1}",
"a4a7dce85cf63874e984719f4fdd239f5145052f",
"refs/heads/br2");
}
void test_refs_revparse__ext_can_expand_short_reference_names(void)
{
test_object_and_ref(
"master",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"refs/heads/master");
test_object_and_ref(
"HEAD",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"refs/heads/master");
test_object_and_ref(
"tags/test",
"b25fa35b38051e4ae45d4222e795f9df2e43f1d1",
"refs/tags/test");
}
void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_revision(void)
{
test_object_and_ref(
"HEAD~3",
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
NULL);
test_object_and_ref(
"HEAD~0",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
NULL);
test_object_and_ref(
"HEAD^0",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
NULL);
test_object_and_ref(
"@{-1}@{0}",
"a4a7dce85cf63874e984719f4fdd239f5145052f",
NULL);
}
void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_tree_content(void)
{
test_object_and_ref(
"tags/test:readme.txt",
"0266163a49e280c4f5ed1e08facd36a2bd716bcf",
NULL);
}
void test_refs_revparse__uneven_sizes(void)
{
test_object("a65fedf39aefe402d3bb6e24df4d4f5fe454775",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("a65fedf39aefe402d3bb6e24df4d4f5fe45477",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("a65fedf39aefe402d3bb6e24df4d4f5fe4547",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
test_object("a65fedf39aefe402d3bb6e24df4d",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
void test_refs_revparse__parses_at_head(void)
{
test_id("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
test_id("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
test_id("@", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
}
| libgit2-main | tests/libgit2/refs/revparse.c |
#include "clar_libgit2.h"
#include "refs.h"
void test_refs_crashes__double_free(void)
{
git_repository *repo;
git_reference *ref, *ref2;
const char *REFNAME = "refs/heads/xxx";
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_reference_symbolic_create(&ref, repo, REFNAME, "refs/heads/master", 0, NULL));
cl_git_pass(git_reference_lookup(&ref2, repo, REFNAME));
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
git_reference_free(ref2);
/* reference is gone from disk, so reloading it will fail */
cl_git_fail(git_reference_lookup(&ref2, repo, REFNAME));
cl_git_sandbox_cleanup();
}
void test_refs_crashes__empty_packedrefs(void)
{
git_repository *repo;
git_reference *ref;
const char *REFNAME = "refs/heads/xxx";
git_str temp_path = GIT_STR_INIT;
int fd = 0;
repo = cl_git_sandbox_init("empty_bare.git");
/* create zero-length packed-refs file */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(repo), GIT_PACKEDREFS_FILE));
cl_git_pass(((fd = p_creat(temp_path.ptr, 0644)) < 0));
cl_git_pass(p_close(fd));
/* should fail gracefully */
cl_git_fail_with(
GIT_ENOTFOUND, git_reference_lookup(&ref, repo, REFNAME));
cl_git_sandbox_cleanup();
git_str_dispose(&temp_path);
}
| libgit2-main | tests/libgit2/refs/crashes.c |
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *g_repo;
void test_refs_update__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_update__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_update__updating_the_target_of_a_symref_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
git_reference *head;
cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(head));
git_reference_free(head);
cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create(&head, g_repo, GIT_HEAD_FILE, "refs/heads/inv@{id", 1, NULL));
}
| libgit2-main | tests/libgit2/refs/update.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "vector.h"
#include "odb.h"
static git_repository *repo;
void test_refs_iterator__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_iterator__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static const char *refnames[] = {
"refs/blobs/annotated_tag_to_blob",
"refs/heads/br2",
"refs/heads/cannot-fetch",
"refs/heads/chomped",
"refs/heads/haacked",
"refs/heads/master",
"refs/heads/not-good",
"refs/heads/packed",
"refs/heads/packed-test",
"refs/heads/subtrees",
"refs/heads/test",
"refs/heads/track-local",
"refs/heads/trailing",
"refs/heads/with-empty-log",
"refs/notes/fanout",
"refs/remotes/test/master",
"refs/tags/annotated_tag_to_blob",
"refs/tags/e90810b",
"refs/tags/hard_tag",
"refs/tags/point_to_blob",
"refs/tags/taggerless",
"refs/tags/test",
"refs/tags/wrapped_tag",
NULL
};
static const char *refnames_with_symlink[] = {
"refs/blobs/annotated_tag_to_blob",
"refs/heads/br2",
"refs/heads/cannot-fetch",
"refs/heads/chomped",
"refs/heads/haacked",
"refs/heads/link/a",
"refs/heads/link/b",
"refs/heads/link/c",
"refs/heads/link/d",
"refs/heads/master",
"refs/heads/not-good",
"refs/heads/packed",
"refs/heads/packed-test",
"refs/heads/subtrees",
"refs/heads/test",
"refs/heads/track-local",
"refs/heads/trailing",
"refs/heads/with-empty-log",
"refs/notes/fanout",
"refs/remotes/test/master",
"refs/tags/annotated_tag_to_blob",
"refs/tags/e90810b",
"refs/tags/hard_tag",
"refs/tags/point_to_blob",
"refs/tags/taggerless",
"refs/tags/test",
"refs/tags/wrapped_tag",
NULL
};
static int refcmp_cb(const void *a, const void *b)
{
const git_reference *refa = (const git_reference *)a;
const git_reference *refb = (const git_reference *)b;
return strcmp(refa->name, refb->name);
}
static void assert_all_refnames_match(const char **expected, git_vector *names)
{
size_t i;
git_reference *ref;
git_vector_sort(names);
git_vector_foreach(names, i, ref) {
cl_assert(expected[i] != NULL);
cl_assert_equal_s(expected[i], ref->name);
git_reference_free(ref);
}
cl_assert(expected[i] == NULL);
git_vector_free(names);
}
void test_refs_iterator__list(void)
{
git_reference_iterator *iter;
git_vector output;
git_reference *ref;
cl_git_pass(git_vector_init(&output, 33, &refcmp_cb));
cl_git_pass(git_reference_iterator_new(&iter, repo));
while (1) {
int error = git_reference_next(&ref, iter);
if (error == GIT_ITEROVER)
break;
cl_git_pass(error);
cl_git_pass(git_vector_insert(&output, ref));
}
git_reference_iterator_free(iter);
assert_all_refnames_match(refnames, &output);
}
void test_refs_iterator__empty(void)
{
git_reference_iterator *iter;
git_odb *odb;
git_reference *ref;
git_repository *empty;
cl_git_pass(git_odb__new(&odb, NULL));
cl_git_pass(git_repository_wrap_odb(&empty, odb));
cl_git_pass(git_reference_iterator_new(&iter, empty));
cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&ref, iter));
git_reference_iterator_free(iter);
git_odb_free(odb);
git_repository_free(empty);
}
static int refs_foreach_cb(git_reference *reference, void *payload)
{
git_vector *output = payload;
cl_git_pass(git_vector_insert(output, reference));
return 0;
}
void test_refs_iterator__foreach(void)
{
git_vector output;
cl_git_pass(git_vector_init(&output, 33, &refcmp_cb));
cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
assert_all_refnames_match(refnames, &output);
}
void test_refs_iterator__foreach_through_symlink(void)
{
git_vector output;
#ifdef GIT_WIN32
cl_skip();
#endif
cl_git_pass(git_vector_init(&output, 32, &refcmp_cb));
cl_git_pass(p_mkdir("refs", 0777));
cl_git_mkfile("refs/a", "1234567890123456789012345678901234567890");
cl_git_mkfile("refs/b", "1234567890123456789012345678901234567890");
cl_git_mkfile("refs/c", "1234567890123456789012345678901234567890");
cl_git_mkfile("refs/d", "1234567890123456789012345678901234567890");
cl_git_pass(p_symlink("../../../refs", "testrepo.git/refs/heads/link"));
cl_git_pass(git_reference_foreach(repo, refs_foreach_cb, &output));
assert_all_refnames_match(refnames_with_symlink, &output);
}
static int refs_foreach_cancel_cb(git_reference *reference, void *payload)
{
int *cancel_after = payload;
git_reference_free(reference);
if (!*cancel_after)
return -333;
(*cancel_after)--;
return 0;
}
void test_refs_iterator__foreach_can_cancel(void)
{
int cancel_after = 3;
cl_git_fail_with(
git_reference_foreach(repo, refs_foreach_cancel_cb, &cancel_after),
-333);
cl_assert_equal_i(0, cancel_after);
}
static int refs_foreach_name_cb(const char *name, void *payload)
{
git_vector *output = payload;
cl_git_pass(git_vector_insert(output, git__strdup(name)));
return 0;
}
void test_refs_iterator__foreach_name(void)
{
git_vector output;
size_t i;
char *name;
cl_git_pass(git_vector_init(&output, 32, &git__strcmp_cb));
cl_git_pass(
git_reference_foreach_name(repo, refs_foreach_name_cb, &output));
git_vector_sort(&output);
git_vector_foreach(&output, i, name) {
cl_assert(refnames[i] != NULL);
cl_assert_equal_s(refnames[i], name);
git__free(name);
}
git_vector_free(&output);
}
static int refs_foreach_name_cancel_cb(const char *name, void *payload)
{
int *cancel_after = payload;
if (!*cancel_after)
return -333;
GIT_UNUSED(name);
(*cancel_after)--;
return 0;
}
void test_refs_iterator__foreach_name_can_cancel(void)
{
int cancel_after = 5;
cl_git_fail_with(
git_reference_foreach_name(
repo, refs_foreach_name_cancel_cb, &cancel_after),
-333);
cl_assert_equal_i(0, cancel_after);
}
void test_refs_iterator__concurrent_delete(void)
{
git_reference_iterator *iter;
size_t full_count = 0, concurrent_count = 0;
const char *name;
int error;
cl_git_sandbox_cleanup();
repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_reference_iterator_new(&iter, repo));
while ((error = git_reference_next_name(&name, iter)) == 0) {
full_count++;
}
git_reference_iterator_free(iter);
cl_assert_equal_i(GIT_ITEROVER, error);
cl_git_pass(git_reference_iterator_new(&iter, repo));
while ((error = git_reference_next_name(&name, iter)) == 0) {
cl_git_pass(git_reference_remove(repo, name));
concurrent_count++;
}
git_reference_iterator_free(iter);
cl_assert_equal_i(GIT_ITEROVER, error);
cl_assert_equal_i(full_count, concurrent_count);
}
| libgit2-main | tests/libgit2/refs/iterator.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *current_master_tip = "099fabac3a9ea935598528c27f866e34089c2eff";
static const char *current_head_target = "refs/heads/master";
static git_repository *g_repo;
void test_refs_create__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
p_fsync__cnt = 0;
}
void test_refs_create__cleanup(void)
{
cl_git_sandbox_cleanup();
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 1));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
}
void test_refs_create__symbolic(void)
{
/* create a new symbolic reference */
git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_repository *repo2;
git_oid id;
const char *new_head_tracker = "ANOTHER_HEAD_TRACKER";
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
/* Create and write the new symbolic reference */
cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0, NULL));
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_assert(git_reference_type(looked_up_ref) & GIT_REFERENCE_SYMBOLIC);
cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
/* ...peeled.. */
cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
cl_assert(git_reference_type(resolved_ref) == GIT_REFERENCE_DIRECT);
/* ...and that it points to the current master tip */
cl_assert_equal_oid(&id, git_reference_target(resolved_ref));
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
/* Similar test with a fresh new repository */
cl_git_pass(git_repository_open(&repo2, "testrepo"));
cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker));
cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
cl_assert_equal_oid(&id, git_reference_target(resolved_ref));
git_repository_free(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
}
void test_refs_create__symbolic_with_arbitrary_content(void)
{
git_reference *new_reference, *looked_up_ref;
git_repository *repo2;
git_oid id;
const char *new_head_tracker = "ANOTHER_HEAD_TRACKER";
const char *arbitrary_target = "ARBITRARY DATA";
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
/* Attempt to create symbolic ref with arbitrary data in target
* fails by default
*/
cl_git_fail(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, arbitrary_target, 0, NULL));
git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 0);
/* With strict target validation disabled, ref creation succeeds */
cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, arbitrary_target, 0, NULL));
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_assert(git_reference_type(looked_up_ref) & GIT_REFERENCE_SYMBOLIC);
cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
git_reference_free(looked_up_ref);
/* Ensure the target is what we expect it to be */
cl_assert_equal_s(git_reference_symbolic_target(new_reference), arbitrary_target);
/* Similar test with a fresh new repository object */
cl_git_pass(git_repository_open(&repo2, "testrepo"));
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker));
cl_assert(git_reference_type(looked_up_ref) & GIT_REFERENCE_SYMBOLIC);
cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head_tracker);
/* Ensure the target is what we expect it to be */
cl_assert_equal_s(git_reference_symbolic_target(new_reference), arbitrary_target);
git_repository_free(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
}
void test_refs_create__deep_symbolic(void)
{
/* create a deep symbolic reference */
git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_oid id;
const char *new_head_tracker = "deep/rooted/tracker";
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_symbolic_create(&new_reference, g_repo, new_head_tracker, current_head_target, 0, NULL));
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker));
cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
cl_assert_equal_oid(&id, git_reference_target(resolved_ref));
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
git_reference_free(resolved_ref);
}
void test_refs_create__oid(void)
{
/* create a new OID reference */
git_reference *new_reference, *looked_up_ref;
git_repository *repo2;
git_oid id;
const char *new_head = "refs/heads/new-head";
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
/* Create and write the new object id reference */
cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL));
/* Ensure the reference can be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
cl_assert(git_reference_type(looked_up_ref) & GIT_REFERENCE_DIRECT);
cl_assert(reference_is_packed(looked_up_ref) == 0);
cl_assert_equal_s(looked_up_ref->name, new_head);
/* ...and that it points to the current master tip */
cl_assert_equal_oid(&id, git_reference_target(looked_up_ref));
git_reference_free(looked_up_ref);
/* Similar test with a fresh new repository */
cl_git_pass(git_repository_open(&repo2, "testrepo"));
cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head));
cl_assert_equal_oid(&id, git_reference_target(looked_up_ref));
git_repository_free(repo2);
git_reference_free(new_reference);
git_reference_free(looked_up_ref);
}
/* Can by default create a reference that targets at an unknown id */
void test_refs_create__oid_unknown_succeeds_without_strict(void)
{
git_reference *new_reference, *looked_up_ref;
git_oid id;
const char *new_head = "refs/heads/new-head";
git_oid__fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1);
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
/* Create and write the new object id reference */
cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL));
git_reference_free(new_reference);
/* Ensure the reference can't be looked-up... */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head));
git_reference_free(looked_up_ref);
}
/* Strict object enforcement enforces valid object id */
void test_refs_create__oid_unknown_fails_by_default(void)
{
git_reference *new_reference, *looked_up_ref;
git_oid id;
const char *new_head = "refs/heads/new-head";
git_oid__fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1);
/* Create and write the new object id reference */
cl_git_fail(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL));
/* Ensure the reference can't be looked-up... */
cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head));
}
void test_refs_create__propagate_eexists(void)
{
git_oid oid;
/* Make sure it works for oid and for symbolic both */
cl_git_pass(git_oid__fromstr(&oid, current_master_tip, GIT_OID_SHA1));
cl_git_fail_with(GIT_EEXISTS, git_reference_create(NULL, g_repo, current_head_target, &oid, false, NULL));
cl_git_fail_with(GIT_EEXISTS, git_reference_symbolic_create(NULL, g_repo, "HEAD", current_head_target, false, NULL));
}
void test_refs_create__existing_dir_propagates_edirectory(void)
{
git_reference *new_reference, *fail_reference;
git_oid id;
const char *dir_head = "refs/heads/new-dir/new-head",
*fail_head = "refs/heads/new-dir";
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
/* Create and write the new object id reference */
cl_git_pass(git_reference_create(&new_reference, g_repo, dir_head, &id, 1, NULL));
cl_git_fail_with(GIT_EDIRECTORY,
git_reference_create(&fail_reference, g_repo, fail_head, &id, false, NULL));
git_reference_free(new_reference);
}
static void test_invalid_name(const char *name)
{
git_reference *new_reference;
git_oid id;
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_create(
&new_reference, g_repo, name, &id, 0, NULL));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_reference_symbolic_create(
&new_reference, g_repo, name, current_head_target, 0, NULL));
}
void test_refs_create__creating_a_reference_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
test_invalid_name("refs/heads/inv@{id");
test_invalid_name("refs/heads/back\\slash");
test_invalid_name("refs/heads/foo ");
test_invalid_name("refs/heads/foo /bar");
test_invalid_name("refs/heads/com1:bar/foo");
test_invalid_name("refs/heads/e:");
test_invalid_name("refs/heads/c:/foo");
test_invalid_name("refs/heads/foo.");
}
static void test_win32_name(const char *name)
{
git_reference *new_reference = NULL;
git_oid id;
int ret;
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
ret = git_reference_create(&new_reference, g_repo, name, &id, 0, NULL);
#ifdef GIT_WIN32
cl_assert_equal_i(GIT_EINVALIDSPEC, ret);
#else
cl_git_pass(ret);
#endif
git_reference_free(new_reference);
}
void test_refs_create__creating_a_loose_ref_with_invalid_windows_name(void)
{
test_win32_name("refs/heads/foo./bar");
test_win32_name("refs/heads/aux");
test_win32_name("refs/heads/aux.foo/bar");
test_win32_name("refs/heads/com1");
}
/* Creating a loose ref involves fsync'ing the reference, the
* reflog and (on non-Windows) the containing directories.
* Creating a packed ref involves fsync'ing the packed ref file
* and (on non-Windows) the containing directory.
*/
#ifdef GIT_WIN32
static int expected_fsyncs_create = 2, expected_fsyncs_compress = 1;
#else
static int expected_fsyncs_create = 4, expected_fsyncs_compress = 2;
#endif
static void count_fsyncs(size_t *create_count, size_t *compress_count)
{
git_reference *ref = NULL;
git_refdb *refdb;
git_oid id;
p_fsync__cnt = 0;
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
git_reference_free(ref);
*create_count = p_fsync__cnt;
p_fsync__cnt = 0;
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
git_refdb_free(refdb);
*compress_count = p_fsync__cnt;
p_fsync__cnt = 0;
}
void test_refs_create__does_not_fsync_by_default(void)
{
size_t create_count, compress_count;
count_fsyncs(&create_count, &compress_count);
cl_assert_equal_i(0, create_count);
cl_assert_equal_i(0, compress_count);
}
void test_refs_create__fsyncs_when_global_opt_set(void)
{
size_t create_count, compress_count;
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
count_fsyncs(&create_count, &compress_count);
cl_assert_equal_i(expected_fsyncs_create, create_count);
cl_assert_equal_i(expected_fsyncs_compress, compress_count);
}
void test_refs_create__fsyncs_when_repo_config_set(void)
{
size_t create_count, compress_count;
cl_repo_set_bool(g_repo, "core.fsyncObjectFiles", true);
count_fsyncs(&create_count, &compress_count);
cl_assert_equal_i(expected_fsyncs_create, create_count);
cl_assert_equal_i(expected_fsyncs_compress, compress_count);
}
| libgit2-main | tests/libgit2/refs/create.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist";
static const char *head_tracker_sym_ref_name = "HEAD_TRACKER";
static const char *current_head_target = "refs/heads/master";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static const char *packed_head_name = "refs/heads/packed";
static const char *packed_test_head_name = "refs/heads/packed-test";
static git_repository *g_repo;
void test_refs_read__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
}
void test_refs_read__cleanup(void)
{
git_repository_free(g_repo);
g_repo = NULL;
}
void test_refs_read__loose_tag(void)
{
/* lookup a loose tag reference */
git_reference *reference;
git_object *object;
git_str ref_name_from_tag_name = GIT_STR_INIT;
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(git_reference_type(reference) & GIT_REFERENCE_DIRECT);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, loose_tag_ref_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJECT_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJECT_TAG);
/* Ensure the name of the tag matches the name of the reference */
cl_git_pass(git_str_joinpath(&ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object)));
cl_assert_equal_s(ref_name_from_tag_name.ptr, loose_tag_ref_name);
git_str_dispose(&ref_name_from_tag_name);
git_object_free(object);
git_reference_free(reference);
}
void test_refs_read__nonexisting_tag(void)
{
/* lookup a loose tag reference that doesn't exist */
git_reference *reference;
cl_git_fail(git_reference_lookup(&reference, g_repo, non_existing_tag_ref_name));
git_reference_free(reference);
}
void test_refs_read__symbolic(void)
{
/* lookup a symbolic reference */
git_reference *reference, *resolved_ref;
git_object *object;
git_oid id;
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_assert(git_reference_type(reference) & GIT_REFERENCE_SYMBOLIC);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, GIT_HEAD_FILE);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert(git_reference_type(resolved_ref) == GIT_REFERENCE_DIRECT);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJECT_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJECT_COMMIT);
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_assert_equal_oid(&id, git_object_id(object));
git_object_free(object);
git_reference_free(reference);
git_reference_free(resolved_ref);
}
void test_refs_read__nested_symbolic(void)
{
/* lookup a nested symbolic reference */
git_reference *reference, *resolved_ref;
git_object *object;
git_oid id;
cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
cl_assert(git_reference_type(reference) & GIT_REFERENCE_SYMBOLIC);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, head_tracker_sym_ref_name);
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert(git_reference_type(resolved_ref) == GIT_REFERENCE_DIRECT);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(resolved_ref), GIT_OBJECT_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJECT_COMMIT);
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_assert_equal_oid(&id, git_object_id(object));
git_object_free(object);
git_reference_free(reference);
git_reference_free(resolved_ref);
}
void test_refs_read__head_then_master(void)
{
/* lookup the HEAD and resolve the master branch */
git_reference *reference, *resolved_ref, *comp_base_ref;
cl_git_pass(git_reference_lookup(&reference, g_repo, head_tracker_sym_ref_name));
cl_git_pass(git_reference_resolve(&comp_base_ref, reference));
git_reference_free(reference);
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert_equal_oid(git_reference_target(comp_base_ref), git_reference_target(resolved_ref));
git_reference_free(reference);
git_reference_free(resolved_ref);
cl_git_pass(git_reference_lookup(&reference, g_repo, current_head_target));
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert_equal_oid(git_reference_target(comp_base_ref), git_reference_target(resolved_ref));
git_reference_free(reference);
git_reference_free(resolved_ref);
git_reference_free(comp_base_ref);
}
void test_refs_read__master_then_head(void)
{
/* lookup the master branch and then the HEAD */
git_reference *reference, *master_ref, *resolved_ref;
cl_git_pass(git_reference_lookup(&master_ref, g_repo, current_head_target));
cl_git_pass(git_reference_lookup(&reference, g_repo, GIT_HEAD_FILE));
cl_git_pass(git_reference_resolve(&resolved_ref, reference));
cl_assert_equal_oid(git_reference_target(master_ref), git_reference_target(resolved_ref));
git_reference_free(reference);
git_reference_free(resolved_ref);
git_reference_free(master_ref);
}
void test_refs_read__packed(void)
{
/* lookup a packed reference */
git_reference *reference;
git_object *object;
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
cl_assert(git_reference_type(reference) & GIT_REFERENCE_DIRECT);
cl_assert(reference_is_packed(reference));
cl_assert_equal_s(reference->name, packed_head_name);
cl_git_pass(git_object_lookup(&object, g_repo, git_reference_target(reference), GIT_OBJECT_ANY));
cl_assert(object != NULL);
cl_assert(git_object_type(object) == GIT_OBJECT_COMMIT);
git_object_free(object);
git_reference_free(reference);
}
void test_refs_read__loose_first(void)
{
/* assure that a loose reference is looked up before a packed reference */
git_reference *reference;
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_head_name));
git_reference_free(reference);
cl_git_pass(git_reference_lookup(&reference, g_repo, packed_test_head_name));
cl_assert(git_reference_type(reference) & GIT_REFERENCE_DIRECT);
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, packed_test_head_name);
git_reference_free(reference);
}
void test_refs_read__chomped(void)
{
git_reference *test, *chomped;
cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
cl_git_pass(git_reference_lookup(&chomped, g_repo, "refs/heads/chomped"));
cl_assert_equal_oid(git_reference_target(test), git_reference_target(chomped));
git_reference_free(test);
git_reference_free(chomped);
}
void test_refs_read__trailing(void)
{
git_reference *test, *trailing;
cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
cl_git_pass(git_reference_lookup(&trailing, g_repo, "refs/heads/trailing"));
cl_assert_equal_oid(git_reference_target(test), git_reference_target(trailing));
git_reference_free(trailing);
cl_git_pass(git_reference_lookup(&trailing, g_repo, "FETCH_HEAD"));
git_reference_free(test);
git_reference_free(trailing);
}
void test_refs_read__unfound_return_ENOTFOUND(void)
{
git_reference *reference;
git_oid id;
cl_assert_equal_i(GIT_ENOTFOUND,
git_reference_lookup(&reference, g_repo, "TEST_MASTER"));
cl_assert_equal_i(GIT_ENOTFOUND,
git_reference_lookup(&reference, g_repo, "refs/test/master"));
cl_assert_equal_i(GIT_ENOTFOUND,
git_reference_lookup(&reference, g_repo, "refs/tags/test/master"));
cl_assert_equal_i(GIT_ENOTFOUND,
git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master"));
cl_assert_equal_i(GIT_ENOTFOUND,
git_reference_name_to_id(&id, g_repo, "refs/tags/test/farther/master"));
}
static void assert_is_branch(const char *name, bool expected_branchness)
{
git_reference *reference;
cl_git_pass(git_reference_lookup(&reference, g_repo, name));
cl_assert_equal_i(expected_branchness, git_reference_is_branch(reference));
git_reference_free(reference);
}
void test_refs_read__can_determine_if_a_reference_is_a_local_branch(void)
{
assert_is_branch("refs/heads/master", true);
assert_is_branch("refs/heads/packed", true);
assert_is_branch("refs/remotes/test/master", false);
assert_is_branch("refs/tags/e90810b", false);
}
static void assert_is_tag(const char *name, bool expected_tagness)
{
git_reference *reference;
cl_git_pass(git_reference_lookup(&reference, g_repo, name));
cl_assert_equal_i(expected_tagness, git_reference_is_tag(reference));
git_reference_free(reference);
}
void test_refs_read__can_determine_if_a_reference_is_a_tag(void)
{
assert_is_tag("refs/tags/e90810b", true);
assert_is_tag("refs/tags/test", true);
assert_is_tag("refs/heads/packed", false);
assert_is_tag("refs/remotes/test/master", false);
}
static void assert_is_note(const char *name, bool expected_noteness)
{
git_reference *reference;
cl_git_pass(git_reference_lookup(&reference, g_repo, name));
cl_assert_equal_i(expected_noteness, git_reference_is_note(reference));
git_reference_free(reference);
}
void test_refs_read__can_determine_if_a_reference_is_a_note(void)
{
assert_is_note("refs/notes/fanout", true);
assert_is_note("refs/heads/packed", false);
assert_is_note("refs/remotes/test/master", false);
}
void test_refs_read__invalid_name_returns_EINVALIDSPEC(void)
{
git_reference *reference;
git_oid id;
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_reference_lookup(&reference, g_repo, "refs/heads/Inv@{id"));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_reference_name_to_id(&id, g_repo, "refs/heads/Inv@{id"));
}
| libgit2-main | tests/libgit2/refs/read.c |
#include "git2/repository.h"
#include "git2/refs.h"
#include "common.h"
#include "util.h"
#include "path.h"
#include "ref_helpers.h"
int reference_is_packed(git_reference *ref)
{
git_str ref_path = GIT_STR_INIT;
int packed;
assert(ref);
if (git_str_joinpath(&ref_path,
git_repository_path(git_reference_owner(ref)),
git_reference_name(ref)) < 0)
return -1;
packed = !git_fs_path_isfile(ref_path.ptr);
git_str_dispose(&ref_path);
return packed;
}
| libgit2-main | tests/libgit2/refs/ref_helpers.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "git2/reflog.h"
#include "git2/refdb.h"
#include "reflog.h"
#include "refs.h"
#include "ref_helpers.h"
static const char *loose_tag_ref_name = "refs/tags/e90810b";
static git_repository *g_repo;
void test_refs_pack__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_pack__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void packall(void)
{
git_refdb *refdb;
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
git_refdb_free(refdb);
}
void test_refs_pack__empty(void)
{
/* create a packfile for an empty folder */
git_str temp_path = GIT_STR_INIT;
cl_git_pass(git_str_join_n(&temp_path, '/', 3, git_repository_path(g_repo), GIT_REFS_HEADS_DIR, "empty_dir"));
cl_git_pass(git_futils_mkdir_r(temp_path.ptr, GIT_REFS_DIR_MODE));
git_str_dispose(&temp_path);
packall();
}
void test_refs_pack__loose(void)
{
/* create a packfile from all the loose refs in a repo */
git_reference *reference;
git_str temp_path = GIT_STR_INIT;
/* Ensure a known loose ref can be looked up */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(reference_is_packed(reference) == 0);
cl_assert_equal_s(reference->name, loose_tag_ref_name);
git_reference_free(reference);
/*
* We are now trying to pack also a loose reference
* called `points_to_blob`, to make sure we can properly
* pack weak tags
*/
packall();
/* Ensure the packed-refs file exists */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), GIT_PACKEDREFS_FILE));
cl_assert(git_fs_path_exists(temp_path.ptr));
/* Ensure the known ref can still be looked up but is now packed */
cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
cl_assert(reference_is_packed(reference));
cl_assert_equal_s(reference->name, loose_tag_ref_name);
/* Ensure the known ref has been removed from the loose folder structure */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), loose_tag_ref_name));
cl_assert(!git_fs_path_exists(temp_path.ptr));
git_reference_free(reference);
git_str_dispose(&temp_path);
}
void test_refs_pack__symbolic(void)
{
/* create a packfile from loose refs skipping symbolic refs */
int i;
git_oid head;
git_reference *ref;
char name[128];
cl_git_pass(git_reference_name_to_id(&head, g_repo, "HEAD"));
/* make a bunch of references */
for (i = 0; i < 100; ++i) {
p_snprintf(name, sizeof(name), "refs/heads/symbolic-%03d", i);
cl_git_pass(git_reference_symbolic_create(
&ref, g_repo, name, "refs/heads/master", 0, NULL));
git_reference_free(ref);
p_snprintf(name, sizeof(name), "refs/heads/direct-%03d", i);
cl_git_pass(git_reference_create(&ref, g_repo, name, &head, 0, NULL));
git_reference_free(ref);
}
packall();
}
| libgit2-main | tests/libgit2/refs/pack.c |
#include "clar_libgit2.h"
#include "git2/transaction.h"
static git_repository *g_repo;
static git_transaction *g_tx;
void test_refs_transactions__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_transaction_new(&g_tx, g_repo));
}
void test_refs_transactions__cleanup(void)
{
git_transaction_free(g_tx);
cl_git_sandbox_cleanup();
}
void test_refs_transactions__single_ref_oid(void)
{
git_reference *ref;
git_oid id;
git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
cl_git_pass(git_transaction_set_target(g_tx, "refs/heads/master", &id, NULL, NULL));
cl_git_pass(git_transaction_commit(g_tx));
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
git_reference_free(ref);
}
void test_refs_transactions__single_ref_symbolic(void)
{
git_reference *ref;
cl_git_pass(git_transaction_lock_ref(g_tx, "HEAD"));
cl_git_pass(git_transaction_set_symbolic_target(g_tx, "HEAD", "refs/heads/foo", NULL, NULL));
cl_git_pass(git_transaction_commit(g_tx));
cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
cl_assert_equal_s("refs/heads/foo", git_reference_symbolic_target(ref));
git_reference_free(ref);
}
void test_refs_transactions__single_ref_mix_types(void)
{
git_reference *ref;
git_oid id;
git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
cl_git_pass(git_transaction_lock_ref(g_tx, "HEAD"));
cl_git_pass(git_transaction_set_symbolic_target(g_tx, "refs/heads/master", "refs/heads/foo", NULL, NULL));
cl_git_pass(git_transaction_set_target(g_tx, "HEAD", &id, NULL, NULL));
cl_git_pass(git_transaction_commit(g_tx));
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
cl_assert_equal_s("refs/heads/foo", git_reference_symbolic_target(ref));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
git_reference_free(ref);
}
void test_refs_transactions__single_ref_delete(void)
{
git_reference *ref;
cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
cl_git_pass(git_transaction_remove(g_tx, "refs/heads/master"));
cl_git_pass(git_transaction_commit(g_tx));
cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo, "refs/heads/master"));
}
void test_refs_transactions__single_create(void)
{
git_reference *ref;
const char *name = "refs/heads/new-branch";
git_oid id;
cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, g_repo, name));
cl_git_pass(git_transaction_lock_ref(g_tx, name));
git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
cl_git_pass(git_transaction_set_target(g_tx, name, &id, NULL, NULL));
cl_git_pass(git_transaction_commit(g_tx));
cl_git_pass(git_reference_lookup(&ref, g_repo, name));
cl_assert(!git_oid_cmp(&id, git_reference_target(ref)));
git_reference_free(ref);
}
void test_refs_transactions__unlocked_set(void)
{
git_oid id;
cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
cl_git_fail_with(GIT_ENOTFOUND, git_transaction_set_target(g_tx, "refs/heads/foo", &id, NULL, NULL));
cl_git_pass(git_transaction_commit(g_tx));
}
void test_refs_transactions__error_on_locking_locked_ref(void)
{
git_oid id;
git_transaction *g_tx_with_lock;
git_repository *g_repo_with_locking_tx;
const char *g_repo_path = git_repository_path(g_repo);
/* prepare a separate transaction in another instance of testrepo and lock master */
cl_git_pass(git_repository_open(&g_repo_with_locking_tx, g_repo_path));
cl_git_pass(git_transaction_new(&g_tx_with_lock, g_repo_with_locking_tx));
cl_git_pass(git_transaction_lock_ref(g_tx_with_lock, "refs/heads/master"));
/* lock reference for set_target */
cl_git_pass(git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_fail_with(GIT_ELOCKED, git_transaction_lock_ref(g_tx, "refs/heads/master"));
cl_git_fail_with(GIT_ENOTFOUND, git_transaction_set_target(g_tx, "refs/heads/master", &id, NULL, NULL));
git_transaction_free(g_tx_with_lock);
git_repository_free(g_repo_with_locking_tx);
}
void test_refs_transactions__commit_unlocks_unmodified_ref(void)
{
git_transaction *second_tx;
cl_git_pass(git_transaction_new(&second_tx, g_repo));
cl_git_pass(git_transaction_lock_ref(second_tx, "refs/heads/master"));
cl_git_pass(git_transaction_commit(second_tx));
/* a transaction must now be able to get the lock */
cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
git_transaction_free(second_tx);
}
void test_refs_transactions__free_unlocks_unmodified_ref(void)
{
git_transaction *second_tx;
cl_git_pass(git_transaction_new(&second_tx, g_repo));
cl_git_pass(git_transaction_lock_ref(second_tx, "refs/heads/master"));
git_transaction_free(second_tx);
/* a transaction must now be able to get the lock */
cl_git_pass(git_transaction_lock_ref(g_tx, "refs/heads/master"));
}
| libgit2-main | tests/libgit2/refs/transactions.c |
#include "clar_libgit2.h"
#include "posix.h"
static git_repository *repo;
static git_strarray ref_list;
static void ensure_no_refname_starts_with_a_forward_slash(const char *path)
{
size_t i;
cl_git_pass(git_repository_open(&repo, path));
cl_git_pass(git_reference_list(&ref_list, repo));
cl_assert(ref_list.count > 0);
for (i = 0; i < ref_list.count; i++)
cl_assert(git__prefixcmp(ref_list.strings[i], "/") != 0);
git_strarray_dispose(&ref_list);
git_repository_free(repo);
}
void test_refs_listall__from_repository_opened_through_workdir_path(void)
{
cl_fixture_sandbox("status");
cl_git_pass(p_rename("status/.gitted", "status/.git"));
ensure_no_refname_starts_with_a_forward_slash("status");
cl_fixture_cleanup("status");
}
void test_refs_listall__from_repository_opened_through_gitdir_path(void)
{
ensure_no_refname_starts_with_a_forward_slash(cl_fixture("testrepo.git"));
}
void test_refs_listall__from_repository_with_no_trailing_newline(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("bad_tag.git")));
cl_git_pass(git_reference_list(&ref_list, repo));
cl_assert(ref_list.count > 0);
git_strarray_dispose(&ref_list);
git_repository_free(repo);
}
| libgit2-main | tests/libgit2/refs/listall.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *commit_id = "099fabac3a9ea935598528c27f866e34089c2eff";
static const char *refname = "refs/heads/master";
static const char *other_refname = "refs/heads/foo";
static const char *other_commit_id = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static git_repository *g_repo;
void test_refs_races__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_races__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_races__create_matching_zero_old(void)
{
git_reference *ref;
git_oid id, zero_id;
git_oid__fromstr(&id, commit_id, GIT_OID_SHA1);
git_oid__fromstr(&zero_id, "0000000000000000000000000000000000000000", GIT_OID_SHA1);
cl_git_fail(git_reference_create_matching(&ref, g_repo, refname, &id, 1, &zero_id, NULL));
git_reference_free(ref);
cl_git_pass(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
git_reference_free(ref);
cl_git_fail(git_reference_create_matching(&ref, g_repo, other_refname, &id, 1, &zero_id, NULL));
git_reference_free(ref);
}
void test_refs_races__create_matching(void)
{
git_reference *ref, *ref2, *ref3;
git_oid id, other_id;
git_oid__fromstr(&id, commit_id, GIT_OID_SHA1);
git_oid__fromstr(&other_id, other_commit_id, GIT_OID_SHA1);
cl_git_fail_with(GIT_EMODIFIED, git_reference_create_matching(&ref, g_repo, refname, &other_id, 1, &other_id, NULL));
cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL));
git_reference_free(ref);
git_reference_free(ref2);
git_reference_free(ref3);
}
void test_refs_races__symbolic_create_matching(void)
{
git_reference *ref, *ref2, *ref3;
git_oid id, other_id;
git_oid__fromstr(&id, commit_id, GIT_OID_SHA1);
git_oid__fromstr(&other_id, other_commit_id, GIT_OID_SHA1);
cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_create_matching(&ref, g_repo, "HEAD", other_refname, 1, other_refname, NULL));
cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, refname));
cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
git_reference_free(ref);
git_reference_free(ref2);
git_reference_free(ref3);
}
void test_refs_races__delete(void)
{
git_reference *ref, *ref2;
git_oid id, other_id;
git_oid__fromstr(&id, commit_id, GIT_OID_SHA1);
git_oid__fromstr(&other_id, other_commit_id, GIT_OID_SHA1);
/* We can delete a value that matches */
cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
/* We cannot delete a symbolic value that doesn't match */
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/symref"));
cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "refs/symref", other_refname, 1, NULL, refname));
cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
git_reference_free(ref);
git_reference_free(ref2);
cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
git_reference_free(ref);
/* We cannot delete an oid value that doesn't match */
cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL));
cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
git_reference_free(ref);
git_reference_free(ref2);
}
void test_refs_races__switch_oid_to_symbolic(void)
{
git_reference *ref, *ref2, *ref3;
git_oid id, other_id;
git_oid__fromstr(&id, commit_id, GIT_OID_SHA1);
git_oid__fromstr(&other_id, other_commit_id, GIT_OID_SHA1);
/* Removing a direct ref when it's currently symbolic should fail */
cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
git_reference_free(ref);
git_reference_free(ref2);
cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL));
git_reference_free(ref);
/* Updating a direct ref when it's currently symbolic should fail */
cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL));
cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL));
git_reference_free(ref);
git_reference_free(ref2);
git_reference_free(ref3);
}
void test_refs_races__switch_symbolic_to_oid(void)
{
git_reference *ref, *ref2, *ref3;
git_oid id, other_id;
git_oid__fromstr(&id, commit_id, GIT_OID_SHA1);
git_oid__fromstr(&other_id, other_commit_id, GIT_OID_SHA1);
/* Removing a symbolic ref when it's currently direct should fail */
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/symref"));
cl_git_pass(git_reference_create(&ref2, g_repo, "refs/symref", &id, 1, NULL));
cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
git_reference_free(ref);
git_reference_free(ref2);
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "refs/symref", refname, 1, NULL));
git_reference_free(ref);
/* Updating a symbolic ref when it's currently direct should fail */
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/symref"));
cl_git_pass(git_reference_create(&ref2, g_repo, "refs/symref", &id, 1, NULL));
cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL));
git_reference_free(ref);
git_reference_free(ref2);
git_reference_free(ref3);
}
| libgit2-main | tests/libgit2/refs/races.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
/* Helpers */
static void ensure_refname_normalized(
unsigned int flags,
const char *input_refname,
const char *expected_refname)
{
char buffer_out[GIT_REFNAME_MAX];
cl_git_pass(git_reference_normalize_name(buffer_out, sizeof(buffer_out), input_refname, flags));
cl_assert_equal_s(expected_refname, buffer_out);
}
static void ensure_refname_invalid(unsigned int flags, const char *input_refname)
{
char buffer_out[GIT_REFNAME_MAX];
cl_assert_equal_i(
GIT_EINVALIDSPEC,
git_reference_normalize_name(buffer_out, sizeof(buffer_out), input_refname, flags));
}
void test_refs_normalize__can_normalize_a_direct_reference_name(void)
{
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/dummy/a", "refs/dummy/a");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/stash", "refs/stash");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/tags/a", "refs/tags/a");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/a/b", "refs/heads/a/b");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/a./b", "refs/heads/a./b");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/v@ation", "refs/heads/v@ation");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs///heads///a", "refs/heads/a");
}
void test_refs_normalize__cannot_normalize_any_direct_reference_name(void)
{
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "a");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "/a");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "//a");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "/refs/heads/a/");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/a/");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/a.");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/a.lock");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/foo?bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads\foo");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/v@ation", "refs/heads/v@ation");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "refs///heads///a", "refs/heads/a");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/.a/b");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/foo/../bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/foo..bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/./foo");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "refs/heads/v@{ation");
}
void test_refs_normalize__symbolic(void)
{
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "heads\foo");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "/");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "///");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "ALL_CAPS_AND_UNDERSCORES", "ALL_CAPS_AND_UNDERSCORES");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/MixedCasing", "refs/MixedCasing");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs///heads///a", "refs/heads/a");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "HEAD", "HEAD");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "MERGE_HEAD", "MERGE_HEAD");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "FETCH_HEAD", "FETCH_HEAD");
}
/* Ported from JGit, BSD licence.
* See https://github.com/spearce/JGit/commit/e4bf8f6957bbb29362575d641d1e77a02d906739
*
* Copyright (C) 2009, Google Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Git Development Community nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
void test_refs_normalize__jgit_suite(void)
{
/* tests borrowed from JGit */
/* EmptyString */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "/");
/* MustHaveTwoComponents */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_NORMAL, "master");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_NORMAL, "heads/master", "heads/master");
/* ValidHead */
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master", "refs/heads/master");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/pu", "refs/heads/pu");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/z", "refs/heads/z");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/FoO", "refs/heads/FoO");
/* ValidTag */
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/tags/v1.0", "refs/tags/v1.0");
/* NoLockSuffix */
ensure_refname_invalid(GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master.lock");
/* NoDirectorySuffix */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master/");
/* NoSpace */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/i haz space");
/* NoAsciiControlCharacters */
{
char c;
char buffer[GIT_REFNAME_MAX];
for (c = '\1'; c < ' '; c++) {
p_snprintf(buffer, sizeof(buffer), "refs/heads/mast%cer", c);
ensure_refname_invalid(GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, buffer);
}
}
/* NoBareDot */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/.");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/..");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/./master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/../master");
/* NoLeadingOrTrailingDot */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, ".");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/.bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/..bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/bar.");
/* ContainsDot */
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/m.a.s.t.e.r", "refs/heads/m.a.s.t.e.r");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master..pu");
/* NoMagicRefCharacters */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master^");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/^master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "^refs/heads/master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master~");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/~master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "~refs/heads/master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master:");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/:master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, ":refs/heads/master");
/* ShellGlob */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master?");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/?master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "?refs/heads/master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master[");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/[master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "[refs/heads/master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master*");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/*master");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "*refs/heads/master");
/* ValidSpecialCharacters */
ensure_refname_normalized
(GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/!", "refs/heads/!");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/\"", "refs/heads/\"");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/#", "refs/heads/#");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/$", "refs/heads/$");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/%", "refs/heads/%");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/&", "refs/heads/&");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/'", "refs/heads/'");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/(", "refs/heads/(");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/)", "refs/heads/)");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/+", "refs/heads/+");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/,", "refs/heads/,");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/-", "refs/heads/-");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/;", "refs/heads/;");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/<", "refs/heads/<");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/=", "refs/heads/=");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/>", "refs/heads/>");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/@", "refs/heads/@");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/]", "refs/heads/]");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/_", "refs/heads/_");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/`", "refs/heads/`");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/{", "refs/heads/{");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/|", "refs/heads/|");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/}", "refs/heads/}");
/*
* This is valid on UNIX, but not on Windows
* hence we make in invalid due to non-portability
*/
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/\\");
/* UnicodeNames */
/*
* Currently this fails.
* ensure_refname_normalized(GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/\u00e5ngstr\u00f6m", "refs/heads/\u00e5ngstr\u00f6m");
*/
/* RefLogQueryIsValidRef */
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master@{1}");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL, "refs/heads/master@{1.hour.ago}");
}
void test_refs_normalize__buffer_has_to_be_big_enough_to_hold_the_normalized_version(void)
{
char buffer_out[21];
cl_git_pass(git_reference_normalize_name(
buffer_out, 21, "refs//heads///long///name", GIT_REFERENCE_FORMAT_NORMAL));
cl_git_fail(git_reference_normalize_name(
buffer_out, 20, "refs//heads///long///name", GIT_REFERENCE_FORMAT_NORMAL));
}
#define ONE_LEVEL_AND_REFSPEC \
GIT_REFERENCE_FORMAT_ALLOW_ONELEVEL \
| GIT_REFERENCE_FORMAT_REFSPEC_PATTERN
void test_refs_normalize__refspec_pattern(void)
{
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "heads/*foo/bar", "heads/*foo/bar");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "heads/foo*/bar", "heads/foo*/bar");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "heads/f*o/bar", "heads/f*o/bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "foo");
ensure_refname_normalized(
ONE_LEVEL_AND_REFSPEC, "FOO", "FOO");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "foo/bar", "foo/bar");
ensure_refname_normalized(
ONE_LEVEL_AND_REFSPEC, "foo/bar", "foo/bar");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "*/foo", "*/foo");
ensure_refname_normalized(
ONE_LEVEL_AND_REFSPEC, "*/foo", "*/foo");
ensure_refname_normalized(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "foo/*/bar", "foo/*/bar");
ensure_refname_normalized(
ONE_LEVEL_AND_REFSPEC, "foo/*/bar", "foo/*/bar");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "*");
ensure_refname_normalized(
ONE_LEVEL_AND_REFSPEC, "*", "*");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "foo/*/*");
ensure_refname_invalid(
ONE_LEVEL_AND_REFSPEC, "foo/*/*");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "*/foo/*");
ensure_refname_invalid(
ONE_LEVEL_AND_REFSPEC, "*/foo/*");
ensure_refname_invalid(
GIT_REFERENCE_FORMAT_REFSPEC_PATTERN, "*/*/foo");
ensure_refname_invalid(
ONE_LEVEL_AND_REFSPEC, "*/*/foo");
}
| libgit2-main | tests/libgit2/refs/normalize.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "git2/reflog.h"
#include "git2/refdb.h"
#include "reflog.h"
#include "ref_helpers.h"
static const char *packed_test_head_name = "refs/heads/packed-test";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static git_repository *g_repo;
void test_refs_delete__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_delete__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_delete__packed_loose(void)
{
/* deleting a ref which is both packed and loose should remove both tracks in the filesystem */
git_reference *looked_up_ref, *another_looked_up_ref;
git_str temp_path = GIT_STR_INIT;
/* Ensure the loose reference exists on the file system */
cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), packed_test_head_name));
cl_assert(git_fs_path_exists(temp_path.ptr));
/* Lookup the reference */
cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, packed_test_head_name));
/* Ensure it's the loose version that has been found */
cl_assert(reference_is_packed(looked_up_ref) == 0);
/* Now that the reference is deleted... */
cl_git_pass(git_reference_delete(looked_up_ref));
git_reference_free(looked_up_ref);
/* Looking up the reference once again should not retrieve it */
cl_git_fail(git_reference_lookup(&another_looked_up_ref, g_repo, packed_test_head_name));
/* Ensure the loose reference doesn't exist any longer on the file system */
cl_assert(!git_fs_path_exists(temp_path.ptr));
git_reference_free(another_looked_up_ref);
git_str_dispose(&temp_path);
}
void test_refs_delete__packed_only(void)
{
/* can delete a just packed reference */
git_reference *ref;
git_refdb *refdb;
git_oid id;
const char *new_ref = "refs/heads/new_ref";
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
/* Create and write the new object id reference */
cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &id, 0, NULL));
git_reference_free(ref);
/* Lookup the reference */
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a loose reference */
cl_assert(reference_is_packed(ref) == 0);
/* Pack all existing references */
cl_git_pass(git_repository_refdb(&refdb, g_repo));
cl_git_pass(git_refdb_compress(refdb));
/* Reload the reference from disk */
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, new_ref));
/* Ensure it's a packed reference */
cl_assert(reference_is_packed(ref) == 1);
/* This should pass */
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
git_refdb_free(refdb);
}
void test_refs_delete__remove(void)
{
git_reference *ref;
/* Check that passing no old values lets us delete */
cl_git_pass(git_reference_lookup(&ref, g_repo, packed_test_head_name));
git_reference_free(ref);
cl_git_pass(git_reference_remove(g_repo, packed_test_head_name));
cl_git_fail(git_reference_lookup(&ref, g_repo, packed_test_head_name));
}
void test_refs_delete__head(void)
{
git_reference *ref;
/* Check that it is not possible to delete HEAD */
cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
cl_git_fail(git_reference_delete(ref));
git_reference_free(ref);
}
| libgit2-main | tests/libgit2/refs/delete.c |
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *repo;
static git_reference *fake_remote;
void test_refs_foreachglob__initialize(void)
{
git_oid id;
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_git_pass(git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
}
void test_refs_foreachglob__cleanup(void)
{
git_reference_free(fake_remote);
fake_remote = NULL;
git_repository_free(repo);
repo = NULL;
cl_fixture_cleanup("testrepo.git");
}
static int count_cb(const char *reference_name, void *payload)
{
int *count = (int *)payload;
GIT_UNUSED(reference_name);
(*count)++;
return 0;
}
static void assert_retrieval(const char *glob, int expected_count)
{
int count = 0;
cl_git_pass(git_reference_foreach_glob(repo, glob, count_cb, &count));
cl_assert_equal_i(expected_count, count);
}
void test_refs_foreachglob__retrieve_all_refs(void)
{
/* 13 heads (including one packed head) + 1 note + 2 remotes + 7 tags + 1 blob */
assert_retrieval("*", 24);
}
void test_refs_foreachglob__retrieve_remote_branches(void)
{
assert_retrieval("refs/remotes/*", 2);
}
void test_refs_foreachglob__retrieve_local_branches(void)
{
assert_retrieval("refs/heads/*", 13);
}
void test_refs_foreachglob__retrieve_nonexistant(void)
{
assert_retrieval("refs/nonexistent/*", 0);
}
void test_refs_foreachglob__retrieve_partially_named_references(void)
{
/*
* refs/heads/packed-test, refs/heads/test
* refs/remotes/test/master, refs/tags/test
*/
assert_retrieval("*test*", 4);
}
static int interrupt_cb(const char *reference_name, void *payload)
{
int *count = (int *)payload;
GIT_UNUSED(reference_name);
(*count)++;
return (*count == 11) ? -1000 : 0;
}
void test_refs_foreachglob__can_cancel(void)
{
int count = 0;
cl_assert_equal_i(-1000, git_reference_foreach_glob(
repo, "*", interrupt_cb, &count) );
cl_assert_equal_i(11, count);
}
| libgit2-main | tests/libgit2/refs/foreachglob.c |
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *g_repo;
void test_refs_dup__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_dup__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_dup__direct(void)
{
git_reference *a, *b;
cl_git_pass(git_reference_lookup(&a, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_dup(&b, a));
cl_assert(git_reference_cmp(a, b) == 0);
cl_assert(git_reference_owner(b) == g_repo);
git_reference_free(b);
git_reference_free(a);
}
void test_refs_dup__symbolic(void)
{
git_reference *a, *b;
cl_git_pass(git_reference_lookup(&a, g_repo, "HEAD"));
cl_git_pass(git_reference_dup(&b, a));
cl_assert(git_reference_cmp(a, b) == 0);
cl_assert(git_reference_owner(b) == g_repo);
git_reference_free(b);
git_reference_free(a);
}
| libgit2-main | tests/libgit2/refs/dup.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *ref_name = "refs/heads/other";
static const char *ref_master_name = "refs/heads/master";
static const char *ref_branch_name = "refs/heads/branch";
static const char *ref_test_name = "refs/heads/test";
static git_repository *g_repo;
void test_refs_overwrite__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_refs_overwrite__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_overwrite__symbolic(void)
{
/* Overwrite an existing symbolic reference */
git_reference *ref, *branch_ref;
/* The target needds to exist and we need to check the name has changed */
cl_git_pass(git_reference_symbolic_create(&branch_ref, g_repo, ref_branch_name, ref_master_name, 0, NULL));
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_branch_name, 0, NULL));
git_reference_free(ref);
/* Ensure it points to the right place*/
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_SYMBOLIC);
cl_assert_equal_s(git_reference_symbolic_target(ref), ref_branch_name);
git_reference_free(ref);
/* Ensure we can't create it unless we force it to */
cl_git_fail(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0, NULL));
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 1, NULL));
git_reference_free(ref);
/* Ensure it points to the right place */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_SYMBOLIC);
cl_assert_equal_s(git_reference_symbolic_target(ref), ref_master_name);
git_reference_free(ref);
git_reference_free(branch_ref);
}
void test_refs_overwrite__object_id(void)
{
/* Overwrite an existing object id reference */
git_reference *ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
/* Create it */
cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 0, NULL));
git_reference_free(ref);
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_test_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
/* Ensure we can't overwrite unless we force it */
cl_git_fail(git_reference_create(&ref, g_repo, ref_name, &id, 0, NULL));
cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 1, NULL));
git_reference_free(ref);
/* Ensure it has been overwritten */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert_equal_oid(&id, git_reference_target(ref));
git_reference_free(ref);
}
void test_refs_overwrite__object_id_with_symbolic(void)
{
/* Overwrite an existing object id reference with a symbolic one */
git_reference *ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 0, NULL));
git_reference_free(ref);
cl_git_fail(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0, NULL));
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 1, NULL));
git_reference_free(ref);
/* Ensure it points to the right place */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_SYMBOLIC);
cl_assert_equal_s(git_reference_symbolic_target(ref), ref_master_name);
git_reference_free(ref);
}
void test_refs_overwrite__symbolic_with_object_id(void)
{
/* Overwrite an existing symbolic reference with an object id one */
git_reference *ref;
git_oid id;
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_master_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
git_oid_cpy(&id, git_reference_target(ref));
git_reference_free(ref);
/* Create the symbolic ref */
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, ref_name, ref_master_name, 0, NULL));
git_reference_free(ref);
/* It shouldn't overwrite unless we tell it to */
cl_git_fail(git_reference_create(&ref, g_repo, ref_name, &id, 0, NULL));
cl_git_pass(git_reference_create(&ref, g_repo, ref_name, &id, 1, NULL));
git_reference_free(ref);
/* Ensure it points to the right place */
cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
cl_assert(git_reference_type(ref) & GIT_REFERENCE_DIRECT);
cl_assert_equal_oid(&id, git_reference_target(ref));
git_reference_free(ref);
}
| libgit2-main | tests/libgit2/refs/overwrite.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "reflog.h"
#include "reflog_helpers.h"
int reflog_entry_tostr(git_str *out, const git_reflog_entry *entry)
{
char old_oid[GIT_OID_SHA1_HEXSIZE], new_oid[GIT_OID_SHA1_HEXSIZE];
assert(out && entry);
git_oid_tostr((char *)&old_oid, GIT_OID_SHA1_HEXSIZE, git_reflog_entry_id_old(entry));
git_oid_tostr((char *)&new_oid, GIT_OID_SHA1_HEXSIZE, git_reflog_entry_id_new(entry));
return git_str_printf(out, "%s %s %s %s", old_oid, new_oid, "somesig", git_reflog_entry_message(entry));
}
size_t reflog_entrycount(git_repository *repo, const char *name)
{
git_reflog *log;
size_t ret;
cl_git_pass(git_reflog_read(&log, repo, name));
ret = git_reflog_entrycount(log);
git_reflog_free(log);
return ret;
}
void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx,
const char *old_spec, const char *new_spec,
const char *email, const char *message,
const char *file, const char *func, int line)
{
git_reflog *log;
const git_reflog_entry *entry;
git_str result = GIT_STR_INIT;
cl_git_pass(git_reflog_read(&log, repo, reflog));
entry = git_reflog_entry_byindex(log, idx);
if (entry == NULL)
clar__fail(file, func, line, "Reflog has no such entry", NULL, 1);
if (old_spec) {
git_object *obj = NULL;
if (git_revparse_single(&obj, repo, old_spec) == GIT_OK) {
if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)) != 0) {
git_object__write_oid_header(&result, "\tOld OID: \"", git_object_id(obj));
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_old(entry));
git_str_puts(&result, "\"\n");
}
git_object_free(obj);
} else {
git_oid *oid = git__calloc(1, sizeof(*oid));
git_oid__fromstr(oid, old_spec, GIT_OID_SHA1);
if (git_oid_cmp(oid, git_reflog_entry_id_old(entry)) != 0) {
git_object__write_oid_header(&result, "\tOld OID: \"", oid);
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_old(entry));
git_str_puts(&result, "\"\n");
}
git__free(oid);
}
}
if (new_spec) {
git_object *obj = NULL;
if (git_revparse_single(&obj, repo, new_spec) == GIT_OK) {
if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)) != 0) {
git_object__write_oid_header(&result, "\tNew OID: \"", git_object_id(obj));
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_new(entry));
git_str_puts(&result, "\"\n");
}
git_object_free(obj);
} else {
git_oid *oid = git__calloc(1, sizeof(*oid));
git_oid__fromstr(oid, new_spec, GIT_OID_SHA1);
if (git_oid_cmp(oid, git_reflog_entry_id_new(entry)) != 0) {
git_object__write_oid_header(&result, "\tNew OID: \"", oid);
git_object__write_oid_header(&result, "\" != \"", git_reflog_entry_id_new(entry));
git_str_puts(&result, "\"\n");
}
git__free(oid);
}
}
if (email && strcmp(email, git_reflog_entry_committer(entry)->email) != 0)
git_str_printf(&result, "\tEmail: \"%s\" != \"%s\"\n", email, git_reflog_entry_committer(entry)->email);
if (message) {
const char *entry_msg = git_reflog_entry_message(entry);
if (entry_msg == NULL) entry_msg = "";
if (entry_msg && strcmp(message, entry_msg) != 0)
git_str_printf(&result, "\tMessage: \"%s\" != \"%s\"\n", message, entry_msg);
}
if (git_str_len(&result) != 0)
clar__fail(file, func, line, "Reflog entry mismatch", git_str_cstr(&result), 1);
git_str_dispose(&result);
git_reflog_free(log);
}
void reflog_print(git_repository *repo, const char *reflog_name)
{
git_reflog *reflog;
size_t idx;
git_str out = GIT_STR_INIT;
git_reflog_read(&reflog, repo, reflog_name);
for (idx = 0; idx < git_reflog_entrycount(reflog); idx++) {
const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, idx);
reflog_entry_tostr(&out, entry);
git_str_putc(&out, '\n');
}
fprintf(stderr, "%s", git_str_cstr(&out));
git_str_dispose(&out);
git_reflog_free(reflog);
}
| libgit2-main | tests/libgit2/refs/reflog/reflog_helpers.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "git2/reflog.h"
#include "reflog.h"
static const char *new_ref = "refs/heads/test-reflog";
static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
#define commit_msg "commit: bla bla"
static git_repository *g_repo;
/* helpers */
static void assert_signature(const git_signature *expected, const git_signature *actual)
{
cl_assert(actual);
cl_assert_equal_s(expected->name, actual->name);
cl_assert_equal_s(expected->email, actual->email);
cl_assert(expected->when.offset == actual->when.offset);
cl_assert(expected->when.time == actual->when.time);
}
/* Fixture setup and teardown */
void test_refs_reflog_reflog__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_reflog_reflog__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void assert_appends(const git_signature *committer, const git_oid *oid)
{
git_repository *repo2;
git_reference *lookedup_ref;
git_reflog *reflog;
const git_reflog_entry *entry;
/* Reopen a new instance of the repository */
cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
/* Lookup the previously created branch */
cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref));
/* Read and parse the reflog for this branch */
cl_git_pass(git_reflog_read(&reflog, repo2, new_ref));
cl_assert_equal_i(3, (int)git_reflog_entrycount(reflog));
/* The first one was the creation of the branch */
entry = git_reflog_entry_byindex(reflog, 2);
cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_SHA1_HEXZERO) == 0);
entry = git_reflog_entry_byindex(reflog, 1);
assert_signature(committer, entry->committer);
cl_assert(git_oid_cmp(oid, &entry->oid_old) == 0);
cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
cl_assert(entry->msg == NULL);
entry = git_reflog_entry_byindex(reflog, 0);
assert_signature(committer, entry->committer);
cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
cl_assert_equal_s(commit_msg, entry->msg);
git_reflog_free(reflog);
git_repository_free(repo2);
git_reference_free(lookedup_ref);
}
void test_refs_reflog_reflog__append_then_read(void)
{
/* write a reflog for a given reference and ensure it can be read back */
git_reference *ref;
git_oid oid;
git_signature *committer;
git_reflog *reflog;
/* Create a new branch pointing at the HEAD */
git_oid__fromstr(&oid, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0, NULL));
git_reference_free(ref);
cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
cl_git_pass(git_reflog_read(&reflog, g_repo, new_ref));
cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL));
cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n"));
cl_git_pass(git_reflog_write(reflog));
assert_appends(committer, &oid);
git_reflog_free(reflog);
git_signature_free(committer);
}
void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
{
git_reference *master, *new_master;
git_str master_log_path = GIT_STR_INIT, moved_log_path = GIT_STR_INIT;
git_str_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
git_str_puts(&moved_log_path, git_str_cstr(&master_log_path));
git_str_joinpath(&master_log_path, git_str_cstr(&master_log_path), "refs/heads/master");
git_str_joinpath(&moved_log_path, git_str_cstr(&moved_log_path), "refs/moved");
cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&master_log_path)));
cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&moved_log_path)));
cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0, NULL));
git_reference_free(master);
cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&master_log_path)));
cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&moved_log_path)));
git_reference_free(new_master);
git_str_dispose(&moved_log_path);
git_str_dispose(&master_log_path);
}
void test_refs_reflog_reflog__deleting_the_reference_deletes_the_reflog(void)
{
git_reference *master;
git_str master_log_path = GIT_STR_INIT;
git_str_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
git_str_joinpath(&master_log_path, git_str_cstr(&master_log_path), "refs/heads/master");
cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&master_log_path)));
cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_delete(master));
git_reference_free(master);
cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&master_log_path)));
git_str_dispose(&master_log_path);
}
void test_refs_reflog_reflog__removes_empty_reflog_dir(void)
{
git_reference *ref;
git_str log_path = GIT_STR_INIT;
git_oid id;
/* Create a new branch pointing at the HEAD */
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir/new-head", &id, 0, NULL));
git_str_joinpath(&log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
git_str_joinpath(&log_path, git_str_cstr(&log_path), "refs/heads/new-dir/new-head");
cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&log_path)));
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
/* new ref creation should succeed since new-dir is empty */
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir", &id, 0, NULL));
git_reference_free(ref);
git_str_dispose(&log_path);
}
void test_refs_reflog_reflog__fails_gracefully_on_nonempty_reflog_dir(void)
{
git_reference *ref;
git_str log_path = GIT_STR_INIT;
git_oid id;
/* Create a new branch pointing at the HEAD */
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/new-dir/new-head", &id, 0, NULL));
git_reference_free(ref);
git_str_joinpath(&log_path, git_repository_path(g_repo), GIT_REFLOG_DIR);
git_str_joinpath(&log_path, git_str_cstr(&log_path), "refs/heads/new-dir/new-head");
cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&log_path)));
/* delete the ref manually, leave the reflog */
cl_must_pass(p_unlink("testrepo.git/refs/heads/new-dir/new-head"));
/* new ref creation should fail since new-dir contains reflogs still */
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_fail_with(GIT_EDIRECTORY, git_reference_create(&ref, g_repo, "refs/heads/new-dir", &id, 0, NULL));
git_reference_free(ref);
git_str_dispose(&log_path);
}
static void assert_has_reflog(bool expected_result, const char *name)
{
cl_assert_equal_i(expected_result, git_reference_has_log(g_repo, name));
}
void test_refs_reflog_reflog__reference_has_reflog(void)
{
assert_has_reflog(true, "HEAD");
assert_has_reflog(true, "refs/heads/master");
assert_has_reflog(false, "refs/heads/subtrees");
}
void test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_returns_an_empty_one(void)
{
git_reflog *reflog;
const char *refname = "refs/heads/subtrees";
git_str subtrees_log_path = GIT_STR_INIT;
git_str_join_n(&subtrees_log_path, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, refname);
cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&subtrees_log_path)));
cl_git_pass(git_reflog_read(&reflog, g_repo, refname));
cl_assert_equal_i(0, (int)git_reflog_entrycount(reflog));
git_reflog_free(reflog);
git_str_dispose(&subtrees_log_path);
}
void test_refs_reflog_reflog__reading_a_reflog_with_invalid_format_succeeds(void)
{
git_reflog *reflog;
const char *refname = "refs/heads/newline";
const char *refmessage =
"Reflog*message with a newline and enough content after it to pass the GIT_REFLOG_SIZE_MIN check inside reflog_parse.";
const git_reflog_entry *entry;
git_reference *ref;
git_oid id;
git_str logpath = GIT_STR_INIT, logcontents = GIT_STR_INIT;
char *star;
/* Create a new branch. */
cl_git_pass(git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1));
cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, refmessage));
/*
* Corrupt the branch reflog by introducing a newline inside the reflog message.
* We do this by replacing '*' with '\n'
*/
cl_git_pass(git_str_join_n(&logpath, '/', 3, git_repository_path(g_repo), GIT_REFLOG_DIR, refname));
cl_git_pass(git_futils_readbuffer(&logcontents, git_str_cstr(&logpath)));
cl_assert((star = strchr(git_str_cstr(&logcontents), '*')) != NULL);
*star = '\n';
cl_git_rewritefile(git_str_cstr(&logpath), git_str_cstr(&logcontents));
/*
* Confirm that the file was rewritten successfully
* and now contains a '\n' in the expected location
*/
cl_git_pass(git_futils_readbuffer(&logcontents, git_str_cstr(&logpath)));
cl_assert(strstr(git_str_cstr(&logcontents), "Reflog\nmessage") != NULL);
cl_git_pass(git_reflog_read(&reflog, g_repo, refname));
cl_assert(entry = git_reflog_entry_byindex(reflog, 0));
cl_assert_equal_s(git_reflog_entry_message(entry), "Reflog");
git_reference_free(ref);
git_reflog_free(reflog);
git_str_dispose(&logpath);
git_str_dispose(&logcontents);
}
void test_refs_reflog_reflog__cannot_write_a_moved_reflog(void)
{
git_reference *master, *new_master;
git_str master_log_path = GIT_STR_INIT, moved_log_path = GIT_STR_INIT;
git_reflog *reflog;
cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master"));
cl_git_pass(git_reflog_read(&reflog, g_repo, "refs/heads/master"));
cl_git_pass(git_reflog_write(reflog));
cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0, NULL));
git_reference_free(master);
cl_git_fail(git_reflog_write(reflog));
git_reflog_free(reflog);
git_reference_free(new_master);
git_str_dispose(&moved_log_path);
git_str_dispose(&master_log_path);
}
void test_refs_reflog_reflog__renaming_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_reflog_rename(g_repo, "refs/heads/master", "refs/heads/Inv@{id"));
}
void test_refs_reflog_reflog__write_only_std_locations(void)
{
git_reference *ref;
git_oid id;
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/foo", &id, 1, NULL));
git_reference_free(ref);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/tags/foo", &id, 1, NULL));
git_reference_free(ref);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/notes/foo", &id, 1, NULL));
git_reference_free(ref);
assert_has_reflog(true, "refs/heads/foo");
assert_has_reflog(false, "refs/tags/foo");
assert_has_reflog(true, "refs/notes/foo");
}
void test_refs_reflog_reflog__write_when_explicitly_active(void)
{
git_reference *ref;
git_oid id;
git_oid__fromstr(&id, current_master_tip, GIT_OID_SHA1);
git_reference_ensure_log(g_repo, "refs/tags/foo");
cl_git_pass(git_reference_create(&ref, g_repo, "refs/tags/foo", &id, 1, NULL));
git_reference_free(ref);
assert_has_reflog(true, "refs/tags/foo");
}
void test_refs_reflog_reflog__append_to_HEAD_when_changing_current_branch(void)
{
size_t nlogs, nlogs_after;
git_reference *ref;
git_reflog *log;
git_oid id;
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
nlogs = git_reflog_entrycount(log);
git_reflog_free(log);
/* Move it back */
git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/master", &id, 1, NULL));
git_reference_free(ref);
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
nlogs_after = git_reflog_entrycount(log);
git_reflog_free(log);
cl_assert_equal_i(nlogs_after, nlogs + 1);
}
void test_refs_reflog_reflog__do_not_append_when_no_update(void)
{
size_t nlogs, nlogs_after;
git_reference *ref, *ref2;
git_reflog *log;
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
nlogs = git_reflog_entrycount(log);
git_reflog_free(log);
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_create(&ref2, g_repo, "refs/heads/master",
git_reference_target(ref), 1, NULL));
git_reference_free(ref);
git_reference_free(ref2);
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
nlogs_after = git_reflog_entrycount(log);
git_reflog_free(log);
cl_assert_equal_i(nlogs_after, nlogs);
}
static void assert_no_reflog_update(void)
{
size_t nlogs, nlogs_after;
size_t nlogs_master, nlogs_master_after;
git_reference *ref;
git_reflog *log;
git_oid id;
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
nlogs = git_reflog_entrycount(log);
git_reflog_free(log);
cl_git_pass(git_reflog_read(&log, g_repo, "refs/heads/master"));
nlogs_master = git_reflog_entrycount(log);
git_reflog_free(log);
/* Move it back */
git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/master", &id, 1, NULL));
git_reference_free(ref);
cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
nlogs_after = git_reflog_entrycount(log);
git_reflog_free(log);
cl_assert_equal_i(nlogs_after, nlogs);
cl_git_pass(git_reflog_read(&log, g_repo, "refs/heads/master"));
nlogs_master_after = git_reflog_entrycount(log);
git_reflog_free(log);
cl_assert_equal_i(nlogs_after, nlogs);
cl_assert_equal_i(nlogs_master_after, nlogs_master);
}
void test_refs_reflog_reflog__logallrefupdates_bare_set_false(void)
{
git_config *config;
cl_git_pass(git_repository_config(&config, g_repo));
cl_git_pass(git_config_set_bool(config, "core.logallrefupdates", false));
git_config_free(config);
assert_no_reflog_update();
}
void test_refs_reflog_reflog__logallrefupdates_bare_set_always(void)
{
git_config *config;
git_reference *ref;
git_reflog *log;
git_oid id;
cl_git_pass(git_repository_config(&config, g_repo));
cl_git_pass(git_config_set_string(config, "core.logallrefupdates", "always"));
git_config_free(config);
git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/bork", &id, 1, "message"));
cl_git_pass(git_reflog_read(&log, g_repo, "refs/bork"));
cl_assert_equal_i(1, git_reflog_entrycount(log));
cl_assert_equal_s("message", git_reflog_entry_byindex(log, 0)->msg);
git_reflog_free(log);
git_reference_free(ref);
}
void test_refs_reflog_reflog__logallrefupdates_bare_unset(void)
{
git_config *config;
cl_git_pass(git_repository_config(&config, g_repo));
cl_git_pass(git_config_delete_entry(config, "core.logallrefupdates"));
git_config_free(config);
assert_no_reflog_update();
}
void test_refs_reflog_reflog__logallrefupdates_nonbare_set_false(void)
{
git_config *config;
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_config(&config, g_repo));
cl_git_pass(git_config_set_bool(config, "core.logallrefupdates", false));
git_config_free(config);
assert_no_reflog_update();
}
| libgit2-main | tests/libgit2/refs/reflog/reflog.c |
#include "clar_libgit2.h"
#include "reflog.h"
static git_repository *g_repo;
static git_reflog *g_reflog;
static size_t entrycount;
void test_refs_reflog_drop__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
git_reflog_read(&g_reflog, g_repo, "HEAD");
entrycount = git_reflog_entrycount(g_reflog);
}
void test_refs_reflog_drop__cleanup(void)
{
git_reflog_free(g_reflog);
g_reflog = NULL;
cl_git_sandbox_cleanup();
}
void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void)
{
cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_drop(g_reflog, entrycount, 0));
cl_assert_equal_sz(entrycount, git_reflog_entrycount(g_reflog));
}
void test_refs_reflog_drop__can_drop_an_entry(void)
{
cl_assert(entrycount > 4);
cl_git_pass(git_reflog_drop(g_reflog, 2, 0));
cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
}
void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void)
{
const git_reflog_entry *before_current;
const git_reflog_entry *after_current;
git_oid before_current_old_oid, before_current_cur_oid;
cl_assert(entrycount > 4);
before_current = git_reflog_entry_byindex(g_reflog, 1);
git_oid_cpy(&before_current_old_oid, &before_current->oid_old);
git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur);
cl_git_pass(git_reflog_drop(g_reflog, 1, 1));
cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
after_current = git_reflog_entry_byindex(g_reflog, 0);
cl_assert_equal_i(0, git_oid_cmp(&before_current_old_oid, &after_current->oid_old));
cl_assert(0 != git_oid_cmp(&before_current_cur_oid, &after_current->oid_cur));
}
void test_refs_reflog_drop__can_drop_the_oldest_entry(void)
{
const git_reflog_entry *entry;
cl_assert(entrycount > 2);
cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_SHA1_HEXZERO) != 0);
}
void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void)
{
const git_reflog_entry *entry;
cl_assert(entrycount > 2);
cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_SHA1_HEXZERO) == 0);
}
void test_refs_reflog_drop__can_drop_all_the_entries(void)
{
cl_assert(--entrycount > 0);
do {
cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
} while (--entrycount > 0);
cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
cl_assert_equal_i(0, (int)git_reflog_entrycount(g_reflog));
}
void test_refs_reflog_drop__can_persist_deletion_on_disk(void)
{
cl_assert(entrycount > 2);
cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
cl_git_pass(git_reflog_write(g_reflog));
git_reflog_free(g_reflog);
git_reflog_read(&g_reflog, g_repo, "HEAD");
cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog));
}
| libgit2-main | tests/libgit2/refs/reflog/drop.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "refs.h"
#include "reflog_helpers.h"
static const char *g_email = "[email protected]";
static git_repository *g_repo;
/* Fixture setup and teardown */
void test_refs_reflog_messages__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_set_ident(g_repo, "Foo Bar", g_email));
}
void test_refs_reflog_messages__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_reflog_messages__setting_head_updates_reflog(void)
{
git_object *tag;
git_annotated_commit *annotated;
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked")); /* 4 */
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/unborn"));
cl_git_pass(git_revparse_single(&tag, g_repo, "tags/test"));
cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(tag))); /* 3 */
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked")); /* 2 */
cl_git_pass(git_repository_set_head(g_repo, "refs/tags/test")); /* 1 */
cl_git_pass(git_repository_set_head(g_repo, "refs/remotes/test/master")); /* 0 */
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 4,
NULL, "refs/heads/haacked",
"[email protected]",
"checkout: moving from master to haacked");
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 3,
NULL, "tags/test^{commit}",
"[email protected]",
"checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 2,
"tags/test^{commit}", "refs/heads/haacked",
"[email protected]",
"checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 1,
"refs/heads/haacked", "tags/test^{commit}",
"[email protected]",
"checkout: moving from haacked to test");
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"tags/test^{commit}", "refs/remotes/test/master",
"[email protected]",
"checkout: moving from e90810b8df3e80c413d903f631643c716887138d to test/master");
cl_git_pass(git_annotated_commit_from_revspec(&annotated, g_repo, "haacked~0"));
cl_git_pass(git_repository_set_head_detached_from_annotated(g_repo, annotated));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
NULL, "refs/heads/haacked",
"[email protected]",
"checkout: moving from be3563ae3f795b2b4353bcce3a527ad0a4f7f644 to haacked~0");
git_annotated_commit_free(annotated);
git_object_free(tag);
}
void test_refs_reflog_messages__setting_head_to_same_target_ignores_reflog(void)
{
size_t nentries, nentries_after;
nentries = reflog_entrycount(g_repo, GIT_HEAD_FILE);
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked"));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked"));
nentries_after = reflog_entrycount(g_repo, GIT_HEAD_FILE);
cl_assert_equal_i(nentries + 1, nentries_after);
}
void test_refs_reflog_messages__detaching_writes_reflog(void)
{
git_oid id;
const char *msg;
msg = "checkout: moving from master to e90810b8df3e80c413d903f631643c716887138d";
git_oid__fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1);
cl_git_pass(git_repository_set_head_detached(g_repo, &id));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"e90810b8df3e80c413d903f631643c716887138d",
NULL, msg);
msg = "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked";
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked"));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"e90810b8df3e80c413d903f631643c716887138d",
"258f0e2a959a364e40ed6603d5d44fbb24765b10",
NULL, msg);
}
void test_refs_reflog_messages__orphan_branch_does_not_count(void)
{
git_oid id;
const char *msg;
/* Have something known */
msg = "checkout: moving from master to e90810b8df3e80c413d903f631643c716887138d";
git_oid__fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1);
cl_git_pass(git_repository_set_head_detached(g_repo, &id));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"e90810b8df3e80c413d903f631643c716887138d",
NULL, msg);
/* Switching to an orphan branch does not write to the reflog */
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/orphan"));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"e90810b8df3e80c413d903f631643c716887138d",
NULL, msg);
/* And coming back, we set the source to zero */
msg = "checkout: moving from orphan to haacked";
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked"));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"0000000000000000000000000000000000000000",
"258f0e2a959a364e40ed6603d5d44fbb24765b10",
NULL, msg);
}
void test_refs_reflog_messages__branch_birth(void)
{
git_signature *sig;
git_oid id;
git_tree *tree;
git_reference *ref;
const char *msg;
size_t nentries, nentries_after;
nentries = reflog_entrycount(g_repo, GIT_HEAD_FILE);
cl_git_pass(git_signature_now(&sig, "me", "[email protected]"));
cl_git_pass(git_repository_head(&ref, g_repo));
cl_git_pass(git_reference_peel((git_object **) &tree, ref, GIT_OBJECT_TREE));
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/orphan"));
nentries_after = reflog_entrycount(g_repo, GIT_HEAD_FILE);
cl_assert_equal_i(nentries, nentries_after);
msg = "message 2";
cl_git_pass(git_commit_create(&id, g_repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
cl_assert_equal_i(1, reflog_entrycount(g_repo, "refs/heads/orphan"));
nentries_after = reflog_entrycount(g_repo, GIT_HEAD_FILE);
cl_assert_equal_i(nentries + 1, nentries_after);
git_signature_free(sig);
git_tree_free(tree);
git_reference_free(ref);
}
void test_refs_reflog_messages__commit_on_symbolic_ref_updates_head_reflog(void)
{
git_signature *sig;
git_oid id;
git_tree *tree;
git_reference *ref1, *ref2;
const char *msg;
size_t nentries_head, nentries_master;
nentries_head = reflog_entrycount(g_repo, GIT_HEAD_FILE);
cl_git_pass(git_signature_now(&sig, "me", "[email protected]"));
cl_git_pass(git_repository_head(&ref1, g_repo));
cl_git_pass(git_reference_peel((git_object **) &tree, ref1, GIT_OBJECT_TREE));
nentries_master = reflog_entrycount(g_repo, "refs/heads/master");
msg = "message 1";
cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, "refs/heads/master", "refs/heads/foo", 1, msg));
cl_assert_equal_i(0, reflog_entrycount(g_repo, "refs/heads/foo"));
cl_assert_equal_i(nentries_head, reflog_entrycount(g_repo, GIT_HEAD_FILE));
cl_assert_equal_i(nentries_master, reflog_entrycount(g_repo, "refs/heads/master"));
msg = "message 2";
cl_git_pass(git_commit_create(&id, g_repo, "HEAD", sig, sig, NULL, msg, tree, 0, NULL));
cl_assert_equal_i(1, reflog_entrycount(g_repo, "refs/heads/foo"));
cl_assert_equal_i(nentries_head + 1, reflog_entrycount(g_repo, GIT_HEAD_FILE));
cl_assert_equal_i(nentries_master, reflog_entrycount(g_repo, "refs/heads/master"));
git_signature_free(sig);
git_reference_free(ref1);
git_reference_free(ref2);
git_tree_free(tree);
}
void test_refs_reflog_messages__show_merge_for_merge_commits(void)
{
git_oid b1_oid;
git_oid b2_oid;
git_oid merge_commit_oid;
git_commit *b1_commit;
git_commit *b2_commit;
git_signature *s;
git_commit *parent_commits[2];
git_tree *tree;
cl_git_pass(git_signature_now(&s, "alice", "[email protected]"));
cl_git_pass(git_reference_name_to_id(&b1_oid, g_repo, "HEAD"));
cl_git_pass(git_reference_name_to_id(&b2_oid, g_repo, "refs/heads/test"));
cl_git_pass(git_commit_lookup(&b1_commit, g_repo, &b1_oid));
cl_git_pass(git_commit_lookup(&b2_commit, g_repo, &b2_oid));
parent_commits[0] = b1_commit;
parent_commits[1] = b2_commit;
cl_git_pass(git_commit_tree(&tree, b1_commit));
cl_git_pass(git_commit_create(&merge_commit_oid,
g_repo, "HEAD", s, s, NULL,
"Merge commit", tree,
2, (const struct git_commit **) parent_commits));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
NULL,
git_oid_tostr_s(&merge_commit_oid),
NULL, "commit (merge): Merge commit");
git_tree_free(tree);
git_commit_free(b1_commit);
git_commit_free(b2_commit);
git_signature_free(s);
}
void test_refs_reflog_messages__creating_a_direct_reference(void)
{
git_reference *reference;
git_oid id;
git_reflog *reflog;
const git_reflog_entry *entry;
const char *name = "refs/heads/new-head";
const char *message = "You've been logged, mate!";
cl_git_pass(git_reference_name_to_id(&id, g_repo, "HEAD"));
cl_git_pass(git_reference_create(&reference, g_repo, name, &id, 0, message));
cl_git_pass(git_reflog_read(&reflog, g_repo, name));
cl_assert_equal_sz(1, git_reflog_entrycount(reflog));
entry = git_reflog_entry_byindex(reflog, 0);
cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_SHA1_HEXZERO) == 0);
cl_assert_equal_oid(&id, &entry->oid_cur);
cl_assert_equal_s(message, entry->msg);
git_reflog_free(reflog);
git_reference_free(reference);
}
void test_refs_reflog_messages__newline_gets_replaced(void)
{
const git_reflog_entry *entry;
git_signature *signature;
git_reflog *reflog;
git_oid oid;
cl_git_pass(git_signature_now(&signature, "me", "[email protected]"));
cl_git_pass(git_oid__fromstr(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_pass(git_reflog_read(&reflog, g_repo, "HEAD"));
cl_assert_equal_sz(7, git_reflog_entrycount(reflog));
cl_git_pass(git_reflog_append(reflog, &oid, signature, "inner\nnewline"));
cl_assert_equal_sz(8, git_reflog_entrycount(reflog));
cl_assert(entry = git_reflog_entry_byindex(reflog, 0));
cl_assert_equal_s(git_reflog_entry_message(entry), "inner newline");
git_signature_free(signature);
git_reflog_free(reflog);
}
void test_refs_reflog_messages__renaming_ref(void)
{
git_reference *ref, *new_ref;
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_rename(&new_ref, ref, "refs/heads/renamed", false,
"message"));
cl_reflog_check_entry(g_repo, git_reference_name(new_ref), 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"[email protected]", "message");
git_reference_free(ref);
git_reference_free(new_ref);
}
void test_refs_reflog_messages__updating_a_direct_reference(void)
{
git_reference *ref, *ref_out, *target_ref;
git_oid target_id;
const char *message = "You've been logged, mate!";
git_reference_name_to_id(&target_id, g_repo, "refs/heads/haacked");
cl_git_pass(git_reference_lookup(&target_ref, g_repo, "refs/heads/haacked"));
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
cl_git_pass(git_reference_set_target(&ref_out, ref, &target_id, message));
cl_reflog_check_entry(g_repo, "refs/heads/master", 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"258f0e2a959a364e40ed6603d5d44fbb24765b10",
NULL, message);
git_reference_free(target_ref);
git_reference_free(ref);
git_reference_free(ref_out);
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
void test_refs_reflog_messages__creating_branches_default_messages(void)
{
git_str buf = GIT_STR_INIT;
git_annotated_commit *annotated;
git_object *obj;
git_commit *target;
git_reference *branch1, *branch2;
cl_git_pass(git_revparse_single(&obj, g_repo, "e90810b8df3"));
cl_git_pass(git_commit_lookup(&target, g_repo, git_object_id(obj)));
git_object_free(obj);
cl_git_pass(git_branch_create(&branch1, g_repo, NEW_BRANCH_NAME, target, false));
cl_git_pass(git_str_printf(&buf, "branch: Created from %s", git_oid_tostr_s(git_commit_id(target))));
cl_reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0,
GIT_OID_SHA1_HEXZERO,
git_oid_tostr_s(git_commit_id(target)),
g_email, git_str_cstr(&buf));
cl_git_pass(git_reference_remove(g_repo, "refs/heads/" NEW_BRANCH_NAME));
cl_git_pass(git_annotated_commit_from_revspec(&annotated, g_repo, "e90810b8df3"));
cl_git_pass(git_branch_create_from_annotated(&branch2, g_repo, NEW_BRANCH_NAME, annotated, true));
cl_reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0,
GIT_OID_SHA1_HEXZERO,
git_oid_tostr_s(git_commit_id(target)),
g_email, "branch: Created from e90810b8df3");
git_annotated_commit_free(annotated);
git_str_dispose(&buf);
git_commit_free(target);
git_reference_free(branch1);
git_reference_free(branch2);
}
void test_refs_reflog_messages__moving_branch_default_message(void)
{
git_reference *branch;
git_reference *new_branch;
git_oid id;
cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/heads/master"));
git_oid_cpy(&id, git_reference_target(branch));
cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
cl_reflog_check_entry(g_repo, git_reference_name(new_branch), 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
g_email,
"branch: renamed refs/heads/master to refs/heads/master2");
git_reference_free(branch);
git_reference_free(new_branch);
}
void test_refs_reflog_messages__detaching_head_default_message(void)
{
git_reference *ref;
cl_assert_equal_i(false, git_repository_head_detached(g_repo));
cl_git_pass(git_repository_detach_head(g_repo));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
NULL, "checkout: moving from master to a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
cl_assert_equal_i(true, git_repository_head_detached(g_repo));
/* take the repo back to its original state */
cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "HEAD", "refs/heads/master",
true, "REATTACH"));
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
NULL, "REATTACH");
cl_assert_equal_i(false, git_repository_head_detached(g_repo));
git_reference_free(ref);
}
| libgit2-main | tests/libgit2/refs/reflog/messages.c |
#include "clar_libgit2.h"
static int name_is_valid(const char *name)
{
int valid;
cl_git_pass(git_tag_name_is_valid(&valid, name));
return valid;
}
void test_refs_tags_name__is_name_valid(void)
{
cl_assert_equal_i(true, name_is_valid("sometag"));
cl_assert_equal_i(true, name_is_valid("test/sometag"));
cl_assert_equal_i(false, name_is_valid(""));
cl_assert_equal_i(false, name_is_valid("-dash"));
}
| libgit2-main | tests/libgit2/refs/tags/name.c |
#include "clar_libgit2.h"
#include "branch.h"
#include "remote.h"
static git_repository *g_repo;
static const char *remote_tracking_branch_name = "refs/remotes/test/master";
static const char *expected_remote_name = "test";
static int expected_remote_name_length;
void test_refs_branches_remote__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
expected_remote_name_length = (int)strlen(expected_remote_name) + 1;
}
void test_refs_branches_remote__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_branches_remote__can_get_remote_for_branch(void)
{
git_buf remotename = {0};
cl_git_pass(git_branch_remote_name(&remotename, g_repo, remote_tracking_branch_name));
cl_assert_equal_s("test", remotename.ptr);
git_buf_dispose(&remotename);
}
void test_refs_branches_remote__no_matching_remote_returns_error(void)
{
const char *unknown = "refs/remotes/nonexistent/master";
git_buf buf = GIT_BUF_INIT;
git_error_clear();
cl_git_fail_with(git_branch_remote_name(&buf, g_repo, unknown), GIT_ENOTFOUND);
cl_assert(git_error_last() != NULL);
}
void test_refs_branches_remote__local_remote_returns_error(void)
{
const char *local = "refs/heads/master";
git_buf buf = GIT_BUF_INIT;
git_error_clear();
cl_git_fail_with(git_branch_remote_name(&buf, g_repo, local), GIT_ERROR);
cl_assert(git_error_last() != NULL);
}
void test_refs_branches_remote__ambiguous_remote_returns_error(void)
{
git_remote *remote;
git_buf buf = GIT_BUF_INIT;
/* Create the remote */
cl_git_pass(git_remote_create_with_fetchspec(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2", "refs/heads/*:refs/remotes/test/*"));
git_remote_free(remote);
git_error_clear();
cl_git_fail_with(git_branch_remote_name(&buf, g_repo, remote_tracking_branch_name), GIT_EAMBIGUOUS);
cl_assert(git_error_last() != NULL);
}
| libgit2-main | tests/libgit2/refs/branches/remote.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
static git_repository *repo;
static git_reference *branch;
void test_refs_branches_ishead__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
branch = NULL;
}
void test_refs_branches_ishead__cleanup(void)
{
git_reference_free(branch);
branch = NULL;
cl_git_sandbox_cleanup();
repo = NULL;
}
void test_refs_branches_ishead__can_tell_if_a_branch_is_pointed_at_by_HEAD(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(true, git_branch_is_head(branch));
}
void test_refs_branches_ishead__can_properly_handle_unborn_HEAD(void)
{
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
void test_refs_branches_ishead__can_properly_handle_missing_HEAD(void)
{
delete_head(repo);
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
void test_refs_branches_ishead__can_tell_if_a_branch_is_not_pointed_at_by_HEAD(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/br2"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
void test_refs_branches_ishead__wont_be_fooled_by_a_non_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
/*
* $ git init .
* Initialized empty Git repository in d:/temp/tempee/.git/
*
* $ touch a && git add a
* $ git commit -m" boom"
* [master (root-commit) b47b758] boom
* 0 files changed
* create mode 100644 a
*
* $ echo "ref: refs/heads/master" > .git/refs/heads/linked
* $ echo "ref: refs/heads/linked" > .git/refs/heads/super
* $ echo "ref: refs/heads/super" > .git/HEAD
*
* $ git branch
* linked -> master
* * master
* super -> master
*/
void test_refs_branches_ishead__only_direct_references_are_considered(void)
{
git_reference *linked, *super, *head;
cl_git_pass(git_reference_symbolic_create(&linked, repo, "refs/heads/linked", "refs/heads/master", 0, NULL));
cl_git_pass(git_reference_symbolic_create(&super, repo, "refs/heads/super", "refs/heads/linked", 0, NULL));
cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/heads/super", 1, NULL));
cl_assert_equal_i(false, git_branch_is_head(linked));
cl_assert_equal_i(false, git_branch_is_head(super));
cl_git_pass(git_repository_head(&branch, repo));
cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
git_reference_free(linked);
git_reference_free(super);
git_reference_free(head);
}
| libgit2-main | tests/libgit2/refs/branches/ishead.c |
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *repo;
static git_reference *branch;
void test_refs_branches_lookup__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
branch = NULL;
}
void test_refs_branches_lookup__cleanup(void)
{
git_reference_free(branch);
branch = NULL;
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_lookup__can_retrieve_a_local_branch_local(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
}
void test_refs_branches_lookup__can_retrieve_a_local_branch_all(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_local_branch_remote(void)
{
cl_git_fail(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_REMOTE));
}
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_remote(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_REMOTE));
}
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_all(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_remote_tracking_branch_local(void)
{
cl_git_fail(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_LOCAL));
}
void test_refs_branches_lookup__trying_to_retrieve_an_unknown_branch_returns_ENOTFOUND(void)
{
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "where/are/you", GIT_BRANCH_LOCAL));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "over/here", GIT_BRANCH_REMOTE));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "maybe/here", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "are/you/inv@{id", GIT_BRANCH_LOCAL));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "yes/i am", GIT_BRANCH_REMOTE));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "inv al/id", GIT_BRANCH_ALL));
}
| libgit2-main | tests/libgit2/refs/branches/lookup.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "config/config_helpers.h"
static git_repository *repo;
void test_refs_branches_move__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_branches_move__cleanup(void)
{
cl_git_sandbox_cleanup();
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
void test_refs_branches_move__can_move_a_local_branch(void)
{
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, NEW_BRANCH_NAME, 0));
cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(new_ref));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
{
git_reference *original_ref, *new_ref, *newer_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
/* Downward */
cl_git_pass(git_branch_move(&new_ref, original_ref, "somewhere/" NEW_BRANCH_NAME, 0));
git_reference_free(original_ref);
/* Upward */
cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
git_reference_free(new_ref);
git_reference_free(newer_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
{
git_reference *original_ref, *new_ref, *newer_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
/* Downward */
cl_git_pass(git_branch_move(&new_ref, original_ref, "br2/" NEW_BRANCH_NAME, 0));
git_reference_free(original_ref);
/* Upward */
cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
git_reference_free(new_ref);
git_reference_free(newer_ref);
}
void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
{
git_reference *original_ref, *new_ref;
git_config *config;
git_buf original_remote = GIT_BUF_INIT,
original_merge = GIT_BUF_INIT;
const char *str;
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string_buf(&original_remote, config, "branch.master.remote"));
cl_git_pass(git_config_get_string_buf(&original_merge, config, "branch.master.merge"));
git_config_free(config);
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "master", 0));
cl_assert(git_error_last()->message != 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(original_remote.ptr, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge.ptr, str);
git_config_free(config);
cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
cl_assert(git_error_last()->message != 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(original_remote.ptr, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge.ptr, str);
git_config_free(config);
git_reference_free(original_ref);
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));
cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "master", 0));
cl_assert(git_error_last()->message != 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(original_remote.ptr, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge.ptr, str);
git_buf_dispose(&original_remote);
git_buf_dispose(&original_merge);
git_reference_free(original_ref);
git_config_free(config);
}
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
git_reference_free(original_ref);
}
void test_refs_branches_move__can_not_move_a_non_branch(void)
{
git_reference *tag, *new_ref;
cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
git_reference_free(tag);
}
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
{
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
git_reference *branch;
git_reference *new_branch;
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
assert_config_entry_existence(repo, "branch.track-local.remote", true);
assert_config_entry_existence(repo, "branch.track-local.merge", true);
assert_config_entry_existence(repo, "branch.moved.remote", false);
assert_config_entry_existence(repo, "branch.moved.merge", false);
cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
assert_config_entry_existence(repo, "branch.moved.remote", true);
assert_config_entry_existence(repo, "branch.moved.merge", true);
git_reference_free(new_branch);
}
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
git_reference *branch;
git_reference *new_branch;
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
git_reference_free(branch);
git_reference_free(new_branch);
cl_git_pass(git_repository_head(&branch, repo));
cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
git_reference_free(branch);
}
void test_refs_branches_move__can_move_with_unicode(void)
{
git_reference *original_ref, *new_ref;
const char *new_branch_name = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, new_branch_name, 0));
if (cl_repo_get_bool(repo, "core.precomposeunicode"))
cl_assert_equal_s(GIT_REFS_HEADS_DIR "\xC3\x85\x73\x74\x72\xC3\xB6\x6D", git_reference_name(new_ref));
else
cl_assert_equal_s(GIT_REFS_HEADS_DIR "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D", git_reference_name(new_ref));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
| libgit2-main | tests/libgit2/refs/branches/move.c |
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *repo;
static git_reference *fake_remote;
void test_refs_branches_iterator__initialize(void)
{
git_oid id;
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_git_pass(git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
}
void test_refs_branches_iterator__cleanup(void)
{
git_reference_free(fake_remote);
fake_remote = NULL;
git_repository_free(repo);
repo = NULL;
cl_fixture_cleanup("testrepo.git");
cl_git_sandbox_cleanup();
}
static void assert_retrieval(unsigned int flags, unsigned int expected_count)
{
git_branch_iterator *iter;
git_reference *ref;
int count = 0, error;
git_branch_t type;
cl_git_pass(git_branch_iterator_new(&iter, repo, flags));
while ((error = git_branch_next(&ref, &type, iter)) == 0) {
count++;
git_reference_free(ref);
}
git_branch_iterator_free(iter);
cl_assert_equal_i(error, GIT_ITEROVER);
cl_assert_equal_i(expected_count, count);
}
void test_refs_branches_iterator__retrieve_all_branches(void)
{
assert_retrieval(GIT_BRANCH_ALL, 15);
}
void test_refs_branches_iterator__retrieve_remote_branches(void)
{
assert_retrieval(GIT_BRANCH_REMOTE, 2);
}
void test_refs_branches_iterator__retrieve_local_branches(void)
{
assert_retrieval(GIT_BRANCH_LOCAL, 13);
}
struct expectations {
const char *branch_name;
int encounters;
};
static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name)
{
int pos = 0;
for (pos = 0; findings[pos].branch_name; ++pos) {
if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) {
cl_assert_equal_i(1, findings[pos].encounters);
return;
}
}
cl_fail("expected branch not found in list.");
}
static void contains_branches(struct expectations exp[], git_branch_iterator *iter)
{
git_reference *ref;
git_branch_t type;
int error, pos = 0;
while ((error = git_branch_next(&ref, &type, iter)) == 0) {
for (pos = 0; exp[pos].branch_name; ++pos) {
if (strcmp(git_reference_shorthand(ref), exp[pos].branch_name) == 0)
exp[pos].encounters++;
}
git_reference_free(ref);
}
cl_assert_equal_i(error, GIT_ITEROVER);
}
/*
* $ git branch -r
* nulltoken/HEAD -> nulltoken/master
* nulltoken/master
*/
void test_refs_branches_iterator__retrieve_remote_symbolic_HEAD_when_present(void)
{
git_branch_iterator *iter;
struct expectations exp[] = {
{ "nulltoken/HEAD", 0 },
{ "nulltoken/master", 0 },
{ NULL, 0 }
};
git_reference_free(fake_remote);
cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0, NULL));
assert_retrieval(GIT_BRANCH_REMOTE, 3);
cl_git_pass(git_branch_iterator_new(&iter, repo, GIT_BRANCH_REMOTE));
contains_branches(exp, iter);
git_branch_iterator_free(iter);
assert_branch_has_been_found(exp, "nulltoken/HEAD");
assert_branch_has_been_found(exp, "nulltoken/master");
}
void test_refs_branches_iterator__mix_of_packed_and_loose(void)
{
git_branch_iterator *iter;
struct expectations exp[] = {
{ "master", 0 },
{ "origin/HEAD", 0 },
{ "origin/master", 0 },
{ "origin/packed", 0 },
{ NULL, 0 }
};
git_repository *r2;
r2 = cl_git_sandbox_init("testrepo2");
cl_git_pass(git_branch_iterator_new(&iter, r2, GIT_BRANCH_ALL));
contains_branches(exp, iter);
git_branch_iterator_free(iter);
assert_branch_has_been_found(exp, "master");
assert_branch_has_been_found(exp, "origin/HEAD");
assert_branch_has_been_found(exp, "origin/master");
assert_branch_has_been_found(exp, "origin/packed");
}
| libgit2-main | tests/libgit2/refs/branches/iterator.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "path.h"
static git_repository *repo;
static git_commit *target;
static git_reference *branch;
void test_refs_branches_create__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
branch = NULL;
target = NULL;
}
void test_refs_branches_create__cleanup(void)
{
git_reference_free(branch);
branch = NULL;
git_commit_free(target);
target = NULL;
cl_git_sandbox_cleanup();
repo = NULL;
}
static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha)
{
git_object *obj;
cl_git_pass(git_revparse_single(&obj, repo, sha));
cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj)));
git_object_free(obj);
}
static void retrieve_known_commit(git_commit **commit, git_repository *repo)
{
retrieve_target_from_oid(commit, repo, "e90810b8df3");
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
void test_refs_branches_create__can_create_a_local_branch(void)
{
retrieve_known_commit(&target, repo);
cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
}
void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
{
retrieve_known_commit(&target, repo);
cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0));
}
void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
{
retrieve_known_commit(&target, repo);
cl_git_pass(git_branch_create(&branch, repo, "br2", target, 1));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
}
void test_refs_branches_create__cannot_force_create_over_current_branch_in_nonbare_repo(void)
{
const git_oid *oid;
git_reference *branch2;
/* Default repo for these tests is a bare repo, but this test requires a non-bare one */
cl_git_sandbox_cleanup();
repo = cl_git_sandbox_init("testrepo");
retrieve_known_commit(&target, repo);
cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL));
cl_assert_equal_s("refs/heads/master", git_reference_name(branch2));
cl_assert_equal_i(true, git_branch_is_head(branch2));
oid = git_reference_target(branch2);
cl_git_fail_with(-1, git_branch_create(&branch, repo, "master", target, 1));
branch = NULL;
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
cl_git_pass(git_oid_cmp(git_reference_target(branch), oid));
git_reference_free(branch2);
}
void test_refs_branches_create__can_force_create_over_current_branch_in_bare_repo(void)
{
const git_oid *oid;
git_reference *branch2;
retrieve_known_commit(&target, repo);
cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL));
cl_assert_equal_s("refs/heads/master", git_reference_name(branch2));
cl_assert_equal_i(true, git_branch_is_head(branch2));
oid = git_commit_id(target);
cl_git_pass(git_branch_create(&branch, repo, "master", target, 1));
git_reference_free(branch);
branch = NULL;
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
cl_git_pass(git_oid_cmp(git_reference_target(branch), oid));
git_reference_free(branch2);
}
void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
retrieve_known_commit(&target, repo);
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_create(&branch, repo, "inv@{id", target, 0));
}
static void assert_branch_matches_name(
const char *expected, const char *lookup_as)
{
git_reference *ref;
git_str b = GIT_STR_INIT;
cl_git_pass(git_branch_lookup(&ref, repo, lookup_as, GIT_BRANCH_LOCAL));
cl_git_pass(git_str_sets(&b, "refs/heads/"));
cl_git_pass(git_str_puts(&b, expected));
cl_assert_equal_s(b.ptr, git_reference_name(ref));
cl_git_pass(
git_oid_cmp(git_reference_target(ref), git_commit_id(target)));
git_reference_free(ref);
git_str_dispose(&b);
}
void test_refs_branches_create__can_create_branch_with_unicode(void)
{
const char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
const char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
const char *emoji = "\xF0\x9F\x8D\xB7";
const char *names[] = { nfc, nfd, emoji };
const char *alt[] = { nfd, nfc, NULL };
const char *expected[] = { nfc, nfd, emoji };
unsigned int i;
bool fs_decompose_unicode =
git_fs_path_does_decompose_unicode(git_repository_path(repo));
retrieve_known_commit(&target, repo);
if (cl_repo_get_bool(repo, "core.precomposeunicode"))
expected[1] = nfc;
/* test decomp. because not all Mac filesystems decompose unicode */
else if (fs_decompose_unicode)
expected[0] = nfd;
for (i = 0; i < ARRAY_SIZE(names); ++i) {
const char *name;
cl_git_pass(git_branch_create(
&branch, repo, names[i], target, 0));
cl_git_pass(git_oid_cmp(
git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(expected[i], name);
assert_branch_matches_name(expected[i], names[i]);
if (fs_decompose_unicode && alt[i])
assert_branch_matches_name(expected[i], alt[i]);
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
branch = NULL;
}
}
/**
* Verify that we can create a branch with a name that matches the
* namespace of a previously delete branch.
*
* git branch level_one/level_two
* git branch -D level_one/level_two
* git branch level_one
*
* We expect the delete to have deleted the files:
* ".git/refs/heads/level_one/level_two"
* ".git/logs/refs/heads/level_one/level_two"
* It may or may not have deleted the (now empty)
* containing directories. To match git.git behavior,
* the second create needs to implicilty delete the
* directories and create the new files.
* "refs/heads/level_one"
* "logs/refs/heads/level_one"
*
* We should not fail to create the branch or its
* reflog because of an obsolete namespace container
* directory.
*/
void test_refs_branches_create__name_vs_namespace(void)
{
const char * name;
struct item {
const char *first;
const char *second;
};
static const struct item item[] = {
{ "level_one/level_two", "level_one" },
{ "a/b/c/d/e", "a/b/c/d" },
{ "ss/tt/uu/vv/ww", "ss" },
/* And one test case that is deeper. */
{ "xx1/xx2/xx3/xx4", "xx1/xx2/xx3/xx4/xx5/xx6" },
{ NULL, NULL },
};
const struct item *p;
retrieve_known_commit(&target, repo);
for (p=item; p->first; p++) {
cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(name, p->first);
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
branch = NULL;
cl_git_pass(git_branch_create(&branch, repo, p->second, target, 0));
git_reference_free(branch);
branch = NULL;
}
}
/**
* We still need to fail if part of the namespace is
* still in use.
*/
void test_refs_branches_create__name_vs_namespace_fail(void)
{
const char * name;
struct item {
const char *first;
const char *first_alternate;
const char *second;
};
static const struct item item[] = {
{ "level_one/level_two", "level_one/alternate", "level_one" },
{ "a/b/c/d/e", "a/b/c/d/alternate", "a/b/c/d" },
{ "ss/tt/uu/vv/ww", "ss/alternate", "ss" },
{ NULL, NULL, NULL },
};
const struct item *p;
retrieve_known_commit(&target, repo);
for (p=item; p->first; p++) {
cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(name, p->first);
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
branch = NULL;
cl_git_pass(git_branch_create(&branch, repo, p->first_alternate, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(name, p->first_alternate);
/* we do not delete the alternate. */
git_reference_free(branch);
branch = NULL;
cl_git_fail(git_branch_create(&branch, repo, p->second, target, 0));
git_reference_free(branch);
branch = NULL;
}
}
void test_refs_branches_create__error_when_create_branch_with_invalid_name(void)
{
retrieve_known_commit(&target, repo);
cl_git_fail(git_branch_create(&branch, repo, "HEAD", target, 0));
cl_git_fail(git_branch_create(&branch, repo, "-dash", target, 0));
}
| libgit2-main | tests/libgit2/refs/branches/create.c |
#include "clar_libgit2.h"
#include "config/config_helpers.h"
#include "refs.h"
static git_repository *repo;
static git_reference *branch, *upstream;
void test_refs_branches_upstream__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
branch = NULL;
upstream = NULL;
}
void test_refs_branches_upstream__cleanup(void)
{
git_reference_free(upstream);
git_reference_free(branch);
branch = NULL;
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_git_pass(git_branch_upstream(&upstream, branch));
cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream));
}
void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
cl_git_pass(git_branch_upstream(&upstream, branch));
cl_assert_equal_s("refs/heads/master", git_reference_name(upstream));
}
void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
cl_git_fail(git_branch_upstream(&upstream, branch));
}
void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
}
void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
}
void test_refs_branches_upstream__upstream_remote(void)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_branch_upstream_remote(&buf, repo, "refs/heads/master"));
cl_assert_equal_s("test", buf.ptr);
git_buf_dispose(&buf);
}
void test_refs_branches_upstream__upstream_merge(void)
{
git_reference *branch;
git_repository *repository;
git_buf buf = GIT_BUF_INIT;
repository = cl_git_sandbox_init("testrepo.git");
/* check repository */
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "test/master"));
assert_config_entry_value(repository, "branch.test.remote", "test");
assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
git_reference_free(branch);
/* check merge branch */
cl_git_pass(git_branch_upstream_merge(&buf, repository, "refs/heads/test"));
cl_assert_equal_s("refs/heads/master", buf.ptr);
git_buf_dispose(&buf);
cl_git_sandbox_cleanup();
}
void test_refs_branches_upstream__upstream_remote_empty_value(void)
{
git_repository *repository;
git_config *cfg;
git_buf buf = GIT_BUF_INIT;
repository = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_config(&cfg, repository));
cl_git_pass(git_config_set_string(cfg, "branch.master.remote", ""));
cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
cl_git_pass(git_config_delete_entry(cfg, "branch.master.remote"));
cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
cl_git_sandbox_cleanup();
}
static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
{
git_reference *branch;
cl_assert_equal_i(GIT_OBJECT_COMMIT, git_object_type((git_object*)target));
cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
git_reference_free(branch);
}
void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
{
git_reference *head;
git_repository *repository;
git_commit *target;
repository = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_head(&head, repository));
cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJECT_COMMIT));
git_reference_free(head);
assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
git_commit_free(target);
cl_git_sandbox_cleanup();
}
void test_refs_branches_upstream__set_unset_upstream(void)
{
git_reference *branch;
git_repository *repository;
repository = cl_git_sandbox_init("testrepo.git");
/* remote */
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "test/master"));
assert_config_entry_value(repository, "branch.test.remote", "test");
assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
git_reference_free(branch);
/* local */
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "master"));
assert_config_entry_value(repository, "branch.test.remote", ".");
assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
/* unset */
cl_git_pass(git_branch_set_upstream(branch, NULL));
assert_config_entry_existence(repository, "branch.test.remote", false);
assert_config_entry_existence(repository, "branch.test.merge", false);
git_reference_free(branch);
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
cl_git_pass(git_branch_set_upstream(branch, NULL));
assert_config_entry_existence(repository, "branch.test.remote", false);
assert_config_entry_existence(repository, "branch.test.merge", false);
git_reference_free(branch);
cl_git_sandbox_cleanup();
}
void test_refs_branches_upstream__no_fetch_refspec(void)
{
git_reference *ref, *branch;
git_repository *repo;
git_remote *remote;
git_config *cfg;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "matching", ".", NULL));
cl_git_pass(git_remote_add_push(repo, "matching", ":"));
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/test"));
cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/matching/master", git_reference_target(branch), 1, "fetch"));
cl_git_fail(git_branch_set_upstream(branch, "matching/master"));
cl_assert_equal_s("could not determine remote for 'refs/remotes/matching/master'",
git_error_last()->message);
/* we can't set it automatically, so let's test the user setting it by hand */
cl_git_pass(git_repository_config(&cfg, repo));
cl_git_pass(git_config_set_string(cfg, "branch.test.remote", "matching"));
cl_git_pass(git_config_set_string(cfg, "branch.test.merge", "refs/heads/master"));
/* we still can't find it because there is no rule for that reference */
cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream(&ref, branch));
git_reference_free(ref);
git_reference_free(branch);
git_remote_free(remote);
cl_git_sandbox_cleanup();
}
| libgit2-main | tests/libgit2/refs/branches/upstream.c |
#include "clar_libgit2.h"
#include "branch.h"
static git_repository *repo;
static git_reference *ref;
void test_refs_branches_name__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
}
void test_refs_branches_name__cleanup(void)
{
git_reference_free(ref);
ref = NULL;
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_name__can_get_local_branch_name(void)
{
const char *name;
cl_git_pass(git_branch_lookup(&ref,repo,"master",GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_name(&name,ref));
cl_assert_equal_s("master",name);
}
void test_refs_branches_name__can_get_remote_branch_name(void)
{
const char *name;
cl_git_pass(git_branch_lookup(&ref,repo,"test/master",GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_name(&name,ref));
cl_assert_equal_s("test/master",name);
}
void test_refs_branches_name__error_when_ref_is_no_branch(void)
{
const char *name;
cl_git_pass(git_reference_lookup(&ref,repo,"refs/notes/fanout"));
cl_git_fail(git_branch_name(&name,ref));
}
static int name_is_valid(const char *name)
{
int valid;
cl_git_pass(git_branch_name_is_valid(&valid, name));
return valid;
}
void test_refs_branches_name__is_name_valid(void)
{
cl_assert_equal_i(true, name_is_valid("master"));
cl_assert_equal_i(true, name_is_valid("test/master"));
cl_assert_equal_i(false, name_is_valid(""));
cl_assert_equal_i(false, name_is_valid("HEAD"));
cl_assert_equal_i(false, name_is_valid("-dash"));
}
| libgit2-main | tests/libgit2/refs/branches/name.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
#include "config/config_helpers.h"
#include "futils.h"
#include "reflog.h"
static git_repository *repo;
static git_reference *fake_remote;
void test_refs_branches_delete__initialize(void)
{
git_oid id;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
}
void test_refs_branches_delete__cleanup(void)
{
git_reference_free(fake_remote);
fake_remote = NULL;
cl_git_sandbox_cleanup();
repo = NULL;
}
void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
{
git_reference *head;
git_reference *branch;
/* Ensure HEAD targets the local master branch */
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
git_reference_free(head);
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_fail(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
{
git_reference *head;
git_reference *branch;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
git_reference_delete(head);
git_reference_free(head);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
{
git_reference *branch;
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
git_reference *head, *branch;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(head));
cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
git_reference_free(head);
/* Detach HEAD and make it target the commit that "master" points to */
git_repository_detach_head(repo);
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_local_branch(void)
{
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_remote_branch(void)
{
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
{
git_reference *branch;
assert_config_entry_existence(repo, "branch.track-local.remote", true);
assert_config_entry_existence(repo, "branch.track-local.merge", true);
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
}
void test_refs_branches_delete__removes_reflog(void)
{
git_reference *branch;
git_reflog *log;
git_oid oidzero = GIT_OID_SHA1_ZERO;
git_signature *sig;
/* Ensure the reflog has at least one entry */
cl_git_pass(git_signature_now(&sig, "Me", "[email protected]"));
cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
cl_assert(git_reflog_entrycount(log) > 0);
git_signature_free(sig);
git_reflog_free(log);
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
cl_assert_equal_i(false, git_reference_has_log(repo, "refs/heads/track-local"));
/* Reading a non-existent reflog creates it, but it should be empty */
cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
cl_assert_equal_i(0, git_reflog_entrycount(log));
git_reflog_free(log);
}
void test_refs_branches_delete__removes_empty_folders(void)
{
const char *commondir = git_repository_commondir(repo);
git_oid commit_id;
git_commit *commit;
git_reference *branch;
git_reflog *log;
git_oid oidzero = GIT_OID_SHA1_ZERO;
git_signature *sig;
git_str ref_folder = GIT_STR_INIT;
git_str reflog_folder = GIT_STR_INIT;
/* Create a new branch with a nested name */
cl_git_pass(git_oid__fromstr(&commit_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
cl_git_pass(git_branch_create(&branch, repo, "some/deep/ref", commit, 0));
git_commit_free(commit);
/* Ensure the reflog has at least one entry */
cl_git_pass(git_signature_now(&sig, "Me", "[email protected]"));
cl_git_pass(git_reflog_read(&log, repo, "refs/heads/some/deep/ref"));
cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
cl_assert(git_reflog_entrycount(log) > 0);
git_signature_free(sig);
git_reflog_free(log);
cl_git_pass(git_str_joinpath(&ref_folder, commondir, "refs/heads/some/deep"));
cl_git_pass(git_str_join3(&reflog_folder, '/', commondir, GIT_REFLOG_DIR, "refs/heads/some/deep"));
cl_assert(git_fs_path_exists(git_str_cstr(&ref_folder)) == true);
cl_assert(git_fs_path_exists(git_str_cstr(&reflog_folder)) == true);
cl_git_pass(git_branch_delete(branch));
cl_assert(git_fs_path_exists(git_str_cstr(&ref_folder)) == false);
cl_assert(git_fs_path_exists(git_str_cstr(&reflog_folder)) == false);
git_reference_free(branch);
git_str_dispose(&ref_folder);
git_str_dispose(&reflog_folder);
}
| libgit2-main | tests/libgit2/refs/branches/delete.c |
#include "clar_libgit2.h"
#include "refs.h"
#include "worktree/worktree_helpers.h"
static git_repository *repo;
static void assert_checked_out(git_repository *repo, const char *branch, int checked_out)
{
git_reference *ref;
cl_git_pass(git_reference_lookup(&ref, repo, branch));
cl_assert(git_branch_is_checked_out(ref) == checked_out);
git_reference_free(ref);
}
void test_refs_branches_checkedout__simple_repo(void)
{
repo = cl_git_sandbox_init("testrepo");
assert_checked_out(repo, "refs/heads/master", 1);
assert_checked_out(repo, "refs/heads/executable", 0);
cl_git_sandbox_cleanup();
}
void test_refs_branches_checkedout__worktree(void)
{
static worktree_fixture fixture =
WORKTREE_FIXTURE_INIT("testrepo", "testrepo-worktree");
setup_fixture_worktree(&fixture);
assert_checked_out(fixture.repo, "refs/heads/master", 1);
assert_checked_out(fixture.repo, "refs/heads/testrepo-worktree", 1);
assert_checked_out(fixture.worktree, "refs/heads/master", 1);
assert_checked_out(fixture.worktree, "refs/heads/testrepo-worktree", 1);
cleanup_fixture_worktree(&fixture);
}
void test_refs_branches_checkedout__head_is_not_checked_out(void)
{
repo = cl_git_sandbox_init("testrepo");
assert_checked_out(repo, "HEAD", 0);
cl_git_sandbox_cleanup();
}
void test_refs_branches_checkedout__master_in_bare_repo_is_not_checked_out(void)
{
repo = cl_git_sandbox_init("testrepo.git");
assert_checked_out(repo, "refs/heads/master", 0);
cl_git_sandbox_cleanup();
}
| libgit2-main | tests/libgit2/refs/branches/checkedout.c |
#include "clar_libgit2.h"
#include "branch.h"
static git_repository *repo;
static git_buf upstream_name;
void test_refs_branches_upstreamname__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
}
void test_refs_branches_upstreamname__cleanup(void)
{
git_buf_dispose(&upstream_name);
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void)
{
cl_git_pass(git_branch_upstream_name(
&upstream_name, repo, "refs/heads/master"));
cl_assert_equal_s("refs/remotes/test/master", upstream_name.ptr);
}
void test_refs_branches_upstreamname__can_retrieve_the_local_upstream_reference_name_of_a_local_branch(void)
{
cl_git_pass(git_branch_upstream_name(
&upstream_name, repo, "refs/heads/track-local"));
cl_assert_equal_s("refs/heads/master", upstream_name.ptr);
}
| libgit2-main | tests/libgit2/refs/branches/upstreamname.c |
#include "clar_libgit2.h"
#include "index.h"
#include "git2/sys/index.h"
#include "git2/repository.h"
#include "../reset/reset_helpers.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
/* Fixture setup and teardown */
void test_index_names__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_names__cleanup(void)
{
git_index_free(repo_index);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
static void index_add_conflicts(void)
{
git_index_entry entry = {{0}};
const char *paths[][3] = {
{ "ancestor", "ours", "theirs" },
{ "ancestor2", "ours2", "theirs2" },
{ "ancestor3", "ours3", "theirs3" } };
const char **conflict;
size_t i;
for (i = 0; i < ARRAY_SIZE(paths); i++) {
conflict = paths[i];
/* ancestor */
entry.path = conflict[0];
entry.mode = GIT_FILEMODE_BLOB;
GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_ANCESTOR);
git_oid__fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81", GIT_OID_SHA1);
cl_git_pass(git_index_add(repo_index, &entry));
/* ours */
entry.path = conflict[1];
entry.mode = GIT_FILEMODE_BLOB;
GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_OURS);
git_oid__fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81", GIT_OID_SHA1);
cl_git_pass(git_index_add(repo_index, &entry));
/* theirs */
entry.path = conflict[2];
entry.mode = GIT_FILEMODE_BLOB;
GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_THEIRS);
git_oid__fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81", GIT_OID_SHA1);
cl_git_pass(git_index_add(repo_index, &entry));
}
}
void test_index_names__add(void)
{
const git_index_name_entry *conflict_name;
index_add_conflicts();
cl_git_pass(git_index_name_add(repo_index, "ancestor", "ours", "theirs"));
cl_git_pass(git_index_name_add(repo_index, "ancestor2", "ours2", NULL));
cl_git_pass(git_index_name_add(repo_index, "ancestor3", NULL, "theirs3"));
cl_assert(git_index_name_entrycount(repo_index) == 3);
conflict_name = git_index_name_get_byindex(repo_index, 0);
cl_assert(strcmp(conflict_name->ancestor, "ancestor") == 0);
cl_assert(strcmp(conflict_name->ours, "ours") == 0);
cl_assert(strcmp(conflict_name->theirs, "theirs") == 0);
conflict_name = git_index_name_get_byindex(repo_index, 1);
cl_assert(strcmp(conflict_name->ancestor, "ancestor2") == 0);
cl_assert(strcmp(conflict_name->ours, "ours2") == 0);
cl_assert(conflict_name->theirs == NULL);
conflict_name = git_index_name_get_byindex(repo_index, 2);
cl_assert(strcmp(conflict_name->ancestor, "ancestor3") == 0);
cl_assert(conflict_name->ours == NULL);
cl_assert(strcmp(conflict_name->theirs, "theirs3") == 0);
cl_git_pass(git_index_write(repo_index));
}
void test_index_names__roundtrip(void)
{
const git_index_name_entry *conflict_name;
cl_git_pass(git_index_name_add(repo_index, "ancestor", "ours", "theirs"));
cl_git_pass(git_index_name_add(repo_index, "ancestor2", "ours2", NULL));
cl_git_pass(git_index_name_add(repo_index, "ancestor3", NULL, "theirs3"));
cl_git_pass(git_index_write(repo_index));
git_index_clear(repo_index);
cl_assert(git_index_name_entrycount(repo_index) == 0);
cl_git_pass(git_index_read(repo_index, true));
cl_assert(git_index_name_entrycount(repo_index) == 3);
conflict_name = git_index_name_get_byindex(repo_index, 0);
cl_assert(strcmp(conflict_name->ancestor, "ancestor") == 0);
cl_assert(strcmp(conflict_name->ours, "ours") == 0);
cl_assert(strcmp(conflict_name->theirs, "theirs") == 0);
conflict_name = git_index_name_get_byindex(repo_index, 1);
cl_assert(strcmp(conflict_name->ancestor, "ancestor2") == 0);
cl_assert(strcmp(conflict_name->ours, "ours2") == 0);
cl_assert(conflict_name->theirs == NULL);
conflict_name = git_index_name_get_byindex(repo_index, 2);
cl_assert(strcmp(conflict_name->ancestor, "ancestor3") == 0);
cl_assert(conflict_name->ours == NULL);
cl_assert(strcmp(conflict_name->theirs, "theirs3") == 0);
}
void test_index_names__cleaned_on_reset_hard(void)
{
git_object *target;
cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_names__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
cl_assert(git_index_name_entrycount(repo_index) == 0);
git_object_free(target);
}
void test_index_names__cleaned_on_reset_mixed(void)
{
git_object *target;
cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_names__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
cl_assert(git_index_name_entrycount(repo_index) == 0);
git_object_free(target);
}
void test_index_names__cleaned_on_checkout_tree(void)
{
git_oid oid;
git_object *obj;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
test_index_names__add();
cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/master"));
cl_git_pass(git_object_lookup(&obj, repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(repo, obj, &opts));
cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
git_object_free(obj);
}
void test_index_names__cleaned_on_checkout_head(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
test_index_names__add();
cl_git_pass(git_checkout_head(repo, &opts));
cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
}
void test_index_names__retained_on_checkout_index(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
test_index_names__add();
cl_git_pass(git_checkout_index(repo, repo_index, &opts));
cl_assert(git_index_name_entrycount(repo_index) > 0);
}
| libgit2-main | tests/libgit2/index/names.c |
#include "clar_libgit2.h"
#include "repository.h"
#include "../submodule/submodule_helpers.h"
static git_repository *g_repo;
static git_index *g_idx;
void test_index_bypath__initialize(void)
{
g_repo = setup_fixture_submod2();
cl_git_pass(git_repository_index__weakptr(&g_idx, g_repo));
}
void test_index_bypath__cleanup(void)
{
g_repo = NULL;
g_idx = NULL;
}
void test_index_bypath__add_directory(void)
{
cl_git_fail_with(GIT_EDIRECTORY, git_index_add_bypath(g_idx, "just_a_dir"));
}
void test_index_bypath__add_submodule(void)
{
unsigned int status;
const char *sm_name = "sm_changed_head";
cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
cl_assert_equal_i(GIT_SUBMODULE_STATUS_WD_MODIFIED, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
cl_git_pass(git_index_add_bypath(g_idx, sm_name));
cl_git_pass(git_submodule_status(&status, g_repo, sm_name, 0));
cl_assert_equal_i(0, status & GIT_SUBMODULE_STATUS_WD_MODIFIED);
}
void test_index_bypath__add_submodule_unregistered(void)
{
const char *sm_name = "not-submodule";
const char *sm_head = "68e92c611b80ee1ed8f38314ff9577f0d15b2444";
const git_index_entry *entry;
cl_git_pass(git_index_add_bypath(g_idx, sm_name));
cl_assert(entry = git_index_get_bypath(g_idx, sm_name, 0));
cl_assert_equal_s(sm_head, git_oid_tostr_s(&entry->id));
cl_assert_equal_s(sm_name, entry->path);
}
void test_index_bypath__add_hidden(void)
{
#ifdef GIT_WIN32
const git_index_entry *entry;
bool hidden;
cl_git_mkfile("submod2/hidden_file", "you can't see me");
cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
cl_assert(!hidden);
cl_git_pass(git_win32__set_hidden("submod2/hidden_file", true));
cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
cl_assert(hidden);
cl_git_pass(git_index_add_bypath(g_idx, "hidden_file"));
cl_assert(entry = git_index_get_bypath(g_idx, "hidden_file", 0));
cl_assert_equal_i(GIT_FILEMODE_BLOB, entry->mode);
#endif
}
void test_index_bypath__add_keeps_existing_case(void)
{
const git_index_entry *entry;
if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
clar__skip();
cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/file1.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/file1.txt", 0));
cl_assert_equal_s("just_a_dir/file1.txt", entry->path);
cl_git_rewritefile("submod2/just_a_dir/file1.txt", "Updated!");
cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/FILE1.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/FILE1.txt", 0));
cl_assert_equal_s("just_a_dir/file1.txt", entry->path);
}
void test_index_bypath__add_honors_existing_case(void)
{
const git_index_entry *entry;
if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
clar__skip();
cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/File1.txt"));
cl_git_pass(git_index_add_bypath(g_idx, "JUST_A_DIR/file2.txt"));
cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/FILE3.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/File1.txt", 0));
cl_assert_equal_s("just_a_dir/File1.txt", entry->path);
cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/file2.txt", 0));
cl_assert_equal_s("just_a_dir/file2.txt", entry->path);
cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/FILE3.txt", 0));
cl_assert_equal_s("just_a_dir/FILE3.txt", entry->path);
cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/file3.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/file3.txt", 0));
cl_assert_equal_s("just_a_dir/FILE3.txt", entry->path);
}
void test_index_bypath__add_honors_existing_case_2(void)
{
git_index_entry dummy = { { 0 } };
const git_index_entry *entry;
if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
clar__skip();
dummy.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b", GIT_OID_SHA1));
/* note that `git_index_add` does no checking to canonical directories */
dummy.path = "Just_a_dir/file0.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "just_a_dir/fileA.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "Just_A_Dir/fileB.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "JUST_A_DIR/fileC.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "just_A_dir/fileD.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "JUST_a_DIR/fileE.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
cl_git_mkfile("submod2/just_a_dir/file1.txt", "This is a file");
cl_git_mkfile("submod2/just_a_dir/file2.txt", "This is another file");
cl_git_mkfile("submod2/just_a_dir/file3.txt", "This is another file");
cl_git_mkfile("submod2/just_a_dir/file4.txt", "And another file");
cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/File1.txt"));
cl_git_pass(git_index_add_bypath(g_idx, "JUST_A_DIR/file2.txt"));
cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/FILE3.txt"));
cl_git_pass(git_index_add_bypath(g_idx, "JusT_A_DIR/FILE4.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/File1.txt", 0));
cl_assert_equal_s("just_a_dir/File1.txt", entry->path);
cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/file2.txt", 0));
cl_assert_equal_s("JUST_A_DIR/file2.txt", entry->path);
cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/FILE3.txt", 0));
cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry->path);
cl_git_rewritefile("submod2/just_a_dir/file3.txt", "Rewritten");
cl_git_pass(git_index_add_bypath(g_idx, "Just_A_Dir/file3.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "Just_A_Dir/file3.txt", 0));
cl_assert_equal_s("Just_A_Dir/FILE3.txt", entry->path);
}
void test_index_bypath__add_honors_existing_case_3(void)
{
git_index_entry dummy = { { 0 } };
const git_index_entry *entry;
if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
clar__skip();
dummy.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b", GIT_OID_SHA1));
dummy.path = "just_a_dir/filea.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "Just_A_Dir/fileB.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "just_A_DIR/FILEC.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "Just_a_DIR/FileD.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
cl_git_mkfile("submod2/JuSt_A_DiR/fILEE.txt", "This is a file");
cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/fILEE.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "JUST_A_DIR/fILEE.txt", 0));
cl_assert_equal_s("just_a_dir/fILEE.txt", entry->path);
}
void test_index_bypath__add_honors_existing_case_4(void)
{
git_index_entry dummy = { { 0 } };
const git_index_entry *entry;
if (!cl_repo_get_bool(g_repo, "core.ignorecase"))
clar__skip();
dummy.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b", GIT_OID_SHA1));
dummy.path = "just_a_dir/a/b/c/d/e/file1.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
dummy.path = "just_a_dir/a/B/C/D/E/file2.txt";
cl_git_pass(git_index_add(g_idx, &dummy));
cl_must_pass(p_mkdir("submod2/just_a_dir/a", 0777));
cl_must_pass(p_mkdir("submod2/just_a_dir/a/b", 0777));
cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z", 0777));
cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y", 0777));
cl_must_pass(p_mkdir("submod2/just_a_dir/a/b/z/y/x", 0777));
cl_git_mkfile("submod2/just_a_dir/a/b/z/y/x/FOO.txt", "This is a file");
cl_git_pass(git_index_add_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt"));
cl_assert(entry = git_index_get_bypath(g_idx, "just_a_dir/A/b/Z/y/X/foo.txt", 0));
cl_assert_equal_s("just_a_dir/a/b/Z/y/X/foo.txt", entry->path);
}
void test_index_bypath__add_honors_mode(void)
{
const git_index_entry *entry;
git_index_entry new_entry;
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
memcpy(&new_entry, entry, sizeof(git_index_entry));
new_entry.path = "README.txt";
new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
cl_git_pass(git_index_add(g_idx, &new_entry));
cl_git_pass(git_index_write(g_idx));
cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
cl_git_pass(git_index_write(g_idx));
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, entry->mode);
}
void test_index_bypath__add_honors_conflict_mode(void)
{
const git_index_entry *entry;
git_index_entry new_entry;
int stage = 0;
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
memcpy(&new_entry, entry, sizeof(git_index_entry));
new_entry.path = "README.txt";
new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
cl_git_pass(git_index_remove_bypath(g_idx, "README.txt"));
for (stage = 1; stage <= 3; stage++) {
new_entry.flags = stage << GIT_INDEX_ENTRY_STAGESHIFT;
cl_git_pass(git_index_add(g_idx, &new_entry));
}
cl_git_pass(git_index_write(g_idx));
cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
cl_git_pass(git_index_write(g_idx));
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, entry->mode);
}
void test_index_bypath__add_honors_conflict_case(void)
{
const git_index_entry *entry;
git_index_entry new_entry;
int stage = 0;
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
memcpy(&new_entry, entry, sizeof(git_index_entry));
new_entry.path = "README.txt";
new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
cl_must_pass(p_chmod("submod2/README.txt", GIT_FILEMODE_BLOB_EXECUTABLE));
cl_git_pass(git_index_remove_bypath(g_idx, "README.txt"));
for (stage = 1; stage <= 3; stage++) {
new_entry.flags = stage << GIT_INDEX_ENTRY_STAGESHIFT;
cl_git_pass(git_index_add(g_idx, &new_entry));
}
cl_git_pass(git_index_write(g_idx));
cl_git_rewritefile("submod2/README.txt", "Modified but still executable");
cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
cl_git_pass(git_index_write(g_idx));
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, entry->mode);
}
void test_index_bypath__add_honors_symlink(void)
{
const git_index_entry *entry;
git_index_entry new_entry;
int symlinks;
cl_git_pass(git_repository__configmap_lookup(&symlinks, g_repo, GIT_CONFIGMAP_SYMLINKS));
if (symlinks)
cl_skip();
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
memcpy(&new_entry, entry, sizeof(git_index_entry));
new_entry.path = "README.txt";
new_entry.mode = GIT_FILEMODE_LINK;
cl_git_pass(git_index_add(g_idx, &new_entry));
cl_git_pass(git_index_write(g_idx));
cl_git_rewritefile("submod2/README.txt", "Modified but still a (fake) symlink");
cl_git_pass(git_index_add_bypath(g_idx, "README.txt"));
cl_git_pass(git_index_write(g_idx));
cl_assert((entry = git_index_get_bypath(g_idx, "README.txt", 0)) != NULL);
cl_assert_equal_i(GIT_FILEMODE_LINK, entry->mode);
}
| libgit2-main | tests/libgit2/index/bypath.c |
#include "clar_libgit2.h"
#include "posix.h"
void test_index_rename__single_file(void)
{
git_repository *repo;
git_index *index;
size_t position;
git_oid expected;
const git_index_entry *entry;
p_mkdir("rename", 0700);
cl_git_pass(git_repository_init(&repo, "./rename", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
cl_git_mkfile("./rename/lame.name.txt", "new_file\n");
/* This should add a new blob to the object database in 'd4/fa8600b4f37d7516bef4816ae2c64dbf029e3a' */
cl_git_pass(git_index_add_bypath(index, "lame.name.txt"));
cl_assert(git_index_entrycount(index) == 1);
cl_git_pass(git_oid__fromstr(&expected, "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a", GIT_OID_SHA1));
cl_assert(!git_index_find(&position, index, "lame.name.txt"));
entry = git_index_get_byindex(index, position);
cl_assert_equal_oid(&expected, &entry->id);
/* This removes the entry from the index, but not from the object database */
cl_git_pass(git_index_remove(index, "lame.name.txt", 0));
cl_assert(git_index_entrycount(index) == 0);
p_rename("./rename/lame.name.txt", "./rename/fancy.name.txt");
cl_git_pass(git_index_add_bypath(index, "fancy.name.txt"));
cl_assert(git_index_entrycount(index) == 1);
cl_assert(!git_index_find(&position, index, "fancy.name.txt"));
entry = git_index_get_byindex(index, position);
cl_assert_equal_oid(&expected, &entry->id);
git_index_free(index);
git_repository_free(repo);
cl_fixture_cleanup("rename");
}
void test_index_rename__casechanging(void)
{
git_repository *repo;
git_index *index;
const git_index_entry *entry;
git_index_entry new = {{0}};
p_mkdir("rename", 0700);
cl_git_pass(git_repository_init(&repo, "./rename", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_git_mkfile("./rename/lame.name.txt", "new_file\n");
cl_git_pass(git_index_add_bypath(index, "lame.name.txt"));
cl_assert_equal_i(1, git_index_entrycount(index));
cl_assert((entry = git_index_get_bypath(index, "lame.name.txt", 0)));
memcpy(&new, entry, sizeof(git_index_entry));
new.path = "LAME.name.TXT";
cl_git_pass(git_index_add(index, &new));
cl_assert((entry = git_index_get_bypath(index, "LAME.name.TXT", 0)));
if (cl_repo_get_bool(repo, "core.ignorecase"))
cl_assert_equal_i(1, git_index_entrycount(index));
else
cl_assert_equal_i(2, git_index_entrycount(index));
git_index_free(index);
git_repository_free(repo);
cl_fixture_cleanup("rename");
}
| libgit2-main | tests/libgit2/index/rename.c |
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
/* Fixture setup and teardown */
void test_index_stage__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_stage__cleanup(void)
{
git_index_free(repo_index);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
void test_index_stage__add_always_adds_stage_0(void)
{
size_t entry_idx;
const git_index_entry *entry;
cl_git_mkfile("./mergedrepo/new-file.txt", "new-file\n");
cl_git_pass(git_index_add_bypath(repo_index, "new-file.txt"));
cl_assert(!git_index_find(&entry_idx, repo_index, "new-file.txt"));
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 0);
}
void test_index_stage__find_gets_first_stage(void)
{
size_t entry_idx;
const git_index_entry *entry;
cl_assert(!git_index_find(&entry_idx, repo_index, "one.txt"));
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 0);
cl_assert(!git_index_find(&entry_idx, repo_index, "two.txt"));
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 0);
cl_assert(!git_index_find(&entry_idx, repo_index, "conflicts-one.txt"));
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 1);
cl_assert(!git_index_find(&entry_idx, repo_index, "conflicts-two.txt"));
cl_assert((entry = git_index_get_byindex(repo_index, entry_idx)) != NULL);
cl_assert(git_index_entry_stage(entry) == 1);
}
| libgit2-main | tests/libgit2/index/stage.c |
#include "clar_libgit2.h"
void test_index_inmemory__can_create_an_inmemory_index(void)
{
git_index *index;
cl_git_pass(git_index_new(&index));
cl_assert_equal_i(0, (int)git_index_entrycount(index));
git_index_free(index);
}
void test_index_inmemory__cannot_add_bypath_to_an_inmemory_index(void)
{
git_index *index;
cl_git_pass(git_index_new(&index));
cl_assert_equal_i(GIT_ERROR, git_index_add_bypath(index, "test.txt"));
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/inmemory.c |
#include "clar_libgit2.h"
#include "../checkout/checkout_helpers.h"
#include "index.h"
#include "repository.h"
static git_repository *g_repo;
void test_index_racy__initialize(void)
{
cl_git_pass(git_repository_init(&g_repo, "diff_racy", false));
}
void test_index_racy__cleanup(void)
{
git_repository_free(g_repo);
g_repo = NULL;
cl_fixture_cleanup("diff_racy");
}
void test_index_racy__diff(void)
{
git_index *index;
git_diff *diff;
git_str path = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "A"));
cl_git_mkfile(path.ptr, "A");
/* Put 'A' into the index */
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "A"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
cl_assert_equal_i(0, git_diff_num_deltas(diff));
git_diff_free(diff);
/* Change its contents quickly, so we get the same timestamp */
cl_git_mkfile(path.ptr, "B");
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
git_index_free(index);
git_diff_free(diff);
git_str_dispose(&path);
}
void test_index_racy__write_index_just_after_file(void)
{
git_index *index;
git_diff *diff;
git_str path = GIT_STR_INIT;
struct p_timeval times[2];
/* Make sure we do have a timestamp */
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "A"));
cl_git_mkfile(path.ptr, "A");
/* Force the file's timestamp to be a second after we wrote the index */
times[0].tv_sec = index->stamp.mtime.tv_sec + 1;
times[0].tv_usec = index->stamp.mtime.tv_nsec / 1000;
times[1].tv_sec = index->stamp.mtime.tv_sec + 1;
times[1].tv_usec = index->stamp.mtime.tv_nsec / 1000;
cl_git_pass(p_utimes(path.ptr, times));
/*
* Put 'A' into the index, the size field will be filled,
* because the index' on-disk timestamp does not match the
* file's timestamp.
*/
cl_git_pass(git_index_add_bypath(index, "A"));
cl_git_pass(git_index_write(index));
cl_git_mkfile(path.ptr, "B");
/*
* Pretend this index' modification happened a second after the
* file update, and rewrite the file in that same second.
*/
times[0].tv_sec = index->stamp.mtime.tv_sec + 2;
times[0].tv_usec = index->stamp.mtime.tv_nsec / 1000;
times[1].tv_sec = index->stamp.mtime.tv_sec + 2;
times[0].tv_usec = index->stamp.mtime.tv_nsec / 1000;
cl_git_pass(p_utimes(git_index_path(index), times));
cl_git_pass(p_utimes(path.ptr, times));
cl_git_pass(git_index_read(index, true));
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
git_str_dispose(&path);
git_diff_free(diff);
git_index_free(index);
}
static void setup_race(void)
{
git_str path = GIT_STR_INIT;
git_index *index;
git_index_entry *entry;
struct stat st;
/* Make sure we do have a timestamp */
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "A"));
cl_git_mkfile(path.ptr, "A");
cl_git_pass(git_index_add_bypath(index, "A"));
cl_git_mkfile(path.ptr, "B");
cl_git_pass(git_index_write(index));
cl_git_mkfile(path.ptr, "");
cl_git_pass(p_stat(path.ptr, &st));
cl_assert(entry = (git_index_entry *)git_index_get_bypath(index, "A", 0));
/* force a race */
entry->mtime.seconds = (int32_t)st.st_mtime;
entry->mtime.nanoseconds = (int32_t)st.st_mtime_nsec;
git_str_dispose(&path);
}
void test_index_racy__smudges_index_entry_on_save(void)
{
git_index *index;
const git_index_entry *entry;
setup_race();
/* write the index, which will smudge anything that had the same timestamp
* as the index when the index was loaded. that way future loads of the
* index (with the new timestamp) will know that these files were not
* clean.
*/
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_assert(entry = git_index_get_bypath(index, "A", 0));
cl_assert_equal_i(0, entry->file_size);
}
void test_index_racy__detects_diff_of_change_in_identical_timestamp(void)
{
git_index *index;
git_diff *diff;
cl_git_pass(git_repository_index__weakptr(&index, g_repo));
setup_race();
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
cl_assert_equal_i(1, git_diff_num_deltas(diff));
git_diff_free(diff);
}
static void setup_uptodate_files(void)
{
git_str path = GIT_STR_INIT;
git_index *index;
const git_index_entry *a_entry;
git_index_entry new_entry = {{0}};
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "A"));
cl_git_mkfile(path.ptr, "A");
/* Put 'A' into the index */
cl_git_pass(git_index_add_bypath(index, "A"));
cl_assert((a_entry = git_index_get_bypath(index, "A", 0)));
/* Put 'B' into the index */
new_entry.path = "B";
new_entry.mode = GIT_FILEMODE_BLOB;
git_oid_cpy(&new_entry.id, &a_entry->id);
cl_git_pass(git_index_add(index, &new_entry));
/* Put 'C' into the index */
new_entry.path = "C";
new_entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add_from_buffer(index, &new_entry, "hello!\n", 7));
git_index_free(index);
git_str_dispose(&path);
}
void test_index_racy__adding_to_index_is_uptodate(void)
{
git_index *index;
const git_index_entry *entry;
setup_uptodate_files();
cl_git_pass(git_repository_index(&index, g_repo));
/* ensure that they're all uptodate */
cl_assert((entry = git_index_get_bypath(index, "A", 0)));
cl_assert_equal_i(GIT_INDEX_ENTRY_UPTODATE, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(index, "B", 0)));
cl_assert_equal_i(GIT_INDEX_ENTRY_UPTODATE, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(index, "C", 0)));
cl_assert_equal_i(GIT_INDEX_ENTRY_UPTODATE, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_git_pass(git_index_write(index));
git_index_free(index);
}
void test_index_racy__reading_clears_uptodate_bit(void)
{
git_index *index;
const git_index_entry *entry;
setup_uptodate_files();
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true));
/* ensure that no files are uptodate */
cl_assert((entry = git_index_get_bypath(index, "A", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(index, "B", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(index, "C", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
git_index_free(index);
}
void test_index_racy__read_tree_clears_uptodate_bit(void)
{
git_index *index;
git_tree *tree;
const git_index_entry *entry;
git_oid id;
setup_uptodate_files();
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_write_tree_to(&id, index, g_repo));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_index_read_tree(index, tree));
/* ensure that no files are uptodate */
cl_assert((entry = git_index_get_bypath(index, "A", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(index, "B", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(index, "C", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
git_tree_free(tree);
git_index_free(index);
}
void test_index_racy__read_index_smudges(void)
{
git_index *index, *newindex;
const git_index_entry *entry;
/* if we are reading an index into our new index, ensure that any
* racy entries in the index that we're reading are smudged so that
* we don't propagate their timestamps without further investigation.
*/
setup_race();
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_new(&newindex));
cl_git_pass(git_index_read_index(newindex, index));
cl_assert(entry = git_index_get_bypath(newindex, "A", 0));
cl_assert_equal_i(0, entry->file_size);
git_index_free(index);
git_index_free(newindex);
}
void test_index_racy__read_index_clears_uptodate_bit(void)
{
git_index *index, *newindex;
const git_index_entry *entry;
setup_uptodate_files();
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_new(&newindex));
cl_git_pass(git_index_read_index(newindex, index));
/* ensure that files brought in from the other index are not uptodate */
cl_assert((entry = git_index_get_bypath(newindex, "A", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(newindex, "B", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
cl_assert((entry = git_index_get_bypath(newindex, "C", 0)));
cl_assert_equal_i(0, (entry->flags_extended & GIT_INDEX_ENTRY_UPTODATE));
git_index_free(index);
git_index_free(newindex);
}
| libgit2-main | tests/libgit2/index/racy.c |
#include "clar_libgit2.h"
#include "posix.h"
/* Test that reading and writing a tree is a no-op */
void test_index_read_tree__read_write_involution(void)
{
git_repository *repo;
git_index *index;
git_oid tree_oid;
git_tree *tree;
git_oid expected;
p_mkdir("read_tree", 0700);
cl_git_pass(git_repository_init(&repo, "./read_tree", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
p_mkdir("./read_tree/abc", 0700);
/* Sort order: '-' < '/' < '_' */
cl_git_mkfile("./read_tree/abc-d", NULL);
cl_git_mkfile("./read_tree/abc/d", NULL);
cl_git_mkfile("./read_tree/abc_d", NULL);
cl_git_pass(git_index_add_bypath(index, "abc-d"));
cl_git_pass(git_index_add_bypath(index, "abc_d"));
cl_git_pass(git_index_add_bypath(index, "abc/d"));
/* write-tree */
cl_git_pass(git_index_write_tree(&expected, index));
/* read-tree */
git_tree_lookup(&tree, repo, &expected);
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_git_pass(git_index_write_tree(&tree_oid, index));
cl_assert_equal_oid(&expected, &tree_oid);
git_index_free(index);
git_repository_free(repo);
cl_fixture_cleanup("read_tree");
}
| libgit2-main | tests/libgit2/index/read_tree.c |
#include "clar_libgit2.h"
#include "index.h"
static git_repository *g_repo;
void test_index_splitindex__initialize(void)
{
g_repo = cl_git_sandbox_init("splitindex");
}
void test_index_splitindex__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_index_splitindex__fail_on_open(void)
{
git_index *idx;
cl_git_fail_with(-1, git_repository_index(&idx, g_repo));
cl_assert_equal_s(git_error_last()->message, "unsupported mandatory extension: 'link'");
}
| libgit2-main | tests/libgit2/index/splitindex.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "index.h"
static git_repository *g_repo = NULL;
void test_index_filemodes__initialize(void)
{
g_repo = cl_git_sandbox_init("filemodes");
}
void test_index_filemodes__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_index_filemodes__read(void)
{
git_index *index;
unsigned int i;
static bool expected[6] = { 0, 1, 0, 1, 0, 1 };
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert_equal_i(6, (int)git_index_entrycount(index));
for (i = 0; i < 6; ++i) {
const git_index_entry *entry = git_index_get_byindex(index, i);
cl_assert(entry != NULL);
cl_assert(((entry->mode & 0100) ? 1 : 0) == expected[i]);
}
git_index_free(index);
}
static void replace_file_with_mode(
const char *filename, const char *backup, unsigned int create_mode)
{
git_str path = GIT_STR_INIT, content = GIT_STR_INIT;
cl_git_pass(git_str_joinpath(&path, "filemodes", filename));
cl_git_pass(git_str_printf(&content, "%s as %08u (%d)",
filename, create_mode, rand()));
cl_git_pass(p_rename(path.ptr, backup));
cl_git_write2file(
path.ptr, content.ptr, content.size,
O_WRONLY|O_CREAT|O_TRUNC, create_mode);
git_str_dispose(&path);
git_str_dispose(&content);
}
#define add_and_check_mode(I,F,X) add_and_check_mode_(I,F,X,__FILE__,__func__,__LINE__)
static void add_and_check_mode_(
git_index *index, const char *filename, unsigned int expect_mode,
const char *file, const char *func, int line)
{
size_t pos;
const git_index_entry *entry;
cl_git_pass(git_index_add_bypath(index, filename));
clar__assert(!git_index_find(&pos, index, filename),
file, func, line, "Cannot find index entry", NULL, 1);
entry = git_index_get_byindex(index, pos);
clar__assert_equal(file, func, line, "Expected mode does not match index",
1, "%07o", (unsigned int)entry->mode, (unsigned int)expect_mode);
}
void test_index_filemodes__untrusted(void)
{
git_index *index;
cl_repo_set_bool(g_repo, "core.filemode", false);
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert((git_index_caps(index) & GIT_INDEX_CAPABILITY_NO_FILEMODE) != 0);
/* 1 - add 0644 over existing 0644 -> expect 0644 */
replace_file_with_mode("exec_off", "filemodes/exec_off.0", 0644);
add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB);
/* 2 - add 0644 over existing 0755 -> expect 0755 */
replace_file_with_mode("exec_on", "filemodes/exec_on.0", 0644);
add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 3 - add 0755 over existing 0644 -> expect 0644 */
replace_file_with_mode("exec_off", "filemodes/exec_off.1", 0755);
add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB);
/* 4 - add 0755 over existing 0755 -> expect 0755 */
replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755);
add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 5 - add new 0644 -> expect 0644 */
cl_git_write2file("filemodes/new_off", "blah", 0,
O_WRONLY | O_CREAT | O_TRUNC, 0644);
add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
/* 6 - add new 0755 -> expect 0644 if core.filemode == false */
cl_git_write2file("filemodes/new_on", "blah", 0,
O_WRONLY | O_CREAT | O_TRUNC, 0755);
add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB);
git_index_free(index);
}
void test_index_filemodes__trusted(void)
{
git_index *index;
/* Only run these tests on platforms where I can actually
* chmod a file and get the stat results I expect!
*/
if (!cl_is_chmod_supported())
return;
cl_repo_set_bool(g_repo, "core.filemode", true);
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert((git_index_caps(index) & GIT_INDEX_CAPABILITY_NO_FILEMODE) == 0);
/* 1 - add 0644 over existing 0644 -> expect 0644 */
replace_file_with_mode("exec_off", "filemodes/exec_off.0", 0644);
add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB);
/* 2 - add 0644 over existing 0755 -> expect 0644 */
replace_file_with_mode("exec_on", "filemodes/exec_on.0", 0644);
add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB);
/* 3 - add 0755 over existing 0644 -> expect 0755 */
replace_file_with_mode("exec_off", "filemodes/exec_off.1", 0755);
add_and_check_mode(index, "exec_off", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 4 - add 0755 over existing 0755 -> expect 0755 */
replace_file_with_mode("exec_on", "filemodes/exec_on.1", 0755);
add_and_check_mode(index, "exec_on", GIT_FILEMODE_BLOB_EXECUTABLE);
/* 5 - add new 0644 -> expect 0644 */
cl_git_write2file("filemodes/new_off", "blah", 0,
O_WRONLY | O_CREAT | O_TRUNC, 0644);
add_and_check_mode(index, "new_off", GIT_FILEMODE_BLOB);
/* 6 - add 0755 -> expect 0755 */
cl_git_write2file("filemodes/new_on", "blah", 0,
O_WRONLY | O_CREAT | O_TRUNC, 0755);
add_and_check_mode(index, "new_on", GIT_FILEMODE_BLOB_EXECUTABLE);
git_index_free(index);
}
#define add_entry_and_check_mode(I,FF,X) add_entry_and_check_mode_(I,FF,X,__FILE__,__func__,__LINE__)
static void add_entry_and_check_mode_(
git_index *index, bool from_file, git_filemode_t mode,
const char *file, const char *func, int line)
{
size_t pos;
const git_index_entry* entry;
git_index_entry new_entry;
/* If old_filename exists, we copy that to the new file, and test
* git_index_add(), otherwise create a new entry testing git_index_add_from_buffer
*/
if (from_file)
{
clar__assert(!git_index_find(&pos, index, "exec_off"),
file, func, line, "Cannot find original index entry", NULL, 1);
entry = git_index_get_byindex(index, pos);
memcpy(&new_entry, entry, sizeof(new_entry));
}
else
memset(&new_entry, 0x0, sizeof(git_index_entry));
new_entry.path = "filemodes/explicit_test";
new_entry.mode = mode;
if (from_file)
{
clar__assert(!git_index_add(index, &new_entry),
file, func, line, "Cannot add index entry", NULL, 1);
}
else
{
const char *content = "hey there\n";
clar__assert(!git_index_add_from_buffer(index, &new_entry, content, strlen(content)),
file, func, line, "Cannot add index entry from buffer", NULL, 1);
}
clar__assert(!git_index_find(&pos, index, "filemodes/explicit_test"),
file, func, line, "Cannot find new index entry", NULL, 1);
entry = git_index_get_byindex(index, pos);
clar__assert_equal(file, func, line, "Expected mode does not match index",
1, "%07o", (unsigned int)entry->mode, (unsigned int)mode);
}
void test_index_filemodes__explicit(void)
{
git_index *index;
/* These tests should run and work everywhere, as the filemode is
* given explicitly to git_index_add or git_index_add_from_buffer
*/
cl_repo_set_bool(g_repo, "core.filemode", false);
cl_git_pass(git_repository_index(&index, g_repo));
/* Each of these tests keeps overwriting the same file in the index. */
/* 1 - add new 0644 entry */
add_entry_and_check_mode(index, true, GIT_FILEMODE_BLOB);
/* 2 - add 0755 entry over existing 0644 */
add_entry_and_check_mode(index, true, GIT_FILEMODE_BLOB_EXECUTABLE);
/* 3 - add 0644 entry over existing 0755 */
add_entry_and_check_mode(index, true, GIT_FILEMODE_BLOB);
/* 4 - add 0755 buffer entry over existing 0644 */
add_entry_and_check_mode(index, false, GIT_FILEMODE_BLOB_EXECUTABLE);
/* 5 - add 0644 buffer entry over existing 0755 */
add_entry_and_check_mode(index, false, GIT_FILEMODE_BLOB);
git_index_free(index);
}
void test_index_filemodes__invalid(void)
{
git_index *index;
git_index_entry entry;
const git_index_entry *dummy;
cl_git_pass(git_repository_index(&index, g_repo));
/* add a dummy file so that we have a valid id */
cl_git_mkfile("./filemodes/dummy-file.txt", "new-file\n");
cl_git_pass(git_index_add_bypath(index, "dummy-file.txt"));
cl_assert((dummy = git_index_get_bypath(index, "dummy-file.txt", 0)));
GIT_INDEX_ENTRY_STAGE_SET(&entry, 0);
entry.path = "foo";
entry.mode = GIT_OBJECT_BLOB;
git_oid_cpy(&entry.id, &dummy->id);
cl_git_fail(git_index_add(index, &entry));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add(index, &entry));
git_index_free(index);
}
void test_index_filemodes__frombuffer_requires_files(void)
{
git_index *index;
git_index_entry new_entry;
const git_index_entry *ret_entry;
const char *content = "hey there\n";
memset(&new_entry, 0, sizeof(new_entry));
cl_git_pass(git_repository_index(&index, g_repo));
/* regular blob */
new_entry.path = "dummy-file.txt";
new_entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add_from_buffer(index,
&new_entry, content, strlen(content)));
cl_assert((ret_entry = git_index_get_bypath(index, "dummy-file.txt", 0)));
cl_assert_equal_s("dummy-file.txt", ret_entry->path);
cl_assert_equal_i(GIT_FILEMODE_BLOB, ret_entry->mode);
/* executable blob */
new_entry.path = "dummy-file.txt";
new_entry.mode = GIT_FILEMODE_BLOB_EXECUTABLE;
cl_git_pass(git_index_add_from_buffer(index,
&new_entry, content, strlen(content)));
cl_assert((ret_entry = git_index_get_bypath(index, "dummy-file.txt", 0)));
cl_assert_equal_s("dummy-file.txt", ret_entry->path);
cl_assert_equal_i(GIT_FILEMODE_BLOB_EXECUTABLE, ret_entry->mode);
/* links are also acceptable */
new_entry.path = "dummy-link.txt";
new_entry.mode = GIT_FILEMODE_LINK;
cl_git_pass(git_index_add_from_buffer(index,
&new_entry, content, strlen(content)));
cl_assert((ret_entry = git_index_get_bypath(index, "dummy-link.txt", 0)));
cl_assert_equal_s("dummy-link.txt", ret_entry->path);
cl_assert_equal_i(GIT_FILEMODE_LINK, ret_entry->mode);
/* trees are rejected */
new_entry.path = "invalid_mode.txt";
new_entry.mode = GIT_FILEMODE_TREE;
cl_git_fail(git_index_add_from_buffer(index,
&new_entry, content, strlen(content)));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "invalid_mode.txt", 0));
/* submodules are rejected */
new_entry.path = "invalid_mode.txt";
new_entry.mode = GIT_FILEMODE_COMMIT;
cl_git_fail(git_index_add_from_buffer(index,
&new_entry, content, strlen(content)));
cl_assert_equal_p(NULL, git_index_get_bypath(index, "invalid_mode.txt", 0));
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/filemodes.c |
#include "clar_libgit2.h"
#include "index.h"
#include "git2/sys/index.h"
#include "git2/repository.h"
#include "../reset/reset_helpers.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "nsecs"
/* Fixture setup and teardown */
void test_index_nsec__initialize(void)
{
repo = cl_git_sandbox_init("nsecs");
git_repository_index(&repo_index, repo);
}
void test_index_nsec__cleanup(void)
{
git_index_free(repo_index);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
static bool try_create_file_with_nsec_timestamp(const char *path)
{
struct stat st;
int try;
/* retry a few times to avoid nanos *actually* equal 0 race condition */
for (try = 0; try < 3; try++) {
cl_git_mkfile(path, "This is hopefully a file with nanoseconds!");
cl_must_pass(p_stat(path, &st));
if (st.st_ctime_nsec && st.st_mtime_nsec)
return true;
}
return false;
}
/* try to determine if the underlying filesystem supports a resolution
* higher than a single second. (i'm looking at you, hfs+)
*/
static bool should_expect_nsecs(void)
{
git_str nsec_path = GIT_STR_INIT;
bool expect;
git_str_joinpath(&nsec_path, clar_sandbox_path(), "nsec_test");
expect = try_create_file_with_nsec_timestamp(nsec_path.ptr);
cl_must_pass(p_unlink(nsec_path.ptr));
git_str_dispose(&nsec_path);
return expect;
}
static bool has_nsecs(void)
{
const git_index_entry *entry;
size_t i;
bool has_nsecs = false;
for (i = 0; i < git_index_entrycount(repo_index); i++) {
entry = git_index_get_byindex(repo_index, i);
if (entry->ctime.nanoseconds || entry->mtime.nanoseconds) {
has_nsecs = true;
break;
}
}
return has_nsecs;
}
void test_index_nsec__has_nanos(void)
{
cl_assert_equal_b(true, has_nsecs());
}
void test_index_nsec__staging_maintains_other_nanos(void)
{
const git_index_entry *entry;
bool expect_nsec, test_file_has_nsec;
expect_nsec = should_expect_nsecs();
test_file_has_nsec = try_create_file_with_nsec_timestamp("nsecs/a.txt");
cl_assert_equal_b(expect_nsec, test_file_has_nsec);
cl_git_pass(git_index_add_bypath(repo_index, "a.txt"));
cl_git_pass(git_index_write(repo_index));
cl_git_pass(git_index_write(repo_index));
git_index_read(repo_index, 1);
cl_assert_equal_b(true, has_nsecs());
cl_assert((entry = git_index_get_bypath(repo_index, "a.txt", 0)));
/* if we are writing nanoseconds to the index, expect them to be
* nonzero.
*/
if (expect_nsec) {
cl_assert(entry->ctime.nanoseconds != 0);
cl_assert(entry->mtime.nanoseconds != 0);
} else {
cl_assert_equal_i(0, entry->ctime.nanoseconds);
cl_assert_equal_i(0, entry->mtime.nanoseconds);
}
}
void test_index_nsec__status_doesnt_clear_nsecs(void)
{
git_status_list *statuslist;
cl_git_pass(git_status_list_new(&statuslist, repo, NULL));
git_index_read(repo_index, 1);
cl_assert_equal_b(true, has_nsecs());
git_status_list_free(statuslist);
}
| libgit2-main | tests/libgit2/index/nsec.c |
#include "clar_libgit2.h"
#include "../status/status_helpers.h"
#include "posix.h"
#include "futils.h"
static git_repository *g_repo = NULL;
#define TEST_DIR "addall"
void test_index_addall__initialize(void)
{
}
void test_index_addall__cleanup(void)
{
cl_git_sandbox_cleanup();
}
#define STATUS_INDEX_FLAGS \
(GIT_STATUS_INDEX_NEW | GIT_STATUS_INDEX_MODIFIED | \
GIT_STATUS_INDEX_DELETED | GIT_STATUS_INDEX_RENAMED | \
GIT_STATUS_INDEX_TYPECHANGE)
#define STATUS_WT_FLAGS \
(GIT_STATUS_WT_NEW | GIT_STATUS_WT_MODIFIED | \
GIT_STATUS_WT_DELETED | GIT_STATUS_WT_TYPECHANGE | \
GIT_STATUS_WT_RENAMED)
typedef struct {
size_t index_adds;
size_t index_dels;
size_t index_mods;
size_t wt_adds;
size_t wt_dels;
size_t wt_mods;
size_t ignores;
size_t conflicts;
} index_status_counts;
static int index_status_cb(
const char *path, unsigned int status_flags, void *payload)
{
index_status_counts *vals = payload;
/* cb_status__print(path, status_flags, NULL); */
GIT_UNUSED(path);
if (status_flags & GIT_STATUS_INDEX_NEW)
vals->index_adds++;
if (status_flags & GIT_STATUS_INDEX_MODIFIED)
vals->index_mods++;
if (status_flags & GIT_STATUS_INDEX_DELETED)
vals->index_dels++;
if (status_flags & GIT_STATUS_INDEX_TYPECHANGE)
vals->index_mods++;
if (status_flags & GIT_STATUS_WT_NEW)
vals->wt_adds++;
if (status_flags & GIT_STATUS_WT_MODIFIED)
vals->wt_mods++;
if (status_flags & GIT_STATUS_WT_DELETED)
vals->wt_dels++;
if (status_flags & GIT_STATUS_WT_TYPECHANGE)
vals->wt_mods++;
if (status_flags & GIT_STATUS_IGNORED)
vals->ignores++;
if (status_flags & GIT_STATUS_CONFLICTED)
vals->conflicts++;
return 0;
}
static void check_status_at_line(
git_repository *repo,
size_t index_adds, size_t index_dels, size_t index_mods,
size_t wt_adds, size_t wt_dels, size_t wt_mods, size_t ignores,
size_t conflicts, const char *file, const char *func, int line)
{
index_status_counts vals;
memset(&vals, 0, sizeof(vals));
cl_git_pass(git_status_foreach(repo, index_status_cb, &vals));
clar__assert_equal(
file,func,line,"wrong index adds", 1, "%"PRIuZ, index_adds, vals.index_adds);
clar__assert_equal(
file,func,line,"wrong index dels", 1, "%"PRIuZ, index_dels, vals.index_dels);
clar__assert_equal(
file,func,line,"wrong index mods", 1, "%"PRIuZ, index_mods, vals.index_mods);
clar__assert_equal(
file,func,line,"wrong workdir adds", 1, "%"PRIuZ, wt_adds, vals.wt_adds);
clar__assert_equal(
file,func,line,"wrong workdir dels", 1, "%"PRIuZ, wt_dels, vals.wt_dels);
clar__assert_equal(
file,func,line,"wrong workdir mods", 1, "%"PRIuZ, wt_mods, vals.wt_mods);
clar__assert_equal(
file,func,line,"wrong ignores", 1, "%"PRIuZ, ignores, vals.ignores);
clar__assert_equal(
file,func,line,"wrong conflicts", 1, "%"PRIuZ, conflicts, vals.conflicts);
}
#define check_status(R,IA,ID,IM,WA,WD,WM,IG,C) \
check_status_at_line(R,IA,ID,IM,WA,WD,WM,IG,C,__FILE__,__func__,__LINE__)
static void check_stat_data(git_index *index, const char *path, bool match)
{
const git_index_entry *entry;
struct stat st;
cl_must_pass(p_lstat(path, &st));
/* skip repo base dir name */
while (*path != '/')
++path;
++path;
entry = git_index_get_bypath(index, path, 0);
cl_assert(entry);
if (match) {
cl_assert(st.st_ctime == entry->ctime.seconds);
cl_assert(st.st_mtime == entry->mtime.seconds);
cl_assert(st.st_size == entry->file_size);
cl_assert((uint32_t)st.st_uid == entry->uid);
cl_assert((uint32_t)st.st_gid == entry->gid);
cl_assert_equal_i_fmt(
GIT_MODE_TYPE(st.st_mode), GIT_MODE_TYPE(entry->mode), "%07o");
if (cl_is_chmod_supported())
cl_assert_equal_b(
GIT_PERMS_IS_EXEC(st.st_mode), GIT_PERMS_IS_EXEC(entry->mode));
} else {
/* most things will still match */
cl_assert(st.st_size != entry->file_size);
/* would check mtime, but with second resolution it won't work :( */
}
}
static void addall_create_test_repo(bool check_every_step)
{
g_repo = cl_git_sandbox_init_new(TEST_DIR);
if (check_every_step)
check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
cl_git_mkfile(TEST_DIR "/file.foo", "a file");
if (check_every_step)
check_status(g_repo, 0, 0, 0, 1, 0, 0, 0, 0);
cl_git_mkfile(TEST_DIR "/.gitignore", "*.foo\n");
if (check_every_step)
check_status(g_repo, 0, 0, 0, 1, 0, 0, 1, 0);
cl_git_mkfile(TEST_DIR "/file.bar", "another file");
if (check_every_step)
check_status(g_repo, 0, 0, 0, 2, 0, 0, 1, 0);
}
void test_index_addall__repo_lifecycle(void)
{
int error;
git_index *index;
git_strarray paths = { NULL, 0 };
char *strs[1];
addall_create_test_repo(true);
cl_git_pass(git_repository_index(&index, g_repo));
strs[0] = "file.*";
paths.strings = strs;
paths.count = 1;
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 1, 0, 0, 1, 0, 0, 1, 0);
cl_git_rewritefile(TEST_DIR "/file.bar", "new content for file");
check_stat_data(index, TEST_DIR "/file.bar", false);
check_status(g_repo, 1, 0, 0, 1, 0, 1, 1, 0);
cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
check_status(g_repo, 1, 0, 0, 4, 0, 1, 1, 0);
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.zzz", true);
check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit");
check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
if (cl_repo_get_bool(g_repo, "core.filemode")) {
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
cl_must_pass(p_chmod(TEST_DIR "/file.zzz", 0777));
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_status(g_repo, 0, 0, 1, 3, 0, 0, 1, 0);
/* go back to what we had before */
cl_must_pass(p_chmod(TEST_DIR "/file.zzz", 0666));
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
}
/* attempt to add an ignored file - does nothing */
strs[0] = "file.foo";
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
/* add with check - should generate error */
error = git_index_add_all(
index, &paths, GIT_INDEX_ADD_CHECK_PATHSPEC, NULL, NULL);
cl_assert_equal_i(GIT_EINVALIDSPEC, error);
cl_git_pass(git_index_write(index));
check_status(g_repo, 0, 0, 0, 3, 0, 0, 1, 0);
/* add with force - should allow */
cl_git_pass(git_index_add_all(
index, &paths, GIT_INDEX_ADD_FORCE, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.foo", true);
check_status(g_repo, 1, 0, 0, 3, 0, 0, 0, 0);
/* now it's in the index, so regular add should work */
cl_git_rewritefile(TEST_DIR "/file.foo", "new content for file");
check_stat_data(index, TEST_DIR "/file.foo", false);
check_status(g_repo, 1, 0, 0, 3, 0, 1, 0, 0);
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.foo", true);
check_status(g_repo, 1, 0, 0, 3, 0, 0, 0, 0);
cl_git_pass(git_index_add_bypath(index, "more.zzz"));
check_stat_data(index, TEST_DIR "/more.zzz", true);
check_status(g_repo, 2, 0, 0, 2, 0, 0, 0, 0);
cl_git_rewritefile(TEST_DIR "/file.zzz", "new content for file");
check_status(g_repo, 2, 0, 0, 2, 0, 1, 0, 0);
cl_git_pass(git_index_add_bypath(index, "file.zzz"));
check_stat_data(index, TEST_DIR "/file.zzz", true);
check_status(g_repo, 2, 0, 1, 2, 0, 0, 0, 0);
strs[0] = "*.zzz";
cl_git_pass(git_index_remove_all(index, &paths, NULL, NULL));
check_status(g_repo, 1, 1, 0, 4, 0, 0, 0, 0);
cl_git_pass(git_index_add_bypath(index, "file.zzz"));
check_status(g_repo, 1, 0, 1, 3, 0, 0, 0, 0);
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit");
check_status(g_repo, 0, 0, 0, 3, 0, 0, 0, 0);
cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
check_status(g_repo, 0, 0, 0, 3, 1, 0, 0, 0);
/* update_all should be able to remove entries */
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_status(g_repo, 0, 1, 0, 3, 0, 0, 0, 0);
strs[0] = "*";
cl_git_pass(git_index_add_all(index, &paths, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_status(g_repo, 3, 1, 0, 0, 0, 0, 0, 0);
/* must be able to remove at any position while still updating other files */
cl_must_pass(p_unlink(TEST_DIR "/.gitignore"));
cl_git_rewritefile(TEST_DIR "/file.zzz", "reconstructed file");
cl_git_rewritefile(TEST_DIR "/more.zzz", "altered file reality");
check_status(g_repo, 3, 1, 0, 1, 1, 1, 0, 0);
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_status(g_repo, 2, 1, 0, 1, 0, 0, 0, 0);
/* this behavior actually matches 'git add -u' where "file.zzz" has
* been removed from the index, so when you go to update, even though
* it exists in the HEAD, it is not re-added to the index, leaving it
* as a DELETE when comparing HEAD to index and as an ADD comparing
* index to worktree
*/
git_index_free(index);
}
void test_index_addall__files_in_folders(void)
{
git_index *index;
addall_create_test_repo(true);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 2, 0, 0, 0, 0, 0, 1, 0);
cl_must_pass(p_mkdir(TEST_DIR "/subdir", 0777));
cl_git_mkfile(TEST_DIR "/subdir/file", "hello!\n");
check_status(g_repo, 2, 0, 0, 1, 0, 0, 1, 0);
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_status(g_repo, 3, 0, 0, 0, 0, 0, 1, 0);
git_index_free(index);
}
void test_index_addall__hidden_files(void)
{
#ifdef GIT_WIN32
git_index *index;
addall_create_test_repo(true);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 2, 0, 0, 0, 0, 0, 1, 0);
cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
cl_git_pass(git_win32__set_hidden(TEST_DIR "/file.zzz", true));
cl_git_pass(git_win32__set_hidden(TEST_DIR "/more.zzz", true));
cl_git_pass(git_win32__set_hidden(TEST_DIR "/other.zzz", true));
check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 5, 0, 0, 0, 0, 0, 1, 0);
git_index_free(index);
#endif
}
static int addall_match_prefix(
const char *path, const char *matched_pathspec, void *payload)
{
GIT_UNUSED(matched_pathspec);
return !git__prefixcmp(path, payload) ? 0 : 1;
}
static int addall_match_suffix(
const char *path, const char *matched_pathspec, void *payload)
{
GIT_UNUSED(matched_pathspec);
return !git__suffixcmp(path, payload) ? 0 : 1;
}
static int addall_cancel_at(
const char *path, const char *matched_pathspec, void *payload)
{
GIT_UNUSED(matched_pathspec);
return !strcmp(path, payload) ? -123 : 0;
}
void test_index_addall__callback_filtering(void)
{
git_index *index;
addall_create_test_repo(false);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_prefix, "file."));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 1, 0, 0, 1, 0, 0, 1, 0);
cl_git_mkfile(TEST_DIR "/file.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/more.zzz", "yet another one");
cl_git_mkfile(TEST_DIR "/other.zzz", "yet another one");
cl_git_pass(git_index_update_all(index, NULL, NULL, NULL));
check_stat_data(index, TEST_DIR "/file.bar", true);
check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_prefix, "other"));
cl_git_pass(git_index_write(index));
check_stat_data(index, TEST_DIR "/other.zzz", true);
check_status(g_repo, 2, 0, 0, 3, 0, 0, 1, 0);
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
cl_git_pass(git_index_write(index));
check_status(g_repo, 4, 0, 0, 1, 0, 0, 1, 0);
cl_git_pass(
git_index_remove_all(index, NULL, addall_match_suffix, ".zzz"));
check_status(g_repo, 1, 0, 0, 4, 0, 0, 1, 0);
cl_git_fail_with(
git_index_add_all(index, NULL, 0, addall_cancel_at, "more.zzz"), -123);
check_status(g_repo, 3, 0, 0, 2, 0, 0, 1, 0);
cl_git_fail_with(
git_index_add_all(index, NULL, 0, addall_cancel_at, "other.zzz"), -123);
check_status(g_repo, 4, 0, 0, 1, 0, 0, 1, 0);
cl_git_pass(
git_index_add_all(index, NULL, 0, addall_match_suffix, ".zzz"));
cl_git_pass(git_index_write(index));
check_status(g_repo, 5, 0, 0, 0, 0, 0, 1, 0);
cl_must_pass(p_unlink(TEST_DIR "/file.zzz"));
cl_must_pass(p_unlink(TEST_DIR "/more.zzz"));
cl_must_pass(p_unlink(TEST_DIR "/other.zzz"));
cl_git_fail_with(
git_index_update_all(index, NULL, addall_cancel_at, "more.zzz"), -123);
/* file.zzz removed from index (so Index Adds 5 -> 4) and
* more.zzz + other.zzz removed (so Worktree Dels 0 -> 2) */
check_status(g_repo, 4, 0, 0, 0, 2, 0, 1, 0);
cl_git_fail_with(
git_index_update_all(index, NULL, addall_cancel_at, "other.zzz"), -123);
/* more.zzz removed from index (so Index Adds 4 -> 3) and
* Just other.zzz removed (so Worktree Dels 2 -> 1) */
check_status(g_repo, 3, 0, 0, 0, 1, 0, 1, 0);
git_index_free(index);
}
void test_index_addall__adds_conflicts(void)
{
git_index *index;
git_reference *ref;
git_annotated_commit *annotated;
g_repo = cl_git_sandbox_init("merge-resolve");
cl_git_pass(git_repository_index(&index, g_repo));
check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));
cl_git_pass(git_merge(g_repo, (const git_annotated_commit**)&annotated, 1, NULL, NULL));
check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_status(g_repo, 0, 1, 3, 0, 0, 0, 0, 0);
git_annotated_commit_free(annotated);
git_reference_free(ref);
git_index_free(index);
}
void test_index_addall__removes_deleted_conflicted_files(void)
{
git_index *index;
git_reference *ref;
git_annotated_commit *annotated;
g_repo = cl_git_sandbox_init("merge-resolve");
cl_git_pass(git_repository_index(&index, g_repo));
check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));
cl_git_pass(git_merge(g_repo, (const git_annotated_commit**)&annotated, 1, NULL, NULL));
check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);
cl_git_rmfile("merge-resolve/conflicting.txt");
cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
cl_git_pass(git_index_write(index));
check_status(g_repo, 0, 2, 2, 0, 0, 0, 0, 0);
git_annotated_commit_free(annotated);
git_reference_free(ref);
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/addall.c |
#include "clar_libgit2.h"
#include "index.h"
static const size_t index_entry_count = 109;
static const size_t index_entry_count_2 = 1437;
#define TEST_INDEX_PATH cl_fixture("testrepo.git/index")
#define TEST_INDEX2_PATH cl_fixture("gitgit.index")
#define TEST_INDEXBIG_PATH cl_fixture("big.index")
#define TEST_INDEXBAD_PATH cl_fixture("bad.index")
/* Suite data */
struct test_entry {
size_t index;
char path[128];
off64_t file_size;
git_time_t mtime;
};
static struct test_entry test_entries[] = {
{4, "Makefile", 5064, 0x4C3F7F33},
{6, "git.git-authors", 2709, 0x4C3F7F33},
{36, "src/index.c", 10014, 0x4C43368D},
{48, "src/revobject.h", 1448, 0x4C3F7FE2},
{62, "tests/Makefile", 2631, 0x4C3F7F33}
};
/* Helpers */
static void copy_file(const char *src, const char *dst)
{
git_str source_buf = GIT_STR_INIT;
git_file dst_fd;
cl_git_pass(git_futils_readbuffer(&source_buf, src));
dst_fd = git_futils_creat_withpath(dst, 0777, 0666); /* -V536 */
if (dst_fd < 0)
goto cleanup;
cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size));
cleanup:
git_str_dispose(&source_buf);
p_close(dst_fd);
}
static void files_are_equal(const char *a, const char *b)
{
git_str buf_a = GIT_STR_INIT;
git_str buf_b = GIT_STR_INIT;
int pass;
if (git_futils_readbuffer(&buf_a, a) < 0)
cl_assert(0);
if (git_futils_readbuffer(&buf_b, b) < 0) {
git_str_dispose(&buf_a);
cl_assert(0);
}
pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size));
git_str_dispose(&buf_a);
git_str_dispose(&buf_b);
cl_assert(pass);
}
/* Fixture setup and teardown */
void test_index_tests__initialize(void)
{
}
void test_index_tests__cleanup(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, 0));
}
void test_index_tests__empty_index(void)
{
git_index *index;
cl_git_pass(git_index_open(&index, "in-memory-index"));
cl_assert(index->on_disk == 0);
cl_assert(git_index_entrycount(index) == 0);
cl_assert(git_vector_is_sorted(&index->entries));
git_index_free(index);
}
void test_index_tests__default_test_index(void)
{
git_index *index;
unsigned int i;
git_index_entry **entries;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_assert(index->on_disk);
cl_assert(git_index_entrycount(index) == index_entry_count);
cl_assert(git_vector_is_sorted(&index->entries));
entries = (git_index_entry **)index->entries.contents;
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
git_index_entry *e = entries[test_entries[i].index];
cl_assert_equal_s(e->path, test_entries[i].path);
cl_assert_equal_i(e->mtime.seconds, test_entries[i].mtime);
cl_assert_equal_i(e->file_size, test_entries[i].file_size);
}
git_index_free(index);
}
void test_index_tests__gitgit_index(void)
{
git_index *index;
cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH));
cl_assert(index->on_disk);
cl_assert(git_index_entrycount(index) == index_entry_count_2);
cl_assert(git_vector_is_sorted(&index->entries));
cl_assert(index->tree != NULL);
git_index_free(index);
}
void test_index_tests__find_in_existing(void)
{
git_index *index;
unsigned int i;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
size_t idx;
cl_assert(!git_index_find(&idx, index, test_entries[i].path));
cl_assert(idx == test_entries[i].index);
}
git_index_free(index);
}
void test_index_tests__find_in_empty(void)
{
git_index *index;
unsigned int i;
cl_git_pass(git_index_open(&index, "fake-index"));
for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path));
}
git_index_free(index);
}
void test_index_tests__find_prefix(void)
{
git_index *index;
const git_index_entry *entry;
size_t pos;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_git_pass(git_index_find_prefix(&pos, index, "src"));
entry = git_index_get_byindex(index, pos);
cl_assert(git__strcmp(entry->path, "src/block-sha1/sha1.c") == 0);
cl_git_pass(git_index_find_prefix(&pos, index, "src/co"));
entry = git_index_get_byindex(index, pos);
cl_assert(git__strcmp(entry->path, "src/commit.c") == 0);
cl_assert(GIT_ENOTFOUND == git_index_find_prefix(NULL, index, "blah"));
git_index_free(index);
}
void test_index_tests__write(void)
{
git_index *index;
copy_file(TEST_INDEXBIG_PATH, "index_rewrite");
cl_git_pass(git_index_open(&index, "index_rewrite"));
cl_assert(index->on_disk);
cl_git_pass(git_index_write(index));
files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite");
git_index_free(index);
p_unlink("index_rewrite");
}
void test_index_tests__sort0(void)
{
/* sort the entries in an index */
/*
* TODO: This no longer applies:
* index sorting in Git uses some specific changes to the way
* directories are sorted.
*
* We need to specifically check for this by creating a new
* index, adding entries in random order and then
* checking for consistency
*/
}
void test_index_tests__sort1(void)
{
/* sort the entries in an empty index */
git_index *index;
cl_git_pass(git_index_open(&index, "fake-index"));
/* FIXME: this test is slightly dumb */
cl_assert(git_vector_is_sorted(&index->entries));
git_index_free(index);
}
static void cleanup_myrepo(void *opaque)
{
GIT_UNUSED(opaque);
cl_fixture_cleanup("myrepo");
}
void test_index_tests__add(void)
{
git_index *index;
git_filebuf file = GIT_FILEBUF_INIT;
git_repository *repo;
const git_index_entry *entry;
git_oid id1;
cl_set_cleanup(&cleanup_myrepo, NULL);
/* Initialize a new repository */
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
/* Ensure we're the only guy in the room */
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
/* Create a new file in the working directory */
cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0, 0666));
cl_git_pass(git_filebuf_write(&file, "hey there\n", 10));
cl_git_pass(git_filebuf_commit(&file));
/* Store the expected hash of the file/blob
* This has been generated by executing the following
* $ echo "hey there" | git hash-object --stdin
*/
cl_git_pass(git_oid__fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", GIT_OID_SHA1));
/* Add the new file to the index */
cl_git_pass(git_index_add_bypath(index, "test.txt"));
/* Wow... it worked! */
cl_assert(git_index_entrycount(index) == 1);
entry = git_index_get_byindex(index, 0);
/* And the built-in hashing mechanism worked as expected */
cl_assert_equal_oid(&id1, &entry->id);
/* Test access by path instead of index */
cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
cl_assert_equal_oid(&id1, &entry->id);
git_index_free(index);
git_repository_free(repo);
}
void test_index_tests__add_frombuffer(void)
{
git_index *index;
git_repository *repo;
git_index_entry entry;
const git_index_entry *returned_entry;
git_oid id1;
git_blob *blob;
const char *content = "hey there\n";
cl_set_cleanup(&cleanup_myrepo, NULL);
/* Initialize a new repository */
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
/* Ensure we're the only guy in the room */
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
/* Store the expected hash of the file/blob
* This has been generated by executing the following
* $ echo "hey there" | git hash-object --stdin
*/
cl_git_pass(git_oid__fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", GIT_OID_SHA1));
/* Add the new file to the index */
memset(&entry, 0x0, sizeof(git_index_entry));
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "test.txt";
cl_git_pass(git_index_add_from_buffer(index, &entry,
content, strlen(content)));
/* Wow... it worked! */
cl_assert(git_index_entrycount(index) == 1);
returned_entry = git_index_get_byindex(index, 0);
/* And the built-in hashing mechanism worked as expected */
cl_assert_equal_oid(&id1, &returned_entry->id);
/* And mode is the one asked */
cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);
/* Test access by path instead of index */
cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
cl_assert_equal_oid(&id1, &returned_entry->id);
/* Test the blob is in the repository */
cl_git_pass(git_blob_lookup(&blob, repo, &id1));
cl_assert_equal_s(
content, git_blob_rawcontent(blob));
git_blob_free(blob);
git_index_free(index);
git_repository_free(repo);
}
void test_index_tests__dirty_and_clean(void)
{
git_repository *repo;
git_index *index;
git_index_entry entry = {{0}};
/* Index is not dirty after opening */
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
cl_assert(!git_index_is_dirty(index));
/* Index is dirty after adding an entry */
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "test.txt";
cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
cl_assert(git_index_entrycount(index) == 1);
cl_assert(git_index_is_dirty(index));
/* Index is not dirty after write */
cl_git_pass(git_index_write(index));
cl_assert(!git_index_is_dirty(index));
/* Index is dirty after removing an entry */
cl_git_pass(git_index_remove_bypath(index, "test.txt"));
cl_assert(git_index_entrycount(index) == 0);
cl_assert(git_index_is_dirty(index));
/* Index is not dirty after write */
cl_git_pass(git_index_write(index));
cl_assert(!git_index_is_dirty(index));
/* Index remains not dirty after read */
cl_git_pass(git_index_read(index, 0));
cl_assert(!git_index_is_dirty(index));
/* Index is dirty when we do an unforced read with dirty content */
cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
cl_assert(git_index_entrycount(index) == 1);
cl_assert(git_index_is_dirty(index));
cl_git_pass(git_index_read(index, 0));
cl_assert(git_index_is_dirty(index));
/* Index is clean when we force a read with dirty content */
cl_git_pass(git_index_read(index, 1));
cl_assert(!git_index_is_dirty(index));
git_index_free(index);
git_repository_free(repo);
}
void test_index_tests__dirty_fails_optionally(void)
{
git_repository *repo;
git_index *index;
git_index_entry entry = {{0}};
/* Index is not dirty after opening */
repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_index(&index, repo));
/* Index is dirty after adding an entry */
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "test.txt";
cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
cl_assert(git_index_is_dirty(index));
cl_git_pass(git_checkout_head(repo, NULL));
/* Index is dirty (again) after adding an entry */
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "test.txt";
cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
cl_assert(git_index_is_dirty(index));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, 1));
cl_git_fail_with(GIT_EINDEXDIRTY, git_checkout_head(repo, NULL));
git_index_free(index);
cl_git_sandbox_cleanup();
}
void test_index_tests__add_frombuffer_reset_entry(void)
{
git_index *index;
git_repository *repo;
git_index_entry entry;
const git_index_entry *returned_entry;
git_filebuf file = GIT_FILEBUF_INIT;
git_oid id1;
git_blob *blob;
const char *old_content = "here\n";
const char *content = "hey there\n";
cl_set_cleanup(&cleanup_myrepo, NULL);
/* Initialize a new repository */
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0, 0666));
cl_git_pass(git_filebuf_write(&file, old_content, strlen(old_content)));
cl_git_pass(git_filebuf_commit(&file));
/* Store the expected hash of the file/blob
* This has been generated by executing the following
* $ echo "hey there" | git hash-object --stdin
*/
cl_git_pass(git_oid__fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", GIT_OID_SHA1));
cl_git_pass(git_index_add_bypath(index, "test.txt"));
/* Add the new file to the index */
memset(&entry, 0x0, sizeof(git_index_entry));
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "test.txt";
cl_git_pass(git_index_add_from_buffer(index, &entry,
content, strlen(content)));
/* Wow... it worked! */
cl_assert(git_index_entrycount(index) == 1);
returned_entry = git_index_get_byindex(index, 0);
/* And the built-in hashing mechanism worked as expected */
cl_assert_equal_oid(&id1, &returned_entry->id);
/* And mode is the one asked */
cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);
/* Test access by path instead of index */
cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
cl_assert_equal_oid(&id1, &returned_entry->id);
cl_assert_equal_i(0, returned_entry->dev);
cl_assert_equal_i(0, returned_entry->ino);
cl_assert_equal_i(0, returned_entry->uid);
cl_assert_equal_i(0, returned_entry->uid);
cl_assert_equal_i(10, returned_entry->file_size);
/* Test the blob is in the repository */
cl_git_pass(git_blob_lookup(&blob, repo, &id1));
cl_assert_equal_s(content, git_blob_rawcontent(blob));
git_blob_free(blob);
git_index_free(index);
git_repository_free(repo);
}
static void cleanup_1397(void *opaque)
{
GIT_UNUSED(opaque);
cl_git_sandbox_cleanup();
}
void test_index_tests__add_issue_1397(void)
{
git_index *index;
git_repository *repo;
const git_index_entry *entry;
git_oid id1;
cl_set_cleanup(&cleanup_1397, NULL);
repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(repo, "core.autocrlf", true);
/* Ensure we're the only guy in the room */
cl_git_pass(git_repository_index(&index, repo));
/* Store the expected hash of the file/blob
* This has been generated by executing the following
* $ git hash-object crlf_file.txt
*/
cl_git_pass(git_oid__fromstr(&id1, "8312e0889a9cbab77c732b6bc39b51a683e3a318", GIT_OID_SHA1));
/* Make sure the initial SHA-1 is correct */
cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL);
cl_assert_equal_oid(&id1, &entry->id);
/* Update the index */
cl_git_pass(git_index_add_bypath(index, "crlf_file.txt"));
/* Check the new SHA-1 */
cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL);
cl_assert_equal_oid(&id1, &entry->id);
git_index_free(index);
}
void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void)
{
git_repository *bare_repo;
git_index *index;
cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_index(&index, bare_repo));
cl_assert_equal_i(GIT_EBAREREPO, git_index_add_bypath(index, "test.txt"));
git_index_free(index);
git_repository_free(bare_repo);
}
static void assert_add_bypath_fails(git_repository *repo, const char *fn)
{
git_index *index;
git_str path = GIT_STR_INIT;
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
git_str_joinpath(&path, "./invalid", fn);
cl_git_mkfile(path.ptr, NULL);
cl_git_fail(git_index_add_bypath(index, fn));
cl_must_pass(p_unlink(path.ptr));
cl_assert(git_index_entrycount(index) == 0);
git_str_dispose(&path);
git_index_free(index);
}
/* Test that writing an invalid filename fails */
void test_index_tests__cannot_add_invalid_filename(void)
{
git_repository *repo;
cl_must_pass(p_mkdir("invalid", 0700));
cl_git_pass(git_repository_init(&repo, "./invalid", 0));
cl_must_pass(p_mkdir("./invalid/subdir", 0777));
/* cl_git_mkfile() needs the dir to exist */
if (!git_fs_path_exists("./invalid/.GIT"))
cl_must_pass(p_mkdir("./invalid/.GIT", 0777));
if (!git_fs_path_exists("./invalid/.GiT"))
cl_must_pass(p_mkdir("./invalid/.GiT", 0777));
assert_add_bypath_fails(repo, ".git/hello");
assert_add_bypath_fails(repo, ".GIT/hello");
assert_add_bypath_fails(repo, ".GiT/hello");
assert_add_bypath_fails(repo, "./.git/hello");
assert_add_bypath_fails(repo, "./foo");
assert_add_bypath_fails(repo, "./bar");
assert_add_bypath_fails(repo, "subdir/../bar");
git_repository_free(repo);
cl_fixture_cleanup("invalid");
}
static void assert_add_fails(git_repository *repo, const char *fn)
{
git_index *index;
git_str path = GIT_STR_INIT;
git_index_entry entry = {{0}};
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
entry.path = fn;
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&entry.id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", GIT_OID_SHA1));
cl_git_fail(git_index_add(index, &entry));
cl_assert(git_index_entrycount(index) == 0);
git_str_dispose(&path);
git_index_free(index);
}
/*
* Test that writing an invalid filename fails on filesystem
* specific protected names
*/
void test_index_tests__cannot_add_protected_invalid_filename(void)
{
git_repository *repo;
git_index *index;
cl_must_pass(p_mkdir("invalid", 0700));
cl_git_pass(git_repository_init(&repo, "./invalid", 0));
/* add a file to the repository so we can reference it later */
cl_git_pass(git_repository_index(&index, repo));
cl_git_mkfile("invalid/dummy.txt", "");
cl_git_pass(git_index_add_bypath(index, "dummy.txt"));
cl_must_pass(p_unlink("invalid/dummy.txt"));
cl_git_pass(git_index_remove_bypath(index, "dummy.txt"));
git_index_free(index);
cl_repo_set_bool(repo, "core.protectHFS", true);
cl_repo_set_bool(repo, "core.protectNTFS", true);
assert_add_fails(repo, ".git./hello");
assert_add_fails(repo, ".git\xe2\x80\xad/hello");
assert_add_fails(repo, "git~1/hello");
assert_add_fails(repo, ".git\xe2\x81\xaf/hello");
assert_add_fails(repo, ".git::$INDEX_ALLOCATION/dummy-file");
git_repository_free(repo);
cl_fixture_cleanup("invalid");
}
static void replace_char(char *str, char in, char out)
{
char *c = str;
while (*c++)
if (*c == in)
*c = out;
}
static void assert_write_fails(git_repository *repo, const char *fn_orig)
{
git_index *index;
git_oid expected;
const git_index_entry *entry;
git_str path = GIT_STR_INIT;
char *fn;
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
/*
* Sneak a valid path into the index, we'll update it
* to an invalid path when we try to write the index.
*/
fn = git__strdup(fn_orig);
replace_char(fn, '/', '_');
replace_char(fn, ':', '!');
git_str_joinpath(&path, "./invalid", fn);
cl_git_mkfile(path.ptr, NULL);
cl_git_pass(git_index_add_bypath(index, fn));
cl_assert(entry = git_index_get_bypath(index, fn, 0));
/* kids, don't try this at home */
replace_char((char *)entry->path, '_', '/');
replace_char((char *)entry->path, '!', ':');
/* write-tree */
cl_git_fail(git_index_write_tree(&expected, index));
p_unlink(path.ptr);
cl_git_pass(git_index_remove_all(index, NULL, NULL, NULL));
git_str_dispose(&path);
git_index_free(index);
git__free(fn);
}
void test_index_tests__write_tree_invalid_unowned_index(void)
{
git_index *idx;
git_repository *repo;
git_index_entry entry = {{0}};
git_oid tree_id;
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_oid__fromstr(&entry.id, "8312e0a89a9cbab77c732b6bc39b51a783e3a318", GIT_OID_SHA1));
entry.path = "foo";
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add(idx, &entry));
cl_git_pass(git_repository_init(&repo, "./invalid-id", 0));
cl_git_fail(git_index_write_tree_to(&tree_id, idx, repo));
git_index_free(idx);
git_repository_free(repo);
cl_fixture_cleanup("invalid-id");
}
/* Test that writing an invalid filename fails */
void test_index_tests__write_invalid_filename(void)
{
git_repository *repo;
p_mkdir("invalid", 0700);
cl_git_pass(git_repository_init(&repo, "./invalid", 0));
assert_write_fails(repo, ".git/hello");
assert_write_fails(repo, ".GIT/hello");
assert_write_fails(repo, ".GiT/hello");
assert_write_fails(repo, "./.git/hello");
assert_write_fails(repo, "./foo");
assert_write_fails(repo, "./bar");
assert_write_fails(repo, "foo/../bar");
git_repository_free(repo);
cl_fixture_cleanup("invalid");
}
void test_index_tests__honors_protect_filesystems(void)
{
git_repository *repo;
p_mkdir("invalid", 0700);
cl_git_pass(git_repository_init(&repo, "./invalid", 0));
cl_repo_set_bool(repo, "core.protectHFS", true);
cl_repo_set_bool(repo, "core.protectNTFS", true);
assert_write_fails(repo, ".git./hello");
assert_write_fails(repo, ".git\xe2\x80\xad/hello");
assert_write_fails(repo, "git~1/hello");
assert_write_fails(repo, ".git\xe2\x81\xaf/hello");
assert_write_fails(repo, ".git::$INDEX_ALLOCATION/dummy-file");
git_repository_free(repo);
cl_fixture_cleanup("invalid");
}
void test_index_tests__protectntfs_on_by_default(void)
{
git_repository *repo;
p_mkdir("invalid", 0700);
cl_git_pass(git_repository_init(&repo, "./invalid", 0));
assert_write_fails(repo, ".git./hello");
assert_write_fails(repo, "git~1/hello");
git_repository_free(repo);
cl_fixture_cleanup("invalid");
}
void test_index_tests__can_disable_protectntfs(void)
{
git_repository *repo;
git_index *index;
cl_must_pass(p_mkdir("valid", 0700));
cl_git_rewritefile("valid/git~1", "steal the shortname");
cl_git_pass(git_repository_init(&repo, "./valid", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_repo_set_bool(repo, "core.protectNTFS", false);
cl_git_pass(git_index_add_bypath(index, "git~1"));
git_index_free(index);
git_repository_free(repo);
cl_fixture_cleanup("valid");
}
void test_index_tests__remove_entry(void)
{
git_repository *repo;
git_index *index;
p_mkdir("index_test", 0770);
cl_git_pass(git_repository_init(&repo, "index_test", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_entrycount(index) == 0);
cl_git_mkfile("index_test/hello", NULL);
cl_git_pass(git_index_add_bypath(index, "hello"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true)); /* reload */
cl_assert(git_index_entrycount(index) == 1);
cl_assert(git_index_get_bypath(index, "hello", 0) != NULL);
cl_git_pass(git_index_remove(index, "hello", 0));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true)); /* reload */
cl_assert(git_index_entrycount(index) == 0);
cl_assert(git_index_get_bypath(index, "hello", 0) == NULL);
git_index_free(index);
git_repository_free(repo);
cl_fixture_cleanup("index_test");
}
void test_index_tests__remove_directory(void)
{
git_repository *repo;
git_index *index;
p_mkdir("index_test", 0770);
cl_git_pass(git_repository_init(&repo, "index_test", 0));
cl_git_pass(git_repository_index(&index, repo));
cl_assert_equal_i(0, (int)git_index_entrycount(index));
p_mkdir("index_test/a", 0770);
cl_git_mkfile("index_test/a/1.txt", NULL);
cl_git_mkfile("index_test/a/2.txt", NULL);
cl_git_mkfile("index_test/a/3.txt", NULL);
cl_git_mkfile("index_test/b.txt", NULL);
cl_git_pass(git_index_add_bypath(index, "a/1.txt"));
cl_git_pass(git_index_add_bypath(index, "a/2.txt"));
cl_git_pass(git_index_add_bypath(index, "a/3.txt"));
cl_git_pass(git_index_add_bypath(index, "b.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true)); /* reload */
cl_assert_equal_i(4, (int)git_index_entrycount(index));
cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL);
cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);
cl_git_pass(git_index_remove(index, "a/1.txt", 0));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true)); /* reload */
cl_assert_equal_i(3, (int)git_index_entrycount(index));
cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);
cl_git_pass(git_index_remove_directory(index, "a", 0));
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true)); /* reload */
cl_assert_equal_i(1, (int)git_index_entrycount(index));
cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL);
cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);
git_index_free(index);
git_repository_free(repo);
cl_fixture_cleanup("index_test");
}
void test_index_tests__preserves_case(void)
{
git_repository *repo;
git_index *index;
const git_index_entry *entry;
int index_caps;
cl_set_cleanup(&cleanup_myrepo, NULL);
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
cl_git_pass(git_repository_index(&index, repo));
index_caps = git_index_caps(index);
cl_git_rewritefile("myrepo/test.txt", "hey there\n");
cl_git_pass(git_index_add_bypath(index, "test.txt"));
cl_git_pass(p_rename("myrepo/test.txt", "myrepo/TEST.txt"));
cl_git_rewritefile("myrepo/TEST.txt", "hello again\n");
cl_git_pass(git_index_add_bypath(index, "TEST.txt"));
if (index_caps & GIT_INDEX_CAPABILITY_IGNORE_CASE)
cl_assert_equal_i(1, (int)git_index_entrycount(index));
else
cl_assert_equal_i(2, (int)git_index_entrycount(index));
/* Test access by path instead of index */
cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
/* The path should *not* have changed without an explicit remove */
cl_assert(git__strcmp(entry->path, "test.txt") == 0);
cl_assert((entry = git_index_get_bypath(index, "TEST.txt", 0)) != NULL);
if (index_caps & GIT_INDEX_CAPABILITY_IGNORE_CASE)
/* The path should *not* have changed without an explicit remove */
cl_assert(git__strcmp(entry->path, "test.txt") == 0);
else
cl_assert(git__strcmp(entry->path, "TEST.txt") == 0);
git_index_free(index);
git_repository_free(repo);
}
void test_index_tests__elocked(void)
{
git_repository *repo;
git_index *index;
git_filebuf file = GIT_FILEBUF_INIT;
const git_error *err;
int error;
cl_set_cleanup(&cleanup_myrepo, NULL);
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
cl_git_pass(git_repository_index(&index, repo));
/* Lock the index file so we fail to lock it */
cl_git_pass(git_filebuf_open(&file, index->index_file_path, 0, 0666));
error = git_index_write(index);
cl_assert_equal_i(GIT_ELOCKED, error);
err = git_error_last();
cl_assert_equal_i(err->klass, GIT_ERROR_INDEX);
git_filebuf_cleanup(&file);
git_index_free(index);
git_repository_free(repo);
}
void test_index_tests__reload_from_disk(void)
{
git_repository *repo;
git_index *read_index;
git_index *write_index;
cl_set_cleanup(&cleanup_myrepo, NULL);
cl_git_pass(git_futils_mkdir("./myrepo", 0777, GIT_MKDIR_PATH));
cl_git_mkfile("./myrepo/a.txt", "a\n");
cl_git_mkfile("./myrepo/b.txt", "b\n");
cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
cl_git_pass(git_repository_index(&write_index, repo));
cl_assert_equal_i(false, write_index->on_disk);
cl_git_pass(git_index_open(&read_index, write_index->index_file_path));
cl_assert_equal_i(false, read_index->on_disk);
/* Stage two new files against the write_index */
cl_git_pass(git_index_add_bypath(write_index, "a.txt"));
cl_git_pass(git_index_add_bypath(write_index, "b.txt"));
cl_assert_equal_sz(2, git_index_entrycount(write_index));
/* Persist the index changes to disk */
cl_git_pass(git_index_write(write_index));
cl_assert_equal_i(true, write_index->on_disk);
/* Sync the changes back into the read_index */
cl_assert_equal_sz(0, git_index_entrycount(read_index));
cl_git_pass(git_index_read(read_index, true));
cl_assert_equal_i(true, read_index->on_disk);
cl_assert_equal_sz(2, git_index_entrycount(read_index));
/* Remove the index file from the filesystem */
cl_git_pass(p_unlink(write_index->index_file_path));
/* Sync the changes back into the read_index */
cl_git_pass(git_index_read(read_index, true));
cl_assert_equal_i(false, read_index->on_disk);
cl_assert_equal_sz(0, git_index_entrycount(read_index));
git_index_free(read_index);
git_index_free(write_index);
git_repository_free(repo);
}
void test_index_tests__corrupted_extension(void)
{
git_index *index;
cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR);
}
void test_index_tests__reload_while_ignoring_case(void)
{
git_index *index;
unsigned int caps;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_git_pass(git_vector_verify_sorted(&index->entries));
caps = git_index_caps(index);
cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEX_CAPABILITY_IGNORE_CASE));
cl_git_pass(git_index_read(index, true));
cl_git_pass(git_vector_verify_sorted(&index->entries));
cl_assert(git_index_get_bypath(index, ".HEADER", 0));
cl_assert_equal_p(NULL, git_index_get_bypath(index, ".header", 0));
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEX_CAPABILITY_IGNORE_CASE));
cl_git_pass(git_index_read(index, true));
cl_git_pass(git_vector_verify_sorted(&index->entries));
cl_assert(git_index_get_bypath(index, ".HEADER", 0));
cl_assert(git_index_get_bypath(index, ".header", 0));
git_index_free(index);
}
void test_index_tests__change_icase_on_instance(void)
{
git_index *index;
unsigned int caps;
const git_index_entry *e;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_git_pass(git_vector_verify_sorted(&index->entries));
caps = git_index_caps(index);
cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEX_CAPABILITY_IGNORE_CASE));
cl_assert_equal_i(false, index->ignore_case);
cl_git_pass(git_vector_verify_sorted(&index->entries));
cl_assert(e = git_index_get_bypath(index, "src/common.h", 0));
cl_assert_equal_p(NULL, e = git_index_get_bypath(index, "SRC/Common.h", 0));
cl_assert(e = git_index_get_bypath(index, "COPYING", 0));
cl_assert_equal_p(NULL, e = git_index_get_bypath(index, "copying", 0));
cl_git_pass(git_index_set_caps(index, caps | GIT_INDEX_CAPABILITY_IGNORE_CASE));
cl_assert_equal_i(true, index->ignore_case);
cl_git_pass(git_vector_verify_sorted(&index->entries));
cl_assert(e = git_index_get_bypath(index, "COPYING", 0));
cl_assert_equal_s("COPYING", e->path);
cl_assert(e = git_index_get_bypath(index, "copying", 0));
cl_assert_equal_s("COPYING", e->path);
git_index_free(index);
}
void test_index_tests__can_lock_index(void)
{
git_repository *repo;
git_index *index;
git_indexwriter one = GIT_INDEXWRITER_INIT,
two = GIT_INDEXWRITER_INIT;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_indexwriter_init(&one, index));
cl_git_fail_with(GIT_ELOCKED, git_indexwriter_init(&two, index));
cl_git_fail_with(GIT_ELOCKED, git_index_write(index));
cl_git_pass(git_indexwriter_commit(&one));
cl_git_pass(git_index_write(index));
git_indexwriter_cleanup(&one);
git_indexwriter_cleanup(&two);
git_index_free(index);
cl_git_sandbox_cleanup();
}
void test_index_tests__can_iterate(void)
{
git_index *index;
git_index_iterator *iterator;
const git_index_entry *entry;
size_t i, iterator_idx = 0, found = 0;
int ret;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_git_pass(git_index_iterator_new(&iterator, index));
cl_assert(git_vector_is_sorted(&iterator->snap));
for (i = 0; i < ARRAY_SIZE(test_entries); i++) {
/* Advance iterator to next test entry index */
do {
ret = git_index_iterator_next(&entry, iterator);
if (ret == GIT_ITEROVER)
cl_fail("iterator did not contain all test entries");
cl_git_pass(ret);
} while (iterator_idx++ < test_entries[i].index);
cl_assert_equal_s(entry->path, test_entries[i].path);
cl_assert_equal_i(entry->mtime.seconds, test_entries[i].mtime);
cl_assert_equal_i(entry->file_size, test_entries[i].file_size);
found++;
}
while ((ret = git_index_iterator_next(&entry, iterator)) == 0)
;
if (ret != GIT_ITEROVER)
cl_git_fail(ret);
cl_assert_equal_i(found, ARRAY_SIZE(test_entries));
git_index_iterator_free(iterator);
git_index_free(index);
}
void test_index_tests__can_modify_while_iterating(void)
{
git_index *index;
git_index_iterator *iterator;
const git_index_entry *entry;
git_index_entry new_entry = {{0}};
size_t expected = 0, seen = 0;
int ret;
cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
cl_git_pass(git_index_iterator_new(&iterator, index));
expected = git_index_entrycount(index);
cl_assert(git_vector_is_sorted(&iterator->snap));
/*
* After we've counted the entries, add a new one and change another;
* ensure that our iterator is backed by a snapshot and thus returns
* the number of entries from when the iterator was created.
*/
cl_git_pass(git_oid__fromstr(&new_entry.id, "8312e0a89a9cbab77c732b6bc39b51a783e3a318", GIT_OID_SHA1));
new_entry.path = "newfile";
new_entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add(index, &new_entry));
cl_git_pass(git_oid__fromstr(&new_entry.id, "4141414141414141414141414141414141414141", GIT_OID_SHA1));
new_entry.path = "Makefile";
new_entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add(index, &new_entry));
while (true) {
ret = git_index_iterator_next(&entry, iterator);
if (ret == GIT_ITEROVER)
break;
seen++;
}
cl_assert_equal_i(expected, seen);
git_index_iterator_free(iterator);
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/tests.c |
#include "clar_libgit2.h"
#include "index.h"
#include "git2/sys/index.h"
#include "git2/repository.h"
#include "../reset/reset_helpers.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
#define ONE_ANCESTOR_OID "478871385b9cd03908c5383acfd568bef023c6b3"
#define ONE_OUR_OID "4458b8bc9e72b6c8755ae456f60e9844d0538d8c"
#define ONE_THEIR_OID "8b72416545c7e761b64cecad4f1686eae4078aa8"
#define TWO_ANCESTOR_OID "9d81f82fccc7dcd7de7a1ffead1815294c2e092c"
#define TWO_OUR_OID "8f3c06cff9a83757cec40c80bc9bf31a2582bde9"
#define TWO_THEIR_OID "887b153b165d32409c70163e0f734c090f12f673"
/* Fixture setup and teardown */
void test_index_reuc__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_reuc__cleanup(void)
{
git_index_free(repo_index);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
void test_index_reuc__add(void)
{
git_oid ancestor_oid, our_oid, their_oid;
const git_index_reuc_entry *reuc;
git_oid__fromstr(&ancestor_oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
git_oid__fromstr(&our_oid, ONE_OUR_OID, GIT_OID_SHA1);
git_oid__fromstr(&their_oid, ONE_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_reuc_add(repo_index, "newfile.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt"));
cl_assert_equal_s("newfile.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
cl_assert_equal_oid(&reuc->oid[0], &ancestor_oid);
cl_assert_equal_oid(&reuc->oid[1], &our_oid);
cl_assert_equal_oid(&reuc->oid[2], &their_oid);
cl_git_pass(git_index_write(repo_index));
}
void test_index_reuc__add_no_ancestor(void)
{
git_oid ancestor_oid, our_oid, their_oid;
const git_index_reuc_entry *reuc;
memset(&ancestor_oid, 0x0, sizeof(git_oid));
git_oid__fromstr(&our_oid, ONE_OUR_OID, GIT_OID_SHA1);
git_oid__fromstr(&their_oid, ONE_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_reuc_add(repo_index, "newfile.txt",
0, NULL,
0100644, &our_oid,
0100644, &their_oid));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt"));
cl_assert_equal_s("newfile.txt", reuc->path);
cl_assert(reuc->mode[0] == 0);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
cl_assert_equal_oid(&reuc->oid[0], &ancestor_oid);
cl_assert_equal_oid(&reuc->oid[1], &our_oid);
cl_assert_equal_oid(&reuc->oid[2], &their_oid);
cl_git_pass(git_index_write(repo_index));
}
void test_index_reuc__read_bypath(void)
{
const git_index_reuc_entry *reuc;
git_oid oid;
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "two.txt"));
cl_assert_equal_s("two.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "one.txt"));
cl_assert_equal_s("one.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid__fromstr(&oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, ONE_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, ONE_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
}
void test_index_reuc__ignore_case(void)
{
const git_index_reuc_entry *reuc;
git_oid oid;
int index_caps;
index_caps = git_index_caps(repo_index);
index_caps &= ~GIT_INDEX_CAPABILITY_IGNORE_CASE;
cl_git_pass(git_index_set_caps(repo_index, index_caps));
cl_assert(!git_index_reuc_get_bypath(repo_index, "TWO.txt"));
index_caps |= GIT_INDEX_CAPABILITY_IGNORE_CASE;
cl_git_pass(git_index_set_caps(repo_index, index_caps));
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "TWO.txt"));
cl_assert_equal_s("two.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
}
void test_index_reuc__read_byindex(void)
{
const git_index_reuc_entry *reuc;
git_oid oid;
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert_equal_s("one.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid__fromstr(&oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, ONE_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, ONE_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));
cl_assert_equal_s("two.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
}
void test_index_reuc__updates_existing(void)
{
const git_index_reuc_entry *reuc;
git_oid ancestor_oid, our_oid, their_oid, oid;
int index_caps;
git_index_clear(repo_index);
index_caps = git_index_caps(repo_index);
index_caps |= GIT_INDEX_CAPABILITY_IGNORE_CASE;
cl_git_pass(git_index_set_caps(repo_index, index_caps));
git_oid__fromstr(&ancestor_oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
git_oid__fromstr(&our_oid, TWO_OUR_OID, GIT_OID_SHA1);
git_oid__fromstr(&their_oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_reuc_add(repo_index, "two.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
cl_git_pass(git_index_reuc_add(repo_index, "TWO.txt",
0100644, &our_oid,
0100644, &their_oid,
0100644, &ancestor_oid));
cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert_equal_s("TWO.txt", reuc->path);
git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
}
void test_index_reuc__remove(void)
{
git_oid oid;
const git_index_reuc_entry *reuc;
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
cl_git_pass(git_index_reuc_remove(repo_index, 0));
cl_git_fail(git_index_reuc_remove(repo_index, 1));
cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert_equal_s("two.txt", reuc->path);
cl_assert(reuc->mode[0] == 0100644);
cl_assert(reuc->mode[1] == 0100644);
cl_assert(reuc->mode[2] == 0100644);
git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[0], &oid);
git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[1], &oid);
git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&reuc->oid[2], &oid);
}
void test_index_reuc__write(void)
{
git_oid ancestor_oid, our_oid, their_oid;
const git_index_reuc_entry *reuc;
git_index_clear(repo_index);
/* Write out of order to ensure sorting is correct */
git_oid__fromstr(&ancestor_oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
git_oid__fromstr(&our_oid, TWO_OUR_OID, GIT_OID_SHA1);
git_oid__fromstr(&their_oid, TWO_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_reuc_add(repo_index, "two.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
git_oid__fromstr(&ancestor_oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
git_oid__fromstr(&our_oid, ONE_OUR_OID, GIT_OID_SHA1);
git_oid__fromstr(&their_oid, ONE_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_reuc_add(repo_index, "one.txt",
0100644, &ancestor_oid,
0100644, &our_oid,
0100644, &their_oid));
cl_git_pass(git_index_write(repo_index));
cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));
/* ensure sort order was round-tripped correct */
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
cl_assert_equal_s("one.txt", reuc->path);
cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));
cl_assert_equal_s("two.txt", reuc->path);
}
static int reuc_entry_exists(void)
{
return (git_index_reuc_get_bypath(repo_index, "newfile.txt") != NULL);
}
void test_index_reuc__cleaned_on_reset_hard(void)
{
git_object *target;
cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_reuc__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
cl_assert(reuc_entry_exists() == false);
git_object_free(target);
}
void test_index_reuc__cleaned_on_reset_mixed(void)
{
git_object *target;
cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_reuc__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
cl_assert(reuc_entry_exists() == false);
git_object_free(target);
}
void test_index_reuc__retained_on_reset_soft(void)
{
git_object *target;
cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
test_index_reuc__add();
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
cl_assert(reuc_entry_exists() == true);
git_object_free(target);
}
void test_index_reuc__cleaned_on_checkout_tree(void)
{
git_oid oid;
git_object *obj;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
test_index_reuc__add();
cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/master"));
cl_git_pass(git_object_lookup(&obj, repo, &oid, GIT_OBJECT_ANY));
cl_git_pass(git_checkout_tree(repo, obj, &opts));
cl_assert(reuc_entry_exists() == false);
git_object_free(obj);
}
void test_index_reuc__cleaned_on_checkout_head(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
test_index_reuc__add();
cl_git_pass(git_checkout_head(repo, &opts));
cl_assert(reuc_entry_exists() == false);
}
void test_index_reuc__retained_on_checkout_index(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
test_index_reuc__add();
cl_git_pass(git_checkout_index(repo, repo_index, &opts));
cl_assert(reuc_entry_exists() == true);
}
| libgit2-main | tests/libgit2/index/reuc.c |
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"
#include "conflicts.h"
static git_repository *repo;
static git_index *repo_index;
#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
/* Fixture setup and teardown */
void test_index_conflicts__initialize(void)
{
repo = cl_git_sandbox_init("mergedrepo");
git_repository_index(&repo_index, repo);
}
void test_index_conflicts__cleanup(void)
{
git_index_free(repo_index);
repo_index = NULL;
cl_git_sandbox_cleanup();
}
void test_index_conflicts__add(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
cl_assert(git_index_entrycount(repo_index) == 8);
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 = "test-one.txt";
ancestor_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
our_entry.path = "test-one.txt";
our_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 2);
git_oid__fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
their_entry.path = "test-one.txt";
their_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 2);
git_oid__fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
cl_assert(git_index_entrycount(repo_index) == 11);
}
void test_index_conflicts__add_fixes_incorrect_stage(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
const git_index_entry *conflict_entry[3];
cl_assert(git_index_entrycount(repo_index) == 8);
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 = "test-one.txt";
ancestor_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 3);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
our_entry.path = "test-one.txt";
our_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 1);
git_oid__fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
their_entry.path = "test-one.txt";
their_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&their_entry, 2);
git_oid__fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
cl_assert(git_index_entrycount(repo_index) == 11);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt"));
cl_assert(git_index_entry_stage(conflict_entry[0]) == 1);
cl_assert(git_index_entry_stage(conflict_entry[1]) == 2);
cl_assert(git_index_entry_stage(conflict_entry[2]) == 3);
}
void test_index_conflicts__add_detects_invalid_filemode(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
git_index_entry *conflict_entry[3];
int i;
cl_assert(git_index_entrycount(repo_index) == 8);
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
conflict_entry[0] = &ancestor_entry;
conflict_entry[1] = &our_entry;
conflict_entry[2] = &their_entry;
for (i = 0; i < 3; i++) {
ancestor_entry.path = "test-one.txt";
ancestor_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 3);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
our_entry.path = "test-one.txt";
our_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 1);
git_oid__fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
their_entry.path = "test-one.txt";
their_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&their_entry, 2);
git_oid__fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
/* Corrupt the conflict entry's mode */
conflict_entry[i]->mode = 027431745;
cl_git_fail(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
}
cl_assert(git_index_entrycount(repo_index) == 8);
}
void test_index_conflicts__add_removes_stage_zero(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
const git_index_entry *conflict_entry[3];
cl_assert(git_index_entrycount(repo_index) == 8);
memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
memset(&our_entry, 0x0, sizeof(git_index_entry));
memset(&their_entry, 0x0, sizeof(git_index_entry));
cl_git_mkfile("./mergedrepo/test-one.txt", "new-file\n");
cl_git_pass(git_index_add_bypath(repo_index, "test-one.txt"));
cl_assert(git_index_entrycount(repo_index) == 9);
ancestor_entry.path = "test-one.txt";
ancestor_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 3);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
our_entry.path = "test-one.txt";
our_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 1);
git_oid__fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
their_entry.path = "test-one.txt";
their_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&their_entry, 2);
git_oid__fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));
cl_assert(git_index_entrycount(repo_index) == 11);
cl_assert_equal_p(NULL, git_index_get_bypath(repo_index, "test-one.txt", 0));
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt"));
cl_assert_equal_oid(&ancestor_entry.id, &conflict_entry[0]->id);
cl_assert_equal_i(1, git_index_entry_stage(conflict_entry[0]));
cl_assert_equal_oid(&our_entry.id, &conflict_entry[1]->id);
cl_assert_equal_i(2, git_index_entry_stage(conflict_entry[1]));
cl_assert_equal_oid(&their_entry.id, &conflict_entry[2]->id);
cl_assert_equal_i(3, git_index_entry_stage(conflict_entry[2]));
}
void test_index_conflicts__get(void)
{
const git_index_entry *conflict_entry[3];
git_oid oid;
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, "conflicts-one.txt"));
cl_assert_equal_s("conflicts-one.txt", conflict_entry[0]->path);
git_oid__fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[0]->id);
git_oid__fromstr(&oid, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[1]->id);
git_oid__fromstr(&oid, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[2]->id);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, "conflicts-two.txt"));
cl_assert_equal_s("conflicts-two.txt", conflict_entry[0]->path);
git_oid__fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[0]->id);
git_oid__fromstr(&oid, CONFLICTS_TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[1]->id);
git_oid__fromstr(&oid, CONFLICTS_TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[2]->id);
}
void test_index_conflicts__iterate(void)
{
git_index_conflict_iterator *iterator;
const git_index_entry *conflict_entry[3];
git_oid oid;
cl_git_pass(git_index_conflict_iterator_new(&iterator, repo_index));
cl_git_pass(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator));
git_oid__fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[0]->id);
cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);
git_oid__fromstr(&oid, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[1]->id);
cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);
git_oid__fromstr(&oid, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[2]->id);
cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);
cl_git_pass(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator));
git_oid__fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[0]->id);
cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);
git_oid__fromstr(&oid, CONFLICTS_TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[1]->id);
cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);
git_oid__fromstr(&oid, CONFLICTS_TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[2]->id);
cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);
cl_assert(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator) == GIT_ITEROVER);
cl_assert(conflict_entry[0] == NULL);
cl_assert(conflict_entry[2] == NULL);
cl_assert(conflict_entry[2] == NULL);
git_index_conflict_iterator_free(iterator);
}
void test_index_conflicts__remove(void)
{
const git_index_entry *entry;
size_t i;
cl_assert(git_index_entrycount(repo_index) == 8);
cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-one.txt"));
cl_assert(git_index_entrycount(repo_index) == 5);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0);
}
cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-two.txt"));
cl_assert(git_index_entrycount(repo_index) == 2);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(strcmp(entry->path, "conflicts-two.txt") != 0);
}
}
void test_index_conflicts__moved_to_reuc_on_add(void)
{
const git_index_entry *entry;
size_t i;
cl_assert(git_index_entrycount(repo_index) == 8);
cl_git_mkfile("./mergedrepo/conflicts-one.txt", "new-file\n");
cl_git_pass(git_index_add_bypath(repo_index, "conflicts-one.txt"));
cl_assert(git_index_entrycount(repo_index) == 6);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
if (strcmp(entry->path, "conflicts-one.txt") == 0)
cl_assert(!git_index_entry_is_conflict(entry));
}
}
void test_index_conflicts__moved_to_reuc_on_remove(void)
{
const git_index_entry *entry;
size_t i;
cl_assert(git_index_entrycount(repo_index) == 8);
cl_git_pass(p_unlink("./mergedrepo/conflicts-one.txt"));
cl_git_pass(git_index_remove_bypath(repo_index, "conflicts-one.txt"));
cl_assert(git_index_entrycount(repo_index) == 5);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0);
}
}
void test_index_conflicts__remove_all_conflicts(void)
{
size_t i;
const git_index_entry *entry;
cl_assert(git_index_entrycount(repo_index) == 8);
cl_assert_equal_i(true, git_index_has_conflicts(repo_index));
git_index_conflict_cleanup(repo_index);
cl_assert_equal_i(false, git_index_has_conflicts(repo_index));
cl_assert(git_index_entrycount(repo_index) == 2);
for (i = 0; i < git_index_entrycount(repo_index); i++) {
cl_assert(entry = git_index_get_byindex(repo_index, i));
cl_assert(!git_index_entry_is_conflict(entry));
}
}
void test_index_conflicts__partial(void)
{
git_index_entry ancestor_entry, our_entry, their_entry;
const git_index_entry *conflict_entry[3];
cl_assert(git_index_entrycount(repo_index) == 8);
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 = "test-one.txt";
ancestor_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL));
cl_assert(git_index_entrycount(repo_index) == 9);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, "test-one.txt"));
cl_assert_equal_oid(&ancestor_entry.id, &conflict_entry[0]->id);
cl_assert(conflict_entry[1] == NULL);
cl_assert(conflict_entry[2] == NULL);
}
void test_index_conflicts__case_matters(void)
{
const git_index_entry *conflict_entry[3];
git_oid oid;
const char *upper_case = "DIFFERS-IN-CASE.TXT";
const char *mixed_case = "Differs-In-Case.txt";
const char *correct_case;
bool ignorecase = cl_repo_get_bool(repo, "core.ignorecase");
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 = upper_case;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, GIT_INDEX_STAGE_ANCESTOR);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
ancestor_entry.mode = GIT_FILEMODE_BLOB;
our_entry.path = upper_case;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, GIT_INDEX_STAGE_OURS);
git_oid__fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
our_entry.mode = GIT_FILEMODE_BLOB;
their_entry.path = upper_case;
GIT_INDEX_ENTRY_STAGE_SET(&their_entry, GIT_INDEX_STAGE_THEIRS);
git_oid__fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
their_entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_conflict_add(repo_index,
&ancestor_entry, &our_entry, &their_entry));
ancestor_entry.path = mixed_case;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, GIT_INDEX_STAGE_ANCESTOR);
git_oid__fromstr(&ancestor_entry.id, CONFLICTS_TWO_ANCESTOR_OID, GIT_OID_SHA1);
ancestor_entry.mode = GIT_FILEMODE_BLOB;
our_entry.path = mixed_case;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, GIT_INDEX_STAGE_ANCESTOR);
git_oid__fromstr(&our_entry.id, CONFLICTS_TWO_OUR_OID, GIT_OID_SHA1);
ancestor_entry.mode = GIT_FILEMODE_BLOB;
their_entry.path = mixed_case;
GIT_INDEX_ENTRY_STAGE_SET(&their_entry, GIT_INDEX_STAGE_THEIRS);
git_oid__fromstr(&their_entry.id, CONFLICTS_TWO_THEIR_OID, GIT_OID_SHA1);
their_entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_conflict_add(repo_index,
&ancestor_entry, &our_entry, &their_entry));
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, upper_case));
/*
* We inserted with mixed case last, so on a case-insensitive
* fs we should get the mixed case.
*/
if (ignorecase)
correct_case = mixed_case;
else
correct_case = upper_case;
cl_assert_equal_s(correct_case, conflict_entry[0]->path);
git_oid__fromstr(&oid, ignorecase ? CONFLICTS_TWO_ANCESTOR_OID : CONFLICTS_ONE_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[0]->id);
cl_assert_equal_s(correct_case, conflict_entry[1]->path);
git_oid__fromstr(&oid, ignorecase ? CONFLICTS_TWO_OUR_OID : CONFLICTS_ONE_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[1]->id);
cl_assert_equal_s(correct_case, conflict_entry[2]->path);
git_oid__fromstr(&oid, ignorecase ? CONFLICTS_TWO_THEIR_OID : CONFLICTS_ONE_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[2]->id);
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], repo_index, mixed_case));
cl_assert_equal_s(mixed_case, conflict_entry[0]->path);
git_oid__fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[0]->id);
cl_assert_equal_s(mixed_case, conflict_entry[1]->path);
git_oid__fromstr(&oid, CONFLICTS_TWO_OUR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[1]->id);
cl_assert_equal_s(mixed_case, conflict_entry[2]->path);
git_oid__fromstr(&oid, CONFLICTS_TWO_THEIR_OID, GIT_OID_SHA1);
cl_assert_equal_oid(&oid, &conflict_entry[2]->id);
}
| libgit2-main | tests/libgit2/index/conflicts.c |
#include "clar_libgit2.h"
#include "index.h"
static git_repository *g_repo = NULL;
void test_index_version__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
void test_index_version__can_read_v4(void)
{
const char *paths[] = {
"file.tx", "file.txt", "file.txz", "foo", "zzz",
};
git_index *index;
size_t i;
g_repo = cl_git_sandbox_init("indexv4");
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert_equal_sz(git_index_entrycount(index), 5);
for (i = 0; i < ARRAY_SIZE(paths); i++) {
const git_index_entry *entry =
git_index_get_bypath(index, paths[i], GIT_INDEX_STAGE_NORMAL);
cl_assert(entry != NULL);
}
git_index_free(index);
}
void test_index_version__can_write_v4(void)
{
const char *paths[] = {
"foo",
"foox",
"foobar",
"foobal",
"x",
"xz",
"xyzzyx"
};
git_repository *repo;
git_index_entry entry;
git_index *index;
size_t i;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_set_version(index, 4));
for (i = 0; i < ARRAY_SIZE(paths); i++) {
memset(&entry, 0, sizeof(entry));
entry.path = paths[i];
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_add_from_buffer(index, &entry, paths[i],
strlen(paths[i]) + 1));
}
cl_assert_equal_sz(git_index_entrycount(index), ARRAY_SIZE(paths));
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_repository_open(&repo, git_repository_path(g_repo)));
cl_git_pass(git_repository_index(&index, repo));
cl_assert(git_index_version(index) == 4);
for (i = 0; i < ARRAY_SIZE(paths); i++) {
const git_index_entry *e;
cl_assert(e = git_index_get_bypath(index, paths[i], 0));
cl_assert_equal_s(paths[i], e->path);
}
git_index_free(index);
git_repository_free(repo);
}
void test_index_version__v4_uses_path_compression(void)
{
git_index_entry entry;
git_index *index;
char path[250], buf[1];
struct stat st;
char i, j;
memset(path, 'a', sizeof(path));
memset(buf, 'a', sizeof(buf));
memset(&entry, 0, sizeof(entry));
entry.path = path;
entry.mode = GIT_FILEMODE_BLOB;
g_repo = cl_git_sandbox_init("indexv4");
cl_git_pass(git_repository_index(&index, g_repo));
/* write 676 paths of 250 bytes length */
for (i = 'a'; i <= 'z'; i++) {
for (j = 'a'; j < 'z'; j++) {
path[ARRAY_SIZE(path) - 3] = i;
path[ARRAY_SIZE(path) - 2] = j;
path[ARRAY_SIZE(path) - 1] = '\0';
cl_git_pass(git_index_add_from_buffer(index, &entry, buf, sizeof(buf)));
}
}
cl_git_pass(git_index_write(index));
cl_git_pass(p_stat(git_index_path(index), &st));
/*
* Without path compression, the written paths would at
* least take
*
* (entries * pathlen) = len
* (676 * 250) = 169000
*
* bytes. As index v4 uses suffix-compression and our
* written paths only differ in the last two entries,
* this number will be much smaller, e.g.
*
* (1 * pathlen) + (675 * 2) = len
* 676 + 1350 = 2026
*
* bytes.
*
* Note that the above calculations do not include
* additional metadata of the index, e.g. OIDs or
* index extensions. Including those we get an index
* of approx. 200kB without compression and 40kB with
* compression. As this is a lot smaller than without
* compression, we can verify that path compression is
* used.
*/
cl_assert_(st.st_size < 75000, "path compression not enabled");
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/version.c |
#include "clar_libgit2.h"
static git_repository *g_repo = NULL;
static git_index *g_index = NULL;
static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92";
static const char *valid_tree_id = "181037049a54a1eb5fab404658a3a250b44335d7";
static const char *valid_commit_id = "763d71aadf09a7951596c9746c024e7eece7c7af";
static const char *invalid_id = "1234567890123456789012345678901234567890";
void test_index_add__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_index(&g_index, g_repo));
}
void test_index_add__cleanup(void)
{
git_index_free(g_index);
cl_git_sandbox_cleanup();
g_repo = NULL;
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
}
static void test_add_entry(
bool should_succeed, const char *idstr, git_filemode_t mode)
{
git_index_entry entry = {{0}};
cl_git_pass(git_oid__fromstr(&entry.id, idstr, GIT_OID_SHA1));
entry.path = mode == GIT_FILEMODE_TREE ? "test_folder" : "test_file";
entry.mode = mode;
if (should_succeed)
cl_git_pass(git_index_add(g_index, &entry));
else
cl_git_fail(git_index_add(g_index, &entry));
}
void test_index_add__invalid_entries_succeeds_by_default(void)
{
/*
* Ensure that there is validation on object ids by default
*/
/* ensure that we can add some actually good entries */
test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB);
test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE);
test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK);
/* test that we fail to add some invalid (missing) blobs and trees */
test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB);
test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
test_add_entry(false, invalid_id, GIT_FILEMODE_LINK);
/* test that we validate the types of objects */
test_add_entry(false, valid_commit_id, GIT_FILEMODE_BLOB);
test_add_entry(false, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
test_add_entry(false, valid_commit_id, GIT_FILEMODE_LINK);
/*
* Ensure that there we can disable validation
*/
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
/* ensure that we can add some actually good entries */
test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB);
test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE);
test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK);
/* test that we can now add some invalid (missing) blobs and trees */
test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB);
test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
test_add_entry(true, invalid_id, GIT_FILEMODE_LINK);
/* test that we do not validate the types of objects */
test_add_entry(true, valid_commit_id, GIT_FILEMODE_BLOB);
test_add_entry(true, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
test_add_entry(true, valid_commit_id, GIT_FILEMODE_LINK);
}
| libgit2-main | tests/libgit2/index/add.c |
#include "clar_libgit2.h"
#include "git2.h"
#include "index.h"
#include "tree-cache.h"
static git_repository *g_repo;
void test_index_cache__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_index_cache__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
void test_index_cache__write_extension_at_root(void)
{
git_index *index;
git_tree *tree;
git_oid id;
const char *tree_id_str = "45dd856fdd4d89b884c340ba0e047752d9b085d6";
const char *index_file = "index-tree";
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree == NULL);
cl_git_pass(git_oid__fromstr(&id, tree_id_str, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_assert(index->tree);
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree);
cl_assert_equal_i(git_index_entrycount(index), index->tree->entry_count);
cl_assert_equal_i(0, index->tree->children_count);
cl_assert(git_oid_equal(&id, &index->tree->oid));
cl_git_pass(p_unlink(index_file));
git_index_free(index);
}
void test_index_cache__write_extension_invalidated_root(void)
{
git_index *index;
git_tree *tree;
git_oid id;
const char *tree_id_str = "45dd856fdd4d89b884c340ba0e047752d9b085d6";
const char *index_file = "index-tree-invalidated";
git_index_entry entry;
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree == NULL);
cl_git_pass(git_oid__fromstr(&id, tree_id_str, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_assert(index->tree);
memset(&entry, 0x0, sizeof(git_index_entry));
git_oid_cpy(&entry.id, &git_index_get_byindex(index, 0)->id);
entry.mode = GIT_FILEMODE_BLOB;
entry.path = "some-new-file.txt";
cl_git_pass(git_index_add(index, &entry));
cl_assert_equal_i(-1, index->tree->entry_count);
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_index_open(&index, index_file));
cl_assert(index->tree);
cl_assert_equal_i(-1, index->tree->entry_count);
cl_assert_equal_i(0, index->tree->children_count);
cl_assert(git_oid_cmp(&id, &index->tree->oid));
cl_git_pass(p_unlink(index_file));
git_index_free(index);
}
void test_index_cache__read_tree_no_children(void)
{
git_index *index;
git_index_entry entry;
git_tree *tree;
git_oid id;
cl_git_pass(git_index_new(&index));
cl_assert(index->tree == NULL);
cl_git_pass(git_oid__fromstr(&id, "45dd856fdd4d89b884c340ba0e047752d9b085d6", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_assert(index->tree);
cl_assert(git_oid_equal(&id, &index->tree->oid));
cl_assert_equal_i(0, index->tree->children_count);
cl_assert_equal_i(git_index_entrycount(index), index->tree->entry_count);
memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = "new.txt";
entry.mode = GIT_FILEMODE_BLOB;
git_oid__fromstr(&entry.id, "d4bcc68acd4410bf836a39f20afb2c2ece09584e", GIT_OID_SHA1);
cl_git_pass(git_index_add(index, &entry));
cl_assert_equal_i(-1, index->tree->entry_count);
git_index_free(index);
}
void test_index_cache__two_levels(void)
{
git_tree *tree;
git_oid tree_id;
git_index *index;
git_index_entry entry;
const git_tree_cache *tree_cache;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_clear(index));
memset(&entry, 0x0, sizeof(entry));
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_oid__fromstr(&entry.id, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", GIT_OID_SHA1));
entry.path = "top-level.txt";
cl_git_pass(git_index_add(index, &entry));
entry.path = "subdir/file.txt";
cl_git_pass(git_index_add(index, &entry));
/* the read-tree fills the tree cache */
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_index_read_tree(index, tree));
git_tree_free(tree);
cl_git_pass(git_index_write(index));
/* we now must have cache entries for "" and "subdir" */
cl_assert(index->tree);
cl_assert(git_tree_cache_get(index->tree, "subdir"));
cl_git_pass(git_index_read(index, true));
/* we must still have cache entries for "" and "subdir", since we wrote it out */
cl_assert(index->tree);
cl_assert(git_tree_cache_get(index->tree, "subdir"));
entry.path = "top-level.txt";
cl_git_pass(git_oid__fromstr(&entry.id, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc", GIT_OID_SHA1));
cl_git_pass(git_index_add(index, &entry));
/* writ out the index after we invalidate the root */
cl_git_pass(git_index_write(index));
cl_git_pass(git_index_read(index, true));
/* the cache for the subtree must still be valid, even if the root isn't */
cl_assert(index->tree);
cl_assert_equal_i(-1, index->tree->entry_count);
cl_assert_equal_i(1, index->tree->children_count);
tree_cache = git_tree_cache_get(index->tree, "subdir");
cl_assert(tree_cache);
cl_assert_equal_i(1, tree_cache->entry_count);
git_index_free(index);
}
void test_index_cache__read_tree_children(void)
{
git_index *index;
git_index_entry entry;
git_tree *tree;
const git_tree_cache *cache;
git_oid tree_id;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_clear(index));
cl_assert(index->tree == NULL);
/* add a bunch of entries at different levels */
memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = "top-level";
entry.mode = GIT_FILEMODE_BLOB;
git_oid__fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
cl_git_pass(git_index_add(index, &entry));
entry.path = "subdir/some-file";
cl_git_pass(git_index_add(index, &entry));
entry.path = "subdir/even-deeper/some-file";
cl_git_pass(git_index_add(index, &entry));
entry.path = "subdir2/some-file";
cl_git_pass(git_index_add(index, &entry));
cl_git_pass(git_index_write_tree(&tree_id, index));
cl_git_pass(git_index_clear(index));
cl_assert(index->tree == NULL);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(git_index_read_tree(index, tree));
git_tree_free(tree);
cl_assert(index->tree);
cl_assert_equal_i(2, index->tree->children_count);
/* override with a slightly different id, also dummy */
entry.path = "subdir/some-file";
git_oid__fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
cl_git_pass(git_index_add(index, &entry));
cl_assert_equal_i(-1, index->tree->entry_count);
cache = git_tree_cache_get(index->tree, "subdir");
cl_assert(cache);
cl_assert_equal_i(-1, cache->entry_count);
cache = git_tree_cache_get(index->tree, "subdir/even-deeper");
cl_assert(cache);
cl_assert_equal_i(1, cache->entry_count);
cache = git_tree_cache_get(index->tree, "subdir2");
cl_assert(cache);
cl_assert_equal_i(1, cache->entry_count);
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/cache.c |
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/index.h"
static git_repository *g_repo;
static git_odb *g_odb;
static git_index *g_index;
static git_oid g_empty_id;
void test_index_collision__initialize(void)
{
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_repository_odb(&g_odb, g_repo));
cl_git_pass(git_repository_index(&g_index, g_repo));
cl_git_pass(git_odb_write(&g_empty_id, g_odb, "", 0, GIT_OBJECT_BLOB));
}
void test_index_collision__cleanup(void)
{
git_index_free(g_index);
git_odb_free(g_odb);
cl_git_sandbox_cleanup();
}
void test_index_collision__add_blob_with_conflicting_file(void)
{
git_index_entry entry;
git_tree_entry *tentry;
git_oid tree_id;
git_tree *tree;
memset(&entry, 0, sizeof(entry));
entry.ctime.seconds = 12346789;
entry.mtime.seconds = 12346789;
entry.mode = 0100644;
entry.file_size = 0;
git_oid_cpy(&entry.id, &g_empty_id);
entry.path = "a/b";
cl_git_pass(git_index_add(g_index, &entry));
/* Check a/b exists here */
cl_git_pass(git_index_write_tree(&tree_id, g_index));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
git_tree_entry_free(tentry);
git_tree_free(tree);
/* create a tree/blob collision */
entry.path = "a/b/c";
cl_git_pass(git_index_add(g_index, &entry));
/* a/b should now be a tree and a/b/c a blob */
cl_git_pass(git_index_write_tree(&tree_id, g_index));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
git_tree_entry_free(tentry);
git_tree_free(tree);
}
void test_index_collision__add_blob_with_conflicting_dir(void)
{
git_index_entry entry;
git_tree_entry *tentry;
git_oid tree_id;
git_tree *tree;
memset(&entry, 0, sizeof(entry));
entry.ctime.seconds = 12346789;
entry.mtime.seconds = 12346789;
entry.mode = 0100644;
entry.file_size = 0;
git_oid_cpy(&entry.id, &g_empty_id);
entry.path = "a/b/c";
cl_git_pass(git_index_add(g_index, &entry));
/* Check a/b/c exists here */
cl_git_pass(git_index_write_tree(&tree_id, g_index));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
git_tree_entry_free(tentry);
git_tree_free(tree);
/* create a blob/tree collision */
entry.path = "a/b";
cl_git_pass(git_index_add(g_index, &entry));
/* a/b should now be a tree and a/b/c a blob */
cl_git_pass(git_index_write_tree(&tree_id, g_index));
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
cl_git_fail(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
git_tree_entry_free(tentry);
git_tree_free(tree);
}
void test_index_collision__add_with_highstage_1(void)
{
git_index_entry entry;
memset(&entry, 0, sizeof(entry));
entry.ctime.seconds = 12346789;
entry.mtime.seconds = 12346789;
entry.mode = 0100644;
entry.file_size = 0;
git_oid_cpy(&entry.id, &g_empty_id);
entry.path = "a/b";
GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(g_index, &entry));
/* create a blob beneath the previous tree entry */
entry.path = "a/b/c";
entry.flags = 0;
cl_git_pass(git_index_add(g_index, &entry));
/* create another tree entry above the blob */
entry.path = "a/b";
GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(g_index, &entry));
}
void test_index_collision__add_with_highstage_2(void)
{
git_index_entry entry;
memset(&entry, 0, sizeof(entry));
entry.ctime.seconds = 12346789;
entry.mtime.seconds = 12346789;
entry.mode = 0100644;
entry.file_size = 0;
git_oid_cpy(&entry.id, &g_empty_id);
entry.path = "a/b/c";
GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(g_index, &entry));
/* create a blob beneath the previous tree entry */
entry.path = "a/b/c";
GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(g_index, &entry));
/* create another tree entry above the blob */
entry.path = "a/b";
GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
cl_git_pass(git_index_add(g_index, &entry));
}
| libgit2-main | tests/libgit2/index/collision.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "index.h"
#include "conflicts.h"
static git_repository *_repo;
static git_index *_index;
void test_index_read_index__initialize(void)
{
git_object *head;
git_reference *head_ref;
_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_revparse_ext(&head, &head_ref, _repo, "HEAD"));
cl_git_pass(git_reset(_repo, head, GIT_RESET_HARD, NULL));
cl_git_pass(git_repository_index(&_index, _repo));
git_reference_free(head_ref);
git_object_free(head);
}
void test_index_read_index__cleanup(void)
{
git_index_free(_index);
cl_git_sandbox_cleanup();
}
void test_index_read_index__maintains_stat_cache(void)
{
git_index *new_index;
git_oid index_id;
git_index_entry new_entry;
const git_index_entry *e;
git_tree *tree;
size_t i;
cl_assert_equal_i(4, git_index_entrycount(_index));
/* write-tree */
cl_git_pass(git_index_write_tree(&index_id, _index));
/* read-tree, then read index */
git_tree_lookup(&tree, _repo, &index_id);
cl_git_pass(git_index_new(&new_index));
cl_git_pass(git_index_read_tree(new_index, tree));
git_tree_free(tree);
/* add a new entry that will not have stat data */
memset(&new_entry, 0, sizeof(git_index_entry));
new_entry.path = "Hello";
git_oid__fromstr(&new_entry.id, "0123456789012345678901234567890123456789", GIT_OID_SHA1);
new_entry.file_size = 1234;
new_entry.mode = 0100644;
cl_git_pass(git_index_add(new_index, &new_entry));
cl_assert_equal_i(5, git_index_entrycount(new_index));
cl_git_pass(git_index_read_index(_index, new_index));
git_index_free(new_index);
cl_assert_equal_i(5, git_index_entrycount(_index));
for (i = 0; i < git_index_entrycount(_index); i++) {
e = git_index_get_byindex(_index, i);
if (strcmp(e->path, "Hello") == 0) {
cl_assert_equal_i(0, e->ctime.seconds);
cl_assert_equal_i(0, e->mtime.seconds);
} else {
cl_assert(0 != e->ctime.seconds);
cl_assert(0 != e->mtime.seconds);
}
}
}
static bool roundtrip_with_read_index(const char *tree_idstr)
{
git_oid tree_id, new_tree_id;
git_tree *tree;
git_index *tree_index;
cl_git_pass(git_oid__fromstr(&tree_id, tree_idstr, GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
cl_git_pass(git_index_new(&tree_index));
cl_git_pass(git_index_read_tree(tree_index, tree));
cl_git_pass(git_index_read_index(_index, tree_index));
cl_git_pass(git_index_write_tree(&new_tree_id, _index));
git_tree_free(tree);
git_index_free(tree_index);
return git_oid_equal(&tree_id, &new_tree_id);
}
void test_index_read_index__produces_treesame_indexes(void)
{
roundtrip_with_read_index("53fc32d17276939fc79ed05badaef2db09990016");
roundtrip_with_read_index("944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
roundtrip_with_read_index("1810dff58d8a660512d4832e740f692884338ccd");
roundtrip_with_read_index("d52a8fe84ceedf260afe4f0287bbfca04a117e83");
roundtrip_with_read_index("c36d8ea75da8cb510fcb0c408c1d7e53f9a99dbe");
roundtrip_with_read_index("7b2417a23b63e1fdde88c80e14b33247c6e5785a");
roundtrip_with_read_index("f82a8eb4cb20e88d1030fd10d89286215a715396");
roundtrip_with_read_index("fd093bff70906175335656e6ce6ae05783708765");
roundtrip_with_read_index("ae90f12eea699729ed24555e40b9fd669da12a12");
}
void test_index_read_index__read_and_writes(void)
{
git_oid tree_id, new_tree_id;
git_tree *tree;
git_index *tree_index, *new_index;
cl_git_pass(git_oid__fromstr(&tree_id, "ae90f12eea699729ed24555e40b9fd669da12a12", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
cl_git_pass(git_index_new(&tree_index));
cl_git_pass(git_index_read_tree(tree_index, tree));
cl_git_pass(git_index_read_index(_index, tree_index));
cl_git_pass(git_index_write(_index));
cl_git_pass(git_index_open(&new_index, git_index_path(_index)));
cl_git_pass(git_index_write_tree_to(&new_tree_id, new_index, _repo));
cl_assert_equal_oid(&tree_id, &new_tree_id);
git_tree_free(tree);
git_index_free(tree_index);
git_index_free(new_index);
}
static void add_conflicts(git_index *index, const char *filename)
{
git_index_entry ancestor_entry, our_entry, their_entry;
static int conflict_idx = 0;
char *ancestor_ids[] =
{ CONFLICTS_ONE_ANCESTOR_OID, CONFLICTS_TWO_ANCESTOR_OID };
char *our_ids[] =
{ CONFLICTS_ONE_OUR_OID, CONFLICTS_TWO_OUR_OID };
char *their_ids[] =
{ CONFLICTS_ONE_THEIR_OID, CONFLICTS_TWO_THEIR_OID };
conflict_idx = (conflict_idx + 1) % 2;
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 = filename;
ancestor_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 1);
git_oid__fromstr(&ancestor_entry.id, ancestor_ids[conflict_idx], GIT_OID_SHA1);
our_entry.path = filename;
our_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 2);
git_oid__fromstr(&our_entry.id, our_ids[conflict_idx], GIT_OID_SHA1);
their_entry.path = filename;
their_entry.mode = 0100644;
GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 2);
git_oid__fromstr(&their_entry.id, their_ids[conflict_idx], GIT_OID_SHA1);
cl_git_pass(git_index_conflict_add(index, &ancestor_entry,
&our_entry, &their_entry));
}
void test_index_read_index__handles_conflicts(void)
{
git_oid tree_id;
git_tree *tree;
git_index *index, *new_index;
git_index_conflict_iterator *conflict_iterator;
const git_index_entry *ancestor, *ours, *theirs;
cl_git_pass(git_oid__fromstr(&tree_id, "ae90f12eea699729ed24555e40b9fd669da12a12", GIT_OID_SHA1));
cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
cl_git_pass(git_index_new(&index));
cl_git_pass(git_index_new(&new_index));
cl_git_pass(git_index_read_tree(index, tree));
cl_git_pass(git_index_read_tree(new_index, tree));
/* put some conflicts in only the old side, these should be removed */
add_conflicts(index, "orig_side-1.txt");
add_conflicts(index, "orig_side-2.txt");
/* put some conflicts in both indexes, these should be unchanged */
add_conflicts(index, "both_sides-1.txt");
add_conflicts(new_index, "both_sides-1.txt");
add_conflicts(index, "both_sides-2.txt");
add_conflicts(new_index, "both_sides-2.txt");
/* put some conflicts in the new index, these should be added */
add_conflicts(new_index, "new_side-1.txt");
add_conflicts(new_index, "new_side-2.txt");
cl_git_pass(git_index_read_index(index, new_index));
cl_git_pass(git_index_conflict_iterator_new(&conflict_iterator, index));
cl_git_pass(git_index_conflict_next(
&ancestor, &ours, &theirs, conflict_iterator));
cl_assert_equal_s("both_sides-1.txt", ancestor->path);
cl_assert_equal_s("both_sides-1.txt", ours->path);
cl_assert_equal_s("both_sides-1.txt", theirs->path);
cl_git_pass(git_index_conflict_next(
&ancestor, &ours, &theirs, conflict_iterator));
cl_assert_equal_s("both_sides-2.txt", ancestor->path);
cl_assert_equal_s("both_sides-2.txt", ours->path);
cl_assert_equal_s("both_sides-2.txt", theirs->path);
cl_git_pass(git_index_conflict_next(
&ancestor, &ours, &theirs, conflict_iterator));
cl_assert_equal_s("new_side-1.txt", ancestor->path);
cl_assert_equal_s("new_side-1.txt", ours->path);
cl_assert_equal_s("new_side-1.txt", theirs->path);
cl_git_pass(git_index_conflict_next(
&ancestor, &ours, &theirs, conflict_iterator));
cl_assert_equal_s("new_side-2.txt", ancestor->path);
cl_assert_equal_s("new_side-2.txt", ours->path);
cl_assert_equal_s("new_side-2.txt", theirs->path);
cl_git_fail_with(GIT_ITEROVER, git_index_conflict_next(
&ancestor, &ours, &theirs, conflict_iterator));
git_index_conflict_iterator_free(conflict_iterator);
git_tree_free(tree);
git_index_free(new_index);
git_index_free(index);
}
| libgit2-main | tests/libgit2/index/read_index.c |
#include "clar_libgit2.h"
#include "../filter/crlf.h"
#include "git2/checkout.h"
#include "repository.h"
#include "posix.h"
#define FILE_CONTENTS_LF "one\ntwo\nthree\nfour\n"
#define FILE_CONTENTS_CRLF "one\r\ntwo\r\nthree\r\nfour\r\n"
#define FILE_OID_LF "f384549cbeb481e437091320de6d1f2e15e11b4a"
#define FILE_OID_CRLF "7fbf4d847b191141d80f30c8ab03d2ad4cd543a9"
static git_repository *g_repo;
static git_index *g_index;
static git_str expected_fixture = GIT_STR_INIT;
void test_index_crlf__initialize(void)
{
g_repo = cl_git_sandbox_init_new("crlf");
cl_git_pass(git_repository_index(&g_index, g_repo));
}
void test_index_crlf__cleanup(void)
{
git_index_free(g_index);
cl_git_sandbox_cleanup();
if (expected_fixture.size) {
cl_fixture_cleanup(expected_fixture.ptr);
git_str_dispose(&expected_fixture);
}
}
struct compare_data
{
const char *systype;
const char *dirname;
const char *safecrlf;
const char *autocrlf;
const char *attrs;
};
static int add_and_check_file(void *payload, git_str *actual_path)
{
git_str expected_path = GIT_STR_INIT;
git_str expected_path_fail = GIT_STR_INIT;
git_str expected_contents = GIT_STR_INIT;
struct compare_data *cd = payload;
char *basename;
const git_index_entry *entry;
git_blob *blob;
bool failed = true;
basename = git_fs_path_basename(actual_path->ptr);
if (!strcmp(basename, ".git") || !strcmp(basename, ".gitattributes")) {
failed = false;
goto done;
}
cl_git_pass(git_str_joinpath(&expected_path, cd->dirname, basename));
cl_git_pass(git_str_puts(&expected_path_fail, expected_path.ptr));
cl_git_pass(git_str_puts(&expected_path_fail, ".fail"));
if (git_fs_path_isfile(expected_path.ptr)) {
cl_git_pass(git_index_add_bypath(g_index, basename));
cl_assert(entry = git_index_get_bypath(g_index, basename, 0));
cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
cl_git_pass(git_futils_readbuffer(&expected_contents, expected_path.ptr));
if (strcmp(expected_contents.ptr, git_blob_rawcontent(blob)) != 0)
goto done;
git_blob_free(blob);
} else if (git_fs_path_isfile(expected_path_fail.ptr)) {
cl_git_pass(git_futils_readbuffer(&expected_contents, expected_path_fail.ptr));
git_str_rtrim(&expected_contents);
if (git_index_add_bypath(g_index, basename) == 0 ||
git_error_last()->klass != GIT_ERROR_FILTER ||
strcmp(expected_contents.ptr, git_error_last()->message) != 0)
goto done;
} else {
cl_fail("unexpected index failure");
}
failed = false;
done:
if (failed) {
git_str details = GIT_STR_INIT;
git_str_printf(&details, "filename=%s, system=%s, autocrlf=%s, safecrlf=%s, attrs={%s}",
basename, cd->systype, cd->autocrlf, cd->safecrlf, cd->attrs);
clar__fail(__FILE__, __func__, __LINE__,
"index contents did not match expected", details.ptr, 0);
git_str_dispose(&details);
}
git__free(basename);
git_str_dispose(&expected_contents);
git_str_dispose(&expected_path);
git_str_dispose(&expected_path_fail);
return 0;
}
static const char *system_type(void)
{
if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
return "windows";
else
return "posix";
}
static void test_add_index(const char *safecrlf, const char *autocrlf, const char *attrs)
{
git_str attrbuf = GIT_STR_INIT;
git_str expected_dirname = GIT_STR_INIT;
git_str sandboxname = GIT_STR_INIT;
git_str reponame = GIT_STR_INIT;
struct compare_data compare_data = { system_type(), NULL, safecrlf, autocrlf, attrs };
const char *c;
git_str_puts(&reponame, "crlf");
git_str_puts(&sandboxname, "autocrlf_");
git_str_puts(&sandboxname, autocrlf);
git_str_puts(&sandboxname, ",safecrlf_");
git_str_puts(&sandboxname, safecrlf);
if (*attrs) {
git_str_puts(&sandboxname, ",");
for (c = attrs; *c; c++) {
if (*c == ' ')
git_str_putc(&sandboxname, ',');
else if (*c == '=')
git_str_putc(&sandboxname, '_');
else
git_str_putc(&sandboxname, *c);
}
git_str_printf(&attrbuf, "* %s\n", attrs);
cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
}
cl_repo_set_string(g_repo, "core.safecrlf", safecrlf);
cl_repo_set_string(g_repo, "core.autocrlf", autocrlf);
cl_git_pass(git_index_clear(g_index));
git_str_joinpath(&expected_dirname, "crlf_data", system_type());
git_str_puts(&expected_dirname, "_to_odb");
git_str_joinpath(&expected_fixture, expected_dirname.ptr, sandboxname.ptr);
cl_fixture_sandbox(expected_fixture.ptr);
compare_data.dirname = sandboxname.ptr;
cl_git_pass(git_fs_path_direach(&reponame, 0, add_and_check_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(&reponame);
}
static void set_up_workingdir(const char *name)
{
git_vector contents = GIT_VECTOR_INIT;
size_t i;
const char *fn;
git_fs_path_dirload(&contents, name, 0, 0);
git_vector_foreach(&contents, i, fn) {
char *basename = git_fs_path_basename(fn);
bool skip = strncasecmp(basename, ".git", 4) == 0 && strlen(basename) == 4;
git__free(basename);
if (skip)
continue;
p_unlink(fn);
}
git_vector_free_deep(&contents);
/* copy input files */
git_fs_path_dirload(&contents, cl_fixture("crlf"), 0, 0);
git_vector_foreach(&contents, i, fn) {
char *basename = git_fs_path_basename(fn);
git_str dest_filename = GIT_STR_INIT;
if (strcmp(basename, ".gitted") &&
strcmp(basename, ".gitattributes")) {
git_str_joinpath(&dest_filename, name, basename);
cl_git_pass(git_futils_cp(fn, dest_filename.ptr, 0644));
}
git__free(basename);
git_str_dispose(&dest_filename);
}
git_vector_free_deep(&contents);
}
void test_index_crlf__matches_core_git(void)
{
const char *safecrlf[] = { "true", "false", "warn", NULL };
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, **c;
for (a = safecrlf; *a; a++) {
for (b = autocrlf; *b; b++) {
for (c = attrs; *c; c++) {
set_up_workingdir("crlf");
test_add_index(*a, *b, *c);
}
}
}
}
void test_index_crlf__autocrlf_false_no_attrs(void)
{
const git_index_entry *entry;
git_oid oid;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_mkfile("./crlf/newfile.txt",
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_git_pass(git_oid__fromstr(&oid,
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_OID_CRLF : FILE_OID_LF,
GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
void test_index_crlf__autocrlf_true_no_attrs(void)
{
const git_index_entry *entry;
git_oid oid;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_mkfile("./crlf/newfile.txt",
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
void test_index_crlf__autocrlf_input_no_attrs(void)
{
const git_index_entry *entry;
git_oid oid;
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_git_mkfile("./crlf/newfile.txt",
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
void test_index_crlf__autocrlf_false_text_auto_attr(void)
{
const git_index_entry *entry;
git_oid oid;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_mkfile("./crlf/newfile.txt",
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
void test_index_crlf__autocrlf_true_text_auto_attr(void)
{
const git_index_entry *entry;
git_oid oid;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_mkfile("./crlf/newfile.txt",
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
void test_index_crlf__autocrlf_input_text_auto_attr(void)
{
const git_index_entry *entry;
git_oid oid;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_git_mkfile("./crlf/newfile.txt",
(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
}
void test_index_crlf__safecrlf_true_autocrlf_input_text_auto_attr(void)
{
const git_index_entry *entry;
git_oid oid;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_repo_set_bool(g_repo, "core.safecrlf", true);
cl_git_mkfile("./crlf/newfile.txt", FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_assert(entry);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
cl_git_mkfile("./crlf/newfile2.txt", FILE_CONTENTS_CRLF);
cl_git_fail(git_index_add_bypath(g_index, "newfile2.txt"));
}
void test_index_crlf__safecrlf_true_autocrlf_input_text__no_attr(void)
{
const git_index_entry *entry;
git_oid oid;
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_repo_set_bool(g_repo, "core.safecrlf", true);
cl_git_mkfile("./crlf/newfile.txt", FILE_CONTENTS_LF);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
entry = git_index_get_bypath(g_index, "newfile.txt", 0);
cl_assert(entry);
cl_git_pass(git_oid__fromstr(&oid, FILE_OID_LF, GIT_OID_SHA1));
cl_assert_equal_oid(&oid, &entry->id);
cl_git_mkfile("./crlf/newfile2.txt", FILE_CONTENTS_CRLF);
cl_git_fail(git_index_add_bypath(g_index, "newfile2.txt"));
}
void test_index_crlf__safecrlf_true_no_attrs(void)
{
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_repo_set_bool(g_repo, "core.safecrlf", true);
cl_git_mkfile("crlf/newfile.txt", ALL_LF_TEXT_RAW);
cl_git_fail(git_index_add_bypath(g_index, "newfile.txt"));
cl_git_mkfile("crlf/newfile.txt", ALL_CRLF_TEXT_RAW);
cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
cl_git_mkfile("crlf/newfile.txt", MORE_CRLF_TEXT_RAW);
cl_git_fail(git_index_add_bypath(g_index, "newfile.txt"));
cl_git_mkfile("crlf/newfile.txt", MORE_LF_TEXT_RAW);
cl_git_fail(git_index_add_bypath(g_index, "newfile.txt"));
}
| libgit2-main | tests/libgit2/index/crlf.c |
#include "clar_libgit2.h"
#include "git2/sys/filter.h"
static git_repository *g_repo = NULL;
void test_filter_ident__initialize(void)
{
g_repo = cl_git_sandbox_init("crlf");
}
void test_filter_ident__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void add_blob_and_filter(
const char *data,
git_filter_list *fl,
const char *expected)
{
git_oid id;
git_blob *blob;
git_buf out = { 0 };
cl_git_mkfile("crlf/identtest", data);
cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "identtest"));
cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
cl_git_pass(git_filter_list_apply_to_blob(&out, fl, blob));
cl_assert_equal_s(expected, out.ptr);
git_blob_free(blob);
git_buf_dispose(&out);
}
void test_filter_ident__to_worktree(void)
{
git_filter_list *fl;
git_filter *ident;
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
ident = git_filter_lookup(GIT_FILTER_IDENT);
cl_assert(ident != NULL);
cl_git_pass(git_filter_list_push(fl, ident, NULL));
add_blob_and_filter(
"Hello\n$Id$\nFun stuff\n", fl,
"Hello\n$Id: b69e2387aafcaf73c4de5b9ab59abe27fdadee30 $\nFun stuff\n");
add_blob_and_filter(
"Hello\n$Id: Junky$\nFun stuff\n", fl,
"Hello\n$Id: 45cd107a7102911cb2a7df08404674327fa050b9 $\nFun stuff\n");
add_blob_and_filter(
"$Id$\nAt the start\n", fl,
"$Id: b13415c767abc196fb95bd17070e8c1113e32160 $\nAt the start\n");
add_blob_and_filter(
"At the end\n$Id$", fl,
"At the end\n$Id: 1344925c6bc65b34c5a7b50f86bf688e48e9a272 $");
add_blob_and_filter(
"$Id$", fl,
"$Id: b3f5ebfb5843bc43ceecff6d4f26bb37c615beb1 $");
add_blob_and_filter(
"$Id: Some sort of junk goes here$", fl,
"$Id: ab2dd3853c7c9a4bff55aca2bea077a73c32ac06 $");
add_blob_and_filter("$Id: ", fl, "$Id: ");
add_blob_and_filter("$Id", fl, "$Id");
add_blob_and_filter("$I", fl, "$I");
add_blob_and_filter("Id$", fl, "Id$");
git_filter_list_free(fl);
}
void test_filter_ident__to_odb(void)
{
git_filter_list *fl;
git_filter *ident;
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
ident = git_filter_lookup(GIT_FILTER_IDENT);
cl_assert(ident != NULL);
cl_git_pass(git_filter_list_push(fl, ident, NULL));
add_blob_and_filter(
"Hello\n$Id$\nFun stuff\n",
fl, "Hello\n$Id$\nFun stuff\n");
add_blob_and_filter(
"Hello\n$Id: b69e2387aafcaf73c4de5b9ab59abe27fdadee30$\nFun stuff\n",
fl, "Hello\n$Id$\nFun stuff\n");
add_blob_and_filter(
"Hello\n$Id: Any junk you may have left here$\nFun stuff\n",
fl, "Hello\n$Id$\nFun stuff\n");
add_blob_and_filter(
"Hello\n$Id:$\nFun stuff\n",
fl, "Hello\n$Id$\nFun stuff\n");
add_blob_and_filter(
"Hello\n$Id:x$\nFun stuff\n",
fl, "Hello\n$Id$\nFun stuff\n");
add_blob_and_filter(
"$Id$\nAt the start\n", fl, "$Id$\nAt the start\n");
add_blob_and_filter(
"$Id: lots of random text that should be removed from here$\nAt the start\n", fl, "$Id$\nAt the start\n");
add_blob_and_filter(
"$Id: lots of random text that should not be removed without a terminator\nAt the start\n", fl, "$Id: lots of random text that should not be removed without a terminator\nAt the start\n");
add_blob_and_filter(
"At the end\n$Id$", fl, "At the end\n$Id$");
add_blob_and_filter(
"At the end\n$Id:$", fl, "At the end\n$Id$");
add_blob_and_filter(
"At the end\n$Id:asdfasdf$", fl, "At the end\n$Id$");
add_blob_and_filter(
"At the end\n$Id", fl, "At the end\n$Id");
add_blob_and_filter(
"At the end\n$IddI", fl, "At the end\n$IddI");
add_blob_and_filter("$Id$", fl, "$Id$");
add_blob_and_filter("$Id: any$", fl, "$Id$");
add_blob_and_filter("$Id: any long stuff goes here you see$", fl, "$Id$");
add_blob_and_filter("$Id: ", fl, "$Id: ");
add_blob_and_filter("$Id", fl, "$Id");
add_blob_and_filter("$I", fl, "$I");
add_blob_and_filter("Id$", fl, "Id$");
git_filter_list_free(fl);
}
| libgit2-main | tests/libgit2/filter/ident.c |
#include "clar_libgit2.h"
#include "crlf.h"
static git_repository *g_repo = NULL;
static git_blob_filter_options filter_opts = GIT_BLOB_FILTER_OPTIONS_INIT;
void test_filter_bare__initialize(void)
{
cl_fixture_sandbox("crlf.git");
cl_git_pass(git_repository_open(&g_repo, "crlf.git"));
filter_opts.flags |= GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES;
filter_opts.flags |= GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD;
}
void test_filter_bare__cleanup(void)
{
git_repository_free(g_repo);
cl_fixture_cleanup("crlf.git");
}
void test_filter_bare__all_crlf(void)
{
git_blob *blob;
git_buf buf = { 0 };
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "a9a2e89")); /* all-crlf */
cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", &filter_opts));
cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", &filter_opts));
/* in this case, raw content has crlf in it already */
cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", &filter_opts));
/* we never convert CRLF -> LF on platforms that have LF */
cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.txt", &filter_opts));
/* in this case, raw content has crlf in it already */
cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_bare__from_lf(void)
{
git_blob *blob;
git_buf buf = { 0 };
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "799770d")); /* all-lf */
cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", &filter_opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", &filter_opts));
/* in this case, raw content has crlf in it already */
cl_assert_equal_s(ALL_LF_TEXT_AS_CRLF, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", &filter_opts));
/* we never convert CRLF -> LF on platforms that have LF */
cl_assert_equal_s(ALL_LF_TEXT_AS_LF, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_bare__nested_attributes(void)
{
git_blob *blob;
git_buf buf = { 0 };
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "799770d")); /* all-lf */
cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "raw/file.bin", &filter_opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "raw/file.crlf", &filter_opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "raw/file.lf", &filter_opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_bare__sanitizes(void)
{
git_blob *blob;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "e69de29")); /* zero-byte */
cl_assert_equal_i(0, git_blob_rawsize(blob));
cl_assert_equal_s("", git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", &filter_opts));
cl_assert_equal_sz(0, buf.size);
cl_assert_equal_s("", buf.ptr);
git_buf_dispose(&buf);
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", &filter_opts));
cl_assert_equal_sz(0, buf.size);
cl_assert_equal_s("", buf.ptr);
git_buf_dispose(&buf);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", &filter_opts));
cl_assert_equal_sz(0, buf.size);
cl_assert_equal_s("", buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_bare__from_specific_commit_one(void)
{
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
git_blob *blob;
git_buf buf = { 0 };
opts.flags |= GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES;
opts.flags |= GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT;
cl_git_pass(git_oid__fromstr(&opts.attr_commit_id, "b8986fec0f7bde90f78ac72706e782d82f24f2f0", GIT_OID_SHA1));
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "055c872")); /* ident */
cl_assert_equal_s("$Id$\n", git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "ident.bin", &opts));
cl_assert_equal_s("$Id$\n", buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "ident.identlf", &opts));
cl_assert_equal_s("$Id: 055c8729cdcc372500a08db659c045e16c4409fb $\n", buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_bare__from_specific_commit_with_no_attributes_file(void)
{
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
git_blob *blob;
git_buf buf = { 0 };
opts.flags |= GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES;
opts.flags |= GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT;
cl_git_pass(git_oid__fromstr(&opts.attr_commit_id, "5afb6a14a864e30787857dd92af837e8cdd2cb1b", GIT_OID_SHA1));
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "799770d")); /* all-lf */
cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", &opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
/* we never convert CRLF -> LF on platforms that have LF */
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", &opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
/* we never convert CRLF -> LF on platforms that have LF */
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", &opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
| libgit2-main | tests/libgit2/filter/bare.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "filter.h"
#include "git2/sys/filter.h"
#include "custom_helpers.h"
#define VERY_SECURE_ENCRYPTION(b) ((b) ^ 0xff)
int bitflip_filter_apply(
git_filter *self,
void **payload,
git_str *to,
const git_str *from,
const git_filter_source *source)
{
const unsigned char *src = (const unsigned char *)from->ptr;
unsigned char *dst;
size_t i;
GIT_UNUSED(self); GIT_UNUSED(payload);
/* verify that attribute path match worked as expected */
cl_assert_equal_i(
0, git__strncmp("hero", git_filter_source_path(source), 4));
if (!from->size)
return 0;
cl_git_pass(git_str_grow(to, from->size));
dst = (unsigned char *)to->ptr;
for (i = 0; i < from->size; i++)
dst[i] = VERY_SECURE_ENCRYPTION(src[i]);
to->size = from->size;
return 0;
}
static int bitflip_filter_stream(
git_writestream **out,
git_filter *self,
void **payload,
const git_filter_source *src,
git_writestream *next)
{
return git_filter_buffered_stream_new(out,
self, bitflip_filter_apply, NULL, payload, src, next);
}
static void bitflip_filter_free(git_filter *f)
{
git__free(f);
}
git_filter *create_bitflip_filter(void)
{
git_filter *filter = git__calloc(1, sizeof(git_filter));
cl_assert(filter);
filter->version = GIT_FILTER_VERSION;
filter->attributes = "+bitflip";
filter->shutdown = bitflip_filter_free;
filter->stream = bitflip_filter_stream;
return filter;
}
int reverse_filter_apply(
git_filter *self,
void **payload,
git_str *to,
const git_str *from,
const git_filter_source *source)
{
const unsigned char *src = (const unsigned char *)from->ptr;
const unsigned char *end = src + from->size;
unsigned char *dst;
GIT_UNUSED(self); GIT_UNUSED(payload); GIT_UNUSED(source);
/* verify that attribute path match worked as expected */
cl_assert_equal_i(
0, git__strncmp("hero", git_filter_source_path(source), 4));
if (!from->size)
return 0;
cl_git_pass(git_str_grow(to, from->size));
dst = (unsigned char *)to->ptr + from->size - 1;
while (src < end)
*dst-- = *src++;
to->size = from->size;
return 0;
}
static int reverse_filter_stream(
git_writestream **out,
git_filter *self,
void **payload,
const git_filter_source *src,
git_writestream *next)
{
return git_filter_buffered_stream_new(out,
self, reverse_filter_apply, NULL, payload, src, next);
}
static void reverse_filter_free(git_filter *f)
{
git__free(f);
}
git_filter *create_reverse_filter(const char *attrs)
{
git_filter *filter = git__calloc(1, sizeof(git_filter));
cl_assert(filter);
filter->version = GIT_FILTER_VERSION;
filter->attributes = attrs;
filter->shutdown = reverse_filter_free;
filter->stream = reverse_filter_stream;
return filter;
}
static int erroneous_filter_stream(
git_writestream **out,
git_filter *self,
void **payload,
const git_filter_source *src,
git_writestream *next)
{
GIT_UNUSED(out);
GIT_UNUSED(self);
GIT_UNUSED(payload);
GIT_UNUSED(src);
GIT_UNUSED(next);
return -1;
}
static void erroneous_filter_free(git_filter *f)
{
git__free(f);
}
git_filter *create_erroneous_filter(const char *attrs)
{
git_filter *filter = git__calloc(1, sizeof(git_filter));
cl_assert(filter);
filter->version = GIT_FILTER_VERSION;
filter->attributes = attrs;
filter->stream = erroneous_filter_stream;
filter->shutdown = erroneous_filter_free;
return filter;
}
| libgit2-main | tests/libgit2/filter/custom_helpers.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
#include "filter.h"
#include "git2/sys/filter.h"
#include "git2/sys/repository.h"
#include "custom_helpers.h"
/* going TO_WORKDIR, filters are executed low to high
* going TO_ODB, filters are executed high to low
*/
#define BITFLIP_FILTER_PRIORITY -1
#define REVERSE_FILTER_PRIORITY -2
#ifdef GIT_WIN32
# define NEWLINE "\r\n"
#else
# define NEWLINE "\n"
#endif
static char workdir_data[] =
"some simple" NEWLINE
"data" NEWLINE
"that will be" NEWLINE
"trivially" NEWLINE
"scrambled." NEWLINE;
#define REVERSED_DATA_LEN 51
/* Represents the data above scrambled (bits flipped) after \r\n -> \n
* conversion, then bytewise reversed
*/
static unsigned char bitflipped_and_reversed_data[] =
{ 0xf5, 0xd1, 0x9b, 0x9a, 0x93, 0x9d, 0x92, 0x9e, 0x8d, 0x9c, 0x8c,
0xf5, 0x86, 0x93, 0x93, 0x9e, 0x96, 0x89, 0x96, 0x8d, 0x8b, 0xf5,
0x9a, 0x9d, 0xdf, 0x93, 0x93, 0x96, 0x88, 0xdf, 0x8b, 0x9e, 0x97,
0x8b, 0xf5, 0x9e, 0x8b, 0x9e, 0x9b, 0xf5, 0x9a, 0x93, 0x8f, 0x92,
0x96, 0x8c, 0xdf, 0x9a, 0x92, 0x90, 0x8c };
#define BITFLIPPED_AND_REVERSED_DATA_LEN 51
static git_repository *g_repo = NULL;
static void register_custom_filters(void);
void test_filter_custom__initialize(void)
{
register_custom_filters();
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_mkfile(
"empty_standard_repo/.gitattributes",
"hero* bitflip reverse\n"
"herofile text\n"
"heroflip -reverse binary\n"
"villain erroneous\n"
"*.bin binary\n");
}
void test_filter_custom__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static void register_custom_filters(void)
{
static int filters_registered = 0;
if (!filters_registered) {
cl_git_pass(git_filter_register(
"bitflip", create_bitflip_filter(), BITFLIP_FILTER_PRIORITY));
cl_git_pass(git_filter_register(
"reverse", create_reverse_filter("+reverse"),
REVERSE_FILTER_PRIORITY));
/* re-register reverse filter with standard filter=xyz priority */
cl_git_pass(git_filter_register(
"pre-reverse",
create_reverse_filter("+prereverse"),
GIT_FILTER_DRIVER_PRIORITY));
cl_git_pass(git_filter_register(
"erroneous",
create_erroneous_filter("+erroneous"),
GIT_FILTER_DRIVER_PRIORITY));
filters_registered = 1;
}
}
void test_filter_custom__to_odb(void)
{
git_filter_list *fl;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_ODB, 0));
in = workdir_data;
in_len = strlen(workdir_data);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_i(BITFLIPPED_AND_REVERSED_DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(bitflipped_and_reversed_data, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_custom__to_workdir(void)
{
git_filter_list *fl;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
in = (char *)bitflipped_and_reversed_data;
in_len = BITFLIPPED_AND_REVERSED_DATA_LEN;
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_i(strlen(workdir_data), out.size);
cl_assert_equal_i(
0, memcmp(workdir_data, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_custom__can_register_a_custom_filter_in_the_repository(void)
{
git_filter_list *fl;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "herofile", GIT_FILTER_TO_WORKTREE, 0));
/* expect: bitflip, reverse, crlf */
cl_assert_equal_sz(3, git_filter_list_length(fl));
git_filter_list_free(fl);
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "herocorp", GIT_FILTER_TO_WORKTREE, 0));
/* expect: bitflip, reverse - possibly crlf depending on global config */
{
size_t flen = git_filter_list_length(fl);
cl_assert(flen == 2 || flen == 3);
}
git_filter_list_free(fl);
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "hero.bin", GIT_FILTER_TO_WORKTREE, 0));
/* expect: bitflip, reverse */
cl_assert_equal_sz(2, git_filter_list_length(fl));
git_filter_list_free(fl);
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "heroflip", GIT_FILTER_TO_WORKTREE, 0));
/* expect: bitflip (because of -reverse) */
cl_assert_equal_sz(1, git_filter_list_length(fl));
git_filter_list_free(fl);
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "doesntapplytome.bin",
GIT_FILTER_TO_WORKTREE, 0));
/* expect: none */
cl_assert_equal_sz(0, git_filter_list_length(fl));
git_filter_list_free(fl);
}
void test_filter_custom__order_dependency(void)
{
git_index *index;
git_blob *blob;
git_buf buf = { 0 };
/* so if ident and reverse are used together, an interesting thing
* happens - a reversed "$Id$" string is no longer going to trigger
* ident correctly. When checking out, the filters should be applied
* in order CLRF, then ident, then reverse, so ident expansion should
* work correctly. On check in, the content should be reversed, then
* ident, then CRLF filtered. Let's make sure that works...
*/
cl_git_mkfile(
"empty_standard_repo/.gitattributes",
"hero.*.rev-ident text ident prereverse eol=lf\n");
cl_git_mkfile(
"empty_standard_repo/hero.1.rev-ident",
"This is a test\n$Id$\nHave fun!\n");
cl_git_mkfile(
"empty_standard_repo/hero.2.rev-ident",
"Another test\n$dI$\nCrazy!\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "hero.1.rev-ident"));
cl_git_pass(git_index_add_bypath(index, "hero.2.rev-ident"));
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Filter chains\n");
git_index_free(index);
cl_git_pass(git_blob_lookup(&blob, g_repo,
& git_index_get_bypath(index, "hero.1.rev-ident", 0)->id));
cl_assert_equal_s(
"\n!nuf evaH\n$dI$\ntset a si sihT", git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "hero.1.rev-ident", NULL));
/* no expansion because id was reversed at checkin and now at ident
* time, reverse is not applied yet */
cl_assert_equal_s(
"This is a test\n$Id$\nHave fun!\n", buf.ptr);
git_blob_free(blob);
cl_git_pass(git_blob_lookup(&blob, g_repo,
& git_index_get_bypath(index, "hero.2.rev-ident", 0)->id));
cl_assert_equal_s(
"\n!yzarC\n$Id$\ntset rehtonA", git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "hero.2.rev-ident", NULL));
/* expansion because reverse was applied at checkin and at ident time,
* reverse is not applied yet */
cl_assert_equal_s(
"Another test\n$ 59001fe193103b1016b27027c0c827d036fd0ac8 :dI$\nCrazy!\n", buf.ptr);
cl_assert_equal_i(0, git_oid_strcmp(
git_blob_id(blob), "8ca0df630d728c0c72072b6101b301391ef10095"));
git_blob_free(blob);
git_buf_dispose(&buf);
}
void test_filter_custom__filter_registry_failure_cases(void)
{
git_filter fake = { GIT_FILTER_VERSION, 0 };
cl_assert_equal_i(GIT_EEXISTS, git_filter_register("bitflip", &fake, 0));
cl_git_fail(git_filter_unregister(GIT_FILTER_CRLF));
cl_git_fail(git_filter_unregister(GIT_FILTER_IDENT));
cl_assert_equal_i(GIT_ENOTFOUND, git_filter_unregister("not-a-filter"));
}
void test_filter_custom__erroneous_filter_fails(void)
{
git_filter_list *filters;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_git_pass(git_filter_list_load(
&filters, g_repo, NULL, "villain", GIT_FILTER_TO_WORKTREE, 0));
in = workdir_data;
in_len = strlen(workdir_data);
cl_git_fail(git_filter_list_apply_to_buffer(&out, filters, in, in_len));
git_filter_list_free(filters);
git_buf_dispose(&out);
}
| libgit2-main | tests/libgit2/filter/custom.c |
#include "clar_libgit2.h"
#include "crlf.h"
#include "path.h"
static git_repository *g_repo = NULL;
static git_str system_attr_path = GIT_STR_INIT;
void test_filter_systemattrs__initialize(void)
{
git_buf system_path = GIT_BUF_INIT;
g_repo = cl_git_sandbox_init("crlf");
cl_must_pass(p_unlink("crlf/.gitattributes"));
cl_git_pass(git_libgit2_opts(
GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, &system_path));
cl_git_pass(git_str_joinpath(&system_attr_path,
system_path.ptr, "gitattributes"));
cl_git_mkfile(system_attr_path.ptr,
"*.txt text\n"
"*.bin binary\n"
"*.crlf text eol=crlf\n"
"*.lf text eol=lf\n");
git_buf_dispose(&system_path);
}
void test_filter_systemattrs__cleanup(void)
{
cl_must_pass(p_unlink(system_attr_path.ptr));
git_str_dispose(&system_attr_path);
cl_git_sandbox_cleanup();
}
void test_filter_systemattrs__reads_system_attributes(void)
{
git_blob *blob;
git_buf buf = { 0 };
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "799770d")); /* all-lf */
cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
cl_assert_equal_s(ALL_LF_TEXT_AS_CRLF, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
cl_assert_equal_s(ALL_LF_TEXT_AS_LF, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_systemattrs__disables_system_attributes(void)
{
git_blob *blob;
git_buf buf = { 0 };
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
opts.flags |= GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES;
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "799770d")); /* all-lf */
cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", &opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
/* No attributes mean these are all treated literally */
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", &opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", &opts));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
| libgit2-main | tests/libgit2/filter/systemattrs.c |
#include "clar_libgit2.h"
#include "git2/sys/filter.h"
#include "crlf.h"
static git_repository *g_repo = NULL;
void test_filter_query__initialize(void)
{
g_repo = cl_git_sandbox_init("crlf");
cl_git_mkfile("crlf/.gitattributes",
"*.txt text\n"
"*.bin binary\n"
"*.crlf text eol=crlf\n"
"*.lf text eol=lf\n"
"*.binident binary ident\n"
"*.ident text ident\n"
"*.identcrlf ident text eol=crlf\n"
"*.identlf ident text eol=lf\n"
"*.custom custom ident text\n");
}
void test_filter_query__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static int filter_for(const char *filename, const char *filter)
{
git_filter_list *fl;
int filtered;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, filename, GIT_FILTER_TO_WORKTREE, 0));
filtered = git_filter_list_contains(fl, filter);
git_filter_list_free(fl);
return filtered;
}
void test_filter_query__filters(void)
{
cl_assert_equal_i(1, filter_for("text.txt", "crlf"));
cl_assert_equal_i(0, filter_for("binary.bin", "crlf"));
cl_assert_equal_i(1, filter_for("foo.lf", "crlf"));
cl_assert_equal_i(0, filter_for("foo.lf", "ident"));
cl_assert_equal_i(1, filter_for("id.ident", "crlf"));
cl_assert_equal_i(1, filter_for("id.ident", "ident"));
cl_assert_equal_i(0, filter_for("id.binident", "crlf"));
cl_assert_equal_i(1, filter_for("id.binident", "ident"));
}
void test_filter_query__autocrlf_true_implies_crlf(void)
{
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_assert_equal_i(1, filter_for("not_in_gitattributes", "crlf"));
cl_assert_equal_i(1, filter_for("foo.txt", "crlf"));
cl_assert_equal_i(0, filter_for("foo.bin", "crlf"));
cl_assert_equal_i(1, filter_for("foo.lf", "crlf"));
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_assert_equal_i(0, filter_for("not_in_gitattributes", "crlf"));
cl_assert_equal_i(1, filter_for("foo.txt", "crlf"));
cl_assert_equal_i(0, filter_for("foo.bin", "crlf"));
cl_assert_equal_i(1, filter_for("foo.lf", "crlf"));
}
void test_filter_query__unknown(void)
{
cl_assert_equal_i(1, filter_for("foo.custom", "crlf"));
cl_assert_equal_i(1, filter_for("foo.custom", "ident"));
cl_assert_equal_i(0, filter_for("foo.custom", "custom"));
}
void test_filter_query__custom(void)
{
git_filter custom = { GIT_FILTER_VERSION };
cl_git_pass(git_filter_register(
"custom", &custom, 42));
cl_assert_equal_i(1, filter_for("foo.custom", "crlf"));
cl_assert_equal_i(1, filter_for("foo.custom", "ident"));
cl_assert_equal_i(1, filter_for("foo.custom", "custom"));
git_filter_unregister("custom");
}
| libgit2-main | tests/libgit2/filter/query.c |
#include "clar_libgit2.h"
#include "git2/sys/filter.h"
#include "crlf.h"
static git_repository *g_repo = NULL;
void test_filter_file__initialize(void)
{
git_reference *head_ref;
git_commit *head;
g_repo = cl_git_sandbox_init("crlf");
cl_git_mkfile("crlf/.gitattributes",
"*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_repository_head(&head_ref, g_repo));
cl_git_pass(git_reference_peel((git_object **)&head, head_ref, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(g_repo, (git_object *)head, GIT_RESET_HARD, NULL));
git_commit_free(head);
git_reference_free(head_ref);
}
void test_filter_file__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_filter_file__apply(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
cl_git_pass(git_filter_list_apply_to_file(&buf, fl, g_repo, "all-crlf"));
cl_assert_equal_s("crlf\ncrlf\ncrlf\ncrlf\n", buf.ptr);
git_buf_dispose(&buf);
git_filter_list_free(fl);
}
struct buf_writestream {
git_writestream base;
git_str buf;
};
static int buf_writestream_write(git_writestream *s, const char *buf, size_t len)
{
struct buf_writestream *stream = (struct buf_writestream *)s;
return git_str_put(&stream->buf, buf, len);
}
static int buf_writestream_close(git_writestream *s)
{
GIT_UNUSED(s);
return 0;
}
static void buf_writestream_free(git_writestream *s)
{
struct buf_writestream *stream = (struct buf_writestream *)s;
git_str_dispose(&stream->buf);
}
void test_filter_file__apply_stream(void)
{
git_filter_list *fl;
git_filter *crlf;
struct buf_writestream write_target = { {
buf_writestream_write,
buf_writestream_close,
buf_writestream_free } };
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
cl_git_pass(git_filter_list_stream_file(fl, g_repo, "all-crlf", &write_target.base));
cl_assert_equal_s("crlf\ncrlf\ncrlf\ncrlf\n", write_target.buf.ptr);
git_filter_list_free(fl);
write_target.base.free(&write_target.base);
}
| libgit2-main | tests/libgit2/filter/file.c |
#include "clar_libgit2.h"
#include "git2/sys/filter.h"
static git_repository *g_repo = NULL;
void test_filter_crlf__initialize(void)
{
g_repo = cl_git_sandbox_init("crlf");
cl_git_mkfile("crlf/.gitattributes",
"*.txt text\n*.bin binary\n*.crlf text eol=crlf\n*.lf text eol=lf\n");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
}
void test_filter_crlf__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_filter_crlf__to_worktree(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
in = "Some text\nRight here\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Some text\r\nRight here\r\n", out.ptr);
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_crlf__to_odb(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
in = "Some text\r\nRight here\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Some text\nRight here\n", out.ptr);
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_crlf__with_safecrlf(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_repo_set_bool(g_repo, "core.safecrlf", true);
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
/* Normalized \r\n succeeds with safecrlf */
in = "Normal\r\nCRLF\r\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
/* Mix of line endings fails with safecrlf */
in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
in_len = strlen(in);
cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER);
/* Normalized \n fails for autocrlf=true when safecrlf=true */
in = "Normal\nLF\nonly\nline-endings.\n";
in_len = strlen(in);
cl_git_fail(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_FILTER);
/* String with \r but without \r\n does not fail with safecrlf */
in = "Normal\nCR only\rand some more\nline-endings.\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Normal\nCR only\rand some more\nline-endings.\n", out.ptr);
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_crlf__with_safecrlf_and_unsafe_allowed(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_repo_set_bool(g_repo, "core.safecrlf", true);
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
/* Normalized \r\n succeeds with safecrlf */
in = "Normal\r\nCRLF\r\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
/* Mix of line endings fails with safecrlf, but allowed to pass */
in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
/* TODO: check for warning */
cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
/* Normalized \n fails with safecrlf, but allowed to pass */
in = "Normal\nLF\nonly\nline-endings.\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
/* TODO: check for warning */
cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_crlf__no_safecrlf(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
/* Normalized \r\n succeeds with safecrlf */
in = "Normal\r\nCRLF\r\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
/* Mix of line endings fails with safecrlf */
in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
/* Normalized \n fails with safecrlf */
in = "Normal\nLF\nonly\nline-endings.\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Normal\nLF\nonly\nline-endings.\n", out.ptr);
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_crlf__safecrlf_warn(void)
{
git_filter_list *fl;
git_filter *crlf;
git_buf out = GIT_BUF_INIT;
const char *in;
size_t in_len;
cl_repo_set_string(g_repo, "core.safecrlf", "warn");
cl_git_pass(git_filter_list_new(
&fl, g_repo, GIT_FILTER_TO_ODB, 0));
crlf = git_filter_lookup(GIT_FILTER_CRLF);
cl_assert(crlf != NULL);
cl_git_pass(git_filter_list_push(fl, crlf, NULL));
/* Normalized \r\n succeeds with safecrlf=warn */
in = "Normal\r\nCRLF\r\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s("Normal\nCRLF\nline-endings.\n", out.ptr);
/* Mix of line endings succeeds with safecrlf=warn */
in = "Mixed\nup\r\nLF\nand\r\nCRLF\nline-endings.\r\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
/* TODO: check for warning */
cl_assert_equal_s("Mixed\nup\nLF\nand\nCRLF\nline-endings.\n", out.ptr);
/* Normalized \n is reversible, so does not fail with safecrlf=warn */
in = "Normal\nLF\nonly\nline-endings.\n";
in_len = strlen(in);
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, in, in_len));
cl_assert_equal_s(in, out.ptr);
git_filter_list_free(fl);
git_buf_dispose(&out);
}
| libgit2-main | tests/libgit2/filter/crlf.c |
#include "clar_libgit2.h"
#include "crlf.h"
static git_repository *g_repo = NULL;
void test_filter_blob__initialize(void)
{
g_repo = cl_git_sandbox_init("crlf");
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");
}
void test_filter_blob__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_filter_blob__all_crlf(void)
{
git_blob *blob;
git_buf buf = { 0 };
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */
cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
/* in this case, raw content has crlf in it already */
cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
/* we never convert CRLF -> LF on platforms that have LF */
cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_blob__from_lf(void)
{
git_blob *blob;
git_buf buf = { 0 };
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "799770d")); /* all-lf */
cl_assert_equal_s(ALL_LF_TEXT_RAW, git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
cl_assert_equal_s(ALL_LF_TEXT_RAW, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
/* in this case, raw content has crlf in it already */
cl_assert_equal_s(ALL_LF_TEXT_AS_CRLF, buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
/* we never convert CRLF -> LF on platforms that have LF */
cl_assert_equal_s(ALL_LF_TEXT_AS_LF, buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_blob__sanitizes(void)
{
git_blob *blob;
git_buf buf;
cl_git_pass(git_revparse_single(
(git_object **)&blob, g_repo, "e69de29")); /* zero-byte */
cl_assert_equal_i(0, git_blob_rawsize(blob));
cl_assert_equal_s("", git_blob_rawcontent(blob));
memset(&buf, 0, sizeof(git_buf));
cl_git_pass(git_blob_filter(&buf, blob, "file.bin", NULL));
cl_assert_equal_sz(0, buf.size);
cl_assert_equal_s("", buf.ptr);
git_buf_dispose(&buf);
memset(&buf, 0, sizeof(git_buf));
cl_git_pass(git_blob_filter(&buf, blob, "file.crlf", NULL));
cl_assert_equal_sz(0, buf.size);
cl_assert_equal_s("", buf.ptr);
git_buf_dispose(&buf);
memset(&buf, 0, sizeof(git_buf));
cl_git_pass(git_blob_filter(&buf, blob, "file.lf", NULL));
cl_assert_equal_sz(0, buf.size);
cl_assert_equal_s("", buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
void test_filter_blob__ident(void)
{
git_oid id;
git_blob *blob;
git_buf buf = { 0 };
cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
cl_assert_equal_s(
"Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
git_blob_free(blob);
cl_git_mkfile("crlf/test.ident", "Some text\n$Id: Any old just you want$\nGoes there\n");
cl_git_pass(git_blob_create_from_workdir(&id, g_repo, "test.ident"));
cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
cl_assert_equal_s(
"Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
cl_git_pass(git_blob_filter(&buf, blob, "filter.bin", NULL));
cl_assert_equal_s(
"Some text\n$Id$\nGoes there\n", buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "filter.identcrlf", NULL));
cl_assert_equal_s(
"Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\r\nGoes there\r\n", buf.ptr);
cl_git_pass(git_blob_filter(&buf, blob, "filter.identlf", NULL));
cl_assert_equal_s(
"Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845 $\nGoes there\n", buf.ptr);
git_buf_dispose(&buf);
git_blob_free(blob);
}
| libgit2-main | tests/libgit2/filter/blob.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
#include "filter.h"
#include "git2/sys/filter.h"
#include "git2/sys/repository.h"
static git_repository *g_repo = NULL;
static git_filter *create_compress_filter(void);
static git_filter *compress_filter;
void test_filter_stream__initialize(void)
{
compress_filter = create_compress_filter();
cl_git_pass(git_filter_register("compress", compress_filter, 50));
g_repo = cl_git_sandbox_init("empty_standard_repo");
}
void test_filter_stream__cleanup(void)
{
cl_git_sandbox_cleanup();
g_repo = NULL;
git_filter_unregister("compress");
git__free(compress_filter);
}
#define CHUNKSIZE 10240
struct compress_stream {
git_writestream parent;
git_writestream *next;
git_filter_mode_t mode;
char current;
size_t current_chunk;
};
static int compress_stream_write__deflated(struct compress_stream *stream, const char *buffer, size_t len)
{
size_t idx = 0;
while (len > 0) {
size_t chunkremain, chunksize;
if (stream->current_chunk == 0)
stream->current = buffer[idx];
chunkremain = CHUNKSIZE - stream->current_chunk;
chunksize = min(chunkremain, len);
stream->current_chunk += chunksize;
len -= chunksize;
idx += chunksize;
if (stream->current_chunk == CHUNKSIZE) {
cl_git_pass(stream->next->write(stream->next, &stream->current, 1));
stream->current_chunk = 0;
}
}
return 0;
}
static int compress_stream_write__inflated(struct compress_stream *stream, const char *buffer, size_t len)
{
char inflated[CHUNKSIZE];
size_t i, j;
for (i = 0; i < len; i++) {
for (j = 0; j < CHUNKSIZE; j++)
inflated[j] = buffer[i];
cl_git_pass(stream->next->write(stream->next, inflated, CHUNKSIZE));
}
return 0;
}
static int compress_stream_write(git_writestream *s, const char *buffer, size_t len)
{
struct compress_stream *stream = (struct compress_stream *)s;
return (stream->mode == GIT_FILTER_TO_ODB) ?
compress_stream_write__deflated(stream, buffer, len) :
compress_stream_write__inflated(stream, buffer, len);
}
static int compress_stream_close(git_writestream *s)
{
struct compress_stream *stream = (struct compress_stream *)s;
cl_assert_equal_i(0, stream->current_chunk);
stream->next->close(stream->next);
return 0;
}
static void compress_stream_free(git_writestream *stream)
{
git__free(stream);
}
static int compress_filter_stream_init(
git_writestream **out,
git_filter *self,
void **payload,
const git_filter_source *src,
git_writestream *next)
{
struct compress_stream *stream = git__calloc(1, sizeof(struct compress_stream));
cl_assert(stream);
GIT_UNUSED(self);
GIT_UNUSED(payload);
stream->parent.write = compress_stream_write;
stream->parent.close = compress_stream_close;
stream->parent.free = compress_stream_free;
stream->next = next;
stream->mode = git_filter_source_mode(src);
*out = (git_writestream *)stream;
return 0;
}
git_filter *create_compress_filter(void)
{
git_filter *filter = git__calloc(1, sizeof(git_filter));
cl_assert(filter);
filter->version = GIT_FILTER_VERSION;
filter->attributes = "+compress";
filter->stream = compress_filter_stream_init;
return filter;
}
static void writefile(const char *filename, size_t numchunks)
{
git_str path = GIT_STR_INIT;
char buf[CHUNKSIZE];
size_t i = 0, j = 0;
int fd;
cl_git_pass(git_str_joinpath(&path, "empty_standard_repo", filename));
fd = p_open(path.ptr, O_RDWR|O_CREAT, 0666);
cl_assert(fd >= 0);
for (i = 0; i < numchunks; i++) {
for (j = 0; j < CHUNKSIZE; j++) {
buf[j] = i % 256;
}
cl_git_pass(p_write(fd, buf, CHUNKSIZE));
}
p_close(fd);
git_str_dispose(&path);
}
static void test_stream(size_t numchunks)
{
git_index *index;
const git_index_entry *entry;
git_blob *blob;
struct stat st;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile(
"empty_standard_repo/.gitattributes",
"* compress\n");
/* write a file to disk */
writefile("streamed_file", numchunks);
/* place it in the index */
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "streamed_file"));
cl_git_pass(git_index_write(index));
/* ensure it was appropriately compressed */
cl_assert(entry = git_index_get_bypath(index, "streamed_file", 0));
cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
cl_assert_equal_i(numchunks, git_blob_rawsize(blob));
/* check the file back out */
cl_must_pass(p_unlink("empty_standard_repo/streamed_file"));
cl_git_pass(git_checkout_index(g_repo, index, &checkout_opts));
/* ensure it was decompressed */
cl_must_pass(p_stat("empty_standard_repo/streamed_file", &st));
cl_assert_equal_sz((numchunks * CHUNKSIZE), st.st_size);
git_index_free(index);
git_blob_free(blob);
}
/* write a 50KB file through the "compression" stream */
void test_filter_stream__smallfile(void)
{
test_stream(5);
}
/* optionally write a 500 MB file through the compression stream */
void test_filter_stream__bigfile(void)
{
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE"))
cl_skip();
test_stream(51200);
}
| libgit2-main | tests/libgit2/filter/stream.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "blob.h"
#include "filter.h"
#include "git2/sys/filter.h"
#include "git2/sys/repository.h"
#include "custom_helpers.h"
static git_repository *g_repo = NULL;
static git_filter *create_wildcard_filter(void);
#define DATA_LEN 32
static unsigned char input[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};
static unsigned char reversed[] = {
0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
};
static unsigned char flipped[] = {
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0,
};
void test_filter_wildcard__initialize(void)
{
cl_git_pass(git_filter_register(
"wildcard", create_wildcard_filter(), GIT_FILTER_DRIVER_PRIORITY));
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_rewritefile(
"empty_standard_repo/.gitattributes",
"* binary\n"
"hero-flip-* filter=wcflip\n"
"hero-reverse-* filter=wcreverse\n"
"none-* filter=unregistered\n");
}
void test_filter_wildcard__cleanup(void)
{
cl_git_pass(git_filter_unregister("wildcard"));
cl_git_sandbox_cleanup();
g_repo = NULL;
}
static int wildcard_filter_check(
git_filter *self,
void **payload,
const git_filter_source *src,
const char **attr_values)
{
GIT_UNUSED(self);
GIT_UNUSED(src);
if (strcmp(attr_values[0], "wcflip") == 0 ||
strcmp(attr_values[0], "wcreverse") == 0) {
*payload = git__strdup(attr_values[0]);
GIT_ERROR_CHECK_ALLOC(*payload);
return 0;
}
return GIT_PASSTHROUGH;
}
static int wildcard_filter_apply(
git_filter *self,
void **payload,
git_str *to,
const git_str *from,
const git_filter_source *source)
{
const char *filtername = *payload;
if (filtername && strcmp(filtername, "wcflip") == 0)
return bitflip_filter_apply(self, payload, to, from, source);
else if (filtername && strcmp(filtername, "wcreverse") == 0)
return reverse_filter_apply(self, payload, to, from, source);
cl_fail("Unexpected attribute");
return GIT_PASSTHROUGH;
}
static int wildcard_filter_stream(
git_writestream **out,
git_filter *self,
void **payload,
const git_filter_source *src,
git_writestream *next)
{
return git_filter_buffered_stream_new(out,
self, wildcard_filter_apply, NULL, payload, src, next);
}
static void wildcard_filter_cleanup(git_filter *self, void *payload)
{
GIT_UNUSED(self);
git__free(payload);
}
static void wildcard_filter_free(git_filter *f)
{
git__free(f);
}
static git_filter *create_wildcard_filter(void)
{
git_filter *filter = git__calloc(1, sizeof(git_filter));
cl_assert(filter);
filter->version = GIT_FILTER_VERSION;
filter->attributes = "filter=*";
filter->check = wildcard_filter_check;
filter->stream = wildcard_filter_stream;
filter->cleanup = wildcard_filter_cleanup;
filter->shutdown = wildcard_filter_free;
return filter;
}
void test_filter_wildcard__reverse(void)
{
git_filter_list *fl;
git_buf out = GIT_BUF_INIT;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "hero-reverse-foo", GIT_FILTER_TO_ODB, 0));
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
cl_assert_equal_i(DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(reversed, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_wildcard__flip(void)
{
git_filter_list *fl;
git_buf out = GIT_BUF_INIT;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "hero-flip-foo", GIT_FILTER_TO_ODB, 0));
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
cl_assert_equal_i(DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(flipped, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_dispose(&out);
}
void test_filter_wildcard__none(void)
{
git_filter_list *fl;
git_buf out = GIT_BUF_INIT;
cl_git_pass(git_filter_list_load(
&fl, g_repo, NULL, "none-foo", GIT_FILTER_TO_ODB, 0));
cl_git_pass(git_filter_list_apply_to_buffer(&out, fl, (char *)input, DATA_LEN));
cl_assert_equal_i(DATA_LEN, out.size);
cl_assert_equal_i(
0, memcmp(input, out.ptr, out.size));
git_filter_list_free(fl);
git_buf_dispose(&out);
}
| libgit2-main | tests/libgit2/filter/wildcard.c |
#include "clar_libgit2.h"
#include "futils.h"
static git_repository *_repo;
static int counter;
static char *_remote_proxy_scheme = NULL;
static char *_remote_proxy_host = NULL;
static char *_remote_proxy_user = NULL;
static char *_remote_proxy_pass = NULL;
static char *_remote_redirect_initial = NULL;
static char *_remote_redirect_subsequent = NULL;
void test_online_fetch__initialize(void)
{
cl_git_pass(git_repository_init(&_repo, "./fetch", 0));
_remote_proxy_scheme = cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
_remote_proxy_host = cl_getenv("GITTEST_REMOTE_PROXY_HOST");
_remote_proxy_user = cl_getenv("GITTEST_REMOTE_PROXY_USER");
_remote_proxy_pass = cl_getenv("GITTEST_REMOTE_PROXY_PASS");
_remote_redirect_initial = cl_getenv("GITTEST_REMOTE_REDIRECT_INITIAL");
_remote_redirect_subsequent = cl_getenv("GITTEST_REMOTE_REDIRECT_SUBSEQUENT");
}
void test_online_fetch__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
cl_fixture_cleanup("./fetch");
cl_fixture_cleanup("./redirected");
git__free(_remote_proxy_scheme);
git__free(_remote_proxy_host);
git__free(_remote_proxy_user);
git__free(_remote_proxy_pass);
git__free(_remote_redirect_initial);
git__free(_remote_redirect_subsequent);
}
static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); GIT_UNUSED(data);
++counter;
return 0;
}
static int progress(const git_indexer_progress *stats, void *payload)
{
size_t *bytes_received = (size_t *)payload;
*bytes_received = stats->received_bytes;
return 0;
}
static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
{
git_remote *remote;
git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
size_t bytes_received = 0;
options.callbacks.transfer_progress = progress;
options.callbacks.update_tips = update_tips;
options.callbacks.payload = &bytes_received;
options.download_tags = flag;
counter = 0;
cl_git_pass(git_remote_create(&remote, _repo, "test", url));
cl_git_pass(git_remote_fetch(remote, NULL, &options, NULL));
cl_assert_equal_i(counter, n);
cl_assert(bytes_received > 0);
git_remote_free(remote);
}
void test_online_fetch__default_http(void)
{
do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
}
void test_online_fetch__default_https(void)
{
do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
}
void test_online_fetch__no_tags_git(void)
{
do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}
void test_online_fetch__no_tags_http(void)
{
do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}
void test_online_fetch__fetch_twice(void)
{
git_remote *remote;
cl_git_pass(git_remote_create(&remote, _repo, "test", "https://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
cl_git_pass(git_remote_download(remote, NULL, NULL));
git_remote_disconnect(remote);
git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL);
cl_git_pass(git_remote_download(remote, NULL, NULL));
git_remote_disconnect(remote);
git_remote_free(remote);
}
static int transferProgressCallback(const git_indexer_progress *stats, void *payload)
{
bool *invoked = (bool *)payload;
GIT_UNUSED(stats);
*invoked = true;
return 0;
}
void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
{
git_repository *_repository;
bool invoked = false;
git_remote *remote;
git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
opts.bare = true;
cl_git_pass(git_clone(&_repository, "https://github.com/libgit2/TestGitRepository.git",
"./fetch/lg2", &opts));
git_repository_free(_repository);
cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));
cl_git_pass(git_remote_lookup(&remote, _repository, "origin"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
cl_assert_equal_i(false, invoked);
options.callbacks.transfer_progress = &transferProgressCallback;
options.callbacks.payload = &invoked;
cl_git_pass(git_remote_download(remote, NULL, &options));
cl_assert_equal_i(false, invoked);
cl_git_pass(git_remote_update_tips(remote, &options.callbacks, 1, options.download_tags, NULL));
git_remote_disconnect(remote);
git_remote_free(remote);
git_repository_free(_repository);
}
static int cancel_at_half(const git_indexer_progress *stats, void *payload)
{
GIT_UNUSED(payload);
if (stats->received_objects > (stats->total_objects/2))
return -4321;
return 0;
}
void test_online_fetch__can_cancel(void)
{
git_remote *remote;
size_t bytes_received = 0;
git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
cl_git_pass(git_remote_create(&remote, _repo, "test",
"http://github.com/libgit2/TestGitRepository.git"));
options.callbacks.transfer_progress = cancel_at_half;
options.callbacks.payload = &bytes_received;
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
cl_git_fail_with(git_remote_download(remote, NULL, &options), -4321);
git_remote_disconnect(remote);
git_remote_free(remote);
}
void test_online_fetch__ls_disconnected(void)
{
const git_remote_head **refs;
size_t refs_len_before, refs_len_after;
git_remote *remote;
cl_git_pass(git_remote_create(&remote, _repo, "test",
"http://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
cl_git_pass(git_remote_ls(&refs, &refs_len_before, remote));
git_remote_disconnect(remote);
cl_git_pass(git_remote_ls(&refs, &refs_len_after, remote));
cl_assert_equal_i(refs_len_before, refs_len_after);
git_remote_free(remote);
}
void test_online_fetch__remote_symrefs(void)
{
const git_remote_head **refs;
size_t refs_len;
git_remote *remote;
cl_git_pass(git_remote_create(&remote, _repo, "test",
"http://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
git_remote_disconnect(remote);
cl_git_pass(git_remote_ls(&refs, &refs_len, remote));
cl_assert_equal_s("HEAD", refs[0]->name);
cl_assert_equal_s("refs/heads/master", refs[0]->symref_target);
git_remote_free(remote);
}
void test_online_fetch__twice(void)
{
git_remote *remote;
cl_git_pass(git_remote_create(&remote, _repo, "test", "http://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
git_remote_free(remote);
}
void test_online_fetch__proxy(void)
{
git_remote *remote;
git_str url = GIT_STR_INIT;
git_fetch_options fetch_opts;
if (!_remote_proxy_host || !_remote_proxy_user || !_remote_proxy_pass)
cl_skip();
cl_git_pass(git_str_printf(&url, "%s://%s:%s@%s/",
_remote_proxy_scheme ? _remote_proxy_scheme : "http",
_remote_proxy_user, _remote_proxy_pass, _remote_proxy_host));
cl_git_pass(git_fetch_options_init(&fetch_opts, GIT_FETCH_OPTIONS_VERSION));
fetch_opts.proxy_opts.type = GIT_PROXY_SPECIFIED;
fetch_opts.proxy_opts.url = url.ptr;
cl_git_pass(git_remote_create(&remote, _repo, "test", "https://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, &fetch_opts.proxy_opts, NULL));
cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));
git_remote_free(remote);
git_str_dispose(&url);
}
static int do_redirected_fetch(const char *url, const char *name, const char *config)
{
git_repository *repo;
git_remote *remote;
int error;
cl_git_pass(git_repository_init(&repo, "./redirected", 0));
cl_fixture_cleanup(name);
if (config)
cl_repo_set_string(repo, "http.followRedirects", config);
cl_git_pass(git_remote_create(&remote, repo, name, url));
error = git_remote_fetch(remote, NULL, NULL, NULL);
git_remote_free(remote);
git_repository_free(repo);
cl_fixture_cleanup("./redirected");
return error;
}
void test_online_fetch__redirect_config(void)
{
if (!_remote_redirect_initial || !_remote_redirect_subsequent)
cl_skip();
/* config defaults */
cl_git_pass(do_redirected_fetch(_remote_redirect_initial, "initial", NULL));
cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent, "subsequent", NULL));
/* redirect=initial */
cl_git_pass(do_redirected_fetch(_remote_redirect_initial, "initial", "initial"));
cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent, "subsequent", "initial"));
/* redirect=false */
cl_git_fail(do_redirected_fetch(_remote_redirect_initial, "initial", "false"));
cl_git_fail(do_redirected_fetch(_remote_redirect_subsequent, "subsequent", "false"));
}
void test_online_fetch__reachable_commit(void)
{
git_remote *remote;
git_strarray refspecs;
git_object *obj;
git_oid expected_id;
git_str fetchhead = GIT_STR_INIT;
char *refspec = "+2c349335b7f797072cf729c4f3bb0914ecb6dec9:refs/success";
refspecs.strings = &refspec;
refspecs.count = 1;
git_oid__fromstr(&expected_id, "2c349335b7f797072cf729c4f3bb0914ecb6dec9", GIT_OID_SHA1);
cl_git_pass(git_remote_create(&remote, _repo, "test",
"https://github.com/libgit2/TestGitRepository"));
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_s(fetchhead.ptr,
"2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
git_str_dispose(&fetchhead);
git_object_free(obj);
git_remote_free(remote);
}
void test_online_fetch__reachable_commit_without_destination(void)
{
git_remote *remote;
git_strarray refspecs;
git_object *obj;
git_oid expected_id;
git_str fetchhead = GIT_STR_INIT;
char *refspec = "2c349335b7f797072cf729c4f3bb0914ecb6dec9";
refspecs.strings = &refspec;
refspecs.count = 1;
git_oid__fromstr(&expected_id, "2c349335b7f797072cf729c4f3bb0914ecb6dec9", GIT_OID_SHA1);
cl_git_pass(git_remote_create(&remote, _repo, "test",
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL));
cl_git_fail_with(GIT_ENOTFOUND, git_revparse_single(&obj, _repo, "refs/success"));
cl_git_pass(git_futils_readbuffer(&fetchhead, "./fetch/.git/FETCH_HEAD"));
cl_assert_equal_s(fetchhead.ptr,
"2c349335b7f797072cf729c4f3bb0914ecb6dec9\t\t'2c349335b7f797072cf729c4f3bb0914ecb6dec9' of https://github.com/libgit2/TestGitRepository\n");
git_str_dispose(&fetchhead);
git_object_free(obj);
git_remote_free(remote);
}
| libgit2-main | tests/libgit2/online/fetch.c |
#include "clar_libgit2.h"
#include "path.h"
#include "git2/clone.h"
#include "git2/cred_helpers.h"
#include "remote.h"
#include "futils.h"
#include "refs.h"
/*
* Certificate one is in the `certs` folder; certificate two is in the
* `self-signed.pem` file.
*/
#define CUSTOM_CERT_ONE_URL "https://test.libgit2.org:1443/anonymous/test.git"
#define CUSTOM_CERT_ONE_PATH "certs"
#define CUSTOM_CERT_TWO_URL "https://test.libgit2.org:2443/anonymous/test.git"
#define CUSTOM_CERT_TWO_FILE "self-signed.pem"
#if (GIT_OPENSSL || GIT_MBEDTLS)
static git_repository *g_repo;
static int initialized = false;
#endif
void test_online_customcert__initialize(void)
{
#if (GIT_OPENSSL || GIT_MBEDTLS)
g_repo = NULL;
if (!initialized) {
git_str path = GIT_STR_INIT, file = GIT_STR_INIT;
char cwd[GIT_PATH_MAX];
cl_fixture_sandbox(CUSTOM_CERT_ONE_PATH);
cl_fixture_sandbox(CUSTOM_CERT_TWO_FILE);
cl_must_pass(p_getcwd(cwd, GIT_PATH_MAX));
cl_git_pass(git_str_joinpath(&path, cwd, CUSTOM_CERT_ONE_PATH));
cl_git_pass(git_str_joinpath(&file, cwd, CUSTOM_CERT_TWO_FILE));
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS,
file.ptr, path.ptr));
initialized = true;
git_str_dispose(&file);
git_str_dispose(&path);
}
#endif
}
void test_online_customcert__cleanup(void)
{
#if (GIT_OPENSSL || GIT_MBEDTLS)
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
cl_fixture_cleanup("./cloned");
cl_fixture_cleanup(CUSTOM_CERT_ONE_PATH);
cl_fixture_cleanup(CUSTOM_CERT_TWO_FILE);
#endif
}
void test_online_customcert__file(void)
{
#if (GIT_OPENSSL || GIT_MBEDTLS)
cl_git_pass(git_clone(&g_repo, CUSTOM_CERT_ONE_URL, "./cloned", NULL));
cl_assert(git_fs_path_exists("./cloned/master.txt"));
#endif
}
void test_online_customcert__path(void)
{
#if (GIT_OPENSSL || GIT_MBEDTLS)
cl_git_pass(git_clone(&g_repo, CUSTOM_CERT_TWO_URL, "./cloned", NULL));
cl_assert(git_fs_path_exists("./cloned/master.txt"));
#endif
}
| libgit2-main | tests/libgit2/online/customcert.c |
#include "clar_libgit2.h"
#include "posix.h"
#include "vector.h"
#include "../submodule/submodule_helpers.h"
#include "push_util.h"
#include "refspec.h"
#include "remote.h"
static git_repository *_repo;
static char *_remote_url = NULL;
static char *_remote_user = NULL;
static char *_remote_pass = NULL;
static char *_remote_ssh_key = NULL;
static char *_remote_ssh_pubkey = NULL;
static char *_remote_ssh_passphrase = NULL;
static char *_remote_default = NULL;
static char *_remote_expectcontinue = NULL;
static int cred_acquire_cb(git_credential **, const char *, const char *, unsigned int, void *);
static git_remote *_remote;
static record_callbacks_data _record_cbs_data = {{ 0 }};
static git_remote_callbacks _record_cbs = RECORD_CALLBACKS_INIT(&_record_cbs_data);
static git_oid _oid_b6;
static git_oid _oid_b5;
static git_oid _oid_b4;
static git_oid _oid_b3;
static git_oid _oid_b2;
static git_oid _oid_b1;
static git_oid _tag_commit;
static git_oid _tag_tree;
static git_oid _tag_blob;
static git_oid _tag_lightweight;
static git_oid _tag_tag;
static int cred_acquire_cb(
git_credential **cred,
const char *url,
const char *user_from_url,
unsigned int allowed_types,
void *payload)
{
GIT_UNUSED(url);
GIT_UNUSED(user_from_url);
GIT_UNUSED(payload);
if (GIT_CREDENTIAL_USERNAME & allowed_types) {
if (!_remote_user) {
printf("GITTEST_REMOTE_USER must be set\n");
return -1;
}
return git_credential_username_new(cred, _remote_user);
}
if (GIT_CREDENTIAL_DEFAULT & allowed_types) {
if (!_remote_default) {
printf("GITTEST_REMOTE_DEFAULT must be set to use NTLM/Negotiate credentials\n");
return -1;
}
return git_credential_default_new(cred);
}
if (GIT_CREDENTIAL_SSH_KEY & allowed_types) {
if (!_remote_user || !_remote_ssh_pubkey || !_remote_ssh_key || !_remote_ssh_passphrase) {
printf("GITTEST_REMOTE_USER, GITTEST_REMOTE_SSH_PUBKEY, GITTEST_REMOTE_SSH_KEY and GITTEST_REMOTE_SSH_PASSPHRASE must be set\n");
return -1;
}
return git_credential_ssh_key_new(cred, _remote_user, _remote_ssh_pubkey, _remote_ssh_key, _remote_ssh_passphrase);
}
if (GIT_CREDENTIAL_USERPASS_PLAINTEXT & allowed_types) {
if (!_remote_user || !_remote_pass) {
printf("GITTEST_REMOTE_USER and GITTEST_REMOTE_PASS must be set\n");
return -1;
}
return git_credential_userpass_plaintext_new(cred, _remote_user, _remote_pass);
}
return -1;
}
/**
* git_push_status_foreach callback that records status entries.
*/
static int record_push_status_cb(const char *ref, const char *msg, void *payload)
{
record_callbacks_data *data = (record_callbacks_data *) payload;
push_status *s;
cl_assert(s = git__calloc(1, sizeof(*s)));
if (ref)
cl_assert(s->ref = git__strdup(ref));
s->success = (msg == NULL);
if (msg)
cl_assert(s->msg = git__strdup(msg));
git_vector_insert(&data->statuses, s);
return 0;
}
static void do_verify_push_status(record_callbacks_data *data, const push_status expected[], const size_t expected_len)
{
git_vector *actual = &data->statuses;
push_status *iter;
bool failed = false;
size_t i;
if (expected_len != actual->length)
failed = true;
else
git_vector_foreach(actual, i, iter)
if (strcmp(expected[i].ref, iter->ref) ||
(expected[i].success != iter->success) ||
(expected[i].msg && (!iter->msg || strcmp(expected[i].msg, iter->msg)))) {
failed = true;
break;
}
if (failed) {
git_str msg = GIT_STR_INIT;
git_str_puts(&msg, "Expected and actual push statuses differ:\nEXPECTED:\n");
for(i = 0; i < expected_len; i++) {
git_str_printf(&msg, "%s: %s\n",
expected[i].ref,
expected[i].success ? "success" : "failed");
}
git_str_puts(&msg, "\nACTUAL:\n");
git_vector_foreach(actual, i, iter) {
if (iter->success)
git_str_printf(&msg, "%s: success\n", iter->ref);
else
git_str_printf(&msg, "%s: failed with message: %s", iter->ref, iter->msg);
}
cl_fail(git_str_cstr(&msg));
git_str_dispose(&msg);
}
git_vector_foreach(actual, i, iter) {
push_status *s = (push_status *)iter;
git__free(s->ref);
git__free(s->msg);
git__free(s);
}
git_vector_free(actual);
}
/**
* Verifies that after git_push_finish(), refs on a remote have the expected
* names, oids, and order.
*
* @param remote remote to verify
* @param expected_refs expected remote refs after push
* @param expected_refs_len length of expected_refs
*/
static void verify_refs(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
{
const git_remote_head **actual_refs;
size_t actual_refs_len;
git_remote_ls(&actual_refs, &actual_refs_len, remote);
verify_remote_refs(actual_refs, actual_refs_len, expected_refs, expected_refs_len);
}
/**
* Verifies that after git_push_update_tips(), remote tracking branches have the expected
* names and oids.
*
* @param remote remote to verify
* @param expected_refs expected remote refs after push
* @param expected_refs_len length of expected_refs
*/
static void verify_tracking_branches(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
{
git_refspec *fetch_spec;
size_t i, j;
git_str msg = GIT_STR_INIT;
git_buf ref_name = GIT_BUF_INIT;
git_vector actual_refs = GIT_VECTOR_INIT;
git_branch_iterator *iter;
char *actual_ref;
git_oid oid;
int failed = 0, error;
git_branch_t branch_type;
git_reference *ref;
/* Get current remote-tracking branches */
cl_git_pass(git_branch_iterator_new(&iter, remote->repo, GIT_BRANCH_REMOTE));
while ((error = git_branch_next(&ref, &branch_type, iter)) == 0) {
cl_assert_equal_i(branch_type, GIT_BRANCH_REMOTE);
cl_git_pass(git_vector_insert(&actual_refs, git__strdup(git_reference_name(ref))));
git_reference_free(ref);
}
cl_assert_equal_i(error, GIT_ITEROVER);
git_branch_iterator_free(iter);
/* Loop through expected refs, make sure they exist */
for (i = 0; i < expected_refs_len; i++) {
/* Convert remote reference name into remote-tracking branch name.
* If the spec is not under refs/heads/, then skip.
*/
fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
if (!fetch_spec)
continue;
cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
/* Find matching remote branch */
git_vector_foreach(&actual_refs, j, actual_ref) {
if (!strcmp(ref_name.ptr, actual_ref))
break;
}
if (j == actual_refs.length) {
git_str_printf(&msg, "Did not find expected tracking branch '%s'.", ref_name.ptr);
failed = 1;
goto failed;
}
/* Make sure tracking branch is at expected commit ID */
cl_git_pass(git_reference_name_to_id(&oid, remote->repo, actual_ref));
if (git_oid_cmp(expected_refs[i].oid, &oid) != 0) {
git_str_puts(&msg, "Tracking branch commit does not match expected ID.");
failed = 1;
goto failed;
}
git__free(actual_ref);
cl_git_pass(git_vector_remove(&actual_refs, j));
}
/* Make sure there are no extra branches */
if (actual_refs.length > 0) {
git_str_puts(&msg, "Unexpected remote tracking branches exist.");
failed = 1;
goto failed;
}
failed:
if (failed)
cl_fail(git_str_cstr(&msg));
git_vector_foreach(&actual_refs, i, actual_ref)
git__free(actual_ref);
git_vector_free(&actual_refs);
git_str_dispose(&msg);
git_buf_dispose(&ref_name);
}
static void verify_update_tips_callback(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
{
git_refspec *fetch_spec;
git_str msg = GIT_STR_INIT;
git_buf ref_name = GIT_BUF_INIT;
updated_tip *tip = NULL;
size_t i, j;
int failed = 0;
for (i = 0; i < expected_refs_len; ++i) {
/* Convert remote reference name into tracking branch name.
* If the spec is not under refs/heads/, then skip.
*/
fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
if (!fetch_spec)
continue;
cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
/* Find matching update_tip entry */
git_vector_foreach(&_record_cbs_data.updated_tips, j, tip) {
if (!strcmp(ref_name.ptr, tip->name))
break;
}
if (j == _record_cbs_data.updated_tips.length) {
git_str_printf(&msg, "Did not find expected updated tip entry for branch '%s'.", ref_name.ptr);
failed = 1;
goto failed;
}
if (git_oid_cmp(expected_refs[i].oid, &tip->new_oid) != 0) {
git_str_printf(&msg, "Updated tip ID does not match expected ID");
failed = 1;
goto failed;
}
}
failed:
if (failed)
cl_fail(git_str_cstr(&msg));
git_buf_dispose(&ref_name);
git_str_dispose(&msg);
}
void test_online_push__initialize(void)
{
git_vector delete_specs = GIT_VECTOR_INIT;
const git_remote_head **heads;
size_t heads_len;
git_push_options push_opts = GIT_PUSH_OPTIONS_INIT;
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
_repo = cl_git_sandbox_init("push_src");
cl_git_pass(git_repository_set_ident(_repo, "Random J. Hacker", "[email protected]"));
cl_fixture_sandbox("testrepo.git");
cl_rename("push_src/submodule/.gitted", "push_src/submodule/.git");
rewrite_gitmodules(git_repository_workdir(_repo));
/* git log --format=oneline --decorate --graph
* *-. 951bbbb90e2259a4c8950db78946784fb53fcbce (HEAD, b6) merge b3, b4, and b5 to b6
* |\ \
* | | * fa38b91f199934685819bea316186d8b008c52a2 (b5) added submodule named 'submodule' pointing to '../testrepo.git'
* | * | 27b7ce66243eb1403862d05f958c002312df173d (b4) edited fold\b.txt
* | |/
* * | d9b63a88223d8367516f50bd131a5f7349b7f3e4 (b3) edited a.txt
* |/
* * a78705c3b2725f931d3ee05348d83cc26700f247 (b2, b1) added fold and fold/b.txt
* * 5c0bb3d1b9449d1cc69d7519fd05166f01840915 added a.txt
*/
git_oid__fromstr(&_oid_b6, "951bbbb90e2259a4c8950db78946784fb53fcbce", GIT_OID_SHA1);
git_oid__fromstr(&_oid_b5, "fa38b91f199934685819bea316186d8b008c52a2", GIT_OID_SHA1);
git_oid__fromstr(&_oid_b4, "27b7ce66243eb1403862d05f958c002312df173d", GIT_OID_SHA1);
git_oid__fromstr(&_oid_b3, "d9b63a88223d8367516f50bd131a5f7349b7f3e4", GIT_OID_SHA1);
git_oid__fromstr(&_oid_b2, "a78705c3b2725f931d3ee05348d83cc26700f247", GIT_OID_SHA1);
git_oid__fromstr(&_oid_b1, "a78705c3b2725f931d3ee05348d83cc26700f247", GIT_OID_SHA1);
git_oid__fromstr(&_tag_commit, "805c54522e614f29f70d2413a0470247d8b424ac", GIT_OID_SHA1);
git_oid__fromstr(&_tag_tree, "ff83aa4c5e5d28e3bcba2f5c6e2adc61286a4e5e", GIT_OID_SHA1);
git_oid__fromstr(&_tag_blob, "b483ae7ba66decee9aee971f501221dea84b1498", GIT_OID_SHA1);
git_oid__fromstr(&_tag_lightweight, "951bbbb90e2259a4c8950db78946784fb53fcbce", GIT_OID_SHA1);
git_oid__fromstr(&_tag_tag, "eea4f2705eeec2db3813f2430829afce99cd00b5", GIT_OID_SHA1);
/* Remote URL environment variable must be set. User and password are optional. */
_remote_url = cl_getenv("GITTEST_REMOTE_URL");
_remote_user = cl_getenv("GITTEST_REMOTE_USER");
_remote_pass = cl_getenv("GITTEST_REMOTE_PASS");
_remote_ssh_key = cl_getenv("GITTEST_REMOTE_SSH_KEY");
_remote_ssh_pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
_remote_ssh_passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
_remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT");
_remote_expectcontinue = cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE");
_remote = NULL;
/* Skip the test if we're missing the remote URL */
if (!_remote_url)
cl_skip();
if (_remote_expectcontinue)
git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, 1);
cl_git_pass(git_remote_create(&_remote, _repo, "test", _remote_url));
record_callbacks_data_clear(&_record_cbs_data);
cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH, &_record_cbs, NULL, NULL));
/* Clean up previously pushed branches. Fails if receive.denyDeletes is
* set on the remote. Also, on Git 1.7.0 and newer, you must run
* 'git config receive.denyDeleteCurrent ignore' in the remote repo in
* order to delete the remote branch pointed to by HEAD (usually master).
* See: https://raw.github.com/git/git/master/Documentation/RelNotes/1.7.0.txt
*/
cl_git_pass(git_remote_ls(&heads, &heads_len, _remote));
cl_git_pass(create_deletion_refspecs(&delete_specs, heads, heads_len));
if (delete_specs.length) {
git_strarray arr = {
(char **) delete_specs.contents,
delete_specs.length,
};
memcpy(&push_opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
cl_git_pass(git_remote_upload(_remote, &arr, &push_opts));
}
git_remote_disconnect(_remote);
git_vector_free_deep(&delete_specs);
/* Now that we've deleted everything, fetch from the remote */
memcpy(&fetch_opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
cl_git_pass(git_remote_fetch(_remote, NULL, &fetch_opts, NULL));
}
void test_online_push__cleanup(void)
{
if (_remote)
git_remote_free(_remote);
_remote = NULL;
git__free(_remote_url);
git__free(_remote_user);
git__free(_remote_pass);
git__free(_remote_ssh_key);
git__free(_remote_ssh_pubkey);
git__free(_remote_ssh_passphrase);
git__free(_remote_default);
git__free(_remote_expectcontinue);
/* Freed by cl_git_sandbox_cleanup */
_repo = NULL;
git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, 0);
record_callbacks_data_clear(&_record_cbs_data);
cl_fixture_cleanup("testrepo.git");
cl_git_sandbox_cleanup();
}
static int push_pack_progress_cb(
int stage, unsigned int current, unsigned int total, void* payload)
{
record_callbacks_data *data = (record_callbacks_data *) payload;
GIT_UNUSED(stage); GIT_UNUSED(current); GIT_UNUSED(total);
if (data->pack_progress_calls < 0)
return data->pack_progress_calls;
data->pack_progress_calls++;
return 0;
}
static int push_transfer_progress_cb(
unsigned int current, unsigned int total, size_t bytes, void* payload)
{
record_callbacks_data *data = (record_callbacks_data *) payload;
GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
if (data->transfer_progress_calls < 0)
return data->transfer_progress_calls;
data->transfer_progress_calls++;
return 0;
}
/**
* Calls push and relists refs on remote to verify success.
*
* @param refspecs refspecs to push
* @param refspecs_len length of refspecs
* @param expected_refs expected remote refs after push
* @param expected_refs_len length of expected_refs
* @param expected_ret expected return value from git_push_finish()
* @param check_progress_cb Check that the push progress callbacks are called
*/
static void do_push(
const char *refspecs[], size_t refspecs_len,
push_status expected_statuses[], size_t expected_statuses_len,
expected_ref expected_refs[], size_t expected_refs_len,
int expected_ret, int check_progress_cb, int check_update_tips_cb)
{
git_push_options opts = GIT_PUSH_OPTIONS_INIT;
size_t i;
int error;
git_strarray specs = {0};
record_callbacks_data *data;
if (_remote) {
/* Auto-detect the number of threads to use */
opts.pb_parallelism = 0;
memcpy(&opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
data = opts.callbacks.payload;
opts.callbacks.pack_progress = push_pack_progress_cb;
opts.callbacks.push_transfer_progress = push_transfer_progress_cb;
opts.callbacks.push_update_reference = record_push_status_cb;
if (refspecs_len) {
specs.count = refspecs_len;
specs.strings = git__calloc(refspecs_len, sizeof(char *));
cl_assert(specs.strings);
}
for (i = 0; i < refspecs_len; i++)
specs.strings[i] = (char *) refspecs[i];
/* if EUSER, then abort in transfer */
if (check_progress_cb && expected_ret == GIT_EUSER)
data->transfer_progress_calls = GIT_EUSER;
error = git_remote_push(_remote, &specs, &opts);
git__free(specs.strings);
if (expected_ret < 0) {
cl_git_fail_with(expected_ret, error);
} else {
cl_git_pass(error);
}
if (check_progress_cb && expected_ret == 0) {
cl_assert(data->pack_progress_calls > 0);
cl_assert(data->transfer_progress_calls > 0);
}
do_verify_push_status(data, expected_statuses, expected_statuses_len);
verify_refs(_remote, expected_refs, expected_refs_len);
verify_tracking_branches(_remote, expected_refs, expected_refs_len);
if (check_update_tips_cb)
verify_update_tips_callback(_remote, expected_refs, expected_refs_len);
}
}
/* Call push_finish() without ever calling git_push_add_refspec() */
void test_online_push__noop(void)
{
do_push(NULL, 0, NULL, 0, NULL, 0, 0, 0, 1);
}
void test_online_push__b1(void)
{
const char *specs[] = { "refs/heads/b1:refs/heads/b1" };
push_status exp_stats[] = { { "refs/heads/b1", 1 } };
expected_ref exp_refs[] = { { "refs/heads/b1", &_oid_b1 } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__b2(void)
{
const char *specs[] = { "refs/heads/b2:refs/heads/b2" };
push_status exp_stats[] = { { "refs/heads/b2", 1 } };
expected_ref exp_refs[] = { { "refs/heads/b2", &_oid_b2 } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__b3(void)
{
const char *specs[] = { "refs/heads/b3:refs/heads/b3" };
push_status exp_stats[] = { { "refs/heads/b3", 1 } };
expected_ref exp_refs[] = { { "refs/heads/b3", &_oid_b3 } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__b4(void)
{
const char *specs[] = { "refs/heads/b4:refs/heads/b4" };
push_status exp_stats[] = { { "refs/heads/b4", 1 } };
expected_ref exp_refs[] = { { "refs/heads/b4", &_oid_b4 } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__b5(void)
{
const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
push_status exp_stats[] = { { "refs/heads/b5", 1 } };
expected_ref exp_refs[] = { { "refs/heads/b5", &_oid_b5 } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__b5_cancel(void)
{
const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
do_push(specs, ARRAY_SIZE(specs), NULL, 0, NULL, 0, GIT_EUSER, 1, 1);
}
void test_online_push__multi(void)
{
git_reflog *log;
const git_reflog_entry *entry;
const char *specs[] = {
"refs/heads/b1:refs/heads/b1",
"refs/heads/b2:refs/heads/b2",
"refs/heads/b3:refs/heads/b3",
"refs/heads/b4:refs/heads/b4",
"refs/heads/b5:refs/heads/b5"
};
push_status exp_stats[] = {
{ "refs/heads/b1", 1 },
{ "refs/heads/b2", 1 },
{ "refs/heads/b3", 1 },
{ "refs/heads/b4", 1 },
{ "refs/heads/b5", 1 }
};
expected_ref exp_refs[] = {
{ "refs/heads/b1", &_oid_b1 },
{ "refs/heads/b2", &_oid_b2 },
{ "refs/heads/b3", &_oid_b3 },
{ "refs/heads/b4", &_oid_b4 },
{ "refs/heads/b5", &_oid_b5 }
};
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
cl_git_pass(git_reflog_read(&log, _repo, "refs/remotes/test/b1"));
entry = git_reflog_entry_byindex(log, 0);
if (entry) {
cl_assert_equal_s("update by push", git_reflog_entry_message(entry));
cl_assert_equal_s("[email protected]", git_reflog_entry_committer(entry)->email);
}
git_reflog_free(log);
}
void test_online_push__implicit_tgt(void)
{
const char *specs1[] = { "refs/heads/b1" };
push_status exp_stats1[] = { { "refs/heads/b1", 1 } };
expected_ref exp_refs1[] = { { "refs/heads/b1", &_oid_b1 } };
const char *specs2[] = { "refs/heads/b2" };
push_status exp_stats2[] = { { "refs/heads/b2", 1 } };
expected_ref exp_refs2[] = {
{ "refs/heads/b1", &_oid_b1 },
{ "refs/heads/b2", &_oid_b2 }
};
do_push(specs1, ARRAY_SIZE(specs1),
exp_stats1, ARRAY_SIZE(exp_stats1),
exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
do_push(specs2, ARRAY_SIZE(specs2),
exp_stats2, ARRAY_SIZE(exp_stats2),
exp_refs2, ARRAY_SIZE(exp_refs2), 0, 0, 0);
}
void test_online_push__fast_fwd(void)
{
/* Fast forward b1 in tgt from _oid_b1 to _oid_b6. */
const char *specs_init[] = { "refs/heads/b1:refs/heads/b1" };
push_status exp_stats_init[] = { { "refs/heads/b1", 1 } };
expected_ref exp_refs_init[] = { { "refs/heads/b1", &_oid_b1 } };
const char *specs_ff[] = { "refs/heads/b6:refs/heads/b1" };
push_status exp_stats_ff[] = { { "refs/heads/b1", 1 } };
expected_ref exp_refs_ff[] = { { "refs/heads/b1", &_oid_b6 } };
/* Do a force push to reset b1 in target back to _oid_b1 */
const char *specs_reset[] = { "+refs/heads/b1:refs/heads/b1" };
/* Force should have no effect on a fast forward push */
const char *specs_ff_force[] = { "+refs/heads/b6:refs/heads/b1" };
do_push(specs_init, ARRAY_SIZE(specs_init),
exp_stats_init, ARRAY_SIZE(exp_stats_init),
exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 1, 1);
do_push(specs_ff, ARRAY_SIZE(specs_ff),
exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
do_push(specs_reset, ARRAY_SIZE(specs_reset),
exp_stats_init, ARRAY_SIZE(exp_stats_init),
exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 0, 0);
do_push(specs_ff_force, ARRAY_SIZE(specs_ff_force),
exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
}
void test_online_push__tag_commit(void)
{
const char *specs[] = { "refs/tags/tag-commit:refs/tags/tag-commit" };
push_status exp_stats[] = { { "refs/tags/tag-commit", 1 } };
expected_ref exp_refs[] = { { "refs/tags/tag-commit", &_tag_commit } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__tag_tree(void)
{
const char *specs[] = { "refs/tags/tag-tree:refs/tags/tag-tree" };
push_status exp_stats[] = { { "refs/tags/tag-tree", 1 } };
expected_ref exp_refs[] = { { "refs/tags/tag-tree", &_tag_tree } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__tag_blob(void)
{
const char *specs[] = { "refs/tags/tag-blob:refs/tags/tag-blob" };
push_status exp_stats[] = { { "refs/tags/tag-blob", 1 } };
expected_ref exp_refs[] = { { "refs/tags/tag-blob", &_tag_blob } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__tag_lightweight(void)
{
const char *specs[] = { "refs/tags/tag-lightweight:refs/tags/tag-lightweight" };
push_status exp_stats[] = { { "refs/tags/tag-lightweight", 1 } };
expected_ref exp_refs[] = { { "refs/tags/tag-lightweight", &_tag_lightweight } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
}
void test_online_push__tag_to_tag(void)
{
const char *specs[] = { "refs/tags/tag-tag:refs/tags/tag-tag" };
push_status exp_stats[] = { { "refs/tags/tag-tag", 1 } };
expected_ref exp_refs[] = { { "refs/tags/tag-tag", &_tag_tag } };
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 0, 0);
}
void test_online_push__force(void)
{
const char *specs1[] = {"refs/heads/b3:refs/heads/tgt"};
push_status exp_stats1[] = { { "refs/heads/tgt", 1 } };
expected_ref exp_refs1[] = { { "refs/heads/tgt", &_oid_b3 } };
const char *specs2[] = {"refs/heads/b4:refs/heads/tgt"};
const char *specs2_force[] = {"+refs/heads/b4:refs/heads/tgt"};
push_status exp_stats2_force[] = { { "refs/heads/tgt", 1 } };
expected_ref exp_refs2_force[] = { { "refs/heads/tgt", &_oid_b4 } };
do_push(specs1, ARRAY_SIZE(specs1),
exp_stats1, ARRAY_SIZE(exp_stats1),
exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
do_push(specs2, ARRAY_SIZE(specs2),
NULL, 0,
exp_refs1, ARRAY_SIZE(exp_refs1), GIT_ENONFASTFORWARD, 0, 0);
/* Non-fast-forward update with force should pass. */
record_callbacks_data_clear(&_record_cbs_data);
do_push(specs2_force, ARRAY_SIZE(specs2_force),
exp_stats2_force, ARRAY_SIZE(exp_stats2_force),
exp_refs2_force, ARRAY_SIZE(exp_refs2_force), 0, 1, 1);
}
void test_online_push__delete(void)
{
const char *specs1[] = {
"refs/heads/b1:refs/heads/tgt1",
"refs/heads/b1:refs/heads/tgt2"
};
push_status exp_stats1[] = {
{ "refs/heads/tgt1", 1 },
{ "refs/heads/tgt2", 1 }
};
expected_ref exp_refs1[] = {
{ "refs/heads/tgt1", &_oid_b1 },
{ "refs/heads/tgt2", &_oid_b1 }
};
const char *specs_del_fake[] = { ":refs/heads/fake" };
/* Force has no effect for delete. */
const char *specs_del_fake_force[] = { "+:refs/heads/fake" };
push_status exp_stats_fake[] = { { "refs/heads/fake", 1 } };
const char *specs_delete[] = { ":refs/heads/tgt1" };
push_status exp_stats_delete[] = { { "refs/heads/tgt1", 1 } };
expected_ref exp_refs_delete[] = { { "refs/heads/tgt2", &_oid_b1 } };
/* Force has no effect for delete. */
const char *specs_delete_force[] = { "+:refs/heads/tgt1" };
do_push(specs1, ARRAY_SIZE(specs1),
exp_stats1, ARRAY_SIZE(exp_stats1),
exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
/* When deleting a non-existent branch, the git client sends zero for both
* the old and new commit id. This should succeed on the server with the
* same status report as if the branch were actually deleted. The server
* returns a warning on the side-band iff the side-band is supported.
* Since libgit2 doesn't support the side-band yet, there are no warnings.
*/
do_push(specs_del_fake, ARRAY_SIZE(specs_del_fake),
exp_stats_fake, 1,
exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
do_push(specs_del_fake_force, ARRAY_SIZE(specs_del_fake_force),
exp_stats_fake, 1,
exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
/* Delete one of the pushed branches. */
do_push(specs_delete, ARRAY_SIZE(specs_delete),
exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
/* Re-push branches and retry delete with force. */
do_push(specs1, ARRAY_SIZE(specs1),
exp_stats1, ARRAY_SIZE(exp_stats1),
exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
do_push(specs_delete_force, ARRAY_SIZE(specs_delete_force),
exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
}
void test_online_push__bad_refspecs(void)
{
/* All classes of refspecs that should be rejected by
* git_push_add_refspec() should go in this test.
*/
char *specs = {
"b6:b6",
};
git_strarray arr = {
&specs,
1,
};
if (_remote) {
cl_git_fail(git_remote_upload(_remote, &arr, NULL));
}
}
void test_online_push__expressions(void)
{
/* TODO: Expressions in refspecs doesn't actually work yet */
const char *specs_left_expr[] = { "refs/heads/b2~1:refs/heads/b2" };
/* TODO: Find a more precise way of checking errors than a exit code of -1. */
do_push(specs_left_expr, ARRAY_SIZE(specs_left_expr),
NULL, 0,
NULL, 0, -1, 0, 0);
}
void test_online_push__notes(void)
{
git_oid note_oid, *target_oid, expected_oid;
git_signature *signature;
const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
push_status exp_stats[] = { { "refs/notes/commits", 1 } };
expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
const char *specs_del[] = { ":refs/notes/commits" };
git_oid__fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb", GIT_OID_SHA1);
target_oid = &_oid_b6;
/* Create note to push */
cl_git_pass(git_signature_new(&signature, "nulltoken", "[email protected]", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
cl_git_pass(git_note_create(¬e_oid, _repo, NULL, signature, signature, target_oid, "hello world\n", 0));
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
/* And make sure to delete the note */
do_push(specs_del, ARRAY_SIZE(specs_del),
exp_stats, 1,
NULL, 0, 0, 0, 0);
git_signature_free(signature);
}
void test_online_push__configured(void)
{
git_oid note_oid, *target_oid, expected_oid;
git_signature *signature;
git_remote *old_remote;
const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
push_status exp_stats[] = { { "refs/notes/commits", 1 } };
expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
const char *specs_del[] = { ":refs/notes/commits" };
git_oid__fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb", GIT_OID_SHA1);
target_oid = &_oid_b6;
cl_git_pass(git_remote_add_push(_repo, git_remote_name(_remote), specs[0]));
old_remote = _remote;
cl_git_pass(git_remote_lookup(&_remote, _repo, git_remote_name(_remote)));
git_remote_free(old_remote);
/* Create note to push */
cl_git_pass(git_signature_new(&signature, "nulltoken", "[email protected]", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
cl_git_pass(git_note_create(¬e_oid, _repo, NULL, signature, signature, target_oid, "hello world\n", 0));
do_push(NULL, 0,
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
/* And make sure to delete the note */
do_push(specs_del, ARRAY_SIZE(specs_del),
exp_stats, 1,
NULL, 0, 0, 0, 0);
git_signature_free(signature);
}
| libgit2-main | tests/libgit2/online/push.c |
#include "clar_libgit2.h"
#include "vector.h"
#include "push_util.h"
void updated_tip_free(updated_tip *t)
{
git__free(t->name);
git__free(t);
}
static void push_status_free(push_status *s)
{
git__free(s->ref);
git__free(s->msg);
git__free(s);
}
void record_callbacks_data_clear(record_callbacks_data *data)
{
size_t i;
updated_tip *tip;
push_status *status;
git_vector_foreach(&data->updated_tips, i, tip)
updated_tip_free(tip);
git_vector_free(&data->updated_tips);
git_vector_foreach(&data->statuses, i, status)
push_status_free(status);
git_vector_free(&data->statuses);
data->pack_progress_calls = 0;
data->transfer_progress_calls = 0;
}
int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
updated_tip *t;
record_callbacks_data *record_data = (record_callbacks_data *)data;
cl_assert(t = git__calloc(1, sizeof(*t)));
cl_assert(t->name = git__strdup(refname));
git_oid_cpy(&t->old_oid, a);
git_oid_cpy(&t->new_oid, b);
git_vector_insert(&record_data->updated_tips, t);
return 0;
}
int create_deletion_refspecs(git_vector *out, const git_remote_head **heads, size_t heads_len)
{
git_str del_spec = GIT_STR_INIT;
int valid;
size_t i;
for (i = 0; i < heads_len; i++) {
const git_remote_head *head = heads[i];
/* Ignore malformed ref names (which also saves us from tag^{} */
cl_git_pass(git_reference_name_is_valid(&valid, head->name));
if (!valid)
return 0;
/* Create a refspec that deletes a branch in the remote */
if (strcmp(head->name, "refs/heads/master")) {
cl_git_pass(git_str_putc(&del_spec, ':'));
cl_git_pass(git_str_puts(&del_spec, head->name));
cl_git_pass(git_vector_insert(out, git_str_detach(&del_spec)));
}
}
return 0;
}
int record_ref_cb(git_remote_head *head, void *payload)
{
git_vector *refs = (git_vector *) payload;
return git_vector_insert(refs, head);
}
void verify_remote_refs(const git_remote_head *actual_refs[], size_t actual_refs_len, const expected_ref expected_refs[], size_t expected_refs_len)
{
size_t i, j = 0;
git_str msg = GIT_STR_INIT;
const git_remote_head *actual;
char *oid_str;
bool master_present = false;
/* We don't care whether "master" is present on the other end or not */
for (i = 0; i < actual_refs_len; i++) {
actual = actual_refs[i];
if (!strcmp(actual->name, "refs/heads/master")) {
master_present = true;
break;
}
}
if (expected_refs_len + (master_present ? 1 : 0) != actual_refs_len)
goto failed;
for (i = 0; i < actual_refs_len; i++) {
actual = actual_refs[i];
if (master_present && !strcmp(actual->name, "refs/heads/master"))
continue;
if (strcmp(expected_refs[j].name, actual->name) ||
git_oid_cmp(expected_refs[j].oid, &actual->oid))
goto failed;
j++;
}
return;
failed:
git_str_puts(&msg, "Expected and actual refs differ:\nEXPECTED:\n");
for(i = 0; i < expected_refs_len; i++) {
oid_str = git_oid_tostr_s(expected_refs[i].oid);
cl_git_pass(git_str_printf(&msg, "%s = %s\n", expected_refs[i].name, oid_str));
}
git_str_puts(&msg, "\nACTUAL:\n");
for (i = 0; i < actual_refs_len; i++) {
actual = actual_refs[i];
if (master_present && !strcmp(actual->name, "refs/heads/master"))
continue;
oid_str = git_oid_tostr_s(&actual->oid);
cl_git_pass(git_str_printf(&msg, "%s = %s\n", actual->name, oid_str));
}
cl_fail(git_str_cstr(&msg));
git_str_dispose(&msg);
}
| libgit2-main | tests/libgit2/online/push_util.c |
#include "clar_libgit2.h"
#include "futils.h"
#include "fetchhead.h"
#include "../fetchhead/fetchhead_data.h"
#include "git2/clone.h"
#define LIVE_REPO_URL "https://github.com/libgit2/TestGitRepository"
static git_repository *g_repo;
static git_clone_options g_options;
void test_online_fetchhead__initialize(void)
{
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.fetch_opts = dummy_fetch;
}
void test_online_fetchhead__cleanup(void)
{
if (g_repo) {
git_repository_free(g_repo);
g_repo = NULL;
}
cl_fixture_cleanup("./foo");
}
static void fetchhead_test_clone(void)
{
cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
}
static size_t count_references(void)
{
git_strarray array;
size_t refs;
cl_git_pass(git_reference_list(&array, g_repo));
refs = array.count;
git_strarray_dispose(&array);
return refs;
}
static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
{
git_remote *remote;
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
git_str fetchhead_buf = GIT_STR_INIT;
git_strarray array, *active_refs = NULL;
cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));
fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
if(fetchspec != NULL) {
array.count = 1;
array.strings = (char **) &fetchspec;
active_refs = &array;
}
cl_git_pass(git_remote_fetch(remote, active_refs, &fetch_opts, NULL));
git_remote_free(remote);
cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD"));
cl_assert_equal_s(fetchhead_buf.ptr, expected_fetchhead);
git_str_dispose(&fetchhead_buf);
}
void test_online_fetchhead__wildcard_spec(void)
{
fetchhead_test_clone();
fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA2);
cl_git_pass(git_tag_delete(g_repo, "annotated_tag"));
cl_git_pass(git_tag_delete(g_repo, "blob"));
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
cl_git_pass(git_tag_delete(g_repo, "nearly-dangling"));
fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA);
}
void test_online_fetchhead__explicit_spec(void)
{
fetchhead_test_clone();
fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA);
}
void test_online_fetchhead__no_merges(void)
{
git_config *config;
fetchhead_test_clone();
cl_git_pass(git_repository_config(&config, g_repo));
cl_git_pass(git_config_delete_entry(config, "branch.master.remote"));
cl_git_pass(git_config_delete_entry(config, "branch.master.merge"));
git_config_free(config);
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA2);
cl_git_pass(git_tag_delete(g_repo, "annotated_tag"));
cl_git_pass(git_tag_delete(g_repo, "blob"));
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
cl_git_pass(git_tag_delete(g_repo, "nearly-dangling"));
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA);
cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3);
}
void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
{
git_reference *ref;
size_t refs;
fetchhead_test_clone();
refs = count_references();
fetchhead_test_fetch("refs/heads/first-merge:refs/heads/explicit-refspec", FETCH_HEAD_EXPLICIT_DATA);
cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL));
cl_assert_equal_i(refs + 1, count_references());
git_reference_free(ref);
}
void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
{
git_reference *ref;
size_t refs;
fetchhead_test_clone();
refs = count_references();
fetchhead_test_fetch("refs/heads/first-merge", FETCH_HEAD_EXPLICIT_DATA);
cl_git_fail(git_branch_lookup(&ref, g_repo, "first-merge", GIT_BRANCH_ALL));
cl_assert_equal_i(refs, count_references());
}
void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
{
size_t refs;
fetchhead_test_clone();
refs = count_references();
fetchhead_test_fetch("refs/heads/first-merge:", FETCH_HEAD_EXPLICIT_DATA);
cl_assert_equal_i(refs, count_references());
}
void test_online_fetchhead__creds_get_stripped(void)
{
git_str buf = GIT_STR_INIT;
git_remote *remote;
cl_git_pass(git_repository_init(&g_repo, "./foo", 0));
cl_git_pass(git_remote_create_anonymous(&remote, g_repo, "https://foo:[email protected]/libgit2/TestGitRepository"));
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
cl_git_pass(git_futils_readbuffer(&buf, "./foo/.git/FETCH_HEAD"));
cl_assert_equal_s(buf.ptr,
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\thttps://github.com/libgit2/TestGitRepository\n");
git_remote_free(remote);
git_str_dispose(&buf);
}
| libgit2-main | tests/libgit2/online/fetchhead.c |