diff options
110 files changed, 3908 insertions, 597 deletions
@@ -1,5 +1,6 @@ /tests-clar/clar.h /tests-clar/clar_main.c +/tests-clar/clar_main.c.rule /apidocs /trash-*.exe /libgit2.pc @@ -6,6 +6,7 @@ Alexei Sholik Andreas Ericsson Ankur Sethi Ben Noordhuis +Ben Straub Benjamin C Meyer Brian Lopez Carlos Martín Nieto diff --git a/CMakeLists.txt b/CMakeLists.txt index b0972936..6db18269 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,6 +133,12 @@ SET_TARGET_PROPERTIES(git2 PROPERTIES VERSION ${LIBGIT2_VERSION_STRING}) SET_TARGET_PROPERTIES(git2 PROPERTIES SOVERSION ${LIBGIT2_VERSION_MAJOR}) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libgit2.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libgit2.pc @ONLY) +IF (MSVC) + # Precompiled headers + SET_TARGET_PROPERTIES(git2 PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") + SET_SOURCE_FILES_PROPERTIES(src/win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h") +ENDIF () + # Install INSTALL(TARGETS git2 RUNTIME DESTINATION ${INSTALL_BIN} @@ -165,6 +171,12 @@ IF (BUILD_CLAR) ) ADD_EXECUTABLE(libgit2_clar ${SRC} ${CLAR_PATH}/clar_main.c ${SRC_TEST} ${SRC_ZLIB} ${SRC_HTTP} ${SRC_REGEX}) TARGET_LINK_LIBRARIES(libgit2_clar ${CMAKE_THREAD_LIBS_INIT} ${SSL_LIBRARIES}) + + IF (MSVC) + # Precompiled headers + SET_TARGET_PROPERTIES(libgit2_clar PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h") + ENDIF () + IF (WIN32) TARGET_LINK_LIBRARIES(libgit2_clar ws2_32) ELSEIF (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)") diff --git a/examples/diff.c b/examples/diff.c index 1b4ab549..b72a75e1 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -185,9 +185,7 @@ int main(int argc, char *argv[]) /* open repo */ - check(git_repository_discover(path, sizeof(path), dir, 0, "/"), - "Could not discover repository"); - check(git_repository_open(&repo, path), + check(git_repository_open_ext(&repo, dir, 0, NULL), "Could not open repository"); if (treeish1) diff --git a/include/git2.h b/include/git2.h index d7538731..34601449 100644 --- a/include/git2.h +++ b/include/git2.h @@ -25,6 +25,7 @@ #include "git2/merge.h" #include "git2/refs.h" #include "git2/reflog.h" +#include "git2/revparse.h" #include "git2/object.h" #include "git2/blob.h" @@ -43,5 +44,6 @@ #include "git2/indexer.h" #include "git2/submodule.h" #include "git2/notes.h" +#include "git2/reset.h" #endif diff --git a/include/git2/blob.h b/include/git2/blob.h index 55177067..544dc7c4 100644 --- a/include/git2/blob.h +++ b/include/git2/blob.h @@ -115,6 +115,48 @@ GIT_EXTERN(int) git_blob_create_fromfile(git_oid *oid, git_repository *repo, con */ GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path); +/** + * Write a loose blob to the Object Database from a + * provider of chunks of data. + * + * Provided the `hintpath` parameter is filled, its value + * will help to determine what git filters should be applied + * to the object before it can be placed to the object database. + * + * + * The implementation of the callback has to respect the + * following rules: + * + * - `content` will have to be filled by the consumer. The maximum number + * of bytes that the buffer can accept per call is defined by the + * `max_length` parameter. Allocation and freeing of the buffer will be taken + * care of by the function. + * + * - The callback is expected to return the number of bytes + * that `content` have been filled with. + * + * - When there is no more data to stream, the callback should + * return 0. This will prevent it from being invoked anymore. + * + * - When an error occurs, the callback should return -1. + * + * + * @param oid Return the id of the written blob + * + * @param repo repository where the blob will be written. + * This repository can be bare or not. + * + * @param hintpath if not NULL, will help selecting the filters + * to apply onto the content of the blob to be created. + * + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_blob_create_fromchunks( + git_oid *oid, + git_repository *repo, + const char *hintpath, + int (*source_cb)(char *content, size_t max_length, void *payload), + void *payload); /** * Write an in-memory buffer to the ODB as a blob diff --git a/include/git2/diff.h b/include/git2/diff.h index a0e8ad6d..d4d0eac4 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -29,6 +29,10 @@ */ GIT_BEGIN_DECL +/** + * Flags for diff options. A combination of these flags can be passed + * in via the `flags` value in the `git_diff_options`. + */ enum { GIT_DIFF_NORMAL = 0, GIT_DIFF_REVERSE = (1 << 0), @@ -96,9 +100,9 @@ typedef enum { typedef struct { git_oid oid; char *path; - uint16_t mode; git_off_t size; unsigned int flags; + uint16_t mode; } git_diff_file; /** @@ -160,15 +164,16 @@ typedef int (*git_diff_hunk_fn)( * the file or hunk headers. */ enum { - /* these values will be sent to `git_diff_data_fn` along with the line */ + /* These values will be sent to `git_diff_data_fn` along with the line */ GIT_DIFF_LINE_CONTEXT = ' ', GIT_DIFF_LINE_ADDITION = '+', GIT_DIFF_LINE_DELETION = '-', - GIT_DIFF_LINE_ADD_EOFNL = '\n', /**< LF was added at end of file */ + GIT_DIFF_LINE_ADD_EOFNL = '\n', /**< DEPRECATED */ GIT_DIFF_LINE_DEL_EOFNL = '\0', /**< LF was removed at end of file */ - /* these values will only be sent to a `git_diff_data_fn` when the content - * of a diff is being formatted (eg. through git_diff_print_patch() or - * git_diff_print_compact(), for instance). + + /* The following values will only be sent to a `git_diff_data_fn` when + * the content of a diff is being formatted (eg. through + * git_diff_print_patch() or git_diff_print_compact(), for instance). */ GIT_DIFF_LINE_FILE_HDR = 'F', GIT_DIFF_LINE_HUNK_HDR = 'H', @@ -206,6 +211,8 @@ GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff); /** * Compute a difference between two tree objects. * + * This is equivalent to `git diff <treeish> <treeish>` + * * @param repo The repository containing the trees. * @param opts Structure with options to influence diff or NULL for defaults. * @param old_tree A git_tree object to diff from. @@ -222,6 +229,9 @@ GIT_EXTERN(int) git_diff_tree_to_tree( /** * Compute a difference between a tree and the index. * + * This is equivalent to `git diff --cached <treeish>` or if you pass + * the HEAD tree, then like `git diff --cached`. + * * @param repo The repository containing the tree and index. * @param opts Structure with options to influence diff or NULL for defaults. * @param old_tree A git_tree object to diff from. @@ -236,6 +246,11 @@ GIT_EXTERN(int) git_diff_index_to_tree( /** * Compute a difference between the working directory and the index. * + * This matches the `git diff` command. See the note below on + * `git_diff_workdir_to_tree` for a discussion of the difference between + * `git diff` and `git diff HEAD` and how to emulate a `git diff <treeish>` + * using libgit2. + * * @param repo The repository. * @param opts Structure with options to influence diff or NULL for defaults. * @param diff A pointer to a git_diff_list pointer that will be allocated. @@ -248,14 +263,24 @@ GIT_EXTERN(int) git_diff_workdir_to_index( /** * Compute a difference between the working directory and a tree. * - * This returns strictly the differences between the tree and the - * files contained in the working directory, regardless of the state - * of files in the index. There is no direct equivalent in C git. + * This is *NOT* the same as `git diff <treeish>`. Running `git diff HEAD` + * or the like actually uses information from the index, along with the tree + * and workdir dir info. + * + * This function returns strictly the differences between the tree and the + * files contained in the working directory, regardless of the state of + * files in the index. It may come as a surprise, but there is no direct + * equivalent in core git. + * + * To emulate `git diff <treeish>`, you should call both + * `git_diff_index_to_tree` and `git_diff_workdir_to_index`, then call + * `git_diff_merge` on the results. That will yield a `git_diff_list` that + * matches the git output. * - * This is *NOT* the same as 'git diff HEAD' or 'git diff <SHA>'. Those - * commands diff the tree, the index, and the workdir. To emulate those - * functions, call `git_diff_index_to_tree` and `git_diff_workdir_to_index`, - * then call `git_diff_merge` on the results. + * If this seems confusing, take the case of a file with a staged deletion + * where the file has then been put back into the working dir and modified. + * The tree-to-workdir diff for that file is 'modified', but core git would + * show status 'deleted' since there is a pending deletion in the index. * * @param repo The repository containing the tree. * @param opts Structure with options to influence diff or NULL for defaults. @@ -298,10 +323,23 @@ GIT_EXTERN(int) git_diff_merge( /** * Iterate over a diff list issuing callbacks. * - * If the hunk and/or line callbacks are not NULL, then this will calculate - * text diffs for all files it thinks are not binary. If those are both - * NULL, then this will not bother with the text diffs, so it can be - * efficient. + * This will iterate through all of the files described in a diff. You + * should provide a file callback to learn about each file. + * + * The "hunk" and "line" callbacks are optional, and the text diff of the + * files will only be calculated if they are not NULL. Of course, these + * callbacks will not be invoked for binary files on the diff list or for + * files whose only changed is a file mode change. + * + * @param diff A git_diff_list generated by one of the above functions. + * @param cb_data Reference pointer that will be passed to your callbacks. + * @param file_cb Callback function to make per file in the diff. + * @param hunk_cb Optional callback to make per hunk of text diff. This + * callback is called to describe a range of lines in the + * diff. It will not be issued for binary files. + * @param line_cb Optional callback to make per line of diff text. This + * same callback will be made for context lines, added, and + * removed lines, and even for a deleted trailing newline. */ GIT_EXTERN(int) git_diff_foreach( git_diff_list *diff, @@ -322,6 +360,14 @@ GIT_EXTERN(int) git_diff_print_compact( * Iterate over a diff generating text output like "git diff". * * This is a super easy way to generate a patch from a diff. + * + * @param diff A git_diff_list generated by one of the above functions. + * @param cb_data Reference pointer that will be passed to your callbacks. + * @param print_cb Callback function to output lines of the diff. This + * same function will be called for file headers, hunk + * headers, and diff lines. Fortunately, you can probably + * use various GIT_DIFF_LINE constants to determine what + * text you are given. */ GIT_EXTERN(int) git_diff_print_patch( git_diff_list *diff, @@ -338,13 +384,14 @@ GIT_EXTERN(int) git_diff_print_patch( /** * Directly run a text diff on two blobs. * - * Compared to a file, a blob lacks some contextual information. As such, the - * `git_diff_file` parameters of the callbacks will be filled accordingly to the following: - * `mode` will be set to 0, `path` will be set to NULL. When dealing with a NULL blob, `oid` - * will be set to 0. + * Compared to a file, a blob lacks some contextual information. As such, + * the `git_diff_file` parameters of the callbacks will be filled + * accordingly to the following: `mode` will be set to 0, `path` will be set + * to NULL. When dealing with a NULL blob, `oid` will be set to 0. * - * When at least one of the blobs being dealt with is binary, the `git_diff_delta` binary - * attribute will be set to 1 and no call to the hunk_cb nor line_cb will be made. + * When at least one of the blobs being dealt with is binary, the + * `git_diff_delta` binary attribute will be set to 1 and no call to the + * hunk_cb nor line_cb will be made. */ GIT_EXTERN(int) git_diff_blobs( git_blob *old_blob, diff --git a/include/git2/refspec.h b/include/git2/refspec.h index 1100e902..9e84aad9 100644 --- a/include/git2/refspec.h +++ b/include/git2/refspec.h @@ -20,6 +20,14 @@ GIT_BEGIN_DECL /** + * Parse a refspec string and create a refspec object + * + * @param refspec pointer to the refspec structure to be used + * @param str the refspec as a string + */ +GIT_EXTERN(int) git_refspec_parse(git_refspec *refspec, const char *str); + +/** * Get the source specifier * * @param refspec the refspec diff --git a/include/git2/reset.h b/include/git2/reset.h new file mode 100644 index 00000000..12517874 --- /dev/null +++ b/include/git2/reset.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_reset_h__ +#define INCLUDE_git_reset_h__ + +/** + * @file git2/reset.h + * @brief Git reset management routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Sets the current head to the specified commit oid and optionally + * resets the index and working tree to match. + * + * When specifying a Soft kind of reset, the head will be moved to the commit. + * + * Specifying a Mixed kind of reset will trigger a Soft reset and the index will + * be replaced with the content of the commit tree. + * + * TODO: Implement remaining kinds of resets. + * + * @param repo Repository where to perform the reset operation. + * + * @param target Object to which the Head should be moved to. This object + * must belong to the given `repo` and can either be a git_commit or a + * git_tag. When a git_tag is being passed, it should be dereferencable + * to a git_commit which oid will be used as the target of the branch. + * + * @param reset_type Kind of reset operation to perform. + * + * @return GIT_SUCCESS or an error code + */ +GIT_EXTERN(int) git_reset(git_repository *repo, const git_object *target, git_reset_type reset_type); + +/** @} */ +GIT_END_DECL +#endif diff --git a/include/git2/revparse.h b/include/git2/revparse.h new file mode 100644 index 00000000..4567027e --- /dev/null +++ b/include/git2/revparse.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_revparse_h__ +#define INCLUDE_git_revparse_h__ + +#include "common.h" +#include "types.h" + + +/** + * @file git2/revparse.h + * @brief Git revision parsing routines + * @defgroup git_revparse Git revision parsing routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Find an object, as specified by a revision string. See `man gitrevisions`, or the documentation + * for `git rev-parse` for information on the syntax accepted. + * + * @param out pointer to output object + * @param repo the repository to search in + * @param spec the textual specification for an object + * @return on success, GIT_ERROR otherwise (use git_error_last for information about the error) + */ +GIT_EXTERN(int) git_revparse_single(git_object **out, git_repository *repo, const char *spec); + +/** @} */ +GIT_END_DECL +#endif diff --git a/include/git2/status.h b/include/git2/status.h index 6a424dfd..69b6e47e 100644 --- a/include/git2/status.h +++ b/include/git2/status.h @@ -102,7 +102,7 @@ enum { GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1 << 0), GIT_STATUS_OPT_INCLUDE_IGNORED = (1 << 1), GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1 << 2), - GIT_STATUS_OPT_EXCLUDE_SUBMODULED = (1 << 3), + GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1 << 3), GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1 << 4), }; diff --git a/include/git2/types.h b/include/git2/types.h index cfb0acf3..b4b48afa 100644 --- a/include/git2/types.h +++ b/include/git2/types.h @@ -166,6 +166,12 @@ typedef enum { GIT_BRANCH_REMOTE = 2, } git_branch_t; +/** Kinds of reset operation. */ +typedef enum { + GIT_RESET_SOFT = 1, + GIT_RESET_MIXED = 2, +} git_reset_type; + typedef struct git_refspec git_refspec; typedef struct git_remote git_remote; @@ -415,7 +415,7 @@ int git_attr_cache__push_file( if (parse && (error = parse(repo, content, file)) < 0) goto finish; - git_strmap_insert(cache->files, file->key, file, error); + git_strmap_insert(cache->files, file->key, file, error); //-V595 if (error > 0) error = 0; @@ -148,27 +148,31 @@ static int write_symlink( return error; } -static int blob_create_internal(git_oid *oid, git_repository *repo, const char *path) +static int blob_create_internal(git_oid *oid, git_repository *repo, const char *content_path, const char *hint_path, bool try_load_filters) { int error; struct stat st; git_odb *odb = NULL; git_off_t size; - if ((error = git_path_lstat(path, &st)) < 0 || (error = git_repository_odb__weakptr(&odb, repo)) < 0) + assert(hint_path || !try_load_filters); + + if ((error = git_path_lstat(content_path, &st)) < 0 || (error = git_repository_odb__weakptr(&odb, repo)) < 0) return error; size = st.st_size; if (S_ISLNK(st.st_mode)) { - error = write_symlink(oid, odb, path, (size_t)size); + error = write_symlink(oid, odb, content_path, (size_t)size); } else { git_vector write_filters = GIT_VECTOR_INIT; - int filter_count; + int filter_count = 0; - /* Load the filters for writing this file to the ODB */ - filter_count = git_filters_load( - &write_filters, repo, path, GIT_FILTER_TO_ODB); + if (try_load_filters) { + /* Load the filters for writing this file to the ODB */ + filter_count = git_filters_load( + &write_filters, repo, hint_path, GIT_FILTER_TO_ODB); + } if (filter_count < 0) { /* Negative value means there was a critical error */ @@ -176,10 +180,10 @@ static int blob_create_internal(git_oid *oid, git_repository *repo, const char * } else if (filter_count == 0) { /* No filters need to be applied to the document: we can stream * directly from disk */ - error = write_file_stream(oid, odb, path, size); + error = write_file_stream(oid, odb, content_path, size); } else { /* We need to apply one or more filters */ - error = write_file_filtered(oid, odb, path, &write_filters); + error = write_file_filtered(oid, odb, content_path, &write_filters); } git_filters_free(&write_filters); @@ -216,7 +220,7 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat return -1; } - error = blob_create_internal(oid, repo, git_buf_cstr(&full_path)); + error = blob_create_internal(oid, repo, git_buf_cstr(&full_path), git_buf_cstr(&full_path), true); git_buf_free(&full_path); return error; @@ -232,8 +236,53 @@ int git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *pat return error; } - error = blob_create_internal(oid, repo, git_buf_cstr(&full_path)); + error = blob_create_internal(oid, repo, git_buf_cstr(&full_path), git_buf_cstr(&full_path), true); git_buf_free(&full_path); return error; } + +#define BUFFER_SIZE 4096 + +int git_blob_create_fromchunks( + git_oid *oid, + git_repository *repo, + const char *hintpath, + int (*source_cb)(char *content, size_t max_length, void *payload), + void *payload) +{ + int error = -1, read_bytes; + char *content = NULL; + git_filebuf file = GIT_FILEBUF_INIT; + + content = git__malloc(BUFFER_SIZE); + GITERR_CHECK_ALLOC(content); + + if (git_filebuf_open(&file, hintpath == NULL ? "streamed" : hintpath, GIT_FILEBUF_TEMPORARY) < 0) + goto cleanup; + + while (1) { + read_bytes = source_cb(content, BUFFER_SIZE, payload); + + assert(read_bytes <= BUFFER_SIZE); + + if (read_bytes <= 0) + break; + + if (git_filebuf_write(&file, content, read_bytes) < 0) + goto cleanup; + } + + if (read_bytes < 0) + goto cleanup; + + if (git_filebuf_flush(&file) < 0) + goto cleanup; + + error = blob_create_internal(oid, repo, file.path_lock, hintpath, hintpath != NULL); + +cleanup: + git_filebuf_cleanup(&file); + git__free(content); + return error; +} diff --git a/src/branch.c b/src/branch.c index 5d5a2403..8b97a820 100644 --- a/src/branch.c +++ b/src/branch.c @@ -149,9 +149,11 @@ static int branch_list_cb(const char *branch_name, void *payload) { branch_filter_data *filter = (branch_filter_data *)payload; - if ((filter->branch_type & GIT_BRANCH_LOCAL && git__prefixcmp(branch_name, GIT_REFS_HEADS_DIR) == 0) - || (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)); + 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))); + } return 0; } diff --git a/src/buffer.c b/src/buffer.c index 783a36eb..04aaec3d 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -144,8 +144,9 @@ int git_buf_puts(git_buf *buf, const char *string) int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) { int len; + const size_t expected_size = buf->size + (strlen(format) * 2); - ENSURE_SIZE(buf, buf->size + (strlen(format) * 2)); + ENSURE_SIZE(buf, expected_size); while (1) { va_list args; diff --git a/src/cache.c b/src/cache.c index 31da3c36..f8d89403 100644 --- a/src/cache.c +++ b/src/cache.c @@ -69,7 +69,7 @@ void *git_cache_try_store(git_cache *cache, void *_entry) git_cached_obj *entry = _entry; uint32_t hash; - memcpy(&hash, &entry->oid, sizeof(hash)); + memcpy(&hash, &entry->oid, sizeof(uint32_t)); /* increase the refcount on this object, because * the cache now owns it */ diff --git a/src/commit.c b/src/commit.c index 2f40dc67..a3baf9d4 100644 --- a/src/commit.c +++ b/src/commit.c @@ -81,66 +81,6 @@ int git_commit_create_v( return res; } -/* Update the reference named `ref_name` so it points to `oid` */ -static int update_reference(git_repository *repo, git_oid *oid, const char *ref_name) -{ - git_reference *ref; - int res; - - res = git_reference_lookup(&ref, repo, ref_name); - - /* If we haven't found the reference at all, we assume we need to create - * a new reference and that's it */ - if (res == GIT_ENOTFOUND) { - giterr_clear(); - return git_reference_create_oid(NULL, repo, ref_name, oid, 1); - } - - if (res < 0) - return -1; - - /* If we have found a reference, but it's symbolic, we need to update - * the direct reference it points to */ - if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { - git_reference *aux; - const char *sym_target; - - /* The target pointed at by this reference */ - sym_target = git_reference_target(ref); - - /* resolve the reference to the target it points to */ - res = git_reference_resolve(&aux, ref); - - /* - * if the symbolic reference pointed to an inexisting ref, - * this is means we're creating a new branch, for example. - * We need to create a new direct reference with that name - */ - if (res == GIT_ENOTFOUND) { - giterr_clear(); - res = git_reference_create_oid(NULL, repo, sym_target, oid, 1); - git_reference_free(ref); - return res; - } - - /* free the original symbolic reference now; not before because - * we're using the `sym_target` pointer */ - git_reference_free(ref); - - if (res < 0) - return -1; - - /* store the newly found direct reference in its place */ - ref = aux; - } - - /* ref is made to point to `oid`: ref is either the original reference, - * or the target of the symbolic reference we've looked up */ - res = git_reference_set_oid(ref, oid); - git_reference_free(ref); - return res; -} - int git_commit_create( git_oid *oid, git_repository *repo, @@ -153,7 +93,7 @@ int git_commit_create( int parent_count, const git_commit *parents[]) { - git_buf commit = GIT_BUF_INIT, cleaned_message = GIT_BUF_INIT; + git_buf commit = GIT_BUF_INIT; int i; git_odb *odb; @@ -174,15 +114,9 @@ int git_commit_create( git_buf_putc(&commit, '\n'); - /* Remove comments by default */ - if (git_message_prettify(&cleaned_message, message, 1) < 0) - goto on_error; - - if (git_buf_puts(&commit, git_buf_cstr(&cleaned_message)) < 0) + if (git_buf_puts(&commit, message) < 0) goto on_error; - git_buf_free(&cleaned_message); - if (git_repository_odb__weakptr(&odb, repo) < 0) goto on_error; @@ -192,13 +126,12 @@ int git_commit_create( git_buf_free(&commit); if (update_ref != NULL) - return update_reference(repo, oid, update_ref); + return git_reference__update(repo, oid, update_ref); return 0; on_error: git_buf_free(&commit); - git_buf_free(&cleaned_message); giterr_set(GITERR_OBJECT, "Failed to create commit."); return -1; } diff --git a/src/common.h b/src/common.h index e2a30029..419a8d06 100644 --- a/src/common.h +++ b/src/common.h @@ -24,6 +24,7 @@ # include <io.h> # include <direct.h> +# include <winsock2.h> # include <windows.h> # include "win32/msvc-compat.h" # include "win32/mingw-compat.h" diff --git a/src/date.c b/src/date.c new file mode 100644 index 00000000..f0e637a4 --- /dev/null +++ b/src/date.c @@ -0,0 +1,876 @@ +/* + * GIT - The information manager from hell + * + * Copyright (C) Linus Torvalds, 2005 + */ + +#include "common.h" + +#ifndef GIT_WIN32 +#include <sys/time.h> +#endif + +#include "util.h" +#include "cache.h" +#include "posix.h" + +#include <ctype.h> +#include <time.h> + +typedef enum { + DATE_NORMAL = 0, + DATE_RELATIVE, + DATE_SHORT, + DATE_LOCAL, + DATE_ISO8601, + DATE_RFC2822, + DATE_RAW +} date_mode; + +/* + * This is like mktime, but without normalization of tm_wday and tm_yday. + */ +static git_time_t tm_to_time_t(const struct tm *tm) +{ + static const int mdays[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + int year = tm->tm_year - 70; + int month = tm->tm_mon; + int day = tm->tm_mday; + + if (year < 0 || year > 129) /* algo only works for 1970-2099 */ + return -1; + if (month < 0 || month > 11) /* array bounds */ + return -1; + if (month < 2 || (year + 2) % 4) + day--; + if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) + return -1; + return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + + tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; +} + +static const char *month_names[] = { + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December" +}; + +static const char *weekday_names[] = { + "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" +}; + + + +/* + * Check these. And note how it doesn't do the summer-time conversion. + * + * In my world, it's always summer, and things are probably a bit off + * in other ways too. + */ +static const struct { + const char *name; + int offset; + int dst; +} timezone_names[] = { + { "IDLW", -12, 0, }, /* International Date Line West */ + { "NT", -11, 0, }, /* Nome */ + { "CAT", -10, 0, }, /* Central Alaska */ + { "HST", -10, 0, }, /* Hawaii Standard */ + { "HDT", -10, 1, }, /* Hawaii Daylight */ + { "YST", -9, 0, }, /* Yukon Standard */ + { "YDT", -9, 1, }, /* Yukon Daylight */ + { "PST", -8, 0, }, /* Pacific Standard */ + { "PDT", -8, 1, }, /* Pacific Daylight */ + { "MST", -7, 0, }, /* Mountain Standard */ + { "MDT", -7, 1, }, /* Mountain Daylight */ + { "CST", -6, 0, }, /* Central Standard */ + { "CDT", -6, 1, }, /* Central Daylight */ + { "EST", -5, 0, }, /* Eastern Standard */ + { "EDT", -5, 1, }, /* Eastern Daylight */ + { "AST", -3, 0, }, /* Atlantic Standard */ + { "ADT", -3, 1, }, /* Atlantic Daylight */ + { "WAT", -1, 0, }, /* West Africa */ + + { "GMT", 0, 0, }, /* Greenwich Mean */ + { "UTC", 0, 0, }, /* Universal (Coordinated) */ + { "Z", 0, 0, }, /* Zulu, alias for UTC */ + + { "WET", 0, 0, }, /* Western European */ + { "BST", 0, 1, }, /* British Summer */ + { "CET", +1, 0, }, /* Central European */ + { "MET", +1, 0, }, /* Middle European */ + { "MEWT", +1, 0, }, /* Middle European Winter */ + { "MEST", +1, 1, }, /* Middle European Summer */ + { "CEST", +1, 1, }, /* Central European Summer */ + { "MESZ", +1, 1, }, /* Middle European Summer */ + { "FWT", +1, 0, }, /* French Winter */ + { "FST", +1, 1, }, /* French Summer */ + { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ + { "EEST", +2, 1, }, /* Eastern European Daylight */ + { "WAST", +7, 0, }, /* West Australian Standard */ + { "WADT", +7, 1, }, /* West Australian Daylight */ + { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ + { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ + { "EAST", +10, 0, }, /* Eastern Australian Standard */ + { "EADT", +10, 1, }, /* Eastern Australian Daylight */ + { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ + { "NZT", +12, 0, }, /* New Zealand */ + { "NZST", +12, 0, }, /* New Zealand Standard */ + { "NZDT", +12, 1, }, /* New Zealand Daylight */ + { "IDLE", +12, 0, }, /* International Date Line East */ +}; + +static int match_string(const char *date, const char *str) +{ + int i = 0; + + for (i = 0; *date; date++, str++, i++) { + if (*date == *str) + continue; + if (toupper(*date) == toupper(*str)) + continue; + if (!isalnum(*date)) + break; + return 0; + } + return i; +} + +static int skip_alpha(const char *date) +{ + int i = 0; + do { + i++; + } while (isalpha(date[i])); + return i; +} + +/* +* Parse month, weekday, or timezone name +*/ +static int match_alpha(const char *date, struct tm *tm, int *offset) +{ + unsigned int i; + + for (i = 0; i < 12; i++) { + int match = match_string(date, month_names[i]); + if (match >= 3) { + tm->tm_mon = i; + return match; + } + } + + for (i = 0; i < 7; i++) { + int match = match_string(date, weekday_names[i]); + if (match >= 3) { + tm->tm_wday = i; + return match; + } + } + + for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { + int match = match_string(date, timezone_names[i].name); + if (match >= 3 || match == (int)strlen(timezone_names[i].name)) { + int off = timezone_names[i].offset; + + /* This is bogus, but we like summer */ + off += timezone_names[i].dst; + + /* Only use the tz name offset if we don't have anything better */ + if (*offset == -1) + *offset = 60*off; + + return match; + } + } + + if (match_string(date, "PM") == 2) { + tm->tm_hour = (tm->tm_hour % 12) + 12; + return 2; + } + + if (match_string(date, "AM") == 2) { + tm->tm_hour = (tm->tm_hour % 12) + 0; + return 2; + } + + /* BAD CRAP */ + return skip_alpha(date); +} + +static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) +{ + if (month > 0 && month < 13 && day > 0 && day < 32) { + struct tm check = *tm; + struct tm *r = (now_tm ? &check : tm); + time_t specified; + + r->tm_mon = month - 1; + r->tm_mday = day; + if (year == -1) { + if (!now_tm) + return 1; + r->tm_year = now_tm->tm_year; + } + else if (year >= 1970 && year < 2100) + r->tm_year = year - 1900; + else if (year > 70 && year < 100) + r->tm_year = year; + else if (year < 38) + r->tm_year = year + 100; + else + return 0; + if (!now_tm) + return 1; + + specified = tm_to_time_t(r); + + /* Be it commit time or author time, it does not make + * sense to specify timestamp way into the future. Make + * sure it is not later than ten days from now... + */ + if (now + 10*24*3600 < specified) + return 0; + tm->tm_mon = r->tm_mon; + tm->tm_mday = r->tm_mday; + if (year != -1) + tm->tm_year = r->tm_year; + return 1; + } + return 0; +} + +static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) +{ + time_t now; + struct tm now_tm; + struct tm *refuse_future; + long num2, num3; + + num2 = strtol(end+1, &end, 10); + num3 = -1; + if (*end == c && isdigit(end[1])) + num3 = strtol(end+1, &end, 10); + + /* Time? Date? */ + switch (c) { + case ':': + if (num3 < 0) + num3 = 0; + if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { + tm->tm_hour = num; + tm->tm_min = num2; + tm->tm_sec = num3; + break; + } + return 0; + + case '-': + case '/': + case '.': + now = time(NULL); + refuse_future = NULL; + if (p_gmtime_r(&now, &now_tm)) + refuse_future = &now_tm; + + if (num > 70) { + /* yyyy-mm-dd? */ + if (is_date(num, num2, num3, refuse_future, now, tm)) + break; + /* yyyy-dd-mm? */ + if (is_date(num, num3, num2, refuse_future, now, tm)) + break; + } + /* Our eastern European friends say dd.mm.yy[yy] + * is the norm there, so giving precedence to + * mm/dd/yy[yy] form only when separator is not '.' + */ + if (c != '.' && + is_date(num3, num, num2, refuse_future, now, tm)) + break; + /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ + if (is_date(num3, num2, num, refuse_future, now, tm)) + break; + /* Funny European mm.dd.yy */ + if (c == '.' && + is_date(num3, num, num2, refuse_future, now, tm)) + break; + return 0; + } + return end - date; +} + +/* + * Have we filled in any part of the time/date yet? + * We just do a binary 'and' to see if the sign bit + * is set in all the values. + */ +static int nodate(struct tm *tm) +{ + return (tm->tm_year & + tm->tm_mon & + tm->tm_mday & + tm->tm_hour & + tm->tm_min & + tm->tm_sec) < 0; +} + +/* + * We've seen a digit. Time? Year? Date? + */ +static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) +{ + int n; + char *end; + unsigned long num; + + num = strtoul(date, &end, 10); + + /* + * Seconds since 1970? We trigger on that for any numbers with + * more than 8 digits. This is because we don't want to rule out + * numbers like 20070606 as a YYYYMMDD date. + */ + if (num >= 100000000 && nodate(tm)) { + time_t time = num; + if (p_gmtime_r(&time, tm)) { + *tm_gmt = 1; + return end - date; + } + } + + /* + * Check for special formats: num[-.:/]num[same]num + */ + switch (*end) { + case ':': + case '.': + case '/': + case '-': + if (isdigit(end[1])) { + int match = match_multi_number(num, *end, date, end, tm); + if (match) + return match; + } + } + + /* + * None of the special formats? Try to guess what + * the number meant. We use the number of digits + * to make a more educated guess.. + */ + n = 0; + do { + n++; + } while (isdigit(date[n])); + + /* Four-digit year or a timezone? */ + if (n == 4) { + if (num <= 1400 && *offset == -1) { + unsigned int minutes = num % 100; + unsigned int hours = num / 100; + *offset = hours*60 + minutes; + } else if (num > 1900 && num < 2100) + tm->tm_year = num - 1900; + return n; + } + + /* + * Ignore lots of numerals. We took care of 4-digit years above. + * Days or months must be one or two digits. + */ + if (n > 2) + return n; + + /* + * NOTE! We will give precedence to day-of-month over month or + * year numbers in the 1-12 range. So 05 is always "mday 5", + * unless we already have a mday.. + * + * IOW, 01 Apr 05 parses as "April 1st, 2005". + */ + if (num > 0 && num < 32 && tm->tm_mday < 0) { + tm->tm_mday = num; + return n; + } + + /* Two-digit year? */ + if (n == 2 && tm->tm_year < 0) { + if (num < 10 && tm->tm_mday >= 0) { + tm->tm_year = num + 100; + return n; + } + if (num >= 70) { + tm->tm_year = num; + return n; + } + } + + if (num > 0 && num < 13 && tm->tm_mon < 0) + tm->tm_mon = num-1; + + return n; +} + +static int match_tz(const char *date, int *offp) +{ + char *end; + int hour = strtoul(date + 1, &end, 10); + int n = end - (date + 1); + int min = 0; + + if (n == 4) { + /* hhmm */ + min = hour % 100; + hour = hour / 100; + } else if (n != 2) { + min = 99; /* random crap */ + } else if (*end == ':') { + /* hh:mm? */ + min = strtoul(end + 1, &end, 10); + if (end - (date + 1) != 5) + min = 99; /* random crap */ + } /* otherwise we parsed "hh" */ + + /* + * Don't accept any random crap. Even though some places have + * offset larger than 12 hours (e.g. Pacific/Kiritimati is at + * UTC+14), there is something wrong if hour part is much + * larger than that. We might also want to check that the + * minutes are divisible by 15 or something too. (Offset of + * Kathmandu, Nepal is UTC+5:45) + */ + if (min < 60 && hour < 24) { + int offset = hour * 60 + min; + if (*date == '-') + offset = -offset; + *offp = offset; + } + return end - date; +} + +/* + * Parse a string like "0 +0000" as ancient timestamp near epoch, but + * only when it appears not as part of any other string. + */ +static int match_object_header_date(const char *date, git_time_t *timestamp, int *offset) +{ + char *end; + unsigned long stamp; + int ofs; + + if (*date < '0' || '9' <= *date) + return -1; + stamp = strtoul(date, &end, 10); + if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) + return -1; + date = end + 2; + ofs = strtol(date, &end, 10); + if ((*end != '\0' && (*end != '\n')) || end != date + 4) + return -1; + ofs = (ofs / 100) * 60 + (ofs % 100); + if (date[-1] == '-') + ofs = -ofs; + *timestamp = stamp; + *offset = ofs; + return 0; +} + +/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 + (i.e. English) day/month names, and it doesn't work correctly with %z. */ +static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset) +{ + struct tm tm; + int tm_gmt; + git_time_t dummy_timestamp; + int dummy_offset; + + if (!timestamp) + timestamp = &dummy_timestamp; + if (!offset) + offset = &dummy_offset; + + memset(&tm, 0, sizeof(tm)); + tm.tm_year = -1; + tm.tm_mon = -1; + tm.tm_mday = -1; + tm.tm_isdst = -1; + tm.tm_hour = -1; + tm.tm_min = -1; + tm.tm_sec = -1; + *offset = -1; + tm_gmt = 0; + + if (*date == '@' && + !match_object_header_date(date + 1, timestamp, offset)) + return 0; /* success */ + for (;;) { + int match = 0; + unsigned char c = *date; + + /* Stop at end of string or newline */ + if (!c || c == '\n') + break; + + if (isalpha(c)) + match = match_alpha(date, &tm, offset); + else if (isdigit(c)) + match = match_digit(date, &tm, offset, &tm_gmt); + else if ((c == '-' || c == '+') && isdigit(date[1])) + match = match_tz(date, offset); + + if (!match) { + /* BAD CRAP */ + match = 1; + } + + date += match; + } + + /* mktime uses local timezone */ + *timestamp = tm_to_time_t(&tm); + if (*offset == -1) + *offset = (int)((time_t)*timestamp - mktime(&tm)) / 60; + + if (*timestamp == (git_time_t)-1) + return -1; + + if (!tm_gmt) + *timestamp -= *offset * 60; + return 0; /* success */ +} + + +/* + * Relative time update (eg "2 days ago"). If we haven't set the time + * yet, we need to set it from current time. + */ +static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) +{ + time_t n; + + if (tm->tm_mday < 0) + tm->tm_mday = now->tm_mday; + if (tm->tm_mon < 0) + tm->tm_mon = now->tm_mon; + if (tm->tm_year < 0) { + tm->tm_year = now->tm_year; + if (tm->tm_mon > now->tm_mon) + tm->tm_year--; + } + + n = mktime(tm) - sec; + p_localtime_r(&n, tm); + return n; +} + +static void date_now(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + update_tm(tm, now, 0); +} + +static void date_yesterday(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + update_tm(tm, now, 24*60*60); +} + +static void date_time(struct tm *tm, struct tm *now, int hour) +{ + if (tm->tm_hour < hour) + date_yesterday(tm, now, NULL); + tm->tm_hour = hour; + tm->tm_min = 0; + tm->tm_sec = 0; +} + +static void date_midnight(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 0); +} + +static void date_noon(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 12); +} + +static void date_tea(struct tm *tm, struct tm *now, int *num) +{ + GIT_UNUSED(num); + date_time(tm, now, 17); +} + +static void date_pm(struct tm *tm, struct tm *now, int *num) +{ + int hour, n = *num; + *num = 0; + GIT_UNUSED(now); + + hour = tm->tm_hour; + if (n) { + hour = n; + tm->tm_min = 0; + tm->tm_sec = 0; + } + tm->tm_hour = (hour % 12) + 12; +} + +static void date_am(struct tm *tm, struct tm *now, int *num) +{ + int hour, n = *num; + *num = 0; + GIT_UNUSED(now); + + hour = tm->tm_hour; + if (n) { + hour = n; + tm->tm_min = 0; + tm->tm_sec = 0; + } + tm->tm_hour = (hour % 12); +} + +static void date_never(struct tm *tm, struct tm *now, int *num) +{ + time_t n = 0; + GIT_UNUSED(now); + GIT_UNUSED(num); + p_localtime_r(&n, tm); +} + +static const struct special { + const char *name; + void (*fn)(struct tm *, struct tm *, int *); +} special[] = { + { "yesterday", date_yesterday }, + { "noon", date_noon }, + { "midnight", date_midnight }, + { "tea", date_tea }, + { "PM", date_pm }, + { "AM", date_am }, + { "never", date_never }, + { "now", date_now }, + { NULL } +}; + +static const char *number_name[] = { + "zero", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine", "ten", +}; + +static const struct typelen { + const char *type; + int length; +} typelen[] = { + { "seconds", 1 }, + { "minutes", 60 }, + { "hours", 60*60 }, + { "days", 24*60*60 }, + { "weeks", 7*24*60*60 }, + { NULL } +}; + +static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) +{ + const struct typelen *tl; + const struct special *s; + const char *end = date; + int i; + + while (isalpha(*++end)); + ; + + for (i = 0; i < 12; i++) { + int match = match_string(date, month_names[i]); + if (match >= 3) { + tm->tm_mon = i; + *touched = 1; + return end; + } + } + + for (s = special; s->name; s++) { + int len = strlen(s->name); + if (match_string(date, s->name) == len) { + s->fn(tm, now, num); + *touched = 1; + return end; + } + } + + if (!*num) { + for (i = 1; i < 11; i++) { + int len = strlen(number_name[i]); + if (match_string(date, number_name[i]) == len) { + *num = i; + *touched = 1; + return end; + } + } + if (match_string(date, "last") == 4) { + *num = 1; + *touched = 1; + } + return end; + } + + tl = typelen; + while (tl->type) { + int len = strlen(tl->type); + if (match_string(date, tl->type) >= len-1) { + update_tm(tm, now, tl->length * *num); + *num = 0; + *touched = 1; + return end; + } + tl++; + } + + for (i = 0; i < 7; i++) { + int match = match_string(date, weekday_names[i]); + if (match >= 3) { + int diff, n = *num -1; + *num = 0; + + diff = tm->tm_wday - i; + if (diff <= 0) + n++; + diff += 7*n; + + update_tm(tm, now, diff * 24 * 60 * 60); + *touched = 1; + return end; + } + } + + if (match_string(date, "months") >= 5) { + int n; + update_tm(tm, now, 0); /* fill in date fields if needed */ + n = tm->tm_mon - *num; + *num = 0; + while (n < 0) { + n += 12; + tm->tm_year--; + } + tm->tm_mon = n; + *touched = 1; + return end; + } + + if (match_string(date, "years") >= 4) { + update_tm(tm, now, 0); /* fill in date fields if needed */ + tm->tm_year -= *num; + *num = 0; + *touched = 1; + return end; + } + + return end; +} + +static const char *approxidate_digit(const char *date, struct tm *tm, int *num) +{ + char *end; + unsigned long number = strtoul(date, &end, 10); + + switch (*end) { + case ':': + case '.': + case '/': + case '-': + if (isdigit(end[1])) { + int match = match_multi_number(number, *end, date, end, tm); + if (match) + return date + match; + } + } + + /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ + if (date[0] != '0' || end - date <= 2) + *num = number; + return end; +} + +/* + * Do we have a pending number at the end, or when + * we see a new one? Let's assume it's a month day, + * as in "Dec 6, 1992" + */ +static void pending_number(struct tm *tm, int *num) +{ + int number = *num; + + if (number) { + *num = 0; + if (tm->tm_mday < 0 && number < 32) + tm->tm_mday = number; + else if (tm->tm_mon < 0 && number < 13) + tm->tm_mon = number-1; + else if (tm->tm_year < 0) { + if (number > 1969 && number < 2100) + tm->tm_year = number - 1900; + else if (number > 69 && number < 100) + tm->tm_year = number; + else if (number < 38) + tm->tm_year = 100 + number; + /* We screw up for number = 00 ? */ + } + } +} + +static git_time_t approxidate_str(const char *date, + const struct timeval *tv, + int *error_ret) +{ + int number = 0; + int touched = 0; + struct tm tm = {0}, now; + time_t time_sec; + + time_sec = tv->tv_sec; + p_localtime_r(&time_sec, &tm); + now = tm; + + tm.tm_year = -1; + tm.tm_mon = -1; + tm.tm_mday = -1; + + for (;;) { + unsigned char c = *date; + if (!c) + break; + date++; + if (isdigit(c)) { + pending_number(&tm, &number); + date = approxidate_digit(date-1, &tm, &number); + touched = 1; + continue; + } + if (isalpha(c)) + date = approxidate_alpha(date-1, &tm, &now, &number, &touched); + } + pending_number(&tm, &number); + if (!touched) + *error_ret = 1; + return update_tm(&tm, &now, 0); +} + +int git__date_parse(git_time_t *out, const char *date) +{ + struct timeval tv; + git_time_t timestamp; + int offset, error_ret=0; + + if (!parse_date_basic(date, ×tamp, &offset)) { + *out = timestamp; + return 0; + } + + p_gettimeofday(&tv, NULL); + *out = approxidate_str(date, &tv, &error_ret); + return error_ret; +} @@ -130,37 +130,50 @@ fail: static git_diff_delta *diff_delta__merge_like_cgit( const git_diff_delta *a, const git_diff_delta *b, git_pool *pool) { - git_diff_delta *dup = diff_delta__dup(a, pool); - if (!dup) - return NULL; - - if (git_oid_cmp(&dup->new_file.oid, &b->new_file.oid) == 0) - return dup; - - git_oid_cpy(&dup->new_file.oid, &b->new_file.oid); - - dup->new_file.mode = b->new_file.mode; - dup->new_file.size = b->new_file.size; - dup->new_file.flags = b->new_file.flags; + git_diff_delta *dup; /* Emulate C git for merging two diffs (a la 'git diff <sha>'). * * When C git does a diff between the work dir and a tree, it actually * diffs with the index but uses the workdir contents. This emulates * those choices so we can emulate the type of diff. + * + * We have three file descriptions here, let's call them: + * f1 = a->old_file + * f2 = a->new_file AND b->old_file + * f3 = b->new_file */ - if (git_oid_cmp(&dup->old_file.oid, &dup->new_file.oid) == 0) { - if (dup->status == GIT_DELTA_DELETED) - /* preserve pending delete info */; - else if (b->status == GIT_DELTA_UNTRACKED || - b->status == GIT_DELTA_IGNORED) - dup->status = b->status; - else + + /* if f2 == f3 or f2 is deleted, then just dup the 'a' diff */ + if (b->status == GIT_DELTA_UNMODIFIED || a->status == GIT_DELTA_DELETED) + return diff_delta__dup(a, pool); + + /* otherwise, base this diff on the 'b' diff */ + if ((dup = diff_delta__dup(b, pool)) == NULL) + return NULL; + + /* If 'a' status is uninteresting, then we're done */ + if (a->status == GIT_DELTA_UNMODIFIED) + return dup; + + assert(a->status != GIT_DELTA_UNMODIFIED); + assert(b->status != GIT_DELTA_UNMODIFIED); + + /* A cgit exception is that the diff of a file that is only in the + * index (i.e. not in HEAD nor workdir) is given as empty. + */ + if (dup->status == GIT_DELTA_DELETED) { + if (a->status == GIT_DELTA_ADDED) dup->status = GIT_DELTA_UNMODIFIED; + /* else don't overwrite DELETE status */ + } else { + dup->status = a->status; } - else if (dup->status == GIT_DELTA_UNMODIFIED || - b->status == GIT_DELTA_DELETED) - dup->status = b->status; + + git_oid_cpy(&dup->old_file.oid, &a->old_file.oid); + dup->old_file.mode = a->old_file.mode; + dup->old_file.size = a->old_file.size; + dup->old_file.flags = a->old_file.flags; return dup; } @@ -214,7 +227,9 @@ static int diff_delta__from_two( git_diff_list *diff, git_delta_t status, const git_index_entry *old_entry, + uint32_t old_mode, const git_index_entry *new_entry, + uint32_t new_mode, git_oid *new_oid) { git_diff_delta *delta; @@ -224,19 +239,22 @@ static int diff_delta__from_two( return 0; if ((diff->opts.flags & GIT_DIFF_REVERSE) != 0) { - const git_index_entry *temp = old_entry; + uint32_t temp_mode = old_mode; + const git_index_entry *temp_entry = old_entry; old_entry = new_entry; - new_entry = temp; + new_entry = temp_entry; + old_mode = new_mode; + new_mode = temp_mode; } delta = diff_delta__alloc(diff, status, old_entry->path); GITERR_CHECK_ALLOC(delta); - delta->old_file.mode = old_entry->mode; + delta->old_file.mode = old_mode; git_oid_cpy(&delta->old_file.oid, &old_entry->oid); delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID; - delta->new_file.mode = new_entry->mode; + delta->new_file.mode = new_mode; git_oid_cpy(&delta->new_file.oid, new_oid ? new_oid : &new_entry->oid); if (new_oid || !git_oid_iszero(&new_entry->oid)) delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID; @@ -300,7 +318,7 @@ static git_diff_list *git_diff_list_alloc( if (config_bool(cfg, "core.ignorestat", 0)) diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_ASSUME_UNCHANGED; if (config_bool(cfg, "core.filemode", 1)) - diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_EXEC_BIT; + diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_MODE_BITS; if (config_bool(cfg, "core.trustctime", 1)) diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_CTIME; /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */ @@ -419,7 +437,7 @@ static int oid_for_workdir_item( return result; } -#define EXEC_BIT_MASK 0000111 +#define MODE_BITS_MASK 0000777 static int maybe_modified( git_iterator *old_iter, @@ -443,13 +461,13 @@ static int maybe_modified( !(diff->diffcaps & GIT_DIFFCAPS_HAS_SYMLINKS)) nmode = GIT_MODE_TYPE(omode) | (nmode & GIT_MODE_PERMS_MASK); - /* on platforms with no execmode, clear exec bit from comparisons */ - if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_EXEC_BIT)) { - omode = omode & ~EXEC_BIT_MASK; - nmode = nmode & ~EXEC_BIT_MASK; - } + /* on platforms with no execmode, just preserve old mode */ + if (!(diff->diffcaps & GIT_DIFFCAPS_TRUST_MODE_BITS) && + (nmode & MODE_BITS_MASK) != (omode & MODE_BITS_MASK) && + new_iter->type == GIT_ITERATOR_WORKDIR) + nmode = (nmode & ~MODE_BITS_MASK) | (omode & MODE_BITS_MASK); - /* support "assume unchanged" (badly, b/c we still stat everything) */ + /* support "assume unchanged" (poorly, b/c we still stat everything) */ if ((diff->diffcaps & GIT_DIFFCAPS_ASSUME_UNCHANGED) != 0) status = (oitem->flags_extended & GIT_IDXENTRY_INTENT_TO_ADD) ? GIT_DELTA_MODIFIED : GIT_DELTA_UNMODIFIED; @@ -471,8 +489,13 @@ static int maybe_modified( omode == nmode) status = GIT_DELTA_UNMODIFIED; - /* if we have a workdir item with an unknown oid, check deeper */ - else if (git_oid_iszero(&nitem->oid) && new_iter->type == GIT_ITERATOR_WORKDIR) { + /* if modes match and we have an unknown OID and a workdir iterator, + * then check deeper for matching + */ + else if (omode == nmode && + git_oid_iszero(&nitem->oid) && + new_iter->type == GIT_ITERATOR_WORKDIR) + { /* TODO: add check against index file st_mtime to avoid racy-git */ /* if they files look exactly alike, then we'll assume the same */ @@ -517,7 +540,8 @@ static int maybe_modified( use_noid = &noid; } - return diff_delta__from_two(diff, status, oitem, nitem, use_noid); + return diff_delta__from_two( + diff, status, oitem, omode, nitem, nmode, use_noid); } static int diff_from_iterators( @@ -772,6 +796,12 @@ int git_diff_merge( git_vector_swap(&onto->deltas, &onto_new); git_pool_swap(&onto->pool, &onto_pool); onto->new_src = from->new_src; + + /* prefix strings also come from old pool, so recreate those.*/ + onto->opts.old_prefix = + git_pool_strdup(&onto->pool, onto->opts.old_prefix); + onto->opts.new_prefix = + git_pool_strdup(&onto->pool, onto->opts.new_prefix); } git_vector_foreach(&onto_new, i, delta) @@ -20,7 +20,7 @@ enum { GIT_DIFFCAPS_HAS_SYMLINKS = (1 << 0), /* symlinks on platform? */ GIT_DIFFCAPS_ASSUME_UNCHANGED = (1 << 1), /* use stat? */ - GIT_DIFFCAPS_TRUST_EXEC_BIT = (1 << 2), /* use st_mode exec bit? */ + GIT_DIFFCAPS_TRUST_MODE_BITS = (1 << 2), /* use st_mode? */ GIT_DIFFCAPS_TRUST_CTIME = (1 << 3), /* use st_ctime? */ GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */ }; @@ -36,5 +36,8 @@ struct git_diff_list { uint32_t diffcaps; }; +extern void git_diff__cleanup_modes( + uint32_t diffcaps, uint32_t *omode, uint32_t *nmode); + #endif diff --git a/src/diff_output.c b/src/diff_output.c index 5ffa641c..92f7f8f2 100644 --- a/src/diff_output.c +++ b/src/diff_output.c @@ -83,12 +83,13 @@ static int diff_output_cb(void *priv, mmbuffer_t *bufs, int len) info->cb_data, info->delta, &info->range, origin, bufs[1].ptr, bufs[1].size) < 0) return -1; - /* deal with adding and removing newline at EOF */ + /* This should only happen if we are adding a line that does not + * have a newline at the end and the old code did. In that case, + * we have a ADD with a DEL_EOFNL as a pair. + */ if (len == 3) { - if (origin == GIT_DIFF_LINE_ADDITION) - origin = GIT_DIFF_LINE_ADD_EOFNL; - else - origin = GIT_DIFF_LINE_DEL_EOFNL; + origin = (origin == GIT_DIFF_LINE_ADDITION) ? + GIT_DIFF_LINE_DEL_EOFNL : GIT_DIFF_LINE_ADD_EOFNL; return info->line_cb( info->cb_data, info->delta, &info->range, origin, bufs[2].ptr, bufs[2].size); @@ -359,7 +360,7 @@ int git_diff_foreach( /* map files */ if (delta->binary != 1 && - (hunk_cb || line_cb) && + (hunk_cb || line_cb || git_oid_iszero(&delta->old_file.oid)) && (delta->status == GIT_DELTA_DELETED || delta->status == GIT_DELTA_MODIFIED)) { @@ -397,7 +398,9 @@ int git_diff_foreach( /* since we did not have the definitive oid, we may have * incorrect status and need to skip this item. */ - if (git_oid_cmp(&delta->old_file.oid, &delta->new_file.oid) == 0) { + if (delta->old_file.mode == delta->new_file.mode && + !git_oid_cmp(&delta->old_file.oid, &delta->new_file.oid)) + { delta->status = GIT_DELTA_UNMODIFIED; if ((diff->opts.flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0) goto cleanup; @@ -420,7 +423,8 @@ int git_diff_foreach( */ if (file_cb != NULL) { - error = file_cb(data, delta, (float)info.index / diff->deltas.length); + error = file_cb( + data, delta, (float)info.index / diff->deltas.length); if (error < 0) goto cleanup; } @@ -433,6 +437,10 @@ int git_diff_foreach( if (!old_data.len && !new_data.len) goto cleanup; + /* nothing to do if only diff was a mode change */ + if (!git_oid_cmp(&delta->old_file.oid, &delta->new_file.oid)) + goto cleanup; + assert(hunk_cb || line_cb); info.delta = delta; @@ -467,7 +475,7 @@ static char pick_suffix(int mode) { if (S_ISDIR(mode)) return '/'; - else if (mode & 0100) + else if (mode & 0100) //-V536 /* in git, modes are very regular, so we must have 0100755 mode */ return '*'; else diff --git a/src/filebuf.c b/src/filebuf.c index 6538aea6..876f8e3e 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -130,6 +130,11 @@ GIT_INLINE(int) flush_buffer(git_filebuf *file) return result; } +int git_filebuf_flush(git_filebuf *file) +{ + return flush_buffer(file); +} + static int write_normal(git_filebuf *file, void *source, size_t len) { if (len > 0) { diff --git a/src/filebuf.h b/src/filebuf.h index 72563b57..37788314 100644 --- a/src/filebuf.h +++ b/src/filebuf.h @@ -81,5 +81,6 @@ int git_filebuf_commit(git_filebuf *lock, mode_t mode); int git_filebuf_commit_at(git_filebuf *lock, const char *path, mode_t mode); void git_filebuf_cleanup(git_filebuf *lock); int git_filebuf_hash(git_oid *oid, git_filebuf *file); +int git_filebuf_flush(git_filebuf *file); #endif diff --git a/src/ignore.c b/src/ignore.c index fc6194bb..f2d08f59 100644 --- a/src/ignore.c +++ b/src/ignore.c @@ -28,6 +28,8 @@ static int parse_ignore_file( GITERR_CHECK_ALLOC(match); } + match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE; + if (!(error = git_attr_fnmatch__parse( match, ignores->pool, context, &scan))) { diff --git a/src/indexer.c b/src/indexer.c index f0e0a638..5ae66c9f 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -292,11 +292,13 @@ int git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t siz { int error; struct git_pack_header hdr; - size_t processed = stats->processed; + size_t processed; git_mwindow_file *mwf = &idx->pack->mwf; assert(idx && data && stats); + processed = stats->processed; + if (git_filebuf_write(&idx->pack_file, data, size) < 0) return -1; diff --git a/src/netops.c b/src/netops.c index b12750be..32554743 100644 --- a/src/netops.c +++ b/src/netops.c @@ -12,7 +12,6 @@ # include <netdb.h> # include <arpa/inet.h> #else -# include <winsock2.h> # include <ws2tcpip.h> # ifdef _MSC_VER # pragma comment(lib, "ws2_32.lib") @@ -144,7 +143,9 @@ void gitno_consume_n(gitno_buffer *buf, size_t cons) int gitno_ssl_teardown(git_transport *t) { - int ret = ret; +#ifdef GIT_SSL + int ret; +#endif if (!t->encrypt) return 0; diff --git a/src/notes.c b/src/notes.c index 84ad9408..efbdbabe 100644 --- a/src/notes.c +++ b/src/notes.c @@ -12,50 +12,67 @@ #include "config.h" #include "iterator.h" -static int find_subtree(git_tree **subtree, const git_oid *root, - git_repository *repo, const char *target, int *fanout) +static int find_subtree_in_current_level( + git_tree **out, + git_repository *repo, + git_tree *parent, + const char *annotated_object_sha, + int fanout) { - int error; unsigned int i; - git_tree *tree; const git_tree_entry *entry; - *subtree = NULL; - - error = git_tree_lookup(&tree, repo, root); - if (error < 0) - return error; + *out = NULL; + + if (parent == NULL) + return GIT_ENOTFOUND; - for (i=0; i<git_tree_entrycount(tree); i++) { - entry = git_tree_entry_byindex(tree, i); + for (i = 0; i < git_tree_entrycount(parent); i++) { + entry = git_tree_entry_byindex(parent, i); if (!git__ishex(git_tree_entry_name(entry))) continue; - /* - * A notes tree follows a strict byte-based progressive fanout - * (i.e. using 2/38, 2/2/36, etc. fanouts, not e.g. 4/36 fanout) - */ - if (S_ISDIR(git_tree_entry_attributes(entry)) - && strlen(git_tree_entry_name(entry)) == 2 - && !strncmp(git_tree_entry_name(entry), target + *fanout, 2)) { + && strlen(git_tree_entry_name(entry)) == 2 + && !strncmp(git_tree_entry_name(entry), annotated_object_sha + fanout, 2)) + return git_tree_lookup(out, repo, git_tree_entry_id(entry)); - /* found matching subtree - unpack and resume lookup */ + /* Not a DIR, so do we have an already existing blob? */ + if (!strcmp(git_tree_entry_name(entry), annotated_object_sha + fanout)) + return GIT_EEXISTS; + } + + return GIT_ENOTFOUND; +} - git_oid subtree_sha; - git_oid_cpy(&subtree_sha, git_tree_entry_id(entry)); - git_tree_free(tree); +static int find_subtree_r(git_tree **out, git_tree *root, + git_repository *repo, const char *target, int *fanout) +{ + int error; + git_tree *subtree = NULL; - *fanout += 2; + *out = NULL; - return find_subtree(subtree, &subtree_sha, repo, - target, fanout); - } + 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)); } - *subtree = tree; - return 0; + if (error < 0) + return error; + + *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); + + return error; } static int find_blob(git_oid *blob, git_tree *tree, const char *target) @@ -76,191 +93,275 @@ static int find_blob(git_oid *blob, git_tree *tree, const char *target) return GIT_ENOTFOUND; } -static int note_write(git_oid *out, git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const char *note, - const git_oid *tree_sha, const char *target, - int nparents, git_commit **parents) +static int tree_write( + git_tree **out, + git_repository *repo, + git_tree *source_tree, + const git_oid *object_oid, + const char *treeentry_name, + unsigned int attributes) { - int error, fanout = 0; - git_oid oid; - git_tree *tree = NULL; + int error; + git_treebuilder *tb = NULL; git_tree_entry *entry; - git_treebuilder *tb; - - /* check for existing notes tree */ - - if (tree_sha) { - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; - - error = find_blob(&oid, tree, target + fanout); - if (error != GIT_ENOTFOUND) { - git_tree_free(tree); - if (!error) { - giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", target); - error = GIT_EEXISTS; - } - return error; - } - } + git_oid tree_oid; - /* no matching tree entry - add note object to target tree */ + if ((error = git_treebuilder_create(&tb, source_tree)) < 0) + goto cleanup; - error = git_treebuilder_create(&tb, tree); - git_tree_free(tree); + if (object_oid) { + if ((error = git_treebuilder_insert( + &entry, tb, treeentry_name, object_oid, attributes)) < 0) + goto cleanup; + } else { + if ((error = git_treebuilder_remove(tb, treeentry_name)) < 0) + goto cleanup; + } - if (error < 0) - return error; + if ((error = git_treebuilder_write(&tree_oid, repo, tb)) < 0) + goto cleanup; - if (!tree_sha) - /* no notes tree yet - create fanout */ - fanout += 2; + error = git_tree_lookup(out, repo, &tree_oid); - /* create note object */ - error = git_blob_create_frombuffer(&oid, repo, note, strlen(note)); - if (error < 0) { - git_treebuilder_free(tb); - return error; - } +cleanup: + git_treebuilder_free(tb); + return error; +} - error = git_treebuilder_insert(&entry, tb, target + fanout, &oid, 0100644); - if (error < 0) { - /* libgit2 doesn't support object removal (gc) yet */ - /* we leave an orphaned blob object behind - TODO */ +static int manipulate_note_in_tree_r( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int (*note_exists_cb)( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error), + int (*note_notfound_cb)( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error)) +{ + int error = -1; + git_tree *subtree = NULL; + char subtree_name[3]; - git_treebuilder_free(tb); - return error; - } + error = find_subtree_in_current_level( + &subtree, repo, parent, annotated_object_sha, fanout); - if (out) - git_oid_cpy(out, git_tree_entry_id(entry)); + if (error == GIT_EEXISTS) { + error = note_exists_cb( + out, repo, parent, note_oid, annotated_object_sha, fanout, error); + goto cleanup; + } - error = git_treebuilder_write(&oid, repo, tb); - git_treebuilder_free(tb); + if (error == GIT_ENOTFOUND) { + error = note_notfound_cb( + out, repo, parent, note_oid, annotated_object_sha, fanout, error); + goto cleanup; + } if (error < 0) - return 0; - - if (!tree_sha) { - /* create fanout subtree */ + goto cleanup; - char subtree[3]; - strncpy(subtree, target, 2); - subtree[2] = '\0'; + /* An existing fanout has been found, let's dig deeper */ + error = manipulate_note_in_tree_r( + out, repo, subtree, note_oid, annotated_object_sha, + fanout + 2, note_exists_cb, note_notfound_cb); - error = git_treebuilder_create(&tb, NULL); - if (error < 0) - return error; + if (error < 0) + goto cleanup; - error = git_treebuilder_insert(NULL, tb, subtree, &oid, 0040000); - if (error < 0) { - git_treebuilder_free(tb); - return error; - } + strncpy(subtree_name, annotated_object_sha + fanout, 2); + subtree_name[2] = '\0'; - error = git_treebuilder_write(&oid, repo, tb); + error = tree_write(out, repo, parent, + git_object_id((const git_object *)(*out)), subtree_name, 0040000); - git_treebuilder_free(tb); +cleanup: + git_tree_free(subtree); + return error; +} - if (error < 0) - return error; - } +static int remove_note_in_tree_eexists_cb( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(note_oid); + GIT_UNUSED(current_error); - /* create new notes commit */ + return tree_write(out, repo, parent, NULL, annotated_object_sha + fanout, 0); +} - error = git_tree_lookup(&tree, repo, &oid); - if (error < 0) - return error; +static int remove_note_in_tree_enotfound_cb( + git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(out); + GIT_UNUSED(repo); + GIT_UNUSED(parent); + GIT_UNUSED(note_oid); + GIT_UNUSED(fanout); + + giterr_set(GITERR_REPOSITORY, "Object '%s' has no note", annotated_object_sha); + return current_error; +} - error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_ADD, - tree, nparents, (const git_commit **) parents); +static int insert_note_in_tree_eexists_cb(git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(out); + GIT_UNUSED(repo); + GIT_UNUSED(parent); + GIT_UNUSED(note_oid); + GIT_UNUSED(fanout); + + giterr_set(GITERR_REPOSITORY, "Note for '%s' exists already", annotated_object_sha); + return current_error; +} - git_tree_free(tree); +static int insert_note_in_tree_enotfound_cb(git_tree **out, + git_repository *repo, + git_tree *parent, + git_oid *note_oid, + const char *annotated_object_sha, + int fanout, + int current_error) +{ + GIT_UNUSED(current_error); - return error; + /* No existing fanout at this level, insert in place */ + return tree_write(out, repo, parent, note_oid, annotated_object_sha + fanout, 0100644); } -static int note_lookup(git_note **out, git_repository *repo, - const git_oid *tree_sha, const char *target) +static int note_write(git_oid *out, + git_repository *repo, + git_signature *author, + git_signature *committer, + const char *notes_ref, + const char *note, + git_tree *commit_tree, + const char *target, + git_commit **parents) { - int error, fanout = 0; + int error; git_oid oid; - git_blob *blob; - git_tree *tree; - git_note *note; + git_tree *tree = NULL; + + // TODO: should we apply filters? + /* create note object */ + if ((error = git_blob_create_frombuffer(&oid, repo, note, strlen(note))) < 0) + goto cleanup; - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; + if ((error = manipulate_note_in_tree_r( + &tree, repo, commit_tree, &oid, target, 0, + insert_note_in_tree_eexists_cb, insert_note_in_tree_enotfound_cb)) < 0) + goto cleanup; - error = find_blob(&oid, tree, target + fanout); + if (out) + git_oid_cpy(out, &oid); + + error = git_commit_create(&oid, repo, notes_ref, author, committer, + NULL, GIT_NOTES_DEFAULT_MSG_ADD, + tree, *parents == NULL ? 0 : 1, (const git_commit **) parents); +cleanup: git_tree_free(tree); - if (error < 0) - return error; + return error; +} - error = git_blob_lookup(&blob, repo, &oid); - if (error < 0) - return error; +static int note_new(git_note **out, git_oid *note_oid, git_blob *blob) +{ + git_note *note = NULL; - note = git__malloc(sizeof(git_note)); + note = (git_note *)git__malloc(sizeof(git_note)); GITERR_CHECK_ALLOC(note); - git_oid_cpy(¬e->oid, &oid); - note->message = git__strdup(git_blob_rawcontent(blob)); + git_oid_cpy(¬e->oid, note_oid); + note->message = git__strdup((char *)git_blob_rawcontent(blob)); GITERR_CHECK_ALLOC(note->message); *out = note; - git_blob_free(blob); - return error; + return 0; } -static int note_remove(git_repository *repo, - git_signature *author, git_signature *committer, - const char *notes_ref, const git_oid *tree_sha, - const char *target, int nparents, git_commit **parents) +static int note_lookup(git_note **out, git_repository *repo, + git_tree *tree, const char *target) { int error, fanout = 0; git_oid oid; - git_tree *tree; - git_treebuilder *tb; + git_blob *blob = NULL; + git_note *note = NULL; + git_tree *subtree = NULL; - error = find_subtree(&tree, tree_sha, repo, target, &fanout); - if (error < 0) - return error; + if ((error = find_subtree_r(&subtree, tree, repo, target, &fanout)) < 0) + goto cleanup; - error = find_blob(&oid, tree, target + fanout); - if (!error) - error = git_treebuilder_create(&tb, tree); + if ((error = find_blob(&oid, subtree, target + fanout)) < 0) + goto cleanup; - git_tree_free(tree); - if (error < 0) - return error; + if ((error = git_blob_lookup(&blob, repo, &oid)) < 0) + goto cleanup; - error = git_treebuilder_remove(tb, target + fanout); - if (!error) - error = git_treebuilder_write(&oid, repo, tb); + if ((error = note_new(¬e, &oid, blob)) < 0) + goto cleanup; - git_treebuilder_free(tb); - if (error < 0) - return error; + *out = note; - /* create new notes commit */ +cleanup: + git_tree_free(subtree); + git_blob_free(blob); + return error; +} - error = git_tree_lookup(&tree, repo, &oid); - if (error < 0) - return error; +static int note_remove(git_repository *repo, + git_signature *author, git_signature *committer, + const char *notes_ref, git_tree *tree, + const char *target, git_commit **parents) +{ + int error; + git_tree *tree_after_removal = NULL; + git_oid oid; + + if ((error = manipulate_note_in_tree_r( + &tree_after_removal, repo, tree, NULL, target, 0, + remove_note_in_tree_eexists_cb, remove_note_in_tree_enotfound_cb)) < 0) + goto cleanup; error = git_commit_create(&oid, repo, notes_ref, author, committer, - NULL, GIT_NOTES_DEFAULT_MSG_RM, - tree, nparents, (const git_commit **) parents); - - git_tree_free(tree); + NULL, GIT_NOTES_DEFAULT_MSG_RM, + tree_after_removal, + *parents == NULL ? 0 : 1, + (const git_commit **) parents); +cleanup: + git_tree_free(tree_after_removal); return error; } @@ -291,48 +392,50 @@ static int normalize_namespace(const char **notes_ref, git_repository *repo) return note_get_default_ref(notes_ref, repo); } -static int retrieve_note_tree_oid(git_oid *tree_oid_out, git_repository *repo, const char *notes_ref) +static int retrieve_note_tree_and_commit( + git_tree **tree_out, + git_commit **commit_out, + git_repository *repo, + const char **notes_ref) { - int error = -1; - git_commit *commit = NULL; + int error; git_oid oid; - if ((error = git_reference_name_to_oid(&oid, repo, notes_ref)) < 0) - goto cleanup; + if ((error = normalize_namespace(notes_ref, repo)) < 0) + return error; - if (git_commit_lookup(&commit, repo, &oid) < 0) - goto cleanup; + if ((error = git_reference_name_to_oid(&oid, repo, *notes_ref)) < 0) + return error; - git_oid_cpy(tree_oid_out, git_commit_tree_oid(commit)); + if (git_commit_lookup(commit_out, repo, &oid) < 0) + return error; - error = 0; + if ((error = git_commit_tree(tree_out, *commit_out)) < 0) + return error; -cleanup: - git_commit_free(commit); - return error; + return 0; } int git_note_read(git_note **out, git_repository *repo, const char *notes_ref, const git_oid *oid) { int error; - char *target; - git_oid sha; - - *out = NULL; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - if ((error = retrieve_note_tree_oid(&sha, repo, notes_ref)) < 0) - return error; + char *target = NULL; + git_tree *tree = NULL; + git_commit *commit = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); - error = note_lookup(out, repo, &sha, target); + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) + goto cleanup; + + error = note_lookup(out, repo, tree, target); +cleanup: git__free(target); + git_tree_free(tree); + git_commit_free(commit); return error; } @@ -342,44 +445,26 @@ int git_note_create( const char *notes_ref, const git_oid *oid, const char *note) { - int error, nparents = 0; - char *target; - git_oid sha; + int error; + char *target = NULL; git_commit *commit = NULL; - git_reference *ref; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - error = git_reference_lookup(&ref, repo, notes_ref); - if (error < 0 && error != GIT_ENOTFOUND) - return error; - - if (!error) { - assert(git_reference_type(ref) == GIT_REF_OID); - - /* lookup existing notes tree oid */ - - git_oid_cpy(&sha, git_reference_oid(ref)); - git_reference_free(ref); - - error = git_commit_lookup(&commit, repo, &sha); - if (error < 0) - return error; - - git_oid_cpy(&sha, git_commit_tree_oid(commit)); - nparents++; - } + git_tree *tree = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); + error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref); + + if (error < 0 && error != GIT_ENOTFOUND) + goto cleanup; + error = note_write(out, repo, author, committer, notes_ref, - note, nparents ? &sha : NULL, target, - nparents, &commit); + note, tree, target, &commit); +cleanup: git__free(target); git_commit_free(commit); + git_tree_free(tree); return error; } @@ -388,37 +473,23 @@ int git_note_remove(git_repository *repo, const char *notes_ref, const git_oid *oid) { int error; - char *target; - git_oid sha; - git_commit *commit; - git_reference *ref; - - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - error = git_reference_lookup(&ref, repo, notes_ref); - if (error < 0) - return error; - - assert(git_reference_type(ref) == GIT_REF_OID); - - git_oid_cpy(&sha, git_reference_oid(ref)); - git_reference_free(ref); - - error = git_commit_lookup(&commit, repo, &sha); - if (error < 0) - return error; - - git_oid_cpy(&sha, git_commit_tree_oid(commit)); + char *target = NULL; + git_commit *commit = NULL; + git_tree *tree = NULL; target = git_oid_allocfmt(oid); GITERR_CHECK_ALLOC(target); + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) + goto cleanup; + error = note_remove(repo, author, committer, notes_ref, - &sha, target, 1, &commit); + tree, target, &commit); +cleanup: git__free(target); git_commit_free(commit); + git_tree_free(tree); return error; } @@ -511,18 +582,12 @@ int git_note_foreach( void *payload) { int error = -1; - git_oid tree_oid; git_iterator *iter = NULL; git_tree *tree = NULL; + git_commit *commit = NULL; const git_index_entry *item; - if (normalize_namespace(¬es_ref, repo) < 0) - return -1; - - if ((error = retrieve_note_tree_oid(&tree_oid, repo, notes_ref)) < 0) - goto cleanup; - - if (git_tree_lookup(&tree, repo, &tree_oid) < 0) + if ((error = retrieve_note_tree_and_commit(&tree, &commit, repo, ¬es_ref)) < 0) goto cleanup; if (git_iterator_for_tree(&iter, repo, tree) < 0) @@ -544,5 +609,6 @@ int git_note_foreach( cleanup: git_iterator_free(iter); git_tree_free(tree); + git_commit_free(commit); return error; } @@ -17,6 +17,10 @@ #include <stdio.h> #include <ctype.h> +#ifdef GIT_WIN32 +#define LOOKS_LIKE_DRIVE_PREFIX(S) (git__isalpha((S)[0]) && (S)[1] == ':') +#endif + /* * Based on the Android implementation, BSD licensed. * Check http://android.git.kernel.org/ @@ -105,7 +109,7 @@ int git_path_dirname_r(git_buf *buffer, const char *path) /* Mimic unix behavior where '/.git' returns '/': 'C:/.git' will return 'C:/' here */ - if (len == 2 && isalpha(path[0]) && path[1] == ':') { + if (len == 2 && LOOKS_LIKE_DRIVE_PREFIX(path)) { len = 3; goto Exit; } @@ -170,7 +174,7 @@ int git_path_root(const char *path) #ifdef GIT_WIN32 /* Does the root of the path look like a windows drive ? */ - if (isalpha(path[0]) && (path[1] == ':')) + if (LOOKS_LIKE_DRIVE_PREFIX(path)) offset += 2; /* Are we dealing with a windows network path? */ @@ -210,7 +214,7 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base) giterr_set(GITERR_OS, "Failed to resolve path '%s'", path); git_buf_clear(path_out); - + return error; } @@ -464,19 +468,24 @@ int git_path_cmp( const char *name1, size_t len1, int isdir1, const char *name2, size_t len2, int isdir2) { + unsigned char c1, c2; size_t len = len1 < len2 ? len1 : len2; int cmp; cmp = memcmp(name1, name2, len); if (cmp) return cmp; - if (len1 < len2) - return (!isdir1 && !isdir2) ? -1 : - (isdir1 ? '/' - name2[len1] : name2[len1] - '/'); - if (len1 > len2) - return (!isdir1 && !isdir2) ? 1 : - (isdir2 ? name1[len2] - '/' : '/' - name1[len2]); - return 0; + + c1 = name1[len]; + c2 = name2[len]; + + if (c1 == '\0' && isdir1) + c1 = '/'; + + if (c2 == '\0' && isdir2) + c2 = '/'; + + return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; } /* Taken from git.git */ diff --git a/src/posix.h b/src/posix.h index d020d94a..5799c049 100644 --- a/src/posix.h +++ b/src/posix.h @@ -59,9 +59,18 @@ extern int p_rename(const char *from, const char *to); typedef int GIT_SOCKET; #define INVALID_SOCKET -1 +#define p_localtime_r localtime_r +#define p_gmtime_r gmtime_r +#define p_gettimeofday gettimeofday + #else typedef SOCKET GIT_SOCKET; +struct timezone; +extern struct tm * p_localtime_r (const time_t *timer, struct tm *result); +extern struct tm * p_gmtime_r (const time_t *timer, struct tm *result); +extern int p_gettimeofday(struct timeval *tv, struct timezone *tz); + #endif @@ -1705,3 +1705,62 @@ int git_reference_cmp(git_reference *ref1, git_reference *ref2) return git_oid_cmp(&ref1->target.oid, &ref2->target.oid); } +/* Update the reference named `ref_name` so it points to `oid` */ +int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name) +{ + git_reference *ref; + int res; + + res = git_reference_lookup(&ref, repo, ref_name); + + /* If we haven't found the reference at all, we assume we need to create + * a new reference and that's it */ + if (res == GIT_ENOTFOUND) { + giterr_clear(); + return git_reference_create_oid(NULL, repo, ref_name, oid, 1); + } + + if (res < 0) + return -1; + + /* If we have found a reference, but it's symbolic, we need to update + * the direct reference it points to */ + if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { + git_reference *aux; + const char *sym_target; + + /* The target pointed at by this reference */ + sym_target = git_reference_target(ref); + + /* resolve the reference to the target it points to */ + res = git_reference_resolve(&aux, ref); + + /* + * if the symbolic reference pointed to an inexisting ref, + * this is means we're creating a new branch, for example. + * We need to create a new direct reference with that name + */ + if (res == GIT_ENOTFOUND) { + giterr_clear(); + res = git_reference_create_oid(NULL, repo, sym_target, oid, 1); + git_reference_free(ref); + return res; + } + + /* free the original symbolic reference now; not before because + * we're using the `sym_target` pointer */ + git_reference_free(ref); + + if (res < 0) + return -1; + + /* store the newly found direct reference in its place */ + ref = aux; + } + + /* ref is made to point to `oid`: ref is either the original reference, + * or the target of the symbolic reference we've looked up */ + res = git_reference_set_oid(ref, oid); + git_reference_free(ref); + return res; +} @@ -54,6 +54,7 @@ void git_repository__refcache_free(git_refcache *refs); int git_reference__normalize_name(char *buffer_out, size_t out_size, const char *name); int git_reference__normalize_name_oid(char *buffer_out, size_t out_size, const char *name); +int git_reference__update(git_repository *repo, const git_oid *oid, const char *ref_name); /** * Lookup a reference by name and try to resolve to an OID. diff --git a/src/remote.c b/src/remote.c index 8d607610..00e108a0 100644 --- a/src/remote.c +++ b/src/remote.c @@ -337,13 +337,16 @@ int git_remote_update_tips(git_remote *remote, int (*cb)(const char *refname, co unsigned int i = 0; git_buf refname = GIT_BUF_INIT; git_oid old; - git_vector *refs = &remote->refs; + git_vector *refs; git_remote_head *head; git_reference *ref; - struct git_refspec *spec = &remote->fetch; + struct git_refspec *spec; assert(remote); + refs = &remote->refs; + spec = &remote->fetch; + if (refs->length == 0) return 0; diff --git a/src/repository.c b/src/repository.c index 71817083..4e467e68 100644 --- a/src/repository.c +++ b/src/repository.c @@ -718,6 +718,9 @@ static int repo_init_config(const char *git_dir, bool is_bare, bool is_reinit) SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION); SET_REPO_CONFIG(bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path))); + if (!is_bare) + SET_REPO_CONFIG(bool, "core.logallrefupdates", true); + if (!is_reinit && is_filesystem_case_insensitive(git_dir)) SET_REPO_CONFIG(bool, "core.ignorecase", true); /* TODO: what other defaults? */ diff --git a/src/reset.c b/src/reset.c new file mode 100644 index 00000000..14f7a236 --- /dev/null +++ b/src/reset.c @@ -0,0 +1,103 @@ +/* + * 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 "common.h" +#include "commit.h" +#include "tag.h" +#include "git2/reset.h" + +#define ERROR_MSG "Cannot perform reset" + +static int reset_error_invalid(const char *msg) +{ + giterr_set(GITERR_INVALID, "%s - %s", ERROR_MSG, msg); + return -1; +} + +int git_reset( + git_repository *repo, + const git_object *target, + git_reset_type reset_type) +{ + git_otype target_type = GIT_OBJ_BAD; + git_object *commit = NULL; + git_index *index = NULL; + git_tree *tree = NULL; + int error = -1; + + assert(repo && target); + assert(reset_type == GIT_RESET_SOFT || reset_type == GIT_RESET_MIXED); + + if (git_object_owner(target) != repo) + return reset_error_invalid("The given target does not belong to this repository."); + + if (reset_type == GIT_RESET_MIXED && git_repository_is_bare(repo)) + return reset_error_invalid("Mixed reset is not allowed in a bare repository."); + + target_type = git_object_type(target); + + switch (target_type) + { + case GIT_OBJ_TAG: + if (git_tag_peel(&commit, (git_tag *)target) < 0) + goto cleanup; + + if (git_object_type(commit) != GIT_OBJ_COMMIT) { + reset_error_invalid("The given target does not resolve to a commit."); + goto cleanup; + } + break; + + case GIT_OBJ_COMMIT: + commit = (git_object *)target; + break; + + default: + return reset_error_invalid("Only git_tag and git_commit objects are valid targets."); + } + + //TODO: Check for unmerged entries + + if (git_reference__update(repo, git_object_id(commit), GIT_HEAD_FILE) < 0) + goto cleanup; + + if (reset_type == GIT_RESET_SOFT) { + error = 0; + goto cleanup; + } + + if (git_commit_tree(&tree, (git_commit *)commit) < 0) { + giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the commit tree.", ERROR_MSG); + goto cleanup; + } + + if (git_repository_index(&index, repo) < 0) { + giterr_set(GITERR_OBJECT, "%s - Failed to retrieve the index.", ERROR_MSG); + goto cleanup; + } + + if (git_index_read_tree(index, tree) < 0) { + giterr_set(GITERR_INDEX, "%s - Failed to update the index.", ERROR_MSG); + goto cleanup; + } + + if (git_index_write(index) < 0) { + giterr_set(GITERR_INDEX, "%s - Failed to write the index.", ERROR_MSG); + goto cleanup; + } + + error = 0; + +cleanup: + if (target_type == GIT_OBJ_TAG) + git_object_free(commit); + + git_index_free(index); + git_tree_free(tree); + + return error; +} diff --git a/src/revparse.c b/src/revparse.c new file mode 100644 index 00000000..c66a9852 --- /dev/null +++ b/src/revparse.c @@ -0,0 +1,748 @@ +/* + * 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 <assert.h> + +#include "common.h" +#include "buffer.h" +#include "tree.h" + +#include "git2.h" + +typedef enum { + 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) +{ + giterr_set(GITERR_INVALID, "Refspec '%s' is not valid.", spec); +} + +static int revparse_lookup_fully_qualifed_ref(git_object **out, git_repository *repo, const char*spec) +{ + git_oid resolved; + + if (git_reference_name_to_oid(&resolved, repo, spec) < 0) + return GIT_ERROR; + + 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_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; +} + +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; + + /* "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; + } + + /* 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; + } + } + + /* Fully-named ref */ + if (!revparse_lookup_fully_qualifed_ref(&obj, repo, spec)) { + *out = obj; + return 0; + } + + /* 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 (!revparse_lookup_fully_qualifed_ref(&obj, repo, git_buf_cstr(&refnamebuf))) { + git_buf_free(&refnamebuf); + *out = obj; + return 0; + } + } + git_buf_free(&refnamebuf); + + giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); + return GIT_ERROR; +} + + +static int all_chars_are_digits(const char *str, size_t len) +{ + size_t i=0; + for (i=0; i<len; i++) { + if (str[i] < '0' || str[i] > '9') return 0; + } + return 1; +} + +static void normalize_maybe_empty_refname(git_buf *buf, git_repository *repo, const char *refspec, size_t refspeclen) +{ + git_reference *ref; + + 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); + } + } +} + +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); + + if (git__prefixcmp(reflogspec, "@{") != 0 || + git__suffixcmp(reflogspec, "}") != 0) { + giterr_set(GITERR_INVALID, "Bad reflogspec '%s'", reflogspec); + return GIT_ERROR; + } + + /* "@{-N}" form means walk back N checkouts. That means the HEAD log. */ + if (refspeclen == 0 && !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 (!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]; + + refloglen = git_reflog_entrycount(reflog); + for (i=refloglen-1; i >= 0; i--) { + const char *msg; + entry = git_reflog_entry_byindex(reflog, i); + + 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; + + git_buf_put(&datebuf, reflogspec+2, reflogspeclen-3); + date_error = git__date_parse(×tamp, git_buf_cstr(&datebuf)); + + /* @{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); + } + } + + /* @{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); + + 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); + } + } + + else if (!date_error) { + /* Ref as it was on a certain date */ + normalize_maybe_empty_refname(&buf, repo, refspec, refspeclen); + + 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; + + /* TODO: clunky. Factor "now" into a utility */ + git_signature *sig; + git_time as_of; + + git_signature_now(&sig, "blah", "blah"); + as_of = sig->when; + git_signature_free(sig); + + as_of.time = (timestamp > 0) + ? timestamp + : sig->when.time + timestamp; + + 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; + } + } + + 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); + } + + git_reflog_free(reflog); + } + + git_reference_free(ref); + } + } + + git_buf_free(&datebuf); + } + + if (reflog) git_reflog_free(reflog); + git_buf_free(&buf); + return retcode; +} + +static git_object* dereference_object(git_object *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; + + 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; +} + +static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type) +{ + int retcode = 1; + git_object *obj1 = obj, *obj2 = obj; + + 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; +} + +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; +} + +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; + + if (*movement == '{') { + if (movement[movementlen-1] != '}') { + set_invalid_syntax_err(movement); + return GIT_ERROR; + } + + /* {} -> 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; + + 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); + + 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); + } + + 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 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; + + /* "^0" just returns the input */ + if (n == 0) { + *out = obj; + return 0; + } + + if (git_commit_parent(&commit, commit, n-1) < 0) { + return GIT_ERROR; + } + + *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; + + /* 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 { + git__strtol32(&n, movement, NULL, 0); + } + commit1 = (git_commit*)obj; + + /* "~0" just returns the input */ + if (n == 0) { + *out = obj; + return 0; + } + + for (i=0; i<n; i++) { + if (git_commit_parent(&commit2, commit1, 0) < 0) { + return GIT_ERROR; + } + if (commit1 != (git_commit*)obj) { + git_commit_free(commit1); + } + commit1 = commit2; + } + + *out = (git_object*)commit1; + return 0; +} + +static int oid_for_tree_path(git_oid *out, git_tree *tree, git_repository *repo, const char *path) +{ + char *str = git__strdup(path); + char *tok; + void *alloc = str; + git_tree *tree2 = tree; + const git_tree_entry *entry; + + while ((tok = git__strtok(&str, "/\\")) != NULL) { + entry = git_tree_entry_byname(tree2, tok); + if (tree2 != tree) git_tree_free(tree2); + if (git_tree_entry__is_tree(entry)) { + if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) { + git__free(alloc); + return GIT_ERROR; + } + } + } + + git_oid_cpy(out, git_tree_entry_id(entry)); + git__free(alloc); + return 0; +} + +static int handle_colon_syntax(git_object **out, + git_repository *repo, + git_object *obj, + const char *path) +{ + git_tree *tree; + git_oid oid; + + /* Dereference until we reach a tree. */ + if (dereference_to_type(&obj, obj, GIT_OBJ_TREE) < 0) { + return GIT_ERROR; + } + tree = (git_tree*)obj; + + /* Find the blob at the given path. */ + oid_for_tree_path(&oid, tree, repo, path); + git_tree_free(tree); + return git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); +} + +static int revparse_global_grep(git_object **out, git_repository *repo, const char *pattern) +{ + git_revwalk *walk; + int retcode = 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; + + 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); + } else { + *out = resultobj; + } + regfree(&preg); + git_revwalk_free(walk); + } + } + + 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; + + 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; + } + + 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; + + + 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_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_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; + } + } + + 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; +} diff --git a/src/revwalk.c b/src/revwalk.c index 719361c5..13d54b72 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -172,14 +172,23 @@ static commit_object *commit_lookup(git_revwalk *walk, const git_oid *oid) return commit; } +static int commit_error(commit_object *commit, const char *msg) +{ + char commit_oid[GIT_OID_HEXSZ + 1]; + git_oid_fmt(commit_oid, &commit->oid); + commit_oid[GIT_OID_HEXSZ] = '\0'; + + giterr_set(GITERR_ODB, "Failed to parse commit %s - %s", commit_oid, msg); + + return -1; +} + static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawobj *raw) { 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; - int i, parents = 0; int commit_time; @@ -210,21 +219,18 @@ static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawo commit->out_degree = (unsigned short)parents; - if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) { - giterr_set(GITERR_ODB, "Failed to parse commit. Object is corrupted"); - return -1; - } + if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) + return commit_error(commit, "object is corrupted"); - buffer = memchr(buffer, '>', buffer_end - buffer); - if (buffer == NULL) { - giterr_set(GITERR_ODB, "Failed to parse commit. Can't find author"); - return -1; - } + if ((buffer = memchr(buffer, '<', buffer_end - buffer)) == NULL || + (buffer = memchr(buffer, '>', buffer_end - buffer)) == NULL) + return commit_error(commit, "malformed author information"); - if (git__strtol32(&commit_time, (char *)buffer + 2, NULL, 10) < 0) { - giterr_set(GITERR_ODB, "Failed to parse commit. Can't parse commit time"); - return -1; - } + while (*buffer == '>' || git__isspace(*buffer)) + buffer++; + + if (git__strtol32(&commit_time, (char *)buffer, NULL, 10) < 0) + return commit_error(commit, "cannot parse commit time"); commit->time = (time_t)commit_time; commit->parsed = 1; diff --git a/src/signature.c b/src/signature.c index 7d329c4c..332bdf65 100644 --- a/src/signature.c +++ b/src/signature.c @@ -113,26 +113,14 @@ int git_signature_now(git_signature **sig_out, const char *name, const char *ema time_t offset; struct tm *utc_tm, *local_tm; git_signature *sig; - -#ifndef GIT_WIN32 struct tm _utc, _local; -#endif *sig_out = NULL; time(&now); - /** - * On Win32, `gmtime_r` doesn't exist but - * `gmtime` is threadsafe, so we can use that - */ -#ifdef GIT_WIN32 - utc_tm = gmtime(&now); - local_tm = localtime(&now); -#else - utc_tm = gmtime_r(&now, &_utc); - local_tm = localtime_r(&now, &_local); -#endif + utc_tm = p_gmtime_r(&now, &_utc); + local_tm = p_localtime_r(&now, &_local); offset = mktime(local_tm) - mktime(utc_tm); offset /= 60; diff --git a/src/tree-cache.c b/src/tree-cache.c index ebc2c680..8d186d2f 100644 --- a/src/tree-cache.c +++ b/src/tree-cache.c @@ -69,7 +69,7 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char return NULL; } - if (end == NULL || end + 1 == '\0') + if (end == NULL || *end + 1 == '\0') return tree; ptr = end + 1; @@ -22,7 +22,7 @@ static int valid_attributes(const int attributes) static int valid_entry_name(const char *filename) { - return strlen(filename) > 0 && strchr(filename, '/') == NULL; + return *filename != '\0' && strchr(filename, '/') == NULL; } static int entry_sort_cmp(const void *a, const void *b) @@ -634,6 +634,9 @@ void git_treebuilder_clear(git_treebuilder *bld) void git_treebuilder_free(git_treebuilder *bld) { + if (bld == NULL) + return; + git_treebuilder_clear(bld); git_vector_free(&bld->entries); git__free(bld); @@ -179,7 +179,7 @@ void git__strtolower(char *str) int git__prefixcmp(const char *str, const char *prefix) { for (;;) { - char p = *(prefix++), s; + unsigned char p = *(prefix++), s; if (!p) return 0; if ((s = *(str++)) != p) @@ -209,6 +209,13 @@ GIT_INLINE(bool) git__isspace(int c) return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v'); } +GIT_INLINE(int) git__time_cmp(const git_time *a, const git_time *b) +{ + /* 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); +} + GIT_INLINE(bool) git__iswildcard(int c) { return (c == '*' || c == '?' || c == '['); @@ -223,4 +230,14 @@ GIT_INLINE(bool) git__iswildcard(int c) */ extern int git__parse_bool(int *out, const char *value); +/* + * Parse a string into a value as a git_time_t. + * + * Sample valid input: + * - "yesterday" + * - "July 17, 2003" + * - "2003-7-17 08:23" + */ +int git__date_parse(git_time_t *out, const char *date); + #endif /* INCLUDE_util_h__ */ diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 10de70da..37956af8 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -414,7 +414,7 @@ int p_mkstemp(char *tmp_path) return -1; #endif - return p_creat(tmp_path, 0744); + return p_creat(tmp_path, 0744); //-V536 } int p_setenv(const char* name, const char* value, int overwrite) @@ -470,3 +470,79 @@ int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) return send(socket, buffer, (int)length, flags); } + +/** + * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html + * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that + */ +struct tm * +p_localtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = localtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} +struct tm * +p_gmtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = gmtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int p_gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} diff --git a/src/win32/precompiled.c b/src/win32/precompiled.c new file mode 100644 index 00000000..c08ca1f1 --- /dev/null +++ b/src/win32/precompiled.c @@ -0,0 +1 @@ +#include "precompiled.h"
\ No newline at end of file diff --git a/src/win32/precompiled.h b/src/win32/precompiled.h new file mode 100644 index 00000000..5de7e6f3 --- /dev/null +++ b/src/win32/precompiled.h @@ -0,0 +1,19 @@ +#include "git2.h" + +#include <assert.h> +#include <errno.h> +#include <limits.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#include <regex.h> + +#include <io.h> +#include <direct.h> +#ifdef GIT_THREADS + #include "win32/pthread.h" +#endif diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c index d180285b..1275d162 100644 --- a/tests-clar/clar_helpers.c +++ b/tests-clar/clar_helpers.c @@ -48,6 +48,62 @@ void cl_git_rewritefile(const char *filename, const char *new_content) cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC); } +#ifdef GIT_WIN32 + +#include "win32/utf-conv.h" + +char *cl_getenv(const char *name) +{ + wchar_t *name_utf16 = gitwin_to_utf16(name); + DWORD value_len, alloc_len; + wchar_t *value_utf16; + char *value_utf8; + + cl_assert(name_utf16); + alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0); + if (alloc_len <= 0) + return NULL; + + cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t))); + + value_len = GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len); + cl_assert_equal_i(value_len, alloc_len - 1); + + cl_assert(value_utf8 = gitwin_from_utf16(value_utf16)); + + git__free(value_utf16); + + return value_utf8; +} + +int cl_setenv(const char *name, const char *value) +{ + wchar_t *name_utf16 = gitwin_to_utf16(name); + wchar_t *value_utf16 = value ? gitwin_to_utf16(value) : NULL; + + cl_assert(name_utf16); + cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16)); + + git__free(name_utf16); + git__free(value_utf16); + + return 0; + +} +#else + +#include <stdlib.h> +char *cl_getenv(const char *name) +{ + return getenv(name); +} + +int cl_setenv(const char *name, const char *value) +{ + return (value == NULL) ? unsetenv(name) : setenv(name, value, 1); +} +#endif + static const char *_cl_sandbox = NULL; static git_repository *_cl_repo = NULL; @@ -98,3 +154,28 @@ void cl_git_sandbox_cleanup(void) _cl_sandbox = NULL; } } + +bool cl_toggle_filemode(const char *filename) +{ + struct stat st1, st2; + + cl_must_pass(p_stat(filename, &st1)); + cl_must_pass(p_chmod(filename, st1.st_mode ^ 0100)); + cl_must_pass(p_stat(filename, &st2)); + + return (st1.st_mode != st2.st_mode); +} + +bool cl_is_chmod_supported(void) +{ + static int _is_supported = -1; + + if (_is_supported < 0) { + cl_git_mkfile("filemode.t", "Test if filemode can be modified"); + _is_supported = cl_toggle_filemode("filemode.t"); + cl_must_pass(p_unlink("filemode.t")); + } + + return _is_supported; +} + diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h index d250494f..a3b03bbb 100644 --- a/tests-clar/clar_libgit2.h +++ b/tests-clar/clar_libgit2.h @@ -40,6 +40,13 @@ void cl_git_append2file(const char *filename, const char *new_content); void cl_git_rewritefile(const char *filename, const char *new_content); void cl_git_write2file(const char *filename, const char *new_content, int mode); +bool cl_toggle_filemode(const char *filename); +bool cl_is_chmod_supported(void); + +/* Environment wrappers */ +char *cl_getenv(const char *name); +int cl_setenv(const char *name, const char *value); + /* Git sandbox setup helpers */ git_repository *cl_git_sandbox_init(const char *sandbox); diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c index 15d431f0..9361341e 100644 --- a/tests-clar/core/env.c +++ b/tests-clar/core/env.c @@ -3,59 +3,6 @@ #include "path.h" #ifdef GIT_WIN32 - -#include "win32/utf-conv.h" - -static char *cl_getenv(const char *name) -{ - wchar_t *name_utf16 = gitwin_to_utf16(name); - DWORD value_len, alloc_len; - wchar_t *value_utf16; - char *value_utf8; - - cl_assert(name_utf16); - alloc_len = GetEnvironmentVariableW(name_utf16, NULL, 0); - if (alloc_len <= 0) - return NULL; - - cl_assert(value_utf16 = git__calloc(alloc_len, sizeof(wchar_t))); - - value_len = GetEnvironmentVariableW(name_utf16, value_utf16, alloc_len); - cl_assert_equal_i(value_len, alloc_len - 1); - - cl_assert(value_utf8 = gitwin_from_utf16(value_utf16)); - - git__free(value_utf16); - - return value_utf8; -} - -static int cl_setenv(const char *name, const char *value) -{ - wchar_t *name_utf16 = gitwin_to_utf16(name); - wchar_t *value_utf16 = value ? gitwin_to_utf16(value) : NULL; - - cl_assert(name_utf16); - cl_assert(SetEnvironmentVariableW(name_utf16, value_utf16)); - - git__free(name_utf16); - git__free(value_utf16); - - return 0; - -} -#else - -#include <stdlib.h> -#define cl_getenv(n) getenv(n) - -static int cl_setenv(const char *name, const char *value) -{ - return (value == NULL) ? unsetenv(name) : setenv(name, value, 1); -} -#endif - -#ifdef GIT_WIN32 static char *env_userprofile = NULL; static char *env_programfiles = NULL; #else diff --git a/tests-clar/core/errors.c b/tests-clar/core/errors.c index 0be3e7ac..10c0cdd3 100644 --- a/tests-clar/core/errors.c +++ b/tests-clar/core/errors.c @@ -31,7 +31,7 @@ void test_core_errors__new_school(void) do { struct stat st; memset(&st, 0, sizeof(st)); - assert(p_lstat("this_file_does_not_exist", &st) < 0); + cl_assert(p_lstat("this_file_does_not_exist", &st) < 0); GIT_UNUSED(st); } while (false); giterr_set(GITERR_OS, "stat failed"); /* internal fn */ diff --git a/tests-clar/core/filebuf.c b/tests-clar/core/filebuf.c index eab8a26e..4451c01c 100644 --- a/tests-clar/core/filebuf.c +++ b/tests-clar/core/filebuf.c @@ -8,7 +8,7 @@ void test_core_filebuf__0(void) int fd; char test[] = "test", testlock[] = "test.lock"; - fd = p_creat(testlock, 0744); + fd = p_creat(testlock, 0744); //-V536 cl_must_pass(fd); cl_must_pass(p_close(fd)); @@ -27,7 +27,7 @@ void test_core_filebuf__1(void) int fd; char test[] = "test"; - fd = p_creat(test, 0666); + fd = p_creat(test, 0666); //-V536 cl_must_pass(fd); cl_must_pass(p_write(fd, "libgit2 rocks\n", 14)); cl_must_pass(p_close(fd)); diff --git a/tests-clar/date/date.c b/tests-clar/date/date.c new file mode 100644 index 00000000..88881d1e --- /dev/null +++ b/tests-clar/date/date.c @@ -0,0 +1,15 @@ +#include "clar_libgit2.h" + +#include "util.h" + +void test_date_date__overflow(void) +{ +#ifdef __LP64__ + git_time_t d2038, d2039; + + /* This is expected to fail on a 32-bit machine. */ + cl_git_pass(git__date_parse(&d2038, "2038-1-1")); + cl_git_pass(git__date_parse(&d2039, "2039-1-1")); + cl_assert(d2038 < d2039); +#endif +} diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 8587be9b..1d9f6121 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -85,11 +85,16 @@ int diff_line_fn( e->line_ctxt++; break; case GIT_DIFF_LINE_ADDITION: - case GIT_DIFF_LINE_ADD_EOFNL: e->line_adds++; break; + case GIT_DIFF_LINE_ADD_EOFNL: + assert(0); + break; case GIT_DIFF_LINE_DELETION: + e->line_dels++; + break; case GIT_DIFF_LINE_DEL_EOFNL: + /* technically not a line delete, but we'll count it as such */ e->line_dels++; break; default: diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c index be29bea6..eee84810 100644 --- a/tests-clar/diff/iterator.c +++ b/tests-clar/diff/iterator.c @@ -474,13 +474,14 @@ static const char *status_paths[] = { "subdir/current_file", "subdir/modified_file", "subdir/new_file", + "\xe8\xbf\x99", NULL }; void test_diff_iterator__workdir_1(void) { workdir_iterator_test( - "status", NULL, NULL, 12, 1, status_paths, "ignored_file"); + "status", NULL, NULL, 13, 1, status_paths, "ignored_file"); } static const char *status_paths_range_0[] = { @@ -527,13 +528,14 @@ static const char *status_paths_range_4[] = { "subdir/current_file", "subdir/modified_file", "subdir/new_file", + "\xe8\xbf\x99", NULL }; void test_diff_iterator__workdir_1_ranged_4(void) { workdir_iterator_test( - "status", "subdir/", NULL, 3, 0, status_paths_range_4, NULL); + "status", "subdir/", NULL, 4, 0, status_paths_range_4, NULL); } static const char *status_paths_range_5[] = { @@ -551,7 +553,7 @@ void test_diff_iterator__workdir_1_ranged_5(void) void test_diff_iterator__workdir_1_ranged_empty_0(void) { workdir_iterator_test( - "status", "z_does_not_exist", NULL, + "status", "\xff_does_not_exist", NULL, 0, 0, NULL, NULL); } diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index b932fa10..4201ad2a 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -116,7 +116,7 @@ void test_diff_tree__options(void) { 5, 3, 0, 2, 0, 0, 0, 4, 0, 0, 51, 2, 46, 3 }, { 5, 3, 0, 2, 0, 0, 0, 4, 0, 0, 53, 4, 46, 3 }, { 5, 0, 3, 2, 0, 0, 0, 4, 0, 0, 52, 3, 3, 46 }, - { 5, 3, 0, 2, 0, 0, 0, 5, 0, 0, 54, 3, 48, 3 }, + { 5, 3, 0, 2, 0, 0, 0, 5, 0, 0, 54, 3, 47, 4 }, /* c vs d tests */ { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 22, 9, 10, 3 }, { 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 19, 12, 7, 0 }, diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 1ea1af86..0c17eeb4 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -5,7 +5,6 @@ static git_repository *g_repo = NULL; void test_diff_workdir__initialize(void) { - g_repo = cl_git_sandbox_init("status"); } void test_diff_workdir__cleanup(void) @@ -19,6 +18,8 @@ void test_diff_workdir__to_index(void) git_diff_list *diff = NULL; diff_expects exp; + g_repo = cl_git_sandbox_init("status"); + opts.context_lines = 3; opts.interhunk_lines = 1; opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; @@ -37,12 +38,12 @@ void test_diff_workdir__to_index(void) * - git diff * - mv .git .gitted */ - cl_assert_equal_i(12, exp.files); + cl_assert_equal_i(13, exp.files); cl_assert_equal_i(0, exp.file_adds); cl_assert_equal_i(4, exp.file_dels); cl_assert_equal_i(4, exp.file_mods); cl_assert_equal_i(1, exp.file_ignored); - cl_assert_equal_i(3, exp.file_untracked); + cl_assert_equal_i(4, exp.file_untracked); cl_assert_equal_i(8, exp.hunks); @@ -59,13 +60,17 @@ void test_diff_workdir__to_tree(void) /* grabbed a couple of commit oids from the history of the attr repo */ const char *a_commit = "26a125ee1bf"; /* the current HEAD */ const char *b_commit = "0017bd4ab1ec3"; /* the start */ - git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); - git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); + git_tree *a, *b; git_diff_options opts = {0}; git_diff_list *diff = NULL; git_diff_list *diff2 = NULL; diff_expects exp; + g_repo = cl_git_sandbox_init("status"); + + a = resolve_commit_oid_to_tree(g_repo, a_commit); + b = resolve_commit_oid_to_tree(g_repo, b_commit); + opts.context_lines = 3; opts.interhunk_lines = 1; opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; @@ -87,12 +92,12 @@ void test_diff_workdir__to_tree(void) cl_git_pass(git_diff_foreach( diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); - cl_assert(exp.files == 13); - cl_assert(exp.file_adds == 0); - cl_assert(exp.file_dels == 4); - cl_assert(exp.file_mods == 4); - cl_assert(exp.file_ignored == 1); - cl_assert(exp.file_untracked == 4); + cl_assert_equal_i(14, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(4, exp.file_dels); + cl_assert_equal_i(4, exp.file_mods); + cl_assert_equal_i(1, exp.file_ignored); + cl_assert_equal_i(5, exp.file_untracked); /* Since there is no git diff equivalent, let's just assume that the * text diffs produced by git_diff_foreach are accurate here. We will @@ -115,19 +120,19 @@ void test_diff_workdir__to_tree(void) cl_git_pass(git_diff_foreach( diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); - cl_assert(exp.files == 14); - cl_assert(exp.file_adds == 2); - cl_assert(exp.file_dels == 5); - cl_assert(exp.file_mods == 4); - cl_assert(exp.file_ignored == 1); - cl_assert(exp.file_untracked == 2); + cl_assert_equal_i(15, exp.files); + cl_assert_equal_i(2, exp.file_adds); + cl_assert_equal_i(5, exp.file_dels); + cl_assert_equal_i(4, exp.file_mods); + cl_assert_equal_i(1, exp.file_ignored); + cl_assert_equal_i(3, exp.file_untracked); - cl_assert(exp.hunks == 11); + cl_assert_equal_i(11, exp.hunks); - cl_assert(exp.lines == 17); - cl_assert(exp.line_ctxt == 4); - cl_assert(exp.line_adds == 8); - cl_assert(exp.line_dels == 5); + cl_assert_equal_i(17, exp.lines); + cl_assert_equal_i(4, exp.line_ctxt); + cl_assert_equal_i(8, exp.line_adds); + cl_assert_equal_i(5, exp.line_dels); git_diff_list_free(diff); diff = NULL; @@ -144,19 +149,19 @@ void test_diff_workdir__to_tree(void) cl_git_pass(git_diff_foreach( diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); - cl_assert(exp.files == 15); - cl_assert(exp.file_adds == 5); - cl_assert(exp.file_dels == 4); - cl_assert(exp.file_mods == 3); - cl_assert(exp.file_ignored == 1); - cl_assert(exp.file_untracked == 2); + cl_assert_equal_i(16, exp.files); + cl_assert_equal_i(5, exp.file_adds); + cl_assert_equal_i(4, exp.file_dels); + cl_assert_equal_i(3, exp.file_mods); + cl_assert_equal_i(1, exp.file_ignored); + cl_assert_equal_i(3, exp.file_untracked); - cl_assert(exp.hunks == 12); + cl_assert_equal_i(12, exp.hunks); - cl_assert(exp.lines == 19); - cl_assert(exp.line_ctxt == 3); - cl_assert(exp.line_adds == 12); - cl_assert(exp.line_dels == 4); + cl_assert_equal_i(19, exp.lines); + cl_assert_equal_i(3, exp.line_ctxt); + cl_assert_equal_i(12, exp.line_adds); + cl_assert_equal_i(4, exp.line_dels); git_diff_list_free(diff); @@ -171,6 +176,8 @@ void test_diff_workdir__to_index_with_pathspec(void) diff_expects exp; char *pathspec = NULL; + g_repo = cl_git_sandbox_init("status"); + opts.context_lines = 3; opts.interhunk_lines = 1; opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; @@ -182,12 +189,12 @@ void test_diff_workdir__to_index_with_pathspec(void) cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); cl_git_pass(git_diff_foreach(diff, &exp, diff_file_fn, NULL, NULL)); - cl_assert_equal_i(12, exp.files); + cl_assert_equal_i(13, exp.files); cl_assert_equal_i(0, exp.file_adds); cl_assert_equal_i(4, exp.file_dels); cl_assert_equal_i(4, exp.file_mods); cl_assert_equal_i(1, exp.file_ignored); - cl_assert_equal_i(3, exp.file_untracked); + cl_assert_equal_i(4, exp.file_untracked); git_diff_list_free(diff); @@ -237,6 +244,241 @@ void test_diff_workdir__to_index_with_pathspec(void) git_diff_list_free(diff); } +void test_diff_workdir__filemode_changes(void) +{ + git_config *cfg; + git_diff_list *diff = NULL; + diff_expects exp; + + if (!cl_is_chmod_supported()) + return; + + g_repo = cl_git_sandbox_init("issue_592"); + + cl_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); + + /* test once with no mods */ + + cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + + cl_assert_equal_i(0, exp.files); + cl_assert_equal_i(0, exp.file_mods); + cl_assert_equal_i(0, exp.hunks); + + git_diff_list_free(diff); + + /* chmod file and test again */ + + cl_assert(cl_toggle_filemode("issue_592/a.txt")); + + cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + + cl_assert_equal_i(1, exp.files); + cl_assert_equal_i(1, exp.file_mods); + cl_assert_equal_i(0, exp.hunks); + + git_diff_list_free(diff); + + cl_assert(cl_toggle_filemode("issue_592/a.txt")); + git_config_free(cfg); +} + +void test_diff_workdir__filemode_changes_with_filemode_false(void) +{ + git_config *cfg; + git_diff_list *diff = NULL; + diff_expects exp; + + if (!cl_is_chmod_supported()) + return; + + g_repo = cl_git_sandbox_init("issue_592"); + + cl_git_pass(git_repository_config(&cfg, g_repo)); + cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); + + /* test once with no mods */ + + cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + + cl_assert_equal_i(0, exp.files); + cl_assert_equal_i(0, exp.file_mods); + cl_assert_equal_i(0, exp.hunks); + + git_diff_list_free(diff); + + /* chmod file and test again */ + + cl_assert(cl_toggle_filemode("issue_592/a.txt")); + + cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + + cl_assert_equal_i(0, exp.files); + cl_assert_equal_i(0, exp.file_mods); + cl_assert_equal_i(0, exp.hunks); + + git_diff_list_free(diff); + + cl_assert(cl_toggle_filemode("issue_592/a.txt")); + git_config_free(cfg); +} + +void test_diff_workdir__head_index_and_workdir_all_differ(void) +{ + git_diff_options opts = {0}; + git_diff_list *diff_i2t = NULL, *diff_w2i = NULL; + diff_expects exp; + char *pathspec = "staged_changes_modified_file"; + git_tree *tree; + + /* For this file, + * - head->index diff has 1 line of context, 1 line of diff + * - index->workdir diff has 2 lines of context, 1 line of diff + * but + * - head->workdir diff has 1 line of context, 2 lines of diff + * Let's make sure the right one is returned from each fn. + */ + + g_repo = cl_git_sandbox_init("status"); + + tree = resolve_commit_oid_to_tree(g_repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); + + opts.pathspec.strings = &pathspec; + opts.pathspec.count = 1; + + cl_git_pass(git_diff_index_to_tree(g_repo, &opts, tree, &diff_i2t)); + cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff_w2i)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff_i2t, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + cl_assert_equal_i(1, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(0, exp.file_dels); + cl_assert_equal_i(1, exp.file_mods); + cl_assert_equal_i(1, exp.hunks); + cl_assert_equal_i(2, exp.lines); + cl_assert_equal_i(1, exp.line_ctxt); + cl_assert_equal_i(1, exp.line_adds); + cl_assert_equal_i(0, exp.line_dels); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff_w2i, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + cl_assert_equal_i(1, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(0, exp.file_dels); + cl_assert_equal_i(1, exp.file_mods); + cl_assert_equal_i(1, exp.hunks); + cl_assert_equal_i(3, exp.lines); + cl_assert_equal_i(2, exp.line_ctxt); + cl_assert_equal_i(1, exp.line_adds); + cl_assert_equal_i(0, exp.line_dels); + + cl_git_pass(git_diff_merge(diff_i2t, diff_w2i)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff_i2t, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + cl_assert_equal_i(1, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(0, exp.file_dels); + cl_assert_equal_i(1, exp.file_mods); + cl_assert_equal_i(1, exp.hunks); + cl_assert_equal_i(3, exp.lines); + cl_assert_equal_i(1, exp.line_ctxt); + cl_assert_equal_i(2, exp.line_adds); + cl_assert_equal_i(0, exp.line_dels); + + git_diff_list_free(diff_i2t); + git_diff_list_free(diff_w2i); +} + +void test_diff_workdir__eof_newline_changes(void) +{ + git_diff_options opts = {0}; + git_diff_list *diff = NULL; + diff_expects exp; + char *pathspec = "current_file"; + + g_repo = cl_git_sandbox_init("status"); + + opts.pathspec.strings = &pathspec; + opts.pathspec.count = 1; + + cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + cl_assert_equal_i(0, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(0, exp.file_dels); + cl_assert_equal_i(0, exp.file_mods); + cl_assert_equal_i(0, exp.hunks); + cl_assert_equal_i(0, exp.lines); + cl_assert_equal_i(0, exp.line_ctxt); + cl_assert_equal_i(0, exp.line_adds); + cl_assert_equal_i(0, exp.line_dels); + + git_diff_list_free(diff); + + cl_git_append2file("status/current_file", "\n"); + + cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + cl_assert_equal_i(1, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(0, exp.file_dels); + cl_assert_equal_i(1, exp.file_mods); + cl_assert_equal_i(1, exp.hunks); + cl_assert_equal_i(2, exp.lines); + cl_assert_equal_i(1, exp.line_ctxt); + cl_assert_equal_i(1, exp.line_adds); + cl_assert_equal_i(0, exp.line_dels); + + git_diff_list_free(diff); + + cl_git_rewritefile("status/current_file", "current_file"); + + cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_foreach( + diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); + cl_assert_equal_i(1, exp.files); + cl_assert_equal_i(0, exp.file_adds); + cl_assert_equal_i(0, exp.file_dels); + cl_assert_equal_i(1, exp.file_mods); + cl_assert_equal_i(1, exp.hunks); + cl_assert_equal_i(3, exp.lines); + cl_assert_equal_i(0, exp.line_ctxt); + cl_assert_equal_i(1, exp.line_adds); + cl_assert_equal_i(2, exp.line_dels); + + git_diff_list_free(diff); +} + /* PREPARATION OF TEST DATA * * Since there is no command line equivalent of git_diff_workdir_to_tree, diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c index 3436f8d1..a535d681 100644 --- a/tests-clar/index/tests.c +++ b/tests-clar/index/tests.c @@ -33,7 +33,7 @@ static void copy_file(const char *src, const char *dst) cl_git_pass(git_futils_readbuffer(&source_buf, src)); - dst_fd = git_futils_creat_withpath(dst, 0777, 0666); + dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536 if (dst_fd < 0) goto cleanup; diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c index 8cee7ce0..41922975 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(how_many_refs == 14); /* 1 HEAD + 6 heads + 1 lightweight tag + 3 annotated tags + 3 peeled target */ + cl_assert_equal_i(how_many_refs, 21); } void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void) @@ -121,7 +121,7 @@ void test_network_remotelocal__retrieve_advertised_references_from_spaced_reposi cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - cl_assert(how_many_refs == 14); /* 1 HEAD + 6 heads + 1 lightweight tag + 3 annotated tags + 3 peeled target */ + cl_assert_equal_i(how_many_refs, 21); git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ remote = NULL; diff --git a/tests-clar/notes/notes.c b/tests-clar/notes/notes.c index 5185f25e..5f7f5a9c 100644 --- a/tests-clar/notes/notes.c +++ b/tests-clar/notes/notes.c @@ -15,6 +15,16 @@ void test_notes_notes__cleanup(void) cl_git_sandbox_cleanup(); } +static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) { + git_blob *blob; + + cl_assert_equal_s(git_note_message(note), message); + cl_assert(!git_oid_cmp(git_note_oid(note), note_oid)); + + cl_git_pass(git_blob_lookup(&blob, _repo, note_oid)); + cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob)); +} + static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message) { git_oid oid; @@ -23,38 +33,6 @@ static void create_note(git_oid *note_oid, const char *canonical_namespace, cons cl_git_pass(git_note_create(note_oid, _repo, _sig, _sig, canonical_namespace, &oid, message)); } -void test_notes_notes__1(void) -{ - git_oid oid, note_oid; - static git_note *note; - static git_blob *blob; - - cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479")); - - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n")); - cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n")); - - cl_git_pass(git_note_read(¬e, _repo, NULL, &oid)); - - cl_assert_equal_s(git_note_message(note), "hello world\n"); - cl_assert(!git_oid_cmp(git_note_oid(note), ¬e_oid)); - - cl_git_pass(git_blob_lookup(&blob, _repo, ¬e_oid)); - cl_assert_equal_s(git_note_message(note), git_blob_rawcontent(blob)); - - cl_git_fail(git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n")); - cl_git_fail(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n")); - - cl_git_pass(git_note_remove(_repo, NULL, _sig, _sig, &oid)); - cl_git_pass(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid)); - - cl_git_fail(git_note_remove(_repo, NULL, _sig, _sig, ¬e_oid)); - cl_git_fail(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid)); - - git_note_free(note); - git_blob_free(blob); -} - static struct { const char *note_sha; const char *annotated_object_sha; @@ -131,3 +109,152 @@ void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_retur cl_assert_equal_i(0, retrieved_notes); } + +void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void) +{ + git_oid note_oid, target_oid; + git_note *note, *default_namespace_note; + const char *default_ref; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + cl_git_pass(git_note_default_ref(&default_ref, _repo)); + + create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); + + cl_git_pass(git_note_read(¬e, _repo, NULL, &target_oid)); + cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref, &target_oid)); + + assert_note_equal(note, "hello world\n", ¬e_oid); + assert_note_equal(default_namespace_note, "hello world\n", ¬e_oid); + + git_note_free(note); + git_note_free(default_namespace_note); +} + +void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void) +{ + git_oid note_oid, target_oid; + git_note *note; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + + create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n"); + + cl_git_pass(git_note_read(¬e, _repo, "refs/notes/some/namespace", &target_oid)); + + assert_note_equal(note, "hello world on a custom namespace\n", ¬e_oid); + + git_note_free(note); +} + +/* + * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479 + * 08b041783f40edfe12bb406c9c9a8a040177c125 + */ +void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void) +{ + int error; + git_oid note_oid, target_oid; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + + create_note(¬e_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); + error = git_note_create(¬e_oid, _repo, _sig, _sig, NULL, &target_oid, "hello world\n"); + cl_git_fail(error); + cl_assert_equal_i(GIT_EEXISTS, error); + + create_note(¬e_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n"); + error = git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello world\n"); + cl_git_fail(error); + cl_assert_equal_i(GIT_EEXISTS, error); +} + +static char *messages[] = { + "08c041783f40edfe12bb406c9c9a8a040177c125", + "96c45fbe09ab7445fc7c60fd8d17f32494399343", + "48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1", + "24c3eaafb681c3df668f9df96f58e7b8c756eb04", + "96ca1b6ccc7858ae94684777f85ac0e7447f7040", + "7ac2db4378a08bb244a427c357e0082ee0d57ac6", + "e6cba23dbf4ef84fe35e884f017f4e24dc228572", + "c8cf3462c7d8feba716deeb2ebe6583bd54589e2", + "39c16b9834c2d665ac5f68ad91dc5b933bad8549", + "f3c582b1397df6a664224ebbaf9d4cc952706597", + "29cec67037fe8e89977474988219016ae7f342a6", + "36c4cd238bf8e82e27b740e0741b025f2e8c79ab", + "f1c45a47c02e01d5a9a326f1d9f7f756373387f8", + "4aca84406f5daee34ab513a60717c8d7b1763ead", + "84ce167da452552f63ed8407b55d5ece4901845f", + NULL +}; + +#define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1 + +/* + * $ git ls-tree refs/notes/fanout + * 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84 + * + * $ git ls-tree 4b22b35 + * 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759 96 + * + * $ git ls-tree d71aab4 + * 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125 071c1b46c854b31185ea97743be6a8774479 + */ +void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void) +{ + size_t i; + git_oid note_oid, target_oid; + git_note *_note; + + cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + + for (i = 0; i < MESSAGES_COUNT; i++) { + cl_git_pass(git_note_create(¬e_oid, _repo, _sig, _sig, "refs/notes/fanout", &target_oid, messages[i])); + cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid)); + git_note_free(_note); + + git_oid_cpy(&target_oid, ¬e_oid); + } +} + +/* + * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479 + * 08b041783f40edfe12bb406c9c9a8a040177c125 + */ +void test_notes_notes__can_read_a_note_in_an_existing_fanout(void) +{ + git_oid note_oid, target_oid; + git_note *note; + + cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); + cl_git_pass(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid)); + + cl_git_pass(git_oid_fromstr(¬e_oid, "08b041783f40edfe12bb406c9c9a8a040177c125")); + cl_assert(!git_oid_cmp(git_note_oid(note), ¬e_oid)); + + git_note_free(note); +} + +void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void) +{ + git_oid target_oid; + git_note *note; + + cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); + cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid)); + + cl_git_fail(git_note_read(¬e, _repo, "refs/notes/fanout", &target_oid)); +} + +void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void) +{ + int error; + git_oid target_oid; + + cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479")); + cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid)); + + error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid); + cl_git_fail(error); + cl_assert_equal_i(GIT_ENOTFOUND, error); +} diff --git a/tests-clar/object/blob/fromchunks.c b/tests-clar/object/blob/fromchunks.c new file mode 100644 index 00000000..228e969b --- /dev/null +++ b/tests-clar/object/blob/fromchunks.c @@ -0,0 +1,87 @@ +#include "clar_libgit2.h" +#include "buffer.h" +#include "posix.h" +#include "path.h" +#include "fileops.h" + +static git_repository *repo; +static char textual_content[] = "libgit2\n\r\n\0"; + +void test_object_blob_fromchunks__initialize(void) +{ + repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_object_blob_fromchunks__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +static int text_chunked_source_cb(char *content, size_t max_length, void *payload) +{ + int *count; + + GIT_UNUSED(max_length); + + count = (int *)payload; + (*count)--; + + if (*count == 0) + return 0; + + strcpy(content, textual_content); + return strlen(textual_content); +} + +void test_object_blob_fromchunks__can_create_a_blob_from_a_in_memory_chunk_provider(void) +{ + git_oid expected_oid, oid; + git_object *blob; + int howmany = 7; + + cl_git_pass(git_oid_fromstr(&expected_oid, "321cbdf08803c744082332332838df6bd160f8f9")); + + cl_git_fail(git_object_lookup(&blob, repo, &expected_oid, GIT_OBJ_ANY)); + + cl_git_pass(git_blob_create_fromchunks(&oid, repo, NULL, text_chunked_source_cb, &howmany)); + + cl_git_pass(git_object_lookup(&blob, repo, &expected_oid, GIT_OBJ_ANY)); + git_object_free(blob); +} + +#define GITATTR "* text=auto\n" \ + "*.txt text\n" \ + "*.data binary\n" + +static void write_attributes(git_repository *repo) +{ + git_buf buf = GIT_BUF_INIT; + + cl_git_pass(git_buf_joinpath(&buf, git_repository_path(repo), "info")); + cl_git_pass(git_buf_joinpath(&buf, git_buf_cstr(&buf), "attributes")); + + cl_git_pass(git_futils_mkpath2file(git_buf_cstr(&buf), 0777)); + cl_git_rewritefile(git_buf_cstr(&buf), GITATTR); + + git_buf_free(&buf); +} + +static void assert_named_chunked_blob(const char *expected_sha, const char *fake_name) +{ + git_oid expected_oid, oid; + int howmany = 7; + + cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha)); + + cl_git_pass(git_blob_create_fromchunks(&oid, repo, fake_name, text_chunked_source_cb, &howmany)); + cl_assert(git_oid_cmp(&expected_oid, &oid) == 0); +} + +void test_object_blob_fromchunks__creating_a_blob_from_chunks_honors_the_attributes_directives(void) +{ + write_attributes(repo); + + assert_named_chunked_blob("321cbdf08803c744082332332838df6bd160f8f9", "dummy.data"); + assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.txt"); + assert_named_chunked_blob("e9671e138a780833cb689753570fd10a55be84fb", "dummy.dunno"); +} diff --git a/tests-clar/object/commit/commitstagedfile.c b/tests-clar/object/commit/commitstagedfile.c index a852458f..76352fc5 100644 --- a/tests-clar/object/commit/commitstagedfile.c +++ b/tests-clar/object/commit/commitstagedfile.c @@ -61,7 +61,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt */ - cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d")); + cl_git_pass(git_oid_fromstr(&expected_commit_oid, "b78d8ac0e448a305bf2806a00947ade8e8966d58")); cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117")); cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4")); diff --git a/tests-clar/object/tree/write.c b/tests-clar/object/tree/write.c index 3911f6f0..a7831203 100644 --- a/tests-clar/object/tree/write.c +++ b/tests-clar/object/tree/write.c @@ -63,14 +63,14 @@ void test_object_tree_write__subtree(void) //create subtree cl_git_pass(git_treebuilder_create(&builder, NULL)); - cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644)); + cl_git_pass(git_treebuilder_insert(NULL,builder,"new.txt",&bid,0100644)); //-V536 cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder)); git_treebuilder_free(builder); // create parent tree cl_git_pass(git_tree_lookup(&tree, g_repo, &id)); cl_git_pass(git_treebuilder_create(&builder, tree)); - cl_git_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000)); + cl_git_pass(git_treebuilder_insert(NULL,builder,"new",&subtree_id,040000)); //-V536 cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder)); git_treebuilder_free(builder); git_tree_free(tree); @@ -82,3 +82,66 @@ void test_object_tree_write__subtree(void) cl_assert(2 == git_tree_entrycount(tree)); git_tree_free(tree); } + +/* + * And the Lord said: Is this tree properly sorted? + */ +void test_object_tree_write__sorted_subtrees(void) +{ + git_treebuilder *builder; + unsigned int i, position_c, position_cake, position_config; + + struct { + unsigned int attr; + const char *filename; + } entries[] = { + { 0100644, ".gitattributes" }, + { 0100644, ".gitignore" }, + { 0100644, ".htaccess" }, + { 0100644, "Capfile" }, + { 0100644, "Makefile"}, + { 0100644, "README"}, + { 0040000, "app"}, + { 0040000, "cake"}, + { 0040000, "config"}, + { 0100644, "c"}, + { 0100644, "git_test.txt"}, + { 0100644, "htaccess.htaccess"}, + { 0100644, "index.php"}, + { 0040000, "plugins"}, + { 0040000, "schemas"}, + { 0040000, "ssl-certs"}, + { 0040000, "vendors"} + }; + + git_oid blank_oid, tree_oid; + + memset(&blank_oid, 0x0, sizeof(blank_oid)); + + cl_git_pass(git_treebuilder_create(&builder, NULL)); + + for (i = 0; i < ARRAY_SIZE(entries); ++i) { + cl_git_pass(git_treebuilder_insert(NULL, + builder, entries[i].filename, &blank_oid, entries[i].attr)); + } + + cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder)); + + for (i = 0; i < builder->entries.length; ++i) { + git_tree_entry *entry = git_vector_get(&builder->entries, i); + + if (strcmp(entry->filename, "c") == 0) + position_c = i; + + if (strcmp(entry->filename, "cake") == 0) + position_cake = i; + + if (strcmp(entry->filename, "config") == 0) + position_config = i; + } + + cl_assert(position_c < position_cake); + cl_assert(position_cake < position_config); + + git_treebuilder_free(builder); +} diff --git a/tests-clar/refs/branches/listall.c b/tests-clar/refs/branches/listall.c index 0a5634fb..77f8270f 100644 --- a/tests-clar/refs/branches/listall.c +++ b/tests-clar/refs/branches/listall.c @@ -30,22 +30,22 @@ 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(expected_count, branch_list.count); + 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, 6 + 1); + assert_retrieval(GIT_BRANCH_LOCAL | GIT_BRANCH_REMOTE, 9); } void test_refs_branches_listall__retrieve_remote_branches(void) { - assert_retrieval(GIT_BRANCH_REMOTE, 1); + assert_retrieval(GIT_BRANCH_REMOTE, 2); } void test_refs_branches_listall__retrieve_local_branches(void) { - assert_retrieval(GIT_BRANCH_LOCAL, 6); + assert_retrieval(GIT_BRANCH_LOCAL, 7); } static void assert_branch_list_contains(git_strarray *branches, const char* expected_branch_name) @@ -72,7 +72,7 @@ void test_refs_branches_listall__retrieve_remote_symbolic_HEAD_when_present(void cl_git_pass(git_branch_list(&branch_list, repo, GIT_BRANCH_REMOTE)); - cl_assert_equal_i(2, branch_list.count); - assert_branch_list_contains(&branch_list, "refs/remotes/nulltoken/HEAD"); - assert_branch_list_contains(&branch_list, "refs/remotes/nulltoken/master"); + cl_assert_equal_i(3, branch_list.count); + assert_branch_list_contains(&branch_list, "remotes/nulltoken/HEAD"); + assert_branch_list_contains(&branch_list, "remotes/nulltoken/master"); } diff --git a/tests-clar/refs/revparse.c b/tests-clar/refs/revparse.c new file mode 100644 index 00000000..fda99e9d --- /dev/null +++ b/tests-clar/refs/revparse.c @@ -0,0 +1,174 @@ +#include "clar_libgit2.h" + +#include "git2/revparse.h" + +static git_repository *g_repo; +static git_object *g_obj; +static char g_orig_tz[16] = {0}; + + + +/* Helpers */ +static void test_object(const char *spec, const char *expected_oid) +{ + char objstr[64] = {0}; + + 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); + + git_object_free(g_obj); + g_obj = NULL; +} + + +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"); +} + +void test_refs_revparse__cleanup(void) +{ + cl_git_sandbox_cleanup(); + 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")); +} + +void test_refs_revparse__shas(void) +{ + test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd"); + test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd"); +} + +void test_refs_revparse__head(void) +{ + 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"); +} + +void test_refs_revparse__partial_refs(void) +{ + 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"); +} + +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"); +} + +void test_refs_revparse__not_tag(void) +{ + 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"); + + cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}")); +} + +void test_refs_revparse__linear_history(void) +{ + 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"); +} + +void test_refs_revparse__reflog(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}")); + + 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"); +} + +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^{/((}")); + + 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"); + + /* Core git gives a65fedf, because they don't take time zones into account. */ + test_object("master@{1335806640}", "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")); + + 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"); +} diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c index af54b226..2e70c511 100644 --- a/tests-clar/repo/init.c +++ b/tests-clar/repo/init.c @@ -166,14 +166,14 @@ void test_repo_init__additional_templates(void) git_buf_free(&path); } -static void assert_config_entry_on_init(const char *config_key, int expected_value) +static void assert_config_entry_on_init_bytype(const char *config_key, int expected_value, bool is_bare) { git_config *config; int current_value; cl_set_cleanup(&cleanup_repository, "config_entry"); - cl_git_pass(git_repository_init(&_repo, "config_entry/test.git", 1)); + cl_git_pass(git_repository_init(&_repo, "config_entry/test.git", is_bare)); git_repository_config(&config, _repo); if (expected_value >= 0) { @@ -189,6 +189,14 @@ static void assert_config_entry_on_init(const char *config_key, int expected_val git_config_free(config); } +static void assert_config_entry_on_init(const char *config_key, int expected_value) +{ + assert_config_entry_on_init_bytype(config_key, expected_value, true); + git_repository_free(_repo); + + assert_config_entry_on_init_bytype(config_key, expected_value, false); +} + void test_repo_init__detect_filemode(void) { #ifdef GIT_WIN32 @@ -233,3 +241,9 @@ void test_repo_init__reinit_doesnot_overwrite_ignorecase(void) 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); + assert_config_entry_on_init_bytype("core.logallrefupdates", true, false); +} diff --git a/tests-clar/reset/mixed.c b/tests-clar/reset/mixed.c new file mode 100644 index 00000000..7cfff65d --- /dev/null +++ b/tests-clar/reset/mixed.c @@ -0,0 +1,47 @@ +#include "clar_libgit2.h" +#include "posix.h" +#include "reset_helpers.h" +#include "path.h" + +static git_repository *repo; +static git_object *target; + +void test_reset_mixed__initialize(void) +{ + repo = cl_git_sandbox_init("attr"); + target = NULL; +} + +void test_reset_mixed__cleanup(void) +{ + git_object_free(target); + cl_git_sandbox_cleanup(); +} + +void test_reset_mixed__cannot_reset_in_a_bare_repository(void) +{ + git_repository *bare; + + cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); + cl_assert(git_repository_is_bare(bare) == true); + + retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); + + cl_git_fail(git_reset(bare, target, GIT_RESET_MIXED)); + + git_repository_free(bare); +} + +void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void) +{ + unsigned int status; + + cl_git_pass(git_status_file(&status, repo, "macro_bad")); + cl_assert(status == GIT_STATUS_CURRENT); + retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7"); + + cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED)); + + cl_git_pass(git_status_file(&status, repo, "macro_bad")); + cl_assert(status == GIT_STATUS_WT_NEW); +} diff --git a/tests-clar/reset/reset_helpers.c b/tests-clar/reset/reset_helpers.c new file mode 100644 index 00000000..17edca4e --- /dev/null +++ b/tests-clar/reset/reset_helpers.c @@ -0,0 +1,10 @@ +#include "clar_libgit2.h" +#include "reset_helpers.h" + +void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha) +{ + git_oid oid; + + cl_git_pass(git_oid_fromstr(&oid, sha)); + cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY)); +} diff --git a/tests-clar/reset/reset_helpers.h b/tests-clar/reset/reset_helpers.h new file mode 100644 index 00000000..5dbe9d2c --- /dev/null +++ b/tests-clar/reset/reset_helpers.h @@ -0,0 +1,6 @@ +#include "common.h" + +#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d" +#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0" + +extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha); diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c new file mode 100644 index 00000000..3200c159 --- /dev/null +++ b/tests-clar/reset/soft.c @@ -0,0 +1,102 @@ +#include "clar_libgit2.h" +#include "reset_helpers.h" + +static git_repository *repo; +static git_object *target; + +void test_reset_soft__initialize(void) +{ + repo = cl_git_sandbox_init("testrepo.git"); +} + +void test_reset_soft__cleanup(void) +{ + git_object_free(target); + cl_git_sandbox_cleanup(); +} + +static void assert_reset_soft(bool should_be_detached) +{ + git_oid oid; + + cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); + cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); + + retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO); + + cl_assert(git_repository_head_detached(repo) == should_be_detached); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_assert(git_repository_head_detached(repo) == should_be_detached); + + cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); + cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); +} + +void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void) +{ + assert_reset_soft(false); +} + +static void detach_head(void) +{ + git_reference *head; + git_oid oid; + + cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); + + cl_git_pass(git_reference_create_oid(&head, repo, "HEAD", &oid, true)); + git_reference_free(head); +} + +void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) +{ + detach_head(); + + assert_reset_soft(true); +} + +void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) +{ + git_oid oid; + char raw_head_oid[GIT_OID_HEXSZ + 1]; + + cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); + git_oid_fmt(raw_head_oid, &oid); + raw_head_oid[GIT_OID_HEXSZ] = '\0'; + + retrieve_target_from_oid(&target, repo, raw_head_oid); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); + cl_git_pass(git_oid_streq(&oid, raw_head_oid)); +} + +void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void) +{ + git_oid oid; + + /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ + retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1"); + + cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT)); + + cl_assert(git_repository_head_detached(repo) == false); + cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD")); + cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); +} + +void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) +{ + /* 53fc32d is the tree of commit e90810b */ + retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016"); + + cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); + git_object_free(target); + + /* 521d87c is an annotated tag pointing to a blob */ + retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"); + cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT)); +} diff --git a/tests-clar/resources/filemodes/.gitted/HEAD b/tests-clar/resources/filemodes/.gitted/HEAD new file mode 100644 index 00000000..cb089cd8 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests-clar/resources/filemodes/.gitted/config b/tests-clar/resources/filemodes/.gitted/config new file mode 100644 index 00000000..af107929 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/config @@ -0,0 +1,6 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true diff --git a/tests-clar/resources/filemodes/.gitted/description b/tests-clar/resources/filemodes/.gitted/description new file mode 100644 index 00000000..498b267a --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests-clar/resources/filemodes/.gitted/hooks/commit-msg.sample b/tests-clar/resources/filemodes/.gitted/hooks/commit-msg.sample new file mode 100755 index 00000000..b58d1184 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/hooks/commit-msg.sample @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to check the commit log message. +# Called by "git commit" with one argument, the name of the file +# that has the commit message. The hook should exit with non-zero +# status after issuing an appropriate message if it wants to stop the +# commit. The hook is allowed to edit the commit message file. +# +# To enable this hook, rename this file to "commit-msg". + +# Uncomment the below to add a Signed-off-by line to the message. +# Doing this in a hook is a bad idea in general, but the prepare-commit-msg +# hook is more suited to it. +# +# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +} diff --git a/tests-clar/resources/filemodes/.gitted/index b/tests-clar/resources/filemodes/.gitted/index Binary files differnew file mode 100644 index 00000000..b1b175a9 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/index diff --git a/tests-clar/resources/filemodes/.gitted/info/exclude b/tests-clar/resources/filemodes/.gitted/info/exclude new file mode 100644 index 00000000..a5196d1b --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests-clar/resources/filemodes/.gitted/logs/HEAD b/tests-clar/resources/filemodes/.gitted/logs/HEAD new file mode 100644 index 00000000..1cb6a84c --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/logs/HEAD @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 9962c8453ba6f0cf8dac7c5dcc2fa2897fa9964a Russell Belfer <rb@github.com> 1338847682 -0700 commit (initial): Initial commit of test data diff --git a/tests-clar/resources/filemodes/.gitted/logs/refs/heads/master b/tests-clar/resources/filemodes/.gitted/logs/refs/heads/master new file mode 100644 index 00000000..1cb6a84c --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 9962c8453ba6f0cf8dac7c5dcc2fa2897fa9964a Russell Belfer <rb@github.com> 1338847682 -0700 commit (initial): Initial commit of test data diff --git a/tests-clar/resources/filemodes/.gitted/objects/99/62c8453ba6f0cf8dac7c5dcc2fa2897fa9964a b/tests-clar/resources/filemodes/.gitted/objects/99/62c8453ba6f0cf8dac7c5dcc2fa2897fa9964a Binary files differnew file mode 100644 index 00000000..cbd2b557 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/objects/99/62c8453ba6f0cf8dac7c5dcc2fa2897fa9964a diff --git a/tests-clar/resources/filemodes/.gitted/objects/a5/c5dd0fc6c313159a69b1d19d7f61a9f978e8f1 b/tests-clar/resources/filemodes/.gitted/objects/a5/c5dd0fc6c313159a69b1d19d7f61a9f978e8f1 Binary files differnew file mode 100644 index 00000000..a9eaf2cb --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/objects/a5/c5dd0fc6c313159a69b1d19d7f61a9f978e8f1 diff --git a/tests-clar/resources/filemodes/.gitted/objects/e7/48d196331bcb20267eaaee4ff3326cb73b8182 b/tests-clar/resources/filemodes/.gitted/objects/e7/48d196331bcb20267eaaee4ff3326cb73b8182 Binary files differnew file mode 100644 index 00000000..98066029 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/objects/e7/48d196331bcb20267eaaee4ff3326cb73b8182 diff --git a/tests-clar/resources/filemodes/.gitted/refs/heads/master b/tests-clar/resources/filemodes/.gitted/refs/heads/master new file mode 100644 index 00000000..9822d2d3 --- /dev/null +++ b/tests-clar/resources/filemodes/.gitted/refs/heads/master @@ -0,0 +1 @@ +9962c8453ba6f0cf8dac7c5dcc2fa2897fa9964a diff --git a/tests-clar/resources/filemodes/exec_off b/tests-clar/resources/filemodes/exec_off new file mode 100644 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_off @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_off2on_staged b/tests-clar/resources/filemodes/exec_off2on_staged new file mode 100755 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_off2on_staged @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_off2on_workdir b/tests-clar/resources/filemodes/exec_off2on_workdir new file mode 100755 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_off2on_workdir @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_off_untracked b/tests-clar/resources/filemodes/exec_off_untracked new file mode 100644 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_off_untracked @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_on b/tests-clar/resources/filemodes/exec_on new file mode 100755 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_on @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_on2off_staged b/tests-clar/resources/filemodes/exec_on2off_staged new file mode 100644 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_on2off_staged @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_on2off_workdir b/tests-clar/resources/filemodes/exec_on2off_workdir new file mode 100644 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_on2off_workdir @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/filemodes/exec_on_untracked b/tests-clar/resources/filemodes/exec_on_untracked new file mode 100755 index 00000000..a5c5dd0f --- /dev/null +++ b/tests-clar/resources/filemodes/exec_on_untracked @@ -0,0 +1 @@ +Howdy diff --git a/tests-clar/resources/status/这 b/tests-clar/resources/status/这 new file mode 100644 index 00000000..f0ff9a19 --- /dev/null +++ b/tests-clar/resources/status/这 @@ -0,0 +1 @@ +This diff --git a/tests-clar/resources/testrepo.git/config b/tests-clar/resources/testrepo.git/config index 1a5aacdf..b4fdac6c 100644 --- a/tests-clar/resources/testrepo.git/config +++ b/tests-clar/resources/testrepo.git/config @@ -6,3 +6,7 @@ [remote "test"] url = git://github.com/libgit2/libgit2 fetch = +refs/heads/*:refs/remotes/test/* + +[branch "master"] + remote = test + merge = refs/heads/master diff --git a/tests-clar/resources/testrepo.git/logs/HEAD b/tests-clar/resources/testrepo.git/logs/HEAD new file mode 100644 index 00000000..b16c9b31 --- /dev/null +++ b/tests-clar/resources/testrepo.git/logs/HEAD @@ -0,0 +1,5 @@ +0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git +be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0900 commit: +a65fedf39aefe402d3bb6e24df4d4f5fe4547750 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0900 checkout: moving from master to br2 +c47800c7266a2be04c571c04d5a6614691ea99bd a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -0900 commit: checking in +a4a7dce85cf63874e984719f4fdd239f5145052f a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806621 -0900 checkout: moving from br2 to master diff --git a/tests-clar/resources/testrepo.git/logs/refs/heads/br2 b/tests-clar/resources/testrepo.git/logs/refs/heads/br2 new file mode 100644 index 00000000..4e27f6b8 --- /dev/null +++ b/tests-clar/resources/testrepo.git/logs/refs/heads/br2 @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 c47800c7266a2be04c571c04d5a6614691ea99bd Ben Straub <bstraub@github.com> 1335806608 -0700 branch: Created from refs/remotes/origin/br2 +a4a7dce85cf63874e984719f4fdd239f5145052f a4a7dce85cf63874e984719f4fdd239f5145052f Ben Straub <bstraub@github.com> 1335806617 -0700 commit: checking in diff --git a/tests-clar/resources/testrepo.git/logs/refs/heads/master b/tests-clar/resources/testrepo.git/logs/refs/heads/master new file mode 100644 index 00000000..e1c729a4 --- /dev/null +++ b/tests-clar/resources/testrepo.git/logs/refs/heads/master @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0800 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git +be3563ae3f795b2b4353bcce3a527ad0a4f7f644 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1335806603 -0800 commit: checking in diff --git a/tests-clar/resources/testrepo.git/logs/refs/heads/not-good b/tests-clar/resources/testrepo.git/logs/refs/heads/not-good new file mode 100644 index 00000000..bfbeacb8 --- /dev/null +++ b/tests-clar/resources/testrepo.git/logs/refs/heads/not-good @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 a65fedf39aefe402d3bb6e24df4d4f5fe4547750 Ben Straub <bstraub@github.com> 1336761944 -0700 branch: Created from master diff --git a/tests-clar/resources/testrepo.git/logs/refs/remotes/origin/HEAD b/tests-clar/resources/testrepo.git/logs/refs/remotes/origin/HEAD new file mode 100644 index 00000000..f1aac6d0 --- /dev/null +++ b/tests-clar/resources/testrepo.git/logs/refs/remotes/origin/HEAD @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 be3563ae3f795b2b4353bcce3a527ad0a4f7f644 Ben Straub <bstraub@github.com> 1335806563 -0700 clone: from /Users/ben/src/libgit2/tests/resources/testrepo.git diff --git a/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 b/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 Binary files differnew file mode 100644 index 00000000..d1c032fc --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/08/b041783f40edfe12bb406c9c9a8a040177c125 diff --git a/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 b/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 Binary files differnew file mode 100644 index 00000000..0a1500a6 --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/2d/59075e0681f540482d4f6223a68e0fef790bc7 diff --git a/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea b/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea Binary files differnew file mode 100644 index 00000000..b4e5aa18 --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/4b/22b35d44b5a4f589edf3dc89196399771796ea diff --git a/tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe b/tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe new file mode 100644 index 00000000..71019a63 --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/84/9a5e34a26815e821f865b8479f5815a47af0fe @@ -0,0 +1,2 @@ +xM F]s41x(IKݽ/_P@!8)es + N&FGSƄh{+CZzvF7Z-kx\[P8GK/^l>.4
\ No newline at end of file diff --git a/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 b/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 Binary files differnew file mode 100644 index 00000000..f3b46b3c --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/d0/7b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 diff --git a/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 b/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 Binary files differnew file mode 100644 index 00000000..2d47e6fa --- /dev/null +++ b/tests-clar/resources/testrepo.git/objects/d7/1aab4f9b04b45ce09bcaa636a9be6231474759 diff --git a/tests-clar/resources/testrepo.git/refs/heads/not-good b/tests-clar/resources/testrepo.git/refs/heads/not-good new file mode 100644 index 00000000..3d8f0a40 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/heads/not-good @@ -0,0 +1 @@ +a65fedf39aefe402d3bb6e24df4d4f5fe4547750 diff --git a/tests-clar/resources/testrepo.git/refs/notes/fanout b/tests-clar/resources/testrepo.git/refs/notes/fanout new file mode 100644 index 00000000..1f170363 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/notes/fanout @@ -0,0 +1 @@ +d07b0f9a8c89f1d9e74dc4fce6421dec5ef8a659 diff --git a/tests-clar/resources/testrepo.git/refs/remotes/test/master b/tests-clar/resources/testrepo.git/refs/remotes/test/master new file mode 100644 index 00000000..9536ad89 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/remotes/test/master @@ -0,0 +1 @@ +be3563ae3f795b2b4353bcce3a527ad0a4f7f644 diff --git a/tests-clar/resources/testrepo.git/refs/tags/hard_tag b/tests-clar/resources/testrepo.git/refs/tags/hard_tag new file mode 100644 index 00000000..59ce6564 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/tags/hard_tag @@ -0,0 +1 @@ +849a5e34a26815e821f865b8479f5815a47af0fe diff --git a/tests-clar/resources/testrepo.git/refs/tags/wrapped_tag b/tests-clar/resources/testrepo.git/refs/tags/wrapped_tag new file mode 100644 index 00000000..59ce6564 --- /dev/null +++ b/tests-clar/resources/testrepo.git/refs/tags/wrapped_tag @@ -0,0 +1 @@ +849a5e34a26815e821f865b8479f5815a47af0fe diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c index 369b25bd..0384306c 100644 --- a/tests-clar/status/ignore.c +++ b/tests-clar/status/ignore.c @@ -131,3 +131,17 @@ void test_status_ignore__empty_repo_with_gitignore_rewrite(void) cl_assert(ignored); } +void test_status_ignore__ignore_pattern_contains_space(void) +{ + unsigned int flags; + const mode_t mode = 0777; + + g_repo = cl_git_sandbox_init("empty_standard_repo"); + cl_git_rewritefile("empty_standard_repo/.gitignore", "foo bar.txt\n"); + + cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", NULL, mode)); + cl_git_mkfile("empty_standard_repo/foo/look-ma.txt", "I'm not going to be ignored!"); + + cl_git_pass(git_status_file(&flags, g_repo, "foo/look-ma.txt")); + cl_assert(flags == GIT_STATUS_WT_NEW); +} diff --git a/tests-clar/status/status_data.h b/tests-clar/status/status_data.h index f109717e..043b8300 100644 --- a/tests-clar/status/status_data.h +++ b/tests-clar/status/status_data.h @@ -19,6 +19,8 @@ static const char *entry_paths0[] = { "subdir/deleted_file", "subdir/modified_file", "subdir/new_file", + + "\xe8\xbf\x99", }; static const unsigned int entry_statuses0[] = { @@ -38,9 +40,11 @@ static const unsigned int entry_statuses0[] = { GIT_STATUS_WT_DELETED, GIT_STATUS_WT_MODIFIED, GIT_STATUS_WT_NEW, + + GIT_STATUS_WT_NEW, }; -static const size_t entry_count0 = 15; +static const size_t entry_count0 = 16; /* entries for a copy of tests/resources/status with all content * deleted from the working directory @@ -108,6 +112,7 @@ static const char *entry_paths3[] = { "subdir/current_file", "subdir/deleted_file", "subdir/modified_file", + "\xe8\xbf\x99", }; static const unsigned int entry_statuses3[] = { @@ -132,9 +137,10 @@ static const unsigned int entry_statuses3[] = { GIT_STATUS_WT_DELETED, GIT_STATUS_WT_DELETED, GIT_STATUS_WT_DELETED, + GIT_STATUS_WT_NEW, }; -static const size_t entry_count3 = 21; +static const size_t entry_count3 = 22; /* entries for a copy of tests/resources/status with some mods @@ -163,7 +169,8 @@ static const char *entry_paths4[] = { "subdir/deleted_file", "subdir/modified_file", "zzz_new_dir/new_file", - "zzz_new_file" + "zzz_new_file", + "\xe8\xbf\x99", }; static const unsigned int entry_statuses4[] = { @@ -189,6 +196,7 @@ static const unsigned int entry_statuses4[] = { GIT_STATUS_WT_DELETED, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, + GIT_STATUS_WT_NEW, }; -static const size_t entry_count4 = 22; +static const size_t entry_count4 = 23; diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c index b3ebdb78..3670b72a 100644 --- a/tests-clar/status/worktree.c +++ b/tests-clar/status/worktree.c @@ -581,3 +581,69 @@ void test_status_worktree__space_in_filename(void) git_index_free(index); git_repository_free(repo); } + +static const char *filemode_paths[] = { + "exec_off", + "exec_off2on_staged", + "exec_off2on_workdir", + "exec_off_untracked", + "exec_on", + "exec_on2off_staged", + "exec_on2off_workdir", + "exec_on_untracked", +}; + +static unsigned int filemode_statuses[] = { + GIT_STATUS_CURRENT, + GIT_STATUS_INDEX_MODIFIED, + GIT_STATUS_WT_MODIFIED, + GIT_STATUS_WT_NEW, + GIT_STATUS_CURRENT, + GIT_STATUS_INDEX_MODIFIED, + GIT_STATUS_WT_MODIFIED, + GIT_STATUS_WT_NEW +}; + +static const size_t filemode_count = 8; + +void test_status_worktree__filemode_changes(void) +{ + git_repository *repo = cl_git_sandbox_init("filemodes"); + status_entry_counts counts; + git_status_options opts; + git_config *cfg; + + /* overwrite stored filemode with platform appropriate value */ + cl_git_pass(git_repository_config(&cfg, repo)); + if (cl_is_chmod_supported()) + cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); + else { + unsigned int i; + cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); + + /* won't trust filesystem mode diffs, so these will appear unchanged */ + for (i = 0; i < filemode_count; ++i) + if (filemode_statuses[i] == GIT_STATUS_WT_MODIFIED) + filemode_statuses[i] = GIT_STATUS_CURRENT; + } + + memset(&opts, 0, sizeof(opts)); + opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | + GIT_STATUS_OPT_INCLUDE_IGNORED | + GIT_STATUS_OPT_INCLUDE_UNMODIFIED; + + memset(&counts, 0, sizeof(counts)); + counts.expected_entry_count = filemode_count; + counts.expected_paths = filemode_paths; + counts.expected_statuses = filemode_statuses; + + cl_git_pass( + git_status_foreach_ext(repo, &opts, cb_status__normal, &counts) + ); + + cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); + cl_assert_equal_i(0, counts.wrong_status_flags_count); + cl_assert_equal_i(0, counts.wrong_sorted_path); + + git_config_free(cfg); +} |