diff options
50 files changed, 1443 insertions, 491 deletions
diff --git a/Makefile.embed b/Makefile.embed index 65f13b9b6..f46eaa4c1 100644 --- a/Makefile.embed +++ b/Makefile.embed @@ -1,15 +1,31 @@ +PLATFORM=$(shell uname -o) + rm=rm -f -CC=cc AR=ar cq RANLIB=ranlib LIBNAME=libgit2.a +ifeq ($(PLATFORM),Msys) + CC=gcc +else + CC=cc +endif INCLUDES= -I. -Isrc -Iinclude -Ideps/http-parser -Ideps/zlib DEFINES= $(INCLUDES) -DNO_VIZ -DSTDC -DNO_GZIP -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(EXTRA_DEFINES) -CFLAGS= -g $(DEFINES) -Wall -Wextra -fPIC -O2 $(EXTRA_CFLAGS) +CFLAGS= -g $(DEFINES) -Wall -Wextra -O2 $(EXTRA_CFLAGS) + +SRCS = $(wildcard src/*.c) $(wildcard src/transports/*.c) $(wildcard src/xdiff/*.c) $(wildcard deps/http-parser/*.c) $(wildcard deps/zlib/*.c) + +ifeq ($(PLATFORM),Msys) + SRCS += $(wildcard src/win32/*.c) $(wildcard src/compat/*.c) deps/regex/regex.c + INCLUDES += -Ideps/regex + DEFINES += -DWIN32 -D_WIN32_WINNT=0x0501 +else + SRCS += $(wildcard src/unix/*.c) + CFLAGS += -fPIC +endif -SRCS = $(wildcard src/*.c) $(wildcard src/transports/*.c) $(wildcard src/unix/*.c) $(wildcard src/xdiff/*.c) $(wildcard deps/http-parser/*.c) $(wildcard deps/zlib/*.c) OBJS = $(patsubst %.c,%.o,$(SRCS)) %.c.o: diff --git a/include/git2.h b/include/git2.h index 34601449c..f260cfacd 100644 --- a/include/git2.h +++ b/include/git2.h @@ -45,5 +45,6 @@ #include "git2/submodule.h" #include "git2/notes.h" #include "git2/reset.h" +#include "git2/message.h" #endif diff --git a/include/git2/branch.h b/include/git2/branch.h index e2432bcfc..8884df15a 100644 --- a/include/git2/branch.h +++ b/include/git2/branch.h @@ -72,15 +72,7 @@ GIT_EXTERN(int) git_branch_delete( git_branch_t branch_type); /** - * Fill a list with all the branches in the Repository - * - * The string array will be filled with the names of the - * matching branches; these values are owned by the user and - * should be free'd manually when no longer needed, using - * `git_strarray_free`. - * - * @param branch_names Pointer to a git_strarray structure - * where the branch names will be stored. + * Loop over all the branches and issue a callback for each one. * * @param repo Repository where to find the branches. * @@ -88,12 +80,21 @@ GIT_EXTERN(int) git_branch_delete( * listing. Valid values are GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE * or a combination of the two. * + * @param branch_cb Callback to invoke per found branch. + * + * @param payload Extra parameter to callback function. + * * @return 0 or an error code. */ -GIT_EXTERN(int) git_branch_list( - git_strarray *branch_names, +GIT_EXTERN(int) git_branch_foreach( git_repository *repo, - unsigned int list_flags); + unsigned int list_flags, + int (*branch_cb)( + const char *branch_name, + git_branch_t branch_type, + void *payload), + void *payload +); /** * Move/rename an existing branch reference. diff --git a/include/git2/commit.h b/include/git2/commit.h index a6d9bb0e3..640adf5c2 100644 --- a/include/git2/commit.h +++ b/include/git2/commit.h @@ -182,8 +182,8 @@ GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned i * Create a new commit in the repository using `git_object` * instances as parameters. * - * The message will be cleaned up from excess whitespace - * it will be made sure that the last line ends with a '\n'. + * The message will not be cleaned up. This can be achieved + * through `git_message_prettify()`. * * @param oid Pointer where to store the OID of the * newly created commit diff --git a/include/git2/index.h b/include/git2/index.h index 0fb0f955a..f863a6065 100644 --- a/include/git2/index.h +++ b/include/git2/index.h @@ -90,6 +90,14 @@ typedef struct git_index_entry_unmerged { char *path; } git_index_entry_unmerged; +/** Capabilities of system that affect index actions. */ +enum { + GIT_INDEXCAP_IGNORE_CASE = 1, + GIT_INDEXCAP_NO_FILEMODE = 2, + GIT_INDEXCAP_NO_SYMLINKS = 4, + GIT_INDEXCAP_FROM_OWNER = ~0u +}; + /** * Create a new bare Git index object as a memory representation * of the Git index file in 'index_path', without a repository @@ -127,6 +135,27 @@ GIT_EXTERN(void) git_index_clear(git_index *index); GIT_EXTERN(void) git_index_free(git_index *index); /** + * Read index capabilities flags. + * + * @param index An existing index object + * @return A combination of GIT_INDEXCAP values + */ +GIT_EXTERN(unsigned int) git_index_caps(const git_index *index); + +/** + * Set index capabilities flags. + * + * If you pass `GIT_INDEXCAP_FROM_OWNER` for the caps, then the + * capabilities will be read from the config of the owner object, + * looking at `core.ignorecase`, `core.filemode`, `core.symlinks`. + * + * @param index An existing index object + * @param caps A combination of GIT_INDEXCAP values + * @return 0 on success, -1 on failure + */ +GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps); + +/** * Update the contents of an existing index object in memory * by reading from the hard disk. * diff --git a/include/git2/merge.h b/include/git2/merge.h index 5a0b2e7f2..c80803d36 100644 --- a/include/git2/merge.h +++ b/include/git2/merge.h @@ -30,6 +30,16 @@ GIT_BEGIN_DECL */ GIT_EXTERN(int) git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *two); +/** + * Find a merge base given a list of commits + * + * @param out the OID of a merge base considering all the commits + * @param repo the repository where the commits exist + * @param input_array oids of the commits + * @param length The number of commits in the provided `input_array` + */ +GIT_EXTERN(int) git_merge_base_many(git_oid *out, git_repository *repo, const git_oid input_array[], size_t length); + /** @} */ GIT_END_DECL #endif diff --git a/include/git2/message.h b/include/git2/message.h new file mode 100644 index 000000000..7f2558583 --- /dev/null +++ b/include/git2/message.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_message_h__ +#define INCLUDE_git_message_h__ + +#include "common.h" + +/** + * @file git2/message.h + * @brief Git message management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Clean up message from excess whitespace and make sure that the last line + * ends with a '\n'. + * + * Optionally, can remove lines starting with a "#". + * + * @param message_out The user allocated buffer which will be filled with + * the cleaned up message. + * + * @param size The size of the allocated buffer message_out. + * + * @param message The message to be prettified. + * + * @param strip_comments 1 to remove lines starting with a "#", 0 otherwise. + * + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments); + +/** @} */ +GIT_END_DECL +#endif /* INCLUDE_git_message_h__ */ diff --git a/include/git2/tag.h b/include/git2/tag.h index 13dc145b6..b522451a1 100644 --- a/include/git2/tag.h +++ b/include/git2/tag.h @@ -137,8 +137,8 @@ GIT_EXTERN(const char *) git_tag_message(git_tag *tag); * this tag object. If `force` is true and a reference * already exists with the given name, it'll be replaced. * - * The message will be cleaned up from excess whitespace - * it will be made sure that the last line ends with a '\n'. + * The message will not be cleaned up. This can be achieved + * through `git_message_prettify()`. * * @param oid Pointer where to store the OID of the * newly created tag. If the tag already exists, this parameter diff --git a/src/branch.c b/src/branch.c index 8b97a8206..671e42051 100644 --- a/src/branch.c +++ b/src/branch.c @@ -141,46 +141,46 @@ on_error: } typedef struct { - git_vector *branchlist; + int (*branch_cb)( + const char *branch_name, + git_branch_t branch_type, + void *payload); + void *callback_payload; unsigned int branch_type; -} branch_filter_data; +} branch_foreach_filter; -static int branch_list_cb(const char *branch_name, void *payload) +static int branch_foreach_cb(const char *branch_name, void *payload) { - branch_filter_data *filter = (branch_filter_data *)payload; + branch_foreach_filter *filter = (branch_foreach_filter *)payload; - if (filter->branch_type & GIT_BRANCH_LOCAL && git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0) { - return git_vector_insert(filter->branchlist, git__strdup(branch_name +strlen(GIT_REFS_HEADS_DIR))); - } else if (filter->branch_type & GIT_BRANCH_REMOTE && git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0) { - return git_vector_insert(filter->branchlist, git__strdup(branch_name+strlen(GIT_REFS_DIR))); - } + if (filter->branch_type & GIT_BRANCH_LOCAL && + git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0) + return filter->branch_cb(branch_name + strlen(GIT_REFS_HEADS_DIR), GIT_BRANCH_LOCAL, filter->callback_payload); + + if (filter->branch_type & GIT_BRANCH_REMOTE && + git__prefixcmp(branch_name, GIT_REFS_REMOTES_DIR) == 0) + return filter->branch_cb(branch_name + strlen(GIT_REFS_REMOTES_DIR), GIT_BRANCH_REMOTE, filter->callback_payload); return 0; } -int git_branch_list(git_strarray *branch_names, git_repository *repo, unsigned int list_flags) +int git_branch_foreach( + git_repository *repo, + unsigned int list_flags, + int (*branch_cb)( + const char *branch_name, + git_branch_t branch_type, + void *payload), + void *payload +) { - int error; - branch_filter_data filter; - git_vector branchlist; - - assert(branch_names && repo); + branch_foreach_filter filter; - if (git_vector_init(&branchlist, 8, NULL) < 0) - return -1; - - filter.branchlist = &branchlist; + filter.branch_cb = branch_cb; filter.branch_type = list_flags; + filter.callback_payload = payload; - error = git_reference_foreach(repo, GIT_REF_LISTALL, &branch_list_cb, (void *)&filter); - if (error < 0) { - git_vector_free(&branchlist); - return -1; - } - - branch_names->strings = (char **)branchlist.contents; - branch_names->count = branchlist.length; - return 0; + return git_reference_foreach(repo, GIT_REF_LISTALL, &branch_foreach_cb, (void *)&filter); } int git_branch_move(git_repository *repo, const char *old_branch_name, const char *new_branch_name, int force) diff --git a/src/commit.c b/src/commit.c index 57eafaa2e..a3baf9d4e 100644 --- a/src/commit.c +++ b/src/commit.c @@ -93,7 +93,7 @@ int git_commit_create( int parent_count, const git_commit *parents[]) { - git_buf commit = GIT_BUF_INIT, cleaned_message = GIT_BUF_INIT; + git_buf commit = GIT_BUF_INIT; int i; git_odb *odb; @@ -114,15 +114,9 @@ int git_commit_create( git_buf_putc(&commit, '\n'); - /* Remove comments by default */ - if (git_message_prettify(&cleaned_message, message, 1) < 0) + if (git_buf_puts(&commit, message) < 0) goto on_error; - if (git_buf_puts(&commit, git_buf_cstr(&cleaned_message)) < 0) - goto on_error; - - git_buf_free(&cleaned_message); - if (git_repository_odb__weakptr(&odb, repo) < 0) goto on_error; @@ -138,7 +132,6 @@ int git_commit_create( on_error: git_buf_free(&commit); - git_buf_free(&cleaned_message); giterr_set(GITERR_OBJECT, "Failed to create commit."); return -1; } diff --git a/src/common.h b/src/common.h index e2a300291..419a8d06b 100644 --- a/src/common.h +++ b/src/common.h @@ -24,6 +24,7 @@ # include <io.h> # include <direct.h> +# include <winsock2.h> # include <windows.h> # include "win32/msvc-compat.h" # include "win32/mingw-compat.h" diff --git a/src/config_file.c b/src/config_file.c index 1c748fad1..fd1aa8d08 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -87,6 +87,7 @@ typedef struct { static int config_parse(diskfile_backend *cfg_file); static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_value); static int config_write(diskfile_backend *cfg, const char *key, const regex_t *preg, const char *value); +static char *escape_value(const char *ptr); static void set_parse_error(diskfile_backend *backend, int col, const char *error_str) { @@ -212,9 +213,9 @@ static int config_set(git_config_file *cfg, const char *name, const char *value) { cvar_t *var = NULL, *old_var; diskfile_backend *b = (diskfile_backend *)cfg; - char *key; + char *key, *esc_value = NULL; khiter_t pos; - int rval; + int rval, ret; if (normalize_name(name, &key) < 0) return -1; @@ -237,12 +238,17 @@ static int config_set(git_config_file *cfg, const char *name, const char *value) if (value) { tmp = git__strdup(value); GITERR_CHECK_ALLOC(tmp); + esc_value = escape_value(value); + GITERR_CHECK_ALLOC(esc_value); } git__free(existing->value); existing->value = tmp; - return config_write(b, existing->key, NULL, value); + ret = config_write(b, existing->key, NULL, esc_value); + + git__free(esc_value); + return ret; } var = git__malloc(sizeof(cvar_t)); @@ -256,13 +262,17 @@ static int config_set(git_config_file *cfg, const char *name, const char *value) if (value) { var->value = git__strdup(value); GITERR_CHECK_ALLOC(var->value); + esc_value = escape_value(value); + GITERR_CHECK_ALLOC(esc_value); } - if (config_write(b, key, NULL, value) < 0) { + if (config_write(b, key, NULL, esc_value) < 0) { + git__free(esc_value); cvar_free(var); return -1; } + git__free(esc_value); git_strmap_insert2(b->values, key, var, old_var, rval); if (rval < 0) return -1; @@ -1155,13 +1165,44 @@ rewrite_fail: return -1; } +static const char *escapes = "ntb\"\\"; +static const char *escaped = "\n\t\b\"\\"; + +/* Escape the values to write them to the file */ +static char *escape_value(const char *ptr) +{ + git_buf buf = GIT_BUF_INIT; + size_t len; + const char *esc; + + assert(ptr); + + len = strlen(ptr); + git_buf_grow(&buf, len); + + while (*ptr != '\0') { + if ((esc = strchr(escaped, *ptr)) != NULL) { + git_buf_putc(&buf, '\\'); + git_buf_putc(&buf, escapes[esc - escaped]); + } else { + git_buf_putc(&buf, *ptr); + } + ptr++; + } + + if (git_buf_oom(&buf)) { + git_buf_free(&buf); + return NULL; + } + + return git_buf_detach(&buf); +} + /* '\"' -> '"' etc */ static char *fixup_line(const char *ptr, int quote_count) { char *str = git__malloc(strlen(ptr) + 1); char *out = str, *esc; - const char *escapes = "ntb\"\\"; - const char *escaped = "\n\t\b\"\\"; if (str == NULL) return NULL; diff --git a/src/diff.c b/src/diff.c index 02b89b46e..4fea894f8 100644 --- a/src/diff.c +++ b/src/diff.c @@ -456,10 +456,10 @@ static int maybe_modified( if (!diff_path_matches_pathspec(diff, oitem->path)) return 0; - /* on platforms with no symlinks, promote plain files to symlinks */ + /* on platforms with no symlinks, preserve mode of existing symlinks */ if (S_ISLNK(omode) && S_ISREG(nmode) && !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS)) - nmode = GIT_MODE_TYPE(omode) | (nmode & GIT_MODE_PERMS_MASK); + nmode = omode; /* on platforms with no execmode, just preserve old mode */ if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) && @@ -704,12 +704,17 @@ int git_diff_index_to_tree( assert(repo && diff); if (git_iterator_for_tree_range(&a, repo, old_tree, prefix, prefix) < 0 || - git_iterator_for_index_range(&b, repo, prefix, prefix) < 0) - return -1; + git_iterator_for_index_range(&b, repo, prefix, prefix) < 0) + goto on_error; git__free(prefix); return diff_from_iterators(repo, opts, a, b, diff); + +on_error: + git__free(prefix); + git_iterator_free(a); + return -1; } int git_diff_workdir_to_index( @@ -723,12 +728,17 @@ int git_diff_workdir_to_index( assert(repo && diff); if (git_iterator_for_index_range(&a, repo, prefix, prefix) < 0 || - git_iterator_for_workdir_range(&b, repo, prefix, prefix) < 0) - return -1; + git_iterator_for_workdir_range(&b, repo, prefix, prefix) < 0) + goto on_error; git__free(prefix); return diff_from_iterators(repo, opts, a, b, diff); + +on_error: + git__free(prefix); + git_iterator_free(a); + return -1; } @@ -744,12 +754,17 @@ int git_diff_workdir_to_tree( assert(repo && old_tree && diff); if (git_iterator_for_tree_range(&a, repo, old_tree, prefix, prefix) < 0 || - git_iterator_for_workdir_range(&b, repo, prefix, prefix) < 0) - return -1; + git_iterator_for_workdir_range(&b, repo, prefix, prefix) < 0) + goto on_error; git__free(prefix); return diff_from_iterators(repo, opts, a, b, diff); + +on_error: + git__free(prefix); + git_iterator_free(a); + return -1; } int git_diff_merge( diff --git a/src/index.c b/src/index.c index f1ae9a710..3fedcd27a 100644 --- a/src/index.c +++ b/src/index.c @@ -15,6 +15,7 @@ #include "hash.h" #include "git2/odb.h" #include "git2/blob.h" +#include "git2/config.h" #define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7) #define short_entry_size(len) entry_size(struct entry_short, len) @@ -124,11 +125,27 @@ static unsigned int index_create_mode(unsigned int mode) { if (S_ISLNK(mode)) return S_IFLNK; + if (S_ISDIR(mode) || (mode & S_IFMT) == (S_IFLNK | S_IFDIR)) return (S_IFLNK | S_IFDIR); + return S_IFREG | ((mode & 0100) ? 0755 : 0644); } +static unsigned int index_merge_mode( + git_index *index, git_index_entry *existing, unsigned int mode) +{ + if (index->no_symlinks && S_ISREG(mode) && + existing && S_ISLNK(existing->mode)) + return existing->mode; + + if (index->distrust_filemode && S_ISREG(mode)) + return (existing && S_ISREG(existing->mode)) ? + existing->mode : index_create_mode(0666); + + return index_create_mode(mode); +} + int git_index_open(git_index **index_out, const char *index_path) { git_index *index; @@ -208,6 +225,45 @@ void git_index_clear(git_index *index) index->tree = NULL; } +int git_index_set_caps(git_index *index, unsigned int caps) +{ + assert(index); + + if (caps == GIT_INDEXCAP_FROM_OWNER) { + git_config *cfg; + int val; + + if (INDEX_OWNER(index) == NULL || + git_repository_config__weakptr(&cfg, INDEX_OWNER(index)) < 0) + { + giterr_set(GITERR_INDEX, + "Cannot get repository config to set index caps"); + return -1; + } + + if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0) + index->ignore_case = (val != 0); + if (git_config_get_bool(&val, cfg, "core.filemode") == 0) + index->distrust_filemode = (val == 0); + if (git_config_get_bool(&val, cfg, "core.symlinks") == 0) + index->no_symlinks = (val != 0); + } + else { + index->ignore_case = ((caps & GIT_INDEXCAP_IGNORE_CASE) != 0); + index->distrust_filemode = ((caps & GIT_INDEXCAP_NO_FILEMODE) != 0); + index->no_symlinks = ((caps & GIT_INDEXCAP_NO_SYMLINKS) != 0); + } + + return 0; +} + +unsigned int git_index_caps(const git_index *index) +{ + return ((index->ignore_case ? GIT_INDEXCAP_IGNORE_CASE : 0) | + (index->distrust_filemode ? GIT_INDEXCAP_NO_FILEMODE : 0) | + (index->no_symlinks ? GIT_INDEXCAP_NO_SYMLINKS : 0)); +} + int git_index_read(git_index *index) { int error, updated; @@ -383,7 +439,7 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace) { size_t path_length; int position; - git_index_entry **entry_array; + git_index_entry **existing = NULL; assert(index && entry && entry->path != NULL); @@ -397,28 +453,24 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace) else entry->flags |= GIT_IDXENTRY_NAMEMASK;; - /* - * replacing is not requested: just insert entry at the end; - * the index is no longer sorted - */ - if (!replace) - return git_vector_insert(&index->entries, entry); - /* look if an entry with this path already exists */ - position = git_index_find(index, entry->path); + if ((position = git_index_find(index, entry->path)) >= 0) { + existing = (git_index_entry **)&index->entries.contents[position]; - /* - * if no entry exists add the entry at the end; - * the index is no longer sorted + /* update filemode to existing values if stat is not trusted */ + entry->mode = index_merge_mode(index, *existing, entry->mode); + } + + /* if replacing is not requested or no existing entry exists, just + * insert entry at the end; the index is no longer sorted */ - if (position == GIT_ENOTFOUND) + if (!replace || !existing) return git_vector_insert(&index->entries, entry); /* exists, replace it */ - entry_array = (git_index_entry **) index->entries.contents; - git__free(entry_array[position]->path); - git__free(entry_array[position]); - entry_array[position] = entry; + git__free((*existing)->path); + git__free(*existing); + *existing = entry; return 0; } @@ -475,7 +527,7 @@ int git_index_add2(git_index *index, const git_index_entry *source_entry) int git_index_append2(git_index *index, const git_index_entry *source_entry) { - return index_add2(index, source_entry, 1); + return index_add2(index, source_entry, 0); } int git_index_remove(git_index *index, int position) diff --git a/src/index.h b/src/index.h index 8515f4fcb..a57da5386 100644 --- a/src/index.h +++ b/src/index.h @@ -26,6 +26,11 @@ struct git_index { git_vector entries; unsigned int on_disk:1; + + unsigned int ignore_case:1; + unsigned int distrust_filemode:1; + unsigned int no_symlinks:1; + git_tree_cache *tree; git_vector unmerged; diff --git a/src/message.c b/src/message.c index aa0220fd0..a4aadb28f 100644 --- a/src/message.c +++ b/src/message.c @@ -6,7 +6,6 @@ */ #include "message.h" -#include <ctype.h> static size_t line_length_without_trailing_spaces(const char *line, size_t len) { @@ -22,7 +21,7 @@ static size_t line_length_without_trailing_spaces(const char *line, size_t len) /* Greatly inspired from git.git "stripspace" */ /* see https://github.com/git/git/blob/497215d8811ac7b8955693ceaad0899ecd894ed2/builtin/stripspace.c#L4-67 */ -int git_message_prettify(git_buf *message_out, const char *message, int strip_comments) +int git_message__prettify(git_buf *message_out, const char *message, int strip_comments) { const size_t message_len = strlen(message); @@ -59,3 +58,25 @@ int git_message_prettify(git_buf *message_out, const char *message, int strip_co return git_buf_oom(message_out) ? -1 : 0; } + +int git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments) +{ + git_buf buf = GIT_BUF_INIT; + + if (strlen(message) + 1 > buffer_size) { /* We have to account for a potentially missing \n */ + giterr_set(GITERR_INVALID, "Buffer too short to hold the cleaned message"); + return -1; + } + + *message_out = '\0'; + + if (git_message__prettify(&buf, message, strip_comments) < 0) { + git_buf_free(&buf); + return -1; + } + + git_buf_copy_cstr(message_out, buffer_size, &buf); + git_buf_free(&buf); + + return 0; +} diff --git a/src/message.h b/src/message.h index ddfa13e18..7e4e7f337 100644 --- a/src/message.h +++ b/src/message.h @@ -7,8 +7,9 @@ #ifndef INCLUDE_message_h__ #define INCLUDE_message_h__ +#include "git2/message.h" #include "buffer.h" -int git_message_prettify(git_buf *message_out, const char *message, int strip_comments); +int git_message__prettify(git_buf *message_out, const char *message, int strip_comments); #endif /* INCLUDE_message_h__ */ diff --git a/src/netops.c b/src/netops.c index 2161dfab9..0342d7fa1 100644 --- a/src/netops.c +++ b/src/netops.c @@ -12,7 +12,6 @@ # include <netdb.h> # include <arpa/inet.h> #else -# include <winsock2.h> # include <ws2tcpip.h> # ifdef _MSC_VER # pragma comment(lib, "ws2_32.lib") diff --git a/src/notes.c b/src/notes.c index e87ea65fb..0dfd3f891 100644 --- a/src/notes.c +++ b/src/notes.c @@ -12,50 +12,61 @@ #include "config.h" #include "iterator.h" -static int find_subtree(git_tree **subtree, const git_oid *root, - git_repository *repo, const char *target, int *fanout) +static int find_subtree_in_current_level( + git_tree **out, + git_repository *repo, + git_tree *parent, + const char *annotated_object_sha, + int fanout) { - int error; unsigned int i; - git_tree *tree; const git_tree_entry *entry; - *subtree = NULL; - - error = git_tree_lookup(&tree, repo, root); - if (error < 0) - return error; + *out = NULL; + + if (parent == NULL) + return GIT_ENOTFOUND; - for (i=0; i<git_tree_entrycount(tree); i++) { - entry = git_tree_entry_byindex(tree, i); + for (i = 0; i < git_tree_entrycount(parent); i++) { + entry = git_tree_entry_byindex(parent, i); if (!git__ishex(git_tree_entry_name(entry))) continue; - /* - * A notes tree follows a strict byte-based progressive fanout - * (i.e. using 2/38, 2/2/36, etc. fanouts, not e.g. 4/36 fanout) - */ - if (S_ISDIR(git_tree_entry_attributes(entry)) - && strlen(git_tree_entry_name(entry)) == 2 - && !strncmp(git_tree_entry_name(entry), target + *fanout, 2)) { + && strlen(git_tree_entry_name(entry)) == 2 + && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) + return git_tree_lookup(out, repo, git_tree_entry_id(entry)); - /* found matching subtree - unpack and resume lookup */ + /* Not a DIR, so do we have an already existing blob? */ + if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) + return GIT_EEXISTS; + } - git_oid subtree_sha; - git_oid_cpy(&subtree_sha, git_tree_entry_id(entry)); - git_tree_free(tree); + return GIT_ENOTFOUND; +} - *fanout += 2; +static int find_subtree_r(git_tree **out, git_tree *root, + git_repository *repo, const char *target, int *fanout) +{ + int error; + git_tree *subtree = NULL; - return find_subtree(subtree, &subtree_sha, repo, - target, fanout); - } + *out = NULL; + + error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); + if (error == GIT_EEXISTS) { + return git_tree_lookup(out, repo, git_tree_id(root)); } - *subtree = tree; - return 0; + if (error < 0) + return error; + + *fanout += 2; + error = find_subtree_r(out, subtree, repo, target, fanout); + git_tree_free(subtree); + + return error; } static int find_blob(git_oid *blob, git_tree *tree, const char *target) @@ -76,191 +87,277 @@ static int find_blob(git_oid *blob, git_tree *tree, const char *target) return GIT_ENOTFOUND; } -static int note_write(git_oid *out, git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const char *note, - const git_oid *tree_sha, const char *target, - int nparents, git_commit **parents) +static int tree_write( + git_tree **out, + git_repository *repo, + git_tree *source_tree, + const git_oid *object_oid, + const char *treeentry_name, + unsigned int attributes) { - int error, fanout = 0; - git_oid oid; - git_tree *tree = NULL; + int error; + git_treebuilder *tb = NULL; git_tree_entry *entry; - git_treebuilder *tb; - - /* check for existing notes tree */ - - if (tree_sha) { - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; - - error = find_blob(&oid, tree, target + fanout); - if (error != GIT_ENOTFOUND) { - git_tree_free(tree); - if (!error) { - giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", target); - error = GIT_EEXISTS; - } - return error; - } - } + git_oid tree_oid; - /* no matching tree entry - add note object to target tree */ + if ((error = git_treebuilder_create(&tb, source_tree)) < 0) + goto cleanup; - error = git_treebuilder_create(&tb, tree); - git_tree_free(tree); + if (object_oid) { + if ((error = git_treebuilder_insert( + &entry, tb, treeentry_name, object_oid, attributes)) < 0) + goto cleanup; + } else { + if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0) + goto cleanup; + } - if (error < 0) - return error; + if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0) + goto cleanup; - if (!tree_sha) - /* no notes tree yet - create fanout */ - fanout += 2; + error = git_tree_lookup(out, repo, &tree_oid); - /* create note object */ - error = git_blob_create_frombuffer(&oid, repo, note, strlen(note)); - if (error < 0) { - git_treebuilder_free(tb); - return error; - } +cleanup: + git_treebuilder_free(tb); + return error; +} - error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644); //-V536 - if (error < 0) { - /* libgit2 doesn't support object removal (gc) yet */ - /* we leave an orphaned blob object behind - TODO */ +static int manipulate_note_in_tree_r( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int (*note_exists_cb)( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error), + int (*note_notfound_cb)( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error)) +{ + int error = -1; + git_tree *subtree = NULL, *new = NULL; + char subtree_name[3]; - git_treebuilder_free(tb); - return error; - } + error = find_subtree_in_current_level( + &subtree, repo, parent, annotated_object_sha, fanout); - if (out) - git_oid_cpy(out, git_tree_entry_id(entry)); + if (error == GIT_EEXISTS) { + error = note_exists_cb( + out, repo, parent, note_oid, annotated_object_sha, fanout, error); + goto cleanup; + } - error = git_treebuilder_write(&oid, repo, tb); - git_treebuilder_free(tb); + if (error == GIT_ENOTFOUND) { + error = note_notfound_cb( + out, repo, parent, note_oid, annotated_object_sha, fanout, error); + goto cleanup; + } if (error < 0) - return 0; + goto cleanup; - if (!tree_sha) { - /* create fanout subtree */ + /* An existing fanout has been found, let's dig deeper */ + error = manipulate_note_in_tree_r( + &new, repo, subtree, note_oid, annotated_object_sha, + fanout + 2, note_exists_cb, note_notfound_cb); - char subtree[3]; - strncpy(subtree, target, 2); - subtree[2] = '\0'; + if (error < 0) + goto cleanup; - error = git_treebuilder_create(&tb, NULL); - if (error < 0) - return error; + strncpy(subtree_name, annotated_object_sha + fanout, 2); + subtree_name[2] = '\0'; - error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000); //-V536 - if (error < 0) { - git_treebuilder_free(tb); - return error; - } + error = tree_write(out, repo, parent, git_tree_id(new), + subtree_name, 0040000); - error = git_treebuilder_write(&oid, repo, tb); - git_treebuilder_free(tb); +cleanup: + git_tree_free(new); + git_tree_free(subtree); + return error; +} - if (error < 0) - return error; - } +static int remove_note_in_tree_eexists_cb( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(note_oid); + GIT_UNUSED(current_error); - /* create new notes commit */ + return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0); +} - error = git_tree_lookup(&tree, repo, &oid); - if (error < 0) - return error; +static int remove_note_in_tree_enotfound_cb( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(out); + GIT_UNUSED(repo); + GIT_UNUSED(parent); + GIT_UNUSED(note_oid); + GIT_UNUSED(fanout); + + giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha); + return current_error; +} - error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_ADD, - tree, nparents, (const git_commit **) parents); +static int insert_note_in_tree_eexists_cb(git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(out); + GIT_UNUSED(repo); + GIT_UNUSED(parent); + GIT_UNUSED(note_oid); + GIT_UNUSED(fanout); + + giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha); + return current_error; +} - git_tree_free(tree); +static int insert_note_in_tree_enotfound_cb(git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(current_error); - return error; + /* No existing fanout at this level, insert in place */ + return tree_write(out, repo, parent, note_oid, annotated_object_sha + fanout, 0100644); } -static int note_lookup(git_note **out, git_repository *repo, - const git_oid *tree_sha, const char *target) +static int note_write(git_oid *out, + git_repository *repo, + git_signature *author, + git_signature *committer, + const char *notes_ref, + const char *note, + git_tree *commit_tree, + const char *target, + git_commit **parents) { - int error, fanout = 0; + int error; git_oid oid; - git_blob *blob; - git_tree *tree; - git_note *note; + git_tree *tree = NULL; + + // TODO: should we apply filters? + /* create note object */ + if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0) + goto cleanup; - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; + if ((error = manipulate_note_in_tree_r( + &tree, repo, commit_tree, &oid, target, 0, + insert_note_in_tree_eexists_cb, insert_note_in_tree_enotfound_cb)) < 0) + goto cleanup; - error = find_blob(&oid, tree, target + fanout); + if (out) + git_oid_cpy(out, &oid); + error = git_commit_create(&oid, repo, notes_ref, author, committer, + NULL, GIT_NOTES_DEFAULT_MSG_ADD, + tree, *parents == NULL ? 0 : 1, (const git_commit **) parents); + +cleanup: git_tree_free(tree); - if (error < 0) - return error; + return error; +} - error = git_blob_lookup(&blob, repo, &oid); - if (error < 0) - return error; +static int note_new(git_note **out, git_oid *note_oid, git_blob *blob) +{ + git_note *note = NULL; - note = git__malloc(sizeof(git_note)); + note = (git_note *)git__malloc(sizeof(git_note)); GITERR_CHECK_ALLOC(note); - git_oid_cpy(¬e->oid, &oid); - note->message = git__strdup(git_blob_rawcontent(blob)); + git_oid_cpy(¬e->oid, note_oid); + note->message = git__strdup((char *)git_blob_rawcontent(blob)); GITERR_CHECK_ALLOC(note->message); *out = note; - git_blob_free(blob); - return error; + return 0; } -static int note_remove(git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const git_oid *tree_sha, - const char *target, int nparents, git_commit **parents) +static int note_lookup(git_note **out, git_repository *repo, + git_tree *tree, const char *target) { int error, fanout = 0; git_oid oid; - git_tree *tree; - git_treebuilder *tb; + git_blob *blob = NULL; + git_note *note = NULL; + git_tree *subtree = NULL; - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; + if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) + goto cleanup; - error = find_blob(&oid, tree, target + fanout); - if (!error) - error = git_treebuilder_create(&tb, tree); + if ((error = find_blob(&oid, subtree, target + fanout)) < 0) + goto cleanup; - git_tree_free(tree); - if (error < 0) - return error; + if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) + goto cleanup; - error = git_treebuilder_remove(tb, target + fanout); - if (!error) - error = git_treebuilder_write(&oid, repo, tb); + if ((error = note_new(¬e, &oid, blob)) < 0) + goto cleanup; - git_treebuilder_free(tb); - if (error < 0) - return error; + *out = note; - /* create new notes commit */ +cleanup: + git_tree_free(subtree); + git_blob_free(blob); + return error; +} - error = git_tree_lookup(&tree, repo, &oid); - if (error < 0) - return error; +static int note_remove(git_repository *repo, + git_signature *author, git_signature *committer, + const char *notes_ref, git_tree *tree, + const char *target, git_commit **parents) +{ + int error; + git_tree *tree_after_removal = NULL; + git_oid oid; + + if ((error = manipulate_note_in_tree_r( + &tree_after_removal, repo, tree, NULL, target, 0, + remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0) + goto cleanup; error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_RM, - tree, nparents, (const git_commit **) parents); - - git_tree_free(tree); + NULL, GIT_NOTES_DEFAULT_MSG_RM, + tree_after_removal, + *parents == NULL ? 0 : 1, + (const git_commit **) parents); +cleanup: + git_tree_free(tree_after_removal); return error; } @@ -291,48 +388,50 @@ static int normalize_namespace(const char **notes_ref, git_repository *repo) return note_get_default_ref(notes_ref, repo); } -static int retrieve_note_tree_oid(git_oid *tree_oid_out, git_repository *repo, const char *notes_ref) +static int retrieve_note_tree_and_commit( + git_tree **tree_out, + git_commit **commit_out, + git_repository *repo, + const char **notes_ref) { - int error = -1; - git_commit *commit = NULL; + int error; git_oid oid; - if ((error = git_reference_name_to_oid(&oid, repo, notes_ref)) < 0) - goto cleanup; + if ((error = normalize_namespace(notes_ref, repo)) < 0) + return error; - if (git_commit_lookup(&commit, repo, &oid) < 0) - goto cleanup; + if ((error = git_reference_name_to_oid(&oid, repo, *notes_ref)) < 0) + return error; - git_oid_cpy(tree_oid_out, git_commit_tree_oid(commit)); + if (git_commit_lookup(commit_out, repo, &oid) < 0) + return error; - error = 0; + if ((error = git_commit_tree(tree_out, *commit_out)) < 0) + return error; -cleanup: - git_commit_free(commit); - return error; + return 0; } int git_note_read(git_note **out, git_repository *repo, const char *notes_ref, const git_oid *oid) { int error; - char *target; - git_oid sha; - - *out = NULL; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - if ((error = retrieve_note_tree_oid(&sha, repo, notes_ref)) < 0) - return error; + char *target = NULL; + git_tree *tree = NULL; + git_commit *commit = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); - error = note_lookup(out, repo, &sha, target); + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) + goto cleanup; + + error = note_lookup(out, repo, tree, target); +cleanup: git__free(target); + git_tree_free(tree); + git_commit_free(commit); return error; } @@ -342,44 +441,26 @@ int git_note_create( const char *notes_ref, const git_oid *oid, const char *note) { - int error, nparents = 0; - char *target; - git_oid sha; + int error; + char *target = NULL; git_commit *commit = NULL; - git_reference *ref; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - error = git_reference_lookup(&ref, repo, notes_ref); - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - if (!error) { - assert(git_reference_type(ref) == GIT_REF_OID); - - /* lookup existing notes tree oid */ - - git_oid_cpy(&sha, git_reference_oid(ref)); - git_reference_free(ref); - - error = git_commit_lookup(&commit, repo, &sha); - if (error < 0) - return error; - - git_oid_cpy(&sha, git_commit_tree_oid(commit)); - nparents++; - } + git_tree *tree = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); + error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); + + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + error = note_write(out, repo, author, committer, notes_ref, - note, nparents ? &sha : NULL, target, - nparents, &commit); + note, tree, target, &commit); +cleanup: git__free(target); git_commit_free(commit); + git_tree_free(tree); return error; } @@ -388,37 +469,23 @@ int git_note_remove(git_repository *repo, const char *notes_ref, const git_oid *oid) { int error; - char *target; - git_oid sha; - git_commit *commit; - git_reference *ref; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - error = git_reference_lookup(&ref, repo, notes_ref); - if (error < 0) - return error; - - assert(git_reference_type(ref) == GIT_REF_OID); - - git_oid_cpy(&sha, git_reference_oid(ref)); - git_reference_free(ref); - - error = git_commit_lookup(&commit, repo, &sha); - if (error < 0) - return error; - - git_oid_cpy(&sha, git_commit_tree_oid(commit)); + char *target = NULL; + git_commit *commit = NULL; + git_tree *tree = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) + goto cleanup; + error = note_remove(repo, author, committer, notes_ref, - &sha, target, 1, &commit); + tree, target, &commit); +cleanup: git__free(target); git_commit_free(commit); + git_tree_free(tree); return error; } @@ -511,18 +578,12 @@ int git_note_foreach( void *payload) { int error = -1; - git_oid tree_oid; git_iterator *iter = NULL; git_tree *tree = NULL; + git_commit *commit = NULL; const git_index_entry *item; - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - if ((error = retrieve_note_tree_oid(&tree_oid, repo, notes_ref)) < 0) - goto cleanup; - - if (git_tree_lookup(&tree, repo, &tree_oid) < 0) + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) goto cleanup; if (git_iterator_for_tree(&iter, repo, tree) < 0) @@ -544,5 +605,6 @@ int git_note_foreach( cleanup: git_iterator_free(iter); git_tree_free(tree); + git_commit_free(commit); return error; } diff --git a/src/object.c b/src/object.c index d3673eda0..14d64befe 100644 --- a/src/object.c +++ b/src/object.c @@ -156,8 +156,10 @@ int git_object_lookup_prefix( type = odb_obj->raw.type; - if (create_object(&object, type) < 0) + if (create_object(&object, type) < 0) { + git_odb_object_free(odb_obj); return -1; + } /* Initialize parent object */ git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid); @@ -559,6 +559,7 @@ int git_odb_read_prefix( int error = GIT_ENOTFOUND; git_oid found_full_oid = {{0}}; git_rawobj raw; + void *data = NULL; bool found = false; assert(out && db); @@ -588,6 +589,8 @@ int git_odb_read_prefix( if (error) return error; + git__free(data); + data = raw.data; if (found && git_oid_cmp(&full_oid, &found_full_oid)) return git_odb__error_ambiguous("multiple matches for prefix"); found_full_oid = full_oid; diff --git a/src/path.c b/src/path.c index 1d85559a9..a6574b3de 100644 --- a/src/path.c +++ b/src/path.c @@ -468,19 +468,24 @@ int git_path_cmp( const char *name1, size_t len1, int isdir1, const char *name2, size_t len2, int isdir2) { + unsigned char c1, c2; size_t len = len1 < len2 ? len1 : len2; int cmp; cmp = memcmp(name1, name2, len); if (cmp) return cmp; - if (len1 < len2) - return (!isdir1 && !isdir2) ? -1 : - (isdir1 ? '/' - name2[len1] : name2[len1] - '/'); - if (len1 > len2) - return (!isdir1 && !isdir2) ? 1 : - (isdir2 ? name1[len2] - '/' : '/' - name1[len2]); - return 0; + + c1 = name1[len]; + c2 = name2[len]; + + if (c1 == '\0' && isdir1) + c1 = '/'; + + if (c2 == '\0' && isdir2) + c2 = '/'; + + return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; } /* Taken from git.git */ diff --git a/src/repository.c b/src/repository.c index 4e467e689..23a95b23e 100644 --- a/src/repository.c +++ b/src/repository.c @@ -573,6 +573,9 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo) return -1; GIT_REFCOUNT_OWN(repo->_index, repo); + + if (git_index_set_caps(repo->_index, GIT_INDEXCAP_FROM_OWNER) < 0) + return -1; } *out = repo->_index; @@ -851,6 +854,7 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is goto cleanup; result = repo_init_config(repository_path.ptr, is_bare, is_reinit); + goto cleanup; } if (repo_init_structure(repository_path.ptr, is_bare) < 0 || diff --git a/src/revparse.c b/src/revparse.c index c66a9852e..3f210d11b 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -323,10 +323,10 @@ static git_object* dereference_object(git_object *obj) break; case GIT_OBJ_TAG: { - git_object *newobj = NULL; - if (0 == git_tag_target(&newobj, (git_tag*)obj)) { - return newobj; - } + git_object *newobj = NULL; + if (0 == git_tag_target(&newobj, (git_tag*)obj)) { + return newobj; + } } break; @@ -406,7 +406,7 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec *out = newobj2; return 0; } - + /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ if (movement[1] == '/') { int retcode = GIT_ERROR; @@ -506,8 +506,8 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m /* "~" is the same as "~1" */ if (*movement == '\0') { n = 1; - } else { - git__strtol32(&n, movement, NULL, 0); + } else if (git__strtol32(&n, movement, NULL, 0) < 0) { + return GIT_ERROR; } commit1 = (git_commit*)obj; @@ -537,7 +537,7 @@ static int oid_for_tree_path(git_oid *out, git_tree *tree, git_repository *repo, char *tok; void *alloc = str; git_tree *tree2 = tree; - const git_tree_entry *entry; + const git_tree_entry *entry = NULL; while ((tok = git__strtok(&str, "/\\")) != NULL) { entry = git_tree_entry_byname(tree2, tok); @@ -550,6 +550,12 @@ static int oid_for_tree_path(git_oid *out, git_tree *tree, git_repository *repo, } } + if (!entry) { + giterr_set(GITERR_INVALID, "Invalid tree path '%s'", path); + git__free(alloc); + return GIT_ERROR; + } + git_oid_cpy(out, git_tree_entry_id(entry)); git__free(alloc); return 0; @@ -562,6 +568,7 @@ static int handle_colon_syntax(git_object **out, { git_tree *tree; git_oid oid; + int error; /* Dereference until we reach a tree. */ if (dereference_to_type(&obj, obj, GIT_OBJ_TREE) < 0) { @@ -570,8 +577,12 @@ static int handle_colon_syntax(git_object **out, tree = (git_tree*)obj; /* Find the blob at the given path. */ - oid_for_tree_path(&oid, tree, repo, path); + error = oid_for_tree_path(&oid, tree, repo, path); git_tree_free(tree); + + if (error < 0) + return error; + return git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); } @@ -612,6 +623,7 @@ static int revparse_global_grep(git_object **out, git_repository *repo, const ch } if (!resultobj) { giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", pattern); + git_object_free(walkobj); } else { *out = resultobj; } diff --git a/src/revwalk.c b/src/revwalk.c index 5aa98e3a7..13d54b725 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -104,6 +104,9 @@ static void commit_list_free(commit_list **list_p) { commit_list *list = *list_p; + if (list == NULL) + return; + while (list) { commit_list *temp = list; list = temp->next; @@ -351,6 +354,59 @@ static int merge_bases_many(commit_list **out, git_revwalk *walk, commit_object return 0; } +int git_merge_base_many(git_oid *out, git_repository *repo, const git_oid input_array[], size_t length) +{ + git_revwalk *walk; + git_vector list; + commit_list *result = NULL; + int error = -1; + unsigned int i; + commit_object *commit; + + assert(out && repo && input_array); + + if (length < 2) { + giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length); + return -1; + } + + if (git_vector_init(&list, length - 1, NULL) < 0) + return -1; + + if (git_revwalk_new(&walk, repo) < 0) + goto cleanup; + + for (i = 1; i < length; i++) { + commit = commit_lookup(walk, &input_array[i]); + if (commit == NULL) + goto cleanup; + + git_vector_insert(&list, commit); + } + + commit = commit_lookup(walk, &input_array[0]); + if (commit == NULL) + goto cleanup; + + if (merge_bases_many(&result, walk, commit, &list) < 0) + goto cleanup; + + if (!result) { + error = GIT_ENOTFOUND; + goto cleanup; + } + + git_oid_cpy(out, &result->item->oid); + + error = 0; + +cleanup: + commit_list_free(&result); + git_revwalk_free(walk); + git_vector_free(&list); + return error; +} + int git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *two) { git_revwalk *walk; @@ -196,7 +196,7 @@ static int write_tag_annotation( const git_signature *tagger, const char *message) { - git_buf tag = GIT_BUF_INIT, cleaned_message = GIT_BUF_INIT; + git_buf tag = GIT_BUF_INIT; git_odb *odb; git_oid__writebuf(&tag, "object ", git_object_id(target)); @@ -205,15 +205,9 @@ static int write_tag_annotation( git_signature__writebuf(&tag, "tagger ", tagger); git_buf_putc(&tag, '\n'); - /* Remove comments by default */ - if (git_message_prettify(&cleaned_message, message, 1) < 0) + if (git_buf_puts(&tag, message) < 0) goto on_error; - if (git_buf_puts(&tag, git_buf_cstr(&cleaned_message)) < 0) - goto on_error; - - git_buf_free(&cleaned_message); - if (git_repository_odb__weakptr(&odb, repo) < 0) goto on_error; @@ -225,7 +219,6 @@ static int write_tag_annotation( on_error: git_buf_free(&tag); - git_buf_free(&cleaned_message); giterr_set(GITERR_OBJECT, "Failed to create tag annotation."); return -1; } diff --git a/src/tree.c b/src/tree.c index d575dc8ff..9bdc2180c 100644 --- a/src/tree.c +++ b/src/tree.c @@ -634,6 +634,9 @@ void git_treebuilder_clear(git_treebuilder *bld) void git_treebuilder_free(git_treebuilder *bld) { + if (bld == NULL) + return; + git_treebuilder_clear(bld); git_vector_free(&bld->entries); git__free(bld); diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 37956af85..4e0150fb5 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -232,7 +232,7 @@ int p_open(const char *path, int flags, ...) va_list arg_list; va_start(arg_list, flags); - mode = va_arg(arg_list, mode_t); + mode = (mode_t)va_arg(arg_list, int); va_end(arg_list); } diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c index 1275d1620..c91479438 100644 --- a/tests-clar/clar_helpers.c +++ b/tests-clar/clar_helpers.c @@ -28,9 +28,10 @@ void cl_git_mkfile(const char *filename, const char *content) cl_must_pass(p_close(fd)); } -void cl_git_write2file(const char *filename, const char *new_content, int flags) +void cl_git_write2file( + const char *filename, const char *new_content, int flags, unsigned int mode) { - int fd = p_open(filename, flags, 0644); + int fd = p_open(filename, flags, mode); cl_assert(fd >= 0); if (!new_content) new_content = "\n"; @@ -40,12 +41,12 @@ void cl_git_write2file(const char *filename, const char *new_content, int flags) void cl_git_append2file(const char *filename, const char *new_content) { - cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_APPEND); + cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_APPEND, 0644); } void cl_git_rewritefile(const char *filename, const char *new_content) { - cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC); + cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC, 0644); } #ifdef GIT_WIN32 diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h index a3b03bbb3..eab6c3d3e 100644 --- a/tests-clar/clar_libgit2.h +++ b/tests-clar/clar_libgit2.h @@ -38,7 +38,7 @@ void cl_git_mkfile(const char *filename, const char *content); void cl_git_append2file(const char *filename, const char *new_content); void cl_git_rewritefile(const char *filename, const char *new_content); -void cl_git_write2file(const char *filename, const char *new_content, int mode); +void cl_git_write2file(const char *filename, const char *new_content, int flags, unsigned int mode); bool cl_toggle_filemode(const char *filename); bool cl_is_chmod_supported(void); diff --git a/tests-clar/config/write.c b/tests-clar/config/write.c index f8774473e..13b669cb2 100644 --- a/tests-clar/config/write.c +++ b/tests-clar/config/write.c @@ -90,3 +90,49 @@ void test_config_write__delete_inexistent(void) cl_assert(git_config_delete(cfg, "core.imaginary") == GIT_ENOTFOUND); git_config_free(cfg); } + +void test_config_write__value_containing_quotes(void) +{ + git_config *cfg; + const char* str; + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes")); + cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); + cl_assert_equal_s(str, "this \"has\" quotes"); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); + cl_assert_equal_s(str, "this \"has\" quotes"); + git_config_free(cfg); + + /* The code path for values that already exist is different, check that one as well */ + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_string(cfg, "core.somevar", "this also \"has\" quotes")); + cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); + cl_assert_equal_s(str, "this also \"has\" quotes"); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); + cl_assert_equal_s(str, "this also \"has\" quotes"); + git_config_free(cfg); +} + +void test_config_write__escape_value(void) +{ + git_config *cfg; + const char* str; + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t")); + cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); + cl_assert_equal_s(str, "this \"has\" quotes and \t"); + git_config_free(cfg); + + cl_git_pass(git_config_open_ondisk(&cfg, "config9")); + cl_git_pass(git_config_get_string(&str, cfg, "core.somevar")); + cl_assert_equal_s(str, "this \"has\" quotes and \t"); + git_config_free(cfg); +} diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c index 9361341eb..a2a65be72 100644 --- a/tests-clar/core/env.c +++ b/tests-clar/core/env.c @@ -112,4 +112,6 @@ void test_core_env__1(void) cl_assert(git_futils_find_system_file(&path, "nonexistentfile") == -1); #endif + + git_buf_free(&path); } diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 0c17eeb4a..801439e30 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -409,6 +409,8 @@ void test_diff_workdir__head_index_and_workdir_all_differ(void) git_diff_list_free(diff_i2t); git_diff_list_free(diff_w2i); + + git_tree_free(tree); } void test_diff_workdir__eof_newline_changes(void) diff --git a/tests-clar/index/filemodes.c b/tests-clar/index/filemodes.c new file mode 100644 index 000000000..8bd35ddab --- /dev/null +++ b/tests-clar/index/filemodes.c @@ -0,0 +1,212 @@ +#include "clar_libgit2.h" +#include "buffer.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, git_index_entrycount(index)); + + for (i = 0; i < 6; ++i) { + git_index_entry *entry = git_index_get(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_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT; + + cl_git_pass(git_buf_joinpath(&path, "filemodes", filename)); + cl_git_pass(git_buf_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, O_WRONLY|O_CREAT|O_TRUNC, create_mode); + + git_buf_free(&path); + git_buf_free(&content); +} + +static void add_and_check_mode( + git_index *index, const char *filename, unsigned int expect_mode) +{ + int pos; + git_index_entry *entry; + + cl_git_pass(git_index_add(index, filename, 0)); + + pos = git_index_find(index, filename); + cl_assert(pos >= 0); + + entry = git_index_get(index, pos); + cl_assert(entry->mode == expect_mode); +} + +static void append_and_check_mode( + git_index *index, const char *filename, unsigned int expect_mode) +{ + unsigned int before, after; + git_index_entry *entry; + + before = git_index_entrycount(index); + + cl_git_pass(git_index_append(index, filename, 0)); + + after = git_index_entrycount(index); + cl_assert_equal_i(before + 1, after); + + /* bypass git_index_get since that resorts the index */ + entry = (git_index_entry *)git_vector_get(&index->entries, after - 1); + + cl_assert_equal_s(entry->path, filename); + cl_assert(expect_mode == entry->mode); +} + +void test_index_filemodes__untrusted(void) +{ + git_config *cfg; + git_index *index; + bool can_filemode = cl_is_chmod_supported(); + + cl_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); + git_config_free(cfg); + + cl_git_pass(git_repository_index(&index, g_repo)); + cl_assert((git_index_caps(index) & GIT_INDEXCAP_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", 0100644); + + /* 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", 0100755); + + /* 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", 0100644); + + /* 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", 0100755); + + /* 5 - append 0644 over existing 0644 -> expect 0644 */ + replace_file_with_mode("exec_off", "filemodes/exec_off.2", 0644); + append_and_check_mode(index, "exec_off", 0100644); + + /* 6 - append 0644 over existing 0755 -> expect 0755 */ + replace_file_with_mode("exec_on", "filemodes/exec_on.2", 0644); + append_and_check_mode(index, "exec_on", 0100755); + + /* 7 - append 0755 over existing 0644 -> expect 0644 */ + replace_file_with_mode("exec_off", "filemodes/exec_off.3", 0755); + append_and_check_mode(index, "exec_off", 0100644); + + /* 8 - append 0755 over existing 0755 -> expect 0755 */ + replace_file_with_mode("exec_on", "filemodes/exec_on.3", 0755); + append_and_check_mode(index, "exec_on", 0100755); + + /* 9 - add new 0644 -> expect 0644 */ + cl_git_write2file("filemodes/new_off", "blah", + O_WRONLY | O_CREAT | O_TRUNC, 0644); + add_and_check_mode(index, "new_off", 0100644); + + /* this test won't give predictable results on a platform + * that doesn't support filemodes correctly, so skip it. + */ + if (can_filemode) { + /* 10 - add 0755 -> expect 0755 */ + cl_git_write2file("filemodes/new_on", "blah", + O_WRONLY | O_CREAT | O_TRUNC, 0755); + add_and_check_mode(index, "new_on", 0100755); + } + + git_index_free(index); +} + +void test_index_filemodes__trusted(void) +{ + git_config *cfg; + 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_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); + git_config_free(cfg); + + cl_git_pass(git_repository_index(&index, g_repo)); + cl_assert((git_index_caps(index) & GIT_INDEXCAP_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", 0100644); + + /* 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", 0100644); + + /* 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", 0100755); + + /* 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", 0100755); + + /* 5 - append 0644 over existing 0644 -> expect 0644 */ + replace_file_with_mode("exec_off", "filemodes/exec_off.2", 0644); + append_and_check_mode(index, "exec_off", 0100644); + + /* 6 - append 0644 over existing 0755 -> expect 0644 */ + replace_file_with_mode("exec_on", "filemodes/exec_on.2", 0644); + append_and_check_mode(index, "exec_on", 0100644); + + /* 7 - append 0755 over existing 0644 -> expect 0755 */ + replace_file_with_mode("exec_off", "filemodes/exec_off.3", 0755); + append_and_check_mode(index, "exec_off", 0100755); + + /* 8 - append 0755 over existing 0755 -> expect 0755 */ + replace_file_with_mode("exec_on", "filemodes/exec_on.3", 0755); + append_and_check_mode(index, "exec_on", 0100755); + + /* 9 - add new 0644 -> expect 0644 */ + cl_git_write2file("filemodes/new_off", "blah", + O_WRONLY | O_CREAT | O_TRUNC, 0644); + add_and_check_mode(index, "new_off", 0100644); + + /* 10 - add 0755 -> expect 0755 */ + cl_git_write2file("filemodes/new_on", "blah", + O_WRONLY | O_CREAT | O_TRUNC, 0755); + add_and_check_mode(index, "new_on", 0100755); + + git_index_free(index); +} diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c index e66ea118f..41922975e 100644 --- a/tests-clar/network/remotelocal.c +++ b/tests-clar/network/remotelocal.c @@ -107,7 +107,7 @@ void test_network_remotelocal__retrieve_advertised_references(void) cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert_equal_i(how_many_refs, 20); + cl_assert_equal_i(how_many_refs, 21); } void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void) @@ -121,7 +121,7 @@ void test_network_remotelocal__retrieve_advertised_references_from_spaced_reposi cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert_equal_i(how_many_refs, 20); + cl_assert_equal_i(how_many_refs, 21); git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ remote = NULL; diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c index 5185f25ea..e1387782e 100644 --- a/tests-clar/notes/notes.c +++ b/tests-clar/notes/notes.c @@ -15,6 +15,18 @@ void test_notes_notes__cleanup(void) cl_git_sandbox_cleanup(); } +static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) { + git_blob *blob; + + cl_assert_equal_s(git_note_message(note), message); + cl_assert(!git_oid_cmp(git_note_oid(note), note_oid)); + + cl_git_pass(git_blob_lookup(&blob, _repo, note_oid)); + cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob)); + + git_blob_free(blob); +} + static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message) { git_oid oid; @@ -23,38 +35,6 @@ static void create_note(git_oid *note_oid, const char *canonical_namespace, cons cl_git_pass(git_note_create(note_oid, _repo, _sig, _sig, canonical_namespace, &oid, message)); } -void test_notes_notes__1(void) -{ - git_oid oid, note_oid; - static git_note *note; - static git_blob *blob; - - cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479")); - - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n")); - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n")); - - cl_git_pass(git_note_read(¬e, _repo, NULL, &oid)); - - cl_assert_equal_s(git_note_message(note), "hello world\n"); - cl_assert(!git_oid_cmp(git_note_oid(note), ¬e_oid)); - - cl_git_pass(git_blob_lookup(&blob, _repo, ¬e_oid)); - cl_assert_equal_s(git_note_message(note), git_blob_rawcontent(blob)); - - cl_git_fail(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n")); - cl_git_fail(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n")); - - cl_git_pass(git_note_remove(_repo, NULL, _sig, _sig, &oid)); - cl_git_pass(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid)); - - cl_git_fail(git_note_remove(_repo, NULL, _sig, _sig, ¬e_oid)); - cl_git_fail(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid)); - - git_note_free(note); - git_blob_free(blob); -} - static struct { const char *note_sha; const char *annotated_object_sha; @@ -131,3 +111,152 @@ void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_retur cl_assert_equal_i(0, retrieved_notes); } + +void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void) +{ + git_oid note_oid, target_oid; + git_note *note, *default_namespace_note; + const char *default_ref; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + cl_git_pass(git_note_default_ref(&default_ref, _repo)); + + create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); + + cl_git_pass(git_note_read(¬e, _repo, NULL, &target_oid)); + cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref, &target_oid)); + + assert_note_equal(note, "hello world\n", ¬e_oid); + assert_note_equal(default_namespace_note, "hello world\n", ¬e_oid); + + git_note_free(note); + git_note_free(default_namespace_note); +} + +void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void) +{ + git_oid note_oid, target_oid; + git_note *note; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + + create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n"); + + cl_git_pass(git_note_read(¬e, _repo, "refs/notes/some/namespace", &target_oid)); + + assert_note_equal(note, "hello world on a custom namespace\n", ¬e_oid); + + git_note_free(note); +} + +/* + * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479 + * 08b041783f40edfe12bb406c9c9a8a040177c125 + */ +void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void) +{ + int error; + git_oid note_oid, target_oid; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + + create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); + error = git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &target_oid, "hello world\n"); + cl_git_fail(error); + cl_assert_equal_i(GIT_EEXISTS, error); + + create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); + error = git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello world\n"); + cl_git_fail(error); + cl_assert_equal_i(GIT_EEXISTS, error); +} + +static char *messages[] = { + "08c041783f40edfe12bb406c9c9a8a040177c125", + "96c45fbe09ab7445fc7c60fd8d17f32494399343", + "48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1", + "24c3eaafb681c3df668f9df96f58e7b8c756eb04", + "96ca1b6ccc7858ae94684777f85ac0e7447f7040", + "7ac2db4378a08bb244a427c357e0082ee0d57ac6", + "e6cba23dbf4ef84fe35e884f017f4e24dc228572", + "c8cf3462c7d8feba716deeb2ebe6583bd54589e2", + "39c16b9834c2d665ac5f68ad91dc5b933bad8549", + "f3c582b1397df6a664224ebbaf9d4cc952706597", + "29cec67037fe8e89977474988219016ae7f342a6", + "36c4cd238bf8e82e27b740e0741b025f2e8c79ab", + "f1c45a47c02e01d5a9a326f1d9f7f756373387f8", + "4aca84406f5daee34ab513a60717c8d7b1763ead", + "84ce167da452552f63ed8407b55d5ece4901845f", + NULL +}; + +#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1 + +/* + * $ git ls-tree refs/notes/fanout + * 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84 + * + * $ git ls-tree 4b22b35 + * 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759 96 + * + * $ git ls-tree d71aab4 + * 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125 071c1b46c854b31185ea97743be6a8774479 + */ +void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void) +{ + size_t i; + git_oid note_oid, target_oid; + git_note *_note; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + + for (i = 0; i < MESSAGES_COUNT; i++) { + cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/fanout", &target_oid, messages[i])); + cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid)); + git_note_free(_note); + + git_oid_cpy(&target_oid, ¬e_oid); + } +} + +/* + * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479 + * 08b041783f40edfe12bb406c9c9a8a040177c125 + */ +void test_notes_notes__can_read_a_note_in_an_existing_fanout(void) +{ + git_oid note_oid, target_oid; + git_note *note; + + cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); + cl_git_pass(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid)); + + cl_git_pass(git_oid_fromstr(¬e_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + cl_assert(!git_oid_cmp(git_note_oid(note), ¬e_oid)); + + git_note_free(note); +} + +void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void) +{ + git_oid target_oid; + git_note *note; + + cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); + cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid)); + + cl_git_fail(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid)); +} + +void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void) +{ + int error; + git_oid target_oid; + + cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); + cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid)); + + error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid); + cl_git_fail(error); + cl_assert_equal_i(GIT_ENOTFOUND, error); +} diff --git a/tests-clar/object/commit/commitstagedfile.c b/tests-clar/object/commit/commitstagedfile.c index a852458f4..628ef43c2 100644 --- a/tests-clar/object/commit/commitstagedfile.c +++ b/tests-clar/object/commit/commitstagedfile.c @@ -23,6 +23,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid; git_signature *signature; git_tree *tree; + char buffer[128]; /* * The test below replicates the following git scenario @@ -107,6 +108,9 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) */ cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); + + cl_git_pass(git_message_prettify(buffer, 128, "Initial commit", 0)); + cl_git_pass(git_commit_create_v( &commit_oid, repo, @@ -114,7 +118,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) signature, signature, NULL, - "Initial commit", + buffer, tree, 0)); diff --git a/tests-clar/object/message.c b/tests-clar/object/message.c index cbdc80a64..43be8b152 100644 --- a/tests-clar/object/message.c +++ b/tests-clar/object/message.c @@ -6,7 +6,7 @@ static void assert_message_prettifying(char *expected_output, char *input, int s { git_buf prettified_message = GIT_BUF_INIT; - git_message_prettify(&prettified_message, input, strip_comments); + git_message__prettify(&prettified_message, input, strip_comments); cl_assert_equal_s(expected_output, git_buf_cstr(&prettified_message)); git_buf_free(&prettified_message); diff --git a/tests-clar/object/tree/write.c b/tests-clar/object/tree/write.c index e6dc549a5..8b0f3417f 100644 --- a/tests-clar/object/tree/write.c +++ b/tests-clar/object/tree/write.c @@ -82,3 +82,71 @@ void test_object_tree_write__subtree(void) cl_assert(2 == git_tree_entrycount(tree)); git_tree_free(tree); } + +/* + * And the Lord said: Is this tree properly sorted? + */ +void test_object_tree_write__sorted_subtrees(void) +{ + git_treebuilder *builder; + unsigned int i; + int position_c = -1, position_cake = -1, position_config = -1; + + struct { + unsigned int attr; + const char *filename; + } entries[] = { + { 0100644, ".gitattributes" }, + { 0100644, ".gitignore" }, + { 0100644, ".htaccess" }, + { 0100644, "Capfile" }, + { 0100644, "Makefile"}, + { 0100644, "README"}, + { 0040000, "app"}, + { 0040000, "cake"}, + { 0040000, "config"}, + { 0100644, "c"}, + { 0100644, "git_test.txt"}, + { 0100644, "htaccess.htaccess"}, + { 0100644, "index.php"}, + { 0040000, "plugins"}, + { 0040000, "schemas"}, + { 0040000, "ssl-certs"}, + { 0040000, "vendors"} + }; + + git_oid blank_oid, tree_oid; + + memset(&blank_oid, 0x0, sizeof(blank_oid)); + + cl_git_pass(git_treebuilder_create(&builder, NULL)); + + for (i = 0; i < ARRAY_SIZE(entries); ++i) { + cl_git_pass(git_treebuilder_insert(NULL, + builder, entries[i].filename, &blank_oid, entries[i].attr)); + } + + cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder)); + + for (i = 0; i < builder->entries.length; ++i) { + git_tree_entry *entry = git_vector_get(&builder->entries, i); + + if (strcmp(entry->filename, "c") == 0) + position_c = i; + + if (strcmp(entry->filename, "cake") == 0) + position_cake = i; + + if (strcmp(entry->filename, "config") == 0) + position_config = i; + } + + cl_assert(position_c != -1); + cl_assert(position_cake != -1); + cl_assert(position_config != -1); + + cl_assert(position_c < position_cake); + cl_assert(position_cake < position_config); + + git_treebuilder_free(builder); +} diff --git a/tests-clar/refs/branches/foreach.c b/tests-clar/refs/branches/foreach.c new file mode 100644 index 000000000..51e3381f8 --- /dev/null +++ b/tests-clar/refs/branches/foreach.c @@ -0,0 +1,128 @@ +#include "clar_libgit2.h" +#include "refs.h" +#include "branch.h" + +static git_repository *repo; +static git_reference *fake_remote; + +void test_refs_branches_foreach__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")); + cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); +} + +void test_refs_branches_foreach__cleanup(void) +{ + git_reference_free(fake_remote); + git_repository_free(repo); + + cl_fixture_cleanup("testrepo.git"); +} + +static int count_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) +{ + int *count; + + GIT_UNUSED(branch_type); + GIT_UNUSED(branch_name); + + count = (int *)payload; + (*count)++; + + return 0; +} + +static void assert_retrieval(unsigned int flags, unsigned int expected_count) +{ + int count = 0; + + cl_git_pass(git_branch_foreach(repo, flags, count_branch_list_cb, &count)); + + cl_assert_equal_i(expected_count, count); +} + +void test_refs_branches_foreach__retrieve_all_branches(void) +{ + assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 9); +} + +void test_refs_branches_foreach__retrieve_remote_branches(void) +{ + assert_retrieval(GIT_BRANCH_REMOTE, 2); +} + +void test_refs_branches_foreach__retrieve_local_branches(void) +{ + assert_retrieval(GIT_BRANCH_LOCAL, 7); +} + +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; + + while (findings[pos].branch_name) + { + if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) { + cl_assert_equal_i(1, findings[pos].encounters); + return; + } + + pos++; + } + + cl_fail("expected branch not found in list."); +} + +static int contains_branch_list_cb(const char *branch_name, git_branch_t branch_type, void *payload) +{ + int pos = 0; + struct expectations *exp; + + GIT_UNUSED(branch_type); + + exp = (struct expectations *)payload; + + while (exp[pos].branch_name) + { + if (strcmp(branch_name, exp[pos].branch_name) == 0) + exp[pos].encounters++; + + pos++; + } + + return 0; +} + +/* + * $ git branch -r + * nulltoken/HEAD -> nulltoken/master + * nulltoken/master + */ +void test_refs_branches_foreach__retrieve_remote_symbolic_HEAD_when_present(void) +{ + struct expectations exp[] = { + { "nulltoken/HEAD", 0 }, + { "nulltoken/master", 0 }, + { NULL, 0 } + }; + + git_reference_free(fake_remote); + cl_git_pass(git_reference_create_symbolic(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0)); + + assert_retrieval(GIT_BRANCH_REMOTE, 3); + + cl_git_pass(git_branch_foreach(repo, GIT_BRANCH_REMOTE, contains_branch_list_cb, &exp)); + + assert_branch_has_been_found(exp, "nulltoken/HEAD"); + assert_branch_has_been_found(exp, "nulltoken/HEAD"); +} diff --git a/tests-clar/refs/branches/listall.c b/tests-clar/refs/branches/listall.c deleted file mode 100644 index 77f8270fb..000000000 --- a/tests-clar/refs/branches/listall.c +++ /dev/null @@ -1,78 +0,0 @@ -#include "clar_libgit2.h" -#include "refs.h" -#include "branch.h" - -static git_repository *repo; -static git_strarray branch_list; -static git_reference *fake_remote; - -void test_refs_branches_listall__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")); - cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); -} - -void test_refs_branches_listall__cleanup(void) -{ - git_strarray_free(&branch_list); - git_reference_free(fake_remote); - git_repository_free(repo); - - cl_fixture_cleanup("testrepo.git"); -} - -static void assert_retrieval(unsigned int flags, unsigned int expected_count) -{ - cl_git_pass(git_branch_list(&branch_list, repo, flags)); - - cl_assert_equal_i(branch_list.count, expected_count); -} - -void test_refs_branches_listall__retrieve_all_branches(void) -{ - assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 9); -} - -void test_refs_branches_listall__retrieve_remote_branches(void) -{ - assert_retrieval(GIT_BRANCH_REMOTE, 2); -} - -void test_refs_branches_listall__retrieve_local_branches(void) -{ - assert_retrieval(GIT_BRANCH_LOCAL, 7); -} - -static void assert_branch_list_contains(git_strarray *branches, const char* expected_branch_name) -{ - unsigned int i; - - for (i = 0; i < branches->count; i++) { - if (strcmp(expected_branch_name, branches->strings[i]) == 0) - return; - } - - cl_fail("expected branch not found in list."); -} - -/* - * $ git branch -r - * nulltoken/HEAD -> nulltoken/master - * nulltoken/master - */ -void test_refs_branches_listall__retrieve_remote_symbolic_HEAD_when_present(void) -{ - git_reference_free(fake_remote); - cl_git_pass(git_reference_create_symbolic(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0)); - - cl_git_pass(git_branch_list(&branch_list, repo, GIT_BRANCH_REMOTE)); - - cl_assert_equal_i(3, branch_list.count); - assert_branch_list_contains(&branch_list, "remotes/nulltoken/HEAD"); - assert_branch_list_contains(&branch_list, "remotes/nulltoken/master"); -} diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index fda99e9da..2ee72f206 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -104,6 +104,9 @@ void test_refs_revparse__to_type(void) void test_refs_revparse__linear_history(void) { + cl_git_fail(git_revparse_single(&g_obj, g_repo, "foo~bar")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master~bar")); + test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a"); @@ -163,6 +166,7 @@ void test_refs_revparse__colon(void) cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/")); cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/not found in any commit")); cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master:")); test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b"); diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c index 2e70c511e..1ba355ed6 100644 --- a/tests-clar/repo/init.c +++ b/tests-clar/repo/init.c @@ -245,5 +245,6 @@ void test_repo_init__reinit_doesnot_overwrite_ignorecase(void) void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void) { assert_config_entry_on_init_bytype("core.logallrefupdates", GIT_ENOTFOUND, true); + git_repository_free(_repo); assert_config_entry_on_init_bytype("core.logallrefupdates", true, false); } diff --git a/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 b/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 Binary files differnew file mode 100644 index 000000000..d1c032fce --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 diff --git a/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 b/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 Binary files differnew file mode 100644 index 000000000..0a1500a6f --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 diff --git a/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea b/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea Binary files differnew file mode 100644 index 000000000..b4e5aa186 --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea diff --git a/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 b/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 Binary files differnew file mode 100644 index 000000000..f3b46b3ca --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 diff --git a/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 b/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 Binary files differnew file mode 100644 index 000000000..2d47e6faf --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 diff --git a/tests-clar/resources/testrepo.git/refs/notes/fanout b/tests-clar/resources/testrepo.git/refs/notes/fanout new file mode 100644 index 000000000..1f1703631 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/notes/fanout @@ -0,0 +1 @@ +d07b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 diff --git a/tests-clar/revwalk/mergebase.c b/tests-clar/revwalk/mergebase.c index e807e3ad2..a210c1ff2 100644 --- a/tests-clar/revwalk/mergebase.c +++ b/tests-clar/revwalk/mergebase.c @@ -1,4 +1,6 @@ #include "clar_libgit2.h" +#include "vector.h" +#include <stdarg.h> static git_repository *_repo; @@ -16,9 +18,9 @@ void test_revwalk_mergebase__single1(void) { git_oid result, one, two, expected; - git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd "); - git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"); - git_oid_fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + cl_git_pass(git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd ")); + cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a")); + cl_git_pass(git_oid_fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644")); cl_git_pass(git_merge_base(&result, _repo, &one, &two)); cl_assert(git_oid_cmp(&result, &expected) == 0); @@ -28,9 +30,9 @@ void test_revwalk_mergebase__single2(void) { git_oid result, one, two, expected; - git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"); - git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"); + cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af")); + cl_git_pass(git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750")); + cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd")); cl_git_pass(git_merge_base(&result, _repo, &one, &two)); cl_assert(git_oid_cmp(&result, &expected) == 0); @@ -40,9 +42,9 @@ void test_revwalk_mergebase__merged_branch(void) { git_oid result, one, two, expected; - git_oid_fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"); - git_oid_fromstr(&expected, "9fd738e8f7967c078dceed8190330fc8648ee56a"); + cl_git_pass(git_oid_fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750")); + cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a")); + cl_git_pass(git_oid_fromstr(&expected, "9fd738e8f7967c078dceed8190330fc8648ee56a")); cl_git_pass(git_merge_base(&result, _repo, &one, &two)); cl_assert(git_oid_cmp(&result, &expected) == 0); @@ -53,12 +55,11 @@ void test_revwalk_mergebase__merged_branch(void) void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void) { - git_oid result, one, two, expected; + git_oid result, one, two; int error; - git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"); - git_oid_fromstr(&two, "e90810b8df3e80c413d903f631643c716887138d"); - git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"); + cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af")); + cl_git_pass(git_oid_fromstr(&two, "e90810b8df3e80c413d903f631643c716887138d")); error = git_merge_base(&result, _repo, &one, &two); cl_git_fail(error); @@ -66,6 +67,71 @@ void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void) cl_assert_equal_i(GIT_ENOTFOUND, error); } +static void assert_mergebase_many(const char *expected_sha, int count, ...) +{ + va_list ap; + int i; + git_oid *oids; + git_oid oid, expected; + char *partial_oid; + git_object *object; + + oids = git__malloc(count * sizeof(git_oid)); + cl_assert(oids != NULL); + + memset(oids, 0x0, count * sizeof(git_oid)); + + va_start(ap, count); + + for (i = 0; i < count; ++i) { + partial_oid = va_arg(ap, char *); + cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid))); + + cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJ_COMMIT)); + git_oid_cpy(&oids[i], git_object_id(object)); + git_object_free(object); + } + + va_end(ap); + + if (expected_sha == NULL) + cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_many(&oid, _repo, oids, count)); + else { + cl_git_pass(git_merge_base_many(&oid, _repo, oids, count)); + cl_git_pass(git_oid_fromstr(&expected, expected_sha)); + + cl_assert(git_oid_cmp(&expected, &oid) == 0); + } + + git__free(oids); +} + +void test_revwalk_mergebase__many_no_common_ancestor_returns_ENOTFOUND(void) +{ + assert_mergebase_many(NULL, 3, "41bc8c", "e90810", "a65fed"); + assert_mergebase_many(NULL, 3, "e90810", "41bc8c", "a65fed"); + assert_mergebase_many(NULL, 3, "e90810", "a65fed", "41bc8c"); + assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c"); + assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c"); + assert_mergebase_many(NULL, 3, "a65fed", "41bc8c", "e90810"); + + assert_mergebase_many(NULL, 3, "e90810", "763d71", "a65fed"); +} + +void test_revwalk_mergebase__many_merge_branch(void) +{ + assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607"); + + assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "e90810", "a65fed"); + assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "a65fed", "e90810"); + + assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607"); + assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "849607", "763d71"); + assert_mergebase_many("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71"); + + assert_mergebase_many("5b5b025afb0b4c913b4c338a42934a3863bf3644", 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c"); +} + /* * $ git log --graph --all * * commit 763d71aadf09a7951596c9746c024e7eece7c7af |
