summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--examples/README.md11
-rw-r--r--include/git2/diff.h2
-rw-r--r--include/git2/tree.h67
-rw-r--r--src/diff_output.c2
-rw-r--r--src/index.c2
-rw-r--r--src/notes.c2
-rw-r--r--src/path.c4
-rw-r--r--src/tree.c235
-rw-r--r--src/tree.h6
-rw-r--r--tests-clar/diff/blob.c70
-rw-r--r--tests-clar/diff/iterator.c4
-rw-r--r--tests-clar/object/tree/frompath.c76
-rw-r--r--tests-clar/resources/attr/.gitted/indexbin1856 -> 1856 bytes
-rw-r--r--tests-clar/resources/attr/.gitted/logs/HEAD1
-rw-r--r--tests-clar/resources/attr/.gitted/logs/refs/heads/master1
-rw-r--r--tests-clar/resources/attr/.gitted/objects/16/983da6643656bb44c43965ecb6855c6d574512bin0 -> 446 bytes
-rw-r--r--tests-clar/resources/attr/.gitted/objects/8d/0b9df9bd30be7910ddda60548d485bc302b9111
-rw-r--r--tests-clar/resources/attr/.gitted/objects/a0/f7217ae99f5ac3e88534f5cea267febc5fa85b1
-rw-r--r--tests-clar/resources/attr/.gitted/refs/heads/master2
-rw-r--r--tests-clar/resources/attr/root_test4.txt4
21 files changed, 313 insertions, 180 deletions
diff --git a/README.md b/README.md
index 4c23fc870..e8dc36a16 100644
--- a/README.md
+++ b/README.md
@@ -89,7 +89,7 @@ Here are the bindings to libgit2 that are currently available:
* Go
* go-git <https://github.com/str1ngs/go-git>
* GObject
- * libgit2-glib <http://git.gnome.org/browse/libgit2-glib>
+ * libgit2-glib <https://live.gnome.org/Libgit2-glib>
* Haskell
* hgit2 <https://github.com/norm2782/hgit2>
* Lua
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 000000000..f2b6d7d23
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,11 @@
+libgit2 examples
+================
+
+These examples are meant as thin, easy-to-read snippets for Docurium
+(https://github.com/github/docurium) rather than full-blown
+implementations of Git commands. They are not vetted as carefully
+for bugs, error handling, or cross-platform compatibility as the
+rest of the code in libgit2, so copy with some caution.
+
+For HTML versions, check "Examples" at http://libgit2.github.com/libgit2
+
diff --git a/include/git2/diff.h b/include/git2/diff.h
index d4d0eac47..edec9957b 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -60,7 +60,7 @@ enum {
typedef struct {
uint32_t flags; /**< defaults to GIT_DIFF_NORMAL */
uint16_t context_lines; /**< defaults to 3 */
- uint16_t interhunk_lines; /**< defaults to 3 */
+ uint16_t interhunk_lines; /**< defaults to 0 */
char *old_prefix; /**< defaults to "a" */
char *new_prefix; /**< defaults to "b" */
git_strarray pathspec; /**< defaults to show all paths */
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 8f62e752a..f12b15e2e 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -46,7 +46,11 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git
* @param len the length of the short identifier
* @return 0 or an error code
*/
-GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, const git_oid *id, unsigned int len)
+GIT_INLINE(int) git_tree_lookup_prefix(
+ git_tree **tree,
+ git_repository *repo,
+ const git_oid *id,
+ unsigned int len)
{
return git_object_lookup_prefix((git_object **)tree, repo, id, len, GIT_OBJ_TREE);
}
@@ -62,12 +66,33 @@ GIT_INLINE(int) git_tree_lookup_prefix(git_tree **tree, git_repository *repo, co
*
* @param tree the tree to close
*/
-
GIT_INLINE(void) git_tree_free(git_tree *tree)
{
git_object_free((git_object *) tree);
}
+/**
+ * Free a tree entry
+ *
+ * IMPORTANT: This function is only needed for tree
+ * entries owned by the user, such as the ones returned
+ * by `git_tree_entry_dup`.
+ *
+ * @param entry The entry to free
+ */
+GIT_EXTERN(void) git_tree_entry_free(git_tree_entry *entry);
+
+/**
+ * Duplicate a tree entry
+ *
+ * Create a copy of a tree entry. The returned copy is owned
+ * by the user, and must be freed manually with
+ * `git_tree_entry_free`.
+ *
+ * @param entry A tree entry to duplicate
+ * @return a copy of the original entry
+ */
+GIT_EXTERN(git_tree_entry *) git_tree_entry_dup(const git_tree_entry *entry);
/**
* Get the id of a tree.
@@ -143,7 +168,10 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
* @param entry a tree entry
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_tree_entry_to_object(git_object **object_out, git_repository *repo, const git_tree_entry *entry);
+GIT_EXTERN(int) git_tree_entry_to_object(
+ git_object **object_out,
+ git_repository *repo,
+ const git_tree_entry *entry);
/**
* Write a tree to the ODB from the index file
@@ -231,7 +259,12 @@ GIT_EXTERN(const git_tree_entry *) git_treebuilder_get(git_treebuilder *bld, con
* @param attributes Folder attributes of the entry
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes);
+GIT_EXTERN(int) git_treebuilder_insert(
+ const git_tree_entry **entry_out,
+ git_treebuilder *bld,
+ const char *filename,
+ const git_oid *id,
+ unsigned int attributes);
/**
* Remove an entry from the builder by its filename
@@ -252,7 +285,10 @@ GIT_EXTERN(int) git_treebuilder_remove(git_treebuilder *bld, const char *filenam
* @param bld Tree builder
* @param filter Callback to filter entries
*/
-GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(const git_tree_entry *, void *), void *payload);
+GIT_EXTERN(void) git_treebuilder_filter(
+ git_treebuilder *bld,
+ int (*filter)(const git_tree_entry *, void *),
+ void *payload);
/**
* Write the contents of the tree builder as a tree object
@@ -269,21 +305,24 @@ GIT_EXTERN(void) git_treebuilder_filter(git_treebuilder *bld, int (*filter)(cons
GIT_EXTERN(int) git_treebuilder_write(git_oid *oid, git_repository *repo, git_treebuilder *bld);
/**
- * Retrieve a subtree contained in a tree, given its
- * relative path.
+ * Retrieve a tree entry contained in a tree or in any
+ * of its subtrees, given its relative path.
*
- * The returned tree is owned by the repository and
- * should be closed with the `git_object_free` method.
+ * The returned tree entry is owned by the user and must
+ * be freed manually with `git_tree_entry_free`.
*
- * @param subtree Pointer where to store the subtree
+ * @param entry Pointer where to store the tree entry
* @param root A previously loaded tree which will be the root of the relative path
- * @param subtree_path Path to the contained subtree
- * @return 0 on success; GIT_ENOTFOUND if the path does not lead to a subtree
+ * @param subtree_path Path to the contained entry
+ * @return 0 on success; GIT_ENOTFOUND if the path does not exist
*/
-GIT_EXTERN(int) git_tree_get_subtree(git_tree **subtree, git_tree *root, const char *subtree_path);
+GIT_EXTERN(int) git_tree_entry_bypath(
+ git_tree_entry **entry,
+ git_tree *root,
+ const char *path);
/** Callback for the tree traversal method */
-typedef int (*git_treewalk_cb)(const char *root, git_tree_entry *entry, void *payload);
+typedef int (*git_treewalk_cb)(const char *root, const git_tree_entry *entry, void *payload);
/** Tree traversal modes */
enum git_treewalk_mode {
diff --git a/src/diff_output.c b/src/diff_output.c
index 92f7f8f2f..f6650b345 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -212,7 +212,7 @@ static void setup_xdiff_options(
cfg->ctxlen =
(!opts || !opts->context_lines) ? 3 : opts->context_lines;
cfg->interhunkctxlen =
- (!opts || !opts->interhunk_lines) ? 3 : opts->interhunk_lines;
+ (!opts) ? 0 : opts->interhunk_lines;
if (!opts)
return;
diff --git a/src/index.c b/src/index.c
index 3fedcd27a..89d479870 100644
--- a/src/index.c
+++ b/src/index.c
@@ -985,7 +985,7 @@ int git_index_entry_stage(const git_index_entry *entry)
return (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT;
}
-static int read_tree_cb(const char *root, git_tree_entry *tentry, void *data)
+static int read_tree_cb(const char *root, const git_tree_entry *tentry, void *data)
{
git_index *index = data;
git_index_entry *entry = NULL;
diff --git a/src/notes.c b/src/notes.c
index 0dfd3f891..7813e9985 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -97,7 +97,7 @@ static int tree_write(
{
int error;
git_treebuilder *tb = NULL;
- git_tree_entry *entry;
+ const git_tree_entry *entry;
git_oid tree_oid;
if ((error = git_treebuilder_create(&tb, source_tree)) < 0)
diff --git a/src/path.c b/src/path.c
index a6574b3de..9c88240e0 100644
--- a/src/path.c
+++ b/src/path.c
@@ -515,7 +515,7 @@ int git_path_direach(
return -1;
}
-#ifdef __sun
+#if defined(__sun) || defined(__GNU__)
de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
#else
de_buf = git__malloc(sizeof(struct dirent));
@@ -569,7 +569,7 @@ int git_path_dirload(
return -1;
}
-#ifdef __sun
+#if defined(__sun) || defined(__GNU__)
de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
#else
de_buf = git__malloc(sizeof(struct dirent));
diff --git a/src/tree.c b/src/tree.c
index 9bdc2180c..b609eea33 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -35,6 +35,22 @@ static int entry_sort_cmp(const void *a, const void *b)
entry_b->filename, entry_b->filename_len, git_tree_entry__is_tree(entry_b));
}
+static git_tree_entry *alloc_entry(const char *filename)
+{
+ git_tree_entry *entry = NULL;
+ size_t filename_len = strlen(filename);
+
+ entry = git__malloc(sizeof(git_tree_entry) + filename_len + 1);
+ if (!entry)
+ return NULL;
+
+ memset(entry, 0x0, sizeof(git_tree_entry));
+ memcpy(entry->filename, filename, filename_len);
+ entry->filename[filename_len] = 0;
+ entry->filename_len = filename_len;
+
+ return entry;
+}
struct tree_key_search {
const char *filename;
@@ -76,7 +92,7 @@ static int homing_search_cmp(const void *key, const void *array_member)
* ambiguous because of folder vs file sorting, we look linearly
* around the area for our target file.
*/
-static int tree_key_search(git_vector *entries, const char *filename)
+static int tree_key_search(git_vector *entries, const char *filename, size_t filename_len)
{
struct tree_key_search ksearch;
const git_tree_entry *entry;
@@ -84,7 +100,7 @@ static int tree_key_search(git_vector *entries, const char *filename)
int homing, i;
ksearch.filename = filename;
- ksearch.filename_len = strlen(filename);
+ ksearch.filename_len = filename_len;
/* Initial homing search; find an entry on the tree with
* the same prefix as the filename we're looking for */
@@ -100,7 +116,8 @@ static int tree_key_search(git_vector *entries, const char *filename)
if (homing_search_cmp(&ksearch, entry) < 0)
break;
- if (strcmp(filename, entry->filename) == 0)
+ if (entry->filename_len == filename_len &&
+ memcmp(filename, entry->filename, filename_len) == 0)
return i;
}
@@ -112,7 +129,8 @@ static int tree_key_search(git_vector *entries, const char *filename)
if (homing_search_cmp(&ksearch, entry) > 0)
break;
- if (strcmp(filename, entry->filename) == 0)
+ if (entry->filename_len == filename_len &&
+ memcmp(filename, entry->filename, filename_len) == 0)
return i;
}
@@ -120,16 +138,35 @@ static int tree_key_search(git_vector *entries, const char *filename)
return GIT_ENOTFOUND;
}
+void git_tree_entry_free(git_tree_entry *entry)
+{
+ git__free(entry);
+}
+
+git_tree_entry *git_tree_entry_dup(const git_tree_entry *entry)
+{
+ size_t total_size;
+ git_tree_entry *copy;
+
+ assert(entry);
+
+ total_size = sizeof(git_tree_entry) + entry->filename_len + 1;
+
+ copy = git__malloc(total_size);
+ if (!copy)
+ return NULL;
+
+ memcpy(copy, entry, total_size);
+ return copy;
+}
+
void git_tree__free(git_tree *tree)
{
unsigned int i;
for (i = 0; i < tree->entries.length; ++i) {
- git_tree_entry *e;
- e = git_vector_get(&tree->entries, i);
-
- git__free(e->filename);
- git__free(e);
+ git_tree_entry *e = git_vector_get(&tree->entries, i);
+ git_tree_entry_free(e);
}
git_vector_free(&tree->entries);
@@ -179,19 +216,21 @@ int git_tree_entry_to_object(
return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY);
}
-const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
+static git_tree_entry *entry_fromname(git_tree *tree, const char *name, size_t name_len)
{
- int idx;
-
- assert(tree && filename);
-
- idx = tree_key_search(&tree->entries, filename);
- if (idx == GIT_ENOTFOUND)
+ int idx = tree_key_search(&tree->entries, name, name_len);
+ if (idx < 0)
return NULL;
return git_vector_get(&tree->entries, idx);
}
+const git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
+{
+ assert(tree && filename);
+ return entry_fromname(tree, filename, strlen(filename));
+}
+
const git_tree_entry *git_tree_entry_byindex(git_tree *tree, unsigned int idx)
{
assert(tree);
@@ -244,28 +283,28 @@ static int tree_parse_buffer(git_tree *tree, const char *buffer, const char *buf
while (buffer < buffer_end) {
git_tree_entry *entry;
- int tmp;
+ int attr;
- entry = git__calloc(1, sizeof(git_tree_entry));
- GITERR_CHECK_ALLOC(entry);
-
- if (git_vector_insert(&tree->entries, entry) < 0)
- return -1;
-
- if (git__strtol32(&tmp, buffer, &buffer, 8) < 0 ||
- !buffer || !valid_attributes(tmp))
+ if (git__strtol32(&attr, buffer, &buffer, 8) < 0 ||
+ !buffer || !valid_attributes(attr))
return tree_error("Failed to parse tree. Can't parse attributes");
- entry->attr = tmp;
-
if (*buffer++ != ' ')
return tree_error("Failed to parse tree. Object is corrupted");
if (memchr(buffer, 0, buffer_end - buffer) == NULL)
return tree_error("Failed to parse tree. Object is corrupted");
- entry->filename = git__strdup(buffer);
- entry->filename_len = strlen(buffer);
+ /** Allocate the entry and store it in the entries vector */
+ {
+ entry = alloc_entry(buffer);
+ GITERR_CHECK_ALLOC(entry);
+
+ if (git_vector_insert(&tree->entries, entry) < 0)
+ return -1;
+
+ entry->attr = attr;
+ }
while (buffer < buffer_end && *buffer != 0)
buffer++;
@@ -303,16 +342,17 @@ static unsigned int find_next_dir(const char *dirname, git_index *index, unsigne
return i;
}
-static int append_entry(git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
+static int append_entry(
+ git_treebuilder *bld,
+ const char *filename,
+ const git_oid *id,
+ unsigned int attributes)
{
git_tree_entry *entry;
- entry = git__calloc(1, sizeof(git_tree_entry));
+ entry = alloc_entry(filename);
GITERR_CHECK_ALLOC(entry);
- entry->filename = git__strdup(filename);
- entry->filename_len = strlen(entry->filename);
-
git_oid_cpy(&entry->oid, id);
entry->attr = attributes;
@@ -488,7 +528,12 @@ on_error:
return -1;
}
-int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, const char *filename, const git_oid *id, unsigned int attributes)
+int git_treebuilder_insert(
+ const git_tree_entry **entry_out,
+ git_treebuilder *bld,
+ const char *filename,
+ const git_oid *id,
+ unsigned int attributes)
{
git_tree_entry *entry;
int pos;
@@ -501,30 +546,28 @@ int git_treebuilder_insert(git_tree_entry **entry_out, git_treebuilder *bld, con
if (!valid_entry_name(filename))
return tree_error("Failed to insert entry. Invalid name for a tree entry");
- pos = tree_key_search(&bld->entries, filename);
+ pos = tree_key_search(&bld->entries, filename, strlen(filename));
if (pos >= 0) {
entry = git_vector_get(&bld->entries, pos);
if (entry->removed)
entry->removed = 0;
} else {
- entry = git__calloc(1, sizeof(git_tree_entry));
+ entry = alloc_entry(filename);
GITERR_CHECK_ALLOC(entry);
-
- entry->filename = git__strdup(filename);
- entry->filename_len = strlen(entry->filename);
}
git_oid_cpy(&entry->oid, id);
entry->attr = attributes;
- if (pos == GIT_ENOTFOUND) {
+ if (pos < 0) {
if (git_vector_insert(&bld->entries, entry) < 0)
return -1;
}
- if (entry_out != NULL)
+ if (entry_out != NULL) {
*entry_out = entry;
+ }
return 0;
}
@@ -536,7 +579,7 @@ static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filenam
assert(bld && filename);
- idx = tree_key_search(&bld->entries, filename);
+ idx = tree_key_search(&bld->entries, filename, strlen(filename));
if (idx < 0)
return NULL;
@@ -625,8 +668,7 @@ void git_treebuilder_clear(git_treebuilder *bld)
for (i = 0; i < bld->entries.length; ++i) {
git_tree_entry *e = bld->entries.contents[i];
- git__free(e->filename);
- git__free(e);
+ git_tree_entry_free(e);
}
git_vector_clear(&bld->entries);
@@ -642,85 +684,78 @@ void git_treebuilder_free(git_treebuilder *bld)
git__free(bld);
}
-static int tree_frompath(
- git_tree **parent_out,
+static size_t subpath_len(const char *path)
+{
+ const char *slash_pos = strchr(path, '/');
+ if (slash_pos == NULL)
+ return strlen(path);
+
+ return slash_pos - path;
+}
+
+int git_tree_entry_bypath(
+ git_tree_entry **entry_out,
git_tree *root,
- git_buf *treeentry_path,
- size_t offset)
+ const char *path)
{
- char *slash_pos = NULL;
- const git_tree_entry* entry;
int error = 0;
git_tree *subtree;
+ const git_tree_entry *entry;
+ size_t filename_len;
- if (!*(treeentry_path->ptr + offset)) {
- giterr_set(GITERR_INVALID,
- "Invalid relative path to a tree entry '%s'.", treeentry_path->ptr);
- return -1;
- }
-
- slash_pos = (char *)strchr(treeentry_path->ptr + offset, '/');
+ /* Find how long is the current path component (i.e.
+ * the filename between two slashes */
+ filename_len = subpath_len(path);
- if (slash_pos == NULL)
- return git_tree_lookup(
- parent_out,
- root->object.repo,
- git_object_id((const git_object *)root)
- );
-
- if (slash_pos == treeentry_path->ptr + offset) {
- giterr_set(GITERR_INVALID,
- "Invalid relative path to a tree entry '%s'.", treeentry_path->ptr);
- return -1;
+ if (filename_len == 0) {
+ giterr_set(GITERR_TREE, "Invalid tree path given");
+ return GIT_ENOTFOUND;
}
- *slash_pos = '\0';
-
- entry = git_tree_entry_byname(root, treeentry_path->ptr + offset);
-
- if (slash_pos != NULL)
- *slash_pos = '/';
+ entry = entry_fromname(root, path, filename_len);
if (entry == NULL) {
giterr_set(GITERR_TREE,
- "No tree entry can be found from "
- "the given tree and relative path '%s'.", treeentry_path->ptr);
+ "The path '%s' does not exist in the given tree", path);
return GIT_ENOTFOUND;
}
+ switch (path[filename_len]) {
+ case '/':
+ /* If there are more components in the path...
+ * then this entry *must* be a tree */
+ if (!git_tree_entry__is_tree(entry)) {
+ giterr_set(GITERR_TREE,
+ "The path '%s' does not exist in the given tree", path);
+ return -1;
+ }
+
+ /* If there's only a slash left in the path, we
+ * return the current entry; otherwise, we keep
+ * walking down the path */
+ if (path[filename_len + 1] != '\0')
+ break;
+
+ case '\0':
+ /* If there are no more components in the path, return
+ * this entry */
+ *entry_out = git_tree_entry_dup(entry);
+ return 0;
+ }
if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0)
- return error;
+ return -1;
- error = tree_frompath(
- parent_out,
+ error = git_tree_entry_bypath(
+ entry_out,
subtree,
- treeentry_path,
- (slash_pos - treeentry_path->ptr) + 1
+ path + filename_len + 1
);
git_tree_free(subtree);
return error;
}
-int git_tree_get_subtree(
- git_tree **subtree,
- git_tree *root,
- const char *subtree_path)
-{
- int error;
- git_buf buffer = GIT_BUF_INIT;
-
- assert(subtree && root && subtree_path);
-
- if ((error = git_buf_sets(&buffer, subtree_path)) == 0)
- error = tree_frompath(subtree, root, &buffer, 0);
-
- git_buf_free(&buffer);
-
- return error;
-}
-
static int tree_walk_post(
git_tree *tree,
git_treewalk_cb callback,
diff --git a/src/tree.h b/src/tree.h
index 498a90d66..c49309cbc 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -13,11 +13,11 @@
#include "vector.h"
struct git_tree_entry {
- unsigned int attr;
- char *filename;
+ uint16_t removed;
+ uint16_t attr;
git_oid oid;
size_t filename_len;
- int removed;
+ char filename[1];
};
struct git_tree {
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index 6d7ad41d6..5d3ab8d56 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -14,13 +14,13 @@ void test_diff_blob__initialize(void)
memset(&opts, 0, sizeof(opts));
opts.context_lines = 1;
- opts.interhunk_lines = 1;
+ opts.interhunk_lines = 0;
memset(&expected, 0, sizeof(expected));
/* tests/resources/attr/root_test4.txt */
- cl_git_pass(git_oid_fromstrn(&oid, "fe773770c5a6", 12));
- cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 6));
+ cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8));
+ cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 4));
/* alien.png */
cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8));
@@ -54,6 +54,7 @@ void test_diff_blob__can_compare_text_blobs(void)
/* Doing the equivalent of a `git diff -U1` on these files */
+ /* diff on tests/resources/attr/root_test1 */
cl_git_pass(git_diff_blobs(
a, b, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
@@ -67,6 +68,7 @@ void test_diff_blob__can_compare_text_blobs(void)
cl_assert(expected.line_adds == 5);
cl_assert(expected.line_dels == 0);
+ /* diff on tests/resources/attr/root_test2 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
b, c, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
@@ -81,6 +83,7 @@ void test_diff_blob__can_compare_text_blobs(void)
cl_assert(expected.line_adds == 9);
cl_assert(expected.line_dels == 3);
+ /* diff on tests/resources/attr/root_test3 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
a, c, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
@@ -95,8 +98,6 @@ void test_diff_blob__can_compare_text_blobs(void)
cl_assert(expected.line_adds == 12);
cl_assert(expected.line_dels == 1);
- opts.context_lines = 1;
-
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
c, d, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
@@ -252,3 +253,62 @@ void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void)
assert_binary_blobs_comparison(expected);
}
+
+/*
+ * $ git diff fe773770 a0f7217
+ * diff --git a/fe773770 b/a0f7217
+ * index fe77377..a0f7217 100644
+ * --- a/fe773770
+ * +++ b/a0f7217
+ * @@ -1,6 +1,6 @@
+ * Here is some stuff at the start
+ *
+ * -This should go in one hunk
+ * +This should go in one hunk (first)
+ *
+ * Some additional lines
+ *
+ * @@ -8,7 +8,7 @@ Down here below the other lines
+ *
+ * With even more at the end
+ *
+ * -Followed by a second hunk of stuff
+ * +Followed by a second hunk of stuff (second)
+ *
+ * That happens down here
+ */
+void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void)
+{
+ git_blob *old_d;
+ git_oid old_d_oid;
+
+ opts.context_lines = 3;
+
+ /* tests/resources/attr/root_test1 from commit f5b0af1 */
+ cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8));
+ cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 4));
+
+ /* Test with default inter-hunk-context (not set) => default is 0 */
+ cl_git_pass(git_diff_blobs(
+ old_d, d, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(expected.hunks == 2);
+
+ /* Test with inter-hunk-context explicitly set to 0 */
+ opts.interhunk_lines = 0;
+ memset(&expected, 0, sizeof(expected));
+ cl_git_pass(git_diff_blobs(
+ old_d, d, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(expected.hunks == 2);
+
+ /* Test with inter-hunk-context explicitly set to 1 */
+ opts.interhunk_lines = 1;
+ memset(&expected, 0, sizeof(expected));
+ cl_git_pass(git_diff_blobs(
+ old_d, d, &opts, &expected, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(expected.hunks == 1);
+
+ git_blob_free(old_d);
+}
diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c
index eee84810a..c27d3fa6c 100644
--- a/tests-clar/diff/iterator.c
+++ b/tests-clar/diff/iterator.c
@@ -312,7 +312,7 @@ static const char *expected_index_oids_0[] = {
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
- "fe773770c5a6cc7185580c9204b1ff18a33ff3fc",
+ "a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
"3e42ffc54a663f9401cc25843d6c0e71a33e4249",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
@@ -343,7 +343,7 @@ static const char *expected_index_oids_range[] = {
"45141a79a77842c59a63229403220a4e4be74e3d",
"4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d",
"108bb4e7fd7b16490dc33ff7d972151e73d7166e",
- "fe773770c5a6cc7185580c9204b1ff18a33ff3fc",
+ "a0f7217ae99f5ac3e88534f5cea267febc5fa85b",
};
void test_diff_iterator__index_range(void)
diff --git a/tests-clar/object/tree/frompath.c b/tests-clar/object/tree/frompath.c
index 06c69ac08..ef092d1db 100644
--- a/tests-clar/object/tree/frompath.c
+++ b/tests-clar/object/tree/frompath.c
@@ -1,15 +1,14 @@
#include "clar_libgit2.h"
static git_repository *repo;
-const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
static git_tree *tree;
void test_object_tree_frompath__initialize(void)
{
git_oid id;
+ const char *tree_with_subtrees_oid = "ae90f12eea699729ed24555e40b9fd669da12a12";
- cl_fixture_sandbox("testrepo.git");
- cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+ cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
cl_assert(repo != NULL);
cl_git_pass(git_oid_fromstr(&id, tree_with_subtrees_oid));
@@ -21,61 +20,46 @@ void test_object_tree_frompath__cleanup(void)
{
git_tree_free(tree);
git_repository_free(repo);
- cl_fixture_cleanup("testrepo.git");
}
-static void assert_tree_from_path(git_tree *root, const char *path, int expected_result, const char *expected_raw_oid)
+static void assert_tree_from_path(
+ git_tree *root,
+ const char *path,
+ const char *expected_entry_name)
{
- git_tree *containing_tree = NULL;
+ git_tree_entry *entry;
- cl_assert(git_tree_get_subtree(&containing_tree, root, path) == expected_result);
-
- if (containing_tree == NULL && expected_result != 0)
- return;
-
- cl_assert(containing_tree != NULL && expected_result == 0);
-
- cl_git_pass(git_oid_streq(git_object_id((const git_object *)containing_tree), expected_raw_oid));
-
- git_tree_free(containing_tree);
-}
-
-static void assert_tree_from_path_klass(git_tree *root, const char *path, int expected_result, const char *expected_raw_oid)
-{
- assert_tree_from_path(root, path, GIT_ERROR, expected_raw_oid);
- cl_assert(giterr_last()->klass == expected_result);
+ cl_git_pass(git_tree_entry_bypath(&entry, root, path));
+ cl_assert_equal_s(git_tree_entry_name(entry), expected_entry_name);
+ git_tree_entry_free(entry);
}
void test_object_tree_frompath__retrieve_tree_from_path_to_treeentry(void)
{
- /* Will return self if given a one path segment... */
- assert_tree_from_path(tree, "README", 0, tree_with_subtrees_oid);
-
- /* ...even one that lead to a non existent tree entry. */
- assert_tree_from_path(tree, "i-do-not-exist.txt", 0, tree_with_subtrees_oid);
-
- /* Will return fgh tree oid given this following path... */
- assert_tree_from_path(tree, "ab/de/fgh/1.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54");
-
- /* ... and ab tree oid given this one. */
- assert_tree_from_path(tree, "ab/de", 0, "f1425cef211cc08caa31e7b545ffb232acb098c3");
+ git_tree_entry *e;
- /* Will succeed if given a valid path which leads to a tree entry which doesn't exist */
- assert_tree_from_path(tree, "ab/de/fgh/i-do-not-exist.txt", 0, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54");
-}
+ assert_tree_from_path(tree, "README", "README");
+ assert_tree_from_path(tree, "ab/de/fgh/1.txt", "1.txt");
+ assert_tree_from_path(tree, "ab/de/fgh", "fgh");
+ assert_tree_from_path(tree, "ab/de/fgh/", "fgh");
+ assert_tree_from_path(tree, "ab/de", "de");
+ assert_tree_from_path(tree, "ab/", "ab");
+ assert_tree_from_path(tree, "ab/de/", "de");
-void test_object_tree_frompath__fail_when_processing_an_unknown_tree_segment(void)
-{
- assert_tree_from_path(tree, "nope/de/fgh/1.txt", GIT_ENOTFOUND, NULL);
- assert_tree_from_path(tree, "ab/me-neither/fgh/2.txt", GIT_ENOTFOUND, NULL);
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "README/"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "nope/de/fgh/1.txt"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt/"));
}
void test_object_tree_frompath__fail_when_processing_an_invalid_path(void)
{
- assert_tree_from_path_klass(tree, "/", GITERR_INVALID, NULL);
- assert_tree_from_path_klass(tree, "/ab", GITERR_INVALID, NULL);
- assert_tree_from_path_klass(tree, "/ab/de", GITERR_INVALID, NULL);
- assert_tree_from_path_klass(tree, "ab/", GITERR_INVALID, NULL);
- assert_tree_from_path_klass(tree, "ab//de", GITERR_INVALID, NULL);
- assert_tree_from_path_klass(tree, "ab/de/", GITERR_INVALID, NULL);
+ git_tree_entry *e;
+
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "/"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "/ab/de"));
+ cl_must_fail(git_tree_entry_bypath(&e, tree, "ab//de"));
}
diff --git a/tests-clar/resources/attr/.gitted/index b/tests-clar/resources/attr/.gitted/index
index 1d60eab8f..943e2243e 100644
--- a/tests-clar/resources/attr/.gitted/index
+++ b/tests-clar/resources/attr/.gitted/index
Binary files differ
diff --git a/tests-clar/resources/attr/.gitted/logs/HEAD b/tests-clar/resources/attr/.gitted/logs/HEAD
index 73f00f345..8ece39f37 100644
--- a/tests-clar/resources/attr/.gitted/logs/HEAD
+++ b/tests-clar/resources/attr/.gitted/logs/HEAD
@@ -6,3 +6,4 @@ a5d76cad53f66f1312bd995909a5bab3c0820770 370fe9ec224ce33e71f9e5ec2bd1142ce9937a6
f5b0af1fb4f5c0cd7aad880711d368a07333c307 a97cc019851d401a4f1d091cb91a15890a0dd1ba Russell Belfer <arrbee@arrbee.com> 1328653313 -0800 commit: Some whitespace only changes for testing purposes
a97cc019851d401a4f1d091cb91a15890a0dd1ba 217878ab49e1314388ea2e32dc6fdb58a1b969e0 Russell Belfer <arrbee@arrbee.com> 1332734901 -0700 commit: added files in sub/sub
217878ab49e1314388ea2e32dc6fdb58a1b969e0 24fa9a9fc4e202313e24b648087495441dab432b Russell Belfer <arrbee@arrbee.com> 1332735555 -0700 commit: adding more files in sub for tree status
+24fa9a9fc4e202313e24b648087495441dab432b 8d0b9df9bd30be7910ddda60548d485bc302b911 yorah <yoram.harmelin@gmail.com> 1341230701 +0200 commit: Updating test data so we can test inter-hunk-context
diff --git a/tests-clar/resources/attr/.gitted/logs/refs/heads/master b/tests-clar/resources/attr/.gitted/logs/refs/heads/master
index 73f00f345..8ece39f37 100644
--- a/tests-clar/resources/attr/.gitted/logs/refs/heads/master
+++ b/tests-clar/resources/attr/.gitted/logs/refs/heads/master
@@ -6,3 +6,4 @@ a5d76cad53f66f1312bd995909a5bab3c0820770 370fe9ec224ce33e71f9e5ec2bd1142ce9937a6
f5b0af1fb4f5c0cd7aad880711d368a07333c307 a97cc019851d401a4f1d091cb91a15890a0dd1ba Russell Belfer <arrbee@arrbee.com> 1328653313 -0800 commit: Some whitespace only changes for testing purposes
a97cc019851d401a4f1d091cb91a15890a0dd1ba 217878ab49e1314388ea2e32dc6fdb58a1b969e0 Russell Belfer <arrbee@arrbee.com> 1332734901 -0700 commit: added files in sub/sub
217878ab49e1314388ea2e32dc6fdb58a1b969e0 24fa9a9fc4e202313e24b648087495441dab432b Russell Belfer <arrbee@arrbee.com> 1332735555 -0700 commit: adding more files in sub for tree status
+24fa9a9fc4e202313e24b648087495441dab432b 8d0b9df9bd30be7910ddda60548d485bc302b911 yorah <yoram.harmelin@gmail.com> 1341230701 +0200 commit: Updating test data so we can test inter-hunk-context
diff --git a/tests-clar/resources/attr/.gitted/objects/16/983da6643656bb44c43965ecb6855c6d574512 b/tests-clar/resources/attr/.gitted/objects/16/983da6643656bb44c43965ecb6855c6d574512
new file mode 100644
index 000000000..e49c94acd
--- /dev/null
+++ b/tests-clar/resources/attr/.gitted/objects/16/983da6643656bb44c43965ecb6855c6d574512
Binary files differ
diff --git a/tests-clar/resources/attr/.gitted/objects/8d/0b9df9bd30be7910ddda60548d485bc302b911 b/tests-clar/resources/attr/.gitted/objects/8d/0b9df9bd30be7910ddda60548d485bc302b911
new file mode 100644
index 000000000..3dcf088e4
--- /dev/null
+++ b/tests-clar/resources/attr/.gitted/objects/8d/0b9df9bd30be7910ddda60548d485bc302b911
@@ -0,0 +1 @@
+xKj1D)zoli _"hiK2LG!7ȪJ,EPXDS ] /)}/UwR. jp##:?:|;F9܋r=_ )ơN/A[l!q}<Lfx4H\\q֏cjT \ No newline at end of file
diff --git a/tests-clar/resources/attr/.gitted/objects/a0/f7217ae99f5ac3e88534f5cea267febc5fa85b b/tests-clar/resources/attr/.gitted/objects/a0/f7217ae99f5ac3e88534f5cea267febc5fa85b
new file mode 100644
index 000000000..985c2e281
--- /dev/null
+++ b/tests-clar/resources/attr/.gitted/objects/a0/f7217ae99f5ac3e88534f5cea267febc5fa85b
@@ -0,0 +1 @@
+x510 E}?΀;S␈Ԯۓv8O'F:2r)( &޷9ZAѹr9l %3Eo.Vi<B FMb+*vmLKݭD?+N \ No newline at end of file
diff --git a/tests-clar/resources/attr/.gitted/refs/heads/master b/tests-clar/resources/attr/.gitted/refs/heads/master
index 8768776b3..b3abfff7d 100644
--- a/tests-clar/resources/attr/.gitted/refs/heads/master
+++ b/tests-clar/resources/attr/.gitted/refs/heads/master
@@ -1 +1 @@
-24fa9a9fc4e202313e24b648087495441dab432b
+8d0b9df9bd30be7910ddda60548d485bc302b911
diff --git a/tests-clar/resources/attr/root_test4.txt b/tests-clar/resources/attr/root_test4.txt
index fe773770c..a0f7217ae 100644
--- a/tests-clar/resources/attr/root_test4.txt
+++ b/tests-clar/resources/attr/root_test4.txt
@@ -1,6 +1,6 @@
Here is some stuff at the start
-This should go in one hunk
+This should go in one hunk (first)
Some additional lines
@@ -8,7 +8,7 @@ Down here below the other lines
With even more at the end
-Followed by a second hunk of stuff
+Followed by a second hunk of stuff (second)
That happens down here