ident.c 3.75 KB
Newer Older
Russell Belfer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#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;
23
	git_buf out = { 0 };
Russell Belfer committed
24 25 26 27 28 29 30 31 32 33

	cl_git_mkfile("crlf/identtest", data);
	cl_git_pass(git_blob_create_fromworkdir(&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);
34
	git_buf_free(&out);
Russell Belfer committed
35 36 37 38 39 40 41
}

void test_filter_ident__to_worktree(void)
{
	git_filter_list *fl;
	git_filter *ident;

42 43
	cl_git_pass(git_filter_list_new(
		&fl, g_repo, GIT_FILTER_TO_WORKTREE, 0));
Russell Belfer committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	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;

82 83
	cl_git_pass(git_filter_list_new(
		&fl, g_repo, GIT_FILTER_TO_ODB, 0));
Russell Belfer committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	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);
}