diff --git a/.travis.yml b/.travis.yml index 2713651a8..c9d99d6f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ env: # Make sure CMake is installed install: - - sudo apt-get install cmake + - sudo apt-get install cmake valgrind # Run the Build script script: @@ -24,6 +24,7 @@ script: # Run Tests after_script: - ctest -V . + - if [ -f ./libgit2_clar ]; then valgrind --leak-check=full --show-reachable=yes ./libgit2_clar; else echo "Skipping valgrind"; fi # Only watch the development branch branches: diff --git a/CMakeLists.txt b/CMakeLists.txt index 6db18269b..6a0ffdd42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,13 +21,30 @@ STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_V STRING(REGEX REPLACE "^.*LIBGIT2_VERSION \"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" LIBGIT2_VERSION_REV "${GIT2_HEADER}") SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${LIBGIT2_VERSION_REV}") +IF (AMIGA) + # Default AmigaOS to use the PowerPC SHA1 + SET(SHA1_TYPE "ppc") +ENDIF() + # Find required dependencies INCLUDE_DIRECTORIES(src include deps/http-parser) FILE(GLOB SRC_HTTP deps/http-parser/*.c) +# Specify sha1 implementation +IF (SHA1_TYPE STREQUAL "ppc") + ADD_DEFINITIONS(-DPPC_SHA1) + FILE(GLOB SRC_SHA1 src/ppc/*.c src/ppc/*.S) +ELSE () + SET (SRC_SHA1) +ENDIF() + IF (NOT WIN32) FIND_PACKAGE(ZLIB) + IF (CMAKE_SYSTEM_NAME STREQUAL "AmigaOS") + INCLUDE_DIRECTORIES(deps/regex) + SET(SRC_REGEX deps/regex/regex.c) + ENDIF() ELSE() # Windows doesn't understand POSIX regex on its own INCLUDE_DIRECTORIES(deps/regex) @@ -45,7 +62,7 @@ ENDIF() # Installation paths SET(INSTALL_BIN bin CACHE PATH "Where to install binaries to.") -SET(INSTALL_LIB lib CACHE PATH "Where to install libraries to.") +SET(LIB_INSTALL_DIR lib CACHE PATH "Where to install libraries to.") SET(INSTALL_INC include CACHE PATH "Where to install headers to.") # Build options @@ -115,12 +132,15 @@ IF (WIN32 AND NOT CYGWIN) FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/win32/*.c src/compat/*.c) ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c src/compat/*.c) +ELSEIF (AMIGA) + ADD_DEFINITIONS(-DNO_ADDRINFO -DNO_READDIR_R) + FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/amiga/*.c src/compat/*.c) ELSE() FILE(GLOB SRC src/*.c src/transports/*.c src/xdiff/*.c src/unix/*.c) ENDIF () # Compile and link libgit2 -ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${WIN_RC}) +ADD_LIBRARY(git2 ${SRC} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX} ${SRC_SHA1} ${WIN_RC}) IF (WIN32) TARGET_LINK_LIBRARIES(git2 ws2_32) @@ -139,13 +159,18 @@ IF (MSVC) SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") ENDIF () +# Backward compatibility with INSTALL_LIB variable +if (INSTALL_LIB) + set(LIB_INSTALL_DIR "${INSTALL_LIB}") +ENDIF() + # Install INSTALL(TARGETS git2 RUNTIME DESTINATION ${INSTALL_BIN} - LIBRARY DESTINATION ${INSTALL_LIB} - ARCHIVE DESTINATION ${INSTALL_LIB} + LIBRARY DESTINATION ${LIB_INSTALL_DIR} + ARCHIVE DESTINATION ${LIB_INSTALL_DIR} ) -INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${INSTALL_LIB}/pkgconfig ) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig ) INSTALL(DIRECTORY include/git2 DESTINATION ${INSTALL_INC} ) INSTALL(FILES include/git2.h DESTINATION ${INSTALL_INC} ) diff --git a/README.md b/README.md index 4c23fc870..0075e53db 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ For more advanced use or questions about CMake please read * GObject - * libgit2-glib + * libgit2-glib * Haskell * 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/examples/network/Makefile b/examples/network/Makefile index c21869ac9..9afd49e5d 100644 --- a/examples/network/Makefile +++ b/examples/network/Makefile @@ -2,7 +2,7 @@ default: all CC = gcc CFLAGS += -g -CFLAGS += -I../../include -L../../ -lgit2 -lpthread +CFLAGS += -I../../include -L../../build -L../.. -lgit2 -lpthread OBJECTS = \ git2.o \ diff --git a/examples/network/fetch.c b/examples/network/fetch.c index 8dcb81b1f..d2752124d 100644 --- a/examples/network/fetch.c +++ b/examples/network/fetch.c @@ -91,6 +91,7 @@ int fetch(git_repository *repo, int argc, char **argv) usleep(10000); printf("\rReceived %d/%d objects in %d bytes", stats.processed, stats.total, bytes); } while (!data.finished); + printf("\rReceived %d/%d objects in %d bytes\n", stats.processed, stats.total, bytes); // Disconnect the underlying connection to prevent from idling. diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c index 5824fc555..ef5a35957 100644 --- a/examples/network/index-pack.c +++ b/examples/network/index-pack.c @@ -25,7 +25,7 @@ int index_pack(git_repository *repo, int argc, char **argv) return EXIT_FAILURE; } - if (git_indexer_stream_new(&idx, ".git") < 0) { + if (git_indexer_stream_new(&idx, ".") < 0) { puts("bad idx"); return -1; } diff --git a/include/git2.h b/include/git2.h index cab517d99..edb73e8a5 100644 --- a/include/git2.h +++ b/include/git2.h @@ -39,6 +39,8 @@ #include "git2/remote.h" #include "git2/clone.h" +#include "git2/attr.h" +#include "git2/branch.h" #include "git2/refspec.h" #include "git2/net.h" #include "git2/status.h" diff --git a/include/git2/attr.h b/include/git2/attr.h index 8f5a1268d..fad7183da 100644 --- a/include/git2/attr.h +++ b/include/git2/attr.h @@ -30,7 +30,7 @@ GIT_BEGIN_DECL * Then for file `xyz.c` looking up attribute "foo" gives a value for * which `GIT_ATTR_TRUE(value)` is true. */ -#define GIT_ATTR_TRUE(attr) ((attr) == git_attr__true) +#define GIT_ATTR_TRUE(attr) ((attr) == git_l_attr__true) /** * GIT_ATTR_FALSE checks if an attribute is set off. In core git @@ -44,7 +44,7 @@ GIT_BEGIN_DECL * Then for file `zyx.h` looking up attribute "foo" gives a value for * which `GIT_ATTR_FALSE(value)` is true. */ -#define GIT_ATTR_FALSE(attr) ((attr) == git_attr__false) +#define GIT_ATTR_FALSE(attr) ((attr) == git_l_attr__false) /** * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This @@ -62,7 +62,7 @@ GIT_BEGIN_DECL * file `onefile.rb` or looking up "bar" on any file will all give * `GIT_ATTR_UNSPECIFIED(value)` of true. */ -#define GIT_ATTR_UNSPECIFIED(attr) (!(attr) || (attr) == git_attr__unset) +#define GIT_ATTR_UNSPECIFIED(attr) (!(attr) || (attr) == git_l_attr__unset) /** * GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as @@ -75,12 +75,12 @@ GIT_BEGIN_DECL * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true. */ #define GIT_ATTR_HAS_VALUE(attr) \ - ((attr) && (attr) != git_attr__unset && \ - (attr) != git_attr__true && (attr) != git_attr__false) + ((attr) && (attr) != git_l_attr__unset && \ + (attr) != git_l_attr__true && (attr) != git_attr__false) -GIT_EXTERN(const char *) git_attr__true; -GIT_EXTERN(const char *) git_attr__false; -GIT_EXTERN(const char *) git_attr__unset; +GIT_EXTERN(const char *) git_l_attr__true; +GIT_EXTERN(const char *) git_l_attr__false; +GIT_EXTERN(const char *) git_l_attr__unset; /** * Check attribute flags: Reading values from index and working directory. 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 640adf5c2..e8ecc808b 100644 --- a/include/git2/commit.h +++ b/include/git2/commit.h @@ -178,6 +178,25 @@ GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsig */ GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned int n); +/** + * Get the commit object that is the th generation ancestor + * of the named commit object, following only the first parents. + * The returned commit has to be freed by the caller. + * + * Passing `0` as the generation number returns another instance of the + * base commit itself. + * + * @param ancestor Pointer where to store the ancestor commit + * @param commit a previously loaded commit. + * @param n the requested generation + * @return 0 on success; GIT_ENOTFOUND if no matching ancestor exists + * or an error code + */ +GIT_EXTERN(int) git_commit_nth_gen_ancestor( + git_commit **ancestor, + const git_commit *commit, + unsigned int n); + /** * Create a new commit in the repository using `git_object` * instances as parameters. diff --git a/include/git2/common.h b/include/git2/common.h index 0e9379804..1af045cff 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -55,6 +55,10 @@ #define GIT_WIN32 1 #endif +#ifdef __amigaos4__ +#include +#endif + /** * @file git2/common.h * @brief Git common platform definitions diff --git a/include/git2/config.h b/include/git2/config.h index 36946c4a5..c46e7fc9d 100644 --- a/include/git2/config.h +++ b/include/git2/config.h @@ -33,7 +33,7 @@ struct git_config_file { int (*set)(struct git_config_file *, const char *key, const char *value); int (*set_multivar)(git_config_file *cfg, const char *name, const char *regexp, const char *value); int (*del)(struct git_config_file *, const char *key); - int (*foreach)(struct git_config_file *, int (*fn)(const char *, const char *, void *), void *data); + int (*foreach)(struct git_config_file *, const char *, int (*fn)(const char *, const char *, void *), void *data); void (*free)(struct git_config_file *); }; @@ -314,6 +314,24 @@ GIT_EXTERN(int) git_config_foreach( int (*callback)(const char *var_name, const char *value, void *payload), void *payload); +/** + * Perform an operation on each config variable matching a regular expression. + * + * This behaviors like `git_config_foreach` with an additional filter of a + * regular expression that filters which config keys are passed to the + * callback. + * + * @param cfg where to get the variables from + * @param regexp regular expression to match against config names + * @param callback the function to call on each variable + * @param payload the data to pass to the callback + * @return 0 or the return value of the callback which didn't return 0 + */ +GIT_EXTERN(int) git_config_foreach_match( + git_config *cfg, + const char *regexp, + int (*callback)(const char *var_name, const char *value, void *payload), + void *payload); /** * Query the value of a config variable and return it mapped to 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/errors.h b/include/git2/errors.h index ccbc9fcf4..ca7f0de6e 100644 --- a/include/git2/errors.h +++ b/include/git2/errors.h @@ -17,43 +17,6 @@ */ GIT_BEGIN_DECL -#ifdef GIT_OLD_ERRORS -enum { - GIT_SUCCESS = 0, - GIT_ENOTOID = -2, - GIT_ENOTFOUND = -3, - GIT_ENOMEM = -4, - GIT_EOSERR = -5, - GIT_EOBJTYPE = -6, - GIT_ENOTAREPO = -7, - GIT_EINVALIDTYPE = -8, - GIT_EMISSINGOBJDATA = -9, - GIT_EPACKCORRUPTED = -10, - GIT_EFLOCKFAIL = -11, - GIT_EZLIB = -12, - GIT_EBUSY = -13, - GIT_EBAREINDEX = -14, - GIT_EINVALIDREFNAME = -15, - GIT_EREFCORRUPTED = -16, - GIT_ETOONESTEDSYMREF = -17, - GIT_EPACKEDREFSCORRUPTED = -18, - GIT_EINVALIDPATH = -19, - GIT_EREVWALKOVER = -20, - GIT_EINVALIDREFSTATE = -21, - GIT_ENOTIMPLEMENTED = -22, - GIT_EEXISTS = -23, - GIT_EOVERFLOW = -24, - GIT_ENOTNUM = -25, - GIT_ESTREAM = -26, - GIT_EINVALIDARGS = -27, - GIT_EOBJCORRUPTED = -28, - GIT_EAMBIGUOUS = -29, - GIT_EPASSTHROUGH = -30, - GIT_ENOMATCH = -31, - GIT_ESHORTBUFFER = -32, -}; -#endif - /** Generic return codes */ enum { GIT_OK = 0, @@ -72,6 +35,7 @@ typedef struct { int klass; } git_error; +/** Error classes */ typedef enum { GITERR_NOMEMORY, GITERR_OS, diff --git a/include/git2/indexer.h b/include/git2/indexer.h index 626377701..d300ba01a 100644 --- a/include/git2/indexer.h +++ b/include/git2/indexer.h @@ -29,9 +29,9 @@ typedef struct git_indexer_stream git_indexer_stream; * Create a new streaming indexer instance * * @param out where to store the indexer instance - * @param path to the gitdir (metadata directory) + * @param path to the directory where the packfile should be stored */ -GIT_EXTERN(int) git_indexer_stream_new(git_indexer_stream **out, const char *gitdir); +GIT_EXTERN(int) git_indexer_stream_new(git_indexer_stream **out, const char *path); /** * Add data to the indexer diff --git a/include/git2/odb.h b/include/git2/odb.h index e2443178c..dac9e06a9 100644 --- a/include/git2/odb.h +++ b/include/git2/odb.h @@ -172,6 +172,20 @@ GIT_EXTERN(int) git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *d */ GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id); +/** + * List all objects available in the database + * + * The callback will be called for each object available in the + * database. Note that the objects are likely to be returned in the + * index order, which would make accessing the objects in that order + * inefficient. + * + * @param db database to use + * @param cb the callback to call for each object + * @param data data to pass to the callback + */ +GIT_EXTERN(int) git_odb_foreach(git_odb *db, int (*cb)(git_oid *oid, void *data), void *data); + /** * Write an object directly into the ODB * diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h index f4620f5f4..3f67202d1 100644 --- a/include/git2/odb_backend.h +++ b/include/git2/odb_backend.h @@ -71,6 +71,12 @@ struct git_odb_backend { struct git_odb_backend *, const git_oid *); + int (*foreach)( + struct git_odb_backend *, + int (*cb)(git_oid *oid, void *data), + void *data + ); + void (* free)(struct git_odb_backend *); }; diff --git a/include/git2/reflog.h b/include/git2/reflog.h index f490e29de..8acba349b 100644 --- a/include/git2/reflog.h +++ b/include/git2/reflog.h @@ -53,6 +53,8 @@ GIT_EXTERN(int) git_reflog_write(git_reference *ref, const git_oid *oid_old, con /** * Rename the reflog for the given reference * + * The reflog to be renamed is expected to already exist + * * @param ref the reference * @param new_name the new name of the reference * @return 0 or an error code diff --git a/include/git2/refs.h b/include/git2/refs.h index 2918215aa..b119e90b1 100644 --- a/include/git2/refs.h +++ b/include/git2/refs.h @@ -258,7 +258,6 @@ GIT_EXTERN(int) git_reference_packall(git_repository *repo); */ GIT_EXTERN(int) git_reference_list(git_strarray *array, git_repository *repo, unsigned int list_flags); - /** * Perform an operation on each reference in the repository * @@ -324,6 +323,67 @@ GIT_EXTERN(void) git_reference_free(git_reference *ref); */ GIT_EXTERN(int) git_reference_cmp(git_reference *ref1, git_reference *ref2); +/** + * Loop over all the references and issue a callback for each one + * which name matches the given glob pattern. + * + * The processed references may be filtered by type, or using + * a bitwise OR of several types. Use the magic value + * `GIT_REF_LISTALL` to obtain all references, including + * packed ones. + * + * @param repo Repository where to find the references. + * + * @param list_flags Filtering flags for the reference + * listing. + * + * @param callback Callback to invoke per found reference. + * + * @param payload Extra parameter to callback function. + * + * @return 0 or an error code. + */ +GIT_EXTERN(int) git_reference_foreach_glob( + git_repository *repo, + const char *glob, + unsigned int list_flags, + int (*callback)( + const char *reference_name, + void *payload), + void *payload +); + +/** + * Check if a reflog exists for the specified reference. + * + * @param ref A git reference + * + * @return 0 when no reflog can be found, 1 when it exists; + * otherwise an error code. + */ +GIT_EXTERN(int) git_reference_has_log(git_reference *ref); + + +/** + * Return the reference supporting the remote tracking branch, + * given a reference branch. + * + * The input reference has to be located in the `refs/heads` + * namespace. + * + * @param tracking_ref Pointer where to store the retrieved + * reference. + * + * @param branch_ref A git local branch reference. + * + * @return 0 on success; GIT_ENOTFOUND when no remote tracking + * reference exists, otherwise an error code. + */ +GIT_EXTERN(int) git_reference_remote_tracking_from_branch( + git_reference **tracking_ref, + git_reference *branch_ref +); + /** @} */ GIT_END_DECL #endif diff --git a/include/git2/repository.h b/include/git2/repository.h index 0b56a0870..ff81b75ec 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -194,9 +194,12 @@ GIT_EXTERN(const char *) git_repository_workdir(git_repository *repo); * * @param repo A repository object * @param workdir The path to a working directory + * @param update_gitlink Create/update gitlink in workdir and set config + * "core.worktree" (if workdir is not the parent of the .git directory) * @return 0, or an error code */ -GIT_EXTERN(int) git_repository_set_workdir(git_repository *repo, const char *workdir); +GIT_EXTERN(int) git_repository_set_workdir( + git_repository *repo, const char *workdir, int update_gitlink); /** * Check if a repository is bare diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h index 2e9dc421a..d86bb28eb 100644 --- a/include/git2/revwalk.h +++ b/include/git2/revwalk.h @@ -107,7 +107,7 @@ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *oid); * The OIDs pointed to by the references that match the given glob * pattern will be pushed to the revision walker. * - * A leading 'refs/' is implied it not present as well as a trailing + * A leading 'refs/' is implied if not present as well as a trailing * '/ *' if the glob lacks '?', '*' or '['. * * @param walk the walker being used for the traversal @@ -146,7 +146,7 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *oid); * pattern and their ancestors will be hidden from the output on the * revision walk. * - * A leading 'refs/' is implied it not present as well as a trailing + * A leading 'refs/' is implied if not present as well as a trailing * '/ *' if the glob lacks '?', '*' or '['. * * @param walk the walker being used for the traversal diff --git a/include/git2/signature.h b/include/git2/signature.h index cbf94269f..cdbe67879 100644 --- a/include/git2/signature.h +++ b/include/git2/signature.h @@ -23,6 +23,9 @@ GIT_BEGIN_DECL * Create a new action signature. The signature must be freed * manually or using git_signature_free * + * Note: angle brackets ('<' and '>') characters are not allowed + * to be used in either the `name` or the `email` parameter. + * * @param sig_out new signature, in case of error NULL * @param name name of the person * @param email email of the person 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/include/git2/types.h b/include/git2/types.h index b4b48afa3..691903005 100644 --- a/include/git2/types.h +++ b/include/git2/types.h @@ -32,6 +32,9 @@ GIT_BEGIN_DECL * stat() functions, for all platforms. */ #include +#ifdef __amigaos4__ +#include +#endif #if defined(_MSC_VER) diff --git a/libgit2.pc.in b/libgit2.pc.in index 6165ad678..ddc03f36b 100644 --- a/libgit2.pc.in +++ b/libgit2.pc.in @@ -1,4 +1,4 @@ -libdir=@CMAKE_INSTALL_PREFIX@/@INSTALL_LIB@ +libdir=@CMAKE_INSTALL_PREFIX@/@LIB_INSTALL_DIR@ includedir=@CMAKE_INSTALL_PREFIX@/@INSTALL_INC@ Name: libgit2 diff --git a/packaging/rpm/libgit2.spec b/packaging/rpm/libgit2.spec index a6e82b241..80e70c164 100644 --- a/packaging/rpm/libgit2.spec +++ b/packaging/rpm/libgit2.spec @@ -65,7 +65,7 @@ to compile and develop applications that use libgit2. cmake . \ -DCMAKE_C_FLAGS:STRING="%{optflags}" \ -DCMAKE_INSTALL_PREFIX:PATH=%{_prefix} \ - -DINSTALL_LIB:PATH=%{_libdir} + -DLIB_INSTALL_DIR:PATH=%{_libdir}S make %{?_smp_mflags} %install diff --git a/src/amiga/map.c b/src/amiga/map.c new file mode 100755 index 000000000..2fb065c8b --- /dev/null +++ b/src/amiga/map.c @@ -0,0 +1,51 @@ +/* + * 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. + */ +#include + +#ifndef GIT_WIN32 + +#include "posix.h" +#include "map.h" +#include + +int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset) +{ + GIT_MMAP_VALIDATE(out, len, prot, flags); + + out->data = NULL; + out->len = 0; + + if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) { + giterr_set(GITERR_OS, "Trying to map shared-writeable"); + return -1; + } + + if((out->data = malloc(len))) { + p_lseek(fd, offset, SEEK_SET); + p_read(fd, out->data, len); + } + + if (!out->data || (out->data == MAP_FAILED)) { + giterr_set(GITERR_OS, "Failed to mmap. Could not write data"); + return -1; + } + + out->len = len; + + return 0; +} + +int p_munmap(git_map *map) +{ + assert(map != NULL); + free(map->data); + + return 0; +} + +#endif + diff --git a/src/attr_file.c b/src/attr_file.c index ca2f8fb58..0dad09727 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -5,9 +5,9 @@ #include "git2/tree.h" #include -const char *git_attr__true = "[internal]__TRUE__"; -const char *git_attr__false = "[internal]__FALSE__"; -const char *git_attr__unset = "[internal]__UNSET__"; +const char *git_l_attr__true = "[internal]__TRUE__"; +const char *git_l_attr__false = "[internal]__FALSE__"; +const char *git_l_attr__unset = "[internal]__UNSET__"; static int sort_by_hash_and_name(const void *a_raw, const void *b_raw); static void git_attr_rule__clear(git_attr_rule *rule); @@ -503,14 +503,14 @@ int git_attr_assignment__parse( } assign->name_hash = 5381; - assign->value = git_attr__true; + assign->value = git_l_attr__true; /* look for magic name prefixes */ if (*scan == '-') { - assign->value = git_attr__false; + assign->value = git_l_attr__false; scan++; } else if (*scan == '!') { - assign->value = git_attr__unset; /* explicit unspecified state */ + assign->value = git_l_attr__unset; /* explicit unspecified state */ scan++; } else if (*scan == '#') /* comment rest of line */ break; @@ -546,7 +546,7 @@ int git_attr_assignment__parse( } /* expand macros (if given a repo with a macro cache) */ - if (repo != NULL && assign->value == git_attr__true) { + if (repo != NULL && assign->value == git_l_attr__true) { git_attr_rule *macro = git_attr_cache__lookup_macro(repo, assign->name); 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; + branch_foreach_filter filter; - assert(branch_names && repo); - - 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/buffer.c b/src/buffer.c index 04aaec3df..5d54ee1a5 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -141,6 +141,42 @@ int git_buf_puts(git_buf *buf, const char *string) return git_buf_put(buf, string, strlen(string)); } +int git_buf_puts_escaped( + git_buf *buf, const char *string, const char *esc_chars, const char *esc_with) +{ + const char *scan = string; + size_t total = 0, esc_with_len = strlen(esc_with); + + while (*scan) { + size_t count = strcspn(scan, esc_chars); + total += count + 1 + esc_with_len; + scan += count + 1; + } + + ENSURE_SIZE(buf, buf->size + total + 1); + + for (scan = string; *scan; ) { + size_t count = strcspn(scan, esc_chars); + + memmove(buf->ptr + buf->size, scan, count); + scan += count; + buf->size += count; + + if (*scan) { + memmove(buf->ptr + buf->size, esc_with, esc_with_len); + buf->size += esc_with_len; + + memmove(buf->ptr + buf->size, scan, 1); + scan += 1; + buf->size += 1; + } + } + + buf->ptr[buf->size] = '\0'; + + return 0; +} + int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) { int len; diff --git a/src/buffer.h b/src/buffer.h index 50c75f64e..75f3b0e4f 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -90,6 +90,18 @@ void git_buf_rtruncate_at_char(git_buf *path, char separator); int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...); int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b); +/** + * Copy string into buf prefixing every character that is contained in the + * esc_chars string with the esc_with string. + */ +int git_buf_puts_escaped( + git_buf *buf, const char *string, const char *esc_chars, const char *esc_with); + +GIT_INLINE(int) git_buf_puts_escape_regex(git_buf *buf, const char *string) +{ + return git_buf_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\"); +} + /** * Join two strings as paths, inserting a slash between as needed. * @return 0 on success, -1 on failure @@ -121,6 +133,13 @@ GIT_INLINE(ssize_t) git_buf_rfind_next(git_buf *buf, char ch) return idx; } +GIT_INLINE(ssize_t) git_buf_rfind(git_buf *buf, char ch) +{ + ssize_t idx = (ssize_t)buf->size - 1; + while (idx >= 0 && buf->ptr[idx] != ch) idx--; + return idx; +} + /* Remove whitespace from the end of the buffer */ void git_buf_rtrim(git_buf *buf); diff --git a/src/commit.c b/src/commit.c index a3baf9d4e..32c47944b 100644 --- a/src/commit.c +++ b/src/commit.c @@ -229,30 +229,63 @@ GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset) GIT_COMMIT_GETTER(unsigned int, parentcount, commit->parent_oids.length) GIT_COMMIT_GETTER(const git_oid *, tree_oid, &commit->tree_oid); - int git_commit_tree(git_tree **tree_out, git_commit *commit) { assert(commit); return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_oid); } -int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n) -{ - git_oid *parent_oid; - assert(commit); - - parent_oid = git_vector_get(&commit->parent_oids, n); - if (parent_oid == NULL) { - giterr_set(GITERR_INVALID, "Parent %u does not exist", n); - return GIT_ENOTFOUND; - } - - return git_commit_lookup(parent, commit->object.repo, parent_oid); -} - const git_oid *git_commit_parent_oid(git_commit *commit, unsigned int n) { assert(commit); return git_vector_get(&commit->parent_oids, n); } + +int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n) +{ + const git_oid *parent_oid; + assert(commit); + + parent_oid = git_commit_parent_oid(commit, n); + if (parent_oid == NULL) { + giterr_set(GITERR_INVALID, "Parent %u does not exist", n); + return GIT_ENOTFOUND; + } + + return git_commit_lookup(parent, commit->object.repo, parent_oid); +} + +int git_commit_nth_gen_ancestor( + git_commit **ancestor, + const git_commit *commit, + unsigned int n) +{ + git_commit *current, *parent; + int error; + + assert(ancestor && commit); + + current = (git_commit *)commit; + + if (n == 0) + return git_commit_lookup( + ancestor, + commit->object.repo, + git_object_id((const git_object *)commit)); + + while (n--) { + error = git_commit_parent(&parent, (git_commit *)current, 0); + + if (current != commit) + git_commit_free(current); + + if (error < 0) + return error; + + current = parent; + } + + *ancestor = parent; + return 0; +} diff --git a/src/config.c b/src/config.c index d18b85c30..98fb3b20d 100644 --- a/src/config.c +++ b/src/config.c @@ -136,17 +136,27 @@ int git_config_add_file(git_config *cfg, git_config_file *file, int priority) * Loop over all the variables */ -int git_config_foreach(git_config *cfg, int (*fn)(const char *, const char *, void *), void *data) +int git_config_foreach( + git_config *cfg, int (*fn)(const char *, const char *, void *), void *data) +{ + return git_config_foreach_match(cfg, NULL, fn, data); +} + +int git_config_foreach_match( + git_config *cfg, + const char *regexp, + int (*fn)(const char *, const char *, void *), + void *data) { int ret = 0; unsigned int i; file_internal *internal; git_config_file *file; - for(i = 0; i < cfg->files.length && ret == 0; ++i) { + for (i = 0; i < cfg->files.length && ret == 0; ++i) { internal = git_vector_get(&cfg->files, i); file = internal->file; - ret = file->foreach(file, fn, data); + ret = file->foreach(file, regexp, fn, data); } return ret; diff --git a/src/config_file.c b/src/config_file.c index fd1aa8d08..1f3ebfca9 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -188,25 +188,46 @@ static void backend_free(git_config_file *_backend) git__free(backend); } -static int file_foreach(git_config_file *backend, int (*fn)(const char *, const char *, void *), void *data) +static int file_foreach( + git_config_file *backend, + const char *regexp, + int (*fn)(const char *, const char *, void *), + void *data) { diskfile_backend *b = (diskfile_backend *)backend; cvar_t *var; const char *key; + regex_t regex; + int result = 0; if (!b->values) return 0; - git_strmap_foreach(b->values, key, var, - do { - if (fn(key, var->value, data) < 0) - break; + if (regexp != NULL) { + if ((result = regcomp(®ex, regexp, REG_EXTENDED)) < 0) { + giterr_set_regex(®ex, result); + regfree(®ex); + return -1; + } + } - var = CVAR_LIST_NEXT(var); - } while (var != NULL); + git_strmap_foreach(b->values, key, var, + for (; var != NULL; var = CVAR_LIST_NEXT(var)) { + /* skip non-matching keys if regexp was provided */ + if (regexp && regexec(®ex, key, 0, NULL, 0) != 0) + continue; + + /* abort iterator on non-zero return value */ + if ((result = fn(key, var->value, data)) != 0) + goto cleanup; + } ); - return 0; +cleanup: + if (regexp != NULL) + regfree(®ex); + + return result; } static int config_set(git_config_file *cfg, const char *name, const char *value) @@ -337,6 +358,7 @@ static int config_get_multivar( result = regcomp(®ex, regex_str, REG_EXTENDED); if (result < 0) { giterr_set_regex(®ex, result); + regfree(®ex); return -1; } @@ -396,6 +418,7 @@ static int config_set_multivar( if (result < 0) { git__free(key); giterr_set_regex(&preg, result); + regfree(&preg); return -1; } diff --git a/src/config_file.h b/src/config_file.h index 0080b5713..c31292881 100644 --- a/src/config_file.h +++ b/src/config_file.h @@ -19,12 +19,27 @@ GIT_INLINE(void) git_config_file_free(git_config_file *cfg) cfg->free(cfg); } +GIT_INLINE(int) git_config_file_set_string( + git_config_file *cfg, const char *name, const char *value) +{ + return cfg->set(cfg, name, value); +} + GIT_INLINE(int) git_config_file_foreach( git_config_file *cfg, int (*fn)(const char *key, const char *value, void *data), void *data) { - return cfg->foreach(cfg, fn, data); + return cfg->foreach(cfg, NULL, fn, data); +} + +GIT_INLINE(int) git_config_file_foreach_match( + git_config_file *cfg, + const char *regexp, + int (*fn)(const char *key, const char *value, void *data), + void *data) +{ + return cfg->foreach(cfg, regexp, fn, data); } #endif 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/fetch.c b/src/fetch.c index 96b263faa..603284842 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -118,7 +118,8 @@ int git_fetch__download_pack( int recvd; char buff[1024]; gitno_buffer buf; - git_indexer_stream *idx; + git_buf path = GIT_BUF_INIT; + git_indexer_stream *idx = NULL; gitno_buffer_setup(t, &buf, buff, sizeof(buff)); @@ -127,9 +128,12 @@ int git_fetch__download_pack( return -1; } - if (git_indexer_stream_new(&idx, git_repository_path(repo)) < 0) + if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0) return -1; + if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0) + goto on_error; + memset(stats, 0, sizeof(git_indexer_stats)); if (git_indexer_stream_add(idx, buffered, buffered_size, stats) < 0) goto on_error; @@ -154,6 +158,7 @@ int git_fetch__download_pack( return 0; on_error: + git_buf_free(&path); git_indexer_stream_free(idx); return -1; } diff --git a/src/fileops.c b/src/fileops.c index 95a65893c..5849b79b2 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -94,7 +94,7 @@ int git_futils_open_ro(const char *path) { int fd = p_open(path, O_RDONLY); if (fd < 0) { - if (errno == ENOENT) + if (errno == ENOENT || errno == ENOTDIR) fd = GIT_ENOTFOUND; giterr_set(GITERR_OS, "Failed to open '%s'", path); } 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/indexer.c b/src/indexer.c index 5ae66c9f1..797a58275 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -142,7 +142,7 @@ int git_indexer_stream_new(git_indexer_stream **out, const char *prefix) { git_indexer_stream *idx; git_buf path = GIT_BUF_INIT; - static const char suff[] = "/objects/pack/pack-received"; + static const char suff[] = "/pack"; int error; idx = git__calloc(1, sizeof(git_indexer_stream)); @@ -169,29 +169,14 @@ cleanup: } /* Try to store the delta so we can try to resolve it later */ -static int store_delta(git_indexer_stream *idx) +static int store_delta(git_indexer_stream *idx, git_off_t entry_start, size_t entry_size, git_otype type) { - git_otype type; git_mwindow *w = NULL; - git_mwindow_file *mwf = &idx->pack->mwf; - git_off_t entry_start = idx->off; struct delta_info *delta; - size_t entry_size; git_rawobj obj; int error; - /* - * ref-delta objects can refer to object that we haven't - * found yet, so give it another opportunity - */ - if (git_packfile_unpack_header(&entry_size, &type, mwf, &w, &idx->off) < 0) - return -1; - - git_mwindow_close(&w); - - /* If it's not a delta, mark it as failure, we can't do anything with it */ - if (type != GIT_OBJ_REF_DELTA && type != GIT_OBJ_OFS_DELTA) - return -1; + assert(type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA); if (type == GIT_OBJ_REF_DELTA) { idx->off += GIT_OID_RAWSZ; @@ -313,8 +298,6 @@ int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t siz mwf = &idx->pack->mwf; if (git_mwindow_file_register(&idx->pack->mwf) < 0) return -1; - - return 0; } if (!idx->parsed_header) { @@ -352,27 +335,44 @@ int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t siz while (processed < idx->nr_objects) { git_rawobj obj; git_off_t entry_start = idx->off; + size_t entry_size; + git_otype type; + git_mwindow *w = NULL; if (idx->pack->mwf.size <= idx->off + 20) return 0; - error = git_packfile_unpack(&obj, idx->pack, &idx->off); + error = git_packfile_unpack_header(&entry_size, &type, mwf, &w, &idx->off); if (error == GIT_EBUFS) { idx->off = entry_start; return 0; } + if (error < 0) + return -1; - if (error < 0) { - idx->off = entry_start; - error = store_delta(idx); - if (error == GIT_EBUFS) + git_mwindow_close(&w); + + if (type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA) { + error = store_delta(idx, entry_start, entry_size, type); + if (error == GIT_EBUFS) { + idx->off = entry_start; return 0; + } if (error < 0) return error; continue; } + idx->off = entry_start; + error = git_packfile_unpack(&obj, idx->pack, &idx->off); + if (error == GIT_EBUFS) { + idx->off = entry_start; + return 0; + } + if (error < 0) + return -1; + if (hash_and_save(idx, &obj, entry_start) < 0) goto on_error; @@ -775,6 +775,7 @@ int git_indexer_write(git_indexer *idx) cleanup: git_mwindow_free_all(&idx->pack->mwf); + git_mwindow_file_deregister(&idx->pack->mwf); if (error < 0) git_filebuf_cleanup(&idx->file); git_buf_free(&filename); @@ -888,6 +889,7 @@ void git_indexer_free(git_indexer *idx) return; p_close(idx->pack->mwf.fd); + git_mwindow_file_deregister(&idx->pack->mwf); git_vector_foreach(&idx->objects, i, e) git__free(e); git_vector_free(&idx->objects); diff --git a/src/khash.h b/src/khash.h index bd67fe1f7..242204464 100644 --- a/src/khash.h +++ b/src/khash.h @@ -131,7 +131,9 @@ typedef unsigned long long khint64_t; #endif #ifdef _MSC_VER -#define inline __inline +#define kh_inline __inline +#else +#define kh_inline inline #endif typedef khint32_t khint_t; @@ -345,7 +347,7 @@ static const double __ac_HASH_UPPER = 0.77; __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) #define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ - KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) + KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) /* --- BEGIN OF HASH FUNCTIONS --- */ @@ -374,7 +376,7 @@ static const double __ac_HASH_UPPER = 0.77; @param s Pointer to a null terminated string @return The hash value */ -static inline khint_t __ac_X31_hash_string(const char *s) +static kh_inline khint_t __ac_X31_hash_string(const char *s) { khint_t h = (khint_t)*s; if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s; @@ -391,7 +393,7 @@ static inline khint_t __ac_X31_hash_string(const char *s) */ #define kh_str_hash_equal(a, b) (strcmp(a, b) == 0) -static inline khint_t __ac_Wang_hash(khint_t key) +static kh_inline khint_t __ac_Wang_hash(khint_t key) { key += ~(key << 15); key ^= (key >> 10); diff --git a/src/map.h b/src/map.h index 96d879547..6ce6d3685 100644 --- a/src/map.h +++ b/src/map.h @@ -23,6 +23,10 @@ #define GIT_MAP_TYPE 0xf #define GIT_MAP_FIXED 0x10 +#ifdef __amigaos4__ +#define MAP_FAILED 0 +#endif + typedef struct { /* memory mapped buffer */ void *data; /* data bytes */ size_t len; /* data length */ diff --git a/src/mwindow.c b/src/mwindow.c index 57adabd48..1a5446b9c 100644 --- a/src/mwindow.c +++ b/src/mwindow.c @@ -158,6 +158,7 @@ static git_mwindow *new_window( git_mwindow *w; w = git__malloc(sizeof(*w)); + if (w == NULL) return NULL; @@ -260,6 +261,23 @@ int git_mwindow_file_register(git_mwindow_file *mwf) return git_vector_insert(&ctl->windowfiles, mwf); } +int git_mwindow_file_deregister(git_mwindow_file *mwf) +{ + git_mwindow_ctl *ctl = &GIT_GLOBAL->mem_ctl; + git_mwindow_file *cur; + unsigned int i; + + git_vector_foreach(&ctl->windowfiles, i, cur) { + if (cur == mwf) { + git_vector_remove(&ctl->windowfiles, i); + return 0; + } + } + + giterr_set(GITERR_ODB, "Failed to find the memory window file to deregister"); + return -1; +} + void git_mwindow_close(git_mwindow **window) { git_mwindow *w = *window; diff --git a/src/mwindow.h b/src/mwindow.h index 058027251..d4fd19569 100644 --- a/src/mwindow.h +++ b/src/mwindow.h @@ -40,6 +40,7 @@ void git_mwindow_free_all(git_mwindow_file *mwf); unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, git_off_t offset, size_t extra, unsigned int *left); void git_mwindow_scan_lru(git_mwindow_file *mwf, git_mwindow **lru_w, git_mwindow **lru_l); int git_mwindow_file_register(git_mwindow_file *mwf); +int git_mwindow_file_deregister(git_mwindow_file *mwf); void git_mwindow_close(git_mwindow **w_cursor); #endif diff --git a/src/netops.c b/src/netops.c index 32554743f..b369e5106 100644 --- a/src/netops.c +++ b/src/netops.c @@ -381,16 +381,18 @@ int gitno_connect(git_transport *t, const char *host, const char *port) GIT_SOCKET s = INVALID_SOCKET; memset(&hints, 0x0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; + hints.ai_family = AF_UNSPEC; - if ((ret = getaddrinfo(host, port, &hints, &info)) < 0) { - giterr_set(GITERR_NET, "Failed to resolve address for %s: %s", host, gai_strerror(ret)); + if ((ret = p_getaddrinfo(host, port, &hints, &info)) < 0) { + giterr_set(GITERR_NET, + "Failed to resolve address for %s: %s", host, p_gai_strerror(ret)); return -1; } for (p = info; p != NULL; p = p->ai_next) { s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); + if (s == INVALID_SOCKET) { net_set_error("error creating socket"); break; @@ -411,7 +413,7 @@ int gitno_connect(git_transport *t, const char *host, const char *port) } t->socket = s; - freeaddrinfo(info); + p_freeaddrinfo(info); if (t->encrypt && ssl_setup(t, host) < 0) return -1; diff --git a/src/notes.c b/src/notes.c index efbdbabeb..7813e9985 100644 --- a/src/notes.c +++ b/src/notes.c @@ -56,7 +56,7 @@ static int find_subtree_r(git_tree **out, git_tree *root, error = find_subtree_in_current_level(&subtree, repo, root, target, *fanout); if (error == GIT_EEXISTS) { - return git_tree_lookup(out, repo, git_object_id((const git_object *)root)); + return git_tree_lookup(out, repo, git_tree_id(root)); } if (error < 0) @@ -64,13 +64,7 @@ static int find_subtree_r(git_tree **out, git_tree *root, *fanout += 2; error = find_subtree_r(out, subtree, repo, target, fanout); - - /* - * root is not ours to free, and the last subtree is the - * one being returned => we only need to free the subtrees in-between - */ - if (*out != subtree) - git_tree_free(subtree); + git_tree_free(subtree); return error; } @@ -103,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) @@ -153,7 +147,7 @@ static int manipulate_note_in_tree_r( int current_error)) { int error = -1; - git_tree *subtree = NULL; + git_tree *subtree = NULL, *new = NULL; char subtree_name[3]; error = find_subtree_in_current_level( @@ -176,7 +170,7 @@ static int manipulate_note_in_tree_r( /* An existing fanout has been found, let's dig deeper */ error = manipulate_note_in_tree_r( - out, repo, subtree, note_oid, annotated_object_sha, + &new, repo, subtree, note_oid, annotated_object_sha, fanout + 2, note_exists_cb, note_notfound_cb); if (error < 0) @@ -185,10 +179,12 @@ static int manipulate_note_in_tree_r( strncpy(subtree_name, annotated_object_sha + fanout, 2); subtree_name[2] = '\0'; - error = tree_write(out, repo, parent, - git_object_id((const git_object *)(*out)), subtree_name, 0040000); + error = tree_write(out, repo, parent, git_tree_id(new), + subtree_name, 0040000); + cleanup: + git_tree_free(new); git_tree_free(subtree); 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); diff --git a/src/odb.c b/src/odb.c index e0c8fa262..493c8292a 100644 --- a/src/odb.c +++ b/src/odb.c @@ -605,6 +605,18 @@ int git_odb_read_prefix( return 0; } +int git_odb_foreach(git_odb *db, int (*cb)(git_oid *oid, void *data), void *data) +{ + unsigned int i; + backend_internal *internal; + git_vector_foreach(&db->backends, i, internal) { + git_odb_backend *b = internal->backend; + b->foreach(b, cb, data); + } + + return 0; +} + int git_odb_write( git_oid *oid, git_odb *db, const void *data, size_t len, git_otype type) { diff --git a/src/odb_loose.c b/src/odb_loose.c index 989b03ab2..2197a4264 100644 --- a/src/odb_loose.c +++ b/src/odb_loose.c @@ -676,6 +676,89 @@ static int loose_backend__exists(git_odb_backend *backend, const git_oid *oid) return !error; } +struct foreach_state { + size_t dir_len; + int (*cb)(git_oid *oid, void *data); + void *data; +}; + +GIT_INLINE(int) filename_to_oid(git_oid *oid, const char *ptr) +{ + int v, i = 0; + if (strlen(ptr) != 41) + return -1; + + if (ptr[2] != '/') { + return -1; + } + + v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i+1]); + if (v < 0) + return -1; + + oid->id[0] = (unsigned char) v; + + ptr += 3; + for (i = 0; i < 38; i += 2) { + v = (git__fromhex(ptr[i]) << 4) | git__fromhex(ptr[i + 1]); + if (v < 0) + return -1; + + oid->id[1 + i/2] = (unsigned char) v; + } + + return 0; +} + +static int foreach_object_dir_cb(void *_state, git_buf *path) +{ + git_oid oid; + struct foreach_state *state = (struct foreach_state *) _state; + + if (filename_to_oid(&oid, path->ptr + state->dir_len) < 0) + return 0; + + if (state->cb(&oid, state->data) < 0) + return -1; + + return 0; +} + +static int foreach_cb(void *_state, git_buf *path) +{ + struct foreach_state *state = (struct foreach_state *) _state; + + if (git_path_direach(path, foreach_object_dir_cb, state) < 0) + return -1; + + return 0; +} + +static int loose_backend__foreach(git_odb_backend *_backend, int (*cb)(git_oid *oid, void *data), void *data) +{ + char *objects_dir; + int error; + git_buf buf = GIT_BUF_INIT; + struct foreach_state state; + loose_backend *backend = (loose_backend *) _backend; + + assert(backend && cb); + + objects_dir = backend->objects_dir; + + git_buf_sets(&buf, objects_dir); + git_path_to_dir(&buf); + + state.cb = cb; + state.data = data; + state.dir_len = git_buf_len(&buf); + + error = git_path_direach(&buf, foreach_cb, &state); + git_buf_free(&buf); + + return error; +} + static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream) { loose_writestream *stream = (loose_writestream *)_stream; @@ -845,6 +928,7 @@ int git_odb_backend_loose( backend->parent.read_header = &loose_backend__read_header; backend->parent.writestream = &loose_backend__stream; backend->parent.exists = &loose_backend__exists; + backend->parent.foreach = &loose_backend__foreach; backend->parent.free = &loose_backend__free; *backend_out = (git_odb_backend *)backend; diff --git a/src/odb_pack.c b/src/odb_pack.c index 458f288d9..4b860e864 100644 --- a/src/odb_pack.c +++ b/src/odb_pack.c @@ -420,6 +420,25 @@ static int pack_backend__exists(git_odb_backend *backend, const git_oid *oid) return pack_entry_find(&e, (struct pack_backend *)backend, oid) == 0; } +static int pack_backend__foreach(git_odb_backend *_backend, int (*cb)(git_oid *oid, void *data), void *data) +{ + struct git_pack_file *p; + struct pack_backend *backend; + unsigned int i; + + assert(_backend && cb); + backend = (struct pack_backend *)_backend; + + /* Make sure we know about the packfiles */ + if (packfile_refresh_all(backend) < 0) + return -1; + + git_vector_foreach(&backend->packs, i, p) { + git_pack_foreach_entry(p, cb, &data); + } + return 0; +} + static void pack_backend__free(git_odb_backend *_backend) { struct pack_backend *backend; @@ -463,6 +482,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir) backend->parent.read_prefix = &pack_backend__read_prefix; backend->parent.read_header = NULL; backend->parent.exists = &pack_backend__exists; + backend->parent.foreach = &pack_backend__foreach; backend->parent.free = &pack_backend__free; *backend_out = (git_odb_backend *)backend; diff --git a/src/oidmap.h b/src/oidmap.h index 858268c92..5a0bab6ec 100644 --- a/src/oidmap.h +++ b/src/oidmap.h @@ -34,7 +34,7 @@ GIT_INLINE(int) hash_git_oid_equal(const git_oid *a, const git_oid *b) } #define GIT__USE_OIDMAP \ - __KHASH_IMPL(oid, static inline, const git_oid *, void *, 1, hash_git_oid, hash_git_oid_equal) + __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, hash_git_oid, hash_git_oid_equal) #define git_oidmap_alloc() kh_init(oid) #define git_oidmap_free(h) kh_destroy(oid,h), h = NULL diff --git a/src/pack.c b/src/pack.c index 0db1069de..1d88eaa7d 100644 --- a/src/pack.c +++ b/src/pack.c @@ -262,7 +262,7 @@ int git_packfile_unpack_header( if (base == NULL) return GIT_EBUFS; - ret = packfile_unpack_header1(&used, size_p, type_p, base, left); + ret = packfile_unpack_header1(&used, size_p, type_p, base, left); git_mwindow_close(w_curs); if (ret == GIT_EBUFS) return ret; @@ -535,6 +535,7 @@ void packfile_free(struct git_pack_file *p) /* clear_delta_base_cache(); */ git_mwindow_free_all(&p->mwf); + git_mwindow_file_deregister(&p->mwf); if (p->mwf.fd != -1) p_close(p->mwf.fd); @@ -685,6 +686,49 @@ static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_ } } +int git_pack_foreach_entry( + struct git_pack_file *p, + int (*cb)(git_oid *oid, void *data), + void *data) + +{ + const unsigned char *index = p->index_map.data, *current; + unsigned stride; + uint32_t i; + + if (index == NULL) { + int error; + + if ((error = pack_index_open(p)) < 0) + return error; + + assert(p->index_map.data); + + index = p->index_map.data; + } + + if (p->index_version > 1) { + index += 8; + } + + index += 4 * 256; + + if (p->index_version > 1) { + stride = 20; + } else { + stride = 24; + index += 4; + } + + current = index; + for (i = 0; i < p->num_objects; i++) { + cb((git_oid *)current, data); + current += stride; + } + + return 0; +} + static int pack_entry_find_offset( git_off_t *offset_out, git_oid *found_oid, diff --git a/src/pack.h b/src/pack.h index cd7a4d2e1..7e1f978b0 100644 --- a/src/pack.h +++ b/src/pack.h @@ -102,5 +102,9 @@ int git_pack_entry_find( struct git_pack_file *p, const git_oid *short_oid, unsigned int len); +int git_pack_foreach_entry( + struct git_pack_file *p, + int (*cb)(git_oid *oid, void *data), + void *data); #endif diff --git a/src/path.c b/src/path.c index ee7e07e45..22391c52b 100644 --- a/src/path.c +++ b/src/path.c @@ -17,9 +17,7 @@ #include #include -#ifdef GIT_WIN32 #define LOOKS_LIKE_DRIVE_PREFIX(S) (git__isalpha((S)[0]) && (S)[1] == ':') -#endif /* * Based on the Android implementation, BSD licensed. @@ -172,11 +170,11 @@ int git_path_root(const char *path) { int offset = 0; -#ifdef GIT_WIN32 /* Does the root of the path look like a windows drive ? */ if (LOOKS_LIKE_DRIVE_PREFIX(path)) offset += 2; +#ifdef GIT_WIN32 /* Are we dealing with a windows network path? */ else if ((path[0] == '/' && path[1] == '/') || (path[0] == '\\' && path[1] == '\\')) @@ -527,6 +525,71 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base) return error; } +int git_path_resolve_relative(git_buf *path, size_t ceiling) +{ + char *base, *to, *from, *next; + size_t len; + + if (!path || git_buf_oom(path)) + return -1; + + if (ceiling > path->size) + ceiling = path->size; + + /* recognize drive prefixes, etc. that should not be backed over */ + if (ceiling == 0) + ceiling = git_path_root(path->ptr) + 1; + + /* recognize URL prefixes that should not be backed over */ + if (ceiling == 0) { + for (next = path->ptr; *next && git__isalpha(*next); ++next); + if (next[0] == ':' && next[1] == '/' && next[2] == '/') + ceiling = (next + 3) - path->ptr; + } + + base = to = from = path->ptr + ceiling; + + while (*from) { + for (next = from; *next && *next != '/'; ++next); + + len = next - from; + + if (len == 1 && from[0] == '.') + /* do nothing with singleton dot */; + + else if (len == 2 && from[0] == '.' && from[1] == '.') { + while (to > base && to[-1] == '/') to--; + while (to > base && to[-1] != '/') to--; + } + + else { + if (*next == '/') + len++; + + if (to != from) + memmove(to, from, len); + + to += len; + } + + from += len; + + while (*from == '/') from++; + } + + *to = '\0'; + + path->size = to - path->ptr; + + return 0; +} + +int git_path_apply_relative(git_buf *target, const char *relpath) +{ + git_buf_joinpath(target, git_buf_cstr(target), relpath); + return git_path_resolve_relative(target, 0); +} + int git_path_cmp( const char *name1, size_t len1, int isdir1, const char *name2, size_t len2, int isdir2) @@ -570,7 +633,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)); @@ -624,7 +687,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/path.h b/src/path.h index a845b3a14..5eecb7261 100644 --- a/src/path.h +++ b/src/path.h @@ -207,6 +207,29 @@ extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char */ extern int git_path_find_dir(git_buf *dir, const char *path, const char *base); +/** + * Resolve relative references within a path. + * + * This eliminates "./" and "../" relative references inside a path, + * as well as condensing multiple slashes into single ones. It will + * not touch the path before the "ceiling" length. + * + * Additionally, this will recognize an "c:/" drive prefix or a "xyz://" URL + * prefix and not touch that part of the path. + */ +extern int git_path_resolve_relative(git_buf *path, size_t ceiling); + +/** + * Apply a relative path to base path. + * + * Note that the base path could be a filename or a URL and this + * should still work. The relative path is walked segment by segment + * with three rules: series of slashes will be condensed to a single + * slash, "." will be eaten with no change, and ".." will remove a + * segment from the base path. + */ +extern int git_path_apply_relative(git_buf *target, const char *relpath); + /** * Walk each directory entry, except '.' and '..', calling fn(state). * diff --git a/src/pkt.c b/src/pkt.c index 88510f4b1..e003b97e2 100644 --- a/src/pkt.c +++ b/src/pkt.c @@ -296,7 +296,7 @@ static int buffer_want_with_caps(git_remote_head *head, git_transport_caps *caps git_buf_grow(buf, git_buf_len(buf) + len); git_oid_fmt(oid, &head->oid); - return git_buf_printf(buf, "%04xwant %s%c%s\n", len, oid, 0, capstr); + return git_buf_printf(buf, "%04xwant %s %s\n", len, oid, capstr); } /* diff --git a/src/pool.c b/src/pool.c index 641292d06..63bf09cee 100644 --- a/src/pool.c +++ b/src/pool.c @@ -275,6 +275,8 @@ uint32_t git_pool__system_page_size(void) SYSTEM_INFO info; GetSystemInfo(&info); size = (uint32_t)info.dwPageSize; +#elif defined(__amigaos4__) + size = (uint32_t)4096; /* 4K as there is no global value we can query */ #else size = (uint32_t)sysconf(_SC_PAGE_SIZE); #endif diff --git a/src/posix.c b/src/posix.c index a9a6af984..985221dd5 100644 --- a/src/posix.c +++ b/src/posix.c @@ -12,6 +12,97 @@ #ifndef GIT_WIN32 +#ifdef NO_ADDRINFO +int p_getaddrinfo( + const char *host, + const char *port, + struct addrinfo *hints, + struct addrinfo **info) +{ + GIT_UNUSED(hints); + + struct addrinfo *ainfo, *ai; + int p = 0; + + if ((ainfo = malloc(sizeof(struct addrinfo))) == NULL) + return -1; + + if ((ainfo->ai_hostent = gethostbyname(host)) == NULL) + return -2; + + ainfo->ai_servent = getservbyname(port, 0); + + if (ainfo->ai_servent) + ainfo->ai_port = ainfo->ai_servent->s_port; + else + ainfo->ai_port = atol(port); + + memcpy(&ainfo->ai_addr_in.sin_addr, + ainfo->ai_hostent->h_addr_list[0], + ainfo->ai_hostent->h_length); + + ainfo->ai_protocol = 0; + ainfo->ai_socktype = hints->ai_socktype; + ainfo->ai_family = ainfo->ai_hostent->h_addrtype; + ainfo->ai_addr_in.sin_family = ainfo->ai_family; + ainfo->ai_addr_in.sin_port = ainfo->ai_port; + ainfo->ai_addr = (struct addrinfo *)&ainfo->ai_addr_in; + ainfo->ai_addrlen = sizeof(struct sockaddr_in); + + *info = ainfo; + + if (ainfo->ai_hostent->h_addr_list[1] == NULL) { + ainfo->ai_next = NULL; + return 0; + } + + ai = ainfo; + + for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) { + ai->ai_next = malloc(sizeof(struct addrinfo)); + memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo)); + memcpy(&ai->ai_next->ai_addr_in.sin_addr, + ainfo->ai_hostent->h_addr_list[p], + ainfo->ai_hostent->h_length); + ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in; + ai = ai->ai_next; + } + + ai->ai_next = NULL; + return 0; +} + +void p_freeaddrinfo(struct addrinfo *info) +{ + struct addrinfo *p, *next; + + p = info; + + while(p != NULL) { + next = p->ai_next; + free(p); + p = next; + } +} + +const char *p_gai_strerror(int ret) +{ + switch(ret) { + case -1: + return "Out of memory"; + break; + + case -2: + return "Address lookup failed"; + break; + + default: + return "Unknown error"; + break; + } +} +#endif /* NO_ADDRINFO */ + int p_open(const char *path, int flags, ...) { mode_t mode = 0; @@ -63,7 +154,7 @@ int p_rename(const char *from, const char *to) return -1; } -#endif +#endif /* GIT_WIN32 */ int p_read(git_file fd, void *buf, size_t cnt) { diff --git a/src/posix.h b/src/posix.h index 5799c0499..d35fe08a5 100644 --- a/src/posix.h +++ b/src/posix.h @@ -83,6 +83,41 @@ extern int p_gettimeofday(struct timeval *tv, struct timezone *tz); # include "unix/posix.h" #endif -#define p_readdir_r(d,e,r) readdir_r(d,e,r) +#ifdef NO_READDIR_R +# include +GIT_INLINE(int) p_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) +{ + GIT_UNUSED(entry); + *result = readdir(dirp); + return 0; +} +#else /* NO_READDIR_R */ +# define p_readdir_r(d,e,r) readdir_r(d,e,r) +#endif + +#ifdef NO_ADDRINFO +# include +struct addrinfo { + struct hostent *ai_hostent; + struct servent *ai_servent; + struct sockaddr_in ai_addr_in; + struct sockaddr *ai_addr; + size_t ai_addrlen; + int ai_family; + int ai_socktype; + int ai_protocol; + long ai_port; + struct addrinfo *ai_next; +}; + +extern int p_getaddrinfo(const char *host, const char *port, + struct addrinfo *hints, struct addrinfo **info); +extern void p_freeaddrinfo(struct addrinfo *info); +extern const char *p_gai_strerror(int ret); +#else +# define p_getaddrinfo(a, b, c, d) getaddrinfo(a, b, c, d) +# define p_freeaddrinfo(a) freeaddrinfo(a) +# define p_gai_strerror(c) gai_strerror(c) +#endif /* NO_ADDRINFO */ #endif diff --git a/src/reflog.c b/src/reflog.c index 3ea073e65..004ba936d 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -269,18 +269,50 @@ cleanup: int git_reflog_rename(git_reference *ref, const char *new_name) { - int error; + int error = -1, fd; git_buf old_path = GIT_BUF_INIT; git_buf new_path = GIT_BUF_INIT; + git_buf temp_path = GIT_BUF_INIT; - if (!git_buf_join_n(&old_path, '/', 3, ref->owner->path_repository, - GIT_REFLOG_DIR, ref->name) && - !git_buf_join_n(&new_path, '/', 3, ref->owner->path_repository, - GIT_REFLOG_DIR, new_name)) - error = p_rename(git_buf_cstr(&old_path), git_buf_cstr(&new_path)); - else - error = -1; + assert(ref && new_name); + if (git_buf_joinpath(&temp_path, ref->owner->path_repository, GIT_REFLOG_DIR) < 0) + return -1; + + if (git_buf_joinpath(&old_path, git_buf_cstr(&temp_path), ref->name) < 0) + goto cleanup; + + if (git_buf_joinpath(&new_path, git_buf_cstr(&temp_path), new_name) < 0) + goto cleanup; + + /* + * Move the reflog to a temporary place. This two-phase renaming is required + * in order to cope with funny renaming use cases when one tries to move a reference + * to a partially colliding namespace: + * - a/b -> a/b/c + * - a/b/c/d -> a/b/c + */ + if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0) + goto cleanup; + + if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0) + goto cleanup; + p_close(fd); + + if (p_rename(git_buf_cstr(&old_path), git_buf_cstr(&temp_path)) < 0) + goto cleanup; + + if (git_path_isdir(git_buf_cstr(&new_path)) && + (git_futils_rmdir_r(git_buf_cstr(&new_path), GIT_DIRREMOVAL_ONLY_EMPTY_DIRS) < 0)) + goto cleanup; + + if (git_futils_mkpath2file(git_buf_cstr(&new_path), GIT_REFLOG_DIR_MODE) < 0) + goto cleanup; + + error = p_rename(git_buf_cstr(&temp_path), git_buf_cstr(&new_path)); + +cleanup: + git_buf_free(&temp_path); git_buf_free(&old_path); git_buf_free(&new_path); diff --git a/src/refs.c b/src/refs.c index 104685793..b3c140bec 100644 --- a/src/refs.c +++ b/src/refs.c @@ -11,6 +11,7 @@ #include "fileops.h" #include "pack.h" #include "reflog.h" +#include "config.h" #include #include @@ -1396,6 +1397,9 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) head_target = git_reference_target(head); if (head_target && !strcmp(head_target, ref->name)) { + git_reference_free(head); + head = NULL; + if (git_reference_create_symbolic(&head, ref->owner, "HEAD", new_name, 1) < 0) { giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference"); @@ -1404,18 +1408,11 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force) } /* - * Rename the reflog file. + * Rename the reflog file, if it exists. */ - if (git_buf_join_n(&aux_path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name) < 0) + if ((git_reference_has_log(ref)) && (git_reflog_rename(ref, new_name) < 0)) goto cleanup; - if (git_path_exists(aux_path.ptr) == true) { - if (git_reflog_rename(ref, new_name) < 0) - goto cleanup; - } else { - giterr_clear(); - } - /* * Change the name of the reference given by the user. */ @@ -1764,3 +1761,130 @@ int git_reference__update(git_repository *repo, const git_oid *oid, const char * git_reference_free(ref); return res; } + +struct glob_cb_data { + const char *glob; + int (*callback)(const char *, void *); + void *payload; +}; + +static int fromglob_cb(const char *reference_name, void *payload) +{ + struct glob_cb_data *data = (struct glob_cb_data *)payload; + + if (!p_fnmatch(data->glob, reference_name, 0)) + return data->callback(reference_name, data->payload); + + return 0; +} + +int git_reference_foreach_glob( + git_repository *repo, + const char *glob, + unsigned int list_flags, + int (*callback)( + const char *reference_name, + void *payload), + void *payload) +{ + struct glob_cb_data data; + + assert(repo && glob && callback); + + data.glob = glob; + data.callback = callback; + data.payload = payload; + + return git_reference_foreach( + repo, list_flags, fromglob_cb, &data); +} + +int git_reference_has_log( + git_reference *ref) +{ + git_buf path = GIT_BUF_INIT; + int result; + + assert(ref); + + if (git_buf_join_n(&path, '/', 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name) < 0) + return -1; + + result = git_path_isfile(git_buf_cstr(&path)); + git_buf_free(&path); + + return result; +} + +//TODO: How about also taking care of local tracking branches? +//cf. http://alblue.bandlem.com/2011/07/git-tip-of-week-tracking-branches.html +int git_reference_remote_tracking_from_branch( + git_reference **tracking_ref, + git_reference *branch_ref) +{ + git_config *config = NULL; + const char *name, *remote, *merge; + git_buf buf = GIT_BUF_INIT; + int error = -1; + + assert(tracking_ref && branch_ref); + + name = git_reference_name(branch_ref); + + if (git__prefixcmp(name, GIT_REFS_HEADS_DIR)) { + giterr_set( + GITERR_INVALID, + "Failed to retrieve tracking reference - '%s' is not a branch.", + name); + return -1; + } + + if (git_repository_config(&config, branch_ref->owner) < 0) + return -1; + + if (git_buf_printf( + &buf, + "branch.%s.remote", + name + strlen(GIT_REFS_HEADS_DIR)) < 0) + goto cleanup; + + if ((error = git_config_get_string(&remote, config, git_buf_cstr(&buf))) < 0) + goto cleanup; + + error = -1; + + git_buf_clear(&buf); + + //TODO: Is it ok to fail when no merge target is found? + if (git_buf_printf( + &buf, + "branch.%s.merge", + name + strlen(GIT_REFS_HEADS_DIR)) < 0) + goto cleanup; + + if (git_config_get_string(&merge, config, git_buf_cstr(&buf)) < 0) + goto cleanup; + + //TODO: Should we test this? + if (git__prefixcmp(merge, GIT_REFS_HEADS_DIR)) + goto cleanup; + + git_buf_clear(&buf); + + if (git_buf_printf( + &buf, + "refs/remotes/%s/%s", + remote, + merge + strlen(GIT_REFS_HEADS_DIR)) < 0) + goto cleanup; + + error = git_reference_lookup( + tracking_ref, + branch_ref->owner, + git_buf_cstr(&buf)); + +cleanup: + git_config_free(config); + git_buf_free(&buf); + return error; +} diff --git a/src/repository.c b/src/repository.c index 23a95b23e..a2931713e 100644 --- a/src/repository.c +++ b/src/repository.c @@ -602,14 +602,10 @@ void git_repository_set_index(git_repository *repo, git_index *index) GIT_REFCOUNT_INC(index); } -static int check_repositoryformatversion(git_repository *repo) +static int check_repositoryformatversion(git_config *config) { - git_config *config; int version; - if (git_repository_config__weakptr(&config, repo) < 0) - return -1; - if (git_config_get_int32(&version, config, "core.repositoryformatversion") < 0) return -1; @@ -623,26 +619,6 @@ static int check_repositoryformatversion(git_repository *repo) return 0; } -static int repo_init_reinit(git_repository **repo_out, const char *repository_path, int is_bare) -{ - git_repository *repo = NULL; - - GIT_UNUSED(is_bare); - - if (git_repository_open(&repo, repository_path) < 0) - return -1; - - if (check_repositoryformatversion(repo) < 0) { - git_repository_free(repo); - return -1; - } - - /* TODO: reinitialize the templates */ - - *repo_out = repo; - return 0; -} - static int repo_init_createhead(const char *git_dir) { git_buf ref_path = GIT_BUF_INIT; @@ -717,6 +693,12 @@ static int repo_init_config(const char *git_dir, bool is_bare, bool is_reinit) return -1; } + if (is_reinit && check_repositoryformatversion(config) < 0) { + git_buf_free(&cfg_path); + git_config_free(config); + return -1; + } + SET_REPO_CONFIG(bool, "core.bare", is_bare); SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION); SET_REPO_CONFIG(bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path))); @@ -759,15 +741,24 @@ static int repo_init_config(const char *git_dir, bool is_bare, bool is_reinit) #define GIT_DESC_CONTENT "Unnamed repository; edit this file 'description' to name the repository.\n" static int repo_write_template( - const char *git_dir, const char *file, mode_t mode, const char *content) + const char *git_dir, + bool allow_overwrite, + const char *file, + mode_t mode, + const char *content) { git_buf path = GIT_BUF_INIT; - int fd, error = 0; + int fd, error = 0, flags; if (git_buf_joinpath(&path, git_dir, file) < 0) return -1; - fd = p_open(git_buf_cstr(&path), O_WRONLY | O_CREAT | O_EXCL, mode); + if (allow_overwrite) + flags = O_WRONLY | O_CREAT | O_TRUNC; + else + flags = O_WRONLY | O_CREAT | O_EXCL; + + fd = p_open(git_buf_cstr(&path), flags, mode); if (fd >= 0) { error = p_write(fd, content, strlen(content)); @@ -829,7 +820,7 @@ static int repo_init_structure(const char *git_dir, int is_bare) /* Make template files as needed */ for (i = 0; tmpl[i].file != NULL; ++i) { if (repo_write_template( - git_dir, tmpl[i].file, tmpl[i].mode, tmpl[i].content) < 0) + git_dir, false, tmpl[i].file, tmpl[i].mode, tmpl[i].content) < 0) return -1; } @@ -850,21 +841,18 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is is_reinit = git_path_isdir(repository_path.ptr) && valid_repository_path(&repository_path); if (is_reinit) { - if (repo_init_reinit(repo_out, repository_path.ptr, is_bare) < 0) + /* TODO: reinitialize the templates */ + + if (repo_init_config(repository_path.ptr, is_bare, is_reinit) < 0) 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 || + } else if (repo_init_structure(repository_path.ptr, is_bare) < 0 || repo_init_config(repository_path.ptr, is_bare, is_reinit) < 0 || - repo_init_createhead(repository_path.ptr) < 0 || - git_repository_open(repo_out, repository_path.ptr) < 0) { + repo_init_createhead(repository_path.ptr) < 0) { goto cleanup; } - result = 0; + result = git_repository_open(repo_out, repository_path.ptr); cleanup: git_buf_free(&repository_path); @@ -964,8 +952,47 @@ const char *git_repository_workdir(git_repository *repo) return repo->workdir; } -int git_repository_set_workdir(git_repository *repo, const char *workdir) +static int write_gitlink( + const char *in_dir, const char *to_repo) { + int error; + git_buf buf = GIT_BUF_INIT; + struct stat st; + + if (git_path_dirname_r(&buf, to_repo) < 0 || + git_path_to_dir(&buf) < 0) + return -1; + + /* don't write gitlink to natural workdir */ + if (git__suffixcmp(to_repo, "/" DOT_GIT "/") == 0 && + strcmp(in_dir, buf.ptr) == 0) + return GIT_PASSTHROUGH; + + if (git_buf_joinpath(&buf, in_dir, DOT_GIT) < 0) + return -1; + + if (!p_stat(buf.ptr, &st) && !S_ISREG(st.st_mode)) { + giterr_set(GITERR_REPOSITORY, + "Cannot overwrite gitlink file into path '%s'", in_dir); + return GIT_EEXISTS; + } + + git_buf_clear(&buf); + + if (git_buf_printf(&buf, "%s %s", GIT_FILE_CONTENT_PREFIX, to_repo) < 0) + return -1; + + error = repo_write_template(in_dir, true, DOT_GIT, 0644, buf.ptr); + + git_buf_free(&buf); + + return error; +} + +int git_repository_set_workdir( + git_repository *repo, const char *workdir, int update_gitlink) +{ + int error = 0; git_buf path = GIT_BUF_INIT; assert(repo && workdir); @@ -973,11 +1000,37 @@ int git_repository_set_workdir(git_repository *repo, const char *workdir) if (git_path_prettify_dir(&path, workdir, NULL) < 0) return -1; - git__free(repo->workdir); + if (repo->workdir && strcmp(repo->workdir, path.ptr) == 0) + return 0; - repo->workdir = git_buf_detach(&path); - repo->is_bare = 0; - return 0; + if (update_gitlink) { + git_config *config; + + if (git_repository_config__weakptr(&config, repo) < 0) + return -1; + + error = write_gitlink(path.ptr, git_repository_path(repo)); + + /* passthrough error means gitlink is unnecessary */ + if (error == GIT_PASSTHROUGH) + error = git_config_delete(config, "core.worktree"); + else if (!error) + error = git_config_set_string(config, "core.worktree", path.ptr); + + if (!error) + error = git_config_set_bool(config, "core.bare", false); + } + + if (!error) { + char *old_workdir = repo->workdir; + + repo->workdir = git_buf_detach(&path); + repo->is_bare = 0; + + git__free(old_workdir); + } + + return error; } int git_repository_is_bare(git_repository *repo) diff --git a/src/revparse.c b/src/revparse.c index 3f210d11b..777dee685 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -14,747 +14,717 @@ #include "git2.h" typedef enum { - REVPARSE_STATE_INIT, - REVPARSE_STATE_CARET, - REVPARSE_STATE_LINEAR, - REVPARSE_STATE_COLON, - REVPARSE_STATE_DONE, + REVPARSE_STATE_INIT, + REVPARSE_STATE_CARET, + REVPARSE_STATE_LINEAR, + REVPARSE_STATE_COLON, + REVPARSE_STATE_DONE, } revparse_state; -static void set_invalid_syntax_err(const char *spec) +static int revspec_error(const char *revspec) { - giterr_set(GITERR_INVALID, "Refspec '%s' is not valid.", spec); + giterr_set(GITERR_INVALID, "Failed to parse revision specifier - Invalid pattern '%s'", revspec); + return -1; } static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository *repo, const char*spec) { - git_oid resolved; + git_oid resolved; + int error; - if (git_reference_name_to_oid(&resolved, repo, spec) < 0) - return GIT_ERROR; + if ((error = git_reference_name_to_oid(&resolved, repo, spec)) < 0) + return error; - return git_object_lookup(out, repo, &resolved, GIT_OBJ_ANY); + return git_object_lookup(out, repo, &resolved, GIT_OBJ_ANY); } /* Returns non-zero if yes */ static int spec_looks_like_describe_output(const char *spec) { - regex_t regex; - int regex_error, retcode; + regex_t regex; + int regex_error, retcode; - regex_error = regcomp(®ex, ".+-[0-9]+-g[0-9a-fA-F]+", REG_EXTENDED); - if (regex_error != 0) { - giterr_set_regex(®ex, regex_error); - return 1; /* To be safe */ - } - retcode = regexec(®ex, spec, 0, NULL, 0); - regfree(®ex); - return retcode == 0; + regex_error = regcomp(®ex, ".+-[0-9]+-g[0-9a-fA-F]+", REG_EXTENDED); + if (regex_error != 0) { + giterr_set_regex(®ex, regex_error); + return regex_error; + } + + retcode = regexec(®ex, spec, 0, NULL, 0); + regfree(®ex); + return retcode == 0; +} + +static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname) +{ + int error, i; + bool fallbackmode = true; + git_reference *ref; + git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT; + + static const char* formatters[] = { + "%s", + "refs/%s", + "refs/tags/%s", + "refs/heads/%s", + "refs/remotes/%s", + "refs/remotes/%s/HEAD", + NULL + }; + + if (*refname) + git_buf_puts(&name, refname); + else { + git_buf_puts(&name, "HEAD"); + fallbackmode = false; + } + + for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) { + + git_buf_clear(&refnamebuf); + + if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0) + goto cleanup; + + error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1); + + if (!error) { + *out = ref; + error = 0; + goto cleanup; + } + + if (error != GIT_ENOTFOUND) + goto cleanup; + } + +cleanup: + git_buf_free(&name); + git_buf_free(&refnamebuf); + return error; +} + +static int maybe_sha_or_abbrev(git_object**out, git_repository *repo, const char *spec) +{ + git_oid oid; + size_t speclen = strlen(spec); + + if (git_oid_fromstrn(&oid, spec, speclen) < 0) + return GIT_ENOTFOUND; + + return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY); +} + +static int maybe_describe(git_object**out, git_repository *repo, const char *spec) +{ + const char *substr; + int match; + + /* "git describe" output; snip everything before/including "-g" */ + substr = strstr(spec, "-g"); + + if (substr == NULL) + return GIT_ENOTFOUND; + + if ((match = spec_looks_like_describe_output(spec)) < 0) + return match; + + if (!match) + return GIT_ENOTFOUND; + + return maybe_sha_or_abbrev(out, repo, substr+2); } static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec) { - size_t speclen = strlen(spec); - git_object *obj = NULL; - git_oid oid; - git_buf refnamebuf = GIT_BUF_INIT; - static const char* formatters[] = { - "refs/%s", - "refs/tags/%s", - "refs/heads/%s", - "refs/remotes/%s", - "refs/remotes/%s/HEAD", - NULL - }; - unsigned int i; - const char *substr; + int error; + git_reference *ref; - /* "git describe" output; snip everything before/including "-g" */ - substr = strstr(spec, "-g"); - if (substr && - spec_looks_like_describe_output(spec) && - !revparse_lookup_object(out, repo, substr+2)) { - return 0; - } + error = maybe_describe(out, repo, spec); + if (!error) + return 0; - /* SHA or prefix */ - if (!git_oid_fromstrn(&oid, spec, speclen)) { - if (!git_object_lookup_prefix(&obj, repo, &oid, speclen, GIT_OBJ_ANY)) { - *out = obj; - return 0; - } - } + if (error < 0 && error != GIT_ENOTFOUND) + return error; - /* Fully-named ref */ - if (!revparse_lookup_fully_qualifed_ref(&obj, repo, spec)) { - *out = obj; - return 0; - } + error = disambiguate_refname(&ref, repo, spec); + if (!error) { + error = git_object_lookup(out, repo, git_reference_oid(ref), GIT_OBJ_ANY); + git_reference_free(ref); + return error; + } - /* Partially-named ref; match in this order: */ - for (i=0; formatters[i]; i++) { - git_buf_clear(&refnamebuf); - if (git_buf_printf(&refnamebuf, formatters[i], spec) < 0) { - return GIT_ERROR; - } + if (error < 0 && error != GIT_ENOTFOUND) + return error; - if (!revparse_lookup_fully_qualifed_ref(&obj, repo, git_buf_cstr(&refnamebuf))) { - git_buf_free(&refnamebuf); - *out = obj; - return 0; - } - } - git_buf_free(&refnamebuf); + error = maybe_sha_or_abbrev(out, repo, spec); + if (!error) + return 0; - giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); - return GIT_ERROR; + if (error < 0 && error != GIT_ENOTFOUND) + return error; + + giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); + return GIT_ENOTFOUND; } - static int all_chars_are_digits(const char *str, size_t len) { - size_t i=0; - for (i=0; i '9') return 0; - } - return 1; -} + size_t i = 0; -static void normalize_maybe_empty_refname(git_buf *buf, git_repository *repo, const char *refspec, size_t refspeclen) -{ - git_reference *ref; + for (i = 0; i < len; i++) + if (!git__isdigit(str[i])) return 0; - if (!refspeclen) { - /* Empty refspec means current branch (target of HEAD) */ - git_reference_lookup(&ref, repo, "HEAD"); - git_buf_puts(buf, git_reference_target(ref)); - git_reference_free(ref); - } else if (strstr(refspec, "HEAD")) { - /* Explicit head */ - git_buf_puts(buf, refspec); - }else { - if (git__prefixcmp(refspec, "refs/heads/") != 0) { - git_buf_printf(buf, "refs/heads/%s", refspec); - } else { - git_buf_puts(buf, refspec); - } - } + return 1; } static int walk_ref_history(git_object **out, git_repository *repo, const char *refspec, const char *reflogspec) { - git_reference *ref; - git_reflog *reflog = NULL; - int n, retcode = GIT_ERROR; - int i, refloglen; - const git_reflog_entry *entry; - git_buf buf = GIT_BUF_INIT; - size_t refspeclen = strlen(refspec); - size_t reflogspeclen = strlen(reflogspec); + git_reference *disambiguated = NULL; + git_reflog *reflog = NULL; + int n, retcode = GIT_ERROR; + int i, refloglen; + const git_reflog_entry *entry; + git_buf buf = GIT_BUF_INIT; + size_t refspeclen = strlen(refspec); + size_t reflogspeclen = strlen(reflogspec); - if (git__prefixcmp(reflogspec, "@{") != 0 || - git__suffixcmp(reflogspec, "}") != 0) { - giterr_set(GITERR_INVALID, "Bad reflogspec '%s'", reflogspec); - return GIT_ERROR; - } + if (git__prefixcmp(reflogspec, "@{") != 0 || + git__suffixcmp(reflogspec, "}") != 0) + return revspec_error(reflogspec); - /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ - if (refspeclen == 0 && !git__prefixcmp(reflogspec, "@{-")) { - regex_t regex; - int regex_error; + /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ + if (!git__prefixcmp(reflogspec, "@{-")) { + regex_t regex; + int regex_error; - if (git__strtol32(&n, reflogspec+3, NULL, 0) < 0 || - n < 1) { - giterr_set(GITERR_INVALID, "Invalid reflogspec %s", reflogspec); - return GIT_ERROR; - } + if (refspeclen > 0) + return revspec_error(reflogspec); - if (!git_reference_lookup(&ref, repo, "HEAD")) { - if (!git_reflog_read(&reflog, ref)) { - regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED); - if (regex_error != 0) { - giterr_set_regex(®ex, regex_error); - } else { - regmatch_t regexmatches[2]; + if (git__strtol32(&n, reflogspec+3, NULL, 10) < 0 || n < 1) + return revspec_error(reflogspec); - refloglen = git_reflog_entrycount(reflog); - for (i=refloglen-1; i >= 0; i--) { - const char *msg; - entry = git_reflog_entry_byindex(reflog, i); + if (!git_reference_lookup(&disambiguated, repo, "HEAD")) { + if (!git_reflog_read(&reflog, disambiguated)) { + regex_error = regcomp(®ex, "checkout: moving from (.*) to .*", REG_EXTENDED); + if (regex_error != 0) { + giterr_set_regex(®ex, regex_error); + } else { + regmatch_t regexmatches[2]; - msg = git_reflog_entry_msg(entry); - if (!regexec(®ex, msg, 2, regexmatches, 0)) { - n--; - if (!n) { - git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); - retcode = revparse_lookup_object(out, repo, git_buf_cstr(&buf)); - break; - } - } - } - regfree(®ex); - } - } - git_reference_free(ref); - } - } else { - int date_error = 0; - git_time_t timestamp; - git_buf datebuf = GIT_BUF_INIT; + retcode = GIT_ENOTFOUND; - git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); - date_error = git__date_parse(×tamp, git_buf_cstr(&datebuf)); + refloglen = git_reflog_entrycount(reflog); + for (i=refloglen-1; i >= 0; i--) { + const char *msg; + entry = git_reflog_entry_byindex(reflog, i); - /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ - if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { - git_config *cfg; - if (!git_repository_config(&cfg, repo)) { - /* Is the ref a tracking branch? */ - const char *remote; - git_buf_clear(&buf); - git_buf_printf(&buf, "branch.%s.remote", refspec); - if (!git_config_get_string(&remote, cfg, git_buf_cstr(&buf))) { - /* Yes. Find the first merge target name. */ - const char *mergetarget; - git_buf_clear(&buf); - git_buf_printf(&buf, "branch.%s.merge", refspec); - if (!git_config_get_string(&mergetarget, cfg, git_buf_cstr(&buf)) && - !git__prefixcmp(mergetarget, "refs/heads/")) { - /* Success. Look up the target and fetch the object. */ - git_buf_clear(&buf); - git_buf_printf(&buf, "refs/remotes/%s/%s", remote, mergetarget+11); - retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); - } - } - git_config_free(cfg); - } - } + msg = git_reflog_entry_msg(entry); + if (!regexec(®ex, msg, 2, regexmatches, 0)) { + n--; + if (!n) { + git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); + retcode = revparse_lookup_object(out, repo, git_buf_cstr(&buf)); + break; + } + } + } + regfree(®ex); + } + } + } + } else { + int date_error = 0, result; + git_time_t timestamp; + git_buf datebuf = GIT_BUF_INIT; - /* @{N} -> Nth prior value for the ref (from reflog) */ - else if (all_chars_are_digits(reflogspec+2, reflogspeclen-3) && - !git__strtol32(&n, reflogspec+2, NULL, 0) && - n <= 100000000) { /* Allow integer time */ - normalize_maybe_empty_refname(&buf, repo, refspec, refspeclen); + result = disambiguate_refname(&disambiguated, repo, refspec); - if (n == 0) { - retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); - } else if (!git_reference_lookup(&ref, repo, git_buf_cstr(&buf))) { - if (!git_reflog_read(&reflog, ref)) { - int numentries = git_reflog_entrycount(reflog); - if (numentries < n) { - giterr_set(GITERR_REFERENCE, "Reflog for '%s' has only %d entries, asked for %d", - git_buf_cstr(&buf), numentries, n); - retcode = GIT_ERROR; - } else { - const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, n); - const git_oid *oid = git_reflog_entry_oidold(entry); - retcode = git_object_lookup(out, repo, oid, GIT_OBJ_ANY); - } - } - git_reference_free(ref); - } - } + if (result < 0) { + retcode = result; + goto cleanup; + } - else if (!date_error) { - /* Ref as it was on a certain date */ - normalize_maybe_empty_refname(&buf, repo, refspec, refspeclen); + git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); + date_error = git__date_parse(×tamp, git_buf_cstr(&datebuf)); - if (!git_reference_lookup(&ref, repo, git_buf_cstr(&buf))) { - git_reflog *reflog; - if (!git_reflog_read(&reflog, ref)) { - /* Keep walking until we find an entry older than the given date */ - int numentries = git_reflog_entrycount(reflog); - int i; + /* @{u} or @{upstream} -> upstream branch, for a tracking branch. This is stored in the config. */ + if (!strcmp(reflogspec, "@{u}") || !strcmp(reflogspec, "@{upstream}")) { + git_reference *tracking; + + if (!(retcode = git_reference_remote_tracking_from_branch(&tracking, disambiguated))) { + retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_reference_name(tracking)); + git_reference_free(tracking); + } + } - /* TODO: clunky. Factor "now" into a utility */ - git_signature *sig; - git_time as_of; + /* @{N} -> Nth prior value for the ref (from reflog) */ + else if (all_chars_are_digits(reflogspec+2, reflogspeclen-3) && + !git__strtol32(&n, reflogspec+2, NULL, 10) && + n <= 100000000) { /* Allow integer time */ - git_signature_now(&sig, "blah", "blah"); - as_of = sig->when; - git_signature_free(sig); + git_buf_puts(&buf, git_reference_name(disambiguated)); - as_of.time = (timestamp > 0) - ? timestamp - : sig->when.time + timestamp; + if (n == 0) + retcode = revparse_lookup_fully_qualifed_ref(out, repo, git_buf_cstr(&buf)); + else if (!git_reflog_read(&reflog, disambiguated)) { + int numentries = git_reflog_entrycount(reflog); + if (numentries < n + 1) { + giterr_set(GITERR_REFERENCE, "Reflog for '%s' has only %d entries, asked for %d", + git_buf_cstr(&buf), numentries, n); + retcode = GIT_ENOTFOUND; + } else { + const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, n); + const git_oid *oid = git_reflog_entry_oidold(entry); + retcode = git_object_lookup(out, repo, oid, GIT_OBJ_ANY); + } + } + } - for (i=numentries-1; i>0; i--) { - const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, i); - git_time commit_time = git_reflog_entry_committer(entry)->when; - if (git__time_cmp(&commit_time, &as_of) <= 0 ) { - retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY); - break; - } - } + else if (!date_error) { + /* Ref as it was on a certain date */ + git_reflog *reflog; + if (!git_reflog_read(&reflog, disambiguated)) { + /* Keep walking until we find an entry older than the given date */ + int numentries = git_reflog_entrycount(reflog); + int i; - if (!i) { - /* Didn't find a match. Use the oldest revision in the reflog. */ - const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, 0); - retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY); - } + for (i = numentries - 1; i >= 0; i--) { + const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, i); + git_time commit_time = git_reflog_entry_committer(entry)->when; + if (commit_time.time - timestamp <= 0) { + retcode = git_object_lookup(out, repo, git_reflog_entry_oidnew(entry), GIT_OBJ_ANY); + break; + } + } - git_reflog_free(reflog); - } + if (i == -1) { + /* Didn't find a match */ + retcode = GIT_ENOTFOUND; + } - git_reference_free(ref); - } - } + git_reflog_free(reflog); + } + } - git_buf_free(&datebuf); - } + git_buf_free(&datebuf); + } - if (reflog) git_reflog_free(reflog); - git_buf_free(&buf); - return retcode; +cleanup: + if (reflog) + git_reflog_free(reflog); + git_buf_free(&buf); + git_reference_free(disambiguated); + return retcode; } static git_object* dereference_object(git_object *obj) { - git_otype type = git_object_type(obj); + git_otype type = git_object_type(obj); - switch (type) { - case GIT_OBJ_COMMIT: - { - git_tree *tree = NULL; - if (0 == git_commit_tree(&tree, (git_commit*)obj)) { - return (git_object*)tree; - } - } - break; - case GIT_OBJ_TAG: - { - git_object *newobj = NULL; - if (0 == git_tag_target(&newobj, (git_tag*)obj)) { - return newobj; - } - } - break; + switch (type) { + case GIT_OBJ_COMMIT: + { + git_tree *tree = NULL; + if (0 == git_commit_tree(&tree, (git_commit*)obj)) { + return (git_object*)tree; + } + } + break; + case GIT_OBJ_TAG: + { + git_object *newobj = NULL; + if (0 == git_tag_target(&newobj, (git_tag*)obj)) { + return newobj; + } + } + break; - default: - case GIT_OBJ_TREE: - case GIT_OBJ_BLOB: - case GIT_OBJ_OFS_DELTA: - case GIT_OBJ_REF_DELTA: - break; - } + default: + case GIT_OBJ_TREE: + case GIT_OBJ_BLOB: + case GIT_OBJ_OFS_DELTA: + case GIT_OBJ_REF_DELTA: + break; + } - /* Can't dereference some types */ - return NULL; + /* Can't dereference some types */ + return NULL; } static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) { - int retcode = 1; - git_object *obj1 = obj, *obj2 = obj; + int retcode = 1; + git_object *obj1 = obj, *obj2 = obj; - while (retcode > 0) { - git_otype this_type = git_object_type(obj1); + while (retcode > 0) { + git_otype this_type = git_object_type(obj1); - if (this_type == target_type) { - *out = obj1; - retcode = 0; - } else { - /* Dereference once, if possible. */ - obj2 = dereference_object(obj1); - if (!obj2) { - giterr_set(GITERR_REFERENCE, "Can't dereference to type"); - retcode = GIT_ERROR; - } - } - if (obj1 != obj && obj1 != obj2) { - git_object_free(obj1); - } - obj1 = obj2; - } - return retcode; + if (this_type == target_type) { + *out = obj1; + retcode = 0; + } else { + /* Dereference once, if possible. */ + obj2 = dereference_object(obj1); + if (!obj2) { + giterr_set(GITERR_REFERENCE, "Can't dereference to type"); + retcode = GIT_ERROR; + } + } + if (obj1 != obj && obj1 != obj2) { + git_object_free(obj1); + } + obj1 = obj2; + } + return retcode; } static git_otype parse_obj_type(const char *str) { - if (!strcmp(str, "{commit}")) return GIT_OBJ_COMMIT; - if (!strcmp(str, "{tree}")) return GIT_OBJ_TREE; - if (!strcmp(str, "{blob}")) return GIT_OBJ_BLOB; - if (!strcmp(str, "{tag}")) return GIT_OBJ_TAG; - return GIT_OBJ_BAD; + if (!strcmp(str, "{commit}")) return GIT_OBJ_COMMIT; + if (!strcmp(str, "{tree}")) return GIT_OBJ_TREE; + if (!strcmp(str, "{blob}")) return GIT_OBJ_BLOB; + if (!strcmp(str, "{tag}")) return GIT_OBJ_TAG; + return GIT_OBJ_BAD; } static int handle_caret_syntax(git_object **out, git_repository *repo, git_object *obj, const char *movement) { - git_commit *commit; - size_t movementlen = strlen(movement); - int n; + git_commit *commit; + size_t movementlen = strlen(movement); + int n; - if (*movement == '{') { - if (movement[movementlen-1] != '}') { - set_invalid_syntax_err(movement); - return GIT_ERROR; - } + if (*movement == '{') { + if (movement[movementlen-1] != '}') + return revspec_error(movement); - /* {} -> Dereference until we reach an object that isn't a tag. */ - if (movementlen == 2) { - git_object *newobj = obj; - git_object *newobj2 = newobj; - while (git_object_type(newobj2) == GIT_OBJ_TAG) { - newobj2 = dereference_object(newobj); - if (newobj != obj) git_object_free(newobj); - if (!newobj2) { - giterr_set(GITERR_REFERENCE, "Couldn't find object of target type."); - return GIT_ERROR; - } - newobj = newobj2; - } - *out = newobj2; - return 0; - } + /* {} -> Dereference until we reach an object that isn't a tag. */ + if (movementlen == 2) { + git_object *newobj = obj; + git_object *newobj2 = newobj; + while (git_object_type(newobj2) == GIT_OBJ_TAG) { + newobj2 = dereference_object(newobj); + if (newobj != obj) git_object_free(newobj); + if (!newobj2) { + giterr_set(GITERR_REFERENCE, "Couldn't find object of target type."); + return GIT_ERROR; + } + newobj = newobj2; + } + *out = newobj2; + return 0; + } - /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ - if (movement[1] == '/') { - int retcode = GIT_ERROR; - git_revwalk *walk; - if (!git_revwalk_new(&walk, repo)) { - git_oid oid; - regex_t preg; - int reg_error; - git_buf buf = GIT_BUF_INIT; + /* {/...} -> Walk all commits until we see a commit msg that matches the phrase. */ + if (movement[1] == '/') { + int retcode = GIT_ERROR; + git_revwalk *walk; + if (!git_revwalk_new(&walk, repo)) { + git_oid oid; + regex_t preg; + int reg_error; + git_buf buf = GIT_BUF_INIT; - git_revwalk_sorting(walk, GIT_SORT_TIME); - git_revwalk_push(walk, git_object_id(obj)); + git_revwalk_sorting(walk, GIT_SORT_TIME); + git_revwalk_push(walk, git_object_id(obj)); - /* Extract the regex from the movement string */ - git_buf_put(&buf, movement+2, strlen(movement)-3); + /* Extract the regex from the movement string */ + git_buf_put(&buf, movement+2, strlen(movement)-3); - reg_error = regcomp(&preg, git_buf_cstr(&buf), REG_EXTENDED); - if (reg_error != 0) { - giterr_set_regex(&preg, reg_error); - } else { - while(!git_revwalk_next(&oid, walk)) { - git_object *walkobj; + reg_error = regcomp(&preg, git_buf_cstr(&buf), REG_EXTENDED); + if (reg_error != 0) { + giterr_set_regex(&preg, reg_error); + } else { + while(!git_revwalk_next(&oid, walk)) { + git_object *walkobj; - /* Fetch the commit object, and check for matches in the message */ - if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) { - if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) { - /* Found it! */ - retcode = 0; - *out = walkobj; - if (obj == walkobj) { - /* Avoid leaking an object */ - git_object_free(walkobj); - } - break; - } - git_object_free(walkobj); - } - } - if (retcode < 0) { - giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", movement); - } - regfree(&preg); - } + /* Fetch the commit object, and check for matches in the message */ + if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) { + if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) { + /* Found it! */ + retcode = 0; + *out = walkobj; + if (obj == walkobj) { + /* Avoid leaking an object */ + git_object_free(walkobj); + } + break; + } + git_object_free(walkobj); + } + } + if (retcode < 0) { + giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", movement); + } + regfree(&preg); + } - git_buf_free(&buf); - git_revwalk_free(walk); - } - return retcode; - } + git_buf_free(&buf); + git_revwalk_free(walk); + } + return retcode; + } - /* {...} -> Dereference until we reach an object of a certain type. */ - if (dereference_to_type(out, obj, parse_obj_type(movement)) < 0) { - return GIT_ERROR; - } - return 0; - } + /* {...} -> Dereference until we reach an object of a certain type. */ + if (dereference_to_type(out, obj, parse_obj_type(movement)) < 0) { + return GIT_ERROR; + } + return 0; + } - /* Dereference until we reach a commit. */ - if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { - /* Can't dereference to a commit; fail */ - return GIT_ERROR; - } + /* Dereference until we reach a commit. */ + if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { + /* Can't dereference to a commit; fail */ + return GIT_ERROR; + } - /* "^" is the same as "^1" */ - if (movementlen == 0) { - n = 1; - } else { - git__strtol32(&n, movement, NULL, 0); - } - commit = (git_commit*)obj; + /* "^" is the same as "^1" */ + if (movementlen == 0) { + n = 1; + } else { + git__strtol32(&n, movement, NULL, 10); + } + commit = (git_commit*)obj; - /* "^0" just returns the input */ - if (n == 0) { - *out = obj; - return 0; - } + /* "^0" just returns the input */ + if (n == 0) { + *out = obj; + return 0; + } - if (git_commit_parent(&commit, commit, n-1) < 0) { - return GIT_ERROR; - } + if (git_commit_parent(&commit, commit, n-1) < 0) { + return GIT_ENOTFOUND; + } - *out = (git_object*)commit; - return 0; + *out = (git_object*)commit; + return 0; } static int handle_linear_syntax(git_object **out, git_object *obj, const char *movement) { - git_commit *commit1, *commit2; - int i, n; + int n; - /* Dereference until we reach a commit. */ - if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { - /* Can't dereference to a commit; fail */ - return GIT_ERROR; - } + /* Dereference until we reach a commit. */ + if (dereference_to_type(&obj, obj, GIT_OBJ_COMMIT) < 0) { + /* Can't dereference to a commit; fail */ + return GIT_ERROR; + } - /* "~" is the same as "~1" */ - if (*movement == '\0') { - n = 1; - } else if (git__strtol32(&n, movement, NULL, 0) < 0) { - return GIT_ERROR; - } - commit1 = (git_commit*)obj; + /* "~" is the same as "~1" */ + if (*movement == '\0') { + n = 1; + } else if (git__strtol32(&n, movement, NULL, 10) < 0) { + return GIT_ERROR; + } - /* "~0" just returns the input */ - if (n == 0) { - *out = obj; - return 0; - } - - for (i=0; ioid) < 0) { - git__free(alloc); - return GIT_ERROR; - } - } - } - - 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; + return git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)obj, n); } static int handle_colon_syntax(git_object **out, - git_repository *repo, - git_object *obj, - const char *path) + git_repository *repo, + git_object *obj, + const char *path) { - git_tree *tree; - git_oid oid; - int error; + git_object *tree = obj; + int error = -1; + git_tree_entry *entry = NULL; - /* Dereference until we reach a tree. */ - if (dereference_to_type(&obj, obj, GIT_OBJ_TREE) < 0) { - return GIT_ERROR; - } - tree = (git_tree*)obj; + /* Dereference until we reach a tree. */ + if (dereference_to_type(&tree, obj, GIT_OBJ_TREE) < 0) + return GIT_ERROR; - /* Find the blob at the given path. */ - error = oid_for_tree_path(&oid, tree, repo, path); - git_tree_free(tree); + if (*path == '\0') + return git_object_lookup(out, repo, git_object_id(tree), GIT_OBJ_TREE); - if (error < 0) - return error; + /* + * TODO: Handle the relative path syntax + * (:./relative/path and :../relative/path) + */ + if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0) + goto cleanup; - return git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); + error = git_tree_entry_to_object(out, repo, entry); + +cleanup: + git_tree_entry_free(entry); + if (tree != obj) + git_object_free(tree); + + return error; } static int revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) { - git_revwalk *walk; - int retcode = GIT_ERROR; + git_revwalk *walk; + int retcode = GIT_ERROR; - if (!pattern[0]) { - giterr_set(GITERR_REGEX, "Empty pattern"); - return GIT_ERROR; - } + if (!pattern[0]) { + giterr_set(GITERR_REGEX, "Empty pattern"); + return GIT_ERROR; + } - if (!git_revwalk_new(&walk, repo)) { - regex_t preg; - int reg_error; - git_oid oid; + if (!git_revwalk_new(&walk, repo)) { + regex_t preg; + int reg_error; + git_oid oid; - git_revwalk_sorting(walk, GIT_SORT_TIME); - git_revwalk_push_glob(walk, "refs/heads/*"); + git_revwalk_sorting(walk, GIT_SORT_TIME); + git_revwalk_push_glob(walk, "refs/heads/*"); - reg_error = regcomp(&preg, pattern, REG_EXTENDED); - if (reg_error != 0) { - giterr_set_regex(&preg, reg_error); - } else { - git_object *walkobj = NULL, *resultobj = NULL; - while(!git_revwalk_next(&oid, walk)) { - /* Fetch the commit object, and check for matches in the message */ - if (walkobj != resultobj) git_object_free(walkobj); - if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) { - if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) { - /* Match! */ - resultobj = walkobj; - retcode = 0; - break; - } - } - } - if (!resultobj) { - giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", pattern); - git_object_free(walkobj); - } else { - *out = resultobj; - } - regfree(&preg); - git_revwalk_free(walk); - } - } + reg_error = regcomp(&preg, pattern, REG_EXTENDED); + if (reg_error != 0) { + giterr_set_regex(&preg, reg_error); + } else { + git_object *walkobj = NULL, *resultobj = NULL; + while(!git_revwalk_next(&oid, walk)) { + /* Fetch the commit object, and check for matches in the message */ + if (walkobj != resultobj) git_object_free(walkobj); + if (!git_object_lookup(&walkobj, repo, &oid, GIT_OBJ_COMMIT)) { + if (!regexec(&preg, git_commit_message((git_commit*)walkobj), 0, NULL, 0)) { + /* Match! */ + resultobj = walkobj; + retcode = 0; + break; + } + } + } + if (!resultobj) { + giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", pattern); + retcode = GIT_ENOTFOUND; + git_object_free(walkobj); + } else { + *out = resultobj; + } + regfree(&preg); + git_revwalk_free(walk); + } + } - return retcode; + return retcode; } int git_revparse_single(git_object **out, git_repository *repo, const char *spec) { - revparse_state current_state = REVPARSE_STATE_INIT, next_state = REVPARSE_STATE_INIT; - const char *spec_cur = spec; - git_object *cur_obj = NULL, *next_obj = NULL; - git_buf specbuffer = GIT_BUF_INIT, stepbuffer = GIT_BUF_INIT; - int retcode = 0; + revparse_state current_state = REVPARSE_STATE_INIT, next_state = REVPARSE_STATE_INIT; + const char *spec_cur = spec; + git_object *cur_obj = NULL, *next_obj = NULL; + git_buf specbuffer = GIT_BUF_INIT, stepbuffer = GIT_BUF_INIT; + int retcode = 0; - assert(out && repo && spec); + assert(out && repo && spec); - if (spec[0] == ':') { - if (spec[1] == '/') { - return revparse_global_grep(out, repo, spec+2); - } - /* TODO: support merge-stage path lookup (":2:Makefile"). */ - giterr_set(GITERR_INVALID, "Unimplemented"); - return GIT_ERROR; - } + if (spec[0] == ':') { + if (spec[1] == '/') { + return revparse_global_grep(out, repo, spec+2); + } + /* TODO: support merge-stage path lookup (":2:Makefile"). */ + giterr_set(GITERR_INVALID, "Unimplemented"); + return GIT_ERROR; + } - while (current_state != REVPARSE_STATE_DONE) { - switch (current_state) { - case REVPARSE_STATE_INIT: - if (!*spec_cur) { - /* No operators, just a name. Find it and return. */ - retcode = revparse_lookup_object(out, repo, spec); - next_state = REVPARSE_STATE_DONE; - } else if (*spec_cur == '@') { - /* '@' syntax doesn't allow chaining */ - git_buf_puts(&stepbuffer, spec_cur); - retcode = walk_ref_history(out, repo, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer)); - next_state = REVPARSE_STATE_DONE; - } else if (*spec_cur == '^') { - next_state = REVPARSE_STATE_CARET; - } else if (*spec_cur == '~') { - next_state = REVPARSE_STATE_LINEAR; - } else if (*spec_cur == ':') { - next_state = REVPARSE_STATE_COLON; - } else { - git_buf_putc(&specbuffer, *spec_cur); - } - spec_cur++; + while (current_state != REVPARSE_STATE_DONE) { + switch (current_state) { + case REVPARSE_STATE_INIT: + if (!*spec_cur) { + /* No operators, just a name. Find it and return. */ + retcode = revparse_lookup_object(out, repo, spec); + next_state = REVPARSE_STATE_DONE; + } else if (*spec_cur == '@') { + /* '@' syntax doesn't allow chaining */ + git_buf_puts(&stepbuffer, spec_cur); + retcode = walk_ref_history(out, repo, git_buf_cstr(&specbuffer), git_buf_cstr(&stepbuffer)); + next_state = REVPARSE_STATE_DONE; + } else if (*spec_cur == '^') { + next_state = REVPARSE_STATE_CARET; + } else if (*spec_cur == '~') { + next_state = REVPARSE_STATE_LINEAR; + } else if (*spec_cur == ':') { + next_state = REVPARSE_STATE_COLON; + } else { + git_buf_putc(&specbuffer, *spec_cur); + } + spec_cur++; - if (current_state != next_state && next_state != REVPARSE_STATE_DONE) { - /* Leaving INIT state, find the object specified, in case that state needs it */ - if (revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer)) < 0) { - retcode = GIT_ERROR; - next_state = REVPARSE_STATE_DONE; - } - } - break; + if (current_state != next_state && next_state != REVPARSE_STATE_DONE) { + /* Leaving INIT state, find the object specified, in case that state needs it */ + if ((retcode = revparse_lookup_object(&next_obj, repo, git_buf_cstr(&specbuffer))) < 0) + next_state = REVPARSE_STATE_DONE; + } + break; - case REVPARSE_STATE_CARET: - /* Gather characters until NULL, '~', or '^' */ - if (!*spec_cur) { - retcode = handle_caret_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer)); - next_state = REVPARSE_STATE_DONE; - } else if (*spec_cur == '~') { - retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer)); - git_buf_clear(&stepbuffer); - next_state = !retcode ? REVPARSE_STATE_LINEAR : REVPARSE_STATE_DONE; - } else if (*spec_cur == '^') { - retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer)); - git_buf_clear(&stepbuffer); - if (retcode < 0) { - next_state = REVPARSE_STATE_DONE; - } - } else { - git_buf_putc(&stepbuffer, *spec_cur); - } - spec_cur++; - break; + case REVPARSE_STATE_CARET: + /* Gather characters until NULL, '~', or '^' */ + if (!*spec_cur) { + retcode = handle_caret_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer)); + next_state = REVPARSE_STATE_DONE; + } else if (*spec_cur == '~') { + retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); + next_state = !retcode ? REVPARSE_STATE_LINEAR : REVPARSE_STATE_DONE; + } else if (*spec_cur == '^') { + retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); + if (retcode < 0) { + next_state = REVPARSE_STATE_DONE; + } + } else if (*spec_cur == ':') { + retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); + next_state = !retcode ? REVPARSE_STATE_COLON : REVPARSE_STATE_DONE; + } else { + git_buf_putc(&stepbuffer, *spec_cur); + } + spec_cur++; + break; - case REVPARSE_STATE_LINEAR: - if (!*spec_cur) { - retcode = handle_linear_syntax(out, cur_obj, git_buf_cstr(&stepbuffer)); - next_state = REVPARSE_STATE_DONE; - } else if (*spec_cur == '~') { - retcode = handle_linear_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); - git_buf_clear(&stepbuffer); - if (retcode < 0) { - next_state = REVPARSE_STATE_DONE; - } - } else if (*spec_cur == '^') { - retcode = handle_linear_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); - git_buf_clear(&stepbuffer); - next_state = !retcode ? REVPARSE_STATE_CARET : REVPARSE_STATE_DONE; - } else { - git_buf_putc(&stepbuffer, *spec_cur); - } - spec_cur++; - break; + case REVPARSE_STATE_LINEAR: + if (!*spec_cur) { + retcode = handle_linear_syntax(out, cur_obj, git_buf_cstr(&stepbuffer)); + next_state = REVPARSE_STATE_DONE; + } else if (*spec_cur == '~') { + retcode = handle_linear_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); + if (retcode < 0) { + next_state = REVPARSE_STATE_DONE; + } + } else if (*spec_cur == '^') { + retcode = handle_linear_syntax(&next_obj, cur_obj, git_buf_cstr(&stepbuffer)); + git_buf_clear(&stepbuffer); + next_state = !retcode ? REVPARSE_STATE_CARET : REVPARSE_STATE_DONE; + } else { + git_buf_putc(&stepbuffer, *spec_cur); + } + spec_cur++; + break; - case REVPARSE_STATE_COLON: - if (*spec_cur) { - git_buf_putc(&stepbuffer, *spec_cur); - } else { - retcode = handle_colon_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer)); - next_state = REVPARSE_STATE_DONE; - } - spec_cur++; - break; + case REVPARSE_STATE_COLON: + if (*spec_cur) { + git_buf_putc(&stepbuffer, *spec_cur); + } else { + retcode = handle_colon_syntax(out, repo, cur_obj, git_buf_cstr(&stepbuffer)); + next_state = REVPARSE_STATE_DONE; + } + spec_cur++; + break; - case REVPARSE_STATE_DONE: - if (cur_obj && *out != cur_obj) git_object_free(cur_obj); - if (next_obj && *out != next_obj) git_object_free(next_obj); - break; - } + case REVPARSE_STATE_DONE: + if (cur_obj && *out != cur_obj) git_object_free(cur_obj); + if (next_obj && *out != next_obj) git_object_free(next_obj); + break; + } - current_state = next_state; - if (cur_obj != next_obj) { - if (cur_obj) git_object_free(cur_obj); - cur_obj = next_obj; - } - } + current_state = next_state; + if (cur_obj != next_obj) { + if (cur_obj) git_object_free(cur_obj); + cur_obj = next_obj; + } + } - if (*out != cur_obj) git_object_free(cur_obj); - if (*out != next_obj && next_obj != cur_obj) git_object_free(next_obj); + if (*out != cur_obj) git_object_free(cur_obj); + if (*out != next_obj && next_obj != cur_obj) git_object_free(next_obj); - git_buf_free(&specbuffer); - git_buf_free(&stepbuffer); - return retcode; + git_buf_free(&specbuffer); + git_buf_free(&stepbuffer); + return retcode; } diff --git a/src/revwalk.c b/src/revwalk.c index 13d54b725..9dff283f5 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -188,7 +188,7 @@ static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawo const size_t parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1; unsigned char *buffer = raw->data; unsigned char *buffer_end = buffer + raw->len; - unsigned char *parents_start; + unsigned char *parents_start, *committer_start; int i, parents = 0; int commit_time; @@ -219,17 +219,34 @@ static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawo commit->out_degree = (unsigned short)parents; + if ((committer_start = buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) + return commit_error(commit, "object is corrupted"); + + buffer++; + if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) return commit_error(commit, "object is corrupted"); - if ((buffer = memchr(buffer, '<', buffer_end - buffer)) == NULL || - (buffer = memchr(buffer, '>', buffer_end - buffer)) == NULL) - return commit_error(commit, "malformed author information"); + /* Skip trailing spaces */ + while (buffer > committer_start && git__isspace(*buffer)) + buffer--; - while (*buffer == '>' || git__isspace(*buffer)) - buffer++; + /* Seek for the begining of the pack of digits */ + while (buffer > committer_start && git__isdigit(*buffer)) + buffer--; - if (git__strtol32(&commit_time, (char *)buffer, NULL, 10) < 0) + /* Skip potential timezone offset */ + if ((buffer > committer_start) && (*buffer == '+' || *buffer == '-')) { + buffer--; + + while (buffer > committer_start && git__isspace(*buffer)) + buffer--; + + while (buffer > committer_start && git__isdigit(*buffer)) + buffer--; + } + + if ((buffer == committer_start) || (git__strtol32(&commit_time, (char *)(buffer + 1), NULL, 10) < 0)) return commit_error(commit, "cannot parse commit time"); commit->time = (time_t)commit_time; @@ -540,7 +557,6 @@ static int push_ref(git_revwalk *walk, const char *refname, int hide) struct push_cb_data { git_revwalk *walk; - const char *glob; int hide; }; @@ -548,10 +564,7 @@ static int push_glob_cb(const char *refname, void *data_) { struct push_cb_data *data = (struct push_cb_data *)data_; - if (!p_fnmatch(data->glob, refname, 0)) - return push_ref(data->walk, refname, data->hide); - - return 0; + return push_ref(data->walk, refname, data->hide); } static int push_glob(git_revwalk *walk, const char *glob, int hide) @@ -584,11 +597,10 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide) goto on_error; data.walk = walk; - data.glob = git_buf_cstr(&buf); data.hide = hide; - if (git_reference_foreach( - walk->repo, GIT_REF_LISTALL, push_glob_cb, &data) < 0) + if (git_reference_foreach_glob( + walk->repo, git_buf_cstr(&buf), GIT_REF_LISTALL, push_glob_cb, &data) < 0) goto on_error; regfree(&preg); diff --git a/src/signature.c b/src/signature.c index 332bdf65f..1f788356b 100644 --- a/src/signature.c +++ b/src/signature.c @@ -40,7 +40,7 @@ static const char *skip_trailing_spaces(const char *buffer_start, const char *bu static int signature_error(const char *msg) { - giterr_set(GITERR_INVALID, "Failed to parse signature - %s", msg); + giterr_set(GITERR_INVALID, "Failed to process signature - %s", msg); return -1; } @@ -72,9 +72,16 @@ static int process_trimming(const char *input, char **storage, const char *input return 0; } +static bool contains_angle_brackets(const char *input) +{ + if (strchr(input, '<') != NULL) + return true; + + return strchr(input, '>') != NULL; +} + int git_signature_new(git_signature **sig_out, const char *name, const char *email, git_time_t time, int offset) { - int error; git_signature *p = NULL; assert(name && email); @@ -84,11 +91,18 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema p = git__calloc(1, sizeof(git_signature)); GITERR_CHECK_ALLOC(p); - if ((error = process_trimming(name, &p->name, name + strlen(name), 1)) < 0 || - (error = process_trimming(email, &p->email, email + strlen(email), 1)) < 0) + if (process_trimming(name, &p->name, name + strlen(name), 1) < 0 || + process_trimming(email, &p->email, email + strlen(email), 1) < 0) { git_signature_free(p); - return error; + return -1; + } + + if (contains_angle_brackets(p->email) || + contains_angle_brackets(p->name)) + { + git_signature_free(p); + return signature_error("Neither `name` nor `email` should contain angle brackets chars."); } p->when.time = time; diff --git a/src/strmap.h b/src/strmap.h index da5ca0dba..9972039a0 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -19,7 +19,7 @@ __KHASH_TYPE(str, const char *, void *); typedef khash_t(str) git_strmap; #define GIT__USE_STRMAP \ - __KHASH_IMPL(str, static inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) + __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) #define git_strmap_alloc() kh_init(str) #define git_strmap_free(h) kh_destroy(str, h), h = NULL diff --git a/src/submodule.c b/src/submodule.c index 3c07e657d..b8537cb8c 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -31,7 +31,7 @@ static git_cvar_map _sm_ignore_map[] = { {GIT_CVAR_STRING, "none", GIT_SUBMODULE_IGNORE_NONE} }; -static inline khint_t str_hash_no_trailing_slash(const char *s) +static kh_inline khint_t str_hash_no_trailing_slash(const char *s) { khint_t h; @@ -42,7 +42,7 @@ static inline khint_t str_hash_no_trailing_slash(const char *s) return h; } -static inline int str_equal_no_trailing_slash(const char *a, const char *b) +static kh_inline int str_equal_no_trailing_slash(const char *a, const char *b) { size_t alen = a ? strlen(a) : 0; size_t blen = b ? strlen(b) : 0; @@ -55,7 +55,7 @@ static inline int str_equal_no_trailing_slash(const char *a, const char *b) return (alen == blen && strncmp(a, b, alen) == 0); } -__KHASH_IMPL(str, static inline, const char *, void *, 1, str_hash_no_trailing_slash, str_equal_no_trailing_slash); +__KHASH_IMPL(str, static kh_inline, const char *, void *, 1, str_hash_no_trailing_slash, str_equal_no_trailing_slash); static git_submodule *submodule_alloc(const char *name) { diff --git a/src/transports/http.c b/src/transports/http.c index 4139a2fa6..f25d639f3 100644 --- a/src/transports/http.c +++ b/src/transports/http.c @@ -545,6 +545,7 @@ static int http_download_pack(git_transport *transport, git_repository *repo, gi http_parser_settings settings; char buffer[1024]; gitno_buffer buf; + git_buf path = GIT_BUF_INIT; git_indexer_stream *idx = NULL; download_pack_cbdata data; @@ -555,7 +556,10 @@ static int http_download_pack(git_transport *transport, git_repository *repo, gi return -1; } - if (git_indexer_stream_new(&idx, git_repository_path(repo)) < 0) + if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0) + return -1; + + if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0) return -1; /* @@ -600,6 +604,7 @@ static int http_download_pack(git_transport *transport, git_repository *repo, gi on_error: git_indexer_stream_free(idx); + git_buf_free(&path); return -1; } diff --git a/src/tree.c b/src/tree.c index 9bdc2180c..31a581cdb 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,38 @@ static int tree_key_search(git_vector *entries, const char *filename) return GIT_ENOTFOUND; } +void git_tree_entry_free(git_tree_entry *entry) +{ + if (entry == NULL) + return; + + 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 +219,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 +286,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 +345,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 +531,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 +549,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 +582,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 +671,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 +687,78 @@ void git_treebuilder_free(git_treebuilder *bld) git__free(bld); } -static int tree_frompath( - git_tree **parent_out, - git_tree *root, - git_buf *treeentry_path, - size_t offset) +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, + 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; - } + /* Find how long is the current path component (i.e. + * the filename between two slashes */ + filename_len = subpath_len(path); - slash_pos = (char *)strchr(treeentry_path->ptr + offset, '/'); - - 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; - } - - *slash_pos = '\0'; - - entry = git_tree_entry_byname(root, treeentry_path->ptr + offset); - - if (slash_pos != NULL) - *slash_pos = '/'; - - if (entry == NULL) { - giterr_set(GITERR_TREE, - "No tree entry can be found from " - "the given tree and relative path '%s'.", treeentry_path->ptr); + if (filename_len == 0) { + giterr_set(GITERR_TREE, "Invalid tree path given"); return GIT_ENOTFOUND; } + entry = entry_fromname(root, path, filename_len); + + if (entry == NULL) { + giterr_set(GITERR_TREE, + "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 GIT_ENOTFOUND; + } + + /* 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/src/unix/map.c b/src/unix/map.c index 772f4e247..9dcae5845 100644 --- a/src/unix/map.c +++ b/src/unix/map.c @@ -33,6 +33,7 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs mflag = MAP_PRIVATE; out->data = mmap(NULL, len, mprot, mflag, fd, offset); + if (!out->data || out->data == MAP_FAILED) { giterr_set(GITERR_OS, "Failed to mmap. Could not write data"); return -1; @@ -47,6 +48,7 @@ int p_munmap(git_map *map) { assert(map != NULL); munmap(map->data, map->len); + return 0; } diff --git a/src/unix/posix.h b/src/unix/posix.h index 304dd1419..7a3a388ec 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -7,7 +7,7 @@ #ifndef INCLUDE_posix__w32_h__ #define INCLUDE_posix__w32_h__ -#ifndef __sun +#if !defined(__sun) && !defined(__amigaos4__) # include # define p_fnmatch(p, s, f) fnmatch(p, s, f) #else diff --git a/src/util.h b/src/util.h index eed2bc80c..a84dcab1e 100644 --- a/src/util.h +++ b/src/util.h @@ -204,16 +204,14 @@ GIT_INLINE(bool) git__isalpha(int c) return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); } -GIT_INLINE(bool) git__isspace(int c) +GIT_INLINE(bool) git__isdigit(int c) { - return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v'); + return (c >= '0' && c <= '9'); } -GIT_INLINE(int) git__time_cmp(const git_time *a, const git_time *b) +GIT_INLINE(bool) git__isspace(int c) { - /* Adjust for time zones. Times are in seconds, offsets are in minutes. */ - git_time_t adjusted_a = a->time + ((b->offset - a->offset) * 60); - return (int)(adjusted_a - b->time); + return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v' || c == 0x85 /* Unicode CR+LF */); } GIT_INLINE(bool) git__iswildcard(int c) diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 62fbd1143..c0d66c7ff 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -238,7 +238,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/commit/parent.c b/tests-clar/commit/parent.c new file mode 100644 index 000000000..a00757732 --- /dev/null +++ b/tests-clar/commit/parent.c @@ -0,0 +1,57 @@ +#include "clar_libgit2.h" + +static git_repository *_repo; +static git_commit *commit; + +void test_commit_parent__initialize(void) +{ + git_oid oid; + + cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); + + git_oid_fromstr(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); +} + +void test_commit_parent__cleanup(void) +{ + git_commit_free(commit); + git_repository_free(_repo); +} + +static void assert_nth_gen_parent(unsigned int gen, const char *expected_oid) +{ + git_commit *parent = NULL; + int error; + + error = git_commit_nth_gen_ancestor(&parent, commit, gen); + + if (expected_oid != NULL) { + cl_assert_equal_i(0, error); + cl_assert_equal_i(0, git_oid_streq(git_commit_id(parent), expected_oid)); + } else + cl_assert_equal_i(GIT_ENOTFOUND, error); + + git_commit_free(parent); +} + +/* + * $ git show be35~0 + * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644 + * + * $ git show be35~1 + * commit 9fd738e8f7967c078dceed8190330fc8648ee56a + * + * $ git show be35~3 + * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644 + * + * $ git show be35~42 + * fatal: ambiguous argument 'be35~42': unknown revision or path not in the working tree. + */ +void test_commit_parent__can_retrieve_nth_generation_parent(void) +{ + assert_nth_gen_parent(0, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + assert_nth_gen_parent(1, "9fd738e8f7967c078dceed8190330fc8648ee56a"); + assert_nth_gen_parent(3, "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + assert_nth_gen_parent(42, NULL); +} diff --git a/tests-clar/commit/signature.c b/tests-clar/commit/signature.c index 290b11fa3..9364efb10 100644 --- a/tests-clar/commit/signature.c +++ b/tests-clar/commit/signature.c @@ -13,17 +13,39 @@ static int try_build_signature(const char *name, const char *email, git_time_t t return error; } - -void test_commit_signature__create_trim(void) +static void assert_name_and_email( + const char *expected_name, + const char *expected_email, + const char *name, + const char *email) { - // creating a signature trims leading and trailing spaces - git_signature *sign; - cl_git_pass(git_signature_new(&sign, " nulltoken ", " emeric.fermas@gmail.com ", 1234567890, 60)); - cl_assert(strcmp(sign->name, "nulltoken") == 0); - cl_assert(strcmp(sign->email, "emeric.fermas@gmail.com") == 0); - git_signature_free((git_signature *)sign); + git_signature *sign; + + cl_git_pass(git_signature_new(&sign, name, email, 1234567890, 60)); + cl_assert_equal_s(expected_name, sign->name); + cl_assert_equal_s(expected_email, sign->email); + + git_signature_free(sign); } +void test_commit_signature__leading_and_trailing_spaces_are_trimmed(void) +{ + assert_name_and_email("nulltoken", "emeric.fermas@gmail.com", " nulltoken ", " emeric.fermas@gmail.com "); +} + +void test_commit_signature__angle_brackets_in_names_are_not_supported(void) +{ + cl_git_fail(try_build_signature("Haack", "phil@haack", 1234567890, 60)); + cl_git_fail(try_build_signature("", "phil@haack", 1234567890, 60)); +} + +void test_commit_signature__angle_brackets_in_email_are_not_supported(void) +{ + cl_git_fail(try_build_signature("Phil Haack", ">phil@haack", 1234567890, 60)); + cl_git_fail(try_build_signature("Phil Haack", "phil@>haack", 1234567890, 60)); + cl_git_fail(try_build_signature("Phil Haack", "", 1234567890, 60)); +} void test_commit_signature__create_empties(void) { @@ -39,21 +61,13 @@ void test_commit_signature__create_empties(void) void test_commit_signature__create_one_char(void) { // creating a one character signature - git_signature *sign; - cl_git_pass(git_signature_new(&sign, "x", "foo@bar.baz", 1234567890, 60)); - cl_assert(strcmp(sign->name, "x") == 0); - cl_assert(strcmp(sign->email, "foo@bar.baz") == 0); - git_signature_free((git_signature *)sign); + assert_name_and_email("x", "foo@bar.baz", "x", "foo@bar.baz"); } void test_commit_signature__create_two_char(void) { // creating a two character signature - git_signature *sign; - cl_git_pass(git_signature_new(&sign, "xx", "x@y.z", 1234567890, 60)); - cl_assert(strcmp(sign->name, "xx") == 0); - cl_assert(strcmp(sign->email, "x@y.z") == 0); - git_signature_free((git_signature *)sign); + assert_name_and_email("xx", "foo@bar.baz", "xx", "foo@bar.baz"); } void test_commit_signature__create_zero_char(void) diff --git a/tests-clar/config/read.c b/tests-clar/config/read.c index f33bdd89e..a8504da02 100644 --- a/tests-clar/config/read.c +++ b/tests-clar/config/read.c @@ -191,6 +191,81 @@ void test_config_read__escaping_quotes(void) git_config_free(cfg); } +static int count_cfg_entries( + const char *var_name, const char *value, void *payload) +{ + int *count = payload; + GIT_UNUSED(var_name); + GIT_UNUSED(value); + (*count)++; + return 0; +} + +static int cfg_callback_countdown( + const char *var_name, const char *value, void *payload) +{ + int *count = payload; + GIT_UNUSED(var_name); + GIT_UNUSED(value); + (*count)--; + if (*count == 0) + return -100; + return 0; +} + +void test_config_read__foreach(void) +{ + git_config *cfg; + int count, ret; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); + + count = 0; + cl_git_pass(git_config_foreach(cfg, count_cfg_entries, &count)); + cl_assert_equal_i(5, count); + + count = 3; + cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count)); + cl_assert_equal_i(-100, ret); + + git_config_free(cfg); +} + +void test_config_read__foreach_match(void) +{ + git_config *cfg; + int count; + + cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); + + count = 0; + cl_git_pass( + git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count)); + cl_assert_equal_i(3, count); + + count = 0; + cl_git_pass( + git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count)); + cl_assert_equal_i(2, count); + + count = 0; + cl_git_pass( + git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count)); + cl_assert_equal_i(2, count); + + count = 0; + cl_git_pass( + git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count)); + cl_assert_equal_i(2, count); + + count = 0; + cl_git_pass( + git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count)); + cl_assert_equal_i(0, count); + + git_config_free(cfg); +} + #if 0 BEGIN_TEST(config10, "a repo's config overrides the global config") diff --git a/tests-clar/core/buffer.c b/tests-clar/core/buffer.c index 6a718f459..21aaaed7e 100644 --- a/tests-clar/core/buffer.c +++ b/tests-clar/core/buffer.c @@ -611,3 +611,50 @@ void test_core_buffer__11(void) git_buf_free(&a); } + +void test_core_buffer__rfind_variants(void) +{ + git_buf a = GIT_BUF_INIT; + ssize_t len; + + cl_git_pass(git_buf_sets(&a, "/this/is/it/")); + + len = (ssize_t)git_buf_len(&a); + + cl_assert(git_buf_rfind(&a, '/') == len - 1); + cl_assert(git_buf_rfind_next(&a, '/') == len - 4); + + cl_assert(git_buf_rfind(&a, 'i') == len - 3); + cl_assert(git_buf_rfind_next(&a, 'i') == len - 3); + + cl_assert(git_buf_rfind(&a, 'h') == 2); + cl_assert(git_buf_rfind_next(&a, 'h') == 2); + + cl_assert(git_buf_rfind(&a, 'q') == -1); + cl_assert(git_buf_rfind_next(&a, 'q') == -1); + + git_buf_free(&a); +} + +void test_core_buffer__puts_escaped(void) +{ + git_buf a = GIT_BUF_INIT; + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "", "")); + cl_assert_equal_s("this is a test", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "t", "\\")); + cl_assert_equal_s("\\this is a \\tes\\t", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escaped(&a, "this is a test", "i ", "__")); + cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr); + + git_buf_clear(&a); + cl_git_pass(git_buf_puts_escape_regex(&a, "^match\\s*[A-Z]+.*")); + cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr); + + git_buf_free(&a); +} diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c index d826612ac..864393b70 100644 --- a/tests-clar/core/path.c +++ b/tests-clar/core/path.c @@ -418,3 +418,54 @@ void test_core_path__13_cannot_prettify_a_non_existing_file(void) git_buf_free(&p); } + +void test_core_path__14_apply_relative(void) +{ + git_buf p = GIT_BUF_INIT; + + cl_git_pass(git_buf_sets(&p, "/this/is/a/base")); + + cl_git_pass(git_path_apply_relative(&p, "../test")); + cl_assert_equal_s("/this/is/a/test", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../the/./end")); + cl_assert_equal_s("/this/is/the/end", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "./of/this/../the/string")); + cl_assert_equal_s("/this/is/the/end/of/the/string", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../../..")); + cl_assert_equal_s("/this/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../../")); + cl_assert_equal_s("/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../..")); + cl_assert_equal_s("/", p.ptr); + + + cl_git_pass(git_buf_sets(&p, "d:/another/test")); + + cl_git_pass(git_path_apply_relative(&p, "../../../../..")); + cl_assert_equal_s("d:/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "from/here/to/../and/./back/.")); + cl_assert_equal_s("d:/from/here/and/back/", p.ptr); + + + cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git")); + + cl_git_pass(git_path_apply_relative(&p, "../another.git")); + cl_assert_equal_s("https://my.url.com/another.git", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../full/path/url.patch")); + cl_assert_equal_s("https://my.url.com/full/path/url.patch", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "..")); + cl_assert_equal_s("https://my.url.com/full/path/", p.ptr); + + cl_git_pass(git_path_apply_relative(&p, "../../../../../")); + cl_assert_equal_s("https://", p.ptr); + + git_buf_free(&p); +} 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/network/remotelocal.c b/tests-clar/network/remotelocal.c index 41922975e..5e20b4240 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, 21); + cl_assert_equal_i(how_many_refs, 22); } 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, 21); + cl_assert_equal_i(how_many_refs, 22); 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 5f7f5a9c3..e1387782e 100644 --- a/tests-clar/notes/notes.c +++ b/tests-clar/notes/notes.c @@ -23,6 +23,8 @@ static void assert_note_equal(git_note *note, char *message, git_oid *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) diff --git a/tests-clar/object/tree/frompath.c b/tests-clar/object/tree/frompath.c index 06c69ac08..fd425517c 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_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "i-do-not-exist.txt")); + cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "README/")); + cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/de/fgh/i-do-not-exist.txt")); + cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "nope/de/fgh/1.txt")); + cl_assert_equal_i(GIT_ENOTFOUND, git_tree_entry_bypath(&e, tree, "ab/me-neither/fgh/2.txt")); + cl_assert_equal_i(GIT_ENOTFOUND, 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/odb/foreach.c b/tests-clar/odb/foreach.c new file mode 100644 index 000000000..525c70c09 --- /dev/null +++ b/tests-clar/odb/foreach.c @@ -0,0 +1,36 @@ +#include "clar_libgit2.h" +#include "odb.h" +#include "git2/odb_backend.h" +#include "pack.h" + +static git_odb *_odb; +static git_repository *_repo; +static int nobj; + +void test_odb_foreach__initialize(void) +{ + cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); + git_repository_odb(&_odb, _repo); +} + +void test_odb_foreach__cleanup(void) +{ + git_odb_free(_odb); + git_repository_free(_repo); +} + +static int foreach_cb(git_oid *oid, void *data) +{ + GIT_UNUSED(data); + GIT_UNUSED(oid); + + nobj++; + + return 0; +} + +void test_odb_foreach__foreach(void) +{ + cl_git_pass(git_odb_foreach(_odb, foreach_cb, NULL)); + cl_assert(nobj == 1683); +} diff --git a/tests-clar/refs/branches/foreach.c b/tests-clar/refs/branches/foreach.c new file mode 100644 index 000000000..b6e973799 --- /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, 10); +} + +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, 8); +} + +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/foreachglob.c b/tests-clar/refs/foreachglob.c new file mode 100644 index 000000000..d1412a94b --- /dev/null +++ b/tests-clar/refs/foreachglob.c @@ -0,0 +1,70 @@ +#include "clar_libgit2.h" +#include "refs.h" + +static git_repository *repo; +static git_reference *fake_remote; + +void test_refs_foreachglob__initialize(void) +{ + git_oid id; + + cl_fixture_sandbox("testrepo.git"); + cl_git_pass(git_repository_open(&repo, "testrepo.git")); + + cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644")); + cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0)); +} + +void test_refs_foreachglob__cleanup(void) +{ + git_reference_free(fake_remote); + git_repository_free(repo); + + cl_fixture_cleanup("testrepo.git"); +} + +static int count_cb(const char *reference_name, void *payload) +{ + int *count = (int *)payload; + + GIT_UNUSED(reference_name); + + (*count)++; + + return 0; +} + +static void assert_retrieval(const char *glob, unsigned int flags, int expected_count) +{ + int count = 0; + + cl_git_pass(git_reference_foreach_glob(repo, glob, flags, count_cb, &count)); + + cl_assert_equal_i(expected_count, count); +} + +void test_refs_foreachglob__retrieve_all_refs(void) +{ + /* 7 heads (including one packed head) + 1 note + 2 remotes + 6 tags */ + assert_retrieval("*", GIT_REF_LISTALL, 17); +} + +void test_refs_foreachglob__retrieve_remote_branches(void) +{ + assert_retrieval("refs/remotes/*", GIT_REF_LISTALL, 2); +} + +void test_refs_foreachglob__retrieve_local_branches(void) +{ + assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 8); +} + +void test_refs_foreachglob__retrieve_partially_named_references(void) +{ + /* + * refs/heads/packed-test, refs/heads/test + * refs/remotes/test/master, refs/tags/test + */ + + assert_retrieval("*test*", GIT_REF_LISTALL, 4); +} diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c index d7111b232..ce4eefeba 100644 --- a/tests-clar/refs/read.c +++ b/tests-clar/refs/read.c @@ -16,12 +16,12 @@ static git_repository *g_repo; void test_refs_read__initialize(void) { - g_repo = cl_git_sandbox_init("testrepo"); + cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); } void test_refs_read__cleanup(void) { - cl_git_sandbox_cleanup(); + git_repository_free(g_repo); } void test_refs_read__loose_tag(void) @@ -192,3 +192,13 @@ void test_refs_read__loose_first(void) git_reference_free(reference); } + +void test_refs_read__unfound_return_ENOTFOUND(void) +{ + git_reference *reference; + + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "test/master")); + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/test/master")); + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/tags/test/master")); + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&reference, g_repo, "refs/tags/test/farther/master")); +} diff --git a/tests-clar/refs/reflog.c b/tests-clar/refs/reflog.c index 1bc51b2b8..05f3786bb 100644 --- a/tests-clar/refs/reflog.c +++ b/tests-clar/refs/reflog.c @@ -26,7 +26,7 @@ static void assert_signature(git_signature *expected, git_signature *actual) // Fixture setup and teardown void test_refs_reflog__initialize(void) { - g_repo = cl_git_sandbox_init("testrepo"); + g_repo = cl_git_sandbox_init("testrepo.git"); } void test_refs_reflog__cleanup(void) @@ -61,7 +61,7 @@ void test_refs_reflog__write_then_read(void) cl_git_pass(git_reflog_write(ref, &oid, committer, commit_msg)); /* Reopen a new instance of the repository */ - cl_git_pass(git_repository_open(&repo2, "testrepo")); + cl_git_pass(git_repository_open(&repo2, "testrepo.git")); /* Lookup the preivously created branch */ cl_git_pass(git_reference_lookup(&lookedup_ref, repo2, new_ref)); @@ -121,3 +121,44 @@ void test_refs_reflog__dont_write_bad(void) git_reference_free(ref); } + +void test_refs_reflog__renaming_the_reference_moves_the_reflog(void) +{ + git_reference *master; + git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT; + + git_buf_joinpath(&master_log_path, git_repository_path(g_repo), GIT_REFLOG_DIR); + git_buf_puts(&moved_log_path, git_buf_cstr(&master_log_path)); + git_buf_joinpath(&master_log_path, git_buf_cstr(&master_log_path), "refs/heads/master"); + git_buf_joinpath(&moved_log_path, git_buf_cstr(&moved_log_path), "refs/moved"); + + cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&master_log_path))); + cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&moved_log_path))); + + cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); + cl_git_pass(git_reference_rename(master, "refs/moved", 0)); + + cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&master_log_path))); + cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&moved_log_path))); + + git_reference_free(master); + git_buf_free(&moved_log_path); + git_buf_free(&master_log_path); +} +static void assert_has_reflog(bool expected_result, const char *name) +{ + git_reference *ref; + + cl_git_pass(git_reference_lookup(&ref, g_repo, name)); + + cl_assert_equal_i(expected_result, git_reference_has_log(ref)); + + git_reference_free(ref); +} + +void test_refs_reflog__reference_has_reflog(void) +{ + assert_has_reflog(true, "HEAD"); + assert_has_reflog(true, "refs/heads/master"); + assert_has_reflog(false, "refs/heads/subtrees"); +} diff --git a/tests-clar/refs/remotetracking.c b/tests-clar/refs/remotetracking.c new file mode 100644 index 000000000..c4ec588ee --- /dev/null +++ b/tests-clar/refs/remotetracking.c @@ -0,0 +1,49 @@ +#include "clar_libgit2.h" + +static git_repository *g_repo; + +void test_refs_remotetracking__initialize(void) +{ + cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); +} + +void test_refs_remotetracking__cleanup(void) +{ + git_repository_free(g_repo); +} + +void test_refs_remotetracking__unfound_returns_GIT_ENOTFOUND(void) +{ + git_reference *branch, *tracking; + + cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/heads/subtrees")); + + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_remote_tracking_from_branch(&tracking, branch)); + + git_reference_free(branch); +} + +void test_refs_remotetracking__retrieving_from_a_non_head_fails(void) +{ + git_reference *branch, *tracking; + + cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/tags/e90810b")); + + cl_git_fail(git_reference_remote_tracking_from_branch(&tracking, branch)); + + git_reference_free(branch); +} + +void test_refs_remotetracking__can_retrieve_a_remote_tracking_branch_reference(void) +{ + git_reference *branch, *tracking; + + cl_git_pass(git_reference_lookup(&branch, g_repo, "refs/heads/master")); + + cl_git_pass(git_reference_remote_tracking_from_branch(&tracking, branch)); + + cl_assert_equal_s("refs/remotes/test/master", git_reference_name(tracking)); + + git_reference_free(branch); + git_reference_free(tracking); +} diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c index 2ee72f206..56d57b21c 100644 --- a/tests-clar/refs/revparse.c +++ b/tests-clar/refs/revparse.c @@ -1,6 +1,9 @@ #include "clar_libgit2.h" #include "git2/revparse.h" +#include "buffer.h" +#include "refs.h" +#include "path.h" static git_repository *g_repo; static git_object *g_obj; @@ -9,170 +12,394 @@ static char g_orig_tz[16] = {0}; /* Helpers */ -static void test_object(const char *spec, const char *expected_oid) +static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo) { - char objstr[64] = {0}; + char objstr[64] = {0}; + git_object *obj = NULL; + int error; - cl_git_pass(git_revparse_single(&g_obj, g_repo, spec)); - git_oid_fmt(objstr, git_object_id(g_obj)); - cl_assert_equal_s(objstr, expected_oid); + error = git_revparse_single(&obj, repo, spec); - git_object_free(g_obj); - g_obj = NULL; + if (expected_oid != NULL) { + cl_assert_equal_i(0, error); + git_oid_fmt(objstr, git_object_id(obj)); + cl_assert_equal_s(objstr, expected_oid); + } else + cl_assert_equal_i(GIT_ENOTFOUND, error); + + git_object_free(obj); } +static void test_object(const char *spec, const char *expected_oid) +{ + test_object_inrepo(spec, expected_oid, g_repo); +} void test_refs_revparse__initialize(void) { - char *tz = cl_getenv("TZ"); - if (tz) - strcpy(g_orig_tz, tz); - cl_setenv("TZ", "UTC"); - g_repo = cl_git_sandbox_init("testrepo.git"); + char *tz = cl_getenv("TZ"); + if (tz) + strcpy(g_orig_tz, tz); + cl_setenv("TZ", "UTC"); + + cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); } void test_refs_revparse__cleanup(void) { - cl_git_sandbox_cleanup(); - g_obj = NULL; - cl_setenv("TZ", g_orig_tz); + git_repository_free(g_repo); + g_obj = NULL; + cl_setenv("TZ", g_orig_tz); } - void test_refs_revparse__nonexistant_object(void) { - cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist")); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist^1")); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist~2")); + test_object("this-does-not-exist", NULL); + test_object("this-does-not-exist^1", NULL); + test_object("this-does-not-exist~2", NULL); +} + +void test_refs_revparse__invalid_reference_name(void) +{ + cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't make sense")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't make sense^1")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't make sense~2")); } void test_refs_revparse__shas(void) { - test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); } void test_refs_revparse__head(void) { - test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__full_refs(void) { - test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d"); - test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d"); + test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); } void test_refs_revparse__partial_refs(void) { - test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08"); - test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08"); + test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f"); } void test_refs_revparse__describe_output(void) { - test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__nth_parent(void) { - test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); - test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"); + test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + + test_object("be3563a^42", NULL); } void test_refs_revparse__not_tag(void) { - test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); - test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); + test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); } void test_refs_revparse__to_type(void) { - test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); - test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); + test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); + test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08"); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}")); } 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")); + 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"); - test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); } void test_refs_revparse__chaining(void) { - test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); - test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479"); + test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479"); } -void test_refs_revparse__reflog(void) +void test_refs_revparse__upstream(void) { - cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}")); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}")); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{1000}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "e90810b@{u}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "refs/tags/e90810b@{u}")); - test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); - test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("refs/heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); +} + +void test_refs_revparse__ordinal(void) +{ + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{-2}")); + + test_object("nope@{0}", NULL); + test_object("master@{31415}", NULL); + test_object("@{1000}", NULL); + test_object("@{2}", NULL); + + test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + + test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("refs/heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); +} + +void test_refs_revparse__previous_head(void) +{ + cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}")); + + test_object("@{-42}", NULL); + + test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); +} + +static void create_fake_stash_reference_and_reflog(git_repository *repo) +{ + git_reference *master; + git_buf log_path = GIT_BUF_INIT; + + git_buf_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash"); + + cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&log_path))); + + cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master")); + cl_git_pass(git_reference_rename(master, "refs/fakestash", 0)); + + cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path))); + + git_buf_free(&log_path); + git_reference_free(master); +} + +void test_refs_revparse__reflog_of_a_ref_under_refs(void) +{ + git_repository *repo = cl_git_sandbox_init("testrepo.git"); + + test_object_inrepo("refs/fakestash", NULL, repo); + + create_fake_stash_reference_and_reflog(repo); + + /* + * $ git reflog -1 refs/fakestash + * a65fedf refs/fakestash@{0}: commit: checking in + * + * $ git reflog -1 refs/fakestash@{0} + * a65fedf refs/fakestash@{0}: commit: checking in + * + * $ git reflog -1 fakestash + * a65fedf fakestash@{0}: commit: checking in + * + * $ git reflog -1 fakestash@{0} + * a65fedf fakestash@{0}: commit: checking in + */ + test_object_inrepo("refs/fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); + test_object_inrepo("refs/fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); + test_object_inrepo("fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); + test_object_inrepo("fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo); + + cl_git_sandbox_cleanup(); } void test_refs_revparse__revwalk(void) { - cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}")); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}")); - cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}")); - test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); - test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); - test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a"); + test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644"); + test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a"); } void test_refs_revparse__date(void) { - test_object("HEAD@{10 years ago}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master@{2012-4-30 10:23:20 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); - test_object("master@{2012-4-30 18:24 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); - test_object("master@{2012-4-30 23:24 -0300}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + /* + * $ git reflog HEAD --date=iso + * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master + * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in + * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2 + * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit: + * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes + * + * $ git reflog HEAD --date=raw + * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master + * a4a7dce HEAD@{1335806617 -0900}: commit: checking in + * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2 + * a65fedf HEAD@{1335806603 -0900}: commit: + * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour + */ + test_object("HEAD@{10 years ago}", NULL); - /* Core git gives a65fedf, because they don't take time zones into account. */ - test_object("master@{1335806640}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + + /* + * $ git reflog master --date=iso + * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in + * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src... + * + * $ git reflog master --date=raw + * a65fedf master@{1335806603 -0800}: commit: checking in + * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso + */ + + + /* + * $ git reflog -1 "master@{2012-04-30 17:22:42 +0000}" + * warning: Log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800. + */ + test_object("master@{2012-04-30 17:22:42 +0000}", NULL); + test_object("master@{2012-04-30 09:22:42 -0800}", NULL); + + /* + * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}" + * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg + */ + test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + + /* + * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}" + * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in + */ + test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + + /* + * $ git reflog -1 master@{2012-05-03} + * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in + */ + test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + + /* + * $ git reflog -1 "master@{1335806603}" + * a65fedf + * + * $ git reflog -1 "master@{1335806602}" + * be3563a + */ + test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"); + test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); } 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:")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/")); + cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); - test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); - test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b"); - test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); - test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); - test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f"); - test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd"); - test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9"); + test_object(":/not found in any commit", NULL); + test_object("subtrees:ab/42.txt", NULL); + test_object("subtrees:ab/4.txt/nope", NULL); + test_object("subtrees:nope", NULL); + test_object("test/master^1:branch_file.txt", NULL); + + /* From tags */ + test_object("test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); + test_object("tags/test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); + test_object("e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); + test_object("tags/e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf"); + + /* From commits */ + test_object("a65f:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); + + /* From trees */ + test_object("a65f^{tree}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); + test_object("944c:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc"); + + /* Retrieving trees */ + test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"); + test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12"); + test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3"); + test_object("subtrees:ab/", "f1425cef211cc08caa31e7b545ffb232acb098c3"); + + /* Retrieving blobs */ + test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f"); + test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b"); + test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6"); + test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"); + test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f"); + test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9"); + test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); +} + +void test_refs_revparse__disambiguation(void) +{ + /* + * $ git show e90810b + * tag e90810b + * Tagger: Vicent Marti + * Date: Thu Aug 12 03:59:17 2010 +0200 + * + * This is a very simple tag. + * + * commit e90810b8df3e80c413d903f631643c716887138d + * Author: Vicent Marti + * Date: Thu Aug 5 18:42:20 2010 +0200 + * + * Test commit 2 + * + * diff --git a/readme.txt b/readme.txt + * index 6336846..0266163 100644 + * --- a/readme.txt + * +++ b/readme.txt + * @@ -1 +1,2 @@ + * Testing a readme.txt + * +Now we add a single line here + * + * $ git show-ref e90810b + * 7b4384978d2493e851f9cca7858815fac9b10980 refs/tags/e90810b + * + */ + test_object("e90810b", "7b4384978d2493e851f9cca7858815fac9b10980"); + + /* + * $ git show e90810 + * commit e90810b8df3e80c413d903f631643c716887138d + * Author: Vicent Marti + * Date: Thu Aug 5 18:42:20 2010 +0200 + * + * Test commit 2 + * + * diff --git a/readme.txt b/readme.txt + * index 6336846..0266163 100644 + * --- a/readme.txt + * +++ b/readme.txt + * @@ -1 +1,2 @@ + * Testing a readme.txt + * +Now we add a single line here + */ + test_object("e90810", "e90810b8df3e80c413d903f631643c716887138d"); } diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c index 1ba355ed6..3d37c3754 100644 --- a/tests-clar/repo/init.c +++ b/tests-clar/repo/init.c @@ -170,10 +170,21 @@ static void assert_config_entry_on_init_bytype(const char *config_key, int expec { git_config *config; int current_value; + git_buf repo_path = GIT_BUF_INIT; cl_set_cleanup(&cleanup_repository, "config_entry"); - - cl_git_pass(git_repository_init(&_repo, "config_entry/test.git", is_bare)); + + cl_git_pass(git_buf_puts(&repo_path, "config_entry/test.")); + + if (!is_bare) + cl_git_pass(git_buf_puts(&repo_path, "non.")); + + cl_git_pass(git_buf_puts(&repo_path, "bare.git")); + + cl_git_pass(git_repository_init(&_repo, git_buf_cstr(&repo_path), is_bare)); + + git_buf_free(&repo_path); + git_repository_config(&config, _repo); if (expected_value >= 0) { @@ -223,16 +234,18 @@ void test_repo_init__reinit_doesnot_overwrite_ignorecase(void) int current_value; /* Init a new repo */ - test_repo_init__detect_ignorecase(); + cl_set_cleanup(&cleanup_repository, "not.overwrite.git"); + cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1)); /* Change the "core.ignorecase" config value to something unlikely */ git_repository_config(&config, _repo); git_config_set_int32(config, "core.ignorecase", 42); git_config_free(config); git_repository_free(_repo); + _repo = NULL; /* Reinit the repository */ - cl_git_pass(git_repository_init(&_repo, "config_entry/test.git", 1)); + cl_git_pass(git_repository_init(&_repo, "not.overwrite.git", 1)); git_repository_config(&config, _repo); /* Ensure the "core.ignorecase" config value hasn't been updated */ @@ -242,6 +255,40 @@ void test_repo_init__reinit_doesnot_overwrite_ignorecase(void) git_config_free(config); } +void test_repo_init__reinit_overwrites_filemode(void) +{ + git_config *config; + int expected, current_value; + +#ifdef GIT_WIN32 + expected = false; +#else + expected = true; +#endif + + /* Init a new repo */ + cl_set_cleanup(&cleanup_repository, "overwrite.git"); + cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1)); + + + /* Change the "core.filemode" config value to something unlikely */ + git_repository_config(&config, _repo); + git_config_set_bool(config, "core.filemode", !expected); + git_config_free(config); + git_repository_free(_repo); + _repo = NULL; + + /* Reinit the repository */ + cl_git_pass(git_repository_init(&_repo, "overwrite.git", 1)); + git_repository_config(&config, _repo); + + /* Ensure the "core.filemode" config value has been reset */ + cl_git_pass(git_config_get_bool(¤t_value, config, "core.filemode")); + cl_assert_equal_i(expected, current_value); + + git_config_free(config); +} + void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void) { assert_config_entry_on_init_bytype("core.logallrefupdates", GIT_ENOTFOUND, true); diff --git a/tests-clar/repo/setters.c b/tests-clar/repo/setters.c index 6242d8541..cd6e389ae 100644 --- a/tests-clar/repo/setters.c +++ b/tests-clar/repo/setters.c @@ -2,6 +2,8 @@ #include "buffer.h" #include "posix.h" #include "util.h" +#include "path.h" +#include "fileops.h" static git_repository *repo; @@ -16,7 +18,7 @@ void test_repo_setters__cleanup(void) { git_repository_free(repo); cl_fixture_cleanup("testrepo.git"); - cl_must_pass(p_rmdir("new_workdir")); + cl_fixture_cleanup("new_workdir"); } void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standard_one(void) @@ -24,7 +26,7 @@ void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standar cl_assert(git_repository_is_bare(repo) == 1); cl_assert(git_repository_workdir(repo) == NULL); - cl_git_pass(git_repository_set_workdir(repo, "./new_workdir")); + cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); cl_assert(git_repository_workdir(repo) != NULL); cl_assert(git_repository_is_bare(repo) == 0); @@ -32,9 +34,30 @@ void test_repo_setters__setting_a_workdir_turns_a_bare_repository_into_a_standar void test_repo_setters__setting_a_workdir_prettifies_its_path(void) { - cl_git_pass(git_repository_set_workdir(repo, "./new_workdir")); + cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", false)); - cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0); + cl_assert(git__suffixcmp(git_repository_workdir(repo), "new_workdir/") == 0); +} + +void test_repo_setters__setting_a_workdir_creates_a_gitlink(void) +{ + git_config *cfg; + const char *val; + git_buf content = GIT_BUF_INIT; + + cl_git_pass(git_repository_set_workdir(repo, "./new_workdir", true)); + + cl_assert(git_path_isfile("./new_workdir/.git")); + + cl_git_pass(git_futils_readbuffer(&content, "./new_workdir/.git")); + cl_assert(git__prefixcmp(git_buf_cstr(&content), "gitdir: ") == 0); + cl_assert(git__suffixcmp(git_buf_cstr(&content), "testrepo.git/") == 0); + git_buf_free(&content); + + cl_git_pass(git_repository_config(&cfg, repo)); + cl_git_pass(git_config_get_string(&val, cfg, "core.worktree")); + cl_assert(git__suffixcmp(val, "new_workdir/") == 0); + git_config_free(cfg); } void test_repo_setters__setting_a_new_index_on_a_repo_which_has_already_loaded_one_properly_honors_the_refcount(void) diff --git a/tests-clar/resources/attr/.gitted/index b/tests-clar/resources/attr/.gitted/index index 1d60eab8f..943e2243e 100644 Binary files a/tests-clar/resources/attr/.gitted/index and b/tests-clar/resources/attr/.gitted/index 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 1328653313 -0800 commit: Some whitespace only changes for testing purposes a97cc019851d401a4f1d091cb91a15890a0dd1ba 217878ab49e1314388ea2e32dc6fdb58a1b969e0 Russell Belfer 1332734901 -0700 commit: added files in sub/sub 217878ab49e1314388ea2e32dc6fdb58a1b969e0 24fa9a9fc4e202313e24b648087495441dab432b Russell Belfer 1332735555 -0700 commit: adding more files in sub for tree status +24fa9a9fc4e202313e24b648087495441dab432b 8d0b9df9bd30be7910ddda60548d485bc302b911 yorah 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 1328653313 -0800 commit: Some whitespace only changes for testing purposes a97cc019851d401a4f1d091cb91a15890a0dd1ba 217878ab49e1314388ea2e32dc6fdb58a1b969e0 Russell Belfer 1332734901 -0700 commit: added files in sub/sub 217878ab49e1314388ea2e32dc6fdb58a1b969e0 24fa9a9fc4e202313e24b648087495441dab432b Russell Belfer 1332735555 -0700 commit: adding more files in sub for tree status +24fa9a9fc4e202313e24b648087495441dab432b 8d0b9df9bd30be7910ddda60548d485bc302b911 yorah 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 Binary files /dev/null and b/tests-clar/resources/attr/.gitted/objects/16/983da6643656bb44c43965ecb6855c6d574512 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 13 */ - cl_assert(i == 13); + /* git log --branches --oneline | wc -l => 14 */ + cl_assert(i == 14); } void test_revwalk_basic__push_head(void) diff --git a/tests-clar/revwalk/signatureparsing.c b/tests-clar/revwalk/signatureparsing.c new file mode 100644 index 000000000..94de1a343 --- /dev/null +++ b/tests-clar/revwalk/signatureparsing.c @@ -0,0 +1,44 @@ +#include "clar_libgit2.h" + +static git_repository *_repo; +static git_revwalk *_walk; + +void test_revwalk_signatureparsing__initialize(void) +{ + cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); + cl_git_pass(git_revwalk_new(&_walk, _repo)); +} + +void test_revwalk_signatureparsing__cleanup(void) +{ + git_revwalk_free(_walk); + git_repository_free(_repo); +} + +void test_revwalk_signatureparsing__do_not_choke_when_name_contains_angle_brackets(void) +{ + git_reference *ref; + git_oid commit_oid; + git_commit *commit; + const git_signature *signature; + + /* + * The branch below points at a commit with angle brackets in the committer/author name + * committer 1323847743 +0100 + */ + cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/haacked")); + + git_revwalk_push(_walk, git_reference_oid(ref)); + cl_git_pass(git_revwalk_next(&commit_oid, _walk)); + + cl_git_pass(git_commit_lookup(&commit, _repo, git_reference_oid(ref))); + + signature = git_commit_committer(commit); + cl_assert_equal_s("Yu V. Bin Haacked", signature->email); + cl_assert_equal_s("", signature->name); + cl_assert_equal_i(1323847743, (int)signature->when.time); + cl_assert_equal_i(60, signature->when.offset); + + git_commit_free(commit); + git_reference_free(ref); +} diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c index 3670b72a8..fed81e570 100644 --- a/tests-clar/status/worktree.c +++ b/tests-clar/status/worktree.c @@ -459,7 +459,7 @@ void test_status_worktree__status_file_without_index_or_workdir(void) cl_git_pass(p_mkdir("wd", 0777)); cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - cl_git_pass(git_repository_set_workdir(repo, "wd")); + cl_git_pass(git_repository_set_workdir(repo, "wd", false)); cl_git_pass(git_index_open(&index, "empty-index")); cl_assert_equal_i(0, git_index_entrycount(index)); @@ -500,7 +500,7 @@ void test_status_worktree__status_file_with_clean_index_and_empty_workdir(void) cl_git_pass(p_mkdir("wd", 0777)); cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); - cl_git_pass(git_repository_set_workdir(repo, "wd")); + cl_git_pass(git_repository_set_workdir(repo, "wd", false)); cl_git_pass(git_index_open(&index, "my-index")); fill_index_wth_head_entries(repo, index);