summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml7
-rw-r--r--CHANGELOG.md94
-rw-r--r--CMakeLists.txt17
-rw-r--r--README.md7
-rw-r--r--examples/network/common.c24
-rw-r--r--include/git2/common.h27
-rw-r--r--include/git2/errors.h2
-rw-r--r--include/git2/global.h2
-rw-r--r--include/git2/remote.h4
-rw-r--r--include/git2/repository.h4
-rw-r--r--include/git2/sys/filter.h11
-rw-r--r--include/git2/sys/transport.h10
-rw-r--r--include/git2/version.h6
-rw-r--r--include/git2/worktree.h67
-rwxr-xr-xscript/cibuild.sh34
-rwxr-xr-xscript/install-deps-osx.sh3
-rw-r--r--src/attr_file.c8
-rw-r--r--src/attrcache.c12
-rw-r--r--src/branch.c65
-rw-r--r--src/buffer.c23
-rw-r--r--src/buffer.h4
-rw-r--r--src/checkout.c6
-rw-r--r--src/config.c77
-rw-r--r--src/config_file.c7
-rw-r--r--src/config_file.h1
-rw-r--r--src/diff_parse.c2
-rw-r--r--src/fileops.c11
-rw-r--r--src/filter.c8
-rw-r--r--src/global.c2
-rw-r--r--src/hash/sha1dc/sha1.c88
-rw-r--r--src/hash/sha1dc/sha1.h79
-rw-r--r--src/hash/sha1dc/ubc_check.c9
-rw-r--r--src/hash/sha1dc/ubc_check.h12
-rw-r--r--src/index.c117
-rw-r--r--src/indexer.c14
-rw-r--r--src/object.c1
-rw-r--r--src/object.h1
-rw-r--r--src/odb.c87
-rw-r--r--src/odb.h8
-rw-r--r--src/odb_loose.c8
-rw-r--r--src/openssl_stream.c2
-rw-r--r--src/pack.c8
-rw-r--r--src/path.c9
-rw-r--r--src/posix.h4
-rw-r--r--src/refdb_fs.c7
-rw-r--r--src/refs.c97
-rw-r--r--src/refs.h14
-rw-r--r--src/remote.c19
-rw-r--r--src/repository.c314
-rw-r--r--src/repository.h22
-rw-r--r--src/revparse.c25
-rw-r--r--src/settings.c23
-rw-r--r--src/signature.c5
-rw-r--r--src/socket_stream.c6
-rw-r--r--src/sysdir.c11
-rw-r--r--src/sysdir.h12
-rw-r--r--src/transports/http.c3
-rw-r--r--src/transports/smart.c6
-rw-r--r--src/transports/smart_protocol.c3
-rw-r--r--src/transports/ssh.c14
-rw-r--r--src/transports/winhttp.c1
-rw-r--r--src/varint.c2
-rw-r--r--src/win32/posix.h3
-rw-r--r--src/win32/posix_w32.c443
-rw-r--r--src/worktree.c75
-rw-r--r--tests/attr/ignore.c12
-rw-r--r--tests/checkout/head.c78
-rw-r--r--tests/clone/local.c2
-rw-r--r--tests/config/include.c20
-rw-r--r--tests/config/readonly.c64
-rw-r--r--tests/core/encoding.c3
-rw-r--r--tests/core/features.c2
-rw-r--r--tests/core/structinit.c11
-rw-r--r--tests/index/version.c126
-rw-r--r--tests/network/remote/local.c17
-rw-r--r--tests/object/lookup.c63
-rw-r--r--tests/odb/backend/backend_helpers.c159
-rw-r--r--tests/odb/backend/backend_helpers.h22
-rw-r--r--tests/odb/backend/multiple.c121
-rw-r--r--tests/odb/backend/nonrefreshing.c177
-rw-r--r--tests/odb/backend/simple.c232
-rw-r--r--tests/odb/freshen.c25
-rw-r--r--tests/odb/loose.c4
-rw-r--r--tests/online/badssl.c2
-rw-r--r--tests/online/clone.c2
-rw-r--r--tests/pack/indexer.c4
-rw-r--r--tests/pack/packbuilder.c12
-rw-r--r--tests/refs/create.c4
-rw-r--r--tests/refs/revparse.c35
-rw-r--r--tests/repo/discover.c2
-rw-r--r--tests/repo/head.c12
-rw-r--r--tests/repo/init.c11
-rw-r--r--tests/repo/open.c3
-rw-r--r--tests/resources/indexv4/.gitted/HEAD1
-rw-r--r--tests/resources/indexv4/.gitted/config5
-rw-r--r--tests/resources/indexv4/.gitted/indexbin0 -> 572 bytes
-rw-r--r--tests/resources/indexv4/.gitted/objects/4c/9109b3e671d851eec87e0e72f6305b582e7e99bin0 -> 70 bytes
-rw-r--r--tests/resources/indexv4/.gitted/objects/b0/952dbb50bed5f01e03e31b296184cb183e54a7bin0 -> 154 bytes
-rw-r--r--tests/resources/indexv4/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391bin0 -> 15 bytes
-rw-r--r--tests/resources/indexv4/.gitted/refs/heads/master1
-rw-r--r--tests/resources/indexv4/file.tx0
-rw-r--r--tests/resources/indexv4/file.txt0
-rw-r--r--tests/resources/indexv4/file.txz0
-rw-r--r--tests/resources/indexv4/foo0
-rw-r--r--tests/resources/indexv4/zzz0
-rw-r--r--tests/status/ignore.c78
-rw-r--r--tests/submodule/open.c90
-rw-r--r--tests/threads/basic.c6
-rw-r--r--tests/threads/diff.c15
-rw-r--r--tests/worktree/refs.c45
-rw-r--r--tests/worktree/submodule.c2
-rw-r--r--tests/worktree/worktree.c119
112 files changed, 2871 insertions, 813 deletions
diff --git a/.travis.yml b/.travis.yml
index af38252ce..d789f15a7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -29,6 +29,7 @@ addons:
- openssh-server
- valgrind
+dist: trusty
sudo: false
matrix:
@@ -38,13 +39,19 @@ matrix:
compiler: gcc
include:
- compiler: gcc
+ env: PRECISE=1
+ os: linux
+ dist: precise
+ - compiler: gcc
env: COVERITY=1
os: linux
+ dist: trusty
- compiler: gcc
env:
- VALGRIND=1
OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=OFF -DDEBUG_POOL=ON -DCMAKE_BUILD_TYPE=Debug"
os: linux
+ dist: trusty
allow_failures:
- env: COVERITY=1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0ae75e156..24ecba426 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,15 +1,103 @@
-v0.25 + 1
--------
+v0.26 + 1
+---------
### Changes or improvements
+### API additions
+
+### API removals
+
+### Breaking API changes
+
+v0.26
+-----
+
+### Changes or improvements
+
+* Support for opening, creating and modifying worktrees.
+
+* We can now detect SHA1 collisions resulting from the SHAttered attack. These
+ checks can be enabled at build time via `-DUSE_SHA1DC`.
+
+* Fix for missing implementation of `git_merge_driver_source` getters.
+
+* Fix for installed pkg-config file being broken when the prefix contains
+ spaces.
+
+* We now detect when the hashsum of on-disk objects does not match their
+ expected hashsum.
+
+* We now support open-ended ranges (e.g. "master..", "...master") in our
+ revision range parsing code.
+
+* We now correctly compute ignores with leading "/" in subdirectories.
+
+* We now optionally call `fsync` on loose objects, packfiles and their indexes,
+ loose references and packed reference files.
+
+* We can now build against OpenSSL v1.1 and against LibreSSL.
+
* `GIT_MERGE_OPTIONS_INIT` now includes a setting to perform rename detection.
This aligns this structure with the default by `git_merge` and
`git_merge_trees` when `NULL` was provided for the options.
+* Improvements for reading index v4 files.
+
+* Perform additional retries for filesystem operations on Windows when files
+ are temporarily locked by other processes.
+
### API additions
-### API removals
+* New family of functions to handle worktrees:
+
+ * `git_worktree_list()` lets you look up worktrees for a repository.
+ * `git_worktree_lookup()` lets you get a specific worktree.
+ * `git_worktree_open_from_repository()` lets you get the associated worktree
+ of a repository.
+ a worktree.
+ * `git_worktree_add` lets you create new worktrees.
+ * `git_worktree_prune` lets you remove worktrees from disk.
+ * `git_worktree_lock()` and `git_worktree_unlock()` let you lock
+ respectively unlock a worktree.
+ * `git_repository_open_from_worktree()` lets you open a repository via
+ * `git_repository_head_for_worktree()` lets you get the current `HEAD` for a
+ linked worktree.
+ * `git_repository_head_detached_for_worktree()` lets you check whether a
+ linked worktree is in detached HEAD mode.
+
+* `git_repository_item_path()` lets you retrieve paths for various repository
+ files.
+
+* `git_repository_commondir()` lets you retrieve the common directory of a
+ repository.
+
+* `git_branch_is_checked_out()` allows you to check whether a branch is checked
+ out in a repository or any of its worktrees.
+
+* `git_repository_submodule_cache_all()` and
+ `git_repository_submodule_cache_clear()` functions allow you to prime or clear
+ the submodule cache of a repository.
+
+* You can disable strict hash verifications via the
+ `GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION` option with `git_libgit2_opts()`.
+
+* You can enable us calling `fsync` for various files inside the ".git"
+ directory by setting the `GIT_OPT_ENABLE_FSYNC_GITDIR` option with
+ `git_libgit2_opts()`.
+
+* You can now enable "offset deltas" when creating packfiles and negotiating
+ packfiles with a remote server by setting `GIT_OPT_ENABLE_OFS_DELTA` option
+ with `GIT_libgit2_opts()`.
+
+* You can now set the default share mode on Windows for opening files using
+ `GIT_OPT_SET_WINDOWS_SHAREMODE` option with `git_libgit2_opts()`.
+ You can query the current share mode with `GIT_OPT_GET_WINDOWS_SHAREMODE`.
+
+* `git_transport_smart_proxy_options()' enables you to get the proxy options for
+ smart transports.
+
+* The `GIT_FILTER_INIT` macro and the `git_filter_init` function are provided
+ to initialize a `git_filter` structure.
### Breaking API changes
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d5bf21f54..4783e3ef9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -238,6 +238,7 @@ ENDIF()
IF (WIN32 AND WINHTTP)
ADD_DEFINITIONS(-DGIT_WINHTTP)
+ ADD_DEFINITIONS(-DGIT_HTTPS)
# Since MinGW does not come with headers or an import library for winhttp,
# we have to include a private header and generate our own import library
@@ -295,6 +296,9 @@ ENDIF()
# Specify sha1 implementation
IF (USE_SHA1DC)
ADD_DEFINITIONS(-DGIT_SHA1_COLLISIONDETECT)
+ ADD_DEFINITIONS(-DSHA1DC_NO_STANDARD_INCLUDES=1)
+ ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\")
+ ADD_DEFINITIONS(-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\")
FILE(GLOB SRC_SHA1 src/hash/hash_collisiondetect.c src/hash/sha1dc/*)
ELSEIF (WIN32 AND NOT MINGW)
ADD_DEFINITIONS(-DGIT_SHA1_WIN32)
@@ -464,9 +468,6 @@ IF (MSVC)
SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
SET(WIN_RC "src/win32/git2.rc")
-
- # Precompiled headers
-
ELSE ()
SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
@@ -477,9 +478,7 @@ ELSE ()
SET(CMAKE_C_FLAGS "-std=c99 -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
ENDIF()
- IF (WIN32 AND NOT CYGWIN)
- SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
- ENDIF ()
+ SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
@@ -546,11 +545,13 @@ ENDIF()
IF (SECURITY_FOUND)
ADD_DEFINITIONS(-DGIT_SECURE_TRANSPORT)
+ ADD_DEFINITIONS(-DGIT_HTTPS)
INCLUDE_DIRECTORIES(${SECURITY_INCLUDE_DIR})
ENDIF ()
IF (OPENSSL_FOUND)
ADD_DEFINITIONS(-DGIT_OPENSSL)
+ ADD_DEFINITIONS(-DGIT_HTTPS)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
SET(SSL_LIBRARIES ${OPENSSL_LIBRARIES})
ENDIF()
@@ -703,9 +704,9 @@ IF (BUILD_CLAR)
ENABLE_TESTING()
IF (WINHTTP OR OPENSSL_FOUND OR SECURITY_FOUND)
- ADD_TEST(libgit2_clar libgit2_clar -ionline)
+ ADD_TEST(libgit2_clar libgit2_clar -ionline -xclone::local::git_style_unc_paths -xclone::local::standard_unc_paths_are_written_git_style)
ELSE ()
- ADD_TEST(libgit2_clar libgit2_clar -v)
+ ADD_TEST(libgit2_clar libgit2_clar -v -xclone::local::git_style_unc_paths -xclone::local::standard_unc_paths_are_written_git_style)
ENDIF ()
# Add a test target which runs the cred callback tests, to be
diff --git a/README.md b/README.md
index 472261cef..1bbd37134 100644
--- a/README.md
+++ b/README.md
@@ -43,10 +43,9 @@ We ask that you not open a GitHub Issue for help, only for bug reports.
What It Can Do
==============
-The goal of this library is to allow its users the ability to handle Git data in
-their applications from their programming language of choice, as is used in
-production for many applications including the GitHub.com site, in Plastic SCM
-and also powering Microsoft's Visual Studio tools for Git.
+libgit2 provides you with the ability to manage Git repositories in the
+programming language of your choice. It's used in production to power many
+applications including GitHub.com, Plastic SCM and Visual Studio Team Services.
It does not aim to replace the git tool or its user-facing commands. Some APIs
resemble the plumbing commands as those align closely with the concepts of the
diff --git a/examples/network/common.c b/examples/network/common.c
index d123eedbd..1a81a10f8 100644
--- a/examples/network/common.c
+++ b/examples/network/common.c
@@ -1,5 +1,7 @@
#include "common.h"
#include <stdio.h>
+#include <string.h>
+#include <errno.h>
/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
* with permission of the original author, Martin Pool.
@@ -20,15 +22,27 @@ int cred_acquire_cb(git_cred **out,
unsigned int UNUSED(allowed_types),
void * UNUSED(payload))
{
- char username[128] = {0};
- char password[128] = {0};
+ char *username = NULL, *password = NULL;
+ int error;
printf("Username: ");
- scanf("%s", username);
+ if (getline(&username, NULL, stdin) < 0) {
+ fprintf(stderr, "Unable to read username: %s", strerror(errno));
+ return -1;
+ }
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
printf("Password: ");
- scanf("%s", password);
+ if (getline(&password, NULL, stdin) < 0) {
+ fprintf(stderr, "Unable to read password: %s", strerror(errno));
+ free(username);
+ return -1;
+ }
- return git_cred_userpass_plaintext_new(out, username, password);
+ error = git_cred_userpass_plaintext_new(out, username, password);
+
+ free(username);
+ free(password);
+
+ return error;
}
diff --git a/include/git2/common.h b/include/git2/common.h
index c909f86ca..f65cfdd01 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -179,7 +179,10 @@ typedef enum {
GIT_OPT_SET_SSL_CIPHERS,
GIT_OPT_GET_USER_AGENT,
GIT_OPT_ENABLE_OFS_DELTA,
- GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION,
+ GIT_OPT_ENABLE_FSYNC_GITDIR,
+ GIT_OPT_GET_WINDOWS_SHAREMODE,
+ GIT_OPT_SET_WINDOWS_SHAREMODE,
+ GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION,
} git_libgit2_opt_t;
/**
@@ -284,6 +287,17 @@ typedef enum {
* > - `user_agent` is the value that will be delivered as the
* > User-Agent header on HTTP requests.
*
+ * * opts(GIT_OPT_SET_WINDOWS_SHAREMODE, unsigned long value)
+ *
+ * > Set the share mode used when opening files on Windows.
+ * > For more information, see the documentation for CreateFile.
+ * > The default is: FILE_SHARE_READ | FILE_SHARE_WRITE. This is
+ * > ignored and unused on non-Windows platforms.
+ *
+ * * opts(GIT_OPT_GET_WINDOWS_SHAREMODE, unsigned long *value)
+ *
+ * > Get the share mode used when opening files on Windows.
+ *
* * opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, int enabled)
*
* > Enable strict input validation when creating new objects
@@ -317,13 +331,20 @@ typedef enum {
* > Packfiles containing offset deltas can still be read.
* > This defaults to enabled.
*
- * * opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, int enabled)
+ * * opts(GIT_OPT_ENABLE_FSYNC_GITDIR, int enabled)
*
- * > Enable synchronized writes of new objects using `fsync`
+ * > Enable synchronized writes of files in the gitdir using `fsync`
* > (or the platform equivalent) to ensure that new object data
* > is written to permanent storage, not simply cached. This
* > defaults to disabled.
*
+ * opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, int enabled)
+ *
+ * > Enable strict verification of object hashsums when reading
+ * > objects from disk. This may impact performance due to an
+ * > additional checksum calculation on each object. This defaults
+ * > to enabled.
+ *
* @param option Option key
* @param ... value to set the option
* @return 0 on success, <0 on failure
diff --git a/include/git2/errors.h b/include/git2/errors.h
index 3b746b758..6f5580253 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -53,6 +53,8 @@ typedef enum {
GIT_PASSTHROUGH = -30, /**< Internal only */
GIT_ITEROVER = -31, /**< Signals end of iteration with iterator */
+ GIT_RETRY = -32, /**< Internal only */
+ GIT_EMISMATCH = -33, /**< Hashsum mismatch in object */
} git_error_code;
/**
diff --git a/include/git2/global.h b/include/git2/global.h
index ce5bdf444..2a87e10c6 100644
--- a/include/git2/global.h
+++ b/include/git2/global.h
@@ -14,7 +14,7 @@ GIT_BEGIN_DECL
/**
* Init the global state
*
- * This function must the called before any other libgit2 function in
+ * This function must be called before any other libgit2 function in
* order to set up global state and threading.
*
* This function may be called multiple times - it will return the number
diff --git a/include/git2/remote.h b/include/git2/remote.h
index 244794e6a..e9e4e5b65 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -715,8 +715,8 @@ GIT_EXTERN(int) git_remote_prune(git_remote *remote, const git_remote_callbacks
* Peform all the steps from a push.
*
* @param remote the remote to push to
- * @param refspecs the refspecs to use for pushing. If none are
- * passed, the configured refspecs will be used
+ * @param refspecs the refspecs to use for pushing. If NULL or an empty
+ * array, the configured refspecs will be used
* @param opts options to use for this push
*/
GIT_EXTERN(int) git_remote_push(git_remote *remote,
diff --git a/include/git2/repository.h b/include/git2/repository.h
index a396a5409..8aac0b3f7 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -433,12 +433,12 @@ typedef enum {
* item. It will thereby honor things like the repository's
* common directory, gitdir, etc. In case a file path cannot
* exist for a given item (e.g. the working directory of a bare
- * repository), an error is returned.
+ * repository), GIT_ENOTFOUND is returned.
*
* @param out Buffer to store the path at
* @param repo Repository to get path for
* @param item The repository item for which to retrieve the path
- * @return 0 on success, otherwise a negative value
+ * @return 0, GIT_ENOTFOUND if the path cannot exist or an error code
*/
GIT_EXTERN(int) git_repository_item_path(git_buf *out, git_repository *repo, git_repository_item_t item);
diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h
index d0e5d4d6f..6d575d4fd 100644
--- a/include/git2/sys/filter.h
+++ b/include/git2/sys/filter.h
@@ -271,6 +271,17 @@ struct git_filter {
};
#define GIT_FILTER_VERSION 1
+#define GIT_FILTER_INIT {GIT_FILTER_VERSION}
+
+/**
+ * Initializes a `git_filter` with default values. Equivalent to
+ * creating an instance with GIT_FILTER_INIT.
+ *
+ * @param filter the `git_filter` struct to initialize.
+ * @param version Version the struct; pass `GIT_FILTER_VERSION`
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_filter_init(git_filter *filter, unsigned int version);
/**
* Register a filter under a given name with a given priority.
diff --git a/include/git2/sys/transport.h b/include/git2/sys/transport.h
index 60e38b21a..a395de5ed 100644
--- a/include/git2/sys/transport.h
+++ b/include/git2/sys/transport.h
@@ -241,6 +241,16 @@ GIT_EXTERN(int) git_transport_smart_certificate_check(git_transport *transport,
*/
GIT_EXTERN(int) git_transport_smart_credentials(git_cred **out, git_transport *transport, const char *user, int methods);
+/**
+ * Get a copy of the proxy options
+ *
+ * The url is copied and must be freed by the caller.
+ *
+ * @param out options struct to fill
+ * @param transport the transport to extract the data from.
+ */
+GIT_EXTERN(int) git_transport_smart_proxy_options(git_proxy_options *out, git_transport *transport);
+
/*
*** End of base transport interface ***
*** Begin interface for subtransports for the smart transport ***
diff --git a/include/git2/version.h b/include/git2/version.h
index 0df191f4f..becf97bd5 100644
--- a/include/git2/version.h
+++ b/include/git2/version.h
@@ -7,12 +7,12 @@
#ifndef INCLUDE_git_version_h__
#define INCLUDE_git_version_h__
-#define LIBGIT2_VERSION "0.25.0"
+#define LIBGIT2_VERSION "0.26.0"
#define LIBGIT2_VER_MAJOR 0
-#define LIBGIT2_VER_MINOR 25
+#define LIBGIT2_VER_MINOR 26
#define LIBGIT2_VER_REVISION 0
#define LIBGIT2_VER_PATCH 0
-#define LIBGIT2_SOVERSION 25
+#define LIBGIT2_SOVERSION 26
#endif
diff --git a/include/git2/worktree.h b/include/git2/worktree.h
index 4c4f9284d..d3fa88e3f 100644
--- a/include/git2/worktree.h
+++ b/include/git2/worktree.h
@@ -74,6 +74,27 @@ GIT_EXTERN(void) git_worktree_free(git_worktree *wt);
*/
GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt);
+typedef struct git_worktree_add_options {
+ unsigned int version;
+
+ int lock; /**< lock newly created worktree */
+} git_worktree_add_options;
+
+#define GIT_WORKTREE_ADD_OPTIONS_VERSION 1
+#define GIT_WORKTREE_ADD_OPTIONS_INIT {GIT_WORKTREE_ADD_OPTIONS_VERSION,0}
+
+/**
+ * Initializes a `git_worktree_add_options` with default vaules.
+ * Equivalent to creating an instance with
+ * GIT_WORKTREE_ADD_OPTIONS_INIT.
+ *
+ * @param opts the struct to initialize
+ * @param version Verison of struct; pass `GIT_WORKTREE_ADD_OPTIONS_VERSION`
+ * @return Zero on success; -1 on failure.
+ */
+int git_worktree_add_init_options(git_worktree_add_options *opts,
+ unsigned int version);
+
/**
* Add a new working tree
*
@@ -85,9 +106,12 @@ GIT_EXTERN(int) git_worktree_validate(const git_worktree *wt);
* @param repo Repository to create working tree for
* @param name Name of the working tree
* @param path Path to create working tree at
+ * @param opts Options to modify default behavior. May be NULL
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *path);
+GIT_EXTERN(int) git_worktree_add(git_worktree **out, git_repository *repo,
+ const char *name, const char *path,
+ const git_worktree_add_options *opts);
/**
* Lock worktree if not already locked
@@ -137,23 +161,44 @@ typedef enum {
GIT_WORKTREE_PRUNE_WORKING_TREE = 1u << 2,
} git_worktree_prune_t;
+typedef struct git_worktree_prune_options {
+ unsigned int version;
+
+ uint32_t flags;
+} git_worktree_prune_options;
+
+#define GIT_WORKTREE_PRUNE_OPTIONS_VERSION 1
+#define GIT_WORKTREE_PRUNE_OPTIONS_INIT {GIT_WORKTREE_PRUNE_OPTIONS_VERSION,0}
+
+/**
+ * Initializes a `git_worktree_prune_options` with default vaules.
+ * Equivalent to creating an instance with
+ * GIT_WORKTREE_PRUNE_OPTIONS_INIT.
+ *
+ * @param opts the struct to initialize
+ * @param version Verison of struct; pass `GIT_WORKTREE_PRUNE_OPTIONS_VERSION`
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_worktree_prune_init_options(
+ git_worktree_prune_options *opts,
+ unsigned int version);
+
/**
- * Is the worktree prunable with the given set of flags?
+ * Is the worktree prunable with the given options?
*
* A worktree is not prunable in the following scenarios:
*
* - the worktree is linking to a valid on-disk worktree. The
- * GIT_WORKTREE_PRUNE_VALID flag will cause this check to be
- * ignored.
- * - the worktree is not valid but locked. The
- * GIT_WORKRTEE_PRUNE_LOCKED flag will cause this check to be
- * ignored.
+ * `valid` member will cause this check to be ignored.
+ * - the worktree is locked. The `locked` flag will cause this
+ * check to be ignored.
*
* If the worktree is not valid and not locked or if the above
* flags have been passed in, this function will return a
* positive value.
*/
-GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags);
+GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt,
+ git_worktree_prune_options *opts);
/**
* Prune working tree
@@ -163,10 +208,12 @@ GIT_EXTERN(int) git_worktree_is_prunable(git_worktree *wt, unsigned flags);
* `git_worktree_is_prunable` succeeds.
*
* @param wt Worktree to prune
- * @param flags git_worktree_prune_t flags
+ * @param opts Specifies which checks to override. See
+ * `git_worktree_is_prunable`. May be NULL
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_worktree_prune(git_worktree *wt, unsigned flags);
+GIT_EXTERN(int) git_worktree_prune(git_worktree *wt,
+ git_worktree_prune_options *opts);
/** @} */
GIT_END_DECL
diff --git a/script/cibuild.sh b/script/cibuild.sh
index 403df223e..9239136dd 100755
--- a/script/cibuild.sh
+++ b/script/cibuild.sh
@@ -43,23 +43,39 @@ ctest -V -R libgit2_clar || exit $?
killall git-daemon
-if [ "$TRAVIS_OS_NAME" = "osx" ]; then
- echo 'PasswordAuthentication yes' | sudo tee -a /etc/sshd_config
-fi
-
+# Set up sshd
+mkdir ~/sshd/
+cat >~/sshd/sshd_config<<-EOF
+ Port 2222
+ ListenAddress 0.0.0.0
+ Protocol 2
+ HostKey ${HOME}/sshd/id_rsa
+ RSAAuthentication yes
+ PasswordAuthentication yes
+ PubkeyAuthentication yes
+ ChallengeResponseAuthentication no
+ # Required here as sshd will simply close connection otherwise
+ UsePAM no
+EOF
+ssh-keygen -t rsa -f ~/sshd/id_rsa -N "" -q
+/usr/sbin/sshd -f ~/sshd/sshd_config
+
+# Set up keys
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q
cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys
-ssh-keyscan -t rsa localhost >>~/.ssh/known_hosts
+while read algorithm key comment; do
+ echo "[localhost]:2222 $algorithm $key" >>~/.ssh/known_hosts
+done <~/sshd/id_rsa.pub
# Get the fingerprint for localhost and remove the colons so we can parse it as
# a hex number. The Mac version is newer so it has a different output format.
if [ "$TRAVIS_OS_NAME" = "osx" ]; then
- export GITTEST_REMOTE_SSH_FINGERPRINT=$(ssh-keygen -E md5 -F localhost -l | tail -n 1 | cut -d ' ' -f 3 | cut -d : -f2- | tr -d :)
+ export GITTEST_REMOTE_SSH_FINGERPRINT=$(ssh-keygen -E md5 -F '[localhost]:2222' -l | tail -n 1 | cut -d ' ' -f 3 | cut -d : -f2- | tr -d :)
else
- export GITTEST_REMOTE_SSH_FINGERPRINT=$(ssh-keygen -F localhost -l | tail -n 1 | cut -d ' ' -f 2 | tr -d ':')
+ export GITTEST_REMOTE_SSH_FINGERPRINT=$(ssh-keygen -F '[localhost]:2222' -l | tail -n 1 | cut -d ' ' -f 2 | tr -d ':')
fi
-export GITTEST_REMOTE_URL="ssh://localhost/$HOME/_temp/test.git"
+export GITTEST_REMOTE_URL="ssh://localhost:2222/$HOME/_temp/test.git"
export GITTEST_REMOTE_USER=$USER
export GITTEST_REMOTE_SSH_KEY="$HOME/.ssh/id_rsa"
export GITTEST_REMOTE_SSH_PUBKEY="$HOME/.ssh/id_rsa.pub"
@@ -83,6 +99,8 @@ if [ -e ./libgit2_clar ]; then
fi
+killall sshd
+
export GITTEST_REMOTE_URL="https://github.com/libgit2/non-existent"
export GITTEST_REMOTE_USER="libgit2test"
ctest -V -R libgit2_clar-cred_callback
diff --git a/script/install-deps-osx.sh b/script/install-deps-osx.sh
index 4b8393b19..94314dbaa 100755
--- a/script/install-deps-osx.sh
+++ b/script/install-deps-osx.sh
@@ -3,6 +3,7 @@
set -x
brew update
-brew install homebrew/dupes/zlib
+brew install zlib
brew install curl
+brew install openssl
brew install libssh2
diff --git a/src/attr_file.c b/src/attr_file.c
index 0bb761d04..e30ea5e0c 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -395,9 +395,13 @@ bool git_attr_fnmatch__match(
if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir) {
bool samename;
- /* for attribute checks or root ignore checks, fail match */
+ /*
+ * for attribute checks or checks at the root of this match's
+ * containing_dir (or root of the repository if no containing_dir),
+ * do not match.
+ */
if (!(match->flags & GIT_ATTR_FNMATCH_IGNORE) ||
- path->basename == path->path)
+ path->basename == relpath)
return false;
flags |= FNM_LEADING_DIR;
diff --git a/src/attrcache.c b/src/attrcache.c
index 4df14ee2c..54161894b 100644
--- a/src/attrcache.c
+++ b/src/attrcache.c
@@ -290,14 +290,16 @@ static int attr_cache__lookup_path(
const char *cfgval = entry->value;
/* expand leading ~/ as needed */
- if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
- !git_sysdir_find_global_file(&buf, &cfgval[2]))
- *out = git_buf_detach(&buf);
- else if (cfgval)
+ if (cfgval && cfgval[0] == '~' && cfgval[1] == '/') {
+ if (! (error = git_sysdir_expand_global_file(&buf, &cfgval[2])))
+ *out = git_buf_detach(&buf);
+ } else if (cfgval) {
*out = git__strdup(cfgval);
+ }
}
- else if (!git_sysdir_find_xdg_file(&buf, fallback))
+ else if (!git_sysdir_find_xdg_file(&buf, fallback)) {
*out = git_buf_detach(&buf);
+ }
git_config_entry_free(entry);
git_buf_free(&buf);
diff --git a/src/branch.c b/src/branch.c
index 7d5e9cb7f..fe4955ad6 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -127,62 +127,31 @@ int git_branch_create_from_annotated(
repository, branch_name, commit->commit, commit->description, force);
}
-int git_branch_is_checked_out(
- const git_reference *branch)
+static int branch_equals(git_repository *repo, const char *path, void *payload)
{
- git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
- git_strarray worktrees;
- git_reference *ref = NULL;
- git_repository *repo;
- const char *worktree;
- int found = false;
- size_t i;
-
- assert(branch && git_reference_is_branch(branch));
-
- repo = git_reference_owner(branch);
+ git_reference *branch = (git_reference *) payload;
+ git_reference *head = NULL;
+ int equal = 0;
- if (git_worktree_list(&worktrees, repo) < 0)
- return -1;
-
- for (i = 0; i < worktrees.count; i++) {
- worktree = worktrees.strings[i];
-
- if (git_repository_head_for_worktree(&ref, repo, worktree) < 0)
- continue;
-
- if (git__strcmp(ref->name, branch->name) == 0) {
- found = true;
- git_reference_free(ref);
- break;
- }
-
- git_reference_free(ref);
- }
- git_strarray_free(&worktrees);
-
- if (found)
- return found;
+ if (git_reference__read_head(&head, repo, path) < 0 ||
+ git_reference_type(head) != GIT_REF_SYMBOLIC)
+ goto done;
- /* Check HEAD of parent */
- if (git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE) < 0)
- goto out;
- if (git_futils_readbuffer(&buf, path.ptr) < 0)
- goto out;
- if (git__prefixcmp(buf.ptr, "ref: ") == 0)
- git_buf_consume(&buf, buf.ptr + strlen("ref: "));
- git_buf_rtrim(&buf);
+ equal = !git__strcmp(head->target.symbolic, branch->name);
- found = git__strcmp(buf.ptr, branch->name) == 0;
+done:
+ git_reference_free(head);
+ return equal;
+}
-out:
- git_buf_free(&buf);
- git_buf_free(&path);
+int git_branch_is_checked_out(const git_reference *branch)
+{
+ assert(branch && git_reference_is_branch(branch));
- return found;
+ return git_repository_foreach_head(git_reference_owner(branch),
+ branch_equals, (void *) branch) == 1;
}
-
int git_branch_delete(git_reference *branch)
{
int is_head;
diff --git a/src/buffer.c b/src/buffer.c
index fdb732d9e..6dfcbfbe6 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -18,18 +18,19 @@ char git_buf__initbuf[1];
char git_buf__oom[1];
#define ENSURE_SIZE(b, d) \
- if ((d) > buf->asize && git_buf_grow(b, (d)) < 0)\
+ if ((d) > (b)->asize && git_buf_grow((b), (d)) < 0)\
return -1;
-void git_buf_init(git_buf *buf, size_t initial_size)
+int git_buf_init(git_buf *buf, size_t initial_size)
{
buf->asize = 0;
buf->size = 0;
buf->ptr = git_buf__initbuf;
- if (initial_size)
- git_buf_grow(buf, initial_size);
+ ENSURE_SIZE(buf, initial_size);
+
+ return 0;
}
int git_buf_try_grow(
@@ -577,7 +578,7 @@ char *git_buf_detach(git_buf *buf)
return data;
}
-void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
+int git_buf_attach(git_buf *buf, char *ptr, size_t asize)
{
git_buf_free(buf);
@@ -588,9 +589,10 @@ void git_buf_attach(git_buf *buf, char *ptr, size_t asize)
buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
else /* pass 0 to fall back on strlen + 1 */
buf->asize = buf->size + 1;
- } else {
- git_buf_grow(buf, asize);
}
+
+ ENSURE_SIZE(buf, asize);
+ return 0;
}
void git_buf_attach_notowned(git_buf *buf, const char *ptr, size_t size)
@@ -724,9 +726,7 @@ int git_buf_join(
GITERR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b);
GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep);
GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
- if (git_buf_grow(buf, alloc_len) < 0)
- return -1;
- assert(buf->ptr);
+ ENSURE_SIZE(buf, alloc_len);
/* fix up internal pointers */
if (offset_a >= 0)
@@ -780,8 +780,7 @@ int git_buf_join3(
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b);
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, len_c);
GITERR_CHECK_ALLOC_ADD(&len_total, len_total, 1);
- if (git_buf_grow(buf, len_total) < 0)
- return -1;
+ ENSURE_SIZE(buf, len_total);
tgt = buf->ptr;
diff --git a/src/buffer.h b/src/buffer.h
index a76b2d771..b0aece488 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -34,7 +34,7 @@ GIT_INLINE(bool) git_buf_is_allocated(const git_buf *buf)
* For the cases where GIT_BUF_INIT cannot be used to do static
* initialization.
*/
-extern void git_buf_init(git_buf *buf, size_t initial_size);
+extern int git_buf_init(git_buf *buf, size_t initial_size);
/**
* Resize the buffer allocation to make more space.
@@ -73,7 +73,7 @@ extern void git_buf_sanitize(git_buf *buf);
extern void git_buf_swap(git_buf *buf_a, git_buf *buf_b);
extern char *git_buf_detach(git_buf *buf);
-extern void git_buf_attach(git_buf *buf, char *ptr, size_t asize);
+extern int git_buf_attach(git_buf *buf, char *ptr, size_t asize);
/* Populates a `git_buf` where the contents are not "owned" by the
* buffer, and calls to `git_buf_free` will not free the given buf.
diff --git a/src/checkout.c b/src/checkout.c
index 9d1eed59f..25018d291 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -370,10 +370,8 @@ static int checkout_action_wd_only(
*/
const git_index_entry *e = git_index_get_byindex(data->index, pos);
- if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) {
- notify = GIT_CHECKOUT_NOTIFY_DIRTY;
- remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
- }
+ if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0)
+ return git_iterator_advance_into(wditem, workdir);
}
}
diff --git a/src/config.c b/src/config.c
index 0d73ad2cc..169a62880 100644
--- a/src/config.c
+++ b/src/config.c
@@ -576,22 +576,50 @@ int git_config_foreach_match(
* Setters
**************/
-static int config_error_nofiles(const char *name)
+typedef enum {
+ BACKEND_USE_SET,
+ BACKEND_USE_DELETE
+} backend_use;
+
+static const char *uses[] = {
+ "set",
+ "delete"
+};
+
+static int get_backend_for_use(git_config_backend **out,
+ git_config *cfg, const char *name, backend_use use)
{
+ size_t i;
+ file_internal *f;
+
+ *out = NULL;
+
+ if (git_vector_length(&cfg->files) == 0) {
+ giterr_set(GITERR_CONFIG,
+ "cannot %s value for '%s' when no config files exist",
+ uses[use], name);
+ return GIT_ENOTFOUND;
+ }
+
+ git_vector_foreach(&cfg->files, i, f) {
+ if (!f->file->readonly) {
+ *out = f->file;
+ return 0;
+ }
+ }
+
giterr_set(GITERR_CONFIG,
- "cannot set value for '%s' when no config files exist", name);
+ "cannot %s value for '%s' when all config files are readonly",
+ uses[use], name);
return GIT_ENOTFOUND;
}
int git_config_delete_entry(git_config *cfg, const char *name)
{
git_config_backend *file;
- file_internal *internal;
- internal = git_vector_get(&cfg->files, 0);
- if (!internal || !internal->file)
- return config_error_nofiles(name);
- file = internal->file;
+ if (get_backend_for_use(&file, cfg, name, BACKEND_USE_DELETE) < 0)
+ return GIT_ENOTFOUND;
return file->del(file, name);
}
@@ -617,17 +645,14 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
{
int error;
git_config_backend *file;
- file_internal *internal;
if (!value) {
giterr_set(GITERR_CONFIG, "the value to set cannot be NULL");
return -1;
}
- internal = git_vector_get(&cfg->files, 0);
- if (!internal || !internal->file)
- return config_error_nofiles(name);
- file = internal->file;
+ if (get_backend_for_use(&file, cfg, name, BACKEND_USE_SET) < 0)
+ return GIT_ENOTFOUND;
error = file->set(file, name, value);
@@ -1032,12 +1057,9 @@ on_error:
int git_config_set_multivar(git_config *cfg, const char *name, const char *regexp, const char *value)
{
git_config_backend *file;
- file_internal *internal;
- internal = git_vector_get(&cfg->files, 0);
- if (!internal || !internal->file)
- return config_error_nofiles(name);
- file = internal->file;
+ if (get_backend_for_use(&file, cfg, name, BACKEND_USE_DELETE) < 0)
+ return GIT_ENOTFOUND;
return file->set_multivar(file, name, regexp, value);
}
@@ -1045,12 +1067,9 @@ int git_config_set_multivar(git_config *cfg, const char *name, const char *regex
int git_config_delete_multivar(git_config *cfg, const char *name, const char *regexp)
{
git_config_backend *file;
- file_internal *internal;
- internal = git_vector_get(&cfg->files, 0);
- if (!internal || !internal->file)
- return config_error_nofiles(name);
- file = internal->file;
+ if (get_backend_for_use(&file, cfg, name, BACKEND_USE_DELETE) < 0)
+ return GIT_ENOTFOUND;
return file->del_multivar(file, name, regexp);
}
@@ -1339,9 +1358,6 @@ fail_parse:
int git_config_parse_path(git_buf *out, const char *value)
{
- int error = 0;
- const git_buf *home;
-
assert(out && value);
git_buf_sanitize(out);
@@ -1352,16 +1368,7 @@ int git_config_parse_path(git_buf *out, const char *value)
return -1;
}
- if ((error = git_sysdir_get(&home, GIT_SYSDIR_GLOBAL)) < 0)
- return error;
-
- git_buf_sets(out, home->ptr);
- git_buf_puts(out, value + 1);
-
- if (git_buf_oom(out))
- return -1;
-
- return 0;
+ return git_sysdir_expand_global_file(out, value[1] ? &value[2] : NULL);
}
return git_buf_sets(out, value);
diff --git a/src/config_file.c b/src/config_file.c
index 7df43c85f..e15d57bbb 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1256,7 +1256,7 @@ static int included_path(git_buf *out, const char *dir, const char *path)
{
/* From the user's home */
if (path[0] == '~' && path[1] == '/')
- return git_sysdir_find_global_file(out, &path[1]);
+ return git_sysdir_expand_global_file(out, &path[1]);
return git_path_join_unrooted(out, path, dir, NULL);
}
@@ -1267,7 +1267,7 @@ static const char *escaped = "\n\t\b\"\\";
/* Escape the values to write them to the file */
static char *escape_value(const char *ptr)
{
- git_buf buf = GIT_BUF_INIT;
+ git_buf buf;
size_t len;
const char *esc;
@@ -1277,7 +1277,8 @@ static char *escape_value(const char *ptr)
if (!len)
return git__calloc(1, sizeof(char));
- git_buf_grow(&buf, len);
+ if (git_buf_init(&buf, len) < 0)
+ return NULL;
while (*ptr != '\0') {
if ((esc = strchr(escaped, *ptr)) != NULL) {
diff --git a/src/config_file.h b/src/config_file.h
index 1c52892c3..654e6cacf 100644
--- a/src/config_file.h
+++ b/src/config_file.h
@@ -7,6 +7,7 @@
#ifndef INCLUDE_config_file_h__
#define INCLUDE_config_file_h__
+#include "git2/sys/config.h"
#include "git2/config.h"
GIT_INLINE(int) git_config_file_open(git_config_backend *cfg, unsigned int level)
diff --git a/src/diff_parse.c b/src/diff_parse.c
index 24a8a4af6..5e3a7a177 100644
--- a/src/diff_parse.c
+++ b/src/diff_parse.c
@@ -45,7 +45,7 @@ static git_diff_parsed *diff_parsed_alloc(void)
diff->base.free_fn = diff_parsed_free;
if (git_diff_init_options(&diff->base.opts, GIT_DIFF_OPTIONS_VERSION) < 0) {
- git__free(&diff);
+ git__free(diff);
return NULL;
}
diff --git a/src/fileops.c b/src/fileops.c
index a0a2795c6..2f3f58d4f 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -770,6 +770,9 @@ static int futils__rmdir_empty_parent(void *opaque, const char *path)
if (en == ENOENT || en == ENOTDIR) {
/* do nothing */
+ } else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0 &&
+ en == EBUSY) {
+ error = git_path_set_error(errno, path, "rmdir");
} else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
error = GIT_ITEROVER;
} else {
@@ -1155,9 +1158,13 @@ int git_futils_fsync_dir(const char *path)
int git_futils_fsync_parent(const char *path)
{
- char *parent = git_path_dirname(path);
- int error = git_futils_fsync_dir(parent);
+ char *parent;
+ int error;
+
+ if ((parent = git_path_dirname(path)) == NULL)
+ return -1;
+ error = git_futils_fsync_dir(parent);
git__free(parent);
return error;
}
diff --git a/src/filter.c b/src/filter.c
index e74cc1053..361e08529 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -895,7 +895,7 @@ static int stream_list_init(
git_array_size(filters->filters) - 1 - i : i;
git_filter_entry *fe = git_array_get(filters->filters, filter_idx);
git_writestream *filter_stream;
-
+
assert(fe->filter->stream || fe->filter->apply);
/* If necessary, create a stream that proxies the traditional
@@ -1022,3 +1022,9 @@ int git_filter_list_stream_blob(
return git_filter_list_stream_data(filters, &in, target);
}
+
+int git_filter_init(git_filter *filter, unsigned int version)
+{
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT);
+ return 0;
+}
diff --git a/src/global.c b/src/global.c
index e2ad8fe71..afa57e1d6 100644
--- a/src/global.c
+++ b/src/global.c
@@ -22,7 +22,7 @@
git_mutex git__mwindow_mutex;
-#define MAX_SHUTDOWN_CB 8
+#define MAX_SHUTDOWN_CB 9
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
static git_atomic git__n_shutdown_callbacks;
diff --git a/src/hash/sha1dc/sha1.c b/src/hash/sha1dc/sha1.c
index 8d12b832b..facea1bb5 100644
--- a/src/hash/sha1dc/sha1.c
+++ b/src/hash/sha1dc/sha1.c
@@ -5,31 +5,75 @@
* https://opensource.org/licenses/MIT
***/
+#ifndef SHA1DC_NO_STANDARD_INCLUDES
#include <string.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
+#endif
+
+#ifdef SHA1DC_CUSTOM_INCLUDE_SHA1_C
+#include SHA1DC_CUSTOM_INCLUDE_SHA1_C
+#endif
+
+#ifndef SHA1DC_INIT_SAFE_HASH_DEFAULT
+#define SHA1DC_INIT_SAFE_HASH_DEFAULT 1
+#endif
#include "sha1.h"
#include "ubc_check.h"
-/*
+/*
Because Little-Endian architectures are most common,
- we only set BIGENDIAN if one of these conditions is met.
+ we only set SHA1DC_BIGENDIAN if one of these conditions is met.
Note that all MSFT platforms are little endian,
so none of these will be defined under the MSC compiler.
If you are compiling on a big endian platform and your compiler does not define one of these,
you will have to add whatever macros your tool chain defines to indicate Big-Endianness.
*/
-#if (defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || \
- (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)) || \
- defined(__BIG_ENDIAN__) || defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
- defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
+#ifdef SHA1DC_BIGENDIAN
+#undef SHA1DC_BIGENDIAN
+#endif
+
+#if (defined(_BYTE_ORDER) || defined(__BYTE_ORDER) || defined(__BYTE_ORDER__))
+
+#if ((defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)) || \
+ (defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN)) || \
+ (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)) )
+#define SHA1DC_BIGENDIAN
+#endif
+
+#else
+
+#if (defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN) || defined(__BIG_ENDIAN__) || \
+ defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
+ defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || \
+ defined(__sparc))
+#define SHA1DC_BIGENDIAN
+#endif
+
+#endif
+
+#if (defined(SHA1DC_FORCE_LITTLEENDIAN) && defined(SHA1DC_BIGENDIAN))
+#undef SHA1DC_BIGENDIAN
+#endif
+#if (defined(SHA1DC_FORCE_BIGENDIAN) && !defined(SHA1DC_BIGENDIAN))
+#define SHA1DC_BIGENDIAN
+#endif
+/*ENDIANNESS SELECTION*/
+
+#if (defined SHA1DC_FORCE_UNALIGNED_ACCESS || \
+ defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || \
+ defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || \
+ defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || \
+ defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || \
+ defined(__386) || defined(_M_X64) || defined(_M_AMD64))
+
+#define SHA1DC_ALLOW_UNALIGNED_ACCESS
-#define BIGENDIAN (1)
+#endif /*UNALIGNMENT DETECTION*/
-#endif /*ENDIANNESS SELECTION*/
#define rotate_right(x,n) (((x)>>(n))|((x)<<(32-(n))))
#define rotate_left(x,n) (((x)<<(n))|((x)>>(32-(n))))
@@ -39,11 +83,11 @@
#define sha1_mix(W, t) (rotate_left(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1))
-#if defined(BIGENDIAN)
+#ifdef SHA1DC_BIGENDIAN
#define sha1_load(m, t, temp) { temp = m[t]; }
#else
#define sha1_load(m, t, temp) { temp = m[t]; sha1_bswap32(temp); }
-#endif /*define(BIGENDIAN)*/
+#endif
#define sha1_store(W, t, x) *(volatile uint32_t *)&W[t] = x
@@ -872,6 +916,11 @@ static void sha1recompress_fast_ ## t (uint32_t ihvin[5], uint32_t ihvout[5], co
ihvout[0] = ihvin[0] + a; ihvout[1] = ihvin[1] + b; ihvout[2] = ihvin[2] + c; ihvout[3] = ihvin[3] + d; ihvout[4] = ihvin[4] + e; \
}
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable: 4127) /* Complier complains about the checks in the above macro being constant. */
+#endif
+
#ifdef DOSTORESTATE0
SHA1_RECOMPRESS(0)
#endif
@@ -1192,6 +1241,10 @@ SHA1_RECOMPRESS(78)
SHA1_RECOMPRESS(79)
#endif
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif
+
static void sha1_recompression_step(uint32_t step, uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5])
{
switch (step)
@@ -1609,7 +1662,7 @@ static void sha1_process(SHA1_CTX* ctx, const uint32_t block[16])
unsigned i, j;
uint32_t ubc_dv_mask[DVMASKSIZE] = { 0xFFFFFFFF };
uint32_t ihvtmp[5];
-
+
ctx->ihv1[0] = ctx->ihv[0];
ctx->ihv1[1] = ctx->ihv[1];
ctx->ihv1[2] = ctx->ihv[2];
@@ -1665,7 +1718,7 @@ void SHA1DCInit(SHA1_CTX* ctx)
ctx->ihv[3] = 0x10325476;
ctx->ihv[4] = 0xC3D2E1F0;
ctx->found_collision = 0;
- ctx->safe_hash = 1;
+ ctx->safe_hash = SHA1DC_INIT_SAFE_HASH_DEFAULT;
ctx->ubc_check = 1;
ctx->detect_coll = 1;
ctx->reduced_round_coll = 0;
@@ -1713,6 +1766,7 @@ void SHA1DCSetCallback(SHA1_CTX* ctx, collision_block_callback callback)
void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
{
unsigned left, fill;
+
if (len == 0)
return;
@@ -1731,7 +1785,13 @@ void SHA1DCUpdate(SHA1_CTX* ctx, const char* buf, size_t len)
while (len >= 64)
{
ctx->total += 64;
+
+#if defined(SHA1DC_ALLOW_UNALIGNED_ACCESS)
sha1_process(ctx, (uint32_t*)(buf));
+#else
+ memcpy(ctx->buffer, buf, 64);
+ sha1_process(ctx, (uint32_t*)(ctx->buffer));
+#endif /* defined(SHA1DC_ALLOW_UNALIGNED_ACCESS) */
buf += 64;
len -= 64;
}
@@ -1790,3 +1850,7 @@ int SHA1DCFinal(unsigned char output[20], SHA1_CTX *ctx)
output[19] = (unsigned char)(ctx->ihv[4]);
return ctx->found_collision;
}
+
+#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C
+#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_C
+#endif
diff --git a/src/hash/sha1dc/sha1.h b/src/hash/sha1dc/sha1.h
index e867724c0..1e4e94be5 100644
--- a/src/hash/sha1dc/sha1.h
+++ b/src/hash/sha1dc/sha1.h
@@ -5,42 +5,37 @@
* https://opensource.org/licenses/MIT
***/
+#ifndef SHA1DC_SHA1_H
+#define SHA1DC_SHA1_H
+
#if defined(__cplusplus)
extern "C" {
#endif
+#ifndef SHA1DC_NO_STANDARD_INCLUDES
#include <stdint.h>
+#endif
-/* uses SHA-1 message expansion to expand the first 16 words of W[] to 80 words */
-/* void sha1_message_expansion(uint32_t W[80]); */
-
-/* sha-1 compression function; first version takes a message block pre-parsed as 16 32-bit integers, second version takes an already expanded message) */
-/* void sha1_compression(uint32_t ihv[5], const uint32_t m[16]);
-void sha1_compression_W(uint32_t ihv[5], const uint32_t W[80]); */
-
-/* same as sha1_compression_W, but additionally store intermediate states */
+/* sha-1 compression function that takes an already expanded message, and additionally store intermediate states */
/* only stores states ii (the state between step ii-1 and step ii) when DOSTORESTATEii is defined in ubc_check.h */
void sha1_compression_states(uint32_t[5], const uint32_t[16], uint32_t[80], uint32_t[80][5]);
/*
-// function type for sha1_recompression_step_T (uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5])
-// where 0 <= T < 80
-// me2 is an expanded message (the expansion of an original message block XOR'ed with a disturbance vector's message block difference)
-// state is the internal state (a,b,c,d,e) before step T of the SHA-1 compression function while processing the original message block
-// the function will return:
-// ihvin: the reconstructed input chaining value
-// ihvout: the reconstructed output chaining value
+// Function type for sha1_recompression_step_T (uint32_t ihvin[5], uint32_t ihvout[5], const uint32_t me2[80], const uint32_t state[5]).
+// Where 0 <= T < 80
+// me2 is an expanded message (the expansion of an original message block XOR'ed with a disturbance vector's message block difference.)
+// state is the internal state (a,b,c,d,e) before step T of the SHA-1 compression function while processing the original message block.
+// The function will return:
+// ihvin: The reconstructed input chaining value.
+// ihvout: The reconstructed output chaining value.
*/
typedef void(*sha1_recompression_type)(uint32_t*, uint32_t*, const uint32_t*, const uint32_t*);
-/* table of sha1_recompression_step_0, ... , sha1_recompression_step_79 */
-/* extern sha1_recompression_type sha1_recompression_step[80];*/
-
-/* a callback function type that can be set to be called when a collision block has been found: */
+/* A callback function type that can be set to be called when a collision block has been found: */
/* void collision_block_callback(uint64_t byteoffset, const uint32_t ihvin1[5], const uint32_t ihvin2[5], const uint32_t m1[80], const uint32_t m2[80]) */
typedef void(*collision_block_callback)(uint64_t, const uint32_t*, const uint32_t*, const uint32_t*, const uint32_t*);
-/* the SHA-1 context */
+/* The SHA-1 context. */
typedef struct {
uint64_t total;
uint32_t ihv[5];
@@ -59,30 +54,34 @@ typedef struct {
uint32_t states[80][5];
} SHA1_CTX;
-/* initialize SHA-1 context */
+/* Initialize SHA-1 context. */
void SHA1DCInit(SHA1_CTX*);
/*
-// function to enable safe SHA-1 hashing:
-// collision attacks are thwarted by hashing a detected near-collision block 3 times
-// think of it as extending SHA-1 from 80-steps to 240-steps for such blocks:
-// the best collision attacks against SHA-1 have complexity about 2^60,
-// thus for 240-steps an immediate lower-bound for the best cryptanalytic attacks would 2^180
-// an attacker would be better off using a generic birthday search of complexity 2^80
-//
-// enabling safe SHA-1 hashing will result in the correct SHA-1 hash for messages where no collision attack was detected
-// but it will result in a different SHA-1 hash for messages where a collision attack was detected
-// this will automatically invalidate SHA-1 based digital signature forgeries
-// enabled by default
+ Function to enable safe SHA-1 hashing:
+ Collision attacks are thwarted by hashing a detected near-collision block 3 times.
+ Think of it as extending SHA-1 from 80-steps to 240-steps for such blocks:
+ The best collision attacks against SHA-1 have complexity about 2^60,
+ thus for 240-steps an immediate lower-bound for the best cryptanalytic attacks would be 2^180.
+ An attacker would be better off using a generic birthday search of complexity 2^80.
+
+ Enabling safe SHA-1 hashing will result in the correct SHA-1 hash for messages where no collision attack was detected,
+ but it will result in a different SHA-1 hash for messages where a collision attack was detected.
+ This will automatically invalidate SHA-1 based digital signature forgeries.
+ Enabled by default.
*/
void SHA1DCSetSafeHash(SHA1_CTX*, int);
-/* function to disable or enable the use of Unavoidable Bitconditions (provides a significant speed up) */
-/* enabled by default */
+/*
+ Function to disable or enable the use of Unavoidable Bitconditions (provides a significant speed up).
+ Enabled by default
+ */
void SHA1DCSetUseUBC(SHA1_CTX*, int);
-/* function to disable or enable the use of Collision Detection */
-/* enabled by default */
+/*
+ Function to disable or enable the use of Collision Detection.
+ Enabled by default.
+ */
void SHA1DCSetUseDetectColl(SHA1_CTX*, int);
/* function to disable or enable the detection of reduced-round SHA-1 collisions */
@@ -98,8 +97,14 @@ void SHA1DCUpdate(SHA1_CTX*, const char*, size_t);
/* obtain SHA-1 hash from SHA-1 context */
/* returns: 0 = no collision detected, otherwise = collision found => warn user for active attack */
-int SHA1DCFinal(unsigned char[20], SHA1_CTX*);
+int SHA1DCFinal(unsigned char[20], SHA1_CTX*);
#if defined(__cplusplus)
}
#endif
+
+#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
+#include SHA1DC_CUSTOM_TRAILING_INCLUDE_SHA1_H
+#endif
+
+#endif
diff --git a/src/hash/sha1dc/ubc_check.c b/src/hash/sha1dc/ubc_check.c
index 27d0976da..b3beff2af 100644
--- a/src/hash/sha1dc/ubc_check.c
+++ b/src/hash/sha1dc/ubc_check.c
@@ -24,7 +24,12 @@
// ubc_check has been verified against ubc_check_verify using the 'ubc_check_test' program in the tools section
*/
+#ifndef SHA1DC_NO_STANDARD_INCLUDES
#include <stdint.h>
+#endif
+#ifdef SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C
+#include SHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C
+#endif
#include "ubc_check.h"
static const uint32_t DV_I_43_0_bit = (uint32_t)(1) << 0;
@@ -361,3 +366,7 @@ if (mask) {
dvmask[0]=mask;
}
+
+#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_C
+#include SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_C
+#endif
diff --git a/src/hash/sha1dc/ubc_check.h b/src/hash/sha1dc/ubc_check.h
index b349bed92..d7e17dc73 100644
--- a/src/hash/sha1dc/ubc_check.h
+++ b/src/hash/sha1dc/ubc_check.h
@@ -20,14 +20,16 @@
// thus one needs to do the recompression check for each DV that has its bit set
*/
-#ifndef UBC_CHECK_H
-#define UBC_CHECK_H
+#ifndef SHA1DC_UBC_CHECK_H
+#define SHA1DC_UBC_CHECK_H
#if defined(__cplusplus)
extern "C" {
#endif
+#ifndef SHA1DC_NO_STANDARD_INCLUDES
#include <stdint.h>
+#endif
#define DVMASKSIZE 1
typedef struct { int dvType; int dvK; int dvB; int testt; int maski; int maskb; uint32_t dm[80]; } dv_info_t;
@@ -43,4 +45,8 @@ void ubc_check(const uint32_t W[80], uint32_t dvmask[DVMASKSIZE]);
}
#endif
-#endif /* UBC_CHECK_H */
+#ifdef SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H
+#include SHA1DC_CUSTOM_TRAILING_INCLUDE_UBC_CHECK_H
+#endif
+
+#endif
diff --git a/src/index.c b/src/index.c
index 932a5306a..c29e90fb0 100644
--- a/src/index.c
+++ b/src/index.c
@@ -54,10 +54,6 @@ static int index_apply_to_wd_diff(git_index *index, int action, const git_strarr
unsigned int flags,
git_index_matched_path_cb cb, void *payload);
-#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
-#define short_entry_size(len) entry_size(struct entry_short, len)
-#define long_entry_size(len) entry_size(struct entry_long, len)
-
#define minimal_entry_size (offsetof(struct entry_short, path))
static const size_t INDEX_FOOTER_SIZE = GIT_OID_RAWSZ;
@@ -2282,12 +2278,29 @@ out_err:
return 0;
}
+static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags)
+{
+ if (varint_len) {
+ if (flags & GIT_IDXENTRY_EXTENDED)
+ return offsetof(struct entry_long, path) + path_len + 1 + varint_len;
+ else
+ return offsetof(struct entry_short, path) + path_len + 1 + varint_len;
+ } else {
+#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7)
+ if (flags & GIT_IDXENTRY_EXTENDED)
+ return entry_size(struct entry_long, path_len);
+ else
+ return entry_size(struct entry_short, path_len);
+#undef entry_size
+ }
+}
+
static size_t read_entry(
git_index_entry **out,
git_index *index,
const void *buffer,
size_t buffer_size,
- const char **last)
+ const char *last)
{
size_t path_length, entry_size;
const char *path_ptr;
@@ -2344,35 +2357,34 @@ static size_t read_entry(
path_length = path_end - path_ptr;
}
- if (entry.flags & GIT_IDXENTRY_EXTENDED)
- entry_size = long_entry_size(path_length);
- else
- entry_size = short_entry_size(path_length);
-
- if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
- return 0;
-
+ entry_size = index_entry_size(path_length, 0, entry.flags);
entry.path = (char *)path_ptr;
} else {
size_t varint_len;
- size_t shared = git_decode_varint((const unsigned char *)path_ptr,
- &varint_len);
- size_t len = strlen(path_ptr + varint_len);
- size_t last_len = strlen(*last);
- size_t tmp_path_len;
+ size_t strip_len = git_decode_varint((const unsigned char *)path_ptr,
+ &varint_len);
+ size_t last_len = strlen(last);
+ size_t prefix_len = last_len - strip_len;
+ size_t suffix_len = strlen(path_ptr + varint_len);
+ size_t path_len;
if (varint_len == 0)
return index_error_invalid("incorrect prefix length");
- GITERR_CHECK_ALLOC_ADD(&tmp_path_len, shared, len + 1);
- tmp_path = git__malloc(tmp_path_len);
+ GITERR_CHECK_ALLOC_ADD(&path_len, prefix_len, suffix_len);
+ GITERR_CHECK_ALLOC_ADD(&path_len, path_len, 1);
+ tmp_path = git__malloc(path_len);
GITERR_CHECK_ALLOC(tmp_path);
- memcpy(tmp_path, last, last_len);
- memcpy(tmp_path + last_len, path_ptr + varint_len, len);
- entry_size = long_entry_size(shared + len);
+
+ memcpy(tmp_path, last, prefix_len);
+ memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1);
+ entry_size = index_entry_size(suffix_len, varint_len, entry.flags);
entry.path = tmp_path;
}
+ if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
+ return 0;
+
if (index_entry_dup(out, index, &entry) < 0) {
git__free(tmp_path);
return 0;
@@ -2445,7 +2457,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
unsigned int i;
struct index_header header = { 0 };
git_oid checksum_calculated, checksum_expected;
- const char **last = NULL;
+ const char *last = NULL;
const char *empty = "";
#define seek_forward(_increase) { \
@@ -2469,7 +2481,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
index->version = header.version;
if (index->version >= INDEX_VERSION_NUMBER_COMP)
- last = &empty;
+ last = empty;
seek_forward(INDEX_HEADER_SIZE);
@@ -2504,6 +2516,9 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
}
error = 0;
+ if (index->version >= INDEX_VERSION_NUMBER_COMP)
+ last = entry->path;
+
seek_forward(entry_size);
}
@@ -2574,11 +2589,12 @@ static bool is_index_extended(git_index *index)
return (extended > 0);
}
-static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char **last)
+static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last)
{
void *mem = NULL;
struct entry_short *ondisk;
size_t path_len, disk_size;
+ int varint_len = 0;
char *path;
const char *path_start = entry->path;
size_t same_len = 0;
@@ -2586,7 +2602,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
path_len = ((struct entry_internal *)entry)->pathlen;
if (last) {
- const char *last_c = *last;
+ const char *last_c = last;
while (*path_start == *last_c) {
if (!*path_start || !*last_c)
@@ -2596,13 +2612,10 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
++same_len;
}
path_len -= same_len;
- *last = entry->path;
+ varint_len = git_encode_varint(NULL, 0, same_len);
}
- if (entry->flags & GIT_IDXENTRY_EXTENDED)
- disk_size = long_entry_size(path_len);
- else
- disk_size = short_entry_size(path_len);
+ disk_size = index_entry_size(path_len, varint_len, entry->flags);
if (git_filebuf_reserve(file, &mem, disk_size) < 0)
return -1;
@@ -2642,16 +2655,34 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
ondisk_ext->flags_extended = htons(entry->flags_extended &
GIT_IDXENTRY_EXTENDED_FLAGS);
path = ondisk_ext->path;
- }
- else
+ disk_size -= offsetof(struct entry_long, path);
+ } else {
path = ondisk->path;
+ disk_size -= offsetof(struct entry_short, path);
+ }
if (last) {
- path += git_encode_varint((unsigned char *) path,
- disk_size,
- path_len - same_len);
+ varint_len = git_encode_varint((unsigned char *) path,
+ disk_size, same_len);
+ assert(varint_len > 0);
+ path += varint_len;
+ disk_size -= varint_len;
+
+ /*
+ * If using path compression, we are not allowed
+ * to have additional trailing NULs.
+ */
+ assert(disk_size == path_len + 1);
+ } else {
+ /*
+ * If no path compression is used, we do have
+ * NULs as padding. As such, simply assert that
+ * we have enough space left to write the path.
+ */
+ assert(disk_size > path_len);
}
- memcpy(path, path_start, path_len);
+
+ memcpy(path, path_start, path_len + 1);
return 0;
}
@@ -2662,8 +2693,7 @@ static int write_entries(git_index *index, git_filebuf *file)
size_t i;
git_vector case_sorted, *entries;
git_index_entry *entry;
- const char **last = NULL;
- const char *empty = "";
+ const char *last = NULL;
/* If index->entries is sorted case-insensitively, then we need
* to re-sort it case-sensitively before writing */
@@ -2676,11 +2706,14 @@ static int write_entries(git_index *index, git_filebuf *file)
}
if (index->version >= INDEX_VERSION_NUMBER_COMP)
- last = &empty;
+ last = "";
- git_vector_foreach(entries, i, entry)
+ git_vector_foreach(entries, i, entry) {
if ((error = write_disk_entry(file, entry, last)) < 0)
break;
+ if (index->version >= INDEX_VERSION_NUMBER_COMP)
+ last = entry->path;
+ }
if (index->ignore_case)
git_vector_free(&case_sorted);
diff --git a/src/indexer.c b/src/indexer.c
index ce67240ce..15f6cc2c4 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -125,7 +125,7 @@ int git_indexer_new(
git_hash_ctx_init(&idx->hash_ctx);
git_hash_ctx_init(&idx->trailer);
- if (git_object__synchronous_writing)
+ if (git_repository__fsync_gitdir)
idx->do_fsync = 1;
error = git_buf_joinpath(&path, prefix, suff);
@@ -937,7 +937,6 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
git_buf filename = GIT_BUF_INIT;
struct entry *entry;
git_oid trailer_hash, file_hash;
- git_hash_ctx ctx;
git_filebuf index_file = {0};
void *packfile_trailer;
@@ -946,9 +945,6 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
return -1;
}
- if (git_hash_ctx_init(&ctx) < 0)
- return -1;
-
/* Test for this before resolve_deltas(), as it plays with idx->off */
if (idx->off + 20 < idx->pack->mwf.size) {
giterr_set(GITERR_INDEXER, "unexpected data at the end of the pack");
@@ -992,6 +988,10 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
git_vector_sort(&idx->objects);
+ /* Use the trailer hash as the pack file name to ensure
+ * files with different contents have different names */
+ git_oid_cpy(&idx->hash, &trailer_hash);
+
git_buf_sets(&filename, idx->pack->pack_name);
git_buf_shorten(&filename, strlen("pack"));
git_buf_puts(&filename, "idx");
@@ -1018,9 +1018,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
/* Write out the object names (SHA-1 hashes) */
git_vector_foreach(&idx->objects, i, entry) {
git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
- git_hash_update(&ctx, &entry->oid, GIT_OID_RAWSZ);
}
- git_hash_final(&idx->hash, &ctx);
/* Write out the CRC32 values */
git_vector_foreach(&idx->objects, i, entry) {
@@ -1106,14 +1104,12 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
idx->pack_committed = 1;
git_buf_free(&filename);
- git_hash_ctx_cleanup(&ctx);
return 0;
on_error:
git_mwindow_free_all(&idx->pack->mwf);
git_filebuf_cleanup(&index_file);
git_buf_free(&filename);
- git_hash_ctx_cleanup(&ctx);
return -1;
}
diff --git a/src/object.c b/src/object.c
index bd87c9310..2da36a2ee 100644
--- a/src/object.c
+++ b/src/object.c
@@ -16,7 +16,6 @@
#include "tag.h"
bool git_object__strict_input_validation = true;
-bool git_object__synchronous_writing = false;
typedef struct {
const char *str; /* type name string */
diff --git a/src/object.h b/src/object.h
index 13117e4c3..dd227d16d 100644
--- a/src/object.h
+++ b/src/object.h
@@ -10,7 +10,6 @@
#include "repository.h"
extern bool git_object__strict_input_validation;
-extern bool git_object__synchronous_writing;
/** Base git object for inheritance */
struct git_object {
diff --git a/src/odb.c b/src/odb.c
index cf321f51e..ae8f247e3 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -31,6 +31,8 @@
#define GIT_ALTERNATES_MAX_DEPTH 5
+bool git_odb__strict_hash_verification = true;
+
typedef struct
{
git_odb_backend *backend;
@@ -998,7 +1000,9 @@ static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
size_t i;
git_rawobj raw;
git_odb_object *object;
+ git_oid hashed;
bool found = false;
+ int error = 0;
if (!only_refreshed && odb_read_hardcoded(&raw, id) == 0)
found = true;
@@ -1011,7 +1015,7 @@ static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
continue;
if (b->read != NULL) {
- int error = b->read(&raw.data, &raw.len, &raw.type, b, id);
+ error = b->read(&raw.data, &raw.len, &raw.type, b, id);
if (error == GIT_PASSTHROUGH || error == GIT_ENOTFOUND)
continue;
@@ -1025,12 +1029,26 @@ static int odb_read_1(git_odb_object **out, git_odb *db, const git_oid *id,
if (!found)
return GIT_ENOTFOUND;
+ if (git_odb__strict_hash_verification) {
+ if ((error = git_odb_hash(&hashed, raw.data, raw.len, raw.type)) < 0)
+ goto out;
+
+ if (!git_oid_equal(id, &hashed)) {
+ error = git_odb__error_mismatch(id, &hashed);
+ goto out;
+ }
+ }
+
giterr_clear();
if ((object = odb_object__alloc(id, &raw)) == NULL)
- return -1;
+ goto out;
*out = git_cache_store_raw(odb_cache(db), object);
- return 0;
+
+out:
+ if (error)
+ git__free(raw.data);
+ return error;
}
int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
@@ -1081,9 +1099,9 @@ static int read_prefix_1(git_odb_object **out, git_odb *db,
const git_oid *key, size_t len, bool only_refreshed)
{
size_t i;
- int error = GIT_ENOTFOUND;
+ int error = 0;
git_oid found_full_oid = {{0}};
- git_rawobj raw;
+ git_rawobj raw = {0};
void *data = NULL;
bool found = false;
git_odb_object *object;
@@ -1098,18 +1116,29 @@ static int read_prefix_1(git_odb_object **out, git_odb *db,
if (b->read_prefix != NULL) {
git_oid full_oid;
error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, key, len);
- if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH)
+
+ if (error == GIT_ENOTFOUND || error == GIT_PASSTHROUGH) {
+ error = 0;
continue;
+ }
if (error)
- return error;
+ goto out;
git__free(data);
data = raw.data;
if (found && git_oid__cmp(&full_oid, &found_full_oid)) {
- git__free(raw.data);
- return git_odb__error_ambiguous("multiple matches for prefix");
+ git_buf buf = GIT_BUF_INIT;
+
+ git_buf_printf(&buf, "multiple matches for prefix: %s",
+ git_oid_tostr_s(&full_oid));
+ git_buf_printf(&buf, " %s",
+ git_oid_tostr_s(&found_full_oid));
+
+ error = git_odb__error_ambiguous(buf.ptr);
+ git_buf_free(&buf);
+ goto out;
}
found_full_oid = full_oid;
@@ -1120,11 +1149,28 @@ static int read_prefix_1(git_odb_object **out, git_odb *db,
if (!found)
return GIT_ENOTFOUND;
+ if (git_odb__strict_hash_verification) {
+ git_oid hash;
+
+ if ((error = git_odb_hash(&hash, raw.data, raw.len, raw.type)) < 0)
+ goto out;
+
+ if (!git_oid_equal(&found_full_oid, &hash)) {
+ error = git_odb__error_mismatch(&found_full_oid, &hash);
+ goto out;
+ }
+ }
+
if ((object = odb_object__alloc(&found_full_oid, &raw)) == NULL)
- return -1;
+ goto out;
*out = git_cache_store_raw(odb_cache(db), object);
- return 0;
+
+out:
+ if (error)
+ git__free(raw.data);
+
+ return error;
}
int git_odb_read_prefix(
@@ -1283,9 +1329,9 @@ static int git_odb_stream__invalid_length(
{
giterr_set(GITERR_ODB,
"cannot %s - "
- "Invalid length. %"PRIuZ" was expected. The "
- "total size of the received chunks amounts to %"PRIuZ".",
- action, stream->declared_size, stream->received_bytes);
+ "Invalid length. %"PRIdZ" was expected. The "
+ "total size of the received chunks amounts to %"PRIdZ".",
+ action, stream->declared_size, stream->received_bytes);
return -1;
}
@@ -1411,6 +1457,19 @@ int git_odb_refresh(struct git_odb *db)
return 0;
}
+int git_odb__error_mismatch(const git_oid *expected, const git_oid *actual)
+{
+ char expected_oid[GIT_OID_HEXSZ + 1], actual_oid[GIT_OID_HEXSZ + 1];
+
+ git_oid_tostr(expected_oid, sizeof(expected_oid), expected);
+ git_oid_tostr(actual_oid, sizeof(actual_oid), actual);
+
+ giterr_set(GITERR_ODB, "object hash mismatch - expected %s but got %s",
+ expected_oid, actual_oid);
+
+ return GIT_EMISMATCH;
+}
+
int git_odb__error_notfound(
const char *message, const git_oid *oid, size_t oid_len)
{
diff --git a/src/odb.h b/src/odb.h
index 4f548bbba..61d687abf 100644
--- a/src/odb.h
+++ b/src/odb.h
@@ -20,6 +20,8 @@
#define GIT_OBJECT_DIR_MODE 0777
#define GIT_OBJECT_FILE_MODE 0444
+extern bool git_odb__strict_hash_verification;
+
/* DO NOT EXPORT */
typedef struct {
void *data; /**< Raw, decompressed object data. */
@@ -96,6 +98,12 @@ int git_odb__hashfd_filtered(
*/
int git_odb__hashlink(git_oid *out, const char *path);
+/**
+ * Generate a GIT_EMISMATCH error for the ODB.
+ */
+int git_odb__error_mismatch(
+ const git_oid *expected, const git_oid *actual);
+
/*
* Generate a GIT_ENOTFOUND error for the ODB.
*/
diff --git a/src/odb_loose.c b/src/odb_loose.c
index e14af4fab..99fdcb44f 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -205,6 +205,11 @@ static int start_inflate(z_stream *s, git_buf *obj, void *out, size_t len)
return inflate(s, 0);
}
+static void abort_inflate(z_stream *s)
+{
+ inflateEnd(s);
+}
+
static int finish_inflate(z_stream *s)
{
int status = Z_OK;
@@ -367,6 +372,7 @@ static int inflate_disk_obj(git_rawobj *out, git_buf *obj)
(used = get_object_header(&hdr, head)) == 0 ||
!git_object_typeisloose(hdr.type))
{
+ abort_inflate(&zs);
giterr_set(GITERR_ODB, "failed to inflate disk object");
return -1;
}
@@ -844,7 +850,7 @@ static int filebuf_flags(loose_backend *backend)
int flags = GIT_FILEBUF_TEMPORARY |
(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT);
- if (backend->fsync_object_files || git_object__synchronous_writing)
+ if (backend->fsync_object_files || git_repository__fsync_gitdir)
flags |= GIT_FILEBUF_FSYNC;
return flags;
diff --git a/src/openssl_stream.c b/src/openssl_stream.c
index 841dcce50..759c5015f 100644
--- a/src/openssl_stream.c
+++ b/src/openssl_stream.c
@@ -103,7 +103,7 @@ int git_openssl_stream_global_init(void)
ssl_opts |= SSL_OP_NO_COMPRESSION;
#endif
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
#else
diff --git a/src/pack.c b/src/pack.c
index 60b757e90..f8d0dc9ac 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -312,7 +312,7 @@ static int pack_index_open(struct git_pack_file *p)
{
int error = 0;
size_t name_len;
- git_buf idx_name = GIT_BUF_INIT;
+ git_buf idx_name;
if (p->index_version > -1)
return 0;
@@ -320,11 +320,13 @@ static int pack_index_open(struct git_pack_file *p)
name_len = strlen(p->pack_name);
assert(name_len > strlen(".pack")); /* checked by git_pack_file alloc */
- git_buf_grow(&idx_name, name_len);
+ if (git_buf_init(&idx_name, name_len) < 0)
+ return -1;
+
git_buf_put(&idx_name, p->pack_name, name_len - strlen(".pack"));
git_buf_puts(&idx_name, ".idx");
if (git_buf_oom(&idx_name)) {
- giterr_set_oom();
+ git_buf_free(&idx_name);
return -1;
}
diff --git a/src/path.c b/src/path.c
index b7205a646..5fc7a055b 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1708,6 +1708,7 @@ GIT_INLINE(unsigned int) dotgit_flags(
unsigned int flags)
{
int protectHFS = 0, protectNTFS = 0;
+ int error = 0;
flags |= GIT_PATH_REJECT_DOT_GIT_LITERAL;
@@ -1720,13 +1721,13 @@ GIT_INLINE(unsigned int) dotgit_flags(
#endif
if (repo && !protectHFS)
- git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
- if (protectHFS)
+ error = git_repository__cvar(&protectHFS, repo, GIT_CVAR_PROTECTHFS);
+ if (!error && protectHFS)
flags |= GIT_PATH_REJECT_DOT_GIT_HFS;
if (repo && !protectNTFS)
- git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
- if (protectNTFS)
+ error = git_repository__cvar(&protectNTFS, repo, GIT_CVAR_PROTECTNTFS);
+ if (!error && protectNTFS)
flags |= GIT_PATH_REJECT_DOT_GIT_NTFS;
return flags;
diff --git a/src/posix.h b/src/posix.h
index bd5a98e26..d26371bca 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -24,6 +24,10 @@
#define _S_IFLNK S_IFLNK
#endif
+#ifndef S_IWUSR
+#define S_IWUSR 00200
+#endif
+
#ifndef S_IXUSR
#define S_IXUSR 00100
#endif
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index b325d2763..eb135dc01 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1140,7 +1140,7 @@ out:
static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref, const git_signature *who, const char *message)
{
int error;
- git_oid old_id = {{0}};
+ git_oid old_id;
git_reference *tmp = NULL, *head = NULL, *peeled = NULL;
const char *name;
@@ -1148,7 +1148,8 @@ static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref
return 0;
/* if we can't resolve, we use {0}*40 as old id */
- git_reference_name_to_id(&old_id, backend->repo, ref->name);
+ if (git_reference_name_to_id(&old_id, backend->repo, ref->name) < 0)
+ memset(&old_id, 0, sizeof(old_id));
if ((error = git_reference_lookup(&head, backend->repo, GIT_HEAD_FILE)) < 0)
return error;
@@ -2031,7 +2032,7 @@ int git_refdb_backend_fs(
backend->direach_flags |= GIT_PATH_DIR_PRECOMPOSE_UNICODE;
}
if ((!git_repository__cvar(&t, backend->repo, GIT_CVAR_FSYNCOBJECTFILES) && t) ||
- git_object__synchronous_writing)
+ git_repository__fsync_gitdir)
backend->fsync = 1;
backend->parent.exists = &refdb_fs_backend__exists;
diff --git a/src/refs.c b/src/refs.c
index 0837dc4a8..f7120d9ee 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -249,6 +249,40 @@ int git_reference_lookup_resolved(
return 0;
}
+int git_reference__read_head(
+ git_reference **out,
+ git_repository *repo,
+ const char *path)
+{
+ git_buf reference = GIT_BUF_INIT;
+ char *name = NULL;
+ int error;
+
+ if ((error = git_futils_readbuffer(&reference, path)) < 0)
+ goto out;
+ git_buf_rtrim(&reference);
+
+ if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) == 0) {
+ git_buf_consume(&reference, reference.ptr + strlen(GIT_SYMREF));
+
+ name = git_path_basename(path);
+
+ if ((*out = git_reference__alloc_symbolic(name, reference.ptr)) == NULL) {
+ error = -1;
+ goto out;
+ }
+ } else {
+ if ((error = git_reference_lookup(out, repo, reference.ptr)) < 0)
+ goto out;
+ }
+
+out:
+ git__free(name);
+ git_buf_free(&reference);
+
+ return error;
+}
+
int git_reference_dwim(git_reference **out, git_repository *repo, const char *refname)
{
int error = 0, i;
@@ -580,20 +614,63 @@ int git_reference_symbolic_set_target(
out, ref->db->repo, ref->name, target, 1, ref->target.symbolic, log_message);
}
+typedef struct {
+ const char *old_name;
+ git_refname_t new_name;
+} rename_cb_data;
+
+static int update_wt_heads(git_repository *repo, const char *path, void *payload)
+{
+ rename_cb_data *data = (rename_cb_data *) payload;
+ git_reference *head = NULL;
+ char *gitdir = NULL;
+ int error;
+
+ if ((error = git_reference__read_head(&head, repo, path)) < 0) {
+ giterr_set(GITERR_REFERENCE, "could not read HEAD when renaming references");
+ goto out;
+ }
+
+ if ((gitdir = git_path_dirname(path)) == NULL) {
+ error = -1;
+ goto out;
+ }
+
+ if (git_reference_type(head) != GIT_REF_SYMBOLIC ||
+ git__strcmp(head->target.symbolic, data->old_name) != 0) {
+ error = 0;
+ goto out;
+ }
+
+ /* Update HEAD it was pointing to the reference being renamed */
+ if ((error = git_repository_create_head(gitdir, data->new_name)) < 0) {
+ giterr_set(GITERR_REFERENCE, "failed to update HEAD after renaming reference");
+ goto out;
+ }
+
+out:
+ git_reference_free(head);
+ git__free(gitdir);
+
+ return error;
+}
+
static int reference__rename(git_reference **out, git_reference *ref, const char *new_name, int force,
const git_signature *signature, const char *message)
{
+ git_repository *repo;
git_refname_t normalized;
bool should_head_be_updated = false;
int error = 0;
assert(ref && new_name && signature);
+ repo = git_reference_owner(ref);
+
if ((error = reference_normalize_for_repo(
- normalized, git_reference_owner(ref), new_name, true)) < 0)
+ normalized, repo, new_name, true)) < 0)
return error;
-
/* Check if we have to update HEAD. */
if ((error = git_branch_is_head(ref)) < 0)
return error;
@@ -603,14 +680,18 @@ static int reference__rename(git_reference **out, git_reference *ref, const char
if ((error = git_refdb_rename(out, ref->db, ref->name, normalized, force, signature, message)) < 0)
return error;
- /* Update HEAD it was pointing to the reference being renamed */
- if (should_head_be_updated &&
- (error = git_repository_set_head(ref->db->repo, normalized)) < 0) {
- giterr_set(GITERR_REFERENCE, "failed to update HEAD after renaming reference");
- return error;
+ /* Update HEAD if it was pointing to the reference being renamed */
+ if (should_head_be_updated) {
+ error = git_repository_set_head(ref->db->repo, normalized);
+ } else {
+ rename_cb_data payload;
+ payload.old_name = ref->name;
+ memcpy(&payload.new_name, &normalized, sizeof(normalized));
+
+ error = git_repository_foreach_head(repo, update_wt_heads, &payload);
}
- return 0;
+ return error;
}
diff --git a/src/refs.h b/src/refs.h
index 80e655af7..0c90db3af 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -107,6 +107,20 @@ int git_reference_lookup_resolved(
const char *name,
int max_deref);
+/**
+ * Read reference from a file.
+ *
+ * This function will read in the file at `path`. If it is a
+ * symref, it will return a new unresolved symbolic reference
+ * with the given name pointing to the reference pointed to by
+ * the file. If it is not a symbolic reference, it will return
+ * the resolved reference.
+ */
+int git_reference__read_head(
+ git_reference **out,
+ git_repository *repo,
+ const char *path);
+
int git_reference__log_signature(git_signature **out, git_repository *repo);
/** Update a reference after a commit. */
diff --git a/src/remote.c b/src/remote.c
index d3132f75c..bd8b3cfbc 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -192,7 +192,7 @@ static int canonicalize_url(git_buf *out, const char *in)
static int create_internal(git_remote **out, git_repository *repo, const char *name, const char *url, const char *fetch)
{
git_remote *remote;
- git_config *config = NULL;
+ git_config *config_ro = NULL, *config_rw;
git_buf canonical_url = GIT_BUF_INIT;
git_buf var = GIT_BUF_INIT;
int error = -1;
@@ -200,7 +200,7 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
/* name is optional */
assert(out && repo && url);
- if ((error = git_repository_config__weakptr(&config, repo)) < 0)
+ if ((error = git_repository_config_snapshot(&config_ro, repo)) < 0)
return error;
remote = git__calloc(1, sizeof(git_remote));
@@ -212,7 +212,8 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
(error = canonicalize_url(&canonical_url, url)) < 0)
goto on_error;
- remote->url = apply_insteadof(repo->_config, canonical_url.ptr, GIT_DIRECTION_FETCH);
+ remote->url = apply_insteadof(config_ro, canonical_url.ptr, GIT_DIRECTION_FETCH);
+ GITERR_CHECK_ALLOC(remote->url);
if (name != NULL) {
remote->name = git__strdup(name);
@@ -221,7 +222,8 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
if ((error = git_buf_printf(&var, CONFIG_URL_FMT, name)) < 0)
goto on_error;
- if ((error = git_config_set_string(config, var.ptr, canonical_url.ptr)) < 0)
+ if ((error = git_repository_config__weakptr(&config_rw, repo)) < 0 ||
+ (error = git_config_set_string(config_rw, var.ptr, canonical_url.ptr)) < 0)
goto on_error;
}
@@ -233,10 +235,7 @@ static int create_internal(git_remote **out, git_repository *repo, const char *n
if (name && (error = write_add_refspec(repo, name, fetch, true)) < 0)
goto on_error;
- if ((error = git_repository_config_snapshot(&config, repo)) < 0)
- goto on_error;
-
- if ((error = lookup_remote_prune_config(remote, config, name)) < 0)
+ if ((error = lookup_remote_prune_config(remote, config_ro, name)) < 0)
goto on_error;
/* Move the data over to where the matching functions can find them */
@@ -260,7 +259,7 @@ on_error:
if (error)
git_remote_free(remote);
- git_config_free(config);
+ git_config_free(config_ro);
git_buf_free(&canonical_url);
git_buf_free(&var);
return error;
@@ -2412,7 +2411,7 @@ int git_remote_push(git_remote *remote, const git_strarray *refspecs, const git_
proxy = &opts->proxy_opts;
}
- assert(remote && refspecs);
+ assert(remote);
if ((error = git_remote_connect(remote, GIT_DIRECTION_PUSH, cbs, proxy, custom_headers)) < 0)
return error;
diff --git a/src/repository.c b/src/repository.c
index 425ef796f..71f77bab9 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -36,6 +36,8 @@
# include "win32/w32_util.h"
#endif
+bool git_repository__fsync_gitdir = false;
+
static const struct {
git_repository_item_t parent;
const char *name;
@@ -422,10 +424,10 @@ static int read_gitfile(git_buf *path_out, const char *file_path)
}
static int find_repo(
- git_buf *repo_path,
- git_buf *parent_path,
- git_buf *link_path,
- git_buf *common_path,
+ git_buf *gitdir_path,
+ git_buf *workdir_path,
+ git_buf *gitlink_path,
+ git_buf *commondir_path,
const char *start_path,
uint32_t flags,
const char *ceiling_dirs)
@@ -440,7 +442,7 @@ static int find_repo(
bool in_dot_git;
size_t ceiling_offset = 0;
- git_buf_free(repo_path);
+ git_buf_clear(gitdir_path);
error = git_path_prettify(&path, start_path, NULL);
if (error < 0)
@@ -482,13 +484,13 @@ static int find_repo(
if (S_ISDIR(st.st_mode)) {
if (valid_repository_path(&path, &common_link)) {
git_path_to_dir(&path);
- git_buf_set(repo_path, path.ptr, path.size);
+ git_buf_set(gitdir_path, path.ptr, path.size);
- if (link_path)
- git_buf_attach(link_path,
+ if (gitlink_path)
+ git_buf_attach(gitlink_path,
git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0);
- if (common_path)
- git_buf_swap(&common_link, common_path);
+ if (commondir_path)
+ git_buf_swap(&common_link, commondir_path);
break;
}
@@ -498,12 +500,12 @@ static int find_repo(
if (error < 0)
break;
if (valid_repository_path(&repo_link, &common_link)) {
- git_buf_swap(repo_path, &repo_link);
+ git_buf_swap(gitdir_path, &repo_link);
- if (link_path)
- error = git_buf_put(link_path, path.ptr, path.size);
- if (common_path)
- git_buf_swap(&common_link, common_path);
+ if (gitlink_path)
+ error = git_buf_put(gitlink_path, path.ptr, path.size);
+ if (commondir_path)
+ git_buf_swap(&common_link, commondir_path);
}
break;
}
@@ -529,20 +531,20 @@ static int find_repo(
break;
}
- if (!error && parent_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) {
- if (!git_buf_len(repo_path))
- git_buf_clear(parent_path);
+ if (!error && workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) {
+ if (!git_buf_len(gitdir_path))
+ git_buf_clear(workdir_path);
else {
- git_path_dirname_r(parent_path, path.ptr);
- git_path_to_dir(parent_path);
+ git_path_dirname_r(workdir_path, path.ptr);
+ git_path_to_dir(workdir_path);
}
- if (git_buf_oom(parent_path))
+ if (git_buf_oom(workdir_path))
return -1;
}
/* If we didn't find the repository, and we don't have any other error
* to report, report that. */
- if (!git_buf_len(repo_path) && !error) {
+ if (!git_buf_len(gitdir_path) && !error) {
giterr_set(GITERR_REPOSITORY,
"could not find repository from '%s'", start_path);
error = GIT_ENOTFOUND;
@@ -758,6 +760,29 @@ success:
return error;
}
+static int repo_is_worktree(unsigned *out, const git_repository *repo)
+{
+ git_buf gitdir_link = GIT_BUF_INIT;
+ int error;
+
+ /* Worktrees cannot have the same commondir and gitdir */
+ if (repo->commondir && repo->gitdir
+ && !strcmp(repo->commondir, repo->gitdir)) {
+ *out = 0;
+ return 0;
+ }
+
+ if ((error = git_buf_joinpath(&gitdir_link, repo->gitdir, "gitdir")) < 0)
+ return -1;
+
+ /* A 'gitdir' file inside a git directory is currently
+ * only used when the repository is a working tree. */
+ *out = !!git_path_exists(gitdir_link.ptr);
+
+ git_buf_free(&gitdir_link);
+ return error;
+}
+
int git_repository_open_ext(
git_repository **repo_ptr,
const char *start_path,
@@ -765,8 +790,9 @@ int git_repository_open_ext(
const char *ceiling_dirs)
{
int error;
- git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT,
- link_path = GIT_BUF_INIT, common_path = GIT_BUF_INIT;
+ unsigned is_worktree;
+ git_buf gitdir = GIT_BUF_INIT, workdir = GIT_BUF_INIT,
+ gitlink = GIT_BUF_INIT, commondir = GIT_BUF_INIT;
git_repository *repo;
git_config *config = NULL;
@@ -777,7 +803,7 @@ int git_repository_open_ext(
*repo_ptr = NULL;
error = find_repo(
- &path, &parent, &link_path, &common_path, start_path, flags, ceiling_dirs);
+ &gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs);
if (error < 0 || !repo_ptr)
return error;
@@ -785,24 +811,21 @@ int git_repository_open_ext(
repo = repository_alloc();
GITERR_CHECK_ALLOC(repo);
- repo->gitdir = git_buf_detach(&path);
+ repo->gitdir = git_buf_detach(&gitdir);
GITERR_CHECK_ALLOC(repo->gitdir);
- if (link_path.size) {
- repo->gitlink = git_buf_detach(&link_path);
+ if (gitlink.size) {
+ repo->gitlink = git_buf_detach(&gitlink);
GITERR_CHECK_ALLOC(repo->gitlink);
}
- if (common_path.size) {
- repo->commondir = git_buf_detach(&common_path);
+ if (commondir.size) {
+ repo->commondir = git_buf_detach(&commondir);
GITERR_CHECK_ALLOC(repo->commondir);
}
- if ((error = git_buf_joinpath(&path, repo->gitdir, "gitdir")) < 0)
+ if ((error = repo_is_worktree(&is_worktree, repo)) < 0)
goto cleanup;
- /* A 'gitdir' file inside a git directory is currently
- * only used when the repository is a working tree. */
- if (git_path_exists(path.ptr))
- repo->is_worktree = 1;
+ repo->is_worktree = is_worktree;
/*
* We'd like to have the config, but git doesn't particularly
@@ -822,13 +845,13 @@ int git_repository_open_ext(
if (config &&
((error = load_config_data(repo, config)) < 0 ||
- (error = load_workdir(repo, config, &parent)) < 0))
+ (error = load_workdir(repo, config, &workdir)) < 0))
goto cleanup;
}
cleanup:
- git_buf_free(&path);
- git_buf_free(&parent);
+ git_buf_free(&gitdir);
+ git_buf_free(&workdir);
git_config_free(config);
if (error < 0)
@@ -920,13 +943,10 @@ static int load_config(
if ((error = git_config_new(&cfg)) < 0)
return error;
- error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG);
- if (error < 0)
- goto on_error;
+ if ((error = git_repository_item_path(&config_path, repo, GIT_REPOSITORY_ITEM_CONFIG)) == 0)
+ error = git_config_add_file_ondisk(cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0);
- if ((error = git_config_add_file_ondisk(
- cfg, config_path.ptr, GIT_CONFIG_LEVEL_LOCAL, 0)) < 0 &&
- error != GIT_ENOTFOUND)
+ if (error && error != GIT_ENOTFOUND)
goto on_error;
git_buf_free(&config_path);
@@ -1215,7 +1235,7 @@ static int reserved_names_add8dot3(git_repository *repo, const char *path)
name_len = strlen(name);
- if ((name_len == def_len && memcmp(name, def, def_len) == 0) ||
+ if ((name_len == def_len && memcmp(name, def, def_len) == 0) ||
(name_len == def_dot_git_len && memcmp(name, def_dot_git, def_dot_git_len) == 0)) {
git__free(name);
return 0;
@@ -1764,7 +1784,13 @@ static int repo_init_structure(
default_template = true;
}
- if (tdir) {
+ /*
+ * If tdir was the empty string, treat it like tdir was a path to an
+ * empty directory (so, don't do any copying). This is the behavior
+ * that git(1) exhibits, although it doesn't seem to be officially
+ * documented.
+ */
+ if (tdir && git__strcmp(tdir, "") != 0) {
uint32_t cpflags = GIT_CPDIR_COPY_SYMLINKS |
GIT_CPDIR_SIMPLE_TO_MODE |
GIT_CPDIR_COPY_DOTFILES;
@@ -2063,47 +2089,27 @@ int git_repository_head_detached(git_repository *repo)
return exists;
}
-static int read_worktree_head(git_buf *out, git_repository *repo, const char *name)
+static int get_worktree_file_path(git_buf *out, git_repository *repo, const char *worktree, const char *file)
{
- git_buf path = GIT_BUF_INIT;
- int err;
-
- assert(out && repo && name);
-
git_buf_clear(out);
-
- if ((err = git_buf_printf(&path, "%s/worktrees/%s/HEAD", repo->commondir, name)) < 0)
- goto out;
- if (!git_path_exists(path.ptr))
- {
- err = -1;
- goto out;
- }
-
- if ((err = git_futils_readbuffer(out, path.ptr)) < 0)
- goto out;
- git_buf_rtrim(out);
-
-out:
- git_buf_free(&path);
-
- return err;
+ return git_buf_printf(out, "%s/worktrees/%s/%s", repo->commondir, worktree, file);
}
int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
{
- git_buf buf = GIT_BUF_INIT;
- int ret;
+ git_reference *ref = NULL;
+ int error;
assert(repo && name);
- if (read_worktree_head(&buf, repo, name) < 0)
- return -1;
+ if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0)
+ goto out;
- ret = git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) != 0;
- git_buf_free(&buf);
+ error = (git_reference_type(ref) != GIT_REF_SYMBOLIC);
+out:
+ git_reference_free(ref);
- return ret;
+ return error;
}
int git_repository_head(git_reference **head_out, git_repository *repo)
@@ -2127,44 +2133,67 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
{
- git_buf buf = GIT_BUF_INIT;
- git_reference *head;
- int err;
+ git_buf path = GIT_BUF_INIT;
+ git_reference *head = NULL;
+ int error;
assert(out && repo && name);
*out = NULL;
- if (git_repository_head_detached_for_worktree(repo, name))
- return -1;
- if ((err = read_worktree_head(&buf, repo, name)) < 0)
+ if ((error = get_worktree_file_path(&path, repo, name, GIT_HEAD_FILE)) < 0 ||
+ (error = git_reference__read_head(&head, repo, path.ptr)) < 0)
goto out;
- /* We can only resolve symbolic references */
- if (git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)))
- {
- err = -1;
- goto out;
+ if (git_reference_type(head) != GIT_REF_OID) {
+ git_reference *resolved;
+
+ error = git_reference_lookup_resolved(&resolved, repo, git_reference_symbolic_target(head), -1);
+ git_reference_free(head);
+ head = resolved;
}
- git_buf_consume(&buf, buf.ptr + strlen(GIT_SYMREF));
- if ((err = git_reference_lookup(&head, repo, buf.ptr)) < 0)
+ *out = head;
+
+out:
+ if (error)
+ git_reference_free(head);
+
+ git_buf_free(&path);
+
+ return error;
+}
+
+int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
+{
+ git_strarray worktrees = GIT_VECTOR_INIT;
+ git_buf path = GIT_BUF_INIT;
+ int error;
+ size_t i;
+
+ /* Execute callback for HEAD of commondir */
+ if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
+ (error = cb(repo, path.ptr, payload) != 0))
goto out;
- if (git_reference_type(head) == GIT_REF_OID)
- {
- *out = head;
- err = 0;
+
+ if ((error = git_worktree_list(&worktrees, repo)) < 0) {
+ error = 0;
goto out;
}
- err = git_reference_lookup_resolved(
- out, repo, git_reference_symbolic_target(head), -1);
- git_reference_free(head);
+ /* Execute callback for all worktree HEADs */
+ for (i = 0; i < worktrees.count; i++) {
+ if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
+ continue;
-out:
- git_buf_free(&buf);
+ if ((error = cb(repo, path.ptr, payload)) != 0)
+ goto out;
+ }
- return err;
+out:
+ git_buf_free(&path);
+ git_strarray_free(&worktrees);
+ return error;
}
int git_repository_head_unborn(git_repository *repo)
@@ -2240,13 +2269,13 @@ int git_repository_item_path(git_buf *out, git_repository *repo, git_repository_
parent = git_repository_commondir(repo);
break;
default:
- giterr_set(GITERR_INVALID, "Invalid item directory");
+ giterr_set(GITERR_INVALID, "invalid item directory");
return -1;
}
if (parent == NULL) {
- giterr_set(GITERR_INVALID, "Path cannot exist in repository");
- return -1;
+ giterr_set(GITERR_INVALID, "path cannot exist in repository");
+ return GIT_ENOTFOUND;
}
if (git_buf_sets(out, parent) < 0)
@@ -2529,7 +2558,9 @@ static int checkout_message(git_buf *out, git_reference *old, const char *new)
git_buf_puts(out, " to ");
- if (git_reference__is_branch(new))
+ if (git_reference__is_branch(new) ||
+ git_reference__is_tag(new) ||
+ git_reference__is_remote(new))
git_buf_puts(out, git_reference__shorthand(new));
else
git_buf_puts(out, new);
@@ -2540,6 +2571,41 @@ static int checkout_message(git_buf *out, git_reference *old, const char *new)
return 0;
}
+static int detach(git_repository *repo, const git_oid *id, const char *new)
+{
+ int error;
+ git_buf log_message = GIT_BUF_INIT;
+ git_object *object = NULL, *peeled = NULL;
+ git_reference *new_head = NULL, *current = NULL;
+
+ assert(repo && id);
+
+ if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
+ return error;
+
+ if ((error = git_object_lookup(&object, repo, id, GIT_OBJ_ANY)) < 0)
+ goto cleanup;
+
+ if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0)
+ goto cleanup;
+
+ if (new == NULL)
+ new = git_oid_tostr_s(git_object_id(peeled));
+
+ if ((error = checkout_message(&log_message, current, new)) < 0)
+ goto cleanup;
+
+ error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message));
+
+cleanup:
+ git_buf_free(&log_message);
+ git_object_free(object);
+ git_object_free(peeled);
+ git_reference_free(current);
+ git_reference_free(new_head);
+ return error;
+}
+
int git_repository_set_head(
git_repository* repo,
const char* refname)
@@ -2562,6 +2628,8 @@ int git_repository_set_head(
if (ref && current->type == GIT_REF_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) &&
git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) {
+ giterr_set(GITERR_REPOSITORY, "cannot set HEAD to reference '%s' as it is the current HEAD "
+ "of a linked repository.", git_reference_name(ref));
error = -1;
goto cleanup;
}
@@ -2571,7 +2639,8 @@ int git_repository_set_head(
error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
git_reference_name(ref), true, git_buf_cstr(&log_message));
} else {
- error = git_repository_set_head_detached(repo, git_reference_target(ref));
+ error = detach(repo, git_reference_target(ref),
+ git_reference_is_tag(ref) || git_reference_is_remote(ref) ? refname : NULL);
}
} else if (git_reference__is_branch(refname)) {
error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE, refname,
@@ -2586,41 +2655,6 @@ cleanup:
return error;
}
-static int detach(git_repository *repo, const git_oid *id, const char *from)
-{
- int error;
- git_buf log_message = GIT_BUF_INIT;
- git_object *object = NULL, *peeled = NULL;
- git_reference *new_head = NULL, *current = NULL;
-
- assert(repo && id);
-
- if ((error = git_reference_lookup(&current, repo, GIT_HEAD_FILE)) < 0)
- return error;
-
- if ((error = git_object_lookup(&object, repo, id, GIT_OBJ_ANY)) < 0)
- goto cleanup;
-
- if ((error = git_object_peel(&peeled, object, GIT_OBJ_COMMIT)) < 0)
- goto cleanup;
-
- if (from == NULL)
- from = git_oid_tostr_s(git_object_id(peeled));
-
- if ((error = checkout_message(&log_message, current, from)) < 0)
- goto cleanup;
-
- error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_object_id(peeled), true, git_buf_cstr(&log_message));
-
-cleanup:
- git_buf_free(&log_message);
- git_object_free(object);
- git_object_free(peeled);
- git_reference_free(current);
- git_reference_free(new_head);
- return error;
-}
-
int git_repository_set_head_detached(
git_repository* repo,
const git_oid* commitish)
@@ -2734,7 +2768,7 @@ int git_repository__cleanup_files(
error = git_futils_rmdir_r(path, NULL,
GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
}
-
+
git_buf_clear(&buf);
}
diff --git a/src/repository.h b/src/repository.h
index 33adfa60a..52f9ec260 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -31,6 +31,8 @@
/* Default DOS-compatible 8.3 "short name" for a git repository, "GIT~1" */
#define GIT_DIR_SHORTNAME "GIT~1"
+extern bool git_repository__fsync_gitdir;
+
/** Cvar cache identifiers */
typedef enum {
GIT_CVAR_AUTO_CRLF = 0, /* core.autocrlf */
@@ -160,6 +162,26 @@ int git_repository_head_tree(git_tree **tree, git_repository *repo);
int git_repository_create_head(const char *git_dir, const char *ref_name);
/*
+ * Called for each HEAD.
+ *
+ * Can return either 0, causing the iteration over HEADs to
+ * continue, or a non-0 value causing the iteration to abort. The
+ * return value is passed back to the caller of
+ * `git_repository_foreach_head`
+ */
+typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *path, void *payload);
+
+/*
+ * Iterate over repository and all worktree HEADs.
+ *
+ * This function will be called for the repository HEAD and for
+ * all HEADS of linked worktrees. For each HEAD, the callback is
+ * executed with the given payload. The return value equals the
+ * return value of the last executed callback function.
+ */
+int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload);
+
+/*
* Weak pointers to repository internals.
*
* The returned pointers do not need to be freed. Do not keep
diff --git a/src/revparse.c b/src/revparse.c
index d5511b47b..fd6bd1ea6 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -892,6 +892,17 @@ int git_revparse(
const char *rstr;
revspec->flags = GIT_REVPARSE_RANGE;
+ /*
+ * Following git.git, don't allow '..' because it makes command line
+ * arguments which can be either paths or revisions ambiguous when the
+ * path is almost certainly intended. The empty range '...' is still
+ * allowed.
+ */
+ if (!git__strcmp(spec, "..")) {
+ giterr_set(GITERR_INVALID, "Invalid pattern '..'");
+ return GIT_EINVALIDSPEC;
+ }
+
lstr = git__substrdup(spec, dotdot - spec);
rstr = dotdot + 2;
if (dotdot[2] == '.') {
@@ -899,9 +910,17 @@ int git_revparse(
rstr++;
}
- error = git_revparse_single(&revspec->from, repo, lstr);
- if (!error)
- error = git_revparse_single(&revspec->to, repo, rstr);
+ error = git_revparse_single(
+ &revspec->from,
+ repo,
+ *lstr == '\0' ? "HEAD" : lstr);
+
+ if (!error) {
+ error = git_revparse_single(
+ &revspec->to,
+ repo,
+ *rstr == '\0' ? "HEAD" : rstr);
+ }
git__free((void*)lstr);
} else {
diff --git a/src/settings.c b/src/settings.c
index 24e549ec1..52b861ba0 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -15,6 +15,7 @@
#include "cache.h"
#include "global.h"
#include "object.h"
+#include "odb.h"
#include "refs.h"
#include "transports/smart.h"
@@ -31,7 +32,7 @@ int git_libgit2_features(void)
#ifdef GIT_THREADS
| GIT_FEATURE_THREADS
#endif
-#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT)
+#ifdef GIT_HTTPS
| GIT_FEATURE_HTTPS
#endif
#if defined(GIT_SSH)
@@ -227,8 +228,24 @@ int git_libgit2_opts(int key, ...)
git_smart__ofs_delta_enabled = (va_arg(ap, int) != 0);
break;
- case GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION:
- git_object__synchronous_writing = (va_arg(ap, int) != 0);
+ case GIT_OPT_ENABLE_FSYNC_GITDIR:
+ git_repository__fsync_gitdir = (va_arg(ap, int) != 0);
+ break;
+
+ case GIT_OPT_GET_WINDOWS_SHAREMODE:
+#ifdef GIT_WIN32
+ *(va_arg(ap, unsigned long *)) = git_win32__createfile_sharemode;
+#endif
+ break;
+
+ case GIT_OPT_SET_WINDOWS_SHAREMODE:
+#ifdef GIT_WIN32
+ git_win32__createfile_sharemode = va_arg(ap, unsigned long);
+#endif
+ break;
+
+ case GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION:
+ git_odb__strict_hash_verification = (va_arg(ap, int) != 0);
break;
default:
diff --git a/src/signature.c b/src/signature.c
index e792a52f8..a56b8a299 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -228,8 +228,11 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
const char *time_start = email_end + 2;
const char *time_end;
- if (git__strtol64(&sig->when.time, time_start, &time_end, 10) < 0)
+ if (git__strtol64(&sig->when.time, time_start, &time_end, 10) < 0) {
+ git__free(sig->name);
+ git__free(sig->email);
return signature_error("invalid Unix timestamp");
+ }
/* do we have a timezone? */
if (time_end + 1 < buffer_end) {
diff --git a/src/socket_stream.c b/src/socket_stream.c
index fca411717..c0a168448 100644
--- a/src/socket_stream.c
+++ b/src/socket_stream.c
@@ -106,10 +106,8 @@ int socket_connect(git_stream *stream)
for (p = info; p != NULL; p = p->ai_next) {
s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
- if (s == INVALID_SOCKET) {
- net_set_error("error creating socket");
- break;
- }
+ if (s == INVALID_SOCKET)
+ continue;
if (connect(s, p->ai_addr, (socklen_t)p->ai_addrlen) == 0)
break;
diff --git a/src/sysdir.c b/src/sysdir.c
index ed11221a3..9312a7edb 100644
--- a/src/sysdir.c
+++ b/src/sysdir.c
@@ -275,3 +275,14 @@ int git_sysdir_find_template_dir(git_buf *path)
path, NULL, GIT_SYSDIR_TEMPLATE, "template");
}
+int git_sysdir_expand_global_file(git_buf *path, const char *filename)
+{
+ int error;
+
+ if ((error = git_sysdir_find_global_file(path, NULL)) == 0) {
+ if (filename)
+ error = git_buf_joinpath(path, path->ptr, filename);
+ }
+
+ return error;
+}
diff --git a/src/sysdir.h b/src/sysdir.h
index 11878981c..79f23818a 100644
--- a/src/sysdir.h
+++ b/src/sysdir.h
@@ -55,6 +55,18 @@ extern int git_sysdir_find_programdata_file(git_buf *path, const char *filename)
*/
extern int git_sysdir_find_template_dir(git_buf *path);
+/**
+ * Expand the name of a "global" file (i.e. one in a user's home
+ * directory). Unlike `find_global_file` (above), this makes no
+ * attempt to check for the existence of the file, and is useful if
+ * you want the full path regardless of existence.
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file in the home directory
+ * @return 0 on success or -1 on error
+ */
+extern int git_sysdir_expand_global_file(git_buf *path, const char *filename);
+
typedef enum {
GIT_SYSDIR_SYSTEM = 0,
GIT_SYSDIR_GLOBAL = 1,
diff --git a/src/transports/http.c b/src/transports/http.c
index 949e857b0..cb4a6d0d5 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -575,6 +575,9 @@ static int apply_proxy_config(http_subtransport *t)
if ((error = git_remote__get_http_proxy(t->owner->owner, !!t->connection_data.use_ssl, &url)) < 0)
return error;
+ opts.credentials = t->owner->proxy.credentials;
+ opts.certificate_check = t->owner->proxy.certificate_check;
+ opts.payload = t->owner->proxy.payload;
opts.type = GIT_PROXY_SPECIFIED;
opts.url = url;
error = git_stream_set_proxy(t->io, &opts);
diff --git a/src/transports/smart.c b/src/transports/smart.c
index e4aa26d86..a96fdf6fb 100644
--- a/src/transports/smart.c
+++ b/src/transports/smart.c
@@ -472,6 +472,12 @@ int git_transport_smart_credentials(git_cred **out, git_transport *transport, co
return t->cred_acquire_cb(out, t->url, user, methods, t->cred_acquire_payload);
}
+int git_transport_smart_proxy_options(git_proxy_options *out, git_transport *transport)
+{
+ transport_smart *t = (transport_smart *) transport;
+ return git_proxy_options_dup(out, &t->proxy);
+}
+
int git_transport_smart(git_transport **out, git_remote *owner, void *param)
{
transport_smart *t;
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index 25e78c65a..8146fa163 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -325,7 +325,8 @@ static int wait_while_ack(gitno_buffer *buf)
if (pkt->type == GIT_PKT_ACK &&
(pkt->status != GIT_ACK_CONTINUE &&
- pkt->status != GIT_ACK_COMMON)) {
+ pkt->status != GIT_ACK_COMMON &&
+ pkt->status != GIT_ACK_READY)) {
git__free(pkt);
return 0;
}
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 44d02e522..4c55e3f2a 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -9,6 +9,7 @@
#include <libssh2.h>
#endif
+#include "global.h"
#include "git2.h"
#include "buffer.h"
#include "netops.h"
@@ -893,11 +894,22 @@ int git_transport_ssh_with_paths(git_transport **out, git_remote *owner, void *p
#endif
}
+#ifdef GIT_SSH
+static void shutdown_ssh(void)
+{
+ libssh2_exit();
+}
+#endif
+
int git_transport_ssh_global_init(void)
{
#ifdef GIT_SSH
+ if (libssh2_init(0) < 0) {
+ giterr_set(GITERR_SSH, "unable to initialize libssh2");
+ return -1;
+ }
- libssh2_init(0);
+ git__on_shutdown(shutdown_ssh);
return 0;
#else
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index e8e848026..fb504c912 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -429,7 +429,6 @@ static int winhttp_stream_connect(winhttp_stream *s)
git_buf_printf(&processed_url, ":%s", t->proxy_connection_data.port);
if (git_buf_oom(&processed_url)) {
- giterr_set_oom();
error = -1;
goto on_error;
}
diff --git a/src/varint.c b/src/varint.c
index 2f868607c..beac8c709 100644
--- a/src/varint.c
+++ b/src/varint.c
@@ -36,7 +36,7 @@ int git_encode_varint(unsigned char *buf, size_t bufsize, uintmax_t value)
while (value >>= 7)
varint[--pos] = 128 | (--value & 127);
if (buf) {
- if (bufsize < pos)
+ if (bufsize < (sizeof(varint) - pos))
return -1;
memcpy(buf, varint + pos, sizeof(varint) - pos);
}
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 73705fb2b..64769ecd3 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -14,6 +14,9 @@
#include "utf-conv.h"
#include "dir.h"
+extern unsigned long git_win32__createfile_sharemode;
+extern int git_win32__retries;
+
typedef SOCKET GIT_SOCKET;
#define p_lseek(f,n,w) _lseeki64(f, n, w)
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 5172627b0..e4fe4142c 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -26,15 +26,6 @@
#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
#endif
-/* Options which we always provide to _wopen.
- *
- * _O_BINARY - Raw access; no translation of CR or LF characters
- * _O_NOINHERIT - Do not mark the created handle as inheritable by child processes.
- * The Windows default is 'not inheritable', but the CRT's default (following
- * POSIX convention) is 'inheritable'. We have no desire for our handles to be
- * inheritable on Windows, so specify the flag to get default behavior back. */
-#define STANDARD_OPEN_FLAGS (_O_BINARY | _O_NOINHERIT)
-
/* Allowable mode bits on Win32. Using mode bits that are not supported on
* Win32 (eg S_IRWXU) is generally ignored, but Wine warns loudly about it
* so we simply remove them.
@@ -44,6 +35,164 @@
/* GetFinalPathNameByHandleW signature */
typedef DWORD(WINAPI *PFGetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD, DWORD);
+unsigned long git_win32__createfile_sharemode =
+ FILE_SHARE_READ | FILE_SHARE_WRITE;
+int git_win32__retries = 10;
+
+GIT_INLINE(void) set_errno(void)
+{
+ switch (GetLastError()) {
+ case ERROR_FILE_NOT_FOUND:
+ case ERROR_PATH_NOT_FOUND:
+ case ERROR_INVALID_DRIVE:
+ case ERROR_NO_MORE_FILES:
+ case ERROR_BAD_NETPATH:
+ case ERROR_BAD_NET_NAME:
+ case ERROR_BAD_PATHNAME:
+ case ERROR_FILENAME_EXCED_RANGE:
+ errno = ENOENT;
+ break;
+ case ERROR_BAD_ENVIRONMENT:
+ errno = E2BIG;
+ break;
+ case ERROR_BAD_FORMAT:
+ case ERROR_INVALID_STARTING_CODESEG:
+ case ERROR_INVALID_STACKSEG:
+ case ERROR_INVALID_MODULETYPE:
+ case ERROR_INVALID_EXE_SIGNATURE:
+ case ERROR_EXE_MARKED_INVALID:
+ case ERROR_BAD_EXE_FORMAT:
+ case ERROR_ITERATED_DATA_EXCEEDS_64k:
+ case ERROR_INVALID_MINALLOCSIZE:
+ case ERROR_DYNLINK_FROM_INVALID_RING:
+ case ERROR_IOPL_NOT_ENABLED:
+ case ERROR_INVALID_SEGDPL:
+ case ERROR_AUTODATASEG_EXCEEDS_64k:
+ case ERROR_RING2SEG_MUST_BE_MOVABLE:
+ case ERROR_RELOC_CHAIN_XEEDS_SEGLIM:
+ case ERROR_INFLOOP_IN_RELOC_CHAIN:
+ errno = ENOEXEC;
+ break;
+ case ERROR_INVALID_HANDLE:
+ case ERROR_INVALID_TARGET_HANDLE:
+ case ERROR_DIRECT_ACCESS_HANDLE:
+ errno = EBADF;
+ break;
+ case ERROR_WAIT_NO_CHILDREN:
+ case ERROR_CHILD_NOT_COMPLETE:
+ errno = ECHILD;
+ break;
+ case ERROR_NO_PROC_SLOTS:
+ case ERROR_MAX_THRDS_REACHED:
+ case ERROR_NESTING_NOT_ALLOWED:
+ errno = EAGAIN;
+ break;
+ case ERROR_ARENA_TRASHED:
+ case ERROR_NOT_ENOUGH_MEMORY:
+ case ERROR_INVALID_BLOCK:
+ case ERROR_NOT_ENOUGH_QUOTA:
+ errno = ENOMEM;
+ break;
+ case ERROR_ACCESS_DENIED:
+ case ERROR_CURRENT_DIRECTORY:
+ case ERROR_WRITE_PROTECT:
+ case ERROR_BAD_UNIT:
+ case ERROR_NOT_READY:
+ case ERROR_BAD_COMMAND:
+ case ERROR_CRC:
+ case ERROR_BAD_LENGTH:
+ case ERROR_SEEK:
+ case ERROR_NOT_DOS_DISK:
+ case ERROR_SECTOR_NOT_FOUND:
+ case ERROR_OUT_OF_PAPER:
+ case ERROR_WRITE_FAULT:
+ case ERROR_READ_FAULT:
+ case ERROR_GEN_FAILURE:
+ case ERROR_SHARING_VIOLATION:
+ case ERROR_LOCK_VIOLATION:
+ case ERROR_WRONG_DISK:
+ case ERROR_SHARING_BUFFER_EXCEEDED:
+ case ERROR_NETWORK_ACCESS_DENIED:
+ case ERROR_CANNOT_MAKE:
+ case ERROR_FAIL_I24:
+ case ERROR_DRIVE_LOCKED:
+ case ERROR_SEEK_ON_DEVICE:
+ case ERROR_NOT_LOCKED:
+ case ERROR_LOCK_FAILED:
+ errno = EACCES;
+ break;
+ case ERROR_FILE_EXISTS:
+ case ERROR_ALREADY_EXISTS:
+ errno = EEXIST;
+ break;
+ case ERROR_NOT_SAME_DEVICE:
+ errno = EXDEV;
+ break;
+ case ERROR_INVALID_FUNCTION:
+ case ERROR_INVALID_ACCESS:
+ case ERROR_INVALID_DATA:
+ case ERROR_INVALID_PARAMETER:
+ case ERROR_NEGATIVE_SEEK:
+ errno = EINVAL;
+ break;
+ case ERROR_TOO_MANY_OPEN_FILES:
+ errno = EMFILE;
+ break;
+ case ERROR_DISK_FULL:
+ errno = ENOSPC;
+ break;
+ case ERROR_BROKEN_PIPE:
+ errno = EPIPE;
+ break;
+ case ERROR_DIR_NOT_EMPTY:
+ errno = ENOTEMPTY;
+ break;
+ default:
+ errno = EINVAL;
+ }
+}
+
+GIT_INLINE(bool) last_error_retryable(void)
+{
+ int os_error = GetLastError();
+
+ return (os_error == ERROR_SHARING_VIOLATION ||
+ os_error == ERROR_ACCESS_DENIED);
+}
+
+#define do_with_retries(fn, remediation) \
+ do { \
+ int __tries, __ret; \
+ for (__tries = 0; __tries < git_win32__retries; __tries++) { \
+ if (__tries && (__ret = (remediation)) != 0) \
+ return __ret; \
+ if ((__ret = (fn)) != GIT_RETRY) \
+ return __ret; \
+ Sleep(5); \
+ } \
+ return -1; \
+ } while (0) \
+
+static int ensure_writable(wchar_t *path)
+{
+ DWORD attrs;
+
+ if ((attrs = GetFileAttributesW(path)) == INVALID_FILE_ATTRIBUTES)
+ goto on_error;
+
+ if ((attrs & FILE_ATTRIBUTE_READONLY) == 0)
+ return 0;
+
+ if (!SetFileAttributesW(path, (attrs & ~FILE_ATTRIBUTE_READONLY)))
+ goto on_error;
+
+ return 0;
+
+on_error:
+ set_errno();
+ return -1;
+}
+
/**
* Truncate or extend file.
*
@@ -89,24 +238,26 @@ int p_link(const char *old, const char *new)
return -1;
}
-int p_unlink(const char *path)
+GIT_INLINE(int) unlink_once(const wchar_t *path)
{
- git_win32_path buf;
- int error;
+ if (DeleteFileW(path))
+ return 0;
- if (git_win32_path_from_utf8(buf, path) < 0)
- return -1;
+ if (last_error_retryable())
+ return GIT_RETRY;
- error = _wunlink(buf);
+ set_errno();
+ return -1;
+}
- /* If the file could not be deleted because it was
- * read-only, clear the bit and try again */
- if (error == -1 && errno == EACCES) {
- _wchmod(buf, 0666);
- error = _wunlink(buf);
- }
+int p_unlink(const char *path)
+{
+ git_win32_path wpath;
- return error;
+ if (git_win32_path_from_utf8(wpath, path) < 0)
+ return -1;
+
+ do_with_retries(unlink_once(wpath), ensure_writable(wpath));
}
int p_fsync(int fd)
@@ -212,44 +363,6 @@ int p_lstat_posixly(const char *filename, struct stat *buf)
return do_lstat(filename, buf, true);
}
-int p_utimes(const char *filename, const struct p_timeval times[2])
-{
- int fd, error;
-
- if ((fd = p_open(filename, O_RDWR)) < 0)
- return fd;
-
- error = p_futimes(fd, times);
-
- close(fd);
- return error;
-}
-
-int p_futimes(int fd, const struct p_timeval times[2])
-{
- HANDLE handle;
- FILETIME atime = {0}, mtime = {0};
-
- if (times == NULL) {
- SYSTEMTIME st;
-
- GetSystemTime(&st);
- SystemTimeToFileTime(&st, &atime);
- SystemTimeToFileTime(&st, &mtime);
- } else {
- git_win32__timeval_to_filetime(&atime, times[0]);
- git_win32__timeval_to_filetime(&mtime, times[1]);
- }
-
- if ((handle = (HANDLE)_get_osfhandle(fd)) == INVALID_HANDLE_VALUE)
- return -1;
-
- if (SetFileTime(handle, NULL, &atime, &mtime) == 0)
- return -1;
-
- return 0;
-}
-
int p_readlink(const char *path, char *buf, size_t bufsiz)
{
git_win32_path path_w, target_w;
@@ -282,12 +395,91 @@ int p_symlink(const char *old, const char *new)
return git_futils_fake_symlink(old, new);
}
+struct open_opts {
+ DWORD access;
+ DWORD sharing;
+ SECURITY_ATTRIBUTES security;
+ DWORD creation_disposition;
+ DWORD attributes;
+ int osf_flags;
+};
+
+GIT_INLINE(void) open_opts_from_posix(struct open_opts *opts, int flags, mode_t mode)
+{
+ memset(opts, 0, sizeof(struct open_opts));
+
+ switch (flags & (O_WRONLY | O_RDWR)) {
+ case O_WRONLY:
+ opts->access = GENERIC_WRITE;
+ break;
+ case O_RDWR:
+ opts->access = GENERIC_READ | GENERIC_WRITE;
+ break;
+ default:
+ opts->access = GENERIC_READ;
+ break;
+ }
+
+ opts->sharing = (DWORD)git_win32__createfile_sharemode;
+
+ switch (flags & (O_CREAT | O_TRUNC | O_EXCL)) {
+ case O_CREAT | O_EXCL:
+ case O_CREAT | O_TRUNC | O_EXCL:
+ opts->creation_disposition = CREATE_NEW;
+ break;
+ case O_CREAT | O_TRUNC:
+ opts->creation_disposition = CREATE_ALWAYS;
+ break;
+ case O_TRUNC:
+ opts->creation_disposition = TRUNCATE_EXISTING;
+ break;
+ case O_CREAT:
+ opts->creation_disposition = OPEN_ALWAYS;
+ break;
+ default:
+ opts->creation_disposition = OPEN_EXISTING;
+ break;
+ }
+
+ opts->attributes = ((flags & O_CREAT) && !(mode & S_IWRITE)) ?
+ FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL;
+ opts->osf_flags = flags & (O_RDONLY | O_APPEND);
+
+ opts->security.nLength = sizeof(SECURITY_ATTRIBUTES);
+ opts->security.lpSecurityDescriptor = NULL;
+ opts->security.bInheritHandle = 0;
+}
+
+GIT_INLINE(int) open_once(
+ const wchar_t *path,
+ struct open_opts *opts)
+{
+ int fd;
+
+ HANDLE handle = CreateFileW(path, opts->access, opts->sharing,
+ &opts->security, opts->creation_disposition, opts->attributes, 0);
+
+ if (handle == INVALID_HANDLE_VALUE) {
+ if (last_error_retryable())
+ return GIT_RETRY;
+
+ set_errno();
+ return -1;
+ }
+
+ if ((fd = _open_osfhandle((intptr_t)handle, opts->osf_flags)) < 0)
+ CloseHandle(handle);
+
+ return fd;
+}
+
int p_open(const char *path, int flags, ...)
{
- git_win32_path buf;
+ git_win32_path wpath;
mode_t mode = 0;
+ struct open_opts opts = {0};
- if (git_win32_path_from_utf8(buf, path) < 0)
+ if (git_win32_path_from_utf8(wpath, path) < 0)
return -1;
if (flags & O_CREAT) {
@@ -298,19 +490,83 @@ int p_open(const char *path, int flags, ...)
va_end(arg_list);
}
- return _wopen(buf, flags | STANDARD_OPEN_FLAGS, mode & WIN32_MODE_MASK);
+ open_opts_from_posix(&opts, flags, mode);
+
+ do_with_retries(
+ open_once(wpath, &opts),
+ 0);
}
int p_creat(const char *path, mode_t mode)
{
- git_win32_path buf;
+ return p_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
+}
- if (git_win32_path_from_utf8(buf, path) < 0)
+int p_utimes(const char *path, const struct p_timeval times[2])
+{
+ git_win32_path wpath;
+ int fd, error;
+ DWORD attrs_orig, attrs_new = 0;
+ struct open_opts opts = { 0 };
+
+ if (git_win32_path_from_utf8(wpath, path) < 0)
+ return -1;
+
+ attrs_orig = GetFileAttributesW(wpath);
+
+ if (attrs_orig & FILE_ATTRIBUTE_READONLY) {
+ attrs_new = attrs_orig & ~FILE_ATTRIBUTE_READONLY;
+
+ if (!SetFileAttributesW(wpath, attrs_new)) {
+ giterr_set(GITERR_OS, "failed to set attributes");
+ return -1;
+ }
+ }
+
+ open_opts_from_posix(&opts, O_RDWR, 0);
+
+ if ((fd = open_once(wpath, &opts)) < 0) {
+ error = -1;
+ goto done;
+ }
+
+ error = p_futimes(fd, times);
+ close(fd);
+
+done:
+ if (attrs_orig != attrs_new) {
+ DWORD os_error = GetLastError();
+ SetFileAttributesW(wpath, attrs_orig);
+ SetLastError(os_error);
+ }
+
+ return error;
+}
+
+int p_futimes(int fd, const struct p_timeval times[2])
+{
+ HANDLE handle;
+ FILETIME atime = { 0 }, mtime = { 0 };
+
+ if (times == NULL) {
+ SYSTEMTIME st;
+
+ GetSystemTime(&st);
+ SystemTimeToFileTime(&st, &atime);
+ SystemTimeToFileTime(&st, &mtime);
+ }
+ else {
+ git_win32__timeval_to_filetime(&atime, times[0]);
+ git_win32__timeval_to_filetime(&mtime, times[1]);
+ }
+
+ if ((handle = (HANDLE)_get_osfhandle(fd)) == INVALID_HANDLE_VALUE)
+ return -1;
+
+ if (SetFileTime(handle, NULL, &atime, &mtime) == 0)
return -1;
- return _wopen(buf,
- _O_WRONLY | _O_CREAT | _O_TRUNC | STANDARD_OPEN_FLAGS,
- mode & WIN32_MODE_MASK);
+ return 0;
}
int p_getcwd(char *buffer_out, size_t size)
@@ -583,62 +839,27 @@ int p_access(const char* path, mode_t mode)
return _waccess(buf, mode & WIN32_MODE_MASK);
}
-static int ensure_writable(wchar_t *fpath)
+GIT_INLINE(int) rename_once(const wchar_t *from, const wchar_t *to)
{
- DWORD attrs;
-
- attrs = GetFileAttributesW(fpath);
- if (attrs == INVALID_FILE_ATTRIBUTES) {
- if (GetLastError() == ERROR_FILE_NOT_FOUND)
- return 0;
-
- giterr_set(GITERR_OS, "failed to get attributes");
- return -1;
- }
-
- if (!(attrs & FILE_ATTRIBUTE_READONLY))
+ if (MoveFileExW(from, to, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
return 0;
- attrs &= ~FILE_ATTRIBUTE_READONLY;
- if (!SetFileAttributesW(fpath, attrs)) {
- giterr_set(GITERR_OS, "failed to set attributes");
- return -1;
- }
+ if (last_error_retryable())
+ return GIT_RETRY;
- return 0;
+ set_errno();
+ return -1;
}
int p_rename(const char *from, const char *to)
{
- git_win32_path wfrom;
- git_win32_path wto;
- int rename_tries;
- int rename_succeeded;
- int error;
+ git_win32_path wfrom, wto;
if (git_win32_path_from_utf8(wfrom, from) < 0 ||
git_win32_path_from_utf8(wto, to) < 0)
return -1;
- /* wait up to 50ms if file is locked by another thread or process */
- rename_tries = 0;
- rename_succeeded = 0;
- while (rename_tries < 10) {
- if (ensure_writable(wto) == 0 &&
- MoveFileExW(wfrom, wto, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) != 0) {
- rename_succeeded = 1;
- break;
- }
-
- error = GetLastError();
- if (error == ERROR_SHARING_VIOLATION || error == ERROR_ACCESS_DENIED) {
- Sleep(5);
- rename_tries++;
- } else
- break;
- }
-
- return rename_succeeded ? 0 : -1;
+ do_with_retries(rename_once(wfrom, wto), ensure_writable(wto));
}
int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags)
diff --git a/src/worktree.c b/src/worktree.c
index 55fbf5204..ede155b69 100644
--- a/src/worktree.c
+++ b/src/worktree.c
@@ -212,7 +212,7 @@ int git_worktree_open_from_repository(git_worktree **out, git_repository *repo)
goto out;
out:
- free(name);
+ git__free(name);
git_buf_free(&parent);
return error;
@@ -269,15 +269,32 @@ out:
return err;
}
-int git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *worktree)
+int git_worktree_add_init_options(git_worktree_add_options *opts,
+ unsigned int version)
+{
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
+ git_worktree_add_options, GIT_WORKTREE_ADD_OPTIONS_INIT);
+ return 0;
+}
+
+int git_worktree_add(git_worktree **out, git_repository *repo,
+ const char *name, const char *worktree,
+ const git_worktree_add_options *opts)
{
git_buf gitdir = GIT_BUF_INIT, wddir = GIT_BUF_INIT, buf = GIT_BUF_INIT;
git_reference *ref = NULL, *head = NULL;
git_commit *commit = NULL;
git_repository *wt = NULL;
git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
+ git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT;
int err;
+ GITERR_CHECK_VERSION(
+ opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options");
+
+ if (opts)
+ memcpy(&wtopts, opts, sizeof(wtopts));
+
assert(out && repo && name && worktree);
*out = NULL;
@@ -301,6 +318,21 @@ int git_worktree_add(git_worktree **out, git_repository *repo, const char *name,
if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
goto out;
+ if (wtopts.lock) {
+ int fd;
+
+ if ((err = git_buf_joinpath(&buf, gitdir.ptr, "locked")) < 0)
+ goto out;
+
+ if ((fd = p_creat(buf.ptr, 0644)) < 0) {
+ err = fd;
+ goto out;
+ }
+
+ p_close(fd);
+ git_buf_clear(&buf);
+ }
+
/* Create worktree .git file */
if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
goto out;
@@ -424,11 +456,29 @@ out:
return ret;
}
-int git_worktree_is_prunable(git_worktree *wt, unsigned flags)
+int git_worktree_prune_init_options(
+ git_worktree_prune_options *opts,
+ unsigned int version)
+{
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
+ git_worktree_prune_options, GIT_WORKTREE_PRUNE_OPTIONS_INIT);
+ return 0;
+}
+
+int git_worktree_is_prunable(git_worktree *wt,
+ git_worktree_prune_options *opts)
{
git_buf reason = GIT_BUF_INIT;
+ git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
+
+ GITERR_CHECK_VERSION(
+ opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
+ "git_worktree_prune_options");
+
+ if (opts)
+ memcpy(&popts, opts, sizeof(popts));
- if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
+ if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 &&
git_worktree_is_locked(&reason, wt))
{
if (!reason.size)
@@ -439,7 +489,7 @@ int git_worktree_is_prunable(git_worktree *wt, unsigned flags)
return 0;
}
- if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
+ if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
git_worktree_validate(wt) == 0)
{
giterr_set(GITERR_WORKTREE, "Not pruning valid working tree");
@@ -449,13 +499,22 @@ int git_worktree_is_prunable(git_worktree *wt, unsigned flags)
return 1;
}
-int git_worktree_prune(git_worktree *wt, unsigned flags)
+int git_worktree_prune(git_worktree *wt,
+ git_worktree_prune_options *opts)
{
+ git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
git_buf path = GIT_BUF_INIT;
char *wtpath;
int err;
- if (!git_worktree_is_prunable(wt, flags)) {
+ GITERR_CHECK_VERSION(
+ opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
+ "git_worktree_prune_options");
+
+ if (opts)
+ memcpy(&popts, opts, sizeof(popts));
+
+ if (!git_worktree_is_prunable(wt, &popts)) {
err = -1;
goto out;
}
@@ -474,7 +533,7 @@ int git_worktree_prune(git_worktree *wt, unsigned flags)
/* Skip deletion of the actual working tree if it does
* not exist or deletion was not requested */
- if ((flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
+ if ((popts.flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
!git_path_exists(wt->gitlink_path))
{
goto out;
diff --git a/tests/attr/ignore.c b/tests/attr/ignore.c
index 3a193890a..a089ee408 100644
--- a/tests/attr/ignore.c
+++ b/tests/attr/ignore.c
@@ -291,3 +291,15 @@ void test_attr_ignore__symlink_to_outside(void)
assert_is_ignored(true, "symlink");
assert_is_ignored(true, "lala/../symlink");
}
+
+void test_attr_ignore__test(void)
+{
+ cl_git_rewritefile("attr/.gitignore",
+ "/*/\n"
+ "!/src\n");
+ assert_is_ignored(false, "src/foo.c");
+ assert_is_ignored(false, "src/foo/foo.c");
+ assert_is_ignored(false, "README.md");
+ assert_is_ignored(true, "dist/foo.o");
+ assert_is_ignored(true, "bin/foo");
+}
diff --git a/tests/checkout/head.c b/tests/checkout/head.c
index 07cc1d209..ded86df33 100644
--- a/tests/checkout/head.c
+++ b/tests/checkout/head.c
@@ -38,7 +38,7 @@ void test_checkout_head__with_index_only_tree(void)
cl_git_pass(git_repository_index(&index, g_repo));
p_mkdir("testrepo/newdir", 0777);
- cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
+ cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
cl_git_pass(git_index_add_bypath(index, "newdir/newfile.txt"));
cl_git_pass(git_index_write(index));
@@ -60,3 +60,79 @@ void test_checkout_head__with_index_only_tree(void)
git_index_free(index);
}
+
+void test_checkout_head__do_not_remove_untracked_file(void)
+{
+ git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
+ git_index *index;
+
+ cl_git_pass(p_mkdir("testrepo/tracked", 0755));
+ cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
+ cl_git_mkfile("testrepo/tracked/untracked", "untracked\n");
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
+ cl_git_pass(git_index_write(index));
+
+ git_index_free(index);
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_assert(!git_path_isfile("testrepo/tracked/tracked"));
+ cl_assert(git_path_isfile("testrepo/tracked/untracked"));
+}
+
+void test_checkout_head__do_not_remove_untracked_file_in_subdir(void)
+{
+ git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
+ git_index *index;
+
+ cl_git_pass(p_mkdir("testrepo/tracked", 0755));
+ cl_git_pass(p_mkdir("testrepo/tracked/subdir", 0755));
+ cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
+ cl_git_mkfile("testrepo/tracked/subdir/tracked", "tracked\n");
+ cl_git_mkfile("testrepo/tracked/subdir/untracked", "untracked\n");
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
+ cl_git_pass(git_index_add_bypath(index, "tracked/subdir/tracked"));
+ cl_git_pass(git_index_write(index));
+
+ git_index_free(index);
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_assert(!git_path_isfile("testrepo/tracked/tracked"));
+ cl_assert(!git_path_isfile("testrepo/tracked/subdir/tracked"));
+ cl_assert(git_path_isfile("testrepo/tracked/subdir/untracked"));
+}
+
+void test_checkout_head__do_remove_tracked_subdir(void)
+{
+ git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
+ git_index *index;
+
+ cl_git_pass(p_mkdir("testrepo/subdir", 0755));
+ cl_git_pass(p_mkdir("testrepo/subdir/tracked", 0755));
+ cl_git_mkfile("testrepo/subdir/tracked-file", "tracked\n");
+ cl_git_mkfile("testrepo/subdir/untracked-file", "untracked\n");
+ cl_git_mkfile("testrepo/subdir/tracked/tracked1", "tracked\n");
+ cl_git_mkfile("testrepo/subdir/tracked/tracked2", "tracked\n");
+
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_add_bypath(index, "subdir/tracked-file"));
+ cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked1"));
+ cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked2"));
+ cl_git_pass(git_index_write(index));
+
+ git_index_free(index);
+
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ cl_git_pass(git_checkout_head(g_repo, &opts));
+
+ cl_assert(!git_path_isdir("testrepo/subdir/tracked"));
+ cl_assert(!git_path_isfile("testrepo/subdir/tracked-file"));
+ cl_assert(git_path_isfile("testrepo/subdir/untracked-file"));
+}
diff --git a/tests/clone/local.c b/tests/clone/local.c
index 91a0a1c2a..7f54d05de 100644
--- a/tests/clone/local.c
+++ b/tests/clone/local.c
@@ -16,6 +16,7 @@ static int file_url(git_buf *buf, const char *host, const char *path)
return git_buf_printf(buf, "file://%s/%s", host, path);
}
+#ifdef GIT_WIN32
static int git_style_unc_path(git_buf *buf, const char *host, const char *path)
{
git_buf_clear(buf);
@@ -49,6 +50,7 @@ static int unc_path(git_buf *buf, const char *host, const char *path)
return 0;
}
+#endif
void test_clone_local__should_clone_local(void)
{
diff --git a/tests/config/include.c b/tests/config/include.c
index 882b89b16..0a07c9b85 100644
--- a/tests/config/include.c
+++ b/tests/config/include.c
@@ -108,6 +108,26 @@ void test_config_include__missing(void)
git_config_free(cfg);
}
+void test_config_include__missing_homedir(void)
+{
+ git_config *cfg;
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config")));
+ cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz");
+
+ giterr_clear();
+ cl_git_pass(git_config_open_ondisk(&cfg, "including"));
+ cl_assert(giterr_last() == NULL);
+ cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar"));
+ cl_assert_equal_s("baz", git_buf_cstr(&buf));
+
+ git_buf_free(&buf);
+ git_config_free(cfg);
+
+ cl_sandbox_set_search_path_defaults();
+}
+
#define replicate10(s) s s s s s s s s s s
void test_config_include__depth2(void)
{
diff --git a/tests/config/readonly.c b/tests/config/readonly.c
new file mode 100644
index 000000000..f45abdd29
--- /dev/null
+++ b/tests/config/readonly.c
@@ -0,0 +1,64 @@
+#include "clar_libgit2.h"
+#include "config_file.h"
+#include "config.h"
+
+static git_config *cfg;
+
+void test_config_readonly__initialize(void)
+{
+ cl_git_pass(git_config_new(&cfg));
+}
+
+void test_config_readonly__cleanup(void)
+{
+ git_config_free(cfg);
+ cfg = NULL;
+}
+
+void test_config_readonly__writing_to_readonly_fails(void)
+{
+ git_config_backend *backend;
+
+ cl_git_pass(git_config_file__ondisk(&backend, "global"));
+ backend->readonly = 1;
+ cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, 0));
+
+ cl_git_fail_with(GIT_ENOTFOUND, git_config_set_string(cfg, "foo.bar", "baz"));
+ cl_assert(!git_path_exists("global"));
+}
+
+void test_config_readonly__writing_to_cfg_with_rw_precedence_succeeds(void)
+{
+ git_config_backend *backend;
+
+ cl_git_pass(git_config_file__ondisk(&backend, "global"));
+ backend->readonly = 1;
+ cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, 0));
+
+ cl_git_pass(git_config_file__ondisk(&backend, "local"));
+ cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_LOCAL, 0));
+
+ cl_git_pass(git_config_set_string(cfg, "foo.bar", "baz"));
+
+ cl_assert(git_path_exists("local"));
+ cl_assert(!git_path_exists("global"));
+ cl_git_pass(p_unlink("local"));
+}
+
+void test_config_readonly__writing_to_cfg_with_ro_precedence_succeeds(void)
+{
+ git_config_backend *backend;
+
+ cl_git_pass(git_config_file__ondisk(&backend, "local"));
+ backend->readonly = 1;
+ cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_LOCAL, 0));
+
+ cl_git_pass(git_config_file__ondisk(&backend, "global"));
+ cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, 0));
+
+ cl_git_pass(git_config_set_string(cfg, "foo.bar", "baz"));
+
+ cl_assert(!git_path_exists("local"));
+ cl_assert(git_path_exists("global"));
+ cl_git_pass(p_unlink("global"));
+}
diff --git a/tests/core/encoding.c b/tests/core/encoding.c
index 7d91720f4..a677afe2e 100644
--- a/tests/core/encoding.c
+++ b/tests/core/encoding.c
@@ -29,6 +29,9 @@ void test_core_encoding__encode(void)
cl_assert(git_encode_varint(buf, 100, 65) == 1);
cl_assert(buf[0] == 'A');
+ cl_assert(git_encode_varint(buf, 1, 1) == 1);
+ cl_assert(!memcmp(buf, "\x01", 1));
+
cl_assert(git_encode_varint(buf, 100, 267869656) == 4);
cl_assert(!memcmp(buf, "\xfe\xdc\xbaX", 4));
diff --git a/tests/core/features.c b/tests/core/features.c
index cf5e19063..7b28cc0cb 100644
--- a/tests/core/features.c
+++ b/tests/core/features.c
@@ -17,7 +17,9 @@ void test_core_features__0(void)
cl_assert((caps & GIT_FEATURE_THREADS) == 0);
#endif
+#ifdef GIT_HTTPS
cl_assert((caps & GIT_FEATURE_HTTPS) != 0);
+#endif
#if defined(GIT_SSH)
cl_assert((caps & GIT_FEATURE_SSH) != 0);
diff --git a/tests/core/structinit.c b/tests/core/structinit.c
index e9f7b4a74..78503fcc6 100644
--- a/tests/core/structinit.c
+++ b/tests/core/structinit.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include <git2/sys/config.h>
+#include <git2/sys/filter.h>
#include <git2/sys/odb_backend.h>
#include <git2/sys/refdb_backend.h>
#include <git2/sys/transport.h>
@@ -96,6 +97,11 @@ void test_core_structinit__compare(void)
git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_init_options);
+ /* filter */
+ CHECK_MACRO_FUNC_INIT_EQUAL( \
+ git_filter, GIT_FILTER_VERSION, \
+ GIT_FILTER_INIT, git_filter_init);
+
/* merge_file_input */
CHECK_MACRO_FUNC_INIT_EQUAL( \
git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \
@@ -165,4 +171,9 @@ void test_core_structinit__compare(void)
CHECK_MACRO_FUNC_INIT_EQUAL( \
git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_init_options);
+
+ /* submodule update */
+ CHECK_MACRO_FUNC_INIT_EQUAL( \
+ git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
+ GIT_PROXY_OPTIONS_INIT, git_proxy_init_options);
}
diff --git a/tests/index/version.c b/tests/index/version.c
index 3fd240d3c..7ada302b5 100644
--- a/tests/index/version.c
+++ b/tests/index/version.c
@@ -3,39 +3,135 @@
static git_repository *g_repo = NULL;
-void test_index_version__can_write_v4(void)
+void test_index_version__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+ g_repo = NULL;
+}
+
+void test_index_version__can_read_v4(void)
{
+ const char *paths[] = {
+ "file.tx", "file.txt", "file.txz", "foo", "zzz",
+ };
git_index *index;
- const git_index_entry *entry;
+ size_t i;
+
+ g_repo = cl_git_sandbox_init("indexv4");
- g_repo = cl_git_sandbox_init("filemodes");
cl_git_pass(git_repository_index(&index, g_repo));
+ cl_assert_equal_sz(git_index_entrycount(index), 5);
- cl_assert(index->on_disk);
- cl_assert(git_index_version(index) == 2);
+ for (i = 0; i < ARRAY_SIZE(paths); i++) {
+ const git_index_entry *entry =
+ git_index_get_bypath(index, paths[i], GIT_INDEX_STAGE_NORMAL);
- cl_assert(git_index_entrycount(index) == 6);
+ cl_assert(entry != NULL);
+ }
+ git_index_free(index);
+}
+
+void test_index_version__can_write_v4(void)
+{
+ const char *paths[] = {
+ "foo",
+ "foox",
+ "foobar",
+ "foobal",
+ "x",
+ "xz",
+ "xyzzyx"
+ };
+ git_index_entry entry;
+ git_index *index;
+ size_t i;
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+ cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_set_version(index, 4));
+ for (i = 0; i < ARRAY_SIZE(paths); i++) {
+ memset(&entry, 0, sizeof(entry));
+ entry.path = paths[i];
+ entry.mode = GIT_FILEMODE_BLOB;
+ cl_git_pass(git_index_add_frombuffer(index, &entry, paths[i],
+ strlen(paths[i]) + 1));
+ }
+ cl_assert_equal_sz(git_index_entrycount(index), ARRAY_SIZE(paths));
+
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert(git_index_version(index) == 4);
- entry = git_index_get_bypath(index, "exec_off", 0);
- cl_assert(entry);
- entry = git_index_get_bypath(index, "exec_off2on_staged", 0);
- cl_assert(entry);
- entry = git_index_get_bypath(index, "exec_on", 0);
- cl_assert(entry);
+ for (i = 0; i < ARRAY_SIZE(paths); i++) {
+ const git_index_entry *e;
+
+ cl_assert(e = git_index_get_bypath(index, paths[i], 0));
+ cl_assert_equal_s(paths[i], e->path);
+ }
git_index_free(index);
}
-void test_index_version__cleanup(void)
+void test_index_version__v4_uses_path_compression(void)
{
- cl_git_sandbox_cleanup();
- g_repo = NULL;
+ git_index_entry entry;
+ git_index *index;
+ char path[250], buf[1];
+ struct stat st;
+ char i, j;
+
+ memset(path, 'a', sizeof(path));
+ memset(buf, 'a', sizeof(buf));
+
+ memset(&entry, 0, sizeof(entry));
+ entry.path = path;
+ entry.mode = GIT_FILEMODE_BLOB;
+
+ g_repo = cl_git_sandbox_init("indexv4");
+ cl_git_pass(git_repository_index(&index, g_repo));
+
+ /* write 676 paths of 250 bytes length */
+ for (i = 'a'; i <= 'z'; i++) {
+ for (j = 'a'; j < 'z'; j++) {
+ path[ARRAY_SIZE(path) - 3] = i;
+ path[ARRAY_SIZE(path) - 2] = j;
+ path[ARRAY_SIZE(path) - 1] = '\0';
+ cl_git_pass(git_index_add_frombuffer(index, &entry, buf, sizeof(buf)));
+ }
+ }
+
+ cl_git_pass(git_index_write(index));
+ cl_git_pass(p_stat(git_index_path(index), &st));
+
+ /*
+ * Without path compression, the written paths would at
+ * least take
+ *
+ * (entries * pathlen) = len
+ * (676 * 250) = 169000
+ *
+ * bytes. As index v4 uses suffix-compression and our
+ * written paths only differ in the last two entries,
+ * this number will be much smaller, e.g.
+ *
+ * (1 * pathlen) + (675 * 2) = len
+ * 676 + 1350 = 2026
+ *
+ * bytes.
+ *
+ * Note that the above calculations do not include
+ * additional metadata of the index, e.g. OIDs or
+ * index extensions. Including those we get an index
+ * of approx. 200kB without compression and 40kB with
+ * compression. As this is a lot smaller than without
+ * compression, we can verify that path compression is
+ * used.
+ */
+ cl_assert_(st.st_size < 75000, "path compression not enabled");
+
+ git_index_free(index);
}
diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c
index 6194802af..76872e51f 100644
--- a/tests/network/remote/local.c
+++ b/tests/network/remote/local.c
@@ -2,6 +2,7 @@
#include "buffer.h"
#include "path.h"
#include "posix.h"
+#include "git2/sys/repository.h"
static git_repository *repo;
static git_buf file_path_buf = GIT_BUF_INIT;
@@ -465,3 +466,19 @@ void test_network_remote_local__push_delete(void)
cl_fixture_cleanup("target.git");
cl_git_sandbox_cleanup();
}
+
+void test_network_remote_local__anonymous_remote_inmemory_repo(void)
+{
+ git_repository *inmemory;
+ git_remote *remote;
+
+ git_buf_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git")));
+
+ cl_git_pass(git_repository_new(&inmemory));
+ cl_git_pass(git_remote_create_anonymous(&remote, inmemory, git_buf_cstr(&file_path_buf)));
+ cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
+ cl_assert(git_remote_connected(remote));
+ git_remote_disconnect(remote);
+ git_remote_free(remote);
+ git_repository_free(inmemory);
+}
diff --git a/tests/object/lookup.c b/tests/object/lookup.c
index cfa6d4678..544f32bc4 100644
--- a/tests/object/lookup.c
+++ b/tests/object/lookup.c
@@ -6,13 +6,12 @@ static git_repository *g_repo;
void test_object_lookup__initialize(void)
{
- cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
+ g_repo = cl_git_sandbox_init("testrepo.git");
}
void test_object_lookup__cleanup(void)
{
- git_repository_free(g_repo);
- g_repo = NULL;
+ cl_git_sandbox_cleanup();
}
void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
@@ -63,3 +62,61 @@ void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
}
+void test_object_lookup__lookup_corrupt_object_returns_error(void)
+{
+ const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
+ *file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
+ git_buf path = GIT_BUF_INIT, contents = GIT_BUF_INIT;
+ git_oid oid;
+ git_object *object;
+ size_t i;
+
+ cl_git_pass(git_oid_fromstr(&oid, commit));
+ cl_git_pass(git_buf_joinpath(&path, git_repository_path(g_repo), file));
+ cl_git_pass(git_futils_readbuffer(&contents, path.ptr));
+
+ /* Corrupt and try to read the object */
+ for (i = 0; i < contents.size; i++) {
+ contents.ptr[i] ^= 0x1;
+ cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
+ cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
+ contents.ptr[i] ^= 0x1;
+ }
+
+ /* Restore original content and assert we can read the object */
+ cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
+ cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
+
+ git_object_free(object);
+ git_buf_free(&path);
+ git_buf_free(&contents);
+}
+
+void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
+{
+ const char *oldloose = "objects/8e/73b769e97678d684b809b163bebdae2911720f",
+ *newloose = "objects/8e/73b769e97678d684b809b163bebdae2911720e",
+ *commit = "8e73b769e97678d684b809b163bebdae2911720e";
+ git_buf oldpath = GIT_BUF_INIT, newpath = GIT_BUF_INIT;
+ git_object *object;
+ git_oid oid;
+
+ cl_git_pass(git_oid_fromstr(&oid, commit));
+
+ /* Copy object to another location with wrong hash */
+ cl_git_pass(git_buf_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
+ cl_git_pass(git_buf_joinpath(&newpath, git_repository_path(g_repo), newloose));
+ cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644));
+
+ /* Verify that lookup fails due to a hashsum mismatch */
+ cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
+
+ /* Disable verification and try again */
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+ cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
+
+ git_object_free(object);
+ git_buf_free(&oldpath);
+ git_buf_free(&newpath);
+}
diff --git a/tests/odb/backend/backend_helpers.c b/tests/odb/backend/backend_helpers.c
new file mode 100644
index 000000000..37a8fd200
--- /dev/null
+++ b/tests/odb/backend/backend_helpers.c
@@ -0,0 +1,159 @@
+#include "clar_libgit2.h"
+#include "git2/sys/odb_backend.h"
+#include "backend_helpers.h"
+
+static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
+{
+ const fake_object *obj = fake->objects, *found = NULL;
+
+ while (obj && obj->oid) {
+ git_oid current_oid;
+
+ git_oid_fromstr(&current_oid, obj->oid);
+
+ if (git_oid_ncmp(&current_oid, oid, len) == 0) {
+ if (found)
+ return GIT_EAMBIGUOUS;
+ found = obj;
+ }
+
+ obj++;
+ }
+
+ if (found && out)
+ *out = found;
+
+ return found ? GIT_OK : GIT_ENOTFOUND;
+}
+
+static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
+{
+ fake_backend *fake;
+
+ fake = (fake_backend *)backend;
+
+ fake->exists_calls++;
+
+ return search_object(NULL, fake, oid, GIT_OID_HEXSZ) == GIT_OK;
+}
+
+static int fake_backend__exists_prefix(
+ git_oid *out, git_odb_backend *backend, const git_oid *oid, size_t len)
+{
+ const fake_object *obj;
+ fake_backend *fake;
+ int error;
+
+ fake = (fake_backend *)backend;
+
+ fake->exists_prefix_calls++;
+
+ if ((error = search_object(&obj, fake, oid, len)) < 0)
+ return error;
+
+ if (out)
+ git_oid_fromstr(out, obj->oid);
+
+ return 0;
+}
+
+static int fake_backend__read(
+ void **buffer_p, size_t *len_p, git_otype *type_p,
+ git_odb_backend *backend, const git_oid *oid)
+{
+ const fake_object *obj;
+ fake_backend *fake;
+ int error;
+
+ fake = (fake_backend *)backend;
+
+ fake->read_calls++;
+
+ if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
+ return error;
+
+ *len_p = strlen(obj->content);
+ *buffer_p = git__strdup(obj->content);
+ *type_p = GIT_OBJ_BLOB;
+
+ return 0;
+}
+
+static int fake_backend__read_header(
+ size_t *len_p, git_otype *type_p,
+ git_odb_backend *backend, const git_oid *oid)
+{
+ const fake_object *obj;
+ fake_backend *fake;
+ int error;
+
+ fake = (fake_backend *)backend;
+
+ fake->read_header_calls++;
+
+ if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
+ return error;
+
+ *len_p = strlen(obj->content);
+ *type_p = GIT_OBJ_BLOB;
+
+ return 0;
+}
+
+static int fake_backend__read_prefix(
+ git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p,
+ git_odb_backend *backend, const git_oid *short_oid, size_t len)
+{
+ const fake_object *obj;
+ fake_backend *fake;
+ int error;
+
+ fake = (fake_backend *)backend;
+
+ fake->read_prefix_calls++;
+
+ if ((error = search_object(&obj, fake, short_oid, len)) < 0)
+ return error;
+
+ git_oid_fromstr(out_oid, obj->oid);
+ *len_p = strlen(obj->content);
+ *buffer_p = git__strdup(obj->content);
+ *type_p = GIT_OBJ_BLOB;
+
+ return 0;
+}
+
+static void fake_backend__free(git_odb_backend *_backend)
+{
+ fake_backend *backend;
+
+ backend = (fake_backend *)_backend;
+
+ git__free(backend);
+}
+
+int build_fake_backend(
+ git_odb_backend **out,
+ const fake_object *objects)
+{
+ fake_backend *backend;
+
+ backend = git__calloc(1, sizeof(fake_backend));
+ GITERR_CHECK_ALLOC(backend);
+
+ backend->parent.version = GIT_ODB_BACKEND_VERSION;
+
+ backend->parent.refresh = NULL;
+ backend->objects = objects;
+
+ backend->parent.read = fake_backend__read;
+ backend->parent.read_prefix = fake_backend__read_prefix;
+ backend->parent.read_header = fake_backend__read_header;
+ backend->parent.exists = fake_backend__exists;
+ backend->parent.exists_prefix = fake_backend__exists_prefix;
+ backend->parent.free = &fake_backend__free;
+
+ *out = (git_odb_backend *)backend;
+
+ return 0;
+}
diff --git a/tests/odb/backend/backend_helpers.h b/tests/odb/backend/backend_helpers.h
new file mode 100644
index 000000000..5c393c0a5
--- /dev/null
+++ b/tests/odb/backend/backend_helpers.h
@@ -0,0 +1,22 @@
+#include "git2/sys/odb_backend.h"
+
+typedef struct {
+ const char *oid;
+ const char *content;
+} fake_object;
+
+typedef struct {
+ git_odb_backend parent;
+
+ int exists_calls;
+ int exists_prefix_calls;
+ int read_calls;
+ int read_header_calls;
+ int read_prefix_calls;
+
+ const fake_object *objects;
+} fake_backend;
+
+int build_fake_backend(
+ git_odb_backend **out,
+ const fake_object *objects);
diff --git a/tests/odb/backend/multiple.c b/tests/odb/backend/multiple.c
new file mode 100644
index 000000000..1c6068df3
--- /dev/null
+++ b/tests/odb/backend/multiple.c
@@ -0,0 +1,121 @@
+#include "clar_libgit2.h"
+#include "repository.h"
+#include "backend_helpers.h"
+
+#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
+
+static git_repository *_repo;
+static git_odb_object *_obj;
+static fake_backend *_fake_empty;
+static fake_backend *_fake_filled;
+
+static git_oid _existing_oid;
+
+static const fake_object _objects_filled[] = {
+ { EXISTING_HASH, "" },
+ { NULL, NULL }
+};
+
+static const fake_object _objects_empty[] = {
+ { NULL, NULL }
+};
+
+void test_odb_backend_multiple__initialize(void)
+{
+ git_odb_backend *backend;
+
+ git_oid_fromstr(&_existing_oid, EXISTING_HASH);
+
+ _obj = NULL;
+ _repo = cl_git_sandbox_init("testrepo.git");
+
+ cl_git_pass(build_fake_backend(&backend, _objects_filled));
+ _fake_filled = (fake_backend *)backend;
+
+ cl_git_pass(build_fake_backend(&backend, _objects_empty));
+ _fake_empty = (fake_backend *)backend;
+}
+
+void test_odb_backend_multiple__cleanup(void)
+{
+ git_odb_object_free(_obj);
+ cl_git_sandbox_cleanup();
+}
+
+void test_odb_backend_multiple__read_with_empty_first_succeeds(void)
+{
+ git_odb *odb;
+
+ cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 10));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 50));
+
+ cl_git_pass(git_odb_read(&_obj, odb, &_existing_oid));
+
+ cl_assert_equal_i(1, _fake_filled->read_calls);
+ cl_assert_equal_i(1, _fake_empty->read_calls);
+}
+
+void test_odb_backend_multiple__read_with_first_matching_stops(void)
+{
+ git_odb *odb;
+
+ cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 10));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
+
+ cl_git_pass(git_odb_read(&_obj, odb, &_existing_oid));
+
+ cl_assert_equal_i(1, _fake_filled->read_calls);
+ cl_assert_equal_i(0, _fake_empty->read_calls);
+}
+
+void test_odb_backend_multiple__read_prefix_with_first_empty_succeeds(void)
+{
+ git_odb *odb;
+
+ cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 10));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 50));
+
+ cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
+
+ cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
+ cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
+}
+
+void test_odb_backend_multiple__read_prefix_with_first_matching_reads_both(void)
+{
+ git_odb *odb;
+
+ cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, -10));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
+
+ cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
+
+ cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
+ cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
+}
+
+void test_odb_backend_multiple__read_prefix_with_first_matching_succeeds_without_hash_verification(void)
+{
+ git_odb *odb;
+
+ git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0);
+
+ cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, -10));
+ cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
+
+ cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
+
+ /*
+ * Both backends should be checked as we have to check
+ * for collisions
+ */
+ cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
+ cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
+
+ git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1);
+}
diff --git a/tests/odb/backend/nonrefreshing.c b/tests/odb/backend/nonrefreshing.c
index b43529479..6abc0c6d2 100644
--- a/tests/odb/backend/nonrefreshing.c
+++ b/tests/odb/backend/nonrefreshing.c
@@ -1,150 +1,41 @@
#include "clar_libgit2.h"
-#include "git2/sys/odb_backend.h"
#include "repository.h"
-
-typedef struct fake_backend {
- git_odb_backend parent;
-
- git_error_code error_code;
-
- int exists_calls;
- int read_calls;
- int read_header_calls;
- int read_prefix_calls;
-} fake_backend;
+#include "backend_helpers.h"
static git_repository *_repo;
static fake_backend *_fake;
-static git_oid _oid;
-
-static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
-{
- fake_backend *fake;
-
- GIT_UNUSED(oid);
-
- fake = (fake_backend *)backend;
-
- fake->exists_calls++;
-
- return (fake->error_code == GIT_OK);
-}
-
-static int fake_backend__read(
- void **buffer_p, size_t *len_p, git_otype *type_p,
- git_odb_backend *backend, const git_oid *oid)
-{
- fake_backend *fake;
-
- GIT_UNUSED(buffer_p);
- GIT_UNUSED(len_p);
- GIT_UNUSED(type_p);
- GIT_UNUSED(oid);
-
- fake = (fake_backend *)backend;
-
- fake->read_calls++;
-
- *len_p = 0;
- *buffer_p = NULL;
- *type_p = GIT_OBJ_BLOB;
-
- return fake->error_code;
-}
-
-static int fake_backend__read_header(
- size_t *len_p, git_otype *type_p,
- git_odb_backend *backend, const git_oid *oid)
-{
- fake_backend *fake;
-
- GIT_UNUSED(len_p);
- GIT_UNUSED(type_p);
- GIT_UNUSED(oid);
-
- fake = (fake_backend *)backend;
-
- fake->read_header_calls++;
-
- *len_p = 0;
- *type_p = GIT_OBJ_BLOB;
-
- return fake->error_code;
-}
-
-static int fake_backend__read_prefix(
- git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p,
- git_odb_backend *backend, const git_oid *short_oid, size_t len)
-{
- fake_backend *fake;
- GIT_UNUSED(out_oid);
- GIT_UNUSED(buffer_p);
- GIT_UNUSED(len_p);
- GIT_UNUSED(type_p);
- GIT_UNUSED(short_oid);
- GIT_UNUSED(len);
+#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
+#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
- fake = (fake_backend *)backend;
+static const fake_object _objects[] = {
+ { EXISTING_HASH, "" },
+ { NULL, NULL }
+};
- fake->read_prefix_calls++;
+static git_oid _nonexisting_oid;
+static git_oid _existing_oid;
- *len_p = 0;
- *buffer_p = NULL;
- *type_p = GIT_OBJ_BLOB;
-
- return fake->error_code;
-}
-
-static void fake_backend__free(git_odb_backend *_backend)
-{
- fake_backend *backend;
-
- backend = (fake_backend *)_backend;
-
- git__free(backend);
-}
-
-static int build_fake_backend(
- git_odb_backend **out,
- git_error_code error_code)
-{
- fake_backend *backend;
-
- backend = git__calloc(1, sizeof(fake_backend));
- GITERR_CHECK_ALLOC(backend);
-
- backend->parent.version = GIT_ODB_BACKEND_VERSION;
-
- backend->parent.refresh = NULL;
- backend->error_code = error_code;
-
- backend->parent.read = fake_backend__read;
- backend->parent.read_prefix = fake_backend__read_prefix;
- backend->parent.read_header = fake_backend__read_header;
- backend->parent.exists = fake_backend__exists;
- backend->parent.free = &fake_backend__free;
-
- *out = (git_odb_backend *)backend;
-
- return 0;
-}
-
-static void setup_repository_and_backend(git_error_code error_code)
+static void setup_repository_and_backend(void)
{
git_odb *odb = NULL;
git_odb_backend *backend = NULL;
_repo = cl_git_sandbox_init("testrepo.git");
- cl_git_pass(build_fake_backend(&backend, error_code));
+ cl_git_pass(build_fake_backend(&backend, _objects));
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, backend, 10));
_fake = (fake_backend *)backend;
+}
- cl_git_pass(git_oid_fromstr(&_oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
+void test_odb_backend_nonrefreshing__initialize(void)
+{
+ git_oid_fromstr(&_nonexisting_oid, NONEXISTING_HASH);
+ git_oid_fromstr(&_existing_oid, EXISTING_HASH);
+ setup_repository_and_backend();
}
void test_odb_backend_nonrefreshing__cleanup(void)
@@ -156,10 +47,8 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void)
{
git_odb *odb;
- setup_repository_and_backend(GIT_ENOTFOUND);
-
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
- cl_assert_equal_b(false, git_odb_exists(odb, &_oid));
+ cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid));
cl_assert_equal_i(1, _fake->exists_calls);
}
@@ -168,10 +57,8 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void)
{
git_object *obj;
- setup_repository_and_backend(GIT_ENOTFOUND);
-
cl_git_fail_with(
- git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY),
+ git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJ_ANY),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_calls);
@@ -181,10 +68,8 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void)
{
git_object *obj;
- setup_repository_and_backend(GIT_ENOTFOUND);
-
cl_git_fail_with(
- git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY),
+ git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJ_ANY),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_prefix_calls);
@@ -196,12 +81,10 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void)
size_t len;
git_otype type;
- setup_repository_and_backend(GIT_ENOTFOUND);
-
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_fail_with(
- git_odb_read_header(&len, &type, odb, &_oid),
+ git_odb_read_header(&len, &type, odb, &_nonexisting_oid),
GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_header_calls);
@@ -211,10 +94,8 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void)
{
git_odb *odb;
- setup_repository_and_backend(GIT_OK);
-
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
- cl_assert_equal_b(true, git_odb_exists(odb, &_oid));
+ cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid));
cl_assert_equal_i(1, _fake->exists_calls);
}
@@ -223,9 +104,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void)
{
git_object *obj;
- setup_repository_and_backend(GIT_OK);
-
- cl_git_pass(git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY));
+ cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJ_ANY));
cl_assert_equal_i(1, _fake->read_calls);
@@ -236,9 +115,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void)
{
git_object *obj;
- setup_repository_and_backend(GIT_OK);
-
- cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY));
+ cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJ_ANY));
cl_assert_equal_i(1, _fake->read_prefix_calls);
@@ -251,11 +128,9 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void)
size_t len;
git_otype type;
- setup_repository_and_backend(GIT_OK);
-
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
- cl_git_pass(git_odb_read_header(&len, &type, odb, &_oid));
+ cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid));
cl_assert_equal_i(1, _fake->read_header_calls);
}
@@ -264,8 +139,6 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_when_revparsing_a_full
{
git_object *obj;
- setup_repository_and_backend(GIT_ENOTFOUND);
-
cl_git_fail_with(
git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
GIT_ENOTFOUND);
diff --git a/tests/odb/backend/simple.c b/tests/odb/backend/simple.c
new file mode 100644
index 000000000..c0fcd403b
--- /dev/null
+++ b/tests/odb/backend/simple.c
@@ -0,0 +1,232 @@
+#include "clar_libgit2.h"
+#include "repository.h"
+#include "backend_helpers.h"
+
+#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
+
+static git_repository *_repo;
+static git_odb *_odb;
+static git_odb_object *_obj;
+static git_oid _oid;
+
+static void setup_backend(const fake_object *objs)
+{
+ git_odb_backend *backend;
+
+ cl_git_pass(build_fake_backend(&backend, objs));
+
+ cl_git_pass(git_repository_odb__weakptr(&_odb, _repo));
+ cl_git_pass(git_odb_add_backend(_odb, backend, 10));
+}
+
+static void assert_object_contains(git_odb_object *obj, const char *expected)
+{
+ const char *actual = (const char *) git_odb_object_data(obj);
+
+ cl_assert_equal_s(actual, expected);
+}
+
+void test_odb_backend_simple__initialize(void)
+{
+ _repo = cl_git_sandbox_init("testrepo.git");
+ _odb = NULL;
+ _obj = NULL;
+}
+
+void test_odb_backend_simple__cleanup(void)
+{
+ git_odb_object_free(_obj);
+ cl_git_sandbox_cleanup();
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
+}
+
+void test_odb_backend_simple__read_of_object_succeeds(void)
+{
+ const fake_object objs[] = {
+ { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
+
+ assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
+{
+ const fake_object objs[] = {
+ { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
+ cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
+}
+
+void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
+{
+ const fake_object objs[] = {
+ { "1234567890123456789012345678901234567890", "nonmatching content" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid));
+}
+
+void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
+{
+ const fake_object objs[] = {
+ { "1234567890123456789012345678901234567890", "nonmatching content" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+ cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
+
+ assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__read_prefix_succeeds(void)
+{
+ const fake_object objs[] = {
+ { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632"));
+ cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
+
+ assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
+{
+ const fake_object objs[] = {
+ { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+ { NULL, NULL }
+ };
+ char *hash = "f6ea0495187600e8";
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstrn(&_oid, hash, strlen(hash)));
+ cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
+}
+
+void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
+{
+ const fake_object objs[] = {
+ { "1234567890111111111111111111111111111111", "first content" },
+ { "1234567890222222222222222222222222222222", "second content" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 7));
+}
+
+void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
+{
+ const fake_object objs[] = {
+ { "1234567890111111111111111111111111111111", "first content" },
+ { "1234567890111111111111111111111111111112", "second content" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+ cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 39));
+ cl_git_pass(git_odb_read_prefix(&_obj, _odb, &_oid, 40));
+ assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__exists_succeeds(void)
+{
+ const fake_object objs[] = {
+ { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_assert(git_odb_exists(_odb, &_oid));
+}
+
+void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
+{
+ const fake_object objs[] = {
+ { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
+ cl_assert(git_odb_exists(_odb, &_oid) == 0);
+}
+
+void test_odb_backend_simple__exists_prefix_succeeds(void)
+{
+ const fake_object objs[] = {
+ { "1234567890111111111111111111111111111111", "first content" },
+ { "1234567890222222222222222222222222222222", "second content" },
+ { NULL, NULL }
+ };
+ git_oid found;
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 12));
+ cl_assert(git_oid_equal(&found, &_oid));
+}
+
+void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
+{
+ const fake_object objs[] = {
+ { "1234567890111111111111111111111111111111", "first content" },
+ { "1234567890222222222222222222222222222222", "second content" },
+ { NULL, NULL }
+ };
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(NULL, _odb, &_oid, 7));
+}
+
+void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
+{
+ const fake_object objs[] = {
+ { "1234567890111111111111111111111111111111", "first content" },
+ { "1234567890111111111111111111111111111112", "second content" },
+ { NULL, NULL }
+ };
+ git_oid found;
+
+ setup_backend(objs);
+
+ cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+ cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &_oid, 39));
+ cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 40));
+ cl_assert(git_oid_equal(&found, &_oid));
+}
diff --git a/tests/odb/freshen.c b/tests/odb/freshen.c
index f41e4361e..9d3cf51dc 100644
--- a/tests/odb/freshen.c
+++ b/tests/odb/freshen.c
@@ -55,6 +55,31 @@ void test_odb_freshen__loose_blob(void)
cl_assert(before.st_mtime < after.st_mtime);
}
+#define UNIQUE_STR "doesnt exist in the odb yet\n"
+#define UNIQUE_BLOB_ID "78a87d0b8878c5953b9a63015ff4e22a3d898826"
+#define UNIQUE_BLOB_FN "78/a87d0b8878c5953b9a63015ff4e22a3d898826"
+
+void test_odb_freshen__readonly_object(void)
+{
+ git_oid expected_id, id;
+ struct stat before, after;
+
+ cl_git_pass(git_oid_fromstr(&expected_id, UNIQUE_BLOB_ID));
+
+ cl_git_pass(git_blob_create_frombuffer(&id, repo, UNIQUE_STR, CONST_STRLEN(UNIQUE_STR)));
+ cl_assert_equal_oid(&expected_id, &id);
+
+ set_time_wayback(&before, UNIQUE_BLOB_FN);
+ cl_assert((before.st_mode & S_IWUSR) == 0);
+
+ cl_git_pass(git_blob_create_frombuffer(&id, repo, UNIQUE_STR, CONST_STRLEN(UNIQUE_STR)));
+ cl_assert_equal_oid(&expected_id, &id);
+ cl_must_pass(p_lstat("testrepo.git/objects/" UNIQUE_BLOB_FN, &after));
+
+ cl_assert(before.st_atime < after.st_atime);
+ cl_assert(before.st_mtime < after.st_mtime);
+}
+
#define LOOSE_TREE_ID "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
#define LOOSE_TREE_FN "94/4c0f6e4dfa41595e6eb3ceecdb14f50fe18162"
diff --git a/tests/odb/loose.c b/tests/odb/loose.c
index dd686aa01..2e24d6723 100644
--- a/tests/odb/loose.c
+++ b/tests/odb/loose.c
@@ -63,7 +63,7 @@ void test_odb_loose__initialize(void)
void test_odb_loose__cleanup(void)
{
- cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, 0));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
cl_fixture_cleanup("test-objects");
}
@@ -181,7 +181,7 @@ void test_odb_loose__fsync_obeys_odb_option(void)
void test_odb_loose__fsync_obeys_global_setting(void)
{
- cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, 1));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
write_object_to_loose_odb(0);
cl_assert(p_fsync__cnt > 0);
}
diff --git a/tests/online/badssl.c b/tests/online/badssl.c
index aa4c24d9c..6524fcd8e 100644
--- a/tests/online/badssl.c
+++ b/tests/online/badssl.c
@@ -4,7 +4,7 @@
static git_repository *g_repo;
-#if defined(GIT_OPENSSL) || defined(GIT_WINHTTP) || defined(GIT_SECURE_TRANSPORT)
+#ifdef GIT_HTTPS
static bool g_has_ssl = true;
#else
static bool g_has_ssl = false;
diff --git a/tests/online/clone.c b/tests/online/clone.c
index 04fd22d45..5eda73f87 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -547,7 +547,7 @@ void test_online_clone__ssh_cert(void)
if (!_remote_ssh_fingerprint)
cl_skip();
- cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, "ssh://localhost/foo", "./foo", &g_options));
+ cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, _remote_url, "./foo", &g_options));
}
static char *read_key_file(const char *path)
diff --git a/tests/pack/indexer.c b/tests/pack/indexer.c
index 1e514b2d6..c73d3974e 100644
--- a/tests/pack/indexer.c
+++ b/tests/pack/indexer.c
@@ -85,7 +85,7 @@ void test_pack_indexer__fix_thin(void)
cl_assert_equal_i(stats.indexed_objects, 2);
cl_assert_equal_i(stats.local_objects, 1);
- git_oid_fromstr(&should_id, "11f0f69b334728fdd8bc86b80499f22f29d85b15");
+ git_oid_fromstr(&should_id, "fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13");
cl_assert_equal_oid(&should_id, git_indexer_hash(idx));
git_indexer_free(idx);
@@ -102,7 +102,7 @@ void test_pack_indexer__fix_thin(void)
int fd;
ssize_t read;
struct stat st;
- const char *name = "pack-11f0f69b334728fdd8bc86b80499f22f29d85b15.pack";
+ const char *name = "pack-fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13.pack";
fd = p_open(name, O_RDONLY);
cl_assert(fd != -1);
diff --git a/tests/pack/packbuilder.c b/tests/pack/packbuilder.c
index 1d7becef7..13ee07353 100644
--- a/tests/pack/packbuilder.c
+++ b/tests/pack/packbuilder.c
@@ -31,7 +31,7 @@ void test_pack_packbuilder__cleanup(void)
git_oid *o;
unsigned int i;
- cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, 0));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
if (_commits_is_initialized) {
_commits_is_initialized = 0;
@@ -116,7 +116,7 @@ void test_pack_packbuilder__create_pack(void)
* $ cd tests/resources/testrepo.git
* $ git rev-list --objects HEAD | \
* git pack-objects -q --no-reuse-delta --threads=1 pack
- * $ sha1sum git-80e61eb315239ef3c53033e37fee43b744d57122.pack
+ * $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack
* 5d410bdf97cf896f9007681b92868471d636954b
*
*/
@@ -145,7 +145,7 @@ void test_pack_packbuilder__get_hash(void)
git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL);
git_oid_fmt(hex, git_packbuilder_hash(_packbuilder));
- cl_assert_equal_s(hex, "80e61eb315239ef3c53033e37fee43b744d57122");
+ cl_assert_equal_s(hex, "7f5fa362c664d68ba7221259be1cbd187434b2f0");
}
static void test_write_pack_permission(mode_t given, mode_t expected)
@@ -169,10 +169,10 @@ static void test_write_pack_permission(mode_t given, mode_t expected)
mask = p_umask(0);
p_umask(mask);
- cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.idx", &statbuf));
+ cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf));
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
- cl_git_pass(p_stat("pack-80e61eb315239ef3c53033e37fee43b744d57122.pack", &statbuf));
+ cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf));
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask);
}
@@ -209,7 +209,7 @@ static int expected_fsyncs = 4;
void test_pack_packbuilder__fsync_global_setting(void)
{
- cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, 1));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
p_fsync__cnt = 0;
seed_packbuilder();
git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
diff --git a/tests/refs/create.c b/tests/refs/create.c
index 4ecc60565..469cddd1e 100644
--- a/tests/refs/create.c
+++ b/tests/refs/create.c
@@ -22,7 +22,7 @@ void test_refs_create__cleanup(void)
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 1));
- cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, 0));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
}
void test_refs_create__symbolic(void)
@@ -347,7 +347,7 @@ void test_refs_create__fsyncs_when_global_opt_set(void)
{
size_t create_count, compress_count;
- cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONOUS_OBJECT_CREATION, 1));
+ cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
count_fsyncs(&create_count, &compress_count);
cl_assert_equal_i(expected_fsyncs_create, create_count);
diff --git a/tests/refs/revparse.c b/tests/refs/revparse.c
index c22c30440..459188cf7 100644
--- a/tests/refs/revparse.c
+++ b/tests/refs/revparse.c
@@ -122,6 +122,14 @@ static void test_id(
test_id_inrepo(spec, expected_left, expected_right, expected_flags, g_repo);
}
+static void test_invalid_revspec(const char* invalid_spec)
+{
+ git_revspec revspec;
+
+ cl_assert_equal_i(
+ GIT_EINVALIDSPEC, git_revparse(&revspec, g_repo, invalid_spec));
+}
+
void test_refs_revparse__initialize(void)
{
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
@@ -749,6 +757,33 @@ void test_refs_revparse__parses_range_operator(void)
"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
GIT_REVPARSE_RANGE | GIT_REVPARSE_MERGE_BASE);
+
+ test_id("HEAD~3..",
+ "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
+ "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
+ GIT_REVPARSE_RANGE);
+
+ test_id("HEAD~3...",
+ "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
+ "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
+ GIT_REVPARSE_RANGE | GIT_REVPARSE_MERGE_BASE);
+
+ test_id("..HEAD~3",
+ "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
+ "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
+ GIT_REVPARSE_RANGE);
+
+ test_id("...HEAD~3",
+ "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
+ "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
+ GIT_REVPARSE_RANGE | GIT_REVPARSE_MERGE_BASE);
+
+ test_id("...",
+ "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
+ "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
+ GIT_REVPARSE_RANGE | GIT_REVPARSE_MERGE_BASE);
+
+ test_invalid_revspec("..");
}
void test_refs_revparse__ext_retrieves_both_the_reference_and_its_target(void)
diff --git a/tests/repo/discover.c b/tests/repo/discover.c
index abb7bd12b..eadd055e9 100644
--- a/tests/repo/discover.c
+++ b/tests/repo/discover.c
@@ -33,7 +33,7 @@ static void ensure_repository_discover(const char *start_path,
git_buf_attach(&resolved, p_realpath(expected_path, NULL), 0);
cl_assert(resolved.size > 0);
cl_git_pass(git_path_to_dir(&resolved));
- cl_git_pass(git_repository_discover(&found_path, start_path, 0, ceiling_dirs));
+ cl_git_pass(git_repository_discover(&found_path, start_path, 1, ceiling_dirs));
cl_assert_equal_s(found_path.ptr, resolved.ptr);
diff --git a/tests/repo/head.c b/tests/repo/head.c
index 31c228777..d02116087 100644
--- a/tests/repo/head.c
+++ b/tests/repo/head.c
@@ -261,15 +261,19 @@ void test_repo_head__setting_head_updates_reflog(void)
cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag)));
cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked"));
+ cl_git_pass(git_repository_set_head(repo, "refs/tags/test"));
+ cl_git_pass(git_repository_set_head(repo, "refs/remotes/test/master"));
- test_reflog(repo, 2, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from master to haacked");
- test_reflog(repo, 1, NULL, "tags/test^{commit}", "foo@example.com", "checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
- test_reflog(repo, 0, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
+ test_reflog(repo, 4, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from master to haacked");
+ test_reflog(repo, 3, NULL, "tags/test^{commit}", "foo@example.com", "checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
+ test_reflog(repo, 2, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
+ test_reflog(repo, 1, "refs/heads/haacked", "tags/test^{commit}", "foo@example.com", "checkout: moving from haacked to test");
+ test_reflog(repo, 0, "tags/test^{commit}", "refs/remotes/test/master", "foo@example.com", "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to test/master");
cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "haacked~0"));
cl_git_pass(git_repository_set_head_detached_from_annotated(repo, annotated));
- test_reflog(repo, 0, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from haacked to haacked~0");
+ test_reflog(repo, 0, NULL, "refs/heads/haacked", "foo@example.com", "checkout: moving from be3563ae3f795b2b4353bcce3a527ad0a4f7f644 to haacked~0");
git_annotated_commit_free(annotated);
git_object_free(tag);
diff --git a/tests/repo/init.c b/tests/repo/init.c
index 04d4a5c5e..6a455bfa6 100644
--- a/tests/repo/init.c
+++ b/tests/repo/init.c
@@ -320,6 +320,17 @@ void test_repo_init__sets_logAllRefUpdates_according_to_type_of_repository(void)
assert_config_entry_on_init_bytype("core.logallrefupdates", true, false);
}
+void test_repo_init__empty_template_path(void)
+{
+ git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
+ opts.template_path = "";
+
+ cl_git_pass(git_futils_mkdir("foo", 0755, 0));
+ cl_git_pass(git_repository_init_ext(&_repo, "foo", &opts));
+
+ cleanup_repository("foo");
+}
+
void test_repo_init__extended_0(void)
{
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
diff --git a/tests/repo/open.c b/tests/repo/open.c
index 6114ad2e1..3239b6fec 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -398,7 +398,8 @@ void test_repo_open__force_bare(void)
cl_git_fail(git_repository_open_bare(&barerepo, "alternate/subdir/sub2"));
cl_git_pass(git_repository_open_ext(
- &barerepo, "alternate/subdir/sub2", GIT_REPOSITORY_OPEN_BARE, NULL));
+ &barerepo, "alternate/subdir/sub2",
+ GIT_REPOSITORY_OPEN_BARE|GIT_REPOSITORY_OPEN_CROSS_FS, NULL));
cl_assert(git_repository_is_bare(barerepo));
git_repository_free(barerepo);
}
diff --git a/tests/resources/indexv4/.gitted/HEAD b/tests/resources/indexv4/.gitted/HEAD
new file mode 100644
index 000000000..cb089cd89
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/tests/resources/indexv4/.gitted/config b/tests/resources/indexv4/.gitted/config
new file mode 100644
index 000000000..515f48362
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/config
@@ -0,0 +1,5 @@
+[core]
+ repositoryformatversion = 0
+ filemode = true
+ bare = false
+ logallrefupdates = true
diff --git a/tests/resources/indexv4/.gitted/index b/tests/resources/indexv4/.gitted/index
new file mode 100644
index 000000000..e8fc61736
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/index
Binary files differ
diff --git a/tests/resources/indexv4/.gitted/objects/4c/9109b3e671d851eec87e0e72f6305b582e7e99 b/tests/resources/indexv4/.gitted/objects/4c/9109b3e671d851eec87e0e72f6305b582e7e99
new file mode 100644
index 000000000..cedd594b0
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/objects/4c/9109b3e671d851eec87e0e72f6305b582e7e99
Binary files differ
diff --git a/tests/resources/indexv4/.gitted/objects/b0/952dbb50bed5f01e03e31b296184cb183e54a7 b/tests/resources/indexv4/.gitted/objects/b0/952dbb50bed5f01e03e31b296184cb183e54a7
new file mode 100644
index 000000000..0ddc1d1a9
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/objects/b0/952dbb50bed5f01e03e31b296184cb183e54a7
Binary files differ
diff --git a/tests/resources/indexv4/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 b/tests/resources/indexv4/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
new file mode 100644
index 000000000..711223894
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
Binary files differ
diff --git a/tests/resources/indexv4/.gitted/refs/heads/master b/tests/resources/indexv4/.gitted/refs/heads/master
new file mode 100644
index 000000000..f3e960eb3
--- /dev/null
+++ b/tests/resources/indexv4/.gitted/refs/heads/master
@@ -0,0 +1 @@
+b0952dbb50bed5f01e03e31b296184cb183e54a7
diff --git a/tests/resources/indexv4/file.tx b/tests/resources/indexv4/file.tx
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/resources/indexv4/file.tx
diff --git a/tests/resources/indexv4/file.txt b/tests/resources/indexv4/file.txt
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/resources/indexv4/file.txt
diff --git a/tests/resources/indexv4/file.txz b/tests/resources/indexv4/file.txz
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/resources/indexv4/file.txz
diff --git a/tests/resources/indexv4/foo b/tests/resources/indexv4/foo
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/resources/indexv4/foo
diff --git a/tests/resources/indexv4/zzz b/tests/resources/indexv4/zzz
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/resources/indexv4/zzz
diff --git a/tests/status/ignore.c b/tests/status/ignore.c
index 251de39dd..23384fb1d 100644
--- a/tests/status/ignore.c
+++ b/tests/status/ignore.c
@@ -1077,3 +1077,81 @@ void test_status_ignore__negate_starstar(void)
cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "code/projects/foo/bar/packages/repositories.config"));
cl_assert_equal_i(0, ignored);
}
+
+void test_status_ignore__ignore_all_toplevel_dirs_include_files(void)
+{
+ static const char *test_files[] = {
+ "empty_standard_repo/README.md",
+ "empty_standard_repo/src/main.c",
+ "empty_standard_repo/src/foo/foo.c",
+ "empty_standard_repo/dist/foo.o",
+ "empty_standard_repo/dist/main.o",
+ NULL
+ };
+
+ make_test_data("empty_standard_repo", test_files);
+ cl_git_mkfile(
+ "empty_standard_repo/.gitignore",
+ "/*/\n"
+ "!/src\n");
+
+ assert_is_ignored("dist/foo.o");
+ assert_is_ignored("dist/main.o");
+
+ refute_is_ignored("README.md");
+ refute_is_ignored("src/foo.c");
+ refute_is_ignored("src/foo/foo.c");
+}
+
+void test_status_ignore__subdir_ignore_all_toplevel_dirs_include_files(void)
+{
+ static const char *test_files[] = {
+ "empty_standard_repo/project/README.md",
+ "empty_standard_repo/project/src/main.c",
+ "empty_standard_repo/project/src/foo/foo.c",
+ "empty_standard_repo/project/dist/foo.o",
+ "empty_standard_repo/project/dist/main.o",
+ NULL
+ };
+
+ make_test_data("empty_standard_repo", test_files);
+ cl_git_mkfile(
+ "empty_standard_repo/project/.gitignore",
+ "/*/\n"
+ "!/src\n");
+
+ assert_is_ignored("project/dist/foo.o");
+ assert_is_ignored("project/dist/main.o");
+
+ refute_is_ignored("project/src/foo.c");
+ refute_is_ignored("project/src/foo/foo.c");
+ refute_is_ignored("project/README.md");
+}
+
+void test_status_ignore__subdir_ignore_everything_except_certain_files(void)
+{
+ static const char *test_files[] = {
+ "empty_standard_repo/project/README.md",
+ "empty_standard_repo/project/some_file",
+ "empty_standard_repo/project/src/main.c",
+ "empty_standard_repo/project/src/foo/foo.c",
+ "empty_standard_repo/project/dist/foo.o",
+ "empty_standard_repo/project/dist/main.o",
+ NULL
+ };
+
+ make_test_data("empty_standard_repo", test_files);
+ cl_git_mkfile(
+ "empty_standard_repo/project/.gitignore",
+ "/*\n"
+ "!/src\n"
+ "!README.md\n");
+
+ assert_is_ignored("project/some_file");
+ assert_is_ignored("project/dist/foo.o");
+ assert_is_ignored("project/dist/main.o");
+
+ refute_is_ignored("project/README.md");
+ refute_is_ignored("project/src/foo.c");
+ refute_is_ignored("project/src/foo/foo.c");
+}
diff --git a/tests/submodule/open.c b/tests/submodule/open.c
new file mode 100644
index 000000000..0ef01ec24
--- /dev/null
+++ b/tests/submodule/open.c
@@ -0,0 +1,90 @@
+#include "clar_libgit2.h"
+#include "submodule_helpers.h"
+#include "path.h"
+
+static git_repository *g_parent;
+static git_repository *g_child;
+static git_submodule *g_module;
+
+void test_submodule_open__initialize(void)
+{
+ g_parent = setup_fixture_submod2();
+}
+
+void test_submodule_open__cleanup(void)
+{
+ git_submodule_free(g_module);
+ git_repository_free(g_child);
+ cl_git_sandbox_cleanup();
+ g_parent = NULL;
+ g_child = NULL;
+ g_module = NULL;
+}
+
+static void assert_sm_valid(git_repository *parent, git_repository *child, const char *sm_name)
+{
+ git_buf expected = GIT_BUF_INIT, actual = GIT_BUF_INIT;
+
+ /* assert working directory */
+ cl_git_pass(git_buf_joinpath(&expected, git_repository_workdir(parent), sm_name));
+ cl_git_pass(git_path_prettify_dir(&expected, expected.ptr, NULL));
+ cl_git_pass(git_buf_sets(&actual, git_repository_workdir(child)));
+ cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL));
+ cl_assert_equal_s(expected.ptr, actual.ptr);
+
+ git_buf_clear(&expected);
+ git_buf_clear(&actual);
+
+ /* assert common directory */
+ cl_git_pass(git_buf_joinpath(&expected, git_repository_commondir(parent), "modules"));
+ cl_git_pass(git_buf_joinpath(&expected, expected.ptr, sm_name));
+ cl_git_pass(git_path_prettify_dir(&expected, expected.ptr, NULL));
+ cl_git_pass(git_buf_sets(&actual, git_repository_commondir(child)));
+ cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL));
+ cl_assert_equal_s(expected.ptr, actual.ptr);
+
+ /* assert git directory */
+ cl_git_pass(git_buf_sets(&actual, git_repository_path(child)));
+ cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL));
+ cl_assert_equal_s(expected.ptr, actual.ptr);
+
+ git_buf_free(&expected);
+ git_buf_free(&actual);
+}
+
+void test_submodule_open__opening_via_lookup_succeeds(void)
+{
+ cl_git_pass(git_submodule_lookup(&g_module, g_parent, "sm_unchanged"));
+ cl_git_pass(git_submodule_open(&g_child, g_module));
+ assert_sm_valid(g_parent, g_child, "sm_unchanged");
+}
+
+void test_submodule_open__direct_open_succeeds(void)
+{
+ git_buf path = GIT_BUF_INIT;
+
+ cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_parent), "sm_unchanged"));
+ cl_git_pass(git_repository_open(&g_child, path.ptr));
+ assert_sm_valid(g_parent, g_child, "sm_unchanged");
+
+ git_buf_free(&path);
+}
+
+void test_submodule_open__direct_open_succeeds_for_broken_sm_with_gitdir(void)
+{
+ git_buf path = GIT_BUF_INIT;
+
+ /*
+ * This is actually not a valid submodule, but we
+ * encountered at least one occasion where the gitdir
+ * file existed inside of a submodule's gitdir. As we are
+ * now able to open these submodules correctly, we still
+ * add a test for this.
+ */
+ cl_git_mkfile("submod2/.git/modules/sm_unchanged/gitdir", ".git");
+ cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_parent), "sm_unchanged"));
+ cl_git_pass(git_repository_open(&g_child, path.ptr));
+ assert_sm_valid(g_parent, g_child, "sm_unchanged");
+
+ git_buf_free(&path);
+}
diff --git a/tests/threads/basic.c b/tests/threads/basic.c
index a9310bbd4..af6049090 100644
--- a/tests/threads/basic.c
+++ b/tests/threads/basic.c
@@ -54,12 +54,6 @@ static void *return_normally(void *param)
{
return param;
}
-
-static void *exit_abruptly(void *param)
-{
- git_thread_exit(param);
- return NULL;
-}
#endif
void test_threads_basic__exit(void)
diff --git a/tests/threads/diff.c b/tests/threads/diff.c
index c32811469..256040265 100644
--- a/tests/threads/diff.c
+++ b/tests/threads/diff.c
@@ -19,12 +19,27 @@ static git_repository *_repo;
static git_tree *_a, *_b;
static git_atomic _counts[4];
static int _check_counts;
+#ifdef GIT_WIN32
+static int _retries;
+#endif
#define THREADS 20
+void test_threads_diff__initialize(void)
+{
+#ifdef GIT_WIN32
+ _retries = git_win32__retries;
+ git_win32__retries = 1;
+#endif
+}
+
void test_threads_diff__cleanup(void)
{
cl_git_sandbox_cleanup();
+
+#ifdef GIT_WIN32
+ git_win32__retries = _retries;
+#endif
}
static void setup_trees(void)
diff --git a/tests/worktree/refs.c b/tests/worktree/refs.c
index b9a05606d..a10f50a2c 100644
--- a/tests/worktree/refs.c
+++ b/tests/worktree/refs.c
@@ -107,28 +107,45 @@ void test_worktree_refs__set_head_fails_when_already_checked_out(void)
void test_worktree_refs__delete_fails_for_checked_out_branch(void)
{
- git_reference *branch;
+ git_reference *branch;
- cl_git_pass(git_branch_lookup(&branch, fixture.repo,
- "testrepo-worktree", GIT_BRANCH_LOCAL));
- cl_git_fail(git_branch_delete(branch));
+ cl_git_pass(git_branch_lookup(&branch, fixture.repo,
+ "testrepo-worktree", GIT_BRANCH_LOCAL));
+ cl_git_fail(git_branch_delete(branch));
- git_reference_free(branch);
+ git_reference_free(branch);
}
void test_worktree_refs__delete_succeeds_after_pruning_worktree(void)
{
- git_reference *branch;
- git_worktree *worktree;
+ git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
+ git_reference *branch;
+ git_worktree *worktree;
- cl_git_pass(git_worktree_lookup(&worktree, fixture.repo, fixture.worktreename));
- cl_git_pass(git_worktree_prune(worktree, GIT_WORKTREE_PRUNE_VALID));
- git_worktree_free(worktree);
+ opts.flags = GIT_WORKTREE_PRUNE_VALID;
- cl_git_pass(git_branch_lookup(&branch, fixture.repo,
- "testrepo-worktree", GIT_BRANCH_LOCAL));
- cl_git_pass(git_branch_delete(branch));
- git_reference_free(branch);
+ cl_git_pass(git_worktree_lookup(&worktree, fixture.repo, fixture.worktreename));
+ cl_git_pass(git_worktree_prune(worktree, &opts));
+ git_worktree_free(worktree);
+
+ cl_git_pass(git_branch_lookup(&branch, fixture.repo,
+ "testrepo-worktree", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_branch_delete(branch));
+ git_reference_free(branch);
+}
+
+void test_worktree_refs__renaming_reference_updates_worktree_heads(void)
+{
+ git_reference *head, *branch, *renamed;
+
+ cl_git_pass(git_branch_lookup(&branch, fixture.repo,
+ "testrepo-worktree", GIT_BRANCH_LOCAL));
+ cl_git_pass(git_reference_rename(&renamed, branch, "refs/heads/renamed", 0, NULL));
+ cl_git_pass(git_repository_head(&head, fixture.worktree));
+
+ git_reference_free(head);
+ git_reference_free(branch);
+ git_reference_free(renamed);
}
void test_worktree_refs__creating_refs_uses_commondir(void)
diff --git a/tests/worktree/submodule.c b/tests/worktree/submodule.c
index 562077597..294385226 100644
--- a/tests/worktree/submodule.c
+++ b/tests/worktree/submodule.c
@@ -74,7 +74,7 @@ void test_worktree_submodule__resolve_relative_url(void)
cl_git_pass(git_repository_open(&child.repo, WORKTREE_CHILD));
/* Create worktree of submodule repository */
- cl_git_pass(git_worktree_add(&wt, child.repo, "subdir", wt_path.ptr));
+ cl_git_pass(git_worktree_add(&wt, child.repo, "subdir", wt_path.ptr, NULL));
cl_git_pass(git_repository_open_from_worktree(&repo, wt));
cl_git_pass(git_submodule_resolve_url(&sm_relative_path, repo,
diff --git a/tests/worktree/worktree.c b/tests/worktree/worktree.c
index 6e90e6ac0..4ac3b8bba 100644
--- a/tests/worktree/worktree.c
+++ b/tests/worktree/worktree.c
@@ -215,7 +215,7 @@ void test_worktree_worktree__init(void)
git_buf path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
- cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr));
+ cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
/* Open and verify created repo */
cl_git_pass(git_repository_open(&repo, path.ptr));
@@ -228,6 +228,31 @@ void test_worktree_worktree__init(void)
git_repository_free(repo);
}
+void test_worktree_worktree__add_locked(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+ git_reference *branch;
+ git_buf path = GIT_BUF_INIT;
+ git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT;
+
+ opts.lock = 1;
+
+ cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-locked"));
+ cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-locked", path.ptr, &opts));
+
+ /* Open and verify created repo */
+ cl_assert(git_worktree_is_locked(NULL, wt));
+ cl_git_pass(git_repository_open(&repo, path.ptr));
+ cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-locked/") == 0);
+ cl_git_pass(git_branch_lookup(&branch, repo, "worktree-locked", GIT_BRANCH_LOCAL));
+
+ git_buf_free(&path);
+ git_worktree_free(wt);
+ git_reference_free(branch);
+ git_repository_free(repo);
+}
+
void test_worktree_worktree__init_existing_branch(void)
{
git_reference *head, *branch;
@@ -240,7 +265,7 @@ void test_worktree_worktree__init_existing_branch(void)
cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-new", commit, false));
cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
- cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr));
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
git_buf_free(&path);
git_commit_free(commit);
@@ -254,7 +279,7 @@ void test_worktree_worktree__init_existing_worktree(void)
git_buf path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../worktree-new"));
- cl_git_fail(git_worktree_add(&wt, fixture.repo, "testrepo-worktree", path.ptr));
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "testrepo-worktree", path.ptr, NULL));
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink);
@@ -279,7 +304,7 @@ void test_worktree_worktree__init_existing_path(void)
}
cl_git_pass(git_buf_joinpath(&path, fixture.repo->workdir, "../testrepo-worktree"));
- cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr));
+ cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL));
/* Verify files have not been re-created */
for (i = 0; i < ARRAY_SIZE(wtfiles); i++) {
@@ -303,7 +328,7 @@ void test_worktree_worktree__init_submodule(void)
cl_git_pass(git_buf_joinpath(&path, repo->workdir, "sm_unchanged"));
cl_git_pass(git_repository_open(&sm, path.ptr));
cl_git_pass(git_buf_joinpath(&path, repo->workdir, "../worktree/"));
- cl_git_pass(git_worktree_add(&worktree, sm, "repo-worktree", path.ptr));
+ cl_git_pass(git_worktree_add(&worktree, sm, "repo-worktree", path.ptr, NULL));
cl_git_pass(git_repository_open_from_worktree(&wt, worktree));
cl_git_pass(git_path_prettify_dir(&path, path.ptr, NULL));
@@ -429,13 +454,31 @@ void test_worktree_worktree__unlock_locked_worktree(void)
git_worktree_free(wt);
}
+void test_worktree_worktree__prune_without_opts_fails(void)
+{
+ git_worktree *wt;
+ git_repository *repo;
+
+ cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
+ cl_git_fail(git_worktree_prune(wt, NULL));
+
+ /* Assert the repository is still valid */
+ cl_git_pass(git_repository_open_from_worktree(&repo, wt));
+
+ git_worktree_free(wt);
+ git_repository_free(repo);
+}
+
void test_worktree_worktree__prune_valid(void)
{
+ git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
git_worktree *wt;
git_repository *repo;
+ opts.flags = GIT_WORKTREE_PRUNE_VALID;
+
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
- cl_git_pass(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_VALID));
+ cl_git_pass(git_worktree_prune(wt, &opts));
/* Assert the repository is not valid anymore */
cl_git_fail(git_repository_open_from_worktree(&repo, wt));
@@ -446,27 +489,33 @@ void test_worktree_worktree__prune_valid(void)
void test_worktree_worktree__prune_locked(void)
{
+ git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
git_worktree *wt;
git_repository *repo;
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
cl_git_pass(git_worktree_lock(wt, NULL));
- cl_git_fail(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_VALID));
- cl_git_fail(git_worktree_prune(wt, ~GIT_WORKTREE_PRUNE_LOCKED));
+ opts.flags = GIT_WORKTREE_PRUNE_VALID;
+ cl_git_fail(git_worktree_prune(wt, &opts));
/* Assert the repository is still valid */
cl_git_pass(git_repository_open_from_worktree(&repo, wt));
+ opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_LOCKED;
+ cl_git_pass(git_worktree_prune(wt, &opts));
+
git_worktree_free(wt);
git_repository_free(repo);
}
-void test_worktree_worktree__prune_gitdir(void)
+void test_worktree_worktree__prune_gitdir_only(void)
{
+ git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
git_worktree *wt;
+ opts.flags = GIT_WORKTREE_PRUNE_VALID;
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
- cl_git_pass(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_VALID));
+ cl_git_pass(git_worktree_prune(wt, &opts));
cl_assert(!git_path_exists(wt->gitdir_path));
cl_assert(git_path_exists(wt->gitlink_path));
@@ -474,15 +523,61 @@ void test_worktree_worktree__prune_gitdir(void)
git_worktree_free(wt);
}
-void test_worktree_worktree__prune_both(void)
+void test_worktree_worktree__prune_worktree(void)
{
+ git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
git_worktree *wt;
+ opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_WORKING_TREE;
+
cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree"));
- cl_git_pass(git_worktree_prune(wt, GIT_WORKTREE_PRUNE_WORKING_TREE | GIT_WORKTREE_PRUNE_VALID));
+ cl_git_pass(git_worktree_prune(wt, &opts));
cl_assert(!git_path_exists(wt->gitdir_path));
cl_assert(!git_path_exists(wt->gitlink_path));
git_worktree_free(wt);
}
+
+static int read_head_ref(git_repository *repo, const char *path, void *payload)
+{
+ git_vector *refs = (git_vector *) payload;
+ git_reference *head;
+
+ GIT_UNUSED(repo);
+
+ cl_git_pass(git_reference__read_head(&head, repo, path));
+
+ git_vector_insert(refs, head);
+
+ return 0;
+}
+
+void test_worktree_worktree__foreach_head_gives_same_results_in_wt_and_repo(void)
+{
+ git_vector repo_refs = GIT_VECTOR_INIT, worktree_refs = GIT_VECTOR_INIT;
+ git_reference *heads[2];
+ size_t i;
+
+ cl_git_pass(git_reference_lookup(&heads[0], fixture.repo, GIT_HEAD_FILE));
+ cl_git_pass(git_reference_lookup(&heads[1], fixture.worktree, GIT_HEAD_FILE));
+
+ cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, &repo_refs));
+ cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, &worktree_refs));
+
+ cl_assert_equal_i(repo_refs.length, ARRAY_SIZE(heads));
+ cl_assert_equal_i(worktree_refs.length, ARRAY_SIZE(heads));
+
+ for (i = 0; i < ARRAY_SIZE(heads); i++) {
+ cl_assert_equal_s(heads[i]->name, ((git_reference *) repo_refs.contents[i])->name);
+ cl_assert_equal_s(heads[i]->name, ((git_reference *) repo_refs.contents[i])->name);
+ cl_assert_equal_s(heads[i]->name, ((git_reference *) worktree_refs.contents[i])->name);
+
+ git_reference_free(heads[i]);
+ git_reference_free(repo_refs.contents[i]);
+ git_reference_free(worktree_refs.contents[i]);
+ }
+
+ git_vector_free(&repo_refs);
+ git_vector_free(&worktree_refs);
+}