Commit d34f6826 by Edward Thomson Committed by Edward Thomson

Patch parsing from patch files

parent 7cb904ba
......@@ -448,6 +448,8 @@ typedef int (*git_diff_file_cb)(
float progress,
void *payload);
#define GIT_DIFF_HUNK_HEADER_SIZE 128
/**
* When producing a binary diff, the binary data returned will be
* either the deflated full ("literal") contents of the file, or
......@@ -499,12 +501,12 @@ typedef int(*git_diff_binary_cb)(
* Structure describing a hunk of a diff.
*/
typedef struct {
int old_start; /**< Starting line number in old_file */
int old_lines; /**< Number of lines in old_file */
int new_start; /**< Starting line number in new_file */
int new_lines; /**< Number of lines in new_file */
size_t header_len; /**< Number of bytes in header text */
char header[128]; /**< Header text, NUL-byte terminated */
int old_start; /** Starting line number in old_file */
int old_lines; /** Number of lines in old_file */
int new_start; /** Starting line number in new_file */
int new_lines; /** Number of lines in new_file */
size_t header_len; /** Number of bytes in header text */
char header[GIT_DIFF_HUNK_HEADER_SIZE]; /** Header text, NUL-byte terminated */
} git_diff_hunk;
/**
......
......@@ -267,6 +267,19 @@ GIT_EXTERN(int) git_patch_to_buf(
git_buf *out,
git_patch *patch);
/**
* Create a patch from the contents of a patch file.
*
* @param out The patch to be created
* @param patchfile The contents of a patch file
* @param patchfile_len The length of the patch file
* @return 0 on success, <0 on failure.
*/
GIT_EXTERN(int) git_patch_from_patchfile(
git_patch **out,
const char *patchfile,
size_t patchfile_len);
GIT_END_DECL
/**@}*/
......
......@@ -766,3 +766,78 @@ int git_buf_splice(
buf->ptr[buf->size] = '\0';
return 0;
}
/* Unquote per http://marc.info/?l=git&m=112927316408690&w=2 */
int git_buf_unquote(git_buf *buf)
{
size_t i, j;
char ch;
git_buf_rtrim(buf);
if (buf->size < 2 || buf->ptr[0] != '"' || buf->ptr[buf->size-1] != '"')
goto invalid;
for (i = 0, j = 1; j < buf->size-1; i++, j++) {
ch = buf->ptr[j];
if (ch == '\\') {
if (j == buf->size-2)
goto invalid;
ch = buf->ptr[++j];
switch (ch) {
/* \" or \\ simply copy the char in */
case '"': case '\\':
break;
/* add the appropriate escaped char */
case 'a': ch = '\a'; break;
case 'b': ch = '\b'; break;
case 'f': ch = '\f'; break;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case 'v': ch = '\v'; break;
/* \xyz digits convert to the char*/
case '0': case '1': case '2':
if (j == buf->size-3) {
giterr_set(GITERR_INVALID,
"Truncated quoted character \\%c", ch);
return -1;
}
if (buf->ptr[j+1] < '0' || buf->ptr[j+1] > '7' ||
buf->ptr[j+2] < '0' || buf->ptr[j+2] > '7') {
giterr_set(GITERR_INVALID,
"Truncated quoted character \\%c%c%c",
buf->ptr[j], buf->ptr[j+1], buf->ptr[j+2]);
return -1;
}
ch = ((buf->ptr[j] - '0') << 6) |
((buf->ptr[j+1] - '0') << 3) |
(buf->ptr[j+2] - '0');
j += 2;
break;
default:
giterr_set(GITERR_INVALID, "Invalid quoted character \\%c", ch);
return -1;
}
}
buf->ptr[i] = ch;
}
buf->ptr[i] = '\0';
buf->size = i;
return 0;
invalid:
giterr_set(GITERR_INVALID, "Invalid quoted line");
return -1;
}
......@@ -173,6 +173,11 @@ void git_buf_rtrim(git_buf *buf);
int git_buf_cmp(const git_buf *a, const git_buf *b);
/* Unquote a buffer as specified in
* http://marc.info/?l=git&m=112927316408690&w=2
*/
int git_buf_unquote(git_buf *buf);
/* Write data as base64 encoded in buffer */
int git_buf_encode_base64(git_buf *buf, const char *data, size_t len);
/* Decode the given bas64 and write the result to the buffer */
......
This diff is collapsed. Click to expand it.
......@@ -306,6 +306,25 @@ int git_path_join_unrooted(
return 0;
}
void git_path_squash_slashes(git_buf *path)
{
char *p, *q;
if (path->size == 0)
return;
for (p = path->ptr, q = path->ptr; *q; p++, q++) {
*p = *q;
while (*q == '/' && *(q+1) == '/') {
path->size--;
q++;
}
}
*p = '\0';
}
int git_path_prettify(git_buf *path_out, const char *path, const char *base)
{
char buf[GIT_PATH_MAX];
......
......@@ -244,6 +244,12 @@ extern int git_path_join_unrooted(
git_buf *path_out, const char *path, const char *base, ssize_t *root_at);
/**
* Removes multiple occurrences of '/' in a row, squashing them into a
* single '/'.
*/
extern void git_path_squash_slashes(git_buf *path);
/**
* Clean up path, prepending base if it is not already rooted.
*/
extern int git_path_prettify(git_buf *path_out, const char *path, const char *base);
......
......@@ -66,6 +66,12 @@ int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base)
{
return git__strntol64(result, nptr, (size_t)-1, endptr, base);
}
int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{
const char *p;
int64_t n, nn;
int c, ovfl, v, neg, ndig;
......@@ -111,7 +117,7 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba
/*
* Non-empty sequence of digits
*/
for (;; p++,ndig++) {
for (; nptr_len > 0; p++,ndig++,nptr_len--) {
c = *p;
v = base;
if ('0'<=c && c<='9')
......@@ -148,11 +154,17 @@ Return:
int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base)
{
return git__strntol32(result, nptr, (size_t)-1, endptr, base);
}
int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{
int error;
int32_t tmp_int;
int64_t tmp_long;
if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < 0)
if ((error = git__strntol64(&tmp_long, nptr, nptr_len, endptr, base)) < 0)
return error;
tmp_int = tmp_long & 0xFFFFFFFF;
......@@ -321,6 +333,12 @@ char *git__strsep(char **end, const char *sep)
return NULL;
}
size_t git__linenlen(const char *buffer, size_t buffer_len)
{
char *nl = memchr(buffer, '\n', buffer_len);
return nl ? (size_t)(nl - buffer) + 1 : buffer_len;
}
void git__hexdump(const char *buffer, size_t len)
{
static const size_t LINE_WIDTH = 16;
......
......@@ -263,7 +263,10 @@ GIT_INLINE(int) git__signum(int val)
}
extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
extern int git__strntol32(int32_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
extern int git__strntol64(int64_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
extern void git__hexdump(const char *buffer, size_t n);
extern uint32_t git__hash(const void *key, int len, uint32_t seed);
......@@ -290,6 +293,8 @@ GIT_INLINE(int) git__tolower(int c)
# define git__tolower(a) tolower(a)
#endif
extern size_t git__linenlen(const char *buffer, size_t buffer_len);
GIT_INLINE(const char *) git__next_line(const char *s)
{
while (*s && *s != '\n') s++;
......@@ -466,6 +471,11 @@ GIT_INLINE(bool) git__iswildcard(int c)
return (c == '*' || c == '?' || c == '[');
}
GIT_INLINE(bool) git__isxdigit(int c)
{
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}
/*
* Parse a string value as a boolean, just like Core Git does.
*
......
......@@ -284,3 +284,192 @@
" and this\n" \
" is additional context\n" \
" below it!\n"
#define PATCH_RENAME_EXACT_QUOTEDNAME \
"diff --git a/file.txt \"b/foo\\\"bar.txt\"\n" \
"similarity index 100%\n" \
"rename from file.txt\n" \
"rename to \"foo\\\"bar.txt\"\n"
#define PATCH_RENAME_SIMILAR_QUOTEDNAME \
"diff --git a/file.txt \"b/foo\\\"bar.txt\"\n" \
"similarity index 77%\n" \
"rename from file.txt\n" \
"rename to \"foo\\\"bar.txt\"\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"+++ \"b/foo\\\"bar.txt\"\n" \
"@@ -3,7 +3,7 @@ this is some context!\n" \
" around some lines\n" \
" that will change\n" \
" yes it is!\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n" \
" and this\n" \
" is additional context\n" \
" below it!\n"
#define PATCH_MODECHANGE_UNCHANGED \
"diff --git a/file.txt b/file.txt\n" \
"old mode 100644\n" \
"new mode 100755\n"
#define PATCH_MODECHANGE_MODIFIED \
"diff --git a/file.txt b/file.txt\n" \
"old mode 100644\n" \
"new mode 100755\n" \
"index 9432026..cd8fd12\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -3,7 +3,7 @@ this is some context!\n" \
" around some lines\n" \
" that will change\n" \
" yes it is!\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n" \
" and this\n" \
" is additional context\n" \
" below it!\n"
#define PATCH_NOISY \
"This is some\nleading noise\n@@ - that\nlooks like a hunk header\n" \
"but actually isn't and should parse ok\n" \
PATCH_ORIGINAL_TO_CHANGE_MIDDLE \
"plus some trailing garbage for good measure\n"
#define PATCH_NOISY_NOCONTEXT \
"This is some\nleading noise\n@@ - that\nlooks like a hunk header\n" \
"but actually isn't and should parse ok\n" \
PATCH_ORIGINAL_TO_CHANGE_MIDDLE_NOCONTEXT \
"plus some trailing garbage for good measure\n"
#define PATCH_TRUNCATED_1 \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -3,7 +3,7 @@ this is some context!\n" \
" around some lines\n" \
" that will change\n" \
" yes it is!\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n" \
" and this\n"
#define PATCH_TRUNCATED_2 \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -3,7 +3,7 @@ this is some context!\n" \
" around some lines\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n" \
" and this\n" \
" is additional context\n" \
" below it!\n"
#define PATCH_TRUNCATED_3 \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -3,7 +3,7 @@ this is some context!\n" \
" around some lines\n" \
" that will change\n" \
" yes it is!\n" \
"+(THIS line is changed!)\n" \
" and this\n" \
" is additional context\n" \
" below it!\n"
#define FILE_EMPTY_CONTEXT_ORIGINAL \
"this\nhas\nan\n\nempty\ncontext\nline\n"
#define FILE_EMPTY_CONTEXT_MODIFIED \
"this\nhas\nan\n\nempty...\ncontext\nline\n"
#define PATCH_EMPTY_CONTEXT \
"diff --git a/file.txt b/file.txt\n" \
"index 398d2df..bb15234 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -2,6 +2,6 @@ this\n" \
" has\n" \
" an\n" \
"\n" \
"-empty\n" \
"+empty...\n" \
" context\n" \
" line\n"
#define FILE_APPEND_NO_NL \
"hey!\n" \
"this is some context!\n" \
"around some lines\n" \
"that will change\n" \
"yes it is!\n" \
"(this line is changed)\n" \
"and this\n" \
"is additional context\n" \
"below it!\n" \
"added line with no nl"
#define PATCH_APPEND_NO_NL \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..83759c0 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -7,3 +7,4 @@ yes it is!\n" \
" and this\n" \
" is additional context\n" \
" below it!\n" \
"+added line with no nl\n" \
"\\ No newline at end of file\n"
#define PATCH_CORRUPT_GIT_HEADER \
"diff --git a/file.txt\n" \
"index 9432026..0f39b9a 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -0,0 +1 @@\n" \
"+insert at front\n"
#define PATCH_CORRUPT_MISSING_NEW_FILE \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"@@ -6 +6 @@ yes it is!\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n"
#define PATCH_CORRUPT_MISSING_OLD_FILE \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"+++ b/file.txt\n" \
"@@ -6 +6 @@ yes it is!\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n"
#define PATCH_CORRUPT_NO_CHANGES \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"@@ -0,0 +0,0 @@ yes it is!\n"
#define PATCH_CORRUPT_MISSING_HUNK_HEADER \
"diff --git a/file.txt b/file.txt\n" \
"index 9432026..cd8fd12 100644\n" \
"--- a/file.txt\n" \
"+++ b/file.txt\n" \
"-(this line is changed)\n" \
"+(THIS line is changed!)\n"
#define PATCH_NOT_A_PATCH \
"+++this is not\n" \
"--actually even\n" \
" a legitimate \n" \
"+patch file\n" \
"-it's something else\n" \
" entirely!"
#include "clar_libgit2.h"
#include "git2/sys/repository.h"
#include "apply.h"
#include "repository.h"
#include "buf_text.h"
#include "apply_common.h"
static git_repository *repo = NULL;
void test_apply_fromfile__initialize(void)
{
repo = cl_git_sandbox_init("renames");
}
void test_apply_fromfile__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static int apply_patchfile(
const char *old,
const char *new,
const char *patchfile,
const char *filename_expected,
unsigned int mode_expected)
{
git_patch *patch;
git_buf result = GIT_BUF_INIT;
git_buf patchbuf = GIT_BUF_INIT;
char *filename;
unsigned int mode;
int error;
cl_git_pass(git_patch_from_patchfile(&patch, patchfile, strlen(patchfile)));
error = git_apply__patch(&result, &filename, &mode, old, old ? strlen(old) : 0, patch);
if (error == 0) {
if (new == NULL)
cl_assert_equal_i(0, result.size);
else
cl_assert_equal_s(new, result.ptr);
cl_assert_equal_s(filename_expected, filename);
cl_assert_equal_i(mode_expected, mode);
}
git__free(filename);
git_buf_free(&result);
git_buf_free(&patchbuf);
git_patch_free(patch);
return error;
}
static int validate_and_apply_patchfile(
const char *old,
const char *new,
const char *patchfile,
const git_diff_options *diff_opts,
const char *filename_expected,
unsigned int mode_expected)
{
git_patch *patch_fromdiff;
git_buf validated = GIT_BUF_INIT;
int error;
cl_git_pass(git_patch_from_buffers(&patch_fromdiff,
old, old ? strlen(old) : 0, "file.txt",
new, new ? strlen(new) : 0, "file.txt",
diff_opts));
cl_git_pass(git_patch_to_buf(&validated, patch_fromdiff));
cl_assert_equal_s(patchfile, validated.ptr);
error = apply_patchfile(old, new, patchfile, filename_expected, mode_expected);
git_buf_free(&validated);
git_patch_free(patch_fromdiff);
return error;
}
void test_apply_fromfile__change_middle(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL,
FILE_CHANGE_MIDDLE, PATCH_ORIGINAL_TO_CHANGE_MIDDLE, NULL,
"b/file.txt", 0100644));
}
void test_apply_fromfile__change_middle_nocontext(void)
{
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
diff_opts.context_lines = 0;
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL,
FILE_CHANGE_MIDDLE, PATCH_ORIGINAL_TO_CHANGE_MIDDLE_NOCONTEXT,
&diff_opts, "b/file.txt", 0100644));
}
void test_apply_fromfile__change_firstline(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL,
FILE_CHANGE_FIRSTLINE, PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE, NULL,
"b/file.txt", 0100644));
}
void test_apply_fromfile__lastline(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL,
FILE_CHANGE_LASTLINE, PATCH_ORIGINAL_TO_CHANGE_LASTLINE, NULL,
"b/file.txt", 0100644));
}
void test_apply_fromfile__prepend(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL, FILE_PREPEND,
PATCH_ORIGINAL_TO_PREPEND, NULL, "b/file.txt", 0100644));
}
void test_apply_fromfile__prepend_nocontext(void)
{
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
diff_opts.context_lines = 0;
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL, FILE_PREPEND,
PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT, &diff_opts,
"b/file.txt", 0100644));
}
void test_apply_fromfile__append(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL, FILE_APPEND,
PATCH_ORIGINAL_TO_APPEND, NULL, "b/file.txt", 0100644));
}
void test_apply_fromfile__append_nocontext(void)
{
git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
diff_opts.context_lines = 0;
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL, FILE_APPEND,
PATCH_ORIGINAL_TO_APPEND_NOCONTEXT, &diff_opts,
"b/file.txt", 0100644));
}
void test_apply_fromfile__prepend_and_append(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL,
FILE_PREPEND_AND_APPEND, PATCH_ORIGINAL_TO_PREPEND_AND_APPEND, NULL,
"b/file.txt", 0100644));
}
void test_apply_fromfile__to_empty_file(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL, "",
PATCH_ORIGINAL_TO_EMPTY_FILE, NULL, "b/file.txt", 0100644));
}
void test_apply_fromfile__from_empty_file(void)
{
cl_git_pass(validate_and_apply_patchfile("", FILE_ORIGINAL,
PATCH_EMPTY_FILE_TO_ORIGINAL, NULL, "b/file.txt", 0100644));
}
void test_apply_fromfile__add(void)
{
cl_git_pass(validate_and_apply_patchfile(NULL, FILE_ORIGINAL,
PATCH_ADD_ORIGINAL, NULL, "b/file.txt", 0100644));
}
void test_apply_fromfile__delete(void)
{
cl_git_pass(validate_and_apply_patchfile(FILE_ORIGINAL, NULL,
PATCH_DELETE_ORIGINAL, NULL, NULL, 0));
}
void test_apply_fromfile__rename_exact(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_ORIGINAL,
PATCH_RENAME_EXACT, "b/newfile.txt", 0100644));
}
void test_apply_fromfile__rename_similar(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_CHANGE_MIDDLE,
PATCH_RENAME_SIMILAR, "b/newfile.txt", 0100644));
}
void test_apply_fromfile__rename_similar_quotedname(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_CHANGE_MIDDLE,
PATCH_RENAME_SIMILAR_QUOTEDNAME, "b/foo\"bar.txt", 0100644));
}
void test_apply_fromfile__modechange(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_ORIGINAL,
PATCH_MODECHANGE_UNCHANGED, "b/file.txt", 0100755));
}
void test_apply_fromfile__modechange_with_modification(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_CHANGE_MIDDLE,
PATCH_MODECHANGE_MODIFIED, "b/file.txt", 0100755));
}
void test_apply_fromfile__noisy(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_CHANGE_MIDDLE,
PATCH_NOISY, "b/file.txt", 0100644));
}
void test_apply_fromfile__noisy_nocontext(void)
{
cl_git_pass(apply_patchfile(FILE_ORIGINAL, FILE_CHANGE_MIDDLE,
PATCH_NOISY_NOCONTEXT, "b/file.txt", 0100644));
}
void test_apply_fromfile__fail_truncated_1(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch, PATCH_TRUNCATED_1,
strlen(PATCH_TRUNCATED_1)));
}
void test_apply_fromfile__fail_truncated_2(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch, PATCH_TRUNCATED_2,
strlen(PATCH_TRUNCATED_2)));
}
void test_apply_fromfile__fail_truncated_3(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch, PATCH_TRUNCATED_3,
strlen(PATCH_TRUNCATED_3)));
}
void test_apply_fromfile__fail_corrupt_githeader(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch, PATCH_CORRUPT_GIT_HEADER,
strlen(PATCH_CORRUPT_GIT_HEADER)));
}
void test_apply_fromfile__empty_context(void)
{
cl_git_pass(apply_patchfile(FILE_EMPTY_CONTEXT_ORIGINAL,
FILE_EMPTY_CONTEXT_MODIFIED, PATCH_EMPTY_CONTEXT,
"b/file.txt", 0100644));
}
void test_apply_fromfile__append_no_nl(void)
{
cl_git_pass(validate_and_apply_patchfile(
FILE_ORIGINAL, FILE_APPEND_NO_NL, PATCH_APPEND_NO_NL, NULL, "b/file.txt", 0100644));
}
void test_apply_fromfile__fail_missing_new_file(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch,
PATCH_CORRUPT_MISSING_NEW_FILE,
strlen(PATCH_CORRUPT_MISSING_NEW_FILE)));
}
void test_apply_fromfile__fail_missing_old_file(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch,
PATCH_CORRUPT_MISSING_OLD_FILE,
strlen(PATCH_CORRUPT_MISSING_OLD_FILE)));
}
void test_apply_fromfile__fail_no_changes(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch,
PATCH_CORRUPT_NO_CHANGES,
strlen(PATCH_CORRUPT_NO_CHANGES)));
}
void test_apply_fromfile__fail_missing_hunk_header(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch,
PATCH_CORRUPT_MISSING_HUNK_HEADER,
strlen(PATCH_CORRUPT_MISSING_HUNK_HEADER)));
}
void test_apply_fromfile__fail_not_a_patch(void)
{
git_patch *patch;
cl_git_fail(git_patch_from_patchfile(&patch, PATCH_NOT_A_PATCH,
strlen(PATCH_NOT_A_PATCH)));
}
#include "clar_libgit2.h"
#include "buffer.h"
static void expect_pass(const char *expected, const char *quoted)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_puts(&buf, quoted));
cl_git_pass(git_buf_unquote(&buf));
cl_assert_equal_s(expected, git_buf_cstr(&buf));
cl_assert_equal_i(strlen(expected), git_buf_len(&buf));
git_buf_free(&buf);
}
static void expect_fail(const char *quoted)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_buf_puts(&buf, quoted));
cl_git_fail(git_buf_unquote(&buf));
git_buf_free(&buf);
}
void test_buf_quote__unquote_succeeds(void)
{
expect_pass("", "\"\"");
expect_pass(" ", "\" \"");
expect_pass("foo", "\"foo\"");
expect_pass("foo bar", "\"foo bar\"");
expect_pass("foo\"bar", "\"foo\\\"bar\"");
expect_pass("foo\\bar", "\"foo\\\\bar\"");
expect_pass("foo\tbar", "\"foo\\tbar\"");
expect_pass("\vfoo\tbar\n", "\"\\vfoo\\tbar\\n\"");
expect_pass("foo\nbar", "\"foo\\012bar\"");
expect_pass("foo\r\nbar", "\"foo\\015\\012bar\"");
expect_pass("foo\r\nbar", "\"\\146\\157\\157\\015\\012\\142\\141\\162\"");
expect_pass("newline: \n", "\"newline: \\012\"");
}
void test_buf_quote__unquote_fails(void)
{
expect_fail("no quotes at all");
expect_fail("\"no trailing quote");
expect_fail("no leading quote\"");
expect_fail("\"invalid \\z escape char\"");
expect_fail("\"\\q invalid escape char\"");
expect_fail("\"invalid escape char \\p\"");
expect_fail("\"invalid \\1 escape char \"");
expect_fail("\"invalid \\14 escape char \"");
expect_fail("\"invalid \\411 escape char\"");
expect_fail("\"truncated escape char \\\"");
expect_fail("\"truncated escape char \\0\"");
expect_fail("\"truncated escape char \\01\"");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment