summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt6
-rw-r--r--CONTRIBUTING.md46
-rw-r--r--CONVENTIONS.md21
-rw-r--r--COPYING75
-rw-r--r--PROJECTS.md88
-rw-r--r--README.md33
-rw-r--r--examples/blame.c5
-rw-r--r--examples/diff.c50
-rw-r--r--examples/network/fetch.c2
-rw-r--r--git.git-authors5
-rw-r--r--include/git2/blame.h3
-rw-r--r--include/git2/commit.h45
-rw-r--r--include/git2/common.h7
-rw-r--r--include/git2/diff.h33
-rw-r--r--include/git2/errors.h1
-rw-r--r--include/git2/index.h12
-rw-r--r--include/git2/merge.h160
-rw-r--r--include/git2/oid.h5
-rw-r--r--include/git2/patch.h28
-rw-r--r--include/git2/push.h8
-rw-r--r--include/git2/refs.h122
-rw-r--r--include/git2/remote.h17
-rw-r--r--include/git2/repository.h6
-rw-r--r--include/git2/reset.h13
-rw-r--r--include/git2/revwalk.h18
-rw-r--r--include/git2/sys/commit.h40
-rw-r--r--include/git2/sys/refdb_backend.h5
-rwxr-xr-xscript/cibuild.sh2
-rw-r--r--src/array.h2
-rw-r--r--src/attr.c8
-rw-r--r--src/bitvec.h2
-rw-r--r--src/blame_git.c6
-rw-r--r--src/buffer.c1
-rw-r--r--src/buffer.h2
-rw-r--r--src/cc-compat.h2
-rw-r--r--src/clone.c2
-rw-r--r--src/commit.c288
-rw-r--r--src/commit_list.c6
-rw-r--r--src/commit_list.h2
-rw-r--r--src/common.h7
-rw-r--r--src/config.c10
-rw-r--r--src/config_file.c4
-rw-r--r--src/crlf.c29
-rw-r--r--src/diff_file.c49
-rw-r--r--src/diff_file.h20
-rw-r--r--src/diff_patch.c230
-rw-r--r--src/diff_tform.c4
-rw-r--r--src/errors.c1
-rw-r--r--src/filebuf.c2
-rw-r--r--src/fileops.c220
-rw-r--r--src/fileops.h88
-rw-r--r--src/global.c6
-rw-r--r--src/graph.c35
-rw-r--r--src/index.c47
-rw-r--r--src/index.h2
-rw-r--r--src/merge.c28
-rw-r--r--src/object.c2
-rw-r--r--src/oid.c5
-rw-r--r--src/path.c1
-rw-r--r--src/pathspec.c2
-rw-r--r--src/posix.c2
-rw-r--r--src/posix.h22
-rw-r--r--src/pqueue.c192
-rw-r--r--src/pqueue.h115
-rw-r--r--src/push.c9
-rw-r--r--src/refdb.c8
-rw-r--r--src/refdb.h4
-rw-r--r--src/refdb_fs.c131
-rw-r--r--src/refs.c91
-rw-r--r--src/remote.c37
-rw-r--r--src/repository.c24
-rw-r--r--src/reset.c12
-rw-r--r--src/revparse.c3
-rw-r--r--src/revwalk.c75
-rw-r--r--src/settings.c24
-rw-r--r--src/signature.c2
-rw-r--r--src/sortedcache.c2
-rw-r--r--src/strnlen.h23
-rw-r--r--src/sysdir.c244
-rw-r--r--src/sysdir.h101
-rw-r--r--src/trace.h2
-rw-r--r--src/tree.c6
-rw-r--r--src/util.h4
-rw-r--r--src/vector.c17
-rw-r--r--src/vector.h19
-rw-r--r--src/win32/utf-conv.c62
-rw-r--r--tests/blame/buffer.c4
-rw-r--r--tests/blame/simple.c15
-rw-r--r--tests/checkout/conflict.c7
-rw-r--r--tests/checkout/crlf.c119
-rw-r--r--tests/checkout/tree.c2
-rw-r--r--tests/clar_libgit2.c9
-rw-r--r--tests/clar_libgit2.h2
-rw-r--r--tests/commit/write.c15
-rw-r--r--tests/core/env.c33
-rw-r--r--tests/core/pqueue.c97
-rw-r--r--tests/diff/blob.c240
-rw-r--r--tests/filter/blob.c3
-rw-r--r--tests/index/addall.c2
-rw-r--r--tests/index/crlf.c136
-rw-r--r--tests/index/names.c8
-rw-r--r--tests/index/reuc.c14
-rw-r--r--tests/index/tests.c42
-rw-r--r--tests/merge/workdir/dirty.c322
-rw-r--r--tests/merge/workdir/renames.c7
-rw-r--r--tests/merge/workdir/simple.c11
-rw-r--r--tests/merge/workdir/submodules.c4
-rw-r--r--tests/network/fetchlocal.c4
-rw-r--r--tests/network/remote/local.c95
-rw-r--r--tests/object/commit/commitstagedfile.c76
-rw-r--r--tests/object/tree/write.c100
-rw-r--r--tests/online/clone.c14
-rw-r--r--tests/online/fetch.c4
-rw-r--r--tests/online/fetchhead.c2
-rw-r--r--tests/online/push.c20
-rw-r--r--tests/refs/branches/create.c9
-rw-r--r--tests/refs/branches/delete.c2
-rw-r--r--tests/refs/delete.c14
-rw-r--r--tests/refs/races.c152
-rw-r--r--tests/refs/read.c15
-rw-r--r--tests/repo/config.c9
-rw-r--r--tests/repo/head.c86
-rw-r--r--tests/repo/headtree.c2
-rw-r--r--tests/repo/iterator.c2
-rw-r--r--tests/repo/message.c28
-rw-r--r--tests/repo/open.c3
-rw-r--r--tests/repo/shallow.c6
-rw-r--r--tests/repo/state.c2
-rw-r--r--tests/reset/hard.c44
-rw-r--r--tests/reset/mixed.c34
-rw-r--r--tests/reset/reset_helpers.c18
-rw-r--r--tests/reset/reset_helpers.h3
-rw-r--r--tests/reset/soft.c61
-rw-r--r--tests/revert/workdir.c35
-rw-r--r--tests/revwalk/basic.c38
-rw-r--r--tests/stash/save.c2
136 files changed, 3529 insertions, 1643 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6f2a2bb82..d6b327503 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
# Add find modules to the path
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/")
+INCLUDE(CheckLibraryExists)
+
# Build options
#
OPTION( SONAME "Set the (SO)VERSION of the target" ON )
@@ -78,7 +80,9 @@ FUNCTION(TARGET_OS_LIBRARIES target)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
TARGET_LINK_LIBRARIES(${target} socket nsl)
SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lsocket -lnsl" PARENT_SCOPE)
- ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Linux")
+ ENDIF()
+ CHECK_LIBRARY_EXISTS(rt clock_gettime "time.h" NEED_LIBRT)
+ IF(NEED_LIBRT)
TARGET_LINK_LIBRARIES(${target} rt)
SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lrt" PARENT_SCOPE)
ENDIF()
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 807cd5320..4efe28ed3 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -5,9 +5,13 @@ your help.
## Licensing
-By contributing to libgit2, you agree to release your contribution under the terms of the license.
-For code under `examples`, this is governed by the [CC0 Public Domain Dedication](examples/COPYING).
-All other code is released under the [GPL v2 with linking exception](COPYING).
+By contributing to libgit2, you agree to release your contribution under
+the terms of the license. Except for the `examples` directory, all code
+is released under the [GPL v2 with linking exception](COPYING).
+
+The `examples` code is governed by the
+[CC0 Public Domain Dedication](examples/COPYING), so that you may copy
+from them into your own application.
## Discussion & Chat
@@ -76,15 +80,19 @@ you're porting code *from* to see what you need to do. As a general rule,
MIT and BSD (3-clause) licenses are typically no problem. Apache 2.0
license typically doesn't work due to GPL incompatibility.
-If you are pulling in code from core Git, another project or code you've pulled from
-a forum / Stack Overflow then please flag this in your PR and also make sure you've
-given proper credit to the original author in the code snippet.
+If you are pulling in code from core Git, another project or code you've
+pulled from a forum / Stack Overflow then please flag this in your PR and
+also make sure you've given proper credit to the original author in the
+code snippet.
## Style Guide
-`libgit2` is written in [ANSI C](http://en.wikipedia.org/wiki/ANSI_C)
-(a.k.a. C89) with some specific conventions for function and type naming,
-code formatting, and testing.
+The public API of `libgit2` is [ANSI C](http://en.wikipedia.org/wiki/ANSI_C)
+(a.k.a. C89) compatible. Internally, `libgit2` is written using a portable
+subset of C99 - in order to compile with GCC, Clang, MSVC, etc., we keep
+local variable declarations at the tops of blocks only and avoid `//` style
+comments. Additionally, `libgit2` follows some extra conventions for
+function and type naming, code formatting, and testing.
We like to keep the source code consistent and easy to read. Maintaining
this takes some discipline, but it's been more than worth it. Take a look
@@ -93,22 +101,4 @@ at the
## Starter Projects
-So, you want to start helping out with `libgit2`? That's fantastic? We
-welcome contributions and we promise we'll try to be nice.
-
-If you want to jump in, you can look at our issues list to see if there
-are any unresolved issues to jump in on. Also, here is a list of some
-smaller project ideas that could help you become familiar with the code
-base and make a nice first step:
-
-* Convert a `git_*modulename*_foreach()` callback-based iteration API
- into a `git_*modulename*_iterator` object with a create/advance style
- of API. This helps folks writing language bindings and usually isn't
- too complicated.
-* Write a new `examples/` program that mirrors a particular core git
- command. (See `examples/diff.c` for example.) This lets you (and us)
- easily exercise a particular facet of the API and measure compatability
- and feature parity with core git.
-* Submit a PR to clarify documentation! While we do try to document all of
- the APIs, your fresh eyes on the documentation will find areas that are
- confusing much more easily.
+See our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).
diff --git a/CONVENTIONS.md b/CONVENTIONS.md
index 59b41a2e6..5b8238a78 100644
--- a/CONVENTIONS.md
+++ b/CONVENTIONS.md
@@ -6,14 +6,18 @@ guidelines that should help with that.
## Compatibility
`libgit2` runs on many different platforms with many different compilers.
-It is written in [ANSI C](http://en.wikipedia.org/wiki/ANSI_C) (a.k.a. C89)
-with some specific standards for function and type naming, code formatting,
-and testing.
-We try to avoid more recent extensions to maximize portability. We also, to
-the greatest extent possible, try to avoid lots of `#ifdef`s inside the core
-code base. This is somewhat unavoidable, but since it can really hamper
-maintainability, we keep it to a minimum.
+The public API of `libgit2` is [ANSI C](http://en.wikipedia.org/wiki/ANSI_C)
+(a.k.a. C89) compatible.
+
+Internally, `libgit2` is written using a portable subset of C99 - in order
+to maximize compatibility (e.g. with MSVC) we avoid certain C99
+extensions. Specifically, we keep local variable declarations at the tops
+of blocks only and we avoid `//` style comments.
+
+Also, to the greatest extent possible, we try to avoid lots of `#ifdef`s
+inside the core code base. This is somewhat unavoidable, but since it can
+really hamper maintainability, we keep it to a minimum.
## Match Surrounding Code
@@ -209,6 +213,9 @@ All inlined functions must be declared as:
GIT_INLINE(result_type) git_modulename_functionname(arg_list);
```
+`GIT_INLINE` (or `inline`) should not be used in public headers in order
+to preserve ANSI C compatibility.
+
## Tests
`libgit2` uses the [clar](https://github.com/vmg/clar) testing framework.
diff --git a/COPYING b/COPYING
index f7e9f3af7..181737284 100644
--- a/COPYING
+++ b/COPYING
@@ -388,19 +388,7 @@ Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
----------------------------------------------------------------------
-The priority queue implementation is based on code licensed under the
-Apache 2.0 license:
-
- Copyright 2010 Volkan Yazıcı <volkan.yazici@gmail.com>
- Copyright 2006-2010 The Apache Software Foundation
-
-The full text of the Apache 2.0 license is available at:
-
- http://www.apache.org/licenses/LICENSE-2.0
-
-----------------------------------------------------------------------
-
-The Clay framework is licensed under the MIT license:
+The Clar framework is licensed under the MIT license:
Copyright (C) 2011 by Vicent Marti
@@ -930,64 +918,3 @@ necessary. Here is a sample; alter the names:
That's all there is to it!
----------------------------------------------------------------------
-
-Portions of src/win32/posix_w32.c are derrived from link_win32.c in PHP:
-
---------------------------------------------------------------------
- The PHP License, version 3.01
-Copyright (c) 1999 - 2012 The PHP Group. All rights reserved.
---------------------------------------------------------------------
-
-Redistribution and use in source and binary forms, with or without
-modification, is permitted provided that the following conditions
-are met:
-
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
-
- 3. The name "PHP" must not be used to endorse or promote products
- derived from this software without prior written permission. For
- written permission, please contact group@php.net.
-
- 4. Products derived from this software may not be called "PHP", nor
- may "PHP" appear in their name, without prior written permission
- from group@php.net. You may indicate that your software works in
- conjunction with PHP by saying "Foo for PHP" instead of calling
- it "PHP Foo" or "phpfoo"
-
- 5. The PHP Group may publish revised and/or new versions of the
- license from time to time. Each version will be given a
- distinguishing version number.
- Once covered code has been published under a particular version
- of the license, you may always continue to use it under the terms
- of that version. You may also choose to use such covered code
- under the terms of any subsequent version of the license
- published by the PHP Group. No one other than the PHP Group has
- the right to modify the terms applicable to covered code created
- under this License.
-
- 6. Redistributions of any form whatsoever must retain the following
- acknowledgment:
- "This product includes PHP software, freely available from
- <http://www.php.net/software/>".
-
-THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND
-ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
-PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP
-DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
-(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
-STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
-OF THE POSSIBILITY OF SUCH DAMAGE.
-
---------------------------------------------------------------------
-
diff --git a/PROJECTS.md b/PROJECTS.md
new file mode 100644
index 000000000..34ba18ace
--- /dev/null
+++ b/PROJECTS.md
@@ -0,0 +1,88 @@
+Projects For LibGit2
+====================
+
+So, you want to start helping out with `libgit2`? That's fantastic! We
+welcome contributions and we promise we'll try to be nice.
+
+This is a list of libgit2 related projects that new contributors can take
+on. It includes a number of good starter projects and well as some larger
+ideas that no one is actively working on.
+
+## Before You Start
+
+Please start by reading the README.md, CONTRIBUTING.md, and CONVENTIONS.md
+files before diving into one of these projects. Those will explain our
+work flow and coding conventions to help ensure that your work will be
+easily integrated into libgit2.
+
+Next, work through the build instructions and make sure you can clone the
+repository, compile it, and run the tests successfully. That will make
+sure that your development environment is set up correctly and you are
+ready to start on libgit2 development.
+
+## Starter Projects
+
+These are good small projects to get started with libgit2.
+
+* Look at the `examples/` programs, find an existing one that mirrors a
+ core Git command and add a missing command-line option. There are many
+ gaps right now and this helps demonstrate how to use the library. Here
+ are some specific ideas:
+ * Add the `--minimal` flag to `examples/diff.c` since the `libgit2`
+ diff API now has a flag to support it
+ * Add the `--patience` flag to `examples/diff.c` since it is also now
+ supported.
+ * Add the `--shortstat` flag to `examples/diff.c` based on the work
+ that was done to add `--numstat` already.
+ * Fix the `examples/diff.c` implementation of the `-B`
+ (a.k.a. `--break-rewrites`) command line option to actually look for
+ the optional `[<n>][/<m>]` configuration values. There is an
+ existing comment that reads `/* TODO: parse thresholds */`. The
+ trick to this one will be doing it in a manner that is clean and
+ simple, but still handles the various cases correctly (e.g. `-B/70%`
+ is apparently a legal setting).
+ * Implement the `--log-size` option for `examples/log.c`. I think all
+ the data is available, you would just need to add the code into the
+ `print_commit()` routine (along with a way of passing the option
+ into that function).
+ * For `examples/log.c`, implement any one of `--author=<...>`,
+ `--committer=<...>`, or `--grep=<...>` but just use simple string
+ match with `strstr()` instead of full regular expression
+ matching. (I.e. I'm suggesting implementing this as if
+ `--fixed-strings` was always turned on, because it will be a simpler
+ project.)
+ * As an extension to the matching idea for `examples/log.c`, add the
+ `-i` option to use `strcasestr()` for matches.
+ * For `examples/log.c`, implement the `--first-parent` option now that
+ libgit2 supports it in the revwalk API.
+* Pick a Git command that is not already emulated in `examples/` and write
+ a new example that mirrors the behavior. Examples don't have to be
+ perfect emulations, but should demonstrate how to use the libgit2 APIs
+ to get results that are similar to Git commands. This lets you (and us)
+ easily exercise a particular facet of the API and measure compatability
+ and feature parity with core git.
+* Submit a PR to clarify documentation! While we do try to document all of
+ the APIs, your fresh eyes on the documentation will find areas that are
+ confusing much more easily.
+
+If none of these appeal to you, take a look at our issues list to see if
+there are any unresolved issues you'd like to jump in on.
+
+## Larger Projects
+
+These are ideas for larger projects mostly taken from our backlog of
+[Issues](https://github.com/libgit2/libgit2/issues). Please don't dive
+into one of these as a first project for libgit2 - we'd rather get to
+know you first by successfully shipping your work on one of the smaller
+projects above.
+
+* Port part of the Git test suite to run against the command line emulation
+ in examples/
+* Fix symlink support for files in the .git directory (i.e. don't overwrite
+ the symlinks when writing the file contents back out)
+* Implement a 'git describe' like API
+* Add hooks API to enumerate and manage hooks (not run them at this point)
+* Isolate logic of ignore evaluation into a standalone API
+* Upgrade internal libxdiff code to latest from core Git
+* Add a hashtable lookup for files in the index instead of binary search
+ every time
diff --git a/README.md b/README.md
index f814b8732..8dd073430 100644
--- a/README.md
+++ b/README.md
@@ -4,9 +4,9 @@ libgit2 - the Git linkable library
[![Build Status](https://secure.travis-ci.org/libgit2/libgit2.png?branch=development)](http://travis-ci.org/libgit2/libgit2)
[![Coverity Scan Build Status](https://scan.coverity.com/projects/639/badge.svg)](https://scan.coverity.com/projects/639)
-`libgit2` is a portable, pure C implementation of the Git core methods provided as a
-re-entrant linkable library with a solid API, allowing you to write native
-speed custom Git applications in any language with bindings.
+`libgit2` is a portable, pure C implementation of the Git core methods
+provided as a re-entrant linkable library with a solid API, allowing you to
+write native speed custom Git applications in any language with bindings.
`libgit2` is licensed under a **very permissive license** (GPLv2 with a special
Linking Exception). This basically means that you can link it (unmodified)
@@ -20,18 +20,19 @@ Additionally, the example code has been released to the public domain (see the
* API documentation: <http://libgit2.github.com/libgit2>
* IRC: [#libgit2](irc://irc.freenode.net/libgit2) on irc.freenode.net.
* Mailing list: The libgit2 mailing list was
- traditionally hosted in Librelist but has been deprecated. We encourage you to
+ traditionally hosted in Librelist but has been deprecated. We encourage you to
[use StackOverflow](http://stackoverflow.com/questions/tagged/libgit2) instead for any questions regarding
- the library, or [open an issue](https://github.com/libgit2/libgit2/issues)
- on GitHub for bug reports. The mailing list archives are still available at
+ the library, or [open an issue](https://github.com/libgit2/libgit2/issues)
+ on GitHub for bug reports. The mailing list archives are still available at
<http://librelist.com/browser/libgit2/>.
What It Can Do
==============
-`libgit2` is already very usable and is being used in production for many applications including the GitHub.com site, in Plastic SCM
-and also powering Microsoft's Visual Studio tools for Git. The library provides:
+`libgit2` is already very usable and is being used in production for many
+applications including the GitHub.com site, in Plastic SCM and also powering
+Microsoft's Visual Studio tools for Git. The library provides:
* SHA conversions, formatting and shortening
* abstracted ODB backend system
@@ -65,7 +66,7 @@ Under Unix-like systems, like Linux, \*BSD and Mac OS X, libgit2 expects `pthrea
they should be installed by default on all systems. Under Windows, libgit2 uses the native Windows API
for threading.
-The `libgit2` library is built using `CMake 2.6+` (<http://www.cmake.org>) on all platforms.
+The `libgit2` library is built using [CMake](<http://www.cmake.org>) (version 2.6 or newer) on all platforms.
On most systems you can build the library using the following commands
@@ -199,14 +200,16 @@ we can add it to the list.
How Can I Contribute?
==================================
-Check the [contribution guidelines](CONTRIBUTING.md).
-
+Check the [contribution guidelines](CONTRIBUTING.md) to understand our
+workflow, the libgit2 [coding conventions](CONVENTIONS.md), and out list of
+[good starting projects](PROJECTS.md).
License
==================================
-`libgit2` is under GPL2 **with linking exemption**. This means you
-can link to and use the library from any program, proprietary or open source; paid
-or gratis. However, you cannot modify libgit2 and distribute it without
+
+`libgit2` is under GPL2 **with linking exemption**. This means you can link to
+and use the library from any program, proprietary or open source; paid or
+gratis. However, you cannot modify libgit2 and distribute it without
supplying the source.
-See the COPYING file for the full license text.
+See the [COPYING file](COPYING) for the full license text.
diff --git a/examples/blame.c b/examples/blame.c
index 1f5db69a1..6bc0581ac 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -31,6 +31,7 @@ struct opts {
int M;
int start_line;
int end_line;
+ int F;
};
static void parse_opts(struct opts *o, int argc, char *argv[]);
@@ -52,6 +53,7 @@ int main(int argc, char *argv[])
parse_opts(&o, argc, argv);
if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
+ if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT;
/** Open the repository. */
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
@@ -146,6 +148,7 @@ static void usage(const char *msg, const char *arg)
fprintf(stderr, " -L <n,m> process only line range n-m, counting from 1\n");
fprintf(stderr, " -M find line moves within and across files\n");
fprintf(stderr, " -C find line copies within and across files\n");
+ fprintf(stderr, " -F follow only the first parent commits\n");
fprintf(stderr, "\n");
exit(1);
}
@@ -174,6 +177,8 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
o->M = 1;
else if (!strcasecmp(a, "-C"))
o->C = 1;
+ else if (!strcasecmp(a, "-F"))
+ o->F = 1;
else if (!strcasecmp(a, "-L")) {
i++; a = argv[i];
if (i >= argc) fatal("Not enough arguments to -L", NULL);
diff --git a/examples/diff.c b/examples/diff.c
index daf5d7030..de994ecab 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -39,6 +39,7 @@ struct opts {
git_diff_find_options findopts;
int color;
int cached;
+ int numstat;
git_diff_format_t format;
const char *treeish1;
const char *treeish2;
@@ -49,6 +50,7 @@ struct opts {
static void parse_opts(struct opts *o, int argc, char *argv[]);
static int color_printer(
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
+static void diff_print_numstat(git_diff *diff);
int main(int argc, char *argv[])
{
@@ -57,7 +59,7 @@ int main(int argc, char *argv[])
git_diff *diff;
struct opts o = {
GIT_DIFF_OPTIONS_INIT, GIT_DIFF_FIND_OPTIONS_INIT,
- -1, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
+ -1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
};
git_threads_init();
@@ -117,15 +119,19 @@ int main(int argc, char *argv[])
/** Generate simple output using libgit2 display helper. */
- if (o.color >= 0)
- fputs(colors[0], stdout);
+ if (o.numstat == 1)
+ diff_print_numstat(diff);
+ else {
+ if (o.color >= 0)
+ fputs(colors[0], stdout);
- check_lg2(
- git_diff_print(diff, o.format, color_printer, &o.color),
- "displaying diff", NULL);
+ check_lg2(
+ git_diff_print(diff, o.format, color_printer, &o.color),
+ "displaying diff", NULL);
- if (o.color >= 0)
- fputs(colors[0], stdout);
+ if (o.color >= 0)
+ fputs(colors[0], stdout);
+ }
/** Cleanup before exiting. */
@@ -228,6 +234,8 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
o->diffopts.flags |= GIT_DIFF_INCLUDE_IGNORED;
else if (!strcmp(a, "--untracked"))
o->diffopts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
+ else if (!strcmp(a, "--numstat"))
+ o->numstat = 1;
else if (match_uint16_arg(
&o->findopts.rename_threshold, &args, "-M") ||
match_uint16_arg(
@@ -255,3 +263,29 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
usage("Unknown command line argument", a);
}
}
+
+/** Display diff output with "--numstat".*/
+static void diff_print_numstat(git_diff *diff)
+{
+ git_patch *patch;
+ const git_diff_delta *delta;
+ size_t d, ndeltas = git_diff_num_deltas(diff);
+ size_t nadditions, ndeletions;
+
+ for (d = 0; d < ndeltas; d++){
+ check_lg2(
+ git_patch_from_diff(&patch, diff, d),
+ "generating patch from diff", NULL);
+
+ check_lg2(
+ git_patch_line_stats(NULL, &nadditions, &ndeletions, patch),
+ "generating the number of additions and deletions", NULL);
+
+ delta = git_patch_get_delta(patch);
+
+ printf("%ld\t%ld\t%s\n",
+ (long)nadditions, (long)ndeletions, delta->new_file.path);
+
+ git_patch_free(patch);
+ }
+}
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index 474b45bbb..ad16f2793 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -156,7 +156,7 @@ int fetch(git_repository *repo, int argc, char **argv)
// right commits. This may be needed even if there was no packfile
// to download, which can happen e.g. when the branches have been
// changed but all the neede objects are available locally.
- if (git_remote_update_tips(remote) < 0)
+ if (git_remote_update_tips(remote, NULL, NULL) < 0)
return -1;
git_remote_free(remote);
diff --git a/git.git-authors b/git.git-authors
index 7c72c4bf6..9131a1fa1 100644
--- a/git.git-authors
+++ b/git.git-authors
@@ -28,9 +28,6 @@
# his/her consent on a patch-by-patch basis.
# "???" means the person is a prominent contributor who has
# not yet made his/her standpoint clear.
-# "ign" means the authors consent is ignored for the purpose
-# of libification. This is because the author has contributed
-# to areas that aren't interesting for the library.
#
# Please try to keep the list alphabetically ordered. It will
# help in case we get all 600-ish git.git authors on it.
@@ -61,7 +58,6 @@ ok Linus Torvalds <torvalds@linux-foundation.org>
ok Lukas Sandström <lukass@etek.chalmers.se>
ok Matthieu Moy <Matthieu.Moy@imag.fr>
ok Michael Haggerty <mhagger@alum.mit.edu>
-ign Mike McCormack <mike@codeweavers.com> (imap-send)
ok Nicolas Pitre <nico@fluxnic.net> <nico@cam.org>
ok Paolo Bonzini <bonzini@gnu.org>
ok Paul Kocher <paul@cryptography.com>
@@ -70,7 +66,6 @@ ok Petr Onderka <gsvick@gmail.com>
ok Pierre Habouzit <madcoder@debian.org>
ok Pieter de Bie <pdebie@ai.rug.nl>
ok René Scharfe <rene.scharfe@lsrfire.ath.cx>
-ign Robert Shearman <rob@codeweavers.com> (imap-send)
ok Sebastian Schuberth <sschuberth@gmail.com>
ok Shawn O. Pearce <spearce@spearce.org>
ok Steffen Prohaska <prohaska@zib.de>
diff --git a/include/git2/blame.h b/include/git2/blame.h
index b98c6f0d7..4ad51ee50 100644
--- a/include/git2/blame.h
+++ b/include/git2/blame.h
@@ -40,6 +40,9 @@ typedef enum {
* commit (like `git blame -CCC`). Implies SAME_COMMIT_COPIES.
* NOT IMPLEMENTED. */
GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<3),
+ /** Restrict the search of commits to those reachable following only the
+ * first parents. */
+ GIT_BLAME_FIRST_PARENT = (1<<4),
} git_blame_flag_t;
/**
diff --git a/include/git2/commit.h b/include/git2/commit.h
index 8b03df0a9..834330b5d 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -242,8 +242,8 @@ GIT_EXTERN(int) git_commit_nth_gen_ancestor(
/**
* Create new commit in the repository from a list of `git_object` pointers
*
- * The message will not be cleaned up automatically. You can do that with
- * the `git_message_prettify()` function.
+ * The message will **not** be cleaned up automatically. You can do that
+ * with the `git_message_prettify()` function.
*
* @param id Pointer in which to store the OID of the newly created commit
*
@@ -291,20 +291,20 @@ GIT_EXTERN(int) git_commit_create(
const char *message_encoding,
const char *message,
const git_tree *tree,
- int parent_count,
+ size_t parent_count,
const git_commit *parents[]);
/**
* Create new commit in the repository using a variable argument list.
*
- * The message will be cleaned up from excess whitespace and it will be made
- * sure that the last line ends with a '\n'.
+ * The message will **not** be cleaned up automatically. You can do that
+ * with the `git_message_prettify()` function.
*
* The parents for the commit are specified as a variable list of pointers
* to `const git_commit *`. Note that this is a convenience method which may
* not be safe to export for certain languages or compilers
*
- * All other parameters remain the same at `git_commit_create()`.
+ * All other parameters remain the same as `git_commit_create()`.
*
* @see git_commit_create
*/
@@ -317,9 +317,40 @@ GIT_EXTERN(int) git_commit_create_v(
const char *message_encoding,
const char *message,
const git_tree *tree,
- int parent_count,
+ size_t parent_count,
...);
+/**
+ * Amend an existing commit by replacing only non-NULL values.
+ *
+ * This creates a new commit that is exactly the same as the old commit,
+ * except that any non-NULL values will be updated. The new commit has
+ * the same parents as the old commit.
+ *
+ * The `update_ref` value works as in the regular `git_commit_create()`,
+ * updating the ref to point to the newly rewritten commit. If you want
+ * to amend a commit that is not currently the HEAD of the branch and then
+ * rewrite the following commits to reach a ref, pass this as NULL and
+ * update the rest of the commit chain and ref separately.
+ *
+ * Unlike `git_commit_create()`, the `author`, `committer`, `message`,
+ * `message_encoding`, and `tree` parameters can be NULL in which case this
+ * will use the values from the original `commit_to_amend`.
+ *
+ * All parameters have the same meanings as in `git_commit_create()`.
+ *
+ * @see git_commit_create
+ */
+GIT_EXTERN(int) git_commit_amend(
+ git_oid *id,
+ const git_commit *commit_to_amend,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/include/git2/common.h b/include/git2/common.h
index dca0c9c21..492715447 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -37,13 +37,6 @@
# define GIT_EXTERN(type) extern type
#endif
-/** Declare a function as always inlined. */
-#if defined(_MSC_VER)
-# define GIT_INLINE(type) static __inline type
-#else
-# define GIT_INLINE(type) static inline type
-#endif
-
/** Declare a function's takes printf style arguments. */
#ifdef __GNUC__
# define GIT_FORMAT_PRINTF(a,b) __attribute__((format (printf, a, b)))
diff --git a/include/git2/diff.h b/include/git2/diff.h
index a3cdd8193..943e2ec4c 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -1013,6 +1013,39 @@ GIT_EXTERN(int) git_diff_blob_to_buffer(
git_diff_line_cb line_cb,
void *payload);
+/**
+ * Directly run a diff between two buffers.
+ *
+ * Even more than with `git_diff_blobs`, comparing two buffer lacks
+ * context, so the `git_diff_file` parameters to the callbacks will be
+ * faked a la the rules for `git_diff_blobs()`.
+ *
+ * @param old_buffer Raw data for old side of diff, or NULL for empty
+ * @param old_len Length of the raw data for old side of the diff
+ * @param old_as_path Treat old buffer as if it had this filename; can be NULL
+ * @param new_buffer Raw data for new side of diff, or NULL for empty
+ * @param new_len Length of raw data for new side of diff
+ * @param new_as_path Treat buffer as if it had this filename; can be NULL
+ * @param options Options for diff, or NULL for default options
+ * @param file_cb Callback for "file"; made once if there is a diff; can be NULL
+ * @param hunk_cb Callback for each hunk in diff; can be NULL
+ * @param line_cb Callback for each line in diff; can be NULL
+ * @param payload Payload passed to each callback function
+ * @return 0 on success, non-zero callback return value, or error code
+ */
+GIT_EXTERN(int) git_diff_buffers(
+ const void *old_buffer,
+ size_t old_len,
+ const char *old_as_path,
+ const void *new_buffer,
+ size_t new_len,
+ const char *new_as_path,
+ const git_diff_options *options,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_line_cb line_cb,
+ void *payload);
+
GIT_END_DECL
diff --git a/include/git2/errors.h b/include/git2/errors.h
index 973d56003..bcf2f80ab 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -40,6 +40,7 @@ typedef enum {
GIT_EINVALIDSPEC = -12, /*< Name/ref spec was not in a valid format */
GIT_EMERGECONFLICT = -13, /*< Merge conflicts prevented operation */
GIT_ELOCKED = -14, /*< Lock file prevented operation */
+ GIT_EMODIFIED = -15, /*< Reference value does not match expected */
GIT_PASSTHROUGH = -30, /*< Internal only */
GIT_ITEROVER = -31, /*< Signals end of iteration with iterator */
diff --git a/include/git2/index.h b/include/git2/index.h
index 4363a3b9b..ae919e133 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -120,10 +120,10 @@ typedef struct git_index_entry {
/** Capabilities of system that affect index actions. */
typedef enum {
- GIT_INDEXCAP_IGNORE_CASE = 1u,
- GIT_INDEXCAP_NO_FILEMODE = 2u,
- GIT_INDEXCAP_NO_SYMLINKS = 4u,
- GIT_INDEXCAP_FROM_OWNER = ~0u
+ GIT_INDEXCAP_IGNORE_CASE = 1,
+ GIT_INDEXCAP_NO_FILEMODE = 2,
+ GIT_INDEXCAP_NO_SYMLINKS = 4,
+ GIT_INDEXCAP_FROM_OWNER = -1,
} git_indexcap_t;
/** Callback for APIs that add/remove/update files matching pathspec */
@@ -206,7 +206,7 @@ GIT_EXTERN(git_repository *) git_index_owner(const git_index *index);
* @param index An existing index object
* @return A combination of GIT_INDEXCAP values
*/
-GIT_EXTERN(unsigned int) git_index_caps(const git_index *index);
+GIT_EXTERN(int) git_index_caps(const git_index *index);
/**
* Set index capabilities flags.
@@ -219,7 +219,7 @@ GIT_EXTERN(unsigned int) git_index_caps(const git_index *index);
* @param caps A combination of GIT_INDEXCAP values
* @return 0 on success, -1 on failure
*/
-GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps);
+GIT_EXTERN(int) git_index_set_caps(git_index *index, int caps);
/**
* Update the contents of an existing index object in memory by reading
diff --git a/include/git2/merge.h b/include/git2/merge.h
index 3ef27e3c7..b45d0fd5e 100644
--- a/include/git2/merge.h
+++ b/include/git2/merge.h
@@ -27,24 +27,47 @@ GIT_BEGIN_DECL
* passed in via the `flags` value in the `git_merge_tree_opts`.
*/
typedef enum {
- /** Detect renames */
+ /**
+ * Detect renames that occur between the common ancestor and the "ours"
+ * side or the common ancestor and the "theirs" side. This will enable
+ * the ability to merge between a modified and renamed file.
+ */
GIT_MERGE_TREE_FIND_RENAMES = (1 << 0),
} git_merge_tree_flag_t;
/**
- * Merge file options for `git_merge_trees_opts`.
+ * Merge file favor options for `git_merge_trees_opts` instruct the file-level
+ * merging functionality how to deal with conflicting regions of the files.
*/
typedef enum {
- /* Produce a conflict in a file when two similar regions are changed. */
+ /**
+ * When a region of a file is changed in both branches, a conflict
+ * will be recorded in the index so that `git_checkout` can produce
+ * a merge file with conflict markers in the working directory.
+ * This is the default.
+ */
GIT_MERGE_FILE_FAVOR_NORMAL = 0,
- /* Produce a file containing the "ours" side of conflicting regions. */
+ /**
+ * When a region of a file is changed in both branches, the file
+ * created in the index will contain the "ours" side of any conflicting
+ * region. The index will not record a conflict.
+ */
GIT_MERGE_FILE_FAVOR_OURS = 1,
- /* Produce a file containing the "theirs" side of conflicting regions. */
+ /**
+ * When a region of a file is changed in both branches, the file
+ * created in the index will contain the "theirs" side of any conflicting
+ * region. The index will not record a conflict.
+ */
GIT_MERGE_FILE_FAVOR_THEIRS = 2,
- /* Produce a file blending the sides in a union of conflicting regions */
+ /**
+ * When a region of a file is changed in both branches, the file
+ * created in the index will contain each unique line from each side,
+ * which has the result of combining both files. The index will not
+ * record a conflict.
+ */
GIT_MERGE_FILE_FAVOR_UNION = 3,
} git_merge_file_favor_t;
@@ -53,18 +76,28 @@ typedef struct {
unsigned int version;
git_merge_tree_flag_t flags;
- /** Similarity to consider a file renamed (default 50) */
+ /**
+ * Similarity to consider a file renamed (default 50). If
+ * `GIT_MERGE_TREE_FIND_RENAMES` is enabled, added files will be compared
+ * with deleted files to determine their similarity. Files that are
+ * more similar than the rename threshold (percentage-wise) will be
+ * treated as a rename.
+ */
unsigned int rename_threshold;
- /** Maximum similarity sources to examine (overrides the
- * `merge.renameLimit` config) (default 200)
+ /**
+ * Maximum similarity sources to examine for renames (default 200).
+ * If the number of rename candidates (add / delete pairs) is greater
+ * than this value, inexact rename detection is aborted.
+ *
+ * This setting overrides the `merge.renameLimit` configuration value.
*/
unsigned int target_limit;
/** Pluggable similarity metric; pass NULL to use internal metric */
git_diff_similarity_metric *metric;
- /** Flags for automerging content. */
+ /** Flags for handling conflicting content. */
git_merge_file_favor_t file_favor;
} git_merge_tree_opts;
@@ -74,20 +107,37 @@ typedef struct {
/**
* Option flags for `git_merge`.
- *
- * GIT_MERGE_NO_FASTFORWARD - Do not fast-forward.
*/
typedef enum {
- GIT_MERGE_NO_FASTFORWARD = 1,
- GIT_MERGE_FASTFORWARD_ONLY = 2,
+ /**
+ * The default behavior is to allow fast-forwards, returning
+ * immediately with the commit ID to fast-forward to.
+ */
+ GIT_MERGE_DEFAULT = 0,
+
+ /**
+ * Do not fast-forward; perform a merge and prepare a merge result even
+ * if the inputs are eligible for fast-forwarding.
+ */
+ GIT_MERGE_NO_FASTFORWARD = 1,
+
+ /**
+ * Ensure that the inputs are eligible for fast-forwarding, error if
+ * a merge needs to be performed.
+ */
+ GIT_MERGE_FASTFORWARD_ONLY = 2,
} git_merge_flags_t;
typedef struct {
unsigned int version;
+ /** Options for handling the commit-level merge. */
git_merge_flags_t merge_flags;
+
+ /** Options for handling the merges of individual files. */
git_merge_tree_opts merge_tree_opts;
+ /** Options for writing the merge result to the working directory. */
git_checkout_opts checkout_opts;
} git_merge_opts;
@@ -102,7 +152,7 @@ typedef struct {
* @param repo the repository where the commits exist
* @param one one of the commits
* @param two the other commit
- * @return Zero on success; GIT_ENOTFOUND or -1 on failure.
+ * @return 0 on success, GIT_ENOTFOUND if not found or error code
*/
GIT_EXTERN(int) git_merge_base(
git_oid *out,
@@ -117,7 +167,7 @@ GIT_EXTERN(int) git_merge_base(
* @param repo the repository where the commits exist
* @param length The number of commits in the provided `input_array`
* @param input_array oids of the commits
- * @return Zero on success; GIT_ENOTFOUND or -1 on failure.
+ * @return 0 on success, GIT_ENOTFOUND if not found or error code
*/
GIT_EXTERN(int) git_merge_base_many(
git_oid *out,
@@ -126,12 +176,13 @@ GIT_EXTERN(int) git_merge_base_many(
const git_oid input_array[]);
/**
- * Creates a `git_merge_head` from the given reference
+ * Creates a `git_merge_head` from the given reference. The resulting
+ * git_merge_head must be freed with `git_merge_head_free`.
*
* @param out pointer to store the git_merge_head result in
* @param repo repository that contains the given reference
* @param ref reference to use as a merge input
- * @return zero on success, -1 on failure.
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge_head_from_ref(
git_merge_head **out,
@@ -139,14 +190,15 @@ GIT_EXTERN(int) git_merge_head_from_ref(
git_reference *ref);
/**
- * Creates a `git_merge_head` from the given fetch head data
+ * Creates a `git_merge_head` from the given fetch head data. The resulting
+ * git_merge_head must be freed with `git_merge_head_free`.
*
* @param out pointer to store the git_merge_head result in
* @param repo repository that contains the given commit
* @param branch_name name of the (remote) branch
* @param remote_url url of the remote
* @param oid the commit object id to use as a merge input
- * @return zero on success, -1 on failure.
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge_head_from_fetchhead(
git_merge_head **out,
@@ -156,12 +208,13 @@ GIT_EXTERN(int) git_merge_head_from_fetchhead(
const git_oid *oid);
/**
- * Creates a `git_merge_head` from the given commit id
+ * Creates a `git_merge_head` from the given commit id. The resulting
+ * git_merge_head must be freed with `git_merge_head_free`.
*
* @param out pointer to store the git_merge_head result in
* @param repo repository that contains the given commit
* @param id the commit object id to use as a merge input
- * @return zero on success, -1 on failure.
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge_head_from_id(
git_merge_head **out,
@@ -169,7 +222,7 @@ GIT_EXTERN(int) git_merge_head_from_id(
const git_oid *id);
/**
- * Frees a `git_merge_head`
+ * Frees a `git_merge_head`.
*
* @param head merge head to free
*/
@@ -178,7 +231,9 @@ GIT_EXTERN(void) git_merge_head_free(
/**
* Merge two trees, producing a `git_index` that reflects the result of
- * the merge.
+ * the merge. The index may be written as-is to the working directory
+ * or checked out. If the index is to be converted to a tree, the caller
+ * should resolve any conflicts that arose as part of the merge.
*
* The returned index must be freed explicitly with `git_index_free`.
*
@@ -188,7 +243,7 @@ GIT_EXTERN(void) git_merge_head_free(
* @param our_tree the tree that reflects the destination tree
* @param their_tree the tree to merge in to `our_tree`
* @param opts the merge tree options (or null for defaults)
- * @return zero on success, -1 on failure.
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge_trees(
git_index **out,
@@ -200,7 +255,9 @@ GIT_EXTERN(int) git_merge_trees(
/**
* Merge two commits, producing a `git_index` that reflects the result of
- * the merge.
+ * the merge. The index may be written as-is to the working directory
+ * or checked out. If the index is to be converted to a tree, the caller
+ * should resolve any conflicts that arose as part of the merge.
*
* The returned index must be freed explicitly with `git_index_free`.
*
@@ -209,7 +266,7 @@ GIT_EXTERN(int) git_merge_trees(
* @param our_commit the commit that reflects the destination tree
* @param their_commit the commit to merge in to `our_commit`
* @param opts the merge tree options (or null for defaults)
- * @return zero on success, -1 on failure.
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge_commits(
git_index **out,
@@ -219,13 +276,32 @@ GIT_EXTERN(int) git_merge_commits(
const git_merge_tree_opts *opts);
/**
- * Merges the given commits into HEAD, producing a new commit.
+ * Merges the given commit(s) into HEAD and either returns immediately
+ * if there was no merge to perform (the specified commits have already
+ * been merged or would produce a fast-forward) or performs the merge
+ * and writes the results into the working directory.
+ *
+ * Callers should inspect the `git_merge_result`:
+ *
+ * If `git_merge_result_is_uptodate` is true, there is no work to perform.
+ *
+ * If `git_merge_result_is_fastforward` is true, the caller should update
+ * any necessary references to the commit ID returned by
+ * `git_merge_result_fastforward_id` and check that out in order to complete
+ * the fast-forward.
+ *
+ * Otherwise, callers should inspect the resulting index, resolve any
+ * conflicts and prepare a commit.
+ *
+ * The resultant `git_merge_result` should be free with
+ * `git_merge_result_free`.
*
* @param out the results of the merge
* @param repo the repository to merge
* @param merge_heads the heads to merge into
* @param merge_heads_len the number of heads to merge
- * @param flags merge flags
+ * @param opts merge options
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge(
git_merge_result **out,
@@ -235,24 +311,42 @@ GIT_EXTERN(int) git_merge(
const git_merge_opts *opts);
/**
- * Returns true if a merge is up-to-date (we were asked to merge the target
- * into itself.)
+ * Returns true if a merge is "up-to-date", meaning that the commit(s)
+ * that were provided to `git_merge` are already included in `HEAD`
+ * and there is no work to do.
+ *
+ * @return true if the merge is up-to-date, false otherwise
*/
GIT_EXTERN(int) git_merge_result_is_uptodate(git_merge_result *merge_result);
/**
- * Returns true if a merge is eligible for fastforward
+ * Returns true if a merge is eligible to be "fast-forwarded", meaning that
+ * the commit that was provided to `git_merge` need not be merged, it can
+ * simply be checked out, because the current `HEAD` is the merge base of
+ * itself and the given commit. To perform the fast-forward, the caller
+ * should check out the results of `git_merge_result_fastforward_id`.
+ *
+ * This will never be true if `GIT_MERGE_NO_FASTFORWARD` is supplied as
+ * a merge option.
+ *
+ * @return true if the merge is fast-forwardable, false otherwise
*/
GIT_EXTERN(int) git_merge_result_is_fastforward(git_merge_result *merge_result);
/**
* Gets the fast-forward OID if the merge was a fastforward.
*
- * @param out the OID of the fast-forward
+ * @param out pointer to populate with the OID of the fast-forward
* @param merge_result the results of the merge
+ * @return 0 on success or error code
*/
GIT_EXTERN(int) git_merge_result_fastforward_id(git_oid *out, git_merge_result *merge_result);
+/**
+ * Frees a `git_merge_result`.
+ *
+ * @param result merge result to free
+ */
GIT_EXTERN(void) git_merge_result_free(git_merge_result *merge_result);
/** @} */
diff --git a/include/git2/oid.h b/include/git2/oid.h
index 384b656d7..1cfd4e5e2 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -167,10 +167,7 @@ GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b);
* @param b second oid structure.
* @return true if equal, false otherwise
*/
-GIT_INLINE(int) git_oid_equal(const git_oid *a, const git_oid *b)
-{
- return !git_oid_cmp(a, b);
-}
+GIT_EXTERN(int) git_oid_equal(const git_oid *a, const git_oid *b);
/**
* Compare the first 'len' hexadecimal characters (packets of 4 bits)
diff --git a/include/git2/patch.h b/include/git2/patch.h
index 1eca29d4a..f5ec682c6 100644
--- a/include/git2/patch.h
+++ b/include/git2/patch.h
@@ -106,6 +106,34 @@ GIT_EXTERN(int) git_patch_from_blob_and_buffer(
const git_diff_options *opts);
/**
+ * Directly generate a patch from the difference between two buffers.
+ *
+ * This is just like `git_diff_buffers()` except it generates a patch
+ * object for the difference instead of directly making callbacks. You can
+ * use the standard `git_patch` accessor functions to read the patch
+ * data, and you must call `git_patch_free()` on the patch when done.
+ *
+ * @param out The generated patch; NULL on error
+ * @param old_buffer Raw data for old side of diff, or NULL for empty
+ * @param old_len Length of the raw data for old side of the diff
+ * @param old_as_path Treat old buffer as if it had this filename; can be NULL
+ * @param new_buffer Raw data for new side of diff, or NULL for empty
+ * @param new_len Length of raw data for new side of diff
+ * @param new_as_path Treat buffer as if it had this filename; can be NULL
+ * @param opts Options for diff, or NULL for default options
+ * @return 0 on success or error code < 0
+ */
+GIT_EXTERN(int) git_patch_from_buffers(
+ git_patch **out,
+ const void *old_buffer,
+ size_t old_len,
+ const char *old_as_path,
+ const char *new_buffer,
+ size_t new_len,
+ const char *new_as_path,
+ const git_diff_options *opts);
+
+/**
* Free a git_patch object.
*/
GIT_EXTERN(void) git_patch_free(git_patch *patch);
diff --git a/include/git2/push.h b/include/git2/push.h
index 12f0e7f2c..67702aca2 100644
--- a/include/git2/push.h
+++ b/include/git2/push.h
@@ -103,10 +103,16 @@ GIT_EXTERN(int) git_push_add_refspec(git_push *push, const char *refspec);
* Update remote tips after a push
*
* @param push The push object
+ * @param signature The identity to use when updating reflogs
+ * @param reflog_message The message to insert into the reflogs. If NULL, the
+ * default is "update by push".
*
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_push_update_tips(git_push *push);
+GIT_EXTERN(int) git_push_update_tips(
+ git_push *push,
+ const git_signature *signature,
+ const char *reflog_message);
/**
* Actually push all given refspecs
diff --git a/include/git2/refs.h b/include/git2/refs.h
index 976b7496b..a4e44c543 100644
--- a/include/git2/refs.h
+++ b/include/git2/refs.h
@@ -68,6 +68,48 @@ GIT_EXTERN(int) git_reference_name_to_id(
GIT_EXTERN(int) git_reference_dwim(git_reference **out, git_repository *repo, const char *shorthand);
/**
+ * Conditionally create a new symbolic reference.
+ *
+ * A symbolic reference is a reference name that refers to another
+ * reference name. If the other name moves, the symbolic name will move,
+ * too. As a simple example, the "HEAD" reference might refer to
+ * "refs/heads/master" while on the "master" branch of a repository.
+ *
+ * The symbolic reference will be created in the repository and written to
+ * the disk. The generated reference object must be freed by the user.
+ *
+ * Valid reference names must follow one of two patterns:
+ *
+ * 1. Top-level names must contain only capital letters and underscores,
+ * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD").
+ * 2. Names prefixed with "refs/" can be almost anything. You must avoid
+ * the characters '~', '^', ':', '\\', '?', '[', and '*', and the
+ * sequences ".." and "@{" which have special meaning to revparse.
+ *
+ * This function will return an error if a reference already exists with the
+ * given name unless `force` is true, in which case it will be overwritten.
+ *
+ * The signature and message for the reflog will be ignored if the
+ * reference does not belong in the standard set (HEAD, branches and
+ * remote-tracking branches) and it does not have a reflog.
+ *
+ * It will return GIT_EMODIFIED if the reference's value at the time
+ * of updating does not match the one passed through `current_value`
+ * (i.e. if the ref has changed since the user read it).
+ *
+ * @param out Pointer to the newly created reference
+ * @param repo Repository where that reference will live
+ * @param name The name of the reference
+ * @param target The target of the reference
+ * @param force Overwrite existing references
+ * @param current_value The expected value of the reference when updating
+ * @param signature The identity that will used to populate the reflog entry
+ * @param log_message The one line long message to be appended to the reflog
+ * @return 0 on success, GIT_EEXISTS, GIT_EINVALIDSPEC, GIT_EMODIFIED or an error code
+ */
+GIT_EXTERN(int) git_reference_symbolic_create_matching(git_reference **out, git_repository *repo, const char *name, const char *target, int force, const char *current_value, const git_signature *signature, const char *log_message);
+
+/**
* Create a new symbolic reference.
*
* A symbolic reference is a reference name that refers to another
@@ -144,6 +186,51 @@ GIT_EXTERN(int) git_reference_symbolic_create(git_reference **out, git_repositor
GIT_EXTERN(int) git_reference_create(git_reference **out, git_repository *repo, const char *name, const git_oid *id, int force, const git_signature *signature, const char *log_message);
/**
+ * Conditionally create new direct reference
+ *
+ * A direct reference (also called an object id reference) refers directly
+ * to a specific object id (a.k.a. OID or SHA) in the repository. The id
+ * permanently refers to the object (although the reference itself can be
+ * moved). For example, in libgit2 the direct ref "refs/tags/v0.17.0"
+ * refers to OID 5b9fac39d8a76b9139667c26a63e6b3f204b3977.
+ *
+ * The direct reference will be created in the repository and written to
+ * the disk. The generated reference object must be freed by the user.
+ *
+ * Valid reference names must follow one of two patterns:
+ *
+ * 1. Top-level names must contain only capital letters and underscores,
+ * and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD").
+ * 2. Names prefixed with "refs/" can be almost anything. You must avoid
+ * the characters '~', '^', ':', '\\', '?', '[', and '*', and the
+ * sequences ".." and "@{" which have special meaning to revparse.
+ *
+ * This function will return an error if a reference already exists with the
+ * given name unless `force` is true, in which case it will be overwritten.
+ *
+ * The signature and message for the reflog will be ignored if the
+ * reference does not belong in the standard set (HEAD, branches and
+ * remote-tracking branches) and and it does not have a reflog.
+ *
+ * It will return GIT_EMODIFIED if the reference's value at the time
+ * of updating does not match the one passed through `current_id`
+ * (i.e. if the ref has changed since the user read it).
+ *
+ * @param out Pointer to the newly created reference
+ * @param repo Repository where that reference will live
+ * @param name The name of the reference
+ * @param id The object id pointed to by the reference.
+ * @param force Overwrite existing references
+ * @param force Overwrite existing references
+ * @param current_id The expected value of the reference at the time of update
+ * @param signature The identity that will used to populate the reflog entry
+ * @param log_message The one line long message to be appended to the reflog
+ * @return 0 on success, GIT_EMODIFIED if the value of the reference
+ * has changed, GIT_EEXISTS, GIT_EINVALIDSPEC or an error code
+ */
+GIT_EXTERN(int) git_reference_create_matching(git_reference **out, git_repository *repo, const char *name, const git_oid *id, int force, const git_oid *current_id, const git_signature *signature, const char *log_message);
+
+/**
* Get the OID pointed to by a direct reference.
*
* Only available if the reference is direct (i.e. an object id reference,
@@ -254,22 +341,19 @@ GIT_EXTERN(int) git_reference_symbolic_set_target(
const char *log_message);
/**
- * Create a new reference with the same name as the given reference but a
+ * Conditionally create a new reference with the same name as the given reference but a
* different OID target. The reference must be a direct reference, otherwise
* this will fail.
*
* The new reference will be written to disk, overwriting the given reference.
*
- * The signature and message for the reflog will be ignored if the
- * reference does not belong in the standard set (HEAD, branches and
- * remote-tracking branches) and and it does not have a reflog.
- *
* @param out Pointer to the newly created reference
* @param ref The reference
* @param id The new target OID for the reference
* @param signature The identity that will used to populate the reflog entry
* @param log_message The one line long message to be appended to the reflog
- * @return 0 or an error code
+ * @return 0 on success, GIT_EMODIFIED if the value of the reference
+ * has changed since it was read, or an error code
*/
GIT_EXTERN(int) git_reference_set_target(
git_reference **out,
@@ -317,12 +401,26 @@ GIT_EXTERN(int) git_reference_rename(
* will be immediately removed on disk but the memory will not be freed.
* Callers must call `git_reference_free`.
*
+ * This function will return an error if the reference has changed
+ * from the time it was looked up.
+ *
* @param ref The reference to remove
- * @return 0 or an error code
+ * @return 0, GIT_EMODIFIED or an error code
*/
GIT_EXTERN(int) git_reference_delete(git_reference *ref);
/**
+ * Delete an existing reference by name
+ *
+ * This method removes the named reference from the repository without
+ * looking at its old value.
+ *
+ * @param ref The reference to remove
+ * @return 0, GIT_EMODIFIED or an error code
+ */
+GIT_EXTERN(int) git_reference_remove(git_repository *repo, const char *name);
+
+/**
* Fill a list with all the references that can be found in a repository.
*
* The string array will be filled with the names of all references; these
@@ -510,6 +608,16 @@ GIT_EXTERN(int) git_reference_is_remote(git_reference *ref);
*/
GIT_EXTERN(int) git_reference_is_tag(git_reference *ref);
+/**
+ * Check if a reference is a note
+ *
+ * @param ref A git reference
+ *
+ * @return 1 when the reference lives in the refs/notes
+ * namespace; 0 otherwise.
+ */
+GIT_EXTERN(int) git_reference_is_note(git_reference *ref);
+
typedef enum {
GIT_REF_FORMAT_NORMAL = 0u,
diff --git a/include/git2/remote.h b/include/git2/remote.h
index eba6ca7f9..238b6fd4f 100644
--- a/include/git2/remote.h
+++ b/include/git2/remote.h
@@ -355,9 +355,16 @@ GIT_EXTERN(void) git_remote_free(git_remote *remote);
* Update the tips to the new state
*
* @param remote the remote to update
+ * @param signature The identity to use when updating reflogs
+ * @param reflog_message The message to insert into the reflogs. If NULL, the
+ * default is "fetch <name>", where <name> is the name of
+ * the remote (or its url, for in-memory remotes).
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_remote_update_tips(git_remote *remote);
+GIT_EXTERN(int) git_remote_update_tips(
+ git_remote *remote,
+ const git_signature *signature,
+ const char *reflog_message);
/**
* Download new data and update tips
@@ -366,9 +373,15 @@ GIT_EXTERN(int) git_remote_update_tips(git_remote *remote);
* disconnect and update the remote-tracking branches.
*
* @param remote the remote to fetch from
+ * @param signature The identity to use when updating reflogs
+ * @param reflog_message The message to insert into the reflogs. If NULL, the
+ * default is "fetch"
* @return 0 or an error code
*/
-GIT_EXTERN(int) git_remote_fetch(git_remote *remote);
+GIT_EXTERN(int) git_remote_fetch(
+ git_remote *remote,
+ const git_signature *signature,
+ const char *reflog_message);
/**
* Return whether a string is a valid remote URL
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 648667cd6..bf12c7a69 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -615,11 +615,15 @@ GIT_EXTERN(int) git_repository_set_head_detached(
* Otherwise, the HEAD will be detached and point to the peeled Commit.
*
* @param repo Repository pointer
+ * @param signature The identity that will used to populate the reflog entry
+ * @param log_message The one line long message to be appended to the reflog
* @return 0 on success, GIT_EUNBORNBRANCH when HEAD points to a non existing
* branch or an error code
*/
GIT_EXTERN(int) git_repository_detach_head(
- git_repository* repo);
+ git_repository* repo,
+ const git_signature *signature,
+ const char *reflog_message);
typedef enum {
GIT_REPOSITORY_STATE_NONE,
diff --git a/include/git2/reset.h b/include/git2/reset.h
index c36781722..1759cc036 100644
--- a/include/git2/reset.h
+++ b/include/git2/reset.h
@@ -48,10 +48,21 @@ typedef enum {
*
* @param reset_type Kind of reset operation to perform.
*
+ * @param signature The identity that will used to populate the reflog entry
+ *
+ * @param log_message The one line long message to be appended to the reflog.
+ * The reflog is only updated if the affected direct reference is actually
+ * changing. If NULL, the default is "reset: moving"; if you want something more
+ * useful, provide a message.
+ *
* @return 0 on success or an error code
*/
GIT_EXTERN(int) git_reset(
- git_repository *repo, git_object *target, git_reset_t reset_type);
+ git_repository *repo,
+ git_object *target,
+ git_reset_t reset_type,
+ git_signature *signature,
+ const char *log_message);
/**
* Updates some entries in the index from the target commit tree.
diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h
index c59b79938..aef0b5fa6 100644
--- a/include/git2/revwalk.h
+++ b/include/git2/revwalk.h
@@ -87,7 +87,7 @@ GIT_EXTERN(void) git_revwalk_reset(git_revwalk *walker);
/**
* Mark a commit to start traversal from.
*
- * The given OID must belong to a commit on the walked
+ * The given OID must belong to a committish on the walked
* repository.
*
* The given commit will be used as one of the roots
@@ -108,7 +108,10 @@ GIT_EXTERN(int) git_revwalk_push(git_revwalk *walk, const git_oid *id);
* pattern will be pushed to the revision walker.
*
* A leading 'refs/' is implied if not present as well as a trailing
- * '/ *' if the glob lacks '?', '*' or '['.
+ * '/\*' if the glob lacks '?', '\*' or '['.
+ *
+ * Any references matching this glob which do not point to a
+ * committish will be ignored.
*
* @param walk the walker being used for the traversal
* @param glob the glob pattern references should match
@@ -127,7 +130,7 @@ GIT_EXTERN(int) git_revwalk_push_head(git_revwalk *walk);
/**
* Mark a commit (and its ancestors) uninteresting for the output.
*
- * The given OID must belong to a commit on the walked
+ * The given OID must belong to a committish on the walked
* repository.
*
* The resolved commit and all its parents will be hidden from the
@@ -147,7 +150,10 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, const git_oid *commit_id);
* revision walk.
*
* A leading 'refs/' is implied if not present as well as a trailing
- * '/ *' if the glob lacks '?', '*' or '['.
+ * '/\*' if the glob lacks '?', '\*' or '['.
+ *
+ * Any references matching this glob which do not point to a
+ * committish will be ignored.
*
* @param walk the walker being used for the traversal
* @param glob the glob pattern references should match
@@ -166,7 +172,7 @@ GIT_EXTERN(int) git_revwalk_hide_head(git_revwalk *walk);
/**
* Push the OID pointed to by a reference
*
- * The reference must point to a commit.
+ * The reference must point to a committish.
*
* @param walk the walker being used for the traversal
* @param refname the reference to push
@@ -177,7 +183,7 @@ GIT_EXTERN(int) git_revwalk_push_ref(git_revwalk *walk, const char *refname);
/**
* Hide the OID pointed to by a reference
*
- * The reference must point to a commit.
+ * The reference must point to a committish.
*
* @param walk the walker being used for the traversal
* @param refname the reference to hide
diff --git a/include/git2/sys/commit.h b/include/git2/sys/commit.h
index c8ed56b66..627d3ae2e 100644
--- a/include/git2/sys/commit.h
+++ b/include/git2/sys/commit.h
@@ -21,16 +21,18 @@
GIT_BEGIN_DECL
/**
- * Create new commit in the repository from a list of `git_oid` values
+ * Create new commit in the repository from a list of `git_oid` values.
*
* See documentation for `git_commit_create()` for information about the
* parameters, as the meaning is identical excepting that `tree` and
* `parents` now take `git_oid`. This is a dangerous API in that nor
* the `tree`, neither the `parents` list of `git_oid`s are checked for
* validity.
+ *
+ * @see git_commit_create
*/
GIT_EXTERN(int) git_commit_create_from_ids(
- git_oid *oid,
+ git_oid *id,
git_repository *repo,
const char *update_ref,
const git_signature *author,
@@ -38,9 +40,41 @@ GIT_EXTERN(int) git_commit_create_from_ids(
const char *message_encoding,
const char *message,
const git_oid *tree,
- int parent_count,
+ size_t parent_count,
const git_oid *parents[]);
+/**
+ * Callback function to return parents for commit.
+ *
+ * This is invoked with the count of the number of parents processed so far
+ * along with the user supplied payload. This should return a git_oid of
+ * the next parent or NULL if all parents have been provided.
+ */
+typedef const git_oid *(*git_commit_parent_callback)(size_t idx, void *payload);
+
+/**
+ * Create a new commit in the repository with an callback to supply parents.
+ *
+ * See documentation for `git_commit_create()` for information about the
+ * parameters, as the meaning is identical excepting that `tree` takes a
+ * `git_oid` and doesn't check for validity, and `parent_cb` is invoked
+ * with `parent_payload` and should return `git_oid` values or NULL to
+ * indicate that all parents are accounted for.
+ *
+ * @see git_commit_create
+ */
+GIT_EXTERN(int) git_commit_create_from_callback(
+ git_oid *id,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_oid *tree,
+ git_commit_parent_callback parent_cb,
+ void *parent_payload);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index 5bbd4ba4c..aa5ef9ecc 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -94,7 +94,8 @@ struct git_refdb_backend {
*/
int (*write)(git_refdb_backend *backend,
const git_reference *ref, int force,
- const git_signature *who, const char *message);
+ const git_signature *who, const char *message,
+ const git_oid *old, const char *old_target);
int (*rename)(
git_reference **out, git_refdb_backend *backend,
@@ -105,7 +106,7 @@ struct git_refdb_backend {
* Deletes the given reference from the refdb. A refdb implementation
* must provide this function.
*/
- int (*del)(git_refdb_backend *backend, const char *ref_name);
+ int (*del)(git_refdb_backend *backend, const char *ref_name, const git_oid *old_id, const char *old_target);
/**
* Suggests that the given refdb compress or optimize its references.
diff --git a/script/cibuild.sh b/script/cibuild.sh
index 5c0584a80..1f15e851e 100755
--- a/script/cibuild.sh
+++ b/script/cibuild.sh
@@ -34,5 +34,5 @@ export GITTEST_REMOTE_SSH_PUBKEY="$HOME/.ssh/id_rsa.pub"
export GITTEST_REMOTE_SSH_PASSPHRASE=""
if [ -e ./libgit2_clar ]; then
- ./libgit2_clar -sonline::push
+ ./libgit2_clar -sonline::push -sonline::clone::cred_callback_failure
fi
diff --git a/src/array.h b/src/array.h
index 1d4e1c224..f8a48722a 100644
--- a/src/array.h
+++ b/src/array.h
@@ -7,7 +7,7 @@
#ifndef INCLUDE_array_h__
#define INCLUDE_array_h__
-#include "util.h"
+#include "common.h"
/*
* Use this to declare a typesafe resizable array of items, a la:
diff --git a/src/attr.c b/src/attr.c
index 15ed5c9e0..d8a171d0f 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -1,6 +1,6 @@
#include "common.h"
#include "repository.h"
-#include "fileops.h"
+#include "sysdir.h"
#include "config.h"
#include "attr.h"
#include "ignore.h"
@@ -589,7 +589,7 @@ static int collect_attr_files(
}
if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
- error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
+ error = git_sysdir_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
if (!error)
error = push_attr_file(repo, files, NULL, dir.ptr);
else if (error == GIT_ENOTFOUND) {
@@ -623,13 +623,13 @@ static int attr_cache__lookup_path(
/* expand leading ~/ as needed */
if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
- !git_futils_find_global_file(&buf, &cfgval[2]))
+ !git_sysdir_find_global_file(&buf, &cfgval[2]))
*out = git_buf_detach(&buf);
else if (cfgval)
*out = git__strdup(cfgval);
}
- else if (!git_futils_find_xdg_file(&buf, fallback))
+ else if (!git_sysdir_find_xdg_file(&buf, fallback))
*out = git_buf_detach(&buf);
git_buf_free(&buf);
diff --git a/src/bitvec.h b/src/bitvec.h
index fd6f0ccf8..544832d95 100644
--- a/src/bitvec.h
+++ b/src/bitvec.h
@@ -7,7 +7,7 @@
#ifndef INCLUDE_bitvec_h__
#define INCLUDE_bitvec_h__
-#include "util.h"
+#include "common.h"
/*
* This is a silly little fixed length bit vector type that will store
diff --git a/src/blame_git.c b/src/blame_git.c
index 800f1f039..72afb852b 100644
--- a/src/blame_git.c
+++ b/src/blame_git.c
@@ -485,12 +485,14 @@ static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt
git_blame__origin *sg_buf[16];
git_blame__origin *porigin, **sg_origin = sg_buf;
- GIT_UNUSED(opt);
-
num_parents = git_commit_parentcount(commit);
if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit))
/* Stop at oldest specified commit */
num_parents = 0;
+ else if (opt & GIT_BLAME_FIRST_PARENT && num_parents > 1)
+ /* Limit search to the first parent */
+ num_parents = 1;
+
if (!num_parents) {
git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit));
goto finish;
diff --git a/src/buffer.c b/src/buffer.c
index 318fee753..a83ca8792 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -7,7 +7,6 @@
#include "buffer.h"
#include "posix.h"
#include "git2/buffer.h"
-#include <stdarg.h>
#include <ctype.h>
/* Used as default value for git_buf->ptr so that people can always
diff --git a/src/buffer.h b/src/buffer.h
index 564a4f561..4c852b3cb 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -7,8 +7,6 @@
#ifndef INCLUDE_buffer_h__
#define INCLUDE_buffer_h__
-#include <stdarg.h>
-
#include "common.h"
#include "git2/strarray.h"
#include "git2/buffer.h"
diff --git a/src/cc-compat.h b/src/cc-compat.h
index 37f1ea81e..e73cb6de8 100644
--- a/src/cc-compat.h
+++ b/src/cc-compat.h
@@ -7,6 +7,8 @@
#ifndef INCLUDE_compat_h__
#define INCLUDE_compat_h__
+#include <stdarg.h>
+
/*
* See if our compiler is known to support flexible array members.
*/
diff --git a/src/clone.c b/src/clone.c
index 3443528f7..bcc38678f 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -360,7 +360,7 @@ int git_clone_into(git_repository *repo, git_remote *remote, const git_checkout_
git_remote_set_update_fetchhead(remote, 0);
git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
- if ((error = git_remote_fetch(remote)) != 0)
+ if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0)
goto cleanup;
if (branch)
diff --git a/src/commit.c b/src/commit.c
index da7c4992e..2f5a5b51e 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -17,8 +17,6 @@
#include "signature.h"
#include "message.h"
-#include <stdarg.h>
-
void git_commit__free(void *_commit)
{
git_commit *commit = _commit;
@@ -36,41 +34,8 @@ void git_commit__free(void *_commit)
git__free(commit);
}
-int git_commit_create_v(
- git_oid *oid,
- git_repository *repo,
- const char *update_ref,
- const git_signature *author,
- const git_signature *committer,
- const char *message_encoding,
- const char *message,
- const git_tree *tree,
- int parent_count,
- ...)
-{
- va_list ap;
- int i, res;
- const git_commit **parents;
-
- parents = git__malloc(parent_count * sizeof(git_commit *));
- GITERR_CHECK_ALLOC(parents);
-
- va_start(ap, parent_count);
- for (i = 0; i < parent_count; ++i)
- parents[i] = va_arg(ap, const git_commit *);
- va_end(ap);
-
- res = git_commit_create(
- oid, repo, update_ref, author, committer,
- message_encoding, message,
- tree, parent_count, parents);
-
- git__free((void *)parents);
- return res;
-}
-
-int git_commit_create_from_ids(
- git_oid *oid,
+int git_commit_create_from_callback(
+ git_oid *id,
git_repository *repo,
const char *update_ref,
const git_signature *author,
@@ -78,19 +43,20 @@ int git_commit_create_from_ids(
const char *message_encoding,
const char *message,
const git_oid *tree,
- int parent_count,
- const git_oid *parents[])
+ git_commit_parent_callback parent_cb,
+ void *parent_payload)
{
git_buf commit = GIT_BUF_INIT;
- int i;
+ size_t i = 0;
git_odb *odb;
+ const git_oid *parent;
- assert(oid && repo && tree && parent_count >= 0);
+ assert(id && repo && tree && parent_cb);
git_oid__writebuf(&commit, "tree ", tree);
- for (i = 0; i < parent_count; ++i)
- git_oid__writebuf(&commit, "parent ", parents[i]);
+ while ((parent = parent_cb(i++, parent_payload)) != NULL)
+ git_oid__writebuf(&commit, "parent ", parent);
git_signature__writebuf(&commit, "author ", author);
git_signature__writebuf(&commit, "committer ", committer);
@@ -106,13 +72,32 @@ int git_commit_create_from_ids(
if (git_repository_odb__weakptr(&odb, repo) < 0)
goto on_error;
- if (git_odb_write(oid, odb, commit.ptr, commit.size, GIT_OBJ_COMMIT) < 0)
+ if (git_odb_write(id, odb, commit.ptr, commit.size, GIT_OBJ_COMMIT) < 0)
goto on_error;
git_buf_free(&commit);
- if (update_ref != NULL)
- return git_reference__update_terminal(repo, update_ref, oid, NULL, NULL);
+ if (update_ref != NULL) {
+ int error;
+ git_commit *c;
+ const char *shortmsg;
+ git_buf reflog_msg = GIT_BUF_INIT;
+
+ if (git_commit_lookup(&c, repo, id) < 0)
+ goto on_error;
+
+ shortmsg = git_commit_summary(c);
+ git_buf_printf(&reflog_msg, "commit%s: %s",
+ git_commit_parentcount(c) == 0 ? " (initial)" : "",
+ shortmsg);
+ git_commit_free(c);
+
+ error = git_reference__update_terminal(repo, update_ref, id,
+ committer, git_buf_cstr(&reflog_msg));
+
+ git_buf_free(&reflog_msg);
+ return error;
+ }
return 0;
@@ -122,8 +107,101 @@ on_error:
return -1;
}
+typedef struct {
+ size_t total;
+ va_list args;
+} commit_parent_varargs;
+
+static const git_oid *commit_parent_from_varargs(size_t curr, void *payload)
+{
+ commit_parent_varargs *data = payload;
+ const git_commit *commit;
+ if (curr >= data->total)
+ return NULL;
+ commit = va_arg(data->args, const git_commit *);
+ return commit ? git_commit_id(commit) : NULL;
+}
+
+int git_commit_create_v(
+ git_oid *id,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree,
+ size_t parent_count,
+ ...)
+{
+ int error = 0;
+ commit_parent_varargs data;
+
+ assert(tree && git_tree_owner(tree) == repo);
+
+ data.total = parent_count;
+ va_start(data.args, parent_count);
+
+ error = git_commit_create_from_callback(
+ id, repo, update_ref, author, committer,
+ message_encoding, message, git_tree_id(tree),
+ commit_parent_from_varargs, &data);
+
+ va_end(data.args);
+ return error;
+}
+
+typedef struct {
+ size_t total;
+ const git_oid **parents;
+} commit_parent_oids;
+
+static const git_oid *commit_parent_from_ids(size_t curr, void *payload)
+{
+ commit_parent_oids *data = payload;
+ return (curr < data->total) ? data->parents[curr] : NULL;
+}
+
+int git_commit_create_from_ids(
+ git_oid *id,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_oid *tree,
+ size_t parent_count,
+ const git_oid *parents[])
+{
+ commit_parent_oids data = { parent_count, parents };
+
+ return git_commit_create_from_callback(
+ id, repo, update_ref, author, committer,
+ message_encoding, message, tree,
+ commit_parent_from_ids, &data);
+}
+
+typedef struct {
+ size_t total;
+ const git_commit **parents;
+ git_repository *repo;
+} commit_parent_data;
+
+static const git_oid *commit_parent_from_array(size_t curr, void *payload)
+{
+ commit_parent_data *data = payload;
+ const git_commit *commit;
+ if (curr >= data->total)
+ return NULL;
+ commit = data->parents[curr];
+ if (git_commit_owner(commit) != data->repo)
+ return NULL;
+ return git_commit_id(commit);
+}
+
int git_commit_create(
- git_oid *oid,
+ git_oid *id,
git_repository *repo,
const char *update_ref,
const git_signature *author,
@@ -131,31 +209,66 @@ int git_commit_create(
const char *message_encoding,
const char *message,
const git_tree *tree,
- int parent_count,
+ size_t parent_count,
const git_commit *parents[])
{
- int retval, i;
- const git_oid **parent_oids;
-
- assert(parent_count >= 0);
- assert(git_object_owner((const git_object *)tree) == repo);
+ commit_parent_data data = { parent_count, parents, repo };
- parent_oids = git__malloc(parent_count * sizeof(git_oid *));
- GITERR_CHECK_ALLOC(parent_oids);
+ assert(tree && git_tree_owner(tree) == repo);
- for (i = 0; i < parent_count; ++i) {
- assert(git_object_owner((const git_object *)parents[i]) == repo);
- parent_oids[i] = git_object_id((const git_object *)parents[i]);
- }
+ return git_commit_create_from_callback(
+ id, repo, update_ref, author, committer,
+ message_encoding, message, git_tree_id(tree),
+ commit_parent_from_array, &data);
+}
- retval = git_commit_create_from_ids(
- oid, repo, update_ref, author, committer,
- message_encoding, message,
- git_object_id((const git_object *)tree), parent_count, parent_oids);
+static const git_oid *commit_parent_for_amend(size_t curr, void *payload)
+{
+ const git_commit *commit_to_amend = payload;
+ if (curr >= git_array_size(commit_to_amend->parent_ids))
+ return NULL;
+ return git_array_get(commit_to_amend->parent_ids, curr);
+}
- git__free((void *)parent_oids);
+int git_commit_amend(
+ git_oid *id,
+ const git_commit *commit_to_amend,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ const git_tree *tree)
+{
+ git_repository *repo;
+ git_oid tree_id;
+
+ assert(id && commit_to_amend);
+
+ repo = git_commit_owner(commit_to_amend);
+
+ if (!author)
+ author = git_commit_author(commit_to_amend);
+ if (!committer)
+ committer = git_commit_committer(commit_to_amend);
+ if (!message_encoding)
+ message_encoding = git_commit_message_encoding(commit_to_amend);
+ if (!message)
+ message = git_commit_message(commit_to_amend);
+
+ if (!tree) {
+ git_tree *old_tree;
+ GITERR_CHECK_ERROR( git_commit_tree(&old_tree, commit_to_amend) );
+ git_oid_cpy(&tree_id, git_tree_id(old_tree));
+ git_tree_free(old_tree);
+ } else {
+ assert(git_tree_owner(tree) == repo);
+ git_oid_cpy(&tree_id, git_tree_id(tree));
+ }
- return retval;
+ return git_commit_create_from_callback(
+ id, repo, update_ref, author, committer, message_encoding, message,
+ &tree_id, commit_parent_for_amend, (void *)commit_to_amend);
}
int git_commit__parse(void *_commit, git_odb_object *odb_obj)
@@ -164,33 +277,15 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
const char *buffer_start = git_odb_object_data(odb_obj), *buffer;
const char *buffer_end = buffer_start + git_odb_object_size(odb_obj);
git_oid parent_id;
- uint32_t parent_count = 0;
size_t header_len;
- /* find end-of-header (counting parents as we go) */
- for (buffer = buffer_start; buffer < buffer_end; ++buffer) {
- if (!strncmp("\n\n", buffer, 2)) {
- ++buffer;
- break;
- }
- if (!strncmp("\nparent ", buffer, strlen("\nparent ")))
- ++parent_count;
- }
+ buffer = buffer_start;
- header_len = buffer - buffer_start;
- commit->raw_header = git__strndup(buffer_start, header_len);
- GITERR_CHECK_ALLOC(commit->raw_header);
-
- /* point "buffer" to header data */
- buffer = commit->raw_header;
- buffer_end = commit->raw_header + header_len;
-
- if (parent_count < 1)
- parent_count = 1;
-
- git_array_init_to_size(commit->parent_ids, parent_count);
+ /* Allocate for one, which will allow not to realloc 90% of the time */
+ git_array_init_to_size(commit->parent_ids, 1);
GITERR_CHECK_ARRAY(commit->parent_ids);
+ /* The tree is always the first field */
if (git_oid__parse(&commit->tree_id, &buffer, buffer_end, "tree ") < 0)
goto bad_buffer;
@@ -221,6 +316,9 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
/* Parse add'l header entries */
while (buffer < buffer_end) {
const char *eoln = buffer;
+ if (buffer[-1] == '\n' && buffer[0] == '\n')
+ break;
+
while (eoln < buffer_end && *eoln != '\n')
++eoln;
@@ -236,13 +334,12 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
buffer = eoln;
}
- /* point "buffer" to data after header */
- buffer = git_odb_object_data(odb_obj);
- buffer_end = buffer + git_odb_object_size(odb_obj);
+ header_len = buffer - buffer_start;
+ commit->raw_header = git__strndup(buffer_start, header_len);
+ GITERR_CHECK_ALLOC(commit->raw_header);
- buffer += header_len;
- if (*buffer == '\n')
- ++buffer;
+ /* point "buffer" to data after header, +1 for the final LF */
+ buffer = buffer_start + header_len + 1;
/* extract commit message */
if (buffer <= buffer_end) {
@@ -311,10 +408,9 @@ const char *git_commit_summary(git_commit *commit)
git_buf_putc(&summary, *msg);
}
- if (summary.asize == 0)
+ commit->summary = git_buf_detach(&summary);
+ if (!commit->summary)
commit->summary = git__strdup("");
- else
- commit->summary = git_buf_detach(&summary);
}
return commit->summary;
diff --git a/src/commit_list.c b/src/commit_list.c
index 64416e54d..9db3f5633 100644
--- a/src/commit_list.c
+++ b/src/commit_list.c
@@ -11,10 +11,10 @@
#include "pool.h"
#include "odb.h"
-int git_commit_list_time_cmp(void *a, void *b)
+int git_commit_list_time_cmp(const void *a, const void *b)
{
- git_commit_list_node *commit_a = (git_commit_list_node *)a;
- git_commit_list_node *commit_b = (git_commit_list_node *)b;
+ const git_commit_list_node *commit_a = a;
+ const git_commit_list_node *commit_b = b;
return (commit_a->time < commit_b->time);
}
diff --git a/src/commit_list.h b/src/commit_list.h
index d2f54b3ca..490d841be 100644
--- a/src/commit_list.h
+++ b/src/commit_list.h
@@ -39,7 +39,7 @@ typedef struct git_commit_list {
} git_commit_list;
git_commit_list_node *git_commit_list_alloc_node(git_revwalk *walk);
-int git_commit_list_time_cmp(void *a, void *b);
+int git_commit_list_time_cmp(const void *a, const void *b);
void git_commit_list_free(git_commit_list **list_p);
git_commit_list *git_commit_list_insert(git_commit_list_node *item, git_commit_list **list_p);
git_commit_list *git_commit_list_insert_by_date(git_commit_list_node *item, git_commit_list **list_p);
diff --git a/src/common.h b/src/common.h
index e315b5979..d389cf85d 100644
--- a/src/common.h
+++ b/src/common.h
@@ -10,6 +10,13 @@
#include "git2/common.h"
#include "cc-compat.h"
+/** Declare a function as always inlined. */
+#if defined(_MSC_VER)
+# define GIT_INLINE(type) static __inline type
+#else
+# define GIT_INLINE(type) static inline type
+#endif
+
#include <assert.h>
#include <errno.h>
#include <limits.h>
diff --git a/src/config.c b/src/config.c
index 6aa71468a..ae093ed64 100644
--- a/src/config.c
+++ b/src/config.c
@@ -6,7 +6,7 @@
*/
#include "common.h"
-#include "fileops.h"
+#include "sysdir.h"
#include "config.h"
#include "git2/config.h"
#include "git2/sys/config.h"
@@ -937,17 +937,17 @@ void git_config_iterator_free(git_config_iterator *iter)
int git_config_find_global(git_buf *path)
{
- return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
+ return git_sysdir_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
}
int git_config_find_xdg(git_buf *path)
{
- return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
+ return git_sysdir_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
}
int git_config_find_system(git_buf *path)
{
- return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
+ return git_sysdir_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
}
int git_config__global_location(git_buf *buf)
@@ -956,7 +956,7 @@ int git_config__global_location(git_buf *buf)
const char *sep, *start;
size_t len;
- if (git_futils_dirs_get(&paths, GIT_FUTILS_DIR_GLOBAL) < 0)
+ if (git_sysdir_get(&paths, GIT_SYSDIR_GLOBAL) < 0)
return -1;
/* no paths, so give up */
diff --git a/src/config_file.c b/src/config_file.c
index c7727c029..aedf2cb12 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -7,8 +7,8 @@
#include "common.h"
#include "config.h"
-#include "fileops.h"
#include "filebuf.h"
+#include "sysdir.h"
#include "buffer.h"
#include "buf_text.h"
#include "git2/config.h"
@@ -1003,7 +1003,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_futils_find_global_file(out, &path[1]);
+ return git_sysdir_find_global_file(out, &path[1]);
return git_path_join_unrooted(out, path, dir, NULL);
}
diff --git a/src/crlf.c b/src/crlf.c
index e1bd5572b..2480cc918 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -218,24 +218,11 @@ static int crlf_apply_to_workdir(
if (!workdir_ending)
return -1;
- if (!strcmp("\n", workdir_ending)) {
- if (ca->crlf_action == GIT_CRLF_GUESS && ca->auto_crlf)
- return GIT_PASSTHROUGH;
-
- if (git_buf_find(from, '\r') < 0)
- return GIT_PASSTHROUGH;
-
- if (git_buf_text_crlf_to_lf(to, from) < 0)
- return -1;
- } else {
- /* only other supported option is lf->crlf conversion */
- assert(!strcmp("\r\n", workdir_ending));
-
- if (git_buf_text_lf_to_crlf(to, from) < 0)
- return -1;
- }
+ /* only LF->CRLF conversion is supported, do nothing on LF platforms */
+ if (strcmp(workdir_ending, "\r\n") != 0)
+ return GIT_PASSTHROUGH;
- return 0;
+ return git_buf_text_lf_to_crlf(to, from);
}
static int crlf_check(
@@ -269,7 +256,9 @@ static int crlf_check(
if (ca.crlf_action == GIT_CRLF_BINARY)
return GIT_PASSTHROUGH;
- if (ca.crlf_action == GIT_CRLF_GUESS) {
+ if (ca.crlf_action == GIT_CRLF_GUESS ||
+ (ca.crlf_action == GIT_CRLF_AUTO &&
+ git_filter_source_mode(src) == GIT_FILTER_SMUDGE)) {
error = git_repository__cvar(
&ca.auto_crlf, git_filter_source_repo(src), GIT_CVAR_AUTO_CRLF);
if (error < 0)
@@ -277,6 +266,10 @@ static int crlf_check(
if (ca.auto_crlf == GIT_AUTO_CRLF_FALSE)
return GIT_PASSTHROUGH;
+
+ if (ca.auto_crlf == GIT_AUTO_CRLF_INPUT &&
+ git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
+ return GIT_PASSTHROUGH;
}
*payload = git__malloc(sizeof(ca));
diff --git a/src/diff_file.c b/src/diff_file.c
index fb5d674f7..7dabf8d6f 100644
--- a/src/diff_file.c
+++ b/src/diff_file.c
@@ -127,57 +127,38 @@ int git_diff_file_content__init_from_diff(
return diff_file_content_init_common(fc, &diff->opts);
}
-int git_diff_file_content__init_from_blob(
+int git_diff_file_content__init_from_src(
git_diff_file_content *fc,
git_repository *repo,
const git_diff_options *opts,
- const git_blob *blob,
+ const git_diff_file_content_src *src,
git_diff_file *as_file)
{
memset(fc, 0, sizeof(*fc));
fc->repo = repo;
fc->file = as_file;
- fc->blob = blob;
+ fc->blob = src->blob;
- if (!blob) {
+ if (!src->blob && !src->buf) {
fc->flags |= GIT_DIFF_FLAG__NO_DATA;
} else {
fc->flags |= GIT_DIFF_FLAG__LOADED;
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
- fc->file->size = git_blob_rawsize(blob);
fc->file->mode = GIT_FILEMODE_BLOB;
- git_oid_cpy(&fc->file->id, git_blob_id(blob));
- fc->map.len = (size_t)fc->file->size;
- fc->map.data = (char *)git_blob_rawcontent(blob);
- }
+ if (src->blob) {
+ fc->file->size = git_blob_rawsize(src->blob);
+ git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
- return diff_file_content_init_common(fc, opts);
-}
+ fc->map.len = (size_t)fc->file->size;
+ fc->map.data = (char *)git_blob_rawcontent(src->blob);
+ } else {
+ fc->file->size = src->buflen;
+ git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
-int git_diff_file_content__init_from_raw(
- git_diff_file_content *fc,
- git_repository *repo,
- const git_diff_options *opts,
- const char *buf,
- size_t buflen,
- git_diff_file *as_file)
-{
- memset(fc, 0, sizeof(*fc));
- fc->repo = repo;
- fc->file = as_file;
-
- if (!buf) {
- fc->flags |= GIT_DIFF_FLAG__NO_DATA;
- } else {
- fc->flags |= GIT_DIFF_FLAG__LOADED;
- fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
- fc->file->size = buflen;
- fc->file->mode = GIT_FILEMODE_BLOB;
- git_odb_hash(&fc->file->id, buf, buflen, GIT_OBJ_BLOB);
-
- fc->map.len = buflen;
- fc->map.data = (char *)buf;
+ fc->map.len = src->buflen;
+ fc->map.data = (char *)src->buf;
+ }
}
return diff_file_content_init_common(fc, opts);
diff --git a/src/diff_file.h b/src/diff_file.h
index 84bf255aa..4d290ad43 100644
--- a/src/diff_file.h
+++ b/src/diff_file.h
@@ -31,19 +31,21 @@ extern int git_diff_file_content__init_from_diff(
size_t delta_index,
bool use_old);
-extern int git_diff_file_content__init_from_blob(
- git_diff_file_content *fc,
- git_repository *repo,
- const git_diff_options *opts,
- const git_blob *blob,
- git_diff_file *as_file);
+typedef struct {
+ const git_blob *blob;
+ const void *buf;
+ size_t buflen;
+ const char *as_path;
+} git_diff_file_content_src;
+
+#define GIT_DIFF_FILE_CONTENT_SRC__BLOB(BLOB,PATH) { (BLOB),NULL,0,(PATH) }
+#define GIT_DIFF_FILE_CONTENT_SRC__BUF(BUF,LEN,PATH) { NULL,(BUF),(LEN),(PATH) }
-extern int git_diff_file_content__init_from_raw(
+extern int git_diff_file_content__init_from_src(
git_diff_file_content *fc,
git_repository *repo,
const git_diff_options *opts,
- const char *buf,
- size_t buflen,
+ const git_diff_file_content_src *src,
git_diff_file *as_file);
/* this loads the blob/file-on-disk as needed */
diff --git a/src/diff_patch.c b/src/diff_patch.c
index ecae3a8ed..dd8b73938 100644
--- a/src/diff_patch.c
+++ b/src/diff_patch.c
@@ -5,6 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
+#include "git2/blob.h"
#include "diff.h"
#include "diff_file.h"
#include "diff_driver.h"
@@ -334,38 +335,45 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
return error;
}
-static int diff_patch_from_blobs(
+static int diff_patch_from_sources(
diff_patch_with_delta *pd,
git_xdiff_output *xo,
- const git_blob *old_blob,
- const char *old_path,
- const git_blob *new_blob,
- const char *new_path,
+ git_diff_file_content_src *oldsrc,
+ git_diff_file_content_src *newsrc,
const git_diff_options *opts)
{
int error = 0;
git_repository *repo =
- new_blob ? git_object_owner((const git_object *)new_blob) :
- old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
+ oldsrc->blob ? git_blob_owner(oldsrc->blob) :
+ newsrc->blob ? git_blob_owner(newsrc->blob) : NULL;
+ git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
+ git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
- const git_blob *tmp_blob;
- const char *tmp_path;
- tmp_blob = old_blob; old_blob = new_blob; new_blob = tmp_blob;
- tmp_path = old_path; old_path = new_path; new_path = tmp_path;
+ void *tmp = lfile; lfile = rfile; rfile = tmp;
+ tmp = ldata; ldata = rdata; rdata = tmp;
}
pd->patch.delta = &pd->delta;
- pd->delta.old_file.path = old_path;
- pd->delta.new_file.path = new_path;
+ if (!oldsrc->as_path) {
+ if (newsrc->as_path)
+ oldsrc->as_path = newsrc->as_path;
+ else
+ oldsrc->as_path = newsrc->as_path = "file";
+ }
+ else if (!newsrc->as_path)
+ newsrc->as_path = oldsrc->as_path;
+
+ lfile->path = oldsrc->as_path;
+ rfile->path = newsrc->as_path;
- if ((error = git_diff_file_content__init_from_blob(
- &pd->patch.ofile, repo, opts, old_blob, &pd->delta.old_file)) < 0 ||
- (error = git_diff_file_content__init_from_blob(
- &pd->patch.nfile, repo, opts, new_blob, &pd->delta.new_file)) < 0)
+ if ((error = git_diff_file_content__init_from_src(
+ ldata, repo, opts, oldsrc, lfile)) < 0 ||
+ (error = git_diff_file_content__init_from_src(
+ rdata, repo, opts, newsrc, rfile)) < 0)
return error;
return diff_single_generate(pd, xo);
@@ -400,11 +408,9 @@ static int diff_patch_with_delta_alloc(
return 0;
}
-int git_diff_blobs(
- const git_blob *old_blob,
- const char *old_path,
- const git_blob *new_blob,
- const char *new_path,
+static int diff_from_sources(
+ git_diff_file_content_src *oldsrc,
+ git_diff_file_content_src *newsrc,
const git_diff_options *opts,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
@@ -420,26 +426,19 @@ int git_diff_blobs(
&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts);
- if (!old_path && new_path)
- old_path = new_path;
- else if (!new_path && old_path)
- new_path = old_path;
-
memset(&pd, 0, sizeof(pd));
- error = diff_patch_from_blobs(
- &pd, &xo, old_blob, old_path, new_blob, new_path, opts);
+
+ error = diff_patch_from_sources(&pd, &xo, oldsrc, newsrc, opts);
git_patch_free(&pd.patch);
return error;
}
-int git_patch_from_blobs(
+static int patch_from_sources(
git_patch **out,
- const git_blob *old_blob,
- const char *old_path,
- const git_blob *new_blob,
- const char *new_path,
+ git_diff_file_content_src *oldsrc,
+ git_diff_file_content_src *newsrc,
const git_diff_options *opts)
{
int error = 0;
@@ -449,17 +448,15 @@ int git_patch_from_blobs(
assert(out);
*out = NULL;
- if (diff_patch_with_delta_alloc(&pd, &old_path, &new_path) < 0)
- return -1;
+ if ((error = diff_patch_with_delta_alloc(
+ &pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
+ return error;
memset(&xo, 0, sizeof(xo));
diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts);
- error = diff_patch_from_blobs(
- pd, &xo, old_blob, old_path, new_blob, new_path, opts);
-
- if (!error)
+ if (!(error = diff_patch_from_sources(pd, &xo, oldsrc, newsrc, opts)))
*out = (git_patch *)pd;
else
git_patch_free((git_patch *)pd);
@@ -467,46 +464,38 @@ int git_patch_from_blobs(
return error;
}
-static int diff_patch_from_blob_and_buffer(
- diff_patch_with_delta *pd,
- git_xdiff_output *xo,
+int git_diff_blobs(
const git_blob *old_blob,
const char *old_path,
- const char *buf,
- size_t buflen,
- const char *buf_path,
- const git_diff_options *opts)
+ const git_blob *new_blob,
+ const char *new_path,
+ const git_diff_options *opts,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_line_cb data_cb,
+ void *payload)
{
- int error = 0;
- git_repository *repo =
- old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
-
- GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
-
- pd->patch.delta = &pd->delta;
-
- if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
- pd->delta.old_file.path = buf_path;
- pd->delta.new_file.path = old_path;
-
- if (!(error = git_diff_file_content__init_from_raw(
- &pd->patch.ofile, repo, opts, buf, buflen, &pd->delta.old_file)))
- error = git_diff_file_content__init_from_blob(
- &pd->patch.nfile, repo, opts, old_blob, &pd->delta.new_file);
- } else {
- pd->delta.old_file.path = old_path;
- pd->delta.new_file.path = buf_path;
-
- if (!(error = git_diff_file_content__init_from_blob(
- &pd->patch.ofile, repo, opts, old_blob, &pd->delta.old_file)))
- error = git_diff_file_content__init_from_raw(
- &pd->patch.nfile, repo, opts, buf, buflen, &pd->delta.new_file);
- }
-
- if (error < 0)
- return error;
+ git_diff_file_content_src osrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
+ git_diff_file_content_src nsrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
+ return diff_from_sources(
+ &osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
+}
- return diff_single_generate(pd, xo);
+int git_patch_from_blobs(
+ git_patch **out,
+ const git_blob *old_blob,
+ const char *old_path,
+ const git_blob *new_blob,
+ const char *new_path,
+ const git_diff_options *opts)
+{
+ git_diff_file_content_src osrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
+ git_diff_file_content_src nsrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
+ return patch_from_sources(out, &osrc, &nsrc, opts);
}
int git_diff_blob_to_buffer(
@@ -521,27 +510,12 @@ int git_diff_blob_to_buffer(
git_diff_line_cb data_cb,
void *payload)
{
- int error = 0;
- diff_patch_with_delta pd;
- git_xdiff_output xo;
-
- memset(&xo, 0, sizeof(xo));
- diff_output_init(
- &xo.output, opts, file_cb, hunk_cb, data_cb, payload);
- git_xdiff_init(&xo, opts);
-
- if (!old_path && buf_path)
- old_path = buf_path;
- else if (!buf_path && old_path)
- buf_path = old_path;
-
- memset(&pd, 0, sizeof(pd));
- error = diff_patch_from_blob_and_buffer(
- &pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
-
- git_patch_free(&pd.patch);
-
- return error;
+ git_diff_file_content_src osrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
+ git_diff_file_content_src nsrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
+ return diff_from_sources(
+ &osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
}
int git_patch_from_blob_and_buffer(
@@ -553,29 +527,49 @@ int git_patch_from_blob_and_buffer(
const char *buf_path,
const git_diff_options *opts)
{
- int error = 0;
- diff_patch_with_delta *pd;
- git_xdiff_output xo;
-
- assert(out);
- *out = NULL;
-
- if (diff_patch_with_delta_alloc(&pd, &old_path, &buf_path) < 0)
- return -1;
-
- memset(&xo, 0, sizeof(xo));
- diff_output_to_patch(&xo.output, &pd->patch);
- git_xdiff_init(&xo, opts);
-
- error = diff_patch_from_blob_and_buffer(
- pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
+ git_diff_file_content_src osrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
+ git_diff_file_content_src nsrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
+ return patch_from_sources(out, &osrc, &nsrc, opts);
+}
- if (!error)
- *out = (git_patch *)pd;
- else
- git_patch_free((git_patch *)pd);
+int git_diff_buffers(
+ const void *old_buf,
+ size_t old_len,
+ const char *old_path,
+ const void *new_buf,
+ size_t new_len,
+ const char *new_path,
+ const git_diff_options *opts,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_line_cb data_cb,
+ void *payload)
+{
+ git_diff_file_content_src osrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
+ git_diff_file_content_src nsrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
+ return diff_from_sources(
+ &osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
+}
- return error;
+int git_patch_from_buffers(
+ git_patch **out,
+ const void *old_buf,
+ size_t old_len,
+ const char *old_path,
+ const char *new_buf,
+ size_t new_len,
+ const char *new_path,
+ const git_diff_options *opts)
+{
+ git_diff_file_content_src osrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
+ git_diff_file_content_src nsrc =
+ GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
+ return patch_from_sources(out, &osrc, &nsrc, opts);
}
int git_patch_from_diff(
diff --git a/src/diff_tform.c b/src/diff_tform.c
index dfb59a3f8..97fbc2883 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -218,7 +218,7 @@ int git_diff_merge(git_diff *onto, const git_diff *from)
int git_diff_find_similar__hashsig_for_file(
void **out, const git_diff_file *f, const char *path, void *p)
{
- git_hashsig_option_t opt = (git_hashsig_option_t)p;
+ git_hashsig_option_t opt = (git_hashsig_option_t)(intptr_t)p;
int error = 0;
GIT_UNUSED(f);
@@ -235,7 +235,7 @@ int git_diff_find_similar__hashsig_for_file(
int git_diff_find_similar__hashsig_for_buf(
void **out, const git_diff_file *f, const char *buf, size_t len, void *p)
{
- git_hashsig_option_t opt = (git_hashsig_option_t)p;
+ git_hashsig_option_t opt = (git_hashsig_option_t)(intptr_t)p;
int error = 0;
GIT_UNUSED(f);
diff --git a/src/errors.c b/src/errors.c
index a0b085923..393a7875f 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -8,7 +8,6 @@
#include "global.h"
#include "posix.h"
#include "buffer.h"
-#include <stdarg.h>
/********************************************
* New error handling
diff --git a/src/filebuf.c b/src/filebuf.c
index 9c3dae811..d23bcc11c 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -4,8 +4,6 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
-#include <stdarg.h>
-
#include "common.h"
#include "filebuf.h"
#include "fileops.h"
diff --git a/src/fileops.c b/src/fileops.c
index a60689f3f..5709499b0 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -558,226 +558,6 @@ int git_futils_rmdir_r(
return error;
}
-
-static int git_futils_guess_system_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
- return git_win32__find_system_dirs(out, L"etc\\");
-#else
- return git_buf_sets(out, "/etc");
-#endif
-}
-
-static int git_futils_guess_global_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
- return git_win32__find_global_dirs(out);
-#else
- return git_buf_sets(out, getenv("HOME"));
-#endif
-}
-
-static int git_futils_guess_xdg_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
- return git_win32__find_xdg_dirs(out);
-#else
- const char *env = NULL;
-
- if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
- return git_buf_joinpath(out, env, "git");
- else if ((env = getenv("HOME")) != NULL)
- return git_buf_joinpath(out, env, ".config/git");
-
- git_buf_clear(out);
- return 0;
-#endif
-}
-
-static int git_futils_guess_template_dirs(git_buf *out)
-{
-#ifdef GIT_WIN32
- return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
-#else
- return git_buf_sets(out, "/usr/share/git-core/templates");
-#endif
-}
-
-typedef int (*git_futils_dirs_guess_cb)(git_buf *out);
-
-static git_buf git_futils__dirs[GIT_FUTILS_DIR__MAX] =
- { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
-
-static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = {
- git_futils_guess_system_dirs,
- git_futils_guess_global_dirs,
- git_futils_guess_xdg_dirs,
- git_futils_guess_template_dirs,
-};
-
-static int git_futils__dirs_shutdown_set = 0;
-
-void git_futils_dirs_global_shutdown(void)
-{
- int i;
- for (i = 0; i < GIT_FUTILS_DIR__MAX; ++i)
- git_buf_free(&git_futils__dirs[i]);
-}
-
-int git_futils_dirs_global_init(void)
-{
- git_futils_dir_t i;
- const git_buf *path;
- int error = 0;
-
- for (i = 0; !error && i < GIT_FUTILS_DIR__MAX; i++)
- error = git_futils_dirs_get(&path, i);
-
- return error;
-}
-
-static int git_futils_check_selector(git_futils_dir_t which)
-{
- if (which < GIT_FUTILS_DIR__MAX)
- return 0;
- giterr_set(GITERR_INVALID, "config directory selector out of range");
- return -1;
-}
-
-int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which)
-{
- assert(out);
-
- *out = NULL;
-
- GITERR_CHECK_ERROR(git_futils_check_selector(which));
-
- if (!git_buf_len(&git_futils__dirs[which])) {
- /* prepare shutdown if we're going to need it */
- if (!git_futils__dirs_shutdown_set) {
- git__on_shutdown(git_futils_dirs_global_shutdown);
- git_futils__dirs_shutdown_set = 1;
- }
-
- GITERR_CHECK_ERROR(
- git_futils__dir_guess[which](&git_futils__dirs[which]));
- }
-
- *out = &git_futils__dirs[which];
- return 0;
-}
-
-int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which)
-{
- const git_buf *path = NULL;
-
- GITERR_CHECK_ERROR(git_futils_check_selector(which));
- GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which));
-
- if (!out || path->size >= outlen) {
- giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
- return GIT_EBUFS;
- }
-
- git_buf_copy_cstr(out, outlen, path);
- return 0;
-}
-
-#define PATH_MAGIC "$PATH"
-
-int git_futils_dirs_set(git_futils_dir_t which, const char *search_path)
-{
- const char *expand_path = NULL;
- git_buf merge = GIT_BUF_INIT;
-
- GITERR_CHECK_ERROR(git_futils_check_selector(which));
-
- if (search_path != NULL)
- expand_path = strstr(search_path, PATH_MAGIC);
-
- /* init with default if not yet done and needed (ignoring error) */
- if ((!search_path || expand_path) &&
- !git_buf_len(&git_futils__dirs[which]))
- git_futils__dir_guess[which](&git_futils__dirs[which]);
-
- /* if $PATH is not referenced, then just set the path */
- if (!expand_path)
- return git_buf_sets(&git_futils__dirs[which], search_path);
-
- /* otherwise set to join(before $PATH, old value, after $PATH) */
- if (expand_path > search_path)
- git_buf_set(&merge, search_path, expand_path - search_path);
-
- if (git_buf_len(&git_futils__dirs[which]))
- git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
- merge.ptr, git_futils__dirs[which].ptr);
-
- expand_path += strlen(PATH_MAGIC);
- if (*expand_path)
- git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
-
- git_buf_swap(&git_futils__dirs[which], &merge);
- git_buf_free(&merge);
-
- return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0;
-}
-
-static int git_futils_find_in_dirlist(
- git_buf *path, const char *name, git_futils_dir_t which, const char *label)
-{
- size_t len;
- const char *scan, *next = NULL;
- const git_buf *syspath;
-
- GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which));
-
- for (scan = git_buf_cstr(syspath); scan; scan = next) {
- for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR);
- next && next > scan && next[-1] == '\\';
- next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR))
- /* find unescaped separator or end of string */;
-
- len = next ? (size_t)(next++ - scan) : strlen(scan);
- if (!len)
- continue;
-
- GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
- if (name)
- GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
-
- if (git_path_exists(path->ptr))
- return 0;
- }
-
- git_buf_clear(path);
- giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name);
- return GIT_ENOTFOUND;
-}
-
-int git_futils_find_system_file(git_buf *path, const char *filename)
-{
- return git_futils_find_in_dirlist(
- path, filename, GIT_FUTILS_DIR_SYSTEM, "system");
-}
-
-int git_futils_find_global_file(git_buf *path, const char *filename)
-{
- return git_futils_find_in_dirlist(
- path, filename, GIT_FUTILS_DIR_GLOBAL, "global");
-}
-
-int git_futils_find_xdg_file(git_buf *path, const char *filename)
-{
- return git_futils_find_in_dirlist(
- path, filename, GIT_FUTILS_DIR_XDG, "global/xdg");
-}
-
-int git_futils_find_template_dir(git_buf *path)
-{
- return git_futils_find_in_dirlist(
- path, NULL, GIT_FUTILS_DIR_TEMPLATE, "template");
-}
-
int git_futils_fake_symlink(const char *old, const char *new)
{
int retcode = GIT_ERROR;
diff --git a/src/fileops.h b/src/fileops.h
index 636c9b67d..6a65235de 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -268,89 +268,6 @@ extern int git_futils_mmap_ro_file(
extern void git_futils_mmap_free(git_map *map);
/**
- * Find a "global" file (i.e. one in a user's home directory).
- *
- * @param path buffer to write the full path into
- * @param filename name of file to find in the home directory
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_global_file(git_buf *path, const char *filename);
-
-/**
- * Find an "XDG" file (i.e. one in user's XDG config path).
- *
- * @param path buffer to write the full path into
- * @param filename name of file to find in the home directory
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_xdg_file(git_buf *path, const char *filename);
-
-/**
- * Find a "system" file (i.e. one shared for all users of the system).
- *
- * @param path buffer to write the full path into
- * @param filename name of file to find in the home directory
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_system_file(git_buf *path, const char *filename);
-
-/**
- * Find template directory.
- *
- * @param path buffer to write the full path into
- * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
- */
-extern int git_futils_find_template_dir(git_buf *path);
-
-typedef enum {
- GIT_FUTILS_DIR_SYSTEM = 0,
- GIT_FUTILS_DIR_GLOBAL = 1,
- GIT_FUTILS_DIR_XDG = 2,
- GIT_FUTILS_DIR_TEMPLATE = 3,
- GIT_FUTILS_DIR__MAX = 4,
-} git_futils_dir_t;
-
-/**
- * Configures global data for configuration file search paths.
- *
- * @return 0 on success, <0 on failure
- */
-extern int git_futils_dirs_global_init(void);
-
-/**
- * Get the search path for global/system/xdg files
- *
- * @param out pointer to git_buf containing search path
- * @param which which list of paths to return
- * @return 0 on success, <0 on failure
- */
-extern int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which);
-
-/**
- * Get search path into a preallocated buffer
- *
- * @param out String buffer to write into
- * @param outlen Size of string buffer
- * @param which Which search path to return
- * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure
- */
-
-extern int git_futils_dirs_get_str(
- char *out, size_t outlen, git_futils_dir_t which);
-
-/**
- * Set search paths for global/system/xdg files
- *
- * The first occurrence of the magic string "$PATH" in the new value will
- * be replaced with the old value of the search path.
- *
- * @param which Which search path to modify
- * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR)
- * @return 0 on success, <0 on failure (allocation error)
- */
-extern int git_futils_dirs_set(git_futils_dir_t which, const char *paths);
-
-/**
* Create a "fake" symlink (text file containing the target path).
*
* @param new symlink file to be created
@@ -399,9 +316,4 @@ extern int git_futils_filestamp_check(
extern void git_futils_filestamp_set(
git_futils_filestamp *tgt, const git_futils_filestamp *src);
-/**
- * Free the configuration file search paths.
- */
-extern void git_futils_dirs_global_shutdown(void);
-
#endif /* INCLUDE_fileops_h__ */
diff --git a/src/global.c b/src/global.c
index 7d39c6fa8..8c8f55a90 100644
--- a/src/global.c
+++ b/src/global.c
@@ -7,7 +7,7 @@
#include "common.h"
#include "global.h"
#include "hash.h"
-#include "fileops.h"
+#include "sysdir.h"
#include "git2/threads.h"
#include "thread-utils.h"
@@ -86,7 +86,7 @@ static int synchronized_threads_init()
/* Initialize any other subsystems that have global state */
if ((error = git_hash_global_init()) >= 0)
- error = git_futils_dirs_global_init();
+ error = git_sysdir_global_init();
win32_pthread_initialize();
@@ -169,7 +169,7 @@ static void init_once(void)
/* Initialize any other subsystems that have global state */
if ((init_error = git_hash_global_init()) >= 0)
- init_error = git_futils_dirs_global_init();
+ init_error = git_sysdir_global_init();
GIT_MEMORY_BARRIER;
}
diff --git a/src/graph.c b/src/graph.c
index f39af5ed5..96fda7add 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -1,4 +1,3 @@
-
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
@@ -13,9 +12,9 @@
static int interesting(git_pqueue *list, git_commit_list *roots)
{
unsigned int i;
- /* element 0 isn't used - we need to start at 1 */
- for (i = 1; i < list->size; i++) {
- git_commit_list_node *commit = list->d[i];
+
+ for (i = 0; i < git_pqueue_size(list); i++) {
+ git_commit_list_node *commit = git_pqueue_get(list, i);
if ((commit->flags & STALE) == 0)
return 1;
}
@@ -42,7 +41,7 @@ static int mark_parents(git_revwalk *walk, git_commit_list_node *one,
return 0;
}
- if (git_pqueue_init(&list, 2, git_commit_list_time_cmp) < 0)
+ if (git_pqueue_init(&list, 0, 2, git_commit_list_time_cmp) < 0)
return -1;
if (git_commit_list_parse(walk, one) < 0)
@@ -59,10 +58,9 @@ static int mark_parents(git_revwalk *walk, git_commit_list_node *one,
/* as long as there are non-STALE commits */
while (interesting(&list, roots)) {
- git_commit_list_node *commit;
+ git_commit_list_node *commit = git_pqueue_pop(&list);
int flags;
- commit = git_pqueue_pop(&list);
if (commit == NULL)
break;
@@ -110,16 +108,16 @@ static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
{
git_commit_list_node *commit;
git_pqueue pq;
- int i;
+ int error = 0, i;
*ahead = 0;
*behind = 0;
- if (git_pqueue_init(&pq, 2, git_commit_list_time_cmp) < 0)
+ if (git_pqueue_init(&pq, 0, 2, git_commit_list_time_cmp) < 0)
return -1;
- if (git_pqueue_insert(&pq, one) < 0)
- goto on_error;
- if (git_pqueue_insert(&pq, two) < 0)
- goto on_error;
+
+ if ((error = git_pqueue_insert(&pq, one)) < 0 ||
+ (error = git_pqueue_insert(&pq, two)) < 0)
+ goto done;
while ((commit = git_pqueue_pop(&pq)) != NULL) {
if (commit->flags & RESULT ||
@@ -132,18 +130,15 @@ static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
for (i = 0; i < commit->out_degree; i++) {
git_commit_list_node *p = commit->parents[i];
- if (git_pqueue_insert(&pq, p) < 0)
- return -1;
+ if ((error = git_pqueue_insert(&pq, p)) < 0)
+ goto done;
}
commit->flags |= RESULT;
}
+done:
git_pqueue_free(&pq);
- return 0;
-
-on_error:
- git_pqueue_free(&pq);
- return -1;
+ return error;
}
int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo,
diff --git a/src/index.c b/src/index.c
index 7bc5d5b24..2de3dfebb 100644
--- a/src/index.c
+++ b/src/index.c
@@ -90,7 +90,7 @@ struct entry_long {
struct entry_srch_key {
const char *path;
- int path_len;
+ size_t path_len;
int stage;
};
@@ -110,7 +110,8 @@ static int index_srch(const void *key, const void *array_member)
{
const struct entry_srch_key *srch_key = key;
const git_index_entry *entry = array_member;
- int cmp, len1, len2, len;
+ int cmp;
+ size_t len1, len2, len;
len1 = srch_key->path_len;
len2 = strlen(entry->path);
@@ -134,7 +135,8 @@ static int index_isrch(const void *key, const void *array_member)
{
const struct entry_srch_key *srch_key = key;
const git_index_entry *entry = array_member;
- int cmp, len1, len2, len;
+ int cmp;
+ size_t len1, len2, len;
len1 = srch_key->path_len;
len2 = strlen(entry->path);
@@ -436,7 +438,7 @@ static int create_index_error(int error, const char *msg)
return error;
}
-int git_index_set_caps(git_index *index, unsigned int caps)
+int git_index_set_caps(git_index *index, int caps)
{
unsigned int old_ignore_case;
@@ -472,7 +474,7 @@ int git_index_set_caps(git_index *index, unsigned int caps)
return 0;
}
-unsigned int git_index_caps(const git_index *index)
+int git_index_caps(const git_index *index)
{
return ((index->ignore_case ? GIT_INDEXCAP_IGNORE_CASE : 0) |
(index->distrust_filemode ? GIT_INDEXCAP_NO_FILEMODE : 0) |
@@ -599,9 +601,7 @@ const git_index_entry *git_index_get_bypath(
assert(index);
- git_vector_sort(&index->entries);
-
- if (git_index__find(&pos, index, path, strlen(path), stage) < 0) {
+ if (git_index__find(&pos, index, path, 0, stage) < 0) {
giterr_set(GITERR_INDEX, "Index does not contain %s", path);
return NULL;
}
@@ -837,8 +837,7 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
/* look if an entry with this path already exists */
if (!git_index__find(
- &position, index, entry->path, strlen(entry->path),
- GIT_IDXENTRY_STAGE(entry))) {
+ &position, index, entry->path, 0, GIT_IDXENTRY_STAGE(entry))) {
existing = (git_index_entry **)&index->entries.contents[position];
/* update filemode to existing values if stat is not trusted */
entry->mode = index_merge_mode(index, *existing, entry->mode);
@@ -950,9 +949,7 @@ int git_index_remove(git_index *index, const char *path, int stage)
int error;
git_index_entry *entry;
- git_vector_sort(&index->entries);
-
- if (git_index__find(&position, index, path, strlen(path), stage) < 0) {
+ if (git_index__find(&position, index, path, 0, stage) < 0) {
giterr_set(GITERR_INDEX, "Index does not contain %s at stage %d",
path, stage);
return GIT_ENOTFOUND;
@@ -1009,18 +1006,20 @@ int git_index_remove_directory(git_index *index, const char *dir, int stage)
}
int git_index__find(
- size_t *at_pos, git_index *index, const char *path, int path_len, int stage)
+ size_t *out, git_index *index, const char *path, size_t path_len, int stage)
{
struct entry_srch_key srch_key;
assert(path);
+ git_vector_sort(&index->entries);
+
srch_key.path = path;
- srch_key.path_len = path_len;
+ srch_key.path_len = !path_len ? strlen(path) : path_len;
srch_key.stage = stage;
return git_vector_bsearch2(
- at_pos, &index->entries, index->entries_search, &srch_key);
+ out, &index->entries, index->entries_search, &srch_key);
}
int git_index_find(size_t *at_pos, git_index *index, const char *path)
@@ -1564,7 +1563,7 @@ static int read_reuc(git_index *index, const char *buffer, size_t size)
}
/* entries are guaranteed to be sorted on-disk */
- index->reuc.sorted = 1;
+ git_vector_set_sorted(&index->reuc, true);
return 0;
}
@@ -1610,7 +1609,7 @@ static int read_conflict_names(git_index *index, const char *buffer, size_t size
#undef read_conflict_name
/* entries are guaranteed to be sorted on-disk */
- index->names.sorted = 1;
+ git_vector_set_sorted(&index->names, true);
return 0;
}
@@ -1811,8 +1810,10 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
#undef seek_forward
- /* Entries are stored case-sensitively on disk. */
- index->entries.sorted = !index->ignore_case;
+ /* Entries are stored case-sensitively on disk, so re-sort now if
+ * in-memory index is supposed to be case-insensitive
+ */
+ git_vector_set_sorted(&index->entries, !index->ignore_case);
git_vector_sort(&index->entries);
return 0;
@@ -2045,13 +2046,15 @@ static int write_index(git_index *index, git_filebuf *file)
git_oid hash_final;
struct index_header header;
bool is_extended;
+ uint32_t index_version_number;
assert(index && file);
is_extended = is_index_extended(index);
+ index_version_number = is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER;
header.signature = htonl(INDEX_HEADER_SIG);
- header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER);
+ header.version = htonl(index_version_number);
header.entry_count = htonl((uint32_t)index->entries.length);
if (git_filebuf_write(file, &header, sizeof(struct index_header)) < 0)
@@ -2232,7 +2235,7 @@ int git_index_add_all(
/* skip ignored items that are not already in the index */
if ((flags & GIT_INDEX_ADD_FORCE) == 0 &&
git_iterator_current_is_ignored(wditer) &&
- git_index__find(&existing, index, wd->path, strlen(wd->path), 0) < 0)
+ git_index__find(&existing, index, wd->path, 0, 0) < 0)
continue;
/* issue notification callback if requested */
diff --git a/src/index.h b/src/index.h
index 3dea4aa14..f88d110f7 100644
--- a/src/index.h
+++ b/src/index.h
@@ -56,7 +56,7 @@ extern int git_index_entry__cmp(const void *a, const void *b);
extern int git_index_entry__cmp_icase(const void *a, const void *b);
extern int git_index__find(
- size_t *at_pos, git_index *index, const char *path, int path_len, int stage);
+ size_t *at_pos, git_index *index, const char *path, size_t path_len, int stage);
extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
diff --git a/src/merge.c b/src/merge.c
index 20cfc0e23..12ff1c91c 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -161,10 +161,10 @@ on_error:
static int interesting(git_pqueue *list)
{
- unsigned int i;
- /* element 0 isn't used - we need to start at 1 */
- for (i = 1; i < list->size; i++) {
- git_commit_list_node *commit = list->d[i];
+ size_t i;
+
+ for (i = 0; i < git_pqueue_size(list); i++) {
+ git_commit_list_node *commit = git_pqueue_get(list, i);
if ((commit->flags & STALE) == 0)
return 1;
}
@@ -186,7 +186,7 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
return git_commit_list_insert(one, out) ? 0 : -1;
}
- if (git_pqueue_init(&list, twos->length * 2, git_commit_list_time_cmp) < 0)
+ if (git_pqueue_init(&list, 0, twos->length * 2, git_commit_list_time_cmp) < 0)
return -1;
if (git_commit_list_parse(walk, one) < 0)
@@ -205,10 +205,11 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
/* as long as there are non-STALE commits */
while (interesting(&list)) {
- git_commit_list_node *commit;
+ git_commit_list_node *commit = git_pqueue_pop(&list);
int flags;
- commit = git_pqueue_pop(&list);
+ if (commit == NULL)
+ break;
flags = commit->flags & (PARENT1 | PARENT2 | STALE);
if (flags == (PARENT1 | PARENT2)) {
@@ -2330,7 +2331,7 @@ done:
static int merge_check_workdir(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths)
{
- git_tree *head_tree = NULL;
+ git_index *index_repo = NULL;
git_diff *wd_diff_list = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
int error = 0;
@@ -2341,9 +2342,6 @@ static int merge_check_workdir(size_t *conflicts, git_repository *repo, git_inde
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
- if ((error = git_repository_head_tree(&head_tree, repo)) < 0)
- goto done;
-
/* Workdir changes may exist iff they do not conflict with changes that
* will be applied by the merge (including conflicts). Ensure that there
* are no changes in the workdir to these paths.
@@ -2351,13 +2349,13 @@ static int merge_check_workdir(size_t *conflicts, git_repository *repo, git_inde
opts.pathspec.count = merged_paths->length;
opts.pathspec.strings = (char **)merged_paths->contents;
- if ((error = git_diff_tree_to_workdir(&wd_diff_list, repo, head_tree, &opts)) < 0)
+ if ((error = git_diff_index_to_workdir(&wd_diff_list, repo, index_repo, &opts)) < 0)
goto done;
*conflicts = wd_diff_list->deltas.length;
done:
- git_tree_free(head_tree);
+ git_index_free(index_repo);
git_diff_free(wd_diff_list);
return error;
@@ -2366,7 +2364,7 @@ done:
int git_merge__indexes(git_repository *repo, git_index *index_new)
{
git_index *index_repo = NULL;
- unsigned int index_repo_caps = 0;
+ int index_repo_caps = 0;
git_vector paths = GIT_VECTOR_INIT;
size_t index_conflicts = 0, wd_conflicts = 0, conflicts, i;
char *path;
@@ -2400,7 +2398,7 @@ int git_merge__indexes(git_repository *repo, git_index *index_new)
/* Remove removed items from the index */
git_vector_foreach(&paths, i, path) {
- if ((e = git_index_get_bypath(index_new, path, 0)) == NULL) {
+ if (git_index_get_bypath(index_new, path, 0) == NULL) {
if ((error = git_index_remove(index_repo, path, 0)) < 0 &&
error != GIT_ENOTFOUND)
goto done;
diff --git a/src/object.c b/src/object.c
index 3fc984b45..40cae184f 100644
--- a/src/object.c
+++ b/src/object.c
@@ -4,8 +4,6 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
-#include <stdarg.h>
-
#include "git2/object.h"
#include "common.h"
diff --git a/src/oid.c b/src/oid.c
index 567b6cf06..f74c43fe2 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -179,6 +179,11 @@ int git_oid_cmp(const git_oid *a, const git_oid *b)
return git_oid__cmp(a, b);
}
+int git_oid_equal(const git_oid *a, const git_oid *b)
+{
+ return (git_oid__cmp(a, b) == 0);
+}
+
int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
{
const unsigned char *a = oid_a->id;
diff --git a/src/path.c b/src/path.c
index feb273915..89409eae4 100644
--- a/src/path.c
+++ b/src/path.c
@@ -12,7 +12,6 @@
#else
#include <dirent.h>
#endif
-#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
diff --git a/src/pathspec.c b/src/pathspec.c
index bee320576..471488495 100644
--- a/src/pathspec.c
+++ b/src/pathspec.c
@@ -445,7 +445,7 @@ static int pathspec_match_from_iterator(
/* check if path is ignored and untracked */
if (index != NULL &&
git_iterator_current_is_ignored(iter) &&
- git_index__find(NULL, index, entry->path, strlen(entry->path), GIT_INDEX_STAGE_ANY) < 0)
+ git_index__find(NULL, index, entry->path, 0, GIT_INDEX_STAGE_ANY) < 0)
continue;
/* mark the matched pattern as used */
diff --git a/src/posix.c b/src/posix.c
index 525785f35..7b2962feb 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -189,7 +189,7 @@ int p_write(git_file fd, const void *buf, size_t cnt)
r = write(fd, b, cnt);
#endif
if (r < 0) {
- if (errno == EINTR || errno == EAGAIN)
+ if (errno == EINTR || GIT_ISBLOCKED(errno))
continue;
return -1;
}
diff --git a/src/posix.h b/src/posix.h
index 0d9be49a9..f85b1aebd 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -29,6 +29,15 @@
#define O_CLOEXEC 0
#endif
+/* Determine whether an errno value indicates that a read or write failed
+ * because the descriptor is blocked.
+ */
+#if defined(EWOULDBLOCK)
+#define GIT_ISBLOCKED(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
+#else
+#define GIT_ISBLOCKED(e) ((e) == EAGAIN)
+#endif
+
typedef int git_file;
/**
@@ -89,18 +98,7 @@ extern struct tm * p_gmtime_r (const time_t *timer, struct tm *result);
# include "unix/posix.h"
#endif
-#if defined(__MINGW32__) || defined(__sun) || defined(__APPLE__)
-# define NO_STRNLEN
-#endif
-
-#ifdef NO_STRNLEN
-GIT_INLINE(size_t) p_strnlen(const char *s, size_t maxlen) {
- const char *end = memchr(s, 0, maxlen);
- return end ? (size_t)(end - s) : maxlen;
-}
-#else
-# define p_strnlen strnlen
-#endif
+#include "strnlen.h"
#ifdef NO_READDIR_R
# include <dirent.h>
diff --git a/src/pqueue.c b/src/pqueue.c
index 7819ed41e..172cf43d5 100644
--- a/src/pqueue.c
+++ b/src/pqueue.c
@@ -3,161 +3,115 @@
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
- *
- * This file is based on a modified version of the priority queue found
- * in the Apache project and libpqueue library.
- *
- * https://github.com/vy/libpqueue
- *
- * Original file notice:
- *
- * Copyright 2010 Volkan Yazici <volkan.yazici@gmail.com>
- * Copyright 2006-2010 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
*/
-#include "common.h"
#include "pqueue.h"
+#include "util.h"
-#define left(i) ((i) << 1)
-#define right(i) (((i) << 1) + 1)
-#define parent(i) ((i) >> 1)
+#define PQUEUE_LCHILD_OF(I) (((I)<<1)+1)
+#define PQUEUE_RCHILD_OF(I) (((I)<<1)+2)
+#define PQUEUE_PARENT_OF(I) (((I)-1)>>1)
-int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri)
+int git_pqueue_init(
+ git_pqueue *pq,
+ uint32_t flags,
+ size_t init_size,
+ git_vector_cmp cmp)
{
- assert(q);
+ int error = git_vector_init(pq, init_size, cmp);
- /* Need to allocate n+1 elements since element 0 isn't used. */
- q->d = git__malloc((n + 1) * sizeof(void *));
- GITERR_CHECK_ALLOC(q->d);
+ if (!error) {
+ /* mix in our flags */
+ pq->flags |= flags;
- q->size = 1;
- q->avail = q->step = (n + 1); /* see comment above about n+1 */
- q->cmppri = cmppri;
+ /* if fixed size heap, pretend vector is exactly init_size elements */
+ if ((flags & GIT_PQUEUE_FIXED_SIZE) && init_size > 0)
+ pq->_alloc_size = init_size;
+ }
- return 0;
+ return error;
}
-
-void git_pqueue_free(git_pqueue *q)
+static void pqueue_up(git_pqueue *pq, size_t el)
{
- git__free(q->d);
- q->d = NULL;
-}
+ size_t parent_el = PQUEUE_PARENT_OF(el);
+ void *kid = git_vector_get(pq, el);
-void git_pqueue_clear(git_pqueue *q)
-{
- q->size = 1;
-}
+ while (el > 0) {
+ void *parent = pq->contents[parent_el];
-size_t git_pqueue_size(git_pqueue *q)
-{
- /* queue element 0 exists but doesn't count since it isn't used. */
- return (q->size - 1);
-}
+ if (pq->_cmp(parent, kid) <= 0)
+ break;
+ pq->contents[el] = parent;
-static void bubble_up(git_pqueue *q, size_t i)
-{
- size_t parent_node;
- void *moving_node = q->d[i];
-
- for (parent_node = parent(i);
- ((i > 1) && q->cmppri(q->d[parent_node], moving_node));
- i = parent_node, parent_node = parent(i)) {
- q->d[i] = q->d[parent_node];
+ el = parent_el;
+ parent_el = PQUEUE_PARENT_OF(el);
}
- q->d[i] = moving_node;
+ pq->contents[el] = kid;
}
-
-static size_t maxchild(git_pqueue *q, size_t i)
+static void pqueue_down(git_pqueue *pq, size_t el)
{
- size_t child_node = left(i);
+ void *parent = git_vector_get(pq, el), *kid, *rkid;
- if (child_node >= q->size)
- return 0;
+ while (1) {
+ size_t kid_el = PQUEUE_LCHILD_OF(el);
- if ((child_node + 1) < q->size &&
- q->cmppri(q->d[child_node], q->d[child_node + 1]))
- child_node++; /* use right child instead of left */
+ if ((kid = git_vector_get(pq, kid_el)) == NULL)
+ break;
- return child_node;
-}
+ if ((rkid = git_vector_get(pq, kid_el + 1)) != NULL &&
+ pq->_cmp(kid, rkid) > 0) {
+ kid = rkid;
+ kid_el += 1;
+ }
+ if (pq->_cmp(parent, kid) < 0)
+ break;
-static void percolate_down(git_pqueue *q, size_t i)
-{
- size_t child_node;
- void *moving_node = q->d[i];
-
- while ((child_node = maxchild(q, i)) != 0 &&
- q->cmppri(moving_node, q->d[child_node])) {
- q->d[i] = q->d[child_node];
- i = child_node;
+ pq->contents[el] = kid;
+ el = kid_el;
}
- q->d[i] = moving_node;
+ pq->contents[el] = parent;
}
-
-int git_pqueue_insert(git_pqueue *q, void *d)
+int git_pqueue_insert(git_pqueue *pq, void *item)
{
- void *tmp;
- size_t i;
- size_t newsize;
-
- if (!q) return 1;
-
- /* allocate more memory if necessary */
- if (q->size >= q->avail) {
- newsize = q->size + q->step;
- tmp = git__realloc(q->d, sizeof(void *) * newsize);
- GITERR_CHECK_ALLOC(tmp);
-
- q->d = tmp;
- q->avail = newsize;
+ int error = 0;
+
+ /* if heap is full, pop the top element if new one should replace it */
+ if ((pq->flags & GIT_PQUEUE_FIXED_SIZE) != 0 &&
+ pq->length >= pq->_alloc_size)
+ {
+ /* skip this item if below min item in heap */
+ if (pq->_cmp(item, git_vector_get(pq, 0)) <= 0)
+ return 0;
+ /* otherwise remove the min item before inserting new */
+ (void)git_pqueue_pop(pq);
}
- /* insert item */
- i = q->size++;
- q->d[i] = d;
- bubble_up(q, i);
+ if (!(error = git_vector_insert(pq, item)))
+ pqueue_up(pq, pq->length - 1);
- return 0;
+ return error;
}
-
-void *git_pqueue_pop(git_pqueue *q)
+void *git_pqueue_pop(git_pqueue *pq)
{
- void *head;
-
- if (!q || q->size == 1)
- return NULL;
-
- head = q->d[1];
- q->d[1] = q->d[--q->size];
- percolate_down(q, 1);
-
- return head;
-}
-
+ void *rval = git_pqueue_get(pq, 0);
+
+ if (git_pqueue_size(pq) > 1) {
+ /* move last item to top of heap, shrink, and push item down */
+ pq->contents[0] = git_vector_last(pq);
+ git_vector_pop(pq);
+ pqueue_down(pq, 0);
+ } else {
+ /* all we need to do is shrink the heap in this case */
+ git_vector_pop(pq);
+ }
-void *git_pqueue_peek(git_pqueue *q)
-{
- if (!q || q->size == 1)
- return NULL;
- return q->d[1];
+ return rval;
}
diff --git a/src/pqueue.h b/src/pqueue.h
index 9061f8279..da7b74edf 100644
--- a/src/pqueue.h
+++ b/src/pqueue.h
@@ -3,99 +3,54 @@
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
- *
- * This file is based on a modified version of the priority queue found
- * in the Apache project and libpqueue library.
- *
- * https://github.com/vy/libpqueue
- *
- * Original file notice:
- *
- * Copyright 2010 Volkan Yazici <volkan.yazici@gmail.com>
- * Copyright 2006-2010 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
*/
-
#ifndef INCLUDE_pqueue_h__
#define INCLUDE_pqueue_h__
-/** callback functions to get/set/compare the priority of an element */
-typedef int (*git_pqueue_cmp)(void *a, void *b);
+#include "vector.h"
-/** the priority queue handle */
-typedef struct {
- size_t size, avail, step;
- git_pqueue_cmp cmppri;
- void **d;
-} git_pqueue;
+typedef git_vector git_pqueue;
+enum {
+ /* flag meaning: don't grow heap, keep highest values only */
+ GIT_PQUEUE_FIXED_SIZE = (GIT_VECTOR_FLAG_MAX << 1),
+};
/**
- * initialize the queue
+ * Initialize priority queue
*
- * @param n the initial estimate of the number of queue items for which memory
- * should be preallocated
- * @param cmppri the callback function to compare two nodes of the queue
- *
- * @return the handle or NULL for insufficent memory
- */
-int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri);
-
-
-/**
- * free all memory used by the queue
- * @param q the queue
- */
-void git_pqueue_free(git_pqueue *q);
-
-/**
- * clear all the elements in the queue
- * @param q the queue
- */
-void git_pqueue_clear(git_pqueue *q);
+ * @param pq The priority queue struct to initialize
+ * @param flags Flags (see above) to control queue behavior
+ * @param init_size The initial queue size
+ * @param cmp The entry priority comparison function
+ * @return 0 on success, <0 on error
+ */
+extern int git_pqueue_init(
+ git_pqueue *pq,
+ uint32_t flags,
+ size_t init_size,
+ git_vector_cmp cmp);
+
+#define git_pqueue_free git_vector_free
+#define git_pqueue_clear git_vector_clear
+#define git_pqueue_size git_vector_length
+#define git_pqueue_get git_vector_get
/**
- * return the size of the queue.
- * @param q the queue
- */
-size_t git_pqueue_size(git_pqueue *q);
-
-
-/**
- * insert an item into the queue.
- * @param q the queue
- * @param d the item
- * @return 0 on success
- */
-int git_pqueue_insert(git_pqueue *q, void *d);
-
-
-/**
- * pop the highest-ranking item from the queue.
- * @param q the queue
- * @return NULL on error, otherwise the entry
+ * Insert a new item into the queue
+ *
+ * @param pq The priority queue
+ * @param item Pointer to the item data
+ * @return 0 on success, <0 on failure
*/
-void *git_pqueue_pop(git_pqueue *q);
-
+extern int git_pqueue_insert(git_pqueue *pq, void *item);
/**
- * access highest-ranking item without removing it.
- * @param q the queue
- * @return NULL on error, otherwise the entry
+ * Remove the top item in the priority queue
+ *
+ * @param pq The priority queue
+ * @return item from heap on success, NULL if queue is empty
*/
-void *git_pqueue_peek(git_pqueue *q);
-
-#endif /* PQUEUE_H */
-/** @} */
+extern void *git_pqueue_pop(git_pqueue *pq);
+#endif
diff --git a/src/push.c b/src/push.c
index d39a27182..c2947808e 100644
--- a/src/push.c
+++ b/src/push.c
@@ -194,7 +194,10 @@ int git_push_add_refspec(git_push *push, const char *refspec)
return 0;
}
-int git_push_update_tips(git_push *push)
+int git_push_update_tips(
+ git_push *push,
+ const git_signature *signature,
+ const char *reflog_message)
{
git_buf remote_ref_name = GIT_BUF_INIT;
size_t i, j;
@@ -241,7 +244,9 @@ int git_push_update_tips(git_push *push)
giterr_clear();
else
goto on_error;
- } else if ((error = git_reference_create(NULL, push->remote->repo, git_buf_cstr(&remote_ref_name), &push_spec->loid, 1, NULL, NULL)) < 0)
+ } else if ((error = git_reference_create(NULL, push->remote->repo,
+ git_buf_cstr(&remote_ref_name), &push_spec->loid, 1, signature,
+ reflog_message ? reflog_message : "update by push")) < 0)
goto on_error;
}
diff --git a/src/refdb.c b/src/refdb.c
index 411423d57..984c3c7f6 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -167,14 +167,14 @@ void git_refdb_iterator_free(git_reference_iterator *iter)
iter->free(iter);
}
-int git_refdb_write(git_refdb *db, git_reference *ref, int force, const git_signature *who, const char *message)
+int git_refdb_write(git_refdb *db, git_reference *ref, int force, const git_signature *who, const char *message, const git_oid *old_id, const char *old_target)
{
assert(db && db->backend);
GIT_REFCOUNT_INC(db);
ref->db = db;
- return db->backend->write(db->backend, ref, force, who, message);
+ return db->backend->write(db->backend, ref, force, who, message, old_id, old_target);
}
int git_refdb_rename(
@@ -201,10 +201,10 @@ int git_refdb_rename(
return 0;
}
-int git_refdb_delete(struct git_refdb *db, const char *ref_name)
+int git_refdb_delete(struct git_refdb *db, const char *ref_name, const git_oid *old_id, const char *old_target)
{
assert(db && db->backend);
- return db->backend->del(db->backend, ref_name);
+ return db->backend->del(db->backend, ref_name, old_id, old_target);
}
int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name)
diff --git a/src/refdb.h b/src/refdb.h
index 91eecb782..cbad86faf 100644
--- a/src/refdb.h
+++ b/src/refdb.h
@@ -42,8 +42,8 @@ int git_refdb_iterator_next(git_reference **out, git_reference_iterator *iter);
int git_refdb_iterator_next_name(const char **out, git_reference_iterator *iter);
void git_refdb_iterator_free(git_reference_iterator *iter);
-int git_refdb_write(git_refdb *refdb, git_reference *ref, int force, const git_signature *who, const char *message);
-int git_refdb_delete(git_refdb *refdb, const char *ref_name);
+int git_refdb_write(git_refdb *refdb, git_reference *ref, int force, const git_signature *who, const char *message, const git_oid *old_id, const char *old_target);
+int git_refdb_delete(git_refdb *refdb, const char *ref_name, const git_oid *old_id, const char *old_target);
int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name);
int git_refdb_reflog_write(git_reflog *reflog);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 41ff01998..43682f40e 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -688,30 +688,32 @@ static int reference_path_available(
return 0;
}
-static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const git_reference *ref)
+static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
{
+ int error;
git_buf ref_path = GIT_BUF_INIT;
+ assert(file && backend && name);
+
/* Remove a possibly existing empty directory hierarchy
* which name would collide with the reference name
*/
- if (git_futils_rmdir_r(ref->name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0)
+ if (git_futils_rmdir_r(name, backend->path, GIT_RMDIR_SKIP_NONEMPTY) < 0)
return -1;
- if (git_buf_joinpath(&ref_path, backend->path, ref->name) < 0)
+ if (git_buf_joinpath(&ref_path, backend->path, name) < 0)
return -1;
- if (git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) {
- git_buf_free(&ref_path);
- return -1;
- }
+ error = git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE);
git_buf_free(&ref_path);
- return 0;
+ return error;
}
static int loose_commit(git_filebuf *file, const git_reference *ref)
{
+ assert(file && ref);
+
if (ref->type == GIT_REF_OID) {
char oid[GIT_OID_HEXSZ + 1];
git_oid_nfmt(oid, sizeof(oid), &ref->target.oid);
@@ -928,16 +930,54 @@ static bool should_write_reflog(git_repository *repo, const char *name)
return 0;
}
+static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const char *name,
+ const git_oid *old_id, const char *old_target)
+{
+ int error = 0;
+ git_reference *old_ref = NULL;
+
+ *cmp = 0;
+ /* It "matches" if there is no old value to compare against */
+ if (!old_id && !old_target)
+ return 0;
+
+ if ((error = refdb_fs_backend__lookup(&old_ref, backend, name)) < 0)
+ goto out;
+
+ /* If the types don't match, there's no way the values do */
+ if (old_id && old_ref->type != GIT_REF_OID) {
+ *cmp = -1;
+ goto out;
+ }
+ if (old_target && old_ref->type != GIT_REF_SYMBOLIC) {
+ *cmp = 1;
+ goto out;
+ }
+
+ if (old_id && old_ref->type == GIT_REF_OID)
+ *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
+
+ if (old_target && old_ref->type == GIT_REF_SYMBOLIC)
+ *cmp = git__strcmp(old_target, old_ref->target.symbolic);
+
+out:
+ git_reference_free(old_ref);
+
+ return error;
+}
+
static int refdb_fs_backend__write(
git_refdb_backend *_backend,
const git_reference *ref,
int force,
const git_signature *who,
- const char *message)
+ const char *message,
+ const git_oid *old_id,
+ const char *old_target)
{
refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
git_filebuf file = GIT_FILEBUF_INIT;
- int error;
+ int error = 0, cmp = 0;
assert(backend);
@@ -945,10 +985,19 @@ static int refdb_fs_backend__write(
if (error < 0)
return error;
- /* We need to perform the reflog append under the ref's lock */
- if ((error = loose_lock(&file, backend, ref)) < 0)
+ /* We need to perform the reflog append and old value check under the ref's lock */
+ if ((error = loose_lock(&file, backend, ref->name)) < 0)
return error;
+ if ((error = cmp_old_ref(&cmp, _backend, ref->name, old_id, old_target)) < 0)
+ goto on_error;
+
+ if (cmp) {
+ giterr_set(GITERR_REFERENCE, "old reference value does not match");
+ error = GIT_EMODIFIED;
+ goto on_error;
+ }
+
if (should_write_reflog(backend->repo, ref->name) &&
(error = reflog_append(backend, ref, who, message)) < 0) {
git_filebuf_cleanup(&file);
@@ -956,20 +1005,39 @@ static int refdb_fs_backend__write(
}
return loose_commit(&file, ref);
+
+on_error:
+ git_filebuf_cleanup(&file);
+ return error;
}
static int refdb_fs_backend__delete(
git_refdb_backend *_backend,
- const char *ref_name)
+ const char *ref_name,
+ const git_oid *old_id, const char *old_target)
{
refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
git_buf loose_path = GIT_BUF_INIT;
size_t pack_pos;
- int error = 0;
+ git_filebuf file = GIT_FILEBUF_INIT;
+ int error = 0, cmp = 0;
bool loose_deleted = 0;
assert(backend && ref_name);
+ if ((error = loose_lock(&file, backend, ref_name)) < 0)
+ return error;
+
+ error = cmp_old_ref(&cmp, _backend, ref_name, old_id, old_target);
+ if (error < 0)
+ goto cleanup;
+
+ if (cmp) {
+ giterr_set(GITERR_REFERENCE, "old reference value does not match");
+ error = GIT_EMODIFIED;
+ goto cleanup;
+ }
+
/* If a loose reference exists, remove it from the filesystem */
if (git_buf_joinpath(&loose_path, backend->path, ref_name) < 0)
return -1;
@@ -982,14 +1050,14 @@ static int refdb_fs_backend__delete(
git_buf_free(&loose_path);
if (error != 0)
- return error;
+ goto cleanup;
- if (packed_reload(backend) < 0)
- return -1;
+ if ((error = packed_reload(backend)) < 0)
+ goto cleanup;
/* If a packed reference exists, remove it from the packfile and repack */
- if (git_sortedcache_wlock(backend->refcache) < 0)
- return -1;
+ if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
+ goto cleanup;
if (!(error = git_sortedcache_lookup_index(
&pack_pos, backend->refcache, ref_name)))
@@ -997,10 +1065,17 @@ static int refdb_fs_backend__delete(
git_sortedcache_wunlock(backend->refcache);
- if (error == GIT_ENOTFOUND)
- return loose_deleted ? 0 : ref_error_notfound(ref_name);
+ if (error == GIT_ENOTFOUND) {
+ error = loose_deleted ? 0 : ref_error_notfound(ref_name);
+ goto cleanup;
+ }
+
+ error = packed_write(backend);
- return packed_write(backend);
+cleanup:
+ git_filebuf_cleanup(&file);
+
+ return error;
}
static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name);
@@ -1026,7 +1101,7 @@ static int refdb_fs_backend__rename(
(error = refdb_fs_backend__lookup(&old, _backend, old_name)) < 0)
return error;
- if ((error = refdb_fs_backend__delete(_backend, old_name)) < 0) {
+ if ((error = refdb_fs_backend__delete(_backend, old_name, NULL, NULL)) < 0) {
git_reference_free(old);
return error;
}
@@ -1037,7 +1112,7 @@ static int refdb_fs_backend__rename(
return -1;
}
- if ((error = loose_lock(&file, backend, new)) < 0) {
+ if ((error = loose_lock(&file, backend, new->name)) < 0) {
git_reference_free(new);
return error;
}
@@ -1452,7 +1527,13 @@ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, co
if (error < 0)
return error;
- if (git_reference_target(ref) != NULL)
+ if (git_reference_symbolic_target(ref) != NULL) {
+ error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
+ if (error != 0 && error != GIT_ENOTFOUND)
+ goto cleanup;
+ giterr_clear();
+ }
+ else if (git_reference_target(ref) != NULL)
git_oid_cpy(&new_id, git_reference_target(ref));
if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
diff --git a/src/refs.c b/src/refs.c
index eb2c34211..e63796c94 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -116,7 +116,26 @@ void git_reference_free(git_reference *reference)
int git_reference_delete(git_reference *ref)
{
- return git_refdb_delete(ref->db, ref->name);
+ const git_oid *old_id = NULL;
+ const char *old_target = NULL;
+
+ if (ref->type == GIT_REF_OID)
+ old_id = &ref->target.oid;
+ else
+ old_target = ref->target.symbolic;
+
+ return git_refdb_delete(ref->db, ref->name, old_id, old_target);
+}
+
+int git_reference_remove(git_repository *repo, const char *name)
+{
+ git_refdb *db;
+ int error;
+
+ if ((error = git_repository_refdb__weakptr(&db, repo)) < 0)
+ return error;
+
+ return git_refdb_delete(db, name, NULL, NULL);
}
int git_reference_lookup(git_reference **ref_out,
@@ -331,7 +350,9 @@ static int reference__create(
const char *symbolic,
int force,
const git_signature *signature,
- const char *log_message)
+ const char *log_message,
+ const git_oid *old_id,
+ const char *old_target)
{
char normalized[GIT_REFNAME_MAX];
git_refdb *refdb;
@@ -380,7 +401,7 @@ static int reference__create(
GITERR_CHECK_ALLOC(ref);
- if ((error = git_refdb_write(refdb, ref, force, signature, log_message)) < 0) {
+ if ((error = git_refdb_write(refdb, ref, force, signature, log_message, old_id, old_target)) < 0) {
git_reference_free(ref);
return error;
}
@@ -406,19 +427,21 @@ static int log_signature(git_signature **out, git_repository *repo)
return 0;
}
-int git_reference_create(
+int git_reference_create_matching(
git_reference **ref_out,
git_repository *repo,
const char *name,
- const git_oid *oid,
+ const git_oid *id,
int force,
+ const git_oid *old_id,
const git_signature *signature,
const char *log_message)
+
{
int error;
git_signature *who = NULL;
- assert(oid);
+ assert(id);
if (!signature) {
if ((error = log_signature(&who, repo)) < 0)
@@ -428,18 +451,31 @@ int git_reference_create(
}
error = reference__create(
- ref_out, repo, name, oid, NULL, force, signature, log_message);
+ ref_out, repo, name, id, NULL, force, signature, log_message, old_id, NULL);
git_signature_free(who);
return error;
}
-int git_reference_symbolic_create(
+int git_reference_create(
+ git_reference **ref_out,
+ git_repository *repo,
+ const char *name,
+ const git_oid *id,
+ int force,
+ const git_signature *signature,
+ const char *log_message)
+{
+ return git_reference_create_matching(ref_out, repo, name, id, force, NULL, signature, log_message);
+}
+
+int git_reference_symbolic_create_matching(
git_reference **ref_out,
git_repository *repo,
const char *name,
const char *target,
int force,
+ const char *old_target,
const git_signature *signature,
const char *log_message)
{
@@ -456,12 +492,24 @@ int git_reference_symbolic_create(
}
error = reference__create(
- ref_out, repo, name, NULL, target, force, signature, log_message);
+ ref_out, repo, name, NULL, target, force, signature, log_message, NULL, old_target);
git_signature_free(who);
return error;
}
+int git_reference_symbolic_create(
+ git_reference **ref_out,
+ git_repository *repo,
+ const char *name,
+ const char *target,
+ int force,
+ const git_signature *signature,
+ const char *log_message)
+{
+ return git_reference_symbolic_create_matching(ref_out, repo, name, target, force, NULL, signature, log_message);
+}
+
static int ensure_is_an_updatable_direct_reference(git_reference *ref)
{
if (ref->type == GIT_REF_OID)
@@ -479,14 +527,16 @@ int git_reference_set_target(
const char *log_message)
{
int error;
+ git_repository *repo;
assert(out && ref && id);
+ repo = ref->db->repo;
+
if ((error = ensure_is_an_updatable_direct_reference(ref)) < 0)
return error;
- return git_reference_create(
- out, ref->db->repo, ref->name, id, 1, signature, log_message);
+ return git_reference_create_matching(out, repo, ref->name, id, 1, &ref->target.oid, signature, log_message);
}
static int ensure_is_an_updatable_symbolic_reference(git_reference *ref)
@@ -512,8 +562,8 @@ int git_reference_symbolic_set_target(
if ((error = ensure_is_an_updatable_symbolic_reference(ref)) < 0)
return error;
- return git_reference_symbolic_create(
- out, ref->db->repo, ref->name, target, 1, signature, log_message);
+ return git_reference_symbolic_create_matching(
+ out, ref->db->repo, ref->name, target, 1, ref->target.symbolic, signature, log_message);
}
static int reference__rename(git_reference **out, git_reference *ref, const char *new_name, int force,
@@ -1032,8 +1082,10 @@ static int reference__update_terminal(
nesting+1, signature, log_message);
git_reference_free(ref);
} else {
+ /* If we're not moving the target, don't recreate the ref */
+ if (0 != git_oid_cmp(git_reference_target(ref), oid))
+ error = git_reference_create(NULL, repo, ref_name, oid, 1, signature, log_message);
git_reference_free(ref);
- error = git_reference_create(NULL, repo, ref_name, oid, 1, signature, log_message);
}
return error;
@@ -1113,6 +1165,17 @@ int git_reference_is_tag(git_reference *ref)
return git_reference__is_tag(ref->name);
}
+int git_reference__is_note(const char *ref_name)
+{
+ return git__prefixcmp(ref_name, GIT_REFS_NOTES_DIR) == 0;
+}
+
+int git_reference_is_note(git_reference *ref)
+{
+ assert(ref);
+ return git_reference__is_note(ref->name);
+}
+
static int peel_error(int error, git_reference *ref, const char* msg)
{
giterr_set(
diff --git a/src/remote.c b/src/remote.c
index f33f5ef3c..f320f4a52 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -845,9 +845,13 @@ int git_remote_download(git_remote *remote)
return git_fetch_download_pack(remote);
}
-int git_remote_fetch(git_remote *remote)
+int git_remote_fetch(
+ git_remote *remote,
+ const git_signature *signature,
+ const char *reflog_message)
{
int error;
+ git_buf reflog_msg_buf = GIT_BUF_INIT;
/* Connect and download everything */
if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0)
@@ -859,8 +863,18 @@ int git_remote_fetch(git_remote *remote)
/* We don't need to be connected anymore */
git_remote_disconnect(remote);
+ /* Default reflog message */
+ if (reflog_message)
+ git_buf_sets(&reflog_msg_buf, reflog_message);
+ else {
+ git_buf_printf(&reflog_msg_buf, "fetch %s",
+ remote->name ? remote->name : remote->url);
+ }
+
/* Create "remote/foo" branches for all remote branches */
- return git_remote_update_tips(remote);
+ error = git_remote_update_tips(remote, signature, git_buf_cstr(&reflog_msg_buf));
+ git_buf_free(&reflog_msg_buf);
+ return error;
}
static int remote_head_for_fetchspec_src(git_remote_head **out, git_vector *update_heads, const char *fetchspec_src)
@@ -978,7 +992,12 @@ cleanup:
return error;
}
-static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vector *refs)
+static int update_tips_for_spec(
+ git_remote *remote,
+ git_refspec *spec,
+ git_vector *refs,
+ const git_signature *signature,
+ const char *log_message)
{
int error = 0, autotag;
unsigned int i = 0;
@@ -1045,7 +1064,8 @@ static int update_tips_for_spec(git_remote *remote, git_refspec *spec, git_vecto
continue;
/* In autotag mode, don't overwrite any locally-existing tags */
- error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag, NULL, NULL);
+ error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, !autotag,
+ signature, log_message);
if (error < 0 && error != GIT_EEXISTS)
goto on_error;
@@ -1074,7 +1094,10 @@ on_error:
}
-int git_remote_update_tips(git_remote *remote)
+int git_remote_update_tips(
+ git_remote *remote,
+ const git_signature *signature,
+ const char *reflog_message)
{
git_refspec *spec, tagspec;
git_vector refs;
@@ -1089,7 +1112,7 @@ int git_remote_update_tips(git_remote *remote)
goto out;
if (remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL) {
- error = update_tips_for_spec(remote, &tagspec, &refs);
+ error = update_tips_for_spec(remote, &tagspec, &refs, signature, reflog_message);
goto out;
}
@@ -1097,7 +1120,7 @@ int git_remote_update_tips(git_remote *remote)
if (spec->push)
continue;
- if ((error = update_tips_for_spec(remote, spec, &refs)) < 0)
+ if ((error = update_tips_for_spec(remote, spec, &refs, signature, reflog_message)) < 0)
goto out;
}
diff --git a/src/repository.c b/src/repository.c
index 2c1b60266..b94973c74 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -4,7 +4,6 @@
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
-#include <stdarg.h>
#include <ctype.h>
#include "git2/object.h"
@@ -17,6 +16,7 @@
#include "tag.h"
#include "blob.h"
#include "fileops.h"
+#include "sysdir.h"
#include "filebuf.h"
#include "index.h"
#include "config.h"
@@ -1265,7 +1265,7 @@ static int repo_init_structure(
}
if (!tdir) {
- if (!(error = git_futils_find_template_dir(&template_buf)))
+ if (!(error = git_sysdir_find_template_dir(&template_buf)))
tdir = template_buf.ptr;
default_template = true;
}
@@ -1716,7 +1716,7 @@ cleanup:
return error;
}
-int git_repository_message(git_buf *out, git_repository *repo)
+int git_repository_message(git_buf *out, git_repository *repo)
{
git_buf path = GIT_BUF_INIT;
struct stat st;
@@ -1731,10 +1731,10 @@ int git_repository_message(git_buf *out, git_repository *repo)
if (errno == ENOENT)
error = GIT_ENOTFOUND;
giterr_set(GITERR_OS, "Could not access message file");
+ } else {
+ error = git_futils_readbuffer(out, git_buf_cstr(&path));
}
- error = git_futils_readbuffer(out, git_buf_cstr(&path));
-
git_buf_free(&path);
return error;
@@ -1891,7 +1891,9 @@ cleanup:
}
int git_repository_detach_head(
- git_repository* repo)
+ git_repository* repo,
+ const git_signature *signature,
+ const char *reflog_message)
{
git_reference *old_head = NULL,
*new_head = NULL;
@@ -1906,7 +1908,8 @@ int git_repository_detach_head(
if ((error = git_object_lookup(&object, repo, git_reference_target(old_head), GIT_OBJ_COMMIT)) < 0)
goto cleanup;
- error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head), 1, NULL, NULL);
+ error = git_reference_create(&new_head, repo, GIT_HEAD_FILE, git_reference_target(old_head),
+ 1, signature, reflog_message);
cleanup:
git_object_free(object);
@@ -1998,9 +2001,12 @@ int git_repository_is_shallow(git_repository *repo)
error = git_path_lstat(path.ptr, &st);
git_buf_free(&path);
- if (error == GIT_ENOTFOUND)
+ if (error == GIT_ENOTFOUND) {
+ giterr_clear();
return 0;
+ }
+
if (error < 0)
- return -1;
+ return error;
return st.st_size == 0 ? 0 : 1;
}
diff --git a/src/reset.c b/src/reset.c
index 15f7fe13a..07fd08863 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -94,13 +94,16 @@ cleanup:
int git_reset(
git_repository *repo,
git_object *target,
- git_reset_t reset_type)
+ git_reset_t reset_type,
+ git_signature *signature,
+ const char *log_message)
{
git_object *commit = NULL;
git_index *index = NULL;
git_tree *tree = NULL;
int error = 0;
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_buf log_message_buf = GIT_BUF_INIT;
assert(repo && target);
@@ -129,9 +132,14 @@ int git_reset(
goto cleanup;
}
+ if (log_message)
+ git_buf_sets(&log_message_buf, log_message);
+ else
+ git_buf_sets(&log_message_buf, "reset: moving");
+
/* move HEAD to the new target */
if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE,
- git_object_id(commit), NULL, NULL)) < 0)
+ git_object_id(commit), signature, git_buf_cstr(&log_message_buf))) < 0)
goto cleanup;
if (reset_type == GIT_RESET_HARD) {
diff --git a/src/revparse.c b/src/revparse.c
index 5cce3be63..60872e187 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -490,8 +490,7 @@ static int handle_grep_syntax(git_object **out, git_repository *repo, const git_
git_revwalk_sorting(walk, GIT_SORT_TIME);
if (spec_oid == NULL) {
- // TODO: @carlosmn: The glob should be refs/* but this makes git_revwalk_next() fails
- if ((error = git_revwalk_push_glob(walk, GIT_REFS_HEADS_DIR "*")) < 0)
+ if ((error = git_revwalk_push_glob(walk, "refs/*")) < 0)
goto cleanup;
} else if ((error = git_revwalk_push(walk, spec_oid)) < 0)
goto cleanup;
diff --git a/src/revwalk.c b/src/revwalk.c
index c0a053211..753911246 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -110,25 +110,34 @@ static int process_commit_parents(git_revwalk *walk, git_commit_list_node *commi
return error;
}
-static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
+static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting, int from_glob)
{
+ git_oid commit_id;
int error;
- git_object *obj;
- git_otype type;
+ git_object *obj, *oobj;
git_commit_list_node *commit;
- if ((error = git_object_lookup(&obj, walk->repo, oid, GIT_OBJ_ANY)) < 0)
+ if ((error = git_object_lookup(&oobj, walk->repo, oid, GIT_OBJ_ANY)) < 0)
return error;
- type = git_object_type(obj);
- git_object_free(obj);
+ error = git_object_peel(&obj, oobj, GIT_OBJ_COMMIT);
+ git_object_free(oobj);
+
+ if (error == GIT_ENOTFOUND) {
+ /* If this comes from e.g. push_glob("tags"), ignore this */
+ if (from_glob)
+ return 0;
- if (type != GIT_OBJ_COMMIT) {
- giterr_set(GITERR_INVALID, "Object is no commit object");
+ giterr_set(GITERR_INVALID, "Object is not a committish");
return -1;
}
+ if (error < 0)
+ return error;
+
+ git_oid_cpy(&commit_id, git_object_id(obj));
+ git_object_free(obj);
- commit = git_revwalk__commit_lookup(walk, oid);
+ commit = git_revwalk__commit_lookup(walk, &commit_id);
if (commit == NULL)
return -1; /* error already reported by failed lookup */
@@ -146,24 +155,24 @@ static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
int git_revwalk_push(git_revwalk *walk, const git_oid *oid)
{
assert(walk && oid);
- return push_commit(walk, oid, 0);
+ return push_commit(walk, oid, 0, false);
}
int git_revwalk_hide(git_revwalk *walk, const git_oid *oid)
{
assert(walk && oid);
- return push_commit(walk, oid, 1);
+ return push_commit(walk, oid, 1, false);
}
-static int push_ref(git_revwalk *walk, const char *refname, int hide)
+static int push_ref(git_revwalk *walk, const char *refname, int hide, int from_glob)
{
git_oid oid;
if (git_reference_name_to_id(&oid, walk->repo, refname) < 0)
return -1;
- return push_commit(walk, &oid, hide);
+ return push_commit(walk, &oid, hide, from_glob);
}
struct push_cb_data {
@@ -171,17 +180,12 @@ struct push_cb_data {
int hide;
};
-static int push_glob_cb(const char *refname, void *data_)
-{
- struct push_cb_data *data = (struct push_cb_data *)data_;
- return push_ref(data->walk, refname, data->hide);
-}
-
static int push_glob(git_revwalk *walk, const char *glob, int hide)
{
int error = 0;
git_buf buf = GIT_BUF_INIT;
- struct push_cb_data data;
+ git_reference *ref;
+ git_reference_iterator *iter;
size_t wildcard;
assert(walk && glob);
@@ -199,12 +203,20 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
if (!glob[wildcard])
git_buf_put(&buf, "/*", 2);
- data.walk = walk;
- data.hide = hide;
+ if ((error = git_reference_iterator_glob_new(&iter, walk->repo, buf.ptr)) < 0)
+ goto out;
- error = git_reference_foreach_glob(
- walk->repo, git_buf_cstr(&buf), push_glob_cb, &data);
+ while ((error = git_reference_next(&ref, iter)) == 0) {
+ error = push_ref(walk, git_reference_name(ref), hide, true);
+ git_reference_free(ref);
+ if (error < 0)
+ break;
+ }
+ git_reference_iterator_free(iter);
+ if (error == GIT_ITEROVER)
+ error = 0;
+out:
git_buf_free(&buf);
return error;
}
@@ -224,19 +236,19 @@ int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
int git_revwalk_push_head(git_revwalk *walk)
{
assert(walk);
- return push_ref(walk, GIT_HEAD_FILE, 0);
+ return push_ref(walk, GIT_HEAD_FILE, 0, false);
}
int git_revwalk_hide_head(git_revwalk *walk)
{
assert(walk);
- return push_ref(walk, GIT_HEAD_FILE, 1);
+ return push_ref(walk, GIT_HEAD_FILE, 1, false);
}
int git_revwalk_push_ref(git_revwalk *walk, const char *refname)
{
assert(walk && refname);
- return push_ref(walk, refname, 0);
+ return push_ref(walk, refname, 0, false);
}
int git_revwalk_push_range(git_revwalk *walk, const char *range)
@@ -253,10 +265,10 @@ int git_revwalk_push_range(git_revwalk *walk, const char *range)
return GIT_EINVALIDSPEC;
}
- if ((error = push_commit(walk, git_object_id(revspec.from), 1)))
+ if ((error = push_commit(walk, git_object_id(revspec.from), 1, false)))
goto out;
- error = push_commit(walk, git_object_id(revspec.to), 0);
+ error = push_commit(walk, git_object_id(revspec.to), 0, false);
out:
git_object_free(revspec.from);
@@ -267,7 +279,7 @@ out:
int git_revwalk_hide_ref(git_revwalk *walk, const char *refname)
{
assert(walk && refname);
- return push_ref(walk, refname, 1);
+ return push_ref(walk, refname, 1, false);
}
static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit)
@@ -439,7 +451,8 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
walk->commits = git_oidmap_alloc();
GITERR_CHECK_ALLOC(walk->commits);
- if (git_pqueue_init(&walk->iterator_time, 8, git_commit_list_time_cmp) < 0 ||
+ if (git_pqueue_init(
+ &walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0 ||
git_vector_init(&walk->twos, 4, NULL) < 0 ||
git_pool_init(&walk->commit_pool, 1,
git_pool__suggest_items_per_page(COMMIT_ALLOC) * COMMIT_ALLOC) < 0)
diff --git a/src/settings.c b/src/settings.c
index 748f76560..3856735f7 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -5,11 +5,9 @@
* a Linking Exception. For full terms see the included COPYING file.
*/
-#include <stdarg.h>
-
#include <git2.h>
#include "common.h"
-#include "fileops.h"
+#include "sysdir.h"
#include "cache.h"
void git_libgit2_version(int *major, int *minor, int *rev)
@@ -38,14 +36,14 @@ int git_libgit2_capabilities()
extern size_t git_mwindow__window_size;
extern size_t git_mwindow__mapped_limit;
-static int config_level_to_futils_dir(int config_level)
+static int config_level_to_sysdir(int config_level)
{
int val = -1;
switch (config_level) {
- case GIT_CONFIG_LEVEL_SYSTEM: val = GIT_FUTILS_DIR_SYSTEM; break;
- case GIT_CONFIG_LEVEL_XDG: val = GIT_FUTILS_DIR_XDG; break;
- case GIT_CONFIG_LEVEL_GLOBAL: val = GIT_FUTILS_DIR_GLOBAL; break;
+ case GIT_CONFIG_LEVEL_SYSTEM: val = GIT_SYSDIR_SYSTEM; break;
+ case GIT_CONFIG_LEVEL_XDG: val = GIT_SYSDIR_XDG; break;
+ case GIT_CONFIG_LEVEL_GLOBAL: val = GIT_SYSDIR_GLOBAL; break;
default:
giterr_set(
GITERR_INVALID, "Invalid config path selector %d", config_level);
@@ -79,17 +77,17 @@ int git_libgit2_opts(int key, ...)
break;
case GIT_OPT_GET_SEARCH_PATH:
- if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0) {
+ if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0) {
char *out = va_arg(ap, char *);
size_t outlen = va_arg(ap, size_t);
- error = git_futils_dirs_get_str(out, outlen, error);
+ error = git_sysdir_get_str(out, outlen, error);
}
break;
case GIT_OPT_SET_SEARCH_PATH:
- if ((error = config_level_to_futils_dir(va_arg(ap, int))) >= 0)
- error = git_futils_dirs_set(error, va_arg(ap, const char *));
+ if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0)
+ error = git_sysdir_set(error, va_arg(ap, const char *));
break;
case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
@@ -118,12 +116,12 @@ int git_libgit2_opts(int key, ...)
char *out = va_arg(ap, char *);
size_t outlen = va_arg(ap, size_t);
- error = git_futils_dirs_get_str(out, outlen, GIT_FUTILS_DIR_TEMPLATE);
+ error = git_sysdir_get_str(out, outlen, GIT_SYSDIR_TEMPLATE);
}
break;
case GIT_OPT_SET_TEMPLATE_PATH:
- error = git_futils_dirs_set(GIT_FUTILS_DIR_TEMPLATE, va_arg(ap, const char *));
+ error = git_sysdir_set(GIT_SYSDIR_TEMPLATE, va_arg(ap, const char *));
break;
}
diff --git a/src/signature.c b/src/signature.c
index f658d6035..f501cd8b6 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -230,6 +230,8 @@ void git_signature__writebuf(git_buf *buf, const char *header, const git_signatu
int offset, hours, mins;
char sign;
+ assert(buf && sig);
+
offset = sig->when.offset;
sign = (sig->when.offset < 0) ? '-' : '+';
diff --git a/src/sortedcache.c b/src/sortedcache.c
index 466e55dbe..13f0921f1 100644
--- a/src/sortedcache.c
+++ b/src/sortedcache.c
@@ -321,7 +321,7 @@ size_t git_sortedcache_entrycount(const git_sortedcache *sc)
void *git_sortedcache_entry(git_sortedcache *sc, size_t pos)
{
/* make sure the items are sorted so this gets the correct item */
- if (!sc->items.sorted)
+ if (!git_vector_is_sorted(&sc->items))
git_vector_sort(&sc->items);
return git_vector_get(&sc->items, pos);
diff --git a/src/strnlen.h b/src/strnlen.h
new file mode 100644
index 000000000..007da2e55
--- /dev/null
+++ b/src/strnlen.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_strlen_h__
+#define INCLUDE_strlen_h__
+
+#if defined(__MINGW32__) || defined(__sun) || defined(__APPLE__)
+# define NO_STRNLEN
+#endif
+
+#ifdef NO_STRNLEN
+GIT_INLINE(size_t) p_strnlen(const char *s, size_t maxlen) {
+ const char *end = memchr(s, 0, maxlen);
+ return end ? (size_t)(end - s) : maxlen;
+}
+#else
+# define p_strnlen strnlen
+#endif
+
+#endif
diff --git a/src/sysdir.c b/src/sysdir.c
new file mode 100644
index 000000000..2e6304e35
--- /dev/null
+++ b/src/sysdir.c
@@ -0,0 +1,244 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "sysdir.h"
+#include "global.h"
+#include "buffer.h"
+#include "path.h"
+#include <ctype.h>
+#if GIT_WIN32
+#include "win32/findfile.h"
+#endif
+
+static int git_sysdir_guess_system_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+ return git_win32__find_system_dirs(out, L"etc\\");
+#else
+ return git_buf_sets(out, "/etc");
+#endif
+}
+
+static int git_sysdir_guess_global_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+ return git_win32__find_global_dirs(out);
+#else
+ return git_buf_sets(out, getenv("HOME"));
+#endif
+}
+
+static int git_sysdir_guess_xdg_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+ return git_win32__find_xdg_dirs(out);
+#else
+ const char *env = NULL;
+
+ if ((env = getenv("XDG_CONFIG_HOME")) != NULL)
+ return git_buf_joinpath(out, env, "git");
+ else if ((env = getenv("HOME")) != NULL)
+ return git_buf_joinpath(out, env, ".config/git");
+
+ git_buf_clear(out);
+ return 0;
+#endif
+}
+
+static int git_sysdir_guess_template_dirs(git_buf *out)
+{
+#ifdef GIT_WIN32
+ return git_win32__find_system_dirs(out, L"share\\git-core\\templates");
+#else
+ return git_buf_sets(out, "/usr/share/git-core/templates");
+#endif
+}
+
+typedef int (*git_sysdir_guess_cb)(git_buf *out);
+
+static git_buf git_sysdir__dirs[GIT_SYSDIR__MAX] =
+ { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT };
+
+static git_sysdir_guess_cb git_sysdir__dir_guess[GIT_SYSDIR__MAX] = {
+ git_sysdir_guess_system_dirs,
+ git_sysdir_guess_global_dirs,
+ git_sysdir_guess_xdg_dirs,
+ git_sysdir_guess_template_dirs,
+};
+
+static int git_sysdir__dirs_shutdown_set = 0;
+
+int git_sysdir_global_init(void)
+{
+ git_sysdir_t i;
+ const git_buf *path;
+ int error = 0;
+
+ for (i = 0; !error && i < GIT_SYSDIR__MAX; i++)
+ error = git_sysdir_get(&path, i);
+
+ return error;
+}
+
+void git_sysdir_global_shutdown(void)
+{
+ int i;
+ for (i = 0; i < GIT_SYSDIR__MAX; ++i)
+ git_buf_free(&git_sysdir__dirs[i]);
+}
+
+static int git_sysdir_check_selector(git_sysdir_t which)
+{
+ if (which < GIT_SYSDIR__MAX)
+ return 0;
+
+ giterr_set(GITERR_INVALID, "config directory selector out of range");
+ return -1;
+}
+
+
+int git_sysdir_get(const git_buf **out, git_sysdir_t which)
+{
+ assert(out);
+
+ *out = NULL;
+
+ GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
+
+ if (!git_buf_len(&git_sysdir__dirs[which])) {
+ /* prepare shutdown if we're going to need it */
+ if (!git_sysdir__dirs_shutdown_set) {
+ git__on_shutdown(git_sysdir_global_shutdown);
+ git_sysdir__dirs_shutdown_set = 1;
+ }
+
+ GITERR_CHECK_ERROR(
+ git_sysdir__dir_guess[which](&git_sysdir__dirs[which]));
+ }
+
+ *out = &git_sysdir__dirs[which];
+ return 0;
+}
+
+int git_sysdir_get_str(
+ char *out,
+ size_t outlen,
+ git_sysdir_t which)
+{
+ const git_buf *path = NULL;
+
+ GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
+ GITERR_CHECK_ERROR(git_sysdir_get(&path, which));
+
+ if (!out || path->size >= outlen) {
+ giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path");
+ return GIT_EBUFS;
+ }
+
+ git_buf_copy_cstr(out, outlen, path);
+ return 0;
+}
+
+#define PATH_MAGIC "$PATH"
+
+int git_sysdir_set(git_sysdir_t which, const char *search_path)
+{
+ const char *expand_path = NULL;
+ git_buf merge = GIT_BUF_INIT;
+
+ GITERR_CHECK_ERROR(git_sysdir_check_selector(which));
+
+ if (search_path != NULL)
+ expand_path = strstr(search_path, PATH_MAGIC);
+
+ /* init with default if not yet done and needed (ignoring error) */
+ if ((!search_path || expand_path) &&
+ !git_buf_len(&git_sysdir__dirs[which]))
+ git_sysdir__dir_guess[which](&git_sysdir__dirs[which]);
+
+ /* if $PATH is not referenced, then just set the path */
+ if (!expand_path)
+ return git_buf_sets(&git_sysdir__dirs[which], search_path);
+
+ /* otherwise set to join(before $PATH, old value, after $PATH) */
+ if (expand_path > search_path)
+ git_buf_set(&merge, search_path, expand_path - search_path);
+
+ if (git_buf_len(&git_sysdir__dirs[which]))
+ git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR,
+ merge.ptr, git_sysdir__dirs[which].ptr);
+
+ expand_path += strlen(PATH_MAGIC);
+ if (*expand_path)
+ git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path);
+
+ git_buf_swap(&git_sysdir__dirs[which], &merge);
+ git_buf_free(&merge);
+
+ return git_buf_oom(&git_sysdir__dirs[which]) ? -1 : 0;
+}
+
+static int git_sysdir_find_in_dirlist(
+ git_buf *path,
+ const char *name,
+ git_sysdir_t which,
+ const char *label)
+{
+ size_t len;
+ const char *scan, *next = NULL;
+ const git_buf *syspath;
+
+ GITERR_CHECK_ERROR(git_sysdir_get(&syspath, which));
+
+ for (scan = git_buf_cstr(syspath); scan; scan = next) {
+ for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR);
+ next && next > scan && next[-1] == '\\';
+ next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR))
+ /* find unescaped separator or end of string */;
+
+ len = next ? (size_t)(next++ - scan) : strlen(scan);
+ if (!len)
+ continue;
+
+ GITERR_CHECK_ERROR(git_buf_set(path, scan, len));
+ if (name)
+ GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name));
+
+ if (git_path_exists(path->ptr))
+ return 0;
+ }
+
+ git_buf_clear(path);
+ giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name);
+ return GIT_ENOTFOUND;
+}
+
+int git_sysdir_find_system_file(git_buf *path, const char *filename)
+{
+ return git_sysdir_find_in_dirlist(
+ path, filename, GIT_SYSDIR_SYSTEM, "system");
+}
+
+int git_sysdir_find_global_file(git_buf *path, const char *filename)
+{
+ return git_sysdir_find_in_dirlist(
+ path, filename, GIT_SYSDIR_GLOBAL, "global");
+}
+
+int git_sysdir_find_xdg_file(git_buf *path, const char *filename)
+{
+ return git_sysdir_find_in_dirlist(
+ path, filename, GIT_SYSDIR_XDG, "global/xdg");
+}
+
+int git_sysdir_find_template_dir(git_buf *path)
+{
+ return git_sysdir_find_in_dirlist(
+ path, NULL, GIT_SYSDIR_TEMPLATE, "template");
+}
+
diff --git a/src/sysdir.h b/src/sysdir.h
new file mode 100644
index 000000000..f1bbf0bae
--- /dev/null
+++ b/src/sysdir.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_sysdir_h__
+#define INCLUDE_sysdir_h__
+
+#include "common.h"
+#include "posix.h"
+#include "buffer.h"
+
+/**
+ * Find a "global" file (i.e. one in a user's home directory).
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_global_file(git_buf *path, const char *filename);
+
+/**
+ * Find an "XDG" file (i.e. one in user's XDG config path).
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_xdg_file(git_buf *path, const char *filename);
+
+/**
+ * Find a "system" file (i.e. one shared for all users of the system).
+ *
+ * @param path buffer to write the full path into
+ * @param filename name of file to find in the home directory
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_system_file(git_buf *path, const char *filename);
+
+/**
+ * Find template directory.
+ *
+ * @param path buffer to write the full path into
+ * @return 0 if found, GIT_ENOTFOUND if not found, or -1 on other OS error
+ */
+extern int git_sysdir_find_template_dir(git_buf *path);
+
+typedef enum {
+ GIT_SYSDIR_SYSTEM = 0,
+ GIT_SYSDIR_GLOBAL = 1,
+ GIT_SYSDIR_XDG = 2,
+ GIT_SYSDIR_TEMPLATE = 3,
+ GIT_SYSDIR__MAX = 4,
+} git_sysdir_t;
+
+/**
+ * Configures global data for configuration file search paths.
+ *
+ * @return 0 on success, <0 on failure
+ */
+extern int git_sysdir_global_init(void);
+
+/**
+ * Get the search path for global/system/xdg files
+ *
+ * @param out pointer to git_buf containing search path
+ * @param which which list of paths to return
+ * @return 0 on success, <0 on failure
+ */
+extern int git_sysdir_get(const git_buf **out, git_sysdir_t which);
+
+/**
+ * Get search path into a preallocated buffer
+ *
+ * @param out String buffer to write into
+ * @param outlen Size of string buffer
+ * @param which Which search path to return
+ * @return 0 on success, GIT_EBUFS if out is too small, <0 on other failure
+ */
+
+extern int git_sysdir_get_str(char *out, size_t outlen, git_sysdir_t which);
+
+/**
+ * Set search paths for global/system/xdg files
+ *
+ * The first occurrence of the magic string "$PATH" in the new value will
+ * be replaced with the old value of the search path.
+ *
+ * @param which Which search path to modify
+ * @param paths New search path (separated by GIT_PATH_LIST_SEPARATOR)
+ * @return 0 on success, <0 on failure (allocation error)
+ */
+extern int git_sysdir_set(git_sysdir_t which, const char *paths);
+
+/**
+ * Free the configuration file search paths.
+ */
+extern void git_sysdir_global_shutdown(void);
+
+#endif /* INCLUDE_sysdir_h__ */
diff --git a/src/trace.h b/src/trace.h
index 77b1e03ef..4d4e3bf53 100644
--- a/src/trace.h
+++ b/src/trace.h
@@ -7,8 +7,6 @@
#ifndef INCLUDE_trace_h__
#define INCLUDE_trace_h__
-#include <stdarg.h>
-
#include <git2/trace.h>
#include "buffer.h"
diff --git a/src/tree.c b/src/tree.c
index 877a3fcee..94f779eca 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -283,7 +283,8 @@ static const git_tree_entry *entry_fromname(
{
size_t idx;
- assert(tree->entries.sorted); /* be safe when we cast away constness */
+ /* be safe when we cast away constness - i.e. don't trigger a sort */
+ assert(git_vector_is_sorted(&tree->entries));
if (tree_key_search(&idx, (git_vector *)&tree->entries, name, name_len) < 0)
return NULL;
@@ -333,7 +334,8 @@ int git_tree__prefix_position(const git_tree *tree, const char *path)
ksearch.filename = path;
ksearch.filename_len = strlen(path);
- assert(tree->entries.sorted); /* be safe when we cast away constness */
+ /* be safe when we cast away constness - i.e. don't trigger a sort */
+ assert(git_vector_is_sorted(&tree->entries));
/* Find tree entry with appropriate prefix */
git_vector_bsearch2(
diff --git a/src/util.h b/src/util.h
index f9de909e9..e378786d9 100644
--- a/src/util.h
+++ b/src/util.h
@@ -8,6 +8,7 @@
#define INCLUDE_util_h__
#include "common.h"
+#include "strnlen.h"
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define bitsizeof(x) (CHAR_BIT * sizeof(x))
@@ -50,8 +51,7 @@ GIT_INLINE(char *) git__strndup(const char *str, size_t n)
size_t length = 0;
char *ptr;
- while (length < n && str[length])
- ++length;
+ length = p_strnlen(str, n);
ptr = (char*)git__malloc(length + 1);
diff --git a/src/vector.c b/src/vector.c
index f0c2f06c2..e5d8919d3 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -56,7 +56,9 @@ int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp)
v->_alloc_size = src->length;
v->_cmp = cmp;
v->length = src->length;
- v->sorted = src->sorted && cmp == src->_cmp;
+ v->flags = src->flags;
+ if (cmp != src->_cmp)
+ git_vector_set_sorted(v, 0);
v->contents = git__malloc(bytes);
GITERR_CHECK_ALLOC(v->contents);
@@ -97,7 +99,7 @@ int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp)
v->_alloc_size = 0;
v->_cmp = cmp;
v->length = 0;
- v->sorted = 1;
+ v->flags = GIT_VECTOR_SORTED;
v->contents = NULL;
return resize_vector(v, max(initial_size, MIN_ALLOCSIZE));
@@ -128,7 +130,8 @@ int git_vector_insert(git_vector *v, void *element)
return -1;
v->contents[v->length++] = element;
- v->sorted = 0;
+
+ git_vector_set_sorted(v, v->length <= 1);
return 0;
}
@@ -141,7 +144,7 @@ int git_vector_insert_sorted(
assert(v && v->_cmp);
- if (!v->sorted)
+ if (!git_vector_is_sorted(v))
git_vector_sort(v);
if (v->length >= v->_alloc_size &&
@@ -171,11 +174,11 @@ void git_vector_sort(git_vector *v)
{
assert(v);
- if (v->sorted || !v->_cmp)
+ if (git_vector_is_sorted(v) || !v->_cmp)
return;
git__tsort(v->contents, v->length, v->_cmp);
- v->sorted = 1;
+ git_vector_set_sorted(v, 1);
}
int git_vector_bsearch2(
@@ -291,7 +294,7 @@ void git_vector_clear(git_vector *v)
{
assert(v);
v->length = 0;
- v->sorted = 1;
+ git_vector_set_sorted(v, 1);
}
void git_vector_swap(git_vector *a, git_vector *b)
diff --git a/src/vector.h b/src/vector.h
index d318463c6..682b6ad27 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -7,16 +7,21 @@
#ifndef INCLUDE_vector_h__
#define INCLUDE_vector_h__
-#include "git2/common.h"
+#include "common.h"
typedef int (*git_vector_cmp)(const void *, const void *);
+enum {
+ GIT_VECTOR_SORTED = (1u << 0),
+ GIT_VECTOR_FLAG_MAX = (1u << 1),
+};
+
typedef struct git_vector {
size_t _alloc_size;
git_vector_cmp _cmp;
void **contents;
size_t length;
- int sorted;
+ uint32_t flags;
} git_vector;
#define GIT_VECTOR_INIT {0}
@@ -86,12 +91,20 @@ void git_vector_remove_matching(
int git_vector_resize_to(git_vector *v, size_t new_length);
int git_vector_set(void **old, git_vector *v, size_t position, void *value);
+/** Check if vector is sorted */
+#define git_vector_is_sorted(V) (((V)->flags & GIT_VECTOR_SORTED) != 0)
+
+/** Directly set sorted state of vector */
+#define git_vector_set_sorted(V,S) do { \
+ (V)->flags = (S) ? ((V)->flags | GIT_VECTOR_SORTED) : \
+ ((V)->flags & ~GIT_VECTOR_SORTED); } while (0)
+
/** Set the comparison function used for sorting the vector */
GIT_INLINE(void) git_vector_set_cmp(git_vector *v, git_vector_cmp cmp)
{
if (cmp != v->_cmp) {
v->_cmp = cmp;
- v->sorted = 0;
+ git_vector_set_sorted(v, 0);
}
}
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index d4dbfbab9..a96385f10 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -8,68 +8,6 @@
#include "common.h"
#include "utf-conv.h"
-#define U16_LEAD(c) (wchar_t)(((c)>>10)+0xd7c0)
-#define U16_TRAIL(c) (wchar_t)(((c)&0x3ff)|0xdc00)
-
-#if 0
-void git__utf8_to_16(wchar_t *dest, size_t length, const char *src)
-{
- wchar_t *pDest = dest;
- uint32_t ch;
- const uint8_t* pSrc = (uint8_t*) src;
-
- assert(dest && src && length);
-
- length--;
-
- while(*pSrc && length > 0) {
- ch = *pSrc++;
- length--;
-
- if(ch < 0xc0) {
- /*
- * ASCII, or a trail byte in lead position which is treated like
- * a single-byte sequence for better character boundary
- * resynchronization after illegal sequences.
- */
- *pDest++ = (wchar_t)ch;
- continue;
- } else if(ch < 0xe0) { /* U+0080..U+07FF */
- if (pSrc[0]) {
- /* 0x3080 = (0xc0 << 6) + 0x80 */
- *pDest++ = (wchar_t)((ch << 6) + *pSrc++ - 0x3080);
- continue;
- }
- } else if(ch < 0xf0) { /* U+0800..U+FFFF */
- if (pSrc[0] && pSrc[1]) {
- /* no need for (ch & 0xf) because the upper bits are truncated after <<12 in the cast to (UChar) */
- /* 0x2080 = (0x80 << 6) + 0x80 */
- ch = (ch << 12) + (*pSrc++ << 6);
- *pDest++ = (wchar_t)(ch + *pSrc++ - 0x2080);
- continue;
- }
- } else /* f0..f4 */ { /* U+10000..U+10FFFF */
- if (length >= 1 && pSrc[0] && pSrc[1] && pSrc[2]) {
- /* 0x3c82080 = (0xf0 << 18) + (0x80 << 12) + (0x80 << 6) + 0x80 */
- ch = (ch << 18) + (*pSrc++ << 12);
- ch += *pSrc++ << 6;
- ch += *pSrc++ - 0x3c82080;
- *(pDest++) = U16_LEAD(ch);
- *(pDest++) = U16_TRAIL(ch);
- length--; /* two bytes for this character */
- continue;
- }
- }
-
- /* truncated character at the end */
- *pDest++ = 0xfffd;
- break;
- }
-
- *pDest++ = 0x0;
-}
-#endif
-
int git__utf8_to_16(wchar_t * dest, size_t dest_size, const char *src)
{
return MultiByteToWideChar(CP_UTF8, 0, src, -1, dest, (int)dest_size);
diff --git a/tests/blame/buffer.c b/tests/blame/buffer.c
index 912ee9846..340b1dced 100644
--- a/tests/blame/buffer.c
+++ b/tests/blame/buffer.c
@@ -1,7 +1,7 @@
#include "blame_helpers.h"
-git_repository *g_repo;
-git_blame *g_fileblame, *g_bufferblame;
+static git_repository *g_repo;
+static git_blame *g_fileblame, *g_bufferblame;
void test_blame_buffer__initialize(void)
{
diff --git a/tests/blame/simple.c b/tests/blame/simple.c
index 79bd56b83..11ff4cd19 100644
--- a/tests/blame/simple.c
+++ b/tests/blame/simple.c
@@ -303,3 +303,18 @@ void test_blame_simple__can_restrict_to_newish_commits(void)
check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 1, "be3563a", "branch_file.txt");
check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "a65fedf", "branch_file.txt");
}
+
+void test_blame_simple__can_restrict_to_first_parent_commits(void)
+{
+ git_blame_options opts = GIT_BLAME_OPTIONS_INIT;
+ opts.flags |= GIT_BLAME_FIRST_PARENT;
+
+ cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git")));
+
+ cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts));
+ cl_assert_equal_i(4, git_blame_get_hunk_count(g_blame));
+ check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt");
+ check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt");
+ check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, 0, "63d671eb", "b.txt");
+ check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, 0, "bc7c5ac2", "b.txt");
+}
diff --git a/tests/checkout/conflict.c b/tests/checkout/conflict.c
index a8b93b28d..2fea511da 100644
--- a/tests/checkout/conflict.c
+++ b/tests/checkout/conflict.c
@@ -61,12 +61,19 @@ struct checkout_name_entry {
void test_checkout_conflict__initialize(void)
{
+ git_config *cfg;
+
g_repo = cl_git_sandbox_init(TEST_REPO_PATH);
git_repository_index(&g_index, g_repo);
cl_git_rewritefile(
TEST_REPO_PATH "/.gitattributes",
"* text eol=lf\n");
+
+ /* Ensure that the user's merge.conflictstyle doesn't interfere */
+ cl_git_pass(git_repository_config(&cfg, g_repo));
+ cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge"));
+ git_config_free(cfg);
}
void test_checkout_conflict__cleanup(void)
diff --git a/tests/checkout/crlf.c b/tests/checkout/crlf.c
index cba79432f..0d78ee039 100644
--- a/tests/checkout/crlf.c
+++ b/tests/checkout/crlf.c
@@ -18,19 +18,6 @@ void test_checkout_crlf__cleanup(void)
cl_git_sandbox_cleanup();
}
-void test_checkout_crlf__detect_crlf_autocrlf_false(void)
-{
- git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
- opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
-
- cl_repo_set_bool(g_repo, "core.autocrlf", false);
-
- git_checkout_head(g_repo, &opts);
-
- check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
- check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
-}
-
void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
git_index *index;
@@ -100,18 +87,6 @@ void test_checkout_crlf__more_crlf_autocrlf_true(void)
check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF);
}
-void test_checkout_crlf__all_crlf_autocrlf_true(void)
-{
- git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
- opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
-
- cl_repo_set_bool(g_repo, "core.autocrlf", true);
-
- git_checkout_head(g_repo, &opts);
-
- check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
-}
-
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
git_index *index;
@@ -229,3 +204,97 @@ void test_checkout_crlf__with_ident(void)
git_index_free(index);
}
+
+void test_checkout_crlf__autocrlf_false_no_attrs(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", false);
+
+ git_checkout_head(g_repo, &opts);
+
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+}
+
+void test_checkout_crlf__autocrlf_true_no_attrs(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+ git_checkout_head(g_repo, &opts);
+
+ if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
+ } else {
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+ }
+}
+
+void test_checkout_crlf__autocrlf_input_no_attrs(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_repo_set_string(g_repo, "core.autocrlf", "input");
+
+ git_checkout_head(g_repo, &opts);
+
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+}
+
+void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", false);
+
+ git_checkout_head(g_repo, &opts);
+
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+}
+
+void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+ git_checkout_head(g_repo, &opts);
+
+ if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
+ } else {
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+ }
+}
+
+void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
+
+ cl_repo_set_string(g_repo, "core.autocrlf", "input");
+
+ git_checkout_head(g_repo, &opts);
+
+ check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
+ check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
+}
diff --git a/tests/checkout/tree.c b/tests/checkout/tree.c
index 407908ad3..f433b2698 100644
--- a/tests/checkout/tree.c
+++ b/tests/checkout/tree.c
@@ -571,7 +571,7 @@ void test_checkout_tree__donot_update_deleted_file_by_default(void)
cl_git_pass(git_oid_fromstr(&old_id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_commit_lookup(&old_commit, g_repo, &old_id));
- cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(p_unlink("testrepo/branch_file.txt"));
cl_git_pass(git_index_remove_bypath(index ,"branch_file.txt"));
diff --git a/tests/clar_libgit2.c b/tests/clar_libgit2.c
index 50762cdb8..9b062ef78 100644
--- a/tests/clar_libgit2.c
+++ b/tests/clar_libgit2.c
@@ -420,6 +420,14 @@ int cl_repo_get_bool(git_repository *repo, const char *cfg)
return val;
}
+void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value)
+{
+ git_config *config;
+ cl_git_pass(git_repository_config(&config, repo));
+ cl_git_pass(git_config_set_string(config, cfg, value));
+ git_config_free(config);
+}
+
/* this is essentially the code from git__unescape modified slightly */
static size_t strip_cr_from_buf(char *start, size_t len)
{
@@ -468,6 +476,7 @@ void clar__assert_equal_file(
p_snprintf(
buf, sizeof(buf), "file content mismatch at byte %d",
(int)(total_bytes + pos));
+ p_close(fd);
clar__fail(file, line, buf, path, 1);
}
diff --git a/tests/clar_libgit2.h b/tests/clar_libgit2.h
index b9ef5627e..915111244 100644
--- a/tests/clar_libgit2.h
+++ b/tests/clar_libgit2.h
@@ -116,4 +116,6 @@ void cl_repo_commit_from_index(
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
int cl_repo_get_bool(git_repository *repo, const char *cfg);
+void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
+
#endif
diff --git a/tests/commit/write.c b/tests/commit/write.c
index 8e5b67f2f..b1cdf4485 100644
--- a/tests/commit/write.c
+++ b/tests/commit/write.c
@@ -7,6 +7,8 @@ static const char *commit_message = "This commit has been created in memory\n\
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
static const char *root_commit_message = "This is a root commit\n\
This is a root commit and should be the only one in this branch\n";
+static const char *root_reflog_message = "commit (initial): This is a root commit \
+ This is a root commit and should be the only one in this branch";
static char *head_old;
static git_reference *head, *branch;
static git_commit *commit;
@@ -101,6 +103,8 @@ void test_commit_write__root(void)
git_signature *author, *committer;
const char *branch_name = "refs/heads/root-commit-branch";
git_tree *tree;
+ git_reflog *log;
+ const git_reflog_entry *entry;
git_oid_fromstr(&tree_id, tree_oid);
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
@@ -130,7 +134,6 @@ void test_commit_write__root(void)
0));
git_object_free((git_object *)tree);
- git_signature_free(committer);
git_signature_free(author);
/*
@@ -144,4 +147,14 @@ void test_commit_write__root(void)
branch_oid = git_reference_target(branch);
cl_git_pass(git_oid_cmp(branch_oid, &commit_id));
cl_assert_equal_s(root_commit_message, git_commit_message(commit));
+
+ cl_git_pass(git_reflog_read(&log, g_repo, branch_name));
+ cl_assert_equal_i(1, git_reflog_entrycount(log));
+ entry = git_reflog_entry_byindex(log, 0);
+ cl_assert_equal_s(committer->email, git_reflog_entry_committer(entry)->email);
+ cl_assert_equal_s(committer->name, git_reflog_entry_committer(entry)->name);
+ cl_assert_equal_s(root_reflog_message, git_reflog_entry_message(entry));
+
+ git_signature_free(committer);
+ git_reflog_free(log);
}
diff --git a/tests/core/env.c b/tests/core/env.c
index 0fa6472d7..a32f5ed3e 100644
--- a/tests/core/env.c
+++ b/tests/core/env.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "fileops.h"
+#include "sysdir.h"
#include "path.h"
#ifdef GIT_WIN32
@@ -41,12 +42,12 @@ void test_core_env__initialize(void)
static void reset_global_search_path(void)
{
- cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_GLOBAL, NULL));
+ cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
}
static void reset_system_search_path(void)
{
- cl_git_pass(git_futils_dirs_set(GIT_FUTILS_DIR_SYSTEM, NULL));
+ cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
}
void test_core_env__cleanup(void)
@@ -120,18 +121,18 @@ void test_core_env__0(void)
git_buf_rtruncate_at_char(&path, '/');
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
setenv_and_check("HOME", path.ptr);
reset_global_search_path();
- cl_git_pass(git_futils_find_global_file(&found, testfile));
+ cl_git_pass(git_sysdir_find_global_file(&found, testfile));
cl_setenv("HOME", env_save[0]);
reset_global_search_path();
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
#ifdef GIT_WIN32
setenv_and_check("HOMEDRIVE", NULL);
@@ -139,7 +140,7 @@ void test_core_env__0(void)
setenv_and_check("USERPROFILE", path.ptr);
reset_global_search_path();
- cl_git_pass(git_futils_find_global_file(&found, testfile));
+ cl_git_pass(git_sysdir_find_global_file(&found, testfile));
{
int root = git_path_root(path.ptr);
@@ -150,7 +151,7 @@ void test_core_env__0(void)
reset_global_search_path();
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
old = path.ptr[root];
path.ptr[root] = '\0';
@@ -159,7 +160,7 @@ void test_core_env__0(void)
setenv_and_check("HOMEPATH", &path.ptr[root]);
reset_global_search_path();
- cl_git_pass(git_futils_find_global_file(&found, testfile));
+ cl_git_pass(git_sysdir_find_global_file(&found, testfile));
}
}
#endif
@@ -177,7 +178,7 @@ void test_core_env__1(void)
git_buf path = GIT_BUF_INIT;
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
cl_git_pass(cl_setenv("HOME", "doesnotexist"));
#ifdef GIT_WIN32
@@ -187,7 +188,7 @@ void test_core_env__1(void)
reset_global_search_path();
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
cl_git_pass(cl_setenv("HOME", NULL));
#ifdef GIT_WIN32
@@ -198,17 +199,17 @@ void test_core_env__1(void)
reset_system_search_path();
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&path, "nonexistentfile"));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
+ GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
#ifdef GIT_WIN32
cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
reset_system_search_path();
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_system_file(&path, "nonexistentfile"));
+ GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
#endif
git_buf_free(&path);
@@ -242,13 +243,13 @@ static void check_global_searchpath(
cl_assert_equal_s(out, path);
/* find file using new path */
- cl_git_pass(git_futils_find_global_file(temp, file));
+ cl_git_pass(git_sysdir_find_global_file(temp, file));
/* reset path and confirm file not found */
cl_git_pass(git_libgit2_opts(
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(temp, file));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
}
void test_core_env__2(void)
@@ -285,7 +286,7 @@ void test_core_env__2(void)
/* default should be NOTFOUND */
cl_assert_equal_i(
- GIT_ENOTFOUND, git_futils_find_global_file(&found, testfile));
+ GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
/* try plain, append $PATH, and prepend $PATH */
check_global_searchpath(path.ptr, 0, testfile, &found);
diff --git a/tests/core/pqueue.c b/tests/core/pqueue.c
new file mode 100644
index 000000000..d91dbb0cd
--- /dev/null
+++ b/tests/core/pqueue.c
@@ -0,0 +1,97 @@
+#include "clar_libgit2.h"
+#include "pqueue.h"
+
+static int cmp_ints(const void *v1, const void *v2)
+{
+ int i1 = *(int *)v1, i2 = *(int *)v2;
+ return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;
+}
+
+void test_core_pqueue__items_are_put_in_order(void)
+{
+ git_pqueue pq;
+ int i, vals[20];
+
+ cl_git_pass(git_pqueue_init(&pq, 0, 20, cmp_ints));
+
+ for (i = 0; i < 20; ++i) {
+ if (i < 10)
+ vals[i] = 10 - i; /* 10 down to 1 */
+ else
+ vals[i] = i + 1; /* 11 up to 20 */
+
+ cl_git_pass(git_pqueue_insert(&pq, &vals[i]));
+ }
+
+ cl_assert_equal_i(20, git_pqueue_size(&pq));
+
+ for (i = 1; i <= 20; ++i) {
+ void *p = git_pqueue_pop(&pq);
+ cl_assert(p);
+ cl_assert_equal_i(i, *(int *)p);
+ }
+
+ cl_assert_equal_i(0, git_pqueue_size(&pq));
+
+ git_pqueue_free(&pq);
+}
+
+void test_core_pqueue__interleave_inserts_and_pops(void)
+{
+ git_pqueue pq;
+ int chunk, v, i, vals[200];
+
+ cl_git_pass(git_pqueue_init(&pq, 0, 20, cmp_ints));
+
+ for (v = 0, chunk = 20; chunk <= 200; chunk += 20) {
+ /* push the next 20 */
+ for (; v < chunk; ++v) {
+ vals[v] = (v & 1) ? 200 - v : v;
+ cl_git_pass(git_pqueue_insert(&pq, &vals[v]));
+ }
+
+ /* pop the lowest 10 */
+ for (i = 0; i < 10; ++i)
+ (void)git_pqueue_pop(&pq);
+ }
+
+ cl_assert_equal_i(100, git_pqueue_size(&pq));
+
+ /* at this point, we've popped 0-99 */
+
+ for (v = 100; v < 200; ++v) {
+ void *p = git_pqueue_pop(&pq);
+ cl_assert(p);
+ cl_assert_equal_i(v, *(int *)p);
+ }
+
+ cl_assert_equal_i(0, git_pqueue_size(&pq));
+
+ git_pqueue_free(&pq);
+}
+
+void test_core_pqueue__max_heap_size(void)
+{
+ git_pqueue pq;
+ int i, vals[100];
+
+ cl_git_pass(git_pqueue_init(&pq, GIT_PQUEUE_FIXED_SIZE, 50, cmp_ints));
+
+ for (i = 0; i < 100; ++i) {
+ vals[i] = (i & 1) ? 100 - i : i;
+ cl_git_pass(git_pqueue_insert(&pq, &vals[i]));
+ }
+
+ cl_assert_equal_i(50, git_pqueue_size(&pq));
+
+ for (i = 50; i < 100; ++i) {
+ void *p = git_pqueue_pop(&pq);
+ cl_assert(p);
+ cl_assert_equal_i(i, *(int *)p);
+ }
+
+ cl_assert_equal_i(0, git_pqueue_size(&pq));
+
+ git_pqueue_free(&pq);
+
+}
diff --git a/tests/diff/blob.c b/tests/diff/blob.c
index 74df3cf85..d1fff9c5b 100644
--- a/tests/diff/blob.c
+++ b/tests/diff/blob.c
@@ -51,6 +51,20 @@ void test_diff_blob__cleanup(void)
cl_git_sandbox_cleanup();
}
+static void assert_one_modified(
+ int hunks, int lines, int ctxt, int adds, int dels, diff_expects *exp)
+{
+ cl_assert_equal_i(1, exp->files);
+ cl_assert_equal_i(1, exp->file_status[GIT_DELTA_MODIFIED]);
+ cl_assert_equal_i(0, exp->files_binary);
+
+ cl_assert_equal_i(hunks, exp->hunks);
+ cl_assert_equal_i(lines, exp->lines);
+ cl_assert_equal_i(ctxt, exp->line_ctxt);
+ cl_assert_equal_i(adds, exp->line_adds);
+ cl_assert_equal_i(dels, exp->line_dels);
+}
+
void test_diff_blob__can_compare_text_blobs(void)
{
git_blob *a, *b, *c;
@@ -71,79 +85,81 @@ void test_diff_blob__can_compare_text_blobs(void)
/* Doing the equivalent of a `git diff -U1` on these files */
/* diff on tests/resources/attr/root_test1 */
+ memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
a, NULL, b, NULL, &opts,
diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
+ assert_one_modified(1, 6, 1, 5, 0, &expected);
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
-
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(6, expected.lines);
- cl_assert_equal_i(1, expected.line_ctxt);
- cl_assert_equal_i(5, expected.line_adds);
- cl_assert_equal_i(0, expected.line_dels);
+ /* same diff but use direct buffers */
+ memset(&expected, 0, sizeof(expected));
+ cl_git_pass(git_diff_buffers(
+ git_blob_rawcontent(a), (size_t)git_blob_rawsize(a), NULL,
+ git_blob_rawcontent(b), (size_t)git_blob_rawsize(b), NULL, &opts,
+ diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
+ assert_one_modified(1, 6, 1, 5, 0, &expected);
/* diff on tests/resources/attr/root_test2 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
b, NULL, c, NULL, &opts,
diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
-
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
-
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(15, expected.lines);
- cl_assert_equal_i(3, expected.line_ctxt);
- cl_assert_equal_i(9, expected.line_adds);
- cl_assert_equal_i(3, expected.line_dels);
+ assert_one_modified(1, 15, 3, 9, 3, &expected);
/* diff on tests/resources/attr/root_test3 */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
a, NULL, c, NULL, &opts,
diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
-
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
-
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(13, expected.lines);
- cl_assert_equal_i(0, expected.line_ctxt);
- cl_assert_equal_i(12, expected.line_adds);
- cl_assert_equal_i(1, expected.line_dels);
+ assert_one_modified(1, 13, 0, 12, 1, &expected);
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
c, NULL, d, NULL, &opts,
diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
-
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
-
- cl_assert_equal_i(2, expected.hunks);
- cl_assert_equal_i(14, expected.lines);
- cl_assert_equal_i(4, expected.line_ctxt);
- cl_assert_equal_i(6, expected.line_adds);
- cl_assert_equal_i(4, expected.line_dels);
+ assert_one_modified(2, 14, 4, 6, 4, &expected);
git_blob_free(a);
git_blob_free(b);
git_blob_free(c);
}
+static void assert_patch_matches_blobs(
+ git_patch *p, git_blob *a, git_blob *b,
+ int hunks, int l0, int l1, int ctxt, int adds, int dels)
+{
+ const git_diff_delta *delta;
+ size_t tc, ta, td;
+
+ cl_assert(p != NULL);
+
+ delta = git_patch_get_delta(p);
+ cl_assert(delta != NULL);
+
+ cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
+ cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.id));
+ cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size);
+ cl_assert(git_oid_equal(git_blob_id(b), &delta->new_file.id));
+ cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size);
+
+ cl_assert_equal_i(hunks, (int)git_patch_num_hunks(p));
+
+ if (hunks > 0)
+ cl_assert_equal_i(l0, git_patch_num_lines_in_hunk(p, 0));
+ if (hunks > 1)
+ cl_assert_equal_i(l1, git_patch_num_lines_in_hunk(p, 1));
+
+ cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
+ cl_assert_equal_i(ctxt, (int)tc);
+ cl_assert_equal_i(adds, (int)ta);
+ cl_assert_equal_i(dels, (int)td);
+}
+
void test_diff_blob__can_compare_text_blobs_with_patch(void)
{
git_blob *a, *b, *c;
git_oid a_oid, b_oid, c_oid;
git_patch *p;
- const git_diff_delta *delta;
- size_t tc, ta, td;
/* tests/resources/attr/root_test1 */
cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
@@ -161,92 +177,22 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void)
/* diff on tests/resources/attr/root_test1 */
cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts));
-
- cl_assert(p != NULL);
-
- delta = git_patch_get_delta(p);
- cl_assert(delta != NULL);
- cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
- cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.id));
- cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size);
- cl_assert(git_oid_equal(git_blob_id(b), &delta->new_file.id));
- cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size);
-
- cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
- cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0));
-
- cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
- cl_assert_equal_i(1, (int)tc);
- cl_assert_equal_i(5, (int)ta);
- cl_assert_equal_i(0, (int)td);
-
+ assert_patch_matches_blobs(p, a, b, 1, 6, 0, 1, 5, 0);
git_patch_free(p);
/* diff on tests/resources/attr/root_test2 */
cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts));
-
- cl_assert(p != NULL);
-
- delta = git_patch_get_delta(p);
- cl_assert(delta != NULL);
- cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
- cl_assert(git_oid_equal(git_blob_id(b), &delta->old_file.id));
- cl_assert_equal_sz(git_blob_rawsize(b), delta->old_file.size);
- cl_assert(git_oid_equal(git_blob_id(c), &delta->new_file.id));
- cl_assert_equal_sz(git_blob_rawsize(c), delta->new_file.size);
-
- cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
- cl_assert_equal_i(15, git_patch_num_lines_in_hunk(p, 0));
-
- cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
- cl_assert_equal_i(3, (int)tc);
- cl_assert_equal_i(9, (int)ta);
- cl_assert_equal_i(3, (int)td);
-
+ assert_patch_matches_blobs(p, b, c, 1, 15, 0, 3, 9, 3);
git_patch_free(p);
/* diff on tests/resources/attr/root_test3 */
cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts));
-
- cl_assert(p != NULL);
-
- delta = git_patch_get_delta(p);
- cl_assert(delta != NULL);
- cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
- cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.id));
- cl_assert_equal_sz(git_blob_rawsize(a), delta->old_file.size);
- cl_assert(git_oid_equal(git_blob_id(c), &delta->new_file.id));
- cl_assert_equal_sz(git_blob_rawsize(c), delta->new_file.size);
-
- cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
- cl_assert_equal_i(0, (int)tc);
- cl_assert_equal_i(12, (int)ta);
- cl_assert_equal_i(1, (int)td);
-
+ assert_patch_matches_blobs(p, a, c, 1, 13, 0, 0, 12, 1);
git_patch_free(p);
/* one more */
cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts));
-
- cl_assert(p != NULL);
-
- delta = git_patch_get_delta(p);
- cl_assert(delta != NULL);
- cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status);
- cl_assert(git_oid_equal(git_blob_id(c), &delta->old_file.id));
- cl_assert_equal_sz(git_blob_rawsize(c), delta->old_file.size);
- cl_assert(git_oid_equal(git_blob_id(d), &delta->new_file.id));
- cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size);
-
- cl_assert_equal_i(2, (int)git_patch_num_hunks(p));
- cl_assert_equal_i(5, git_patch_num_lines_in_hunk(p, 0));
- cl_assert_equal_i(9, git_patch_num_lines_in_hunk(p, 1));
-
- cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p));
- cl_assert_equal_i(4, (int)tc);
- cl_assert_equal_i(6, (int)ta);
- cl_assert_equal_i(4, (int)td);
-
+ assert_patch_matches_blobs(p, c, d, 2, 5, 9, 4, 6, 4);
git_patch_free(p);
git_blob_free(a);
@@ -656,14 +602,7 @@ void test_diff_blob__can_compare_blob_to_buffer(void)
/* diff from blob a to content of b */
quick_diff_blob_to_str(a, NULL, b_content, 0, NULL);
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(6, expected.lines);
- cl_assert_equal_i(1, expected.line_ctxt);
- cl_assert_equal_i(5, expected.line_adds);
- cl_assert_equal_i(0, expected.line_dels);
+ assert_one_modified(1, 6, 1, 5, 0, &expected);
/* diff from blob a to content of a */
opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
@@ -910,14 +849,7 @@ void test_diff_blob__using_path_and_attributes(void)
changed = "Hello from the root\nMore lines\nAnd more\nGo here\n";
quick_diff_blob_to_str(nonbin, NULL, changed, 0, NULL);
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(3, expected.lines);
- cl_assert_equal_i(0, expected.line_ctxt);
- cl_assert_equal_i(3, expected.line_adds);
- cl_assert_equal_i(0, expected.line_dels);
+ assert_one_modified(1, 3, 0, 3, 0, &expected);
quick_diff_blob_to_str(nonbin, "foo/bar.binary", changed, 0, NULL);
cl_assert_equal_i(1, expected.files);
@@ -925,29 +857,12 @@ void test_diff_blob__using_path_and_attributes(void)
cl_assert_equal_i(1, expected.files_binary);
cl_assert_equal_i(0, expected.hunks);
cl_assert_equal_i(0, expected.lines);
- cl_assert_equal_i(0, expected.line_ctxt);
- cl_assert_equal_i(0, expected.line_adds);
- cl_assert_equal_i(0, expected.line_dels);
quick_diff_blob_to_str(nonbin, "foo/bar.textary", changed, 0, NULL);
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(3, expected.lines);
- cl_assert_equal_i(0, expected.line_ctxt);
- cl_assert_equal_i(3, expected.line_adds);
- cl_assert_equal_i(0, expected.line_dels);
+ assert_one_modified(1, 3, 0, 3, 0, &expected);
quick_diff_blob_to_str(nonbin, "foo/bar.alphary", changed, 0, NULL);
- cl_assert_equal_i(1, expected.files);
- cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]);
- cl_assert_equal_i(0, expected.files_binary);
- cl_assert_equal_i(1, expected.hunks);
- cl_assert_equal_i(3, expected.lines);
- cl_assert_equal_i(0, expected.line_ctxt);
- cl_assert_equal_i(3, expected.line_adds);
- cl_assert_equal_i(0, expected.line_dels);
+ assert_one_modified(1, 3, 0, 3, 0, &expected);
cl_git_pass(git_patch_from_blob_and_buffer(
&p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts));
@@ -1066,3 +981,28 @@ void test_diff_blob__using_path_and_attributes(void)
git_blob_free(nonbin);
git_blob_free(bin);
}
+
+void test_diff_blob__can_compare_buffer_to_buffer(void)
+{
+ const char *a = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n";
+ const char *b = "a\nB\nc\nd\nE\nF\nh\nj\nk\n";
+
+ opts.interhunk_lines = 0;
+ opts.context_lines = 0;
+
+ memset(&expected, 0, sizeof(expected));
+
+ cl_git_pass(git_diff_buffers(
+ a, strlen(a), NULL, b, strlen(b), NULL,
+ &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
+ assert_one_modified(4, 9, 0, 4, 5, &expected);
+
+ opts.flags ^= GIT_DIFF_REVERSE;
+
+ memset(&expected, 0, sizeof(expected));
+
+ cl_git_pass(git_diff_buffers(
+ a, strlen(a), NULL, b, strlen(b), NULL,
+ &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
+ assert_one_modified(4, 9, 0, 5, 4, &expected);
+}
diff --git a/tests/filter/blob.c b/tests/filter/blob.c
index 8dce6470a..48edb8405 100644
--- a/tests/filter/blob.c
+++ b/tests/filter/blob.c
@@ -41,7 +41,8 @@ void test_filter_blob__all_crlf(void)
cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1));
- cl_assert_equal_s(ALL_CRLF_TEXT_AS_LF, buf.ptr);
+ /* we never convert CRLF -> LF on platforms that have LF */
+ cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);
git_buf_free(&buf);
git_blob_free(blob);
diff --git a/tests/index/addall.c b/tests/index/addall.c
index 452733710..a7e2583b2 100644
--- a/tests/index/addall.c
+++ b/tests/index/addall.c
@@ -3,7 +3,7 @@
#include "posix.h"
#include "fileops.h"
-git_repository *g_repo = NULL;
+static git_repository *g_repo = NULL;
#define TEST_DIR "addall"
void test_index_addall__initialize(void)
diff --git a/tests/index/crlf.c b/tests/index/crlf.c
new file mode 100644
index 000000000..cf69c6226
--- /dev/null
+++ b/tests/index/crlf.c
@@ -0,0 +1,136 @@
+#include "clar_libgit2.h"
+#include "../filter/crlf.h"
+
+#include "git2/checkout.h"
+#include "repository.h"
+#include "posix.h"
+
+#define FILE_CONTENTS_LF "one\ntwo\nthree\nfour\n"
+#define FILE_CONTENTS_CRLF "one\r\ntwo\r\nthree\r\nfour\r\n"
+
+#define FILE_OID_LF "f384549cbeb481e437091320de6d1f2e15e11b4a"
+#define FILE_OID_CRLF "7fbf4d847b191141d80f30c8ab03d2ad4cd543a9"
+
+static git_repository *g_repo;
+static git_index *g_index;
+
+void test_index_crlf__initialize(void)
+{
+ g_repo = cl_git_sandbox_init("crlf");
+ cl_git_pass(git_repository_index(&g_index, g_repo));
+}
+
+void test_index_crlf__cleanup(void)
+{
+ git_index_free(g_index);
+ cl_git_sandbox_cleanup();
+}
+
+void test_index_crlf__autocrlf_false_no_attrs(void)
+{
+ const git_index_entry *entry;
+ git_oid oid;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", false);
+
+ cl_git_mkfile("./crlf/newfile.txt",
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
+
+ cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
+ entry = git_index_get_bypath(g_index, "newfile.txt", 0);
+
+ cl_git_pass(git_oid_fromstr(&oid,
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_OID_CRLF : FILE_OID_LF));
+ cl_assert(git_oid_cmp(&oid, &entry->id) == 0);
+}
+
+void test_index_crlf__autocrlf_true_no_attrs(void)
+{
+ const git_index_entry *entry;
+ git_oid oid;
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", true);
+
+ cl_git_mkfile("./crlf/newfile.txt",
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
+
+ cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
+ entry = git_index_get_bypath(g_index, "newfile.txt", 0);
+
+ cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
+ cl_assert(git_oid_cmp(&oid, &entry->id) == 0);
+}
+
+void test_index_crlf__autocrlf_input_no_attrs(void)
+{
+ const git_index_entry *entry;
+ git_oid oid;
+
+ cl_repo_set_string(g_repo, "core.autocrlf", "input");
+
+ cl_git_mkfile("./crlf/newfile.txt",
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
+
+ cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
+ entry = git_index_get_bypath(g_index, "newfile.txt", 0);
+
+ cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
+ cl_assert(git_oid_cmp(&oid, &entry->id) == 0);
+}
+
+void test_index_crlf__autocrlf_false_text_auto_attr(void)
+{
+ const git_index_entry *entry;
+ git_oid oid;
+
+ cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", false);
+
+ cl_git_mkfile("./crlf/newfile.txt",
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
+
+ cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
+ entry = git_index_get_bypath(g_index, "newfile.txt", 0);
+
+ cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
+ cl_assert(git_oid_cmp(&oid, &entry->id) == 0);
+}
+
+void test_index_crlf__autocrlf_true_text_auto_attr(void)
+{
+ const git_index_entry *entry;
+ git_oid oid;
+
+ cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
+
+ cl_repo_set_bool(g_repo, "core.autocrlf", false);
+
+ cl_git_mkfile("./crlf/newfile.txt",
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
+
+ cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
+ entry = git_index_get_bypath(g_index, "newfile.txt", 0);
+
+ cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
+ cl_assert(git_oid_cmp(&oid, &entry->id) == 0);
+}
+
+void test_index_crlf__autocrlf_input_text_auto_attr(void)
+{
+ const git_index_entry *entry;
+ git_oid oid;
+
+ cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
+
+ cl_repo_set_string(g_repo, "core.autocrlf", "input");
+
+ cl_git_mkfile("./crlf/newfile.txt",
+ (GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);
+
+ cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
+ entry = git_index_get_bypath(g_index, "newfile.txt", 0);
+
+ cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
+ cl_assert(git_oid_cmp(&oid, &entry->id) == 0);
+}
diff --git a/tests/index/names.c b/tests/index/names.c
index 9007b1b15..4723449d9 100644
--- a/tests/index/names.c
+++ b/tests/index/names.c
@@ -86,10 +86,10 @@ void test_index_names__cleaned_on_reset_hard(void)
{
git_object *target;
- retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+ cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_names__add();
- cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
cl_assert(git_index_name_entrycount(repo_index) == 0);
git_object_free(target);
@@ -99,10 +99,10 @@ void test_index_names__cleaned_on_reset_mixed(void)
{
git_object *target;
- retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+ cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_names__add();
- cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
cl_assert(git_index_name_entrycount(repo_index) == 0);
git_object_free(target);
diff --git a/tests/index/reuc.c b/tests/index/reuc.c
index a18d5602e..bf051c827 100644
--- a/tests/index/reuc.c
+++ b/tests/index/reuc.c
@@ -295,10 +295,10 @@ void test_index_reuc__cleaned_on_reset_hard(void)
{
git_object *target;
- retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+ cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_reuc__add();
- cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
cl_assert(reuc_entry_exists() == false);
git_object_free(target);
@@ -308,10 +308,10 @@ void test_index_reuc__cleaned_on_reset_mixed(void)
{
git_object *target;
- retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+ cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
test_index_reuc__add();
- cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
cl_assert(reuc_entry_exists() == false);
git_object_free(target);
@@ -321,12 +321,12 @@ void test_index_reuc__retained_on_reset_soft(void)
{
git_object *target;
- retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
+ cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
- git_reset(repo, target, GIT_RESET_HARD);
+ git_reset(repo, target, GIT_RESET_HARD, NULL, NULL);
test_index_reuc__add();
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert(reuc_entry_exists() == true);
git_object_free(target);
diff --git a/tests/index/tests.c b/tests/index/tests.c
index bd90bc557..6e28af1f7 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -80,7 +80,7 @@ void test_index_tests__empty_index(void)
cl_assert(index->on_disk == 0);
cl_assert(git_index_entrycount(index) == 0);
- cl_assert(index->entries.sorted);
+ cl_assert(git_vector_is_sorted(&index->entries));
git_index_free(index);
}
@@ -95,7 +95,7 @@ void test_index_tests__default_test_index(void)
cl_assert(index->on_disk);
cl_assert(git_index_entrycount(index) == index_entry_count);
- cl_assert(index->entries.sorted);
+ cl_assert(git_vector_is_sorted(&index->entries));
entries = (git_index_entry **)index->entries.contents;
@@ -118,7 +118,7 @@ void test_index_tests__gitgit_index(void)
cl_assert(index->on_disk);
cl_assert(git_index_entrycount(index) == index_entry_count_2);
- cl_assert(index->entries.sorted);
+ cl_assert(git_vector_is_sorted(&index->entries));
cl_assert(index->tree != NULL);
git_index_free(index);
@@ -195,7 +195,7 @@ void test_index_tests__sort1(void)
cl_git_pass(git_index_open(&index, "fake-index"));
/* FIXME: this test is slightly dumb */
- cl_assert(index->entries.sorted);
+ cl_assert(git_vector_is_sorted(&index->entries));
git_index_free(index);
}
@@ -543,3 +543,37 @@ void test_index_tests__corrupted_extension(void)
cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR);
}
+
+static void assert_index_is_sorted(git_index *index)
+{
+ git_vector *entries = &index->entries;
+ size_t i;
+
+ cl_assert(git_vector_is_sorted(entries));
+
+ for (i = 1; i < git_vector_length(entries); ++i) {
+ git_index_entry *prev = git_vector_get(entries, i - 1);
+ git_index_entry *curr = git_vector_get(entries, i);
+ cl_assert(index->entries._cmp(prev, curr) <= 0);
+ }
+}
+
+void test_index_tests__reload_while_ignoring_case(void)
+{
+ git_index *index;
+ unsigned int caps;
+
+ cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
+ assert_index_is_sorted(index);
+
+ caps = git_index_caps(index);
+ cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEXCAP_IGNORE_CASE));
+ cl_git_pass(git_index_read(index, true));
+ assert_index_is_sorted(index);
+
+ cl_git_pass(git_index_set_caps(index, caps | GIT_INDEXCAP_IGNORE_CASE));
+ cl_git_pass(git_index_read(index, true));
+ assert_index_is_sorted(index);
+
+ git_index_free(index);
+}
diff --git a/tests/merge/workdir/dirty.c b/tests/merge/workdir/dirty.c
new file mode 100644
index 000000000..a77f9b205
--- /dev/null
+++ b/tests/merge/workdir/dirty.c
@@ -0,0 +1,322 @@
+#include "clar_libgit2.h"
+#include "git2/merge.h"
+#include "buffer.h"
+#include "merge.h"
+#include "../merge_helpers.h"
+#include "posix.h"
+
+#define TEST_REPO_PATH "merge-resolve"
+#define MERGE_BRANCH_OID "7cb63eed597130ba4abb87b3e544b85021905520"
+
+#define AUTOMERGEABLE_MERGED_FILE \
+ "this file is changed in master\n" \
+ "this file is automergeable\n" \
+ "this file is automergeable\n" \
+ "this file is automergeable\n" \
+ "this file is automergeable\n" \
+ "this file is automergeable\n" \
+ "this file is automergeable\n" \
+ "this file is automergeable\n" \
+ "this file is changed in branch\n"
+
+#define CHANGED_IN_BRANCH_FILE \
+ "changed in branch\n"
+
+static git_repository *repo;
+static git_index *repo_index;
+
+static char *unaffected[][4] = {
+ { "added-in-master.txt", NULL },
+ { "changed-in-master.txt", NULL },
+ { "unchanged.txt", NULL },
+ { "added-in-master.txt", "changed-in-master.txt", NULL },
+ { "added-in-master.txt", "unchanged.txt", NULL },
+ { "changed-in-master.txt", "unchanged.txt", NULL },
+ { "added-in-master.txt", "changed-in-master.txt", "unchanged.txt", NULL },
+ { "new_file.txt", NULL },
+ { "new_file.txt", "unchanged.txt", NULL },
+ { NULL },
+};
+
+static char *affected[][5] = {
+ { "automergeable.txt", NULL },
+ { "changed-in-branch.txt", NULL },
+ { "conflicting.txt", NULL },
+ { "removed-in-branch.txt", NULL },
+ { "automergeable.txt", "changed-in-branch.txt", NULL },
+ { "automergeable.txt", "conflicting.txt", NULL },
+ { "automergeable.txt", "removed-in-branch.txt", NULL },
+ { "changed-in-branch.txt", "conflicting.txt", NULL },
+ { "changed-in-branch.txt", "removed-in-branch.txt", NULL },
+ { "conflicting.txt", "removed-in-branch.txt", NULL },
+ { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", NULL },
+ { "automergeable.txt", "changed-in-branch.txt", "removed-in-branch.txt", NULL },
+ { "automergeable.txt", "conflicting.txt", "removed-in-branch.txt", NULL },
+ { "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL },
+ { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL },
+ { NULL },
+};
+
+static char *result_contents[4][6] = {
+ { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, NULL, NULL },
+ { "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL },
+ { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL },
+ { NULL }
+};
+
+void test_merge_workdir_dirty__initialize(void)
+{
+ repo = cl_git_sandbox_init(TEST_REPO_PATH);
+ git_repository_index(&repo_index, repo);
+}
+
+void test_merge_workdir_dirty__cleanup(void)
+{
+ git_index_free(repo_index);
+ cl_git_sandbox_cleanup();
+}
+
+static void set_core_autocrlf_to(git_repository *repo, bool value)
+{
+ git_config *cfg;
+
+ cl_git_pass(git_repository_config(&cfg, repo));
+ cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", value));
+
+ git_config_free(cfg);
+}
+
+static int merge_branch(git_merge_result **result, int merge_file_favor, int checkout_strategy)
+{
+ git_oid their_oids[1];
+ git_merge_head *their_heads[1];
+ git_merge_opts opts = GIT_MERGE_OPTS_INIT;
+ int error;
+
+ cl_git_pass(git_oid_fromstr(&their_oids[0], MERGE_BRANCH_OID));
+ cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0]));
+
+ opts.merge_tree_opts.file_favor = merge_file_favor;
+ opts.checkout_opts.checkout_strategy = checkout_strategy;
+ error = git_merge(result, repo, (const git_merge_head **)their_heads, 1, &opts);
+
+ git_merge_head_free(their_heads[0]);
+
+ return error;
+}
+
+static void write_files(char *files[])
+{
+ char *filename;
+ git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT;
+ size_t i;
+
+ for (i = 0, filename = files[i]; filename; filename = files[++i]) {
+ git_buf_clear(&path);
+ git_buf_clear(&content);
+
+ git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename);
+ git_buf_printf(&content, "This is a dirty file in the working directory!\n\n"
+ "It will not be staged! Its filename is %s.\n", filename);
+
+ cl_git_mkfile(path.ptr, content.ptr);
+ }
+
+ git_buf_free(&path);
+ git_buf_free(&content);
+}
+
+static void hack_index(char *files[])
+{
+ char *filename;
+ struct stat statbuf;
+ git_buf path = GIT_BUF_INIT;
+ git_index_entry *entry;
+ size_t i;
+
+ /* Update the index to suggest that checkout placed these files on
+ * disk, keeping the object id but updating the cache, which will
+ * emulate a Git implementation's different filter.
+ */
+ for (i = 0, filename = files[i]; filename; filename = files[++i]) {
+ git_buf_clear(&path);
+
+ cl_assert(entry = (git_index_entry *)
+ git_index_get_bypath(repo_index, filename, 0));
+
+ cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename));
+ cl_git_pass(p_stat(path.ptr, &statbuf));
+
+ entry->ctime.seconds = (git_time_t)statbuf.st_ctime;
+ entry->ctime.nanoseconds = 0;
+ entry->mtime.seconds = (git_time_t)statbuf.st_mtime;
+ entry->mtime.nanoseconds = 0;
+ entry->dev = statbuf.st_dev;
+ entry->ino = statbuf.st_ino;
+ entry->uid = statbuf.st_uid;
+ entry->gid = statbuf.st_gid;
+ entry->file_size = statbuf.st_size;
+ }
+
+ git_buf_free(&path);
+}
+
+static void stage_random_files(char *files[])
+{
+ char *filename;
+ size_t i;
+
+ write_files(files);
+
+ for (i = 0, filename = files[i]; filename; filename = files[++i])
+ cl_git_pass(git_index_add_bypath(repo_index, filename));
+}
+
+static void stage_content(char *content[])
+{
+ git_reference *head;
+ git_object *head_object;
+ git_merge_result *result = NULL;
+ git_buf path = GIT_BUF_INIT;
+ char *filename, *text;
+ size_t i;
+
+ cl_git_pass(git_repository_head(&head, repo));
+ cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT));
+ cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL, NULL));
+
+ for (i = 0, filename = content[i], text = content[++i];
+ filename && text;
+ filename = content[++i], text = content[++i]) {
+
+ git_buf_clear(&path);
+
+ cl_git_pass(git_buf_printf(&path, "%s/%s", TEST_REPO_PATH, filename));
+
+ cl_git_mkfile(path.ptr, text);
+ cl_git_pass(git_index_add_bypath(repo_index, filename));
+ }
+
+ git_merge_result_free(result);
+ git_object_free(head_object);
+ git_reference_free(head);
+ git_buf_free(&path);
+}
+
+static int merge_dirty_files(char *dirty_files[])
+{
+ git_reference *head;
+ git_object *head_object;
+ git_merge_result *result = NULL;
+ int error;
+
+ cl_git_pass(git_repository_head(&head, repo));
+ cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT));
+ cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL, NULL));
+
+ write_files(dirty_files);
+
+ error = merge_branch(&result, 0, 0);
+
+ git_merge_result_free(result);
+ git_object_free(head_object);
+ git_reference_free(head);
+
+ return error;
+}
+
+static int merge_differently_filtered_files(char *files[])
+{
+ git_reference *head;
+ git_object *head_object;
+ git_merge_result *result = NULL;
+ int error;
+
+ cl_git_pass(git_repository_head(&head, repo));
+ cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT));
+ cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL, NULL));
+
+ write_files(files);
+ hack_index(files);
+
+ cl_git_pass(git_index_write(repo_index));
+
+ error = merge_branch(&result, 0, 0);
+
+ git_merge_result_free(result);
+ git_object_free(head_object);
+ git_reference_free(head);
+
+ return error;
+}
+
+static int merge_staged_files(char *staged_files[])
+{
+ git_merge_result *result = NULL;
+ int error;
+
+ stage_random_files(staged_files);
+
+ error = merge_branch(&result, 0, 0);
+
+ git_merge_result_free(result);
+
+ return error;
+}
+
+void test_merge_workdir_dirty__unaffected_dirty_files_allowed(void)
+{
+ char **files;
+ size_t i;
+
+ for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i])
+ cl_git_pass(merge_dirty_files(files));
+}
+
+void test_merge_workdir_dirty__affected_dirty_files_disallowed(void)
+{
+ char **files;
+ size_t i;
+
+ for (i = 0, files = affected[i]; files[0]; files = affected[++i])
+ cl_git_fail(merge_dirty_files(files));
+}
+
+void test_merge_workdir_dirty__staged_files_in_index_disallowed(void)
+{
+ char **files;
+ size_t i;
+
+ for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i])
+ cl_git_fail(merge_staged_files(files));
+
+ for (i = 0, files = affected[i]; files[0]; files = affected[++i])
+ cl_git_fail(merge_staged_files(files));
+}
+
+void test_merge_workdir_dirty__identical_staged_files_allowed(void)
+{
+ git_merge_result *result;
+ char **content;
+ size_t i;
+
+ set_core_autocrlf_to(repo, false);
+
+ for (i = 0, content = result_contents[i]; content[0]; content = result_contents[++i]) {
+ stage_content(content);
+
+ git_index_write(repo_index);
+ cl_git_pass(merge_branch(&result, 0, 0));
+
+ git_merge_result_free(result);
+ }
+}
+
+void test_merge_workdir_dirty__honors_cache(void)
+{
+ char **files;
+ size_t i;
+
+ for (i = 0, files = affected[i]; files[0]; files = affected[++i])
+ cl_git_pass(merge_differently_filtered_files(files));
+}
diff --git a/tests/merge/workdir/renames.c b/tests/merge/workdir/renames.c
index d38397983..27747720e 100644
--- a/tests/merge/workdir/renames.c
+++ b/tests/merge/workdir/renames.c
@@ -17,7 +17,14 @@ static git_repository *repo;
// Fixture setup and teardown
void test_merge_workdir_renames__initialize(void)
{
+ git_config *cfg;
+
repo = cl_git_sandbox_init(TEST_REPO_PATH);
+
+ /* Ensure that the user's merge.conflictstyle doesn't interfere */
+ cl_git_pass(git_repository_config(&cfg, repo));
+ cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge"));
+ git_config_free(cfg);
}
void test_merge_workdir_renames__cleanup(void)
diff --git a/tests/merge/workdir/simple.c b/tests/merge/workdir/simple.c
index 4ff761cf8..a9a63651c 100644
--- a/tests/merge/workdir/simple.c
+++ b/tests/merge/workdir/simple.c
@@ -116,8 +116,15 @@ static git_index *repo_index;
// Fixture setup and teardown
void test_merge_workdir_simple__initialize(void)
{
+ git_config *cfg;
+
repo = cl_git_sandbox_init(TEST_REPO_PATH);
git_repository_index(&repo_index, repo);
+
+ /* Ensure that the user's merge.conflictstyle doesn't interfere */
+ cl_git_pass(git_repository_config(&cfg, repo));
+ cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge"));
+ git_config_free(cfg);
}
void test_merge_workdir_simple__cleanup(void)
@@ -585,7 +592,7 @@ void test_merge_workdir_simple__directory_file(void)
cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_DIR OURS_DIRECTORY_FILE, 1, NULL, NULL));
cl_git_pass(git_reference_name_to_id(&head_commit_id, repo, GIT_HEAD_FILE));
cl_git_pass(git_commit_lookup(&head_commit, repo, &head_commit_id));
- cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_oid_fromstr(&their_oids[0], THEIRS_DIRECTORY_FILE));
cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0]));
@@ -684,7 +691,7 @@ void test_merge_workdir_simple__binary(void)
cl_git_pass(git_oid_fromstr(&their_oid, "ad01aebfdf2ac13145efafe3f9fcf798882f1730"));
cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid));
- cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_merge_head_from_id(&their_head, repo, &their_oid));
diff --git a/tests/merge/workdir/submodules.c b/tests/merge/workdir/submodules.c
index f01faac43..42451bde7 100644
--- a/tests/merge/workdir/submodules.c
+++ b/tests/merge/workdir/submodules.c
@@ -46,7 +46,7 @@ void test_merge_workdir_submodules__automerge(void)
cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH));
cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref)));
- cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_head, repo, their_ref));
@@ -82,7 +82,7 @@ void test_merge_workdir_submodules__take_changed(void)
cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH));
cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref)));
- cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER2_BRANCH));
cl_git_pass(git_merge_head_from_ref(&their_head, repo, their_ref));
diff --git a/tests/network/fetchlocal.c b/tests/network/fetchlocal.c
index 28c7115bf..4c39394bb 100644
--- a/tests/network/fetchlocal.c
+++ b/tests/network/fetchlocal.c
@@ -37,7 +37,7 @@ void test_network_fetchlocal__complete(void)
git_remote_set_callbacks(origin, &callbacks);
cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(origin));
- cl_git_pass(git_remote_update_tips(origin));
+ cl_git_pass(git_remote_update_tips(origin, NULL, NULL));
cl_git_pass(git_reference_list(&refnames, repo));
cl_assert_equal_i(19, (int)refnames.count);
@@ -75,7 +75,7 @@ void test_network_fetchlocal__partial(void)
git_remote_set_callbacks(origin, &callbacks);
cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(origin));
- cl_git_pass(git_remote_update_tips(origin));
+ cl_git_pass(git_remote_update_tips(origin, NULL, NULL));
git_strarray_free(&refnames);
diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c
index 9b9f716b9..589e6ac9b 100644
--- a/tests/network/remote/local.c
+++ b/tests/network/remote/local.c
@@ -115,7 +115,7 @@ void test_network_remote_local__shorthand_fetch_refspec0(void)
cl_git_pass(git_remote_add_fetch(remote, refspec2));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master"));
git_reference_free(ref);
@@ -137,7 +137,7 @@ void test_network_remote_local__shorthand_fetch_refspec1(void)
cl_git_pass(git_remote_add_fetch(remote, refspec2));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/master"));
@@ -152,7 +152,7 @@ void test_network_remote_local__tagopt(void)
git_remote_set_autotag(remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/master"));
@@ -171,7 +171,7 @@ void test_network_remote_local__push_to_bare_remote(void)
connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, "master:master"));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote);
/* Set up an empty bare repo to push into */
@@ -208,7 +208,7 @@ void test_network_remote_local__push_to_bare_remote_with_file_url(void)
connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, "master:master"));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote);
/* Set up an empty bare repo to push into */
@@ -248,7 +248,7 @@ void test_network_remote_local__push_to_non_bare_remote(void)
connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, "master:master"));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote);
/* Set up an empty non-bare repo to push into */
@@ -273,3 +273,86 @@ void test_network_remote_local__push_to_non_bare_remote(void)
git_remote_free(localremote);
cl_fixture_cleanup("localbare.git");
}
+
+void test_network_remote_local__fetch(void)
+{
+ const char *refspec = "master:remotes/sloppy/master";
+
+ git_reflog *log;
+ const git_reflog_entry *entry;
+ git_signature *sig;
+ git_reference *ref;
+
+ cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com"));
+
+ connect_to_local_repository(cl_fixture("testrepo.git"));
+ cl_git_pass(git_remote_add_fetch(remote, refspec));
+
+ cl_git_pass(git_remote_fetch(remote, sig, "UPDAAAAAATE!!"));
+
+ cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master"));
+ git_reference_free(ref);
+
+ cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
+ cl_assert_equal_i(1, git_reflog_entrycount(log));
+ entry = git_reflog_entry_byindex(log, 0);
+ cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+ cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry));
+
+ git_reflog_free(log);
+ git_signature_free(sig);
+}
+
+void test_network_remote_local__reflog(void)
+{
+ const char *refspec = "master:remotes/sloppy/master";
+
+ git_reflog *log;
+ const git_reflog_entry *entry;
+ git_signature *sig;
+
+ cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com"));
+
+ connect_to_local_repository(cl_fixture("testrepo.git"));
+ cl_git_pass(git_remote_add_fetch(remote, refspec));
+
+ cl_git_pass(git_remote_download(remote));
+ cl_git_pass(git_remote_update_tips(remote, sig, "UPDAAAAAATE!!"));
+
+ cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
+ cl_assert_equal_i(1, git_reflog_entrycount(log));
+ entry = git_reflog_entry_byindex(log, 0);
+ cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+ cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry));
+
+ git_reflog_free(log);
+ git_signature_free(sig);
+}
+
+void test_network_remote_local__fetch_default_reflog_message(void)
+{
+ const char *refspec = "master:remotes/sloppy/master";
+
+ git_reflog *log;
+ const git_reflog_entry *entry;
+ git_signature *sig;
+ char expected_reflog_msg[1024];
+
+ cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com"));
+
+ connect_to_local_repository(cl_fixture("testrepo.git"));
+ cl_git_pass(git_remote_add_fetch(remote, refspec));
+
+ cl_git_pass(git_remote_fetch(remote, sig, NULL));
+
+ cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
+ cl_assert_equal_i(1, git_reflog_entrycount(log));
+ entry = git_reflog_entry_byindex(log, 0);
+ cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+
+ sprintf(expected_reflog_msg, "fetch %s", git_remote_url(remote));
+ cl_assert_equal_s(expected_reflog_msg, git_reflog_entry_message(entry));
+
+ git_reflog_free(log);
+ git_signature_free(sig);
+}
diff --git a/tests/object/commit/commitstagedfile.c b/tests/object/commit/commitstagedfile.c
index fbeeccbbd..3e7b3c02c 100644
--- a/tests/object/commit/commitstagedfile.c
+++ b/tests/object/commit/commitstagedfile.c
@@ -132,3 +132,79 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
git_tree_free(tree);
git_index_free(index);
}
+
+static void assert_commit_tree_has_n_entries(git_commit *c, int count)
+{
+ git_tree *tree;
+ cl_git_pass(git_commit_tree(&tree, c));
+ cl_assert_equal_i(count, git_tree_entrycount(tree));
+ git_tree_free(tree);
+}
+
+static void assert_commit_is_head_(git_commit *c, const char *file, int line)
+{
+ git_commit *head;
+ cl_git_pass(git_revparse_single((git_object **)&head, repo, "HEAD"));
+ clar__assert(git_oid_equal(git_commit_id(c), git_commit_id(head)), file, line, "Commit is not the HEAD", NULL, 1);
+ git_commit_free(head);
+}
+#define assert_commit_is_head(C) assert_commit_is_head_((C),__FILE__,__LINE__)
+
+void test_object_commit_commitstagedfile__amend_commit(void)
+{
+ git_index *index;
+ git_oid old_oid, new_oid, tree_oid;
+ git_commit *old_commit, *new_commit;
+ git_tree *tree;
+
+ /* make a commit */
+
+ cl_git_mkfile("treebuilder/myfile", "This is a file\n");
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_index_add_bypath(index, "myfile"));
+ cl_repo_commit_from_index(&old_oid, repo, NULL, 0, "first commit");
+
+ cl_git_pass(git_commit_lookup(&old_commit, repo, &old_oid));
+
+ cl_assert_equal_i(0, git_commit_parentcount(old_commit));
+ assert_commit_tree_has_n_entries(old_commit, 1);
+ assert_commit_is_head(old_commit);
+
+ /* let's amend the message of the HEAD commit */
+
+ cl_git_pass(git_commit_amend(
+ &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL));
+
+ cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid));
+
+ cl_assert_equal_i(0, git_commit_parentcount(new_commit));
+ assert_commit_tree_has_n_entries(new_commit, 1);
+ assert_commit_is_head(new_commit);
+
+ git_commit_free(old_commit);
+ old_commit = new_commit;
+
+ /* let's amend the tree of that last commit */
+
+ cl_git_mkfile("treebuilder/anotherfile", "This is another file\n");
+ cl_git_pass(git_index_add_bypath(index, "anotherfile"));
+ cl_git_pass(git_index_write_tree(&tree_oid, index));
+ cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
+ cl_assert_equal_i(2, git_tree_entrycount(tree));
+
+ cl_git_pass(git_commit_amend(
+ &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", tree));
+ git_tree_free(tree);
+
+ cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid));
+
+ cl_assert_equal_i(0, git_commit_parentcount(new_commit));
+ assert_commit_tree_has_n_entries(new_commit, 2);
+ assert_commit_is_head(new_commit);
+
+ /* cleanup */
+
+ git_commit_free(old_commit);
+ git_commit_free(new_commit);
+ git_index_free(index);
+}
diff --git a/tests/object/tree/write.c b/tests/object/tree/write.c
index 3bea0ed4d..45356e807 100644
--- a/tests/object/tree/write.c
+++ b/tests/object/tree/write.c
@@ -9,7 +9,7 @@ static const char *third_tree = "eb86d8b81d6adbd5290a935d6c9976882de98488";
static git_repository *g_repo;
-// Fixture setup and teardown
+/* Fixture setup and teardown */
void test_object_tree_write__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
@@ -22,7 +22,7 @@ void test_object_tree_write__cleanup(void)
void test_object_tree_write__from_memory(void)
{
- // write a tree from a memory
+ /* write a tree from a memory */
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, rid, id2;
@@ -31,7 +31,9 @@ void test_object_tree_write__from_memory(void)
git_oid_fromstr(&id2, second_tree);
git_oid_fromstr(&bid, blob_oid);
- //create a second tree from first tree using `git_treebuilder_insert` on REPOSITORY_FOLDER.
+ /* create a second tree from first tree using `git_treebuilder_insert`
+ * on REPOSITORY_FOLDER.
+ */
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_create(&builder, tree));
@@ -61,7 +63,7 @@ void test_object_tree_write__from_memory(void)
void test_object_tree_write__subtree(void)
{
- // write a hierarchical tree from a memory
+ /* write a hierarchical tree from a memory */
git_treebuilder *builder;
git_tree *tree;
git_oid id, bid, subtree_id, id2, id3;
@@ -72,25 +74,25 @@ void test_object_tree_write__subtree(void)
git_oid_fromstr(&id3, third_tree);
git_oid_fromstr(&bid, blob_oid);
- //create subtree
+ /* create subtree */
cl_git_pass(git_treebuilder_create(&builder, NULL));
cl_git_pass(git_treebuilder_insert(
- NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); //-V536
+ NULL, builder, "new.txt", &bid, GIT_FILEMODE_BLOB)); /* -V536 */
cl_git_pass(git_treebuilder_write(&subtree_id, g_repo, builder));
git_treebuilder_free(builder);
- // create parent tree
+ /* create parent tree */
cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
cl_git_pass(git_treebuilder_create(&builder, tree));
cl_git_pass(git_treebuilder_insert(
- NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); //-V536
+ NULL, builder, "new", &subtree_id, GIT_FILEMODE_TREE)); /* -V536 */
cl_git_pass(git_treebuilder_write(&id_hiearar, g_repo, builder));
git_treebuilder_free(builder);
git_tree_free(tree);
cl_assert(git_oid_cmp(&id_hiearar, &id3) == 0);
- // check data is correct
+ /* check data is correct */
cl_git_pass(git_tree_lookup(&tree, g_repo, &id_hiearar));
cl_assert(2 == git_tree_entrycount(tree));
git_tree_free(tree);
@@ -314,3 +316,83 @@ void test_object_tree_write__filtering(void)
git_tree_free(tree);
}
+
+void test_object_tree_write__cruel_paths(void)
+{
+ static const char *the_paths[] = {
+ "C:\\",
+ " : * ? \" \n < > |",
+ "a\\b",
+ "\\\\b\a",
+ ":\\",
+ "COM1",
+ "foo.aux",
+ REP1024("1234"), /* 4096 char string */
+ REP1024("12345678"), /* 8192 char string */
+ "\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD", /* Ūnĭcōde̽ */
+ NULL
+ };
+ git_treebuilder *builder;
+ git_tree *tree;
+ git_oid id, bid, subid;
+ const char **scan;
+ int count = 0, i, j;
+ git_tree_entry *te;
+
+ git_oid_fromstr(&bid, blob_oid);
+
+ /* create tree */
+ cl_git_pass(git_treebuilder_create(&builder, NULL));
+ for (scan = the_paths; *scan; ++scan) {
+ cl_git_pass(git_treebuilder_insert(
+ NULL, builder, *scan, &bid, GIT_FILEMODE_BLOB));
+ count++;
+ }
+ cl_git_pass(git_treebuilder_write(&id, g_repo, builder));
+ git_treebuilder_free(builder);
+
+ /* check data is correct */
+ cl_git_pass(git_tree_lookup(&tree, g_repo, &id));
+
+ cl_assert_equal_i(count, git_tree_entrycount(tree));
+
+ for (scan = the_paths; *scan; ++scan) {
+ const git_tree_entry *cte = git_tree_entry_byname(tree, *scan);
+ cl_assert(cte != NULL);
+ cl_assert_equal_s(*scan, git_tree_entry_name(cte));
+ }
+ for (scan = the_paths; *scan; ++scan) {
+ cl_git_pass(git_tree_entry_bypath(&te, tree, *scan));
+ cl_assert_equal_s(*scan, git_tree_entry_name(te));
+ git_tree_entry_free(te);
+ }
+
+ git_tree_free(tree);
+
+ /* let's try longer paths */
+ cl_git_pass(git_treebuilder_create(&builder, NULL));
+ for (scan = the_paths; *scan; ++scan) {
+ cl_git_pass(git_treebuilder_insert(
+ NULL, builder, *scan, &id, GIT_FILEMODE_TREE));
+ }
+ cl_git_pass(git_treebuilder_write(&subid, g_repo, builder));
+ git_treebuilder_free(builder);
+
+ /* check data is correct */
+ cl_git_pass(git_tree_lookup(&tree, g_repo, &subid));
+
+ cl_assert_equal_i(count, git_tree_entrycount(tree));
+
+ for (i = 0; i < count; ++i) {
+ for (j = 0; j < count; ++j) {
+ git_buf b = GIT_BUF_INIT;
+ cl_git_pass(git_buf_joinpath(&b, the_paths[i], the_paths[j]));
+ cl_git_pass(git_tree_entry_bypath(&te, tree, b.ptr));
+ cl_assert_equal_s(the_paths[j], git_tree_entry_name(te));
+ git_tree_entry_free(te);
+ git_buf_free(&b);
+ }
+ }
+
+ git_tree_free(tree);
+}
diff --git a/tests/online/clone.c b/tests/online/clone.c
index 1222d174d..fa2408a75 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -192,30 +192,28 @@ static int cred_failure_cb(
{
GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url);
GIT_UNUSED(allowed_types); GIT_UNUSED(data);
- return -1;
+ return -172;
}
-void test_online_clone__cred_callback_failure_is_euser(void)
+void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
{
const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
- const char *remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT");
- int error;
if (!remote_url) {
printf("GITTEST_REMOTE_URL unset; skipping clone test\n");
return;
}
- if (!remote_user && !remote_default) {
- printf("GITTEST_REMOTE_USER and GITTEST_REMOTE_DEFAULT unset; skipping clone test\n");
+ if (!remote_user) {
+ printf("GITTEST_REMOTE_USER unset; skipping clone test\n");
return;
}
g_options.remote_callbacks.credentials = cred_failure_cb;
- cl_git_fail(error = git_clone(&g_repo, remote_url, "./foo", &g_options));
- cl_assert_equal_i(error, GIT_EUSER);
+ /* TODO: this should expect -172. */
+ cl_git_fail_with(git_clone(&g_repo, remote_url, "./foo", &g_options), -1);
}
void test_online_clone__credentials(void)
diff --git a/tests/online/fetch.c b/tests/online/fetch.c
index 8f71cf3f5..cb84e846c 100644
--- a/tests/online/fetch.c
+++ b/tests/online/fetch.c
@@ -48,7 +48,7 @@ static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
git_remote_set_autotag(remote, flag);
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote);
cl_assert_equal_i(counter, n);
cl_assert(bytes_received > 0);
@@ -117,7 +117,7 @@ void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date
cl_assert_equal_i(false, invoked);
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote);
git_remote_free(remote);
diff --git a/tests/online/fetchhead.c b/tests/online/fetchhead.c
index 57b183f88..0b3f20db1 100644
--- a/tests/online/fetchhead.c
+++ b/tests/online/fetchhead.c
@@ -51,7 +51,7 @@ static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fet
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(remote));
- cl_git_pass(git_remote_update_tips(remote));
+ cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote);
git_remote_free(remote);
diff --git a/tests/online/push.c b/tests/online/push.c
index 8efe21e0e..55b97b282 100644
--- a/tests/online/push.c
+++ b/tests/online/push.c
@@ -351,7 +351,7 @@ void test_online_push__initialize(void)
/* Now that we've deleted everything, fetch from the remote */
cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(_remote));
- cl_git_pass(git_remote_update_tips(_remote));
+ cl_git_pass(git_remote_update_tips(_remote, NULL, NULL));
git_remote_disconnect(_remote);
} else
printf("GITTEST_REMOTE_URL unset; skipping push test\n");
@@ -414,11 +414,13 @@ static void do_push(
git_push_options opts = GIT_PUSH_OPTIONS_INIT;
size_t i;
int pack_progress_calls = 0, transfer_progress_calls = 0;
+ git_signature *pusher;
if (_remote) {
/* Auto-detect the number of threads to use */
opts.pb_parallelism = 0;
+ cl_git_pass(git_signature_now(&pusher, "Foo Bar", "foo@example.com"));
cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH));
cl_git_pass(git_push_new(&push, _remote));
@@ -455,13 +457,15 @@ static void do_push(
verify_refs(_remote, expected_refs, expected_refs_len);
- cl_git_pass(git_push_update_tips(push));
+ cl_git_pass(git_push_update_tips(push, pusher, "test push"));
verify_tracking_branches(_remote, expected_refs, expected_refs_len);
git_push_free(push);
git_remote_disconnect(_remote);
+ git_signature_free(pusher);
}
+
}
/* Call push_finish() without ever calling git_push_add_refspec() */
@@ -528,6 +532,9 @@ void test_online_push__b5_cancel(void)
void test_online_push__multi(void)
{
+ git_reflog *log;
+ const git_reflog_entry *entry;
+
const char *specs[] = {
"refs/heads/b1:refs/heads/b1",
"refs/heads/b2:refs/heads/b2",
@@ -552,6 +559,15 @@ void test_online_push__multi(void)
do_push(specs, ARRAY_SIZE(specs),
exp_stats, ARRAY_SIZE(exp_stats),
exp_refs, ARRAY_SIZE(exp_refs), 0, 1);
+
+ cl_git_pass(git_reflog_read(&log, _repo, "refs/remotes/test/b1"));
+ entry = git_reflog_entry_byindex(log, 0);
+ if (entry) {
+ cl_assert_equal_s("test push", git_reflog_entry_message(entry));
+ cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
+ }
+
+ git_reflog_free(log);
}
void test_online_push__implicit_tgt(void)
diff --git a/tests/refs/branches/create.c b/tests/refs/branches/create.c
index d4b0763da..b91eed6e8 100644
--- a/tests/refs/branches/create.c
+++ b/tests/refs/branches/create.c
@@ -29,15 +29,16 @@ void test_refs_branches_create__cleanup(void)
static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha)
{
- git_oid oid;
+ git_object *obj;
- cl_git_pass(git_oid_fromstr(&oid, sha));
- cl_git_pass(git_commit_lookup(out, repo, &oid));
+ cl_git_pass(git_revparse_single(&obj, repo, sha));
+ cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj)));
+ git_object_free(obj);
}
static void retrieve_known_commit(git_commit **commit, git_repository *repo)
{
- retrieve_target_from_oid(commit, repo, "e90810b8df3e80c413d903f631643c716887138d");
+ retrieve_target_from_oid(commit, repo, "e90810b8df3");
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
diff --git a/tests/refs/branches/delete.c b/tests/refs/branches/delete.c
index 7d1d400c8..ed5f1627b 100644
--- a/tests/refs/branches/delete.c
+++ b/tests/refs/branches/delete.c
@@ -78,7 +78,7 @@ void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(
git_reference_free(head);
/* Detach HEAD and make it target the commit that "master" points to */
- git_repository_detach_head(repo);
+ git_repository_detach_head(repo, NULL, NULL);
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
diff --git a/tests/refs/delete.c b/tests/refs/delete.c
index 5e4afb138..9d1c3fd79 100644
--- a/tests/refs/delete.c
+++ b/tests/refs/delete.c
@@ -91,3 +91,17 @@ void test_refs_delete__packed_only(void)
git_reference_free(ref);
git_refdb_free(refdb);
}
+
+void test_refs_delete__remove(void)
+{
+ git_reference *ref;
+
+ /* Check that passing no old values lets us delete */
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, packed_test_head_name));
+ git_reference_free(ref);
+
+ cl_git_pass(git_reference_remove(g_repo, packed_test_head_name));
+
+ cl_git_fail(git_reference_lookup(&ref, g_repo, packed_test_head_name));
+}
diff --git a/tests/refs/races.c b/tests/refs/races.c
new file mode 100644
index 000000000..643290a8e
--- /dev/null
+++ b/tests/refs/races.c
@@ -0,0 +1,152 @@
+#include "clar_libgit2.h"
+
+#include "repository.h"
+#include "git2/reflog.h"
+#include "reflog.h"
+#include "ref_helpers.h"
+
+static const char *commit_id = "099fabac3a9ea935598528c27f866e34089c2eff";
+static const char *refname = "refs/heads/master";
+static const char *other_refname = "refs/heads/foo";
+static const char *other_commit_id = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
+
+static git_repository *g_repo;
+
+void test_refs_races__initialize(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo");
+}
+
+void test_refs_races__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_refs_races__create_matching(void)
+{
+ git_reference *ref, *ref2, *ref3;
+ git_oid id, other_id;
+
+ git_oid_fromstr(&id, commit_id);
+ git_oid_fromstr(&other_id, other_commit_id);
+
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_create_matching(&ref, g_repo, refname, &other_id, 1, &other_id, NULL, NULL));
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
+ cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL, NULL));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL, NULL));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+ git_reference_free(ref3);
+}
+
+void test_refs_races__symbolic_create_matching(void)
+{
+ git_reference *ref, *ref2, *ref3;
+ git_oid id, other_id;
+
+ git_oid_fromstr(&id, commit_id);
+ git_oid_fromstr(&other_id, other_commit_id);
+
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_create_matching(&ref, g_repo, "HEAD", other_refname, 1, other_refname, NULL, NULL));
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
+ cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, NULL, refname));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL, NULL));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+ git_reference_free(ref3);
+}
+
+void test_refs_races__delete(void)
+{
+ git_reference *ref, *ref2;
+ git_oid id, other_id;
+
+ git_oid_fromstr(&id, commit_id);
+ git_oid_fromstr(&other_id, other_commit_id);
+
+ /* We can delete a value that matches */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
+ cl_git_pass(git_reference_delete(ref));
+ git_reference_free(ref);
+
+ /* We cannot delete a symbolic value that doesn't match */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
+ cl_git_pass(git_reference_symbolic_create_matching(&ref2, g_repo, "HEAD", other_refname, 1, NULL, NULL, refname));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+
+ cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL, NULL));
+ git_reference_free(ref);
+
+ /* We cannot delete an oid value that doesn't match */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
+ cl_git_pass(git_reference_create_matching(&ref2, g_repo, refname, &other_id, 1, &id, NULL, NULL));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+}
+
+void test_refs_races__switch_oid_to_symbolic(void)
+{
+ git_reference *ref, *ref2, *ref3;
+ git_oid id, other_id;
+
+ git_oid_fromstr(&id, commit_id);
+ git_oid_fromstr(&other_id, other_commit_id);
+
+ /* Removing a direct ref when it's currently symbolic should fail */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
+ cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL, NULL));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+
+ cl_git_pass(git_reference_create(&ref, g_repo, refname, &id, 1, NULL, NULL));
+ git_reference_free(ref);
+
+ /* Updating a direct ref when it's currently symbolic should fail */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, refname));
+ cl_git_pass(git_reference_symbolic_create(&ref2, g_repo, refname, other_refname, 1, NULL, NULL));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_set_target(&ref3, ref, &other_id, NULL, NULL));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+ git_reference_free(ref3);
+}
+
+void test_refs_races__switch_symbolic_to_oid(void)
+{
+ git_reference *ref, *ref2, *ref3;
+ git_oid id, other_id;
+
+ git_oid_fromstr(&id, commit_id);
+ git_oid_fromstr(&other_id, other_commit_id);
+
+ /* Removing a symbolic ref when it's currently direct should fail */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
+ cl_git_pass(git_reference_create(&ref2, g_repo, "HEAD", &id, 1, NULL, NULL));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_delete(ref));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+
+ cl_git_pass(git_reference_symbolic_create(&ref, g_repo, "HEAD", refname, 1, NULL, NULL));
+ git_reference_free(ref);
+
+ /* Updating a symbolic ref when it's currently direct should fail */
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD"));
+ cl_git_pass(git_reference_create(&ref2, g_repo, "HEAD", &id, 1, NULL, NULL));
+ cl_git_fail_with(GIT_EMODIFIED, git_reference_symbolic_set_target(&ref3, ref, other_refname, NULL, NULL));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+ git_reference_free(ref3);
+}
diff --git a/tests/refs/read.c b/tests/refs/read.c
index 35cf17e9e..52c307eb0 100644
--- a/tests/refs/read.c
+++ b/tests/refs/read.c
@@ -271,6 +271,21 @@ void test_refs_read__can_determine_if_a_reference_is_a_tag(void)
assert_is_tag("refs/remotes/test/master", false);
}
+static void assert_is_note(const char *name, bool expected_noteness)
+{
+ git_reference *reference;
+ cl_git_pass(git_reference_lookup(&reference, g_repo, name));
+ cl_assert_equal_i(expected_noteness, git_reference_is_note(reference));
+ git_reference_free(reference);
+}
+
+void test_refs_read__can_determine_if_a_reference_is_a_note(void)
+{
+ assert_is_note("refs/notes/fanout", true);
+ assert_is_note("refs/heads/packed", false);
+ assert_is_note("refs/remotes/test/master", false);
+}
+
void test_refs_read__invalid_name_returns_EINVALIDSPEC(void)
{
git_reference *reference;
diff --git a/tests/repo/config.c b/tests/repo/config.c
index e77acc8c5..2e7be37aa 100644
--- a/tests/repo/config.c
+++ b/tests/repo/config.c
@@ -1,4 +1,5 @@
#include "clar_libgit2.h"
+#include "sysdir.h"
#include "fileops.h"
#include <ctype.h>
@@ -47,7 +48,7 @@ void test_repo_config__open_missing_global(void)
git_config_free(config);
git_repository_free(repo);
- git_futils_dirs_global_shutdown();
+ git_sysdir_global_shutdown();
}
void test_repo_config__open_missing_global_with_separators(void)
@@ -76,7 +77,7 @@ void test_repo_config__open_missing_global_with_separators(void)
git_config_free(config);
git_repository_free(repo);
- git_futils_dirs_global_shutdown();
+ git_sysdir_global_shutdown();
}
#include "repository.h"
@@ -105,7 +106,7 @@ void test_repo_config__read_no_configs(void)
cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
git_repository_free(repo);
- git_futils_dirs_global_shutdown();
+ git_sysdir_global_shutdown();
/* with just system */
@@ -204,5 +205,5 @@ void test_repo_config__read_no_configs(void)
cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
cl_assert(!git_path_exists("alternate/3/.gitconfig"));
- git_futils_dirs_global_shutdown();
+ git_sysdir_global_shutdown();
}
diff --git a/tests/repo/head.c b/tests/repo/head.c
index 71cfc3c33..c5965fac6 100644
--- a/tests/repo/head.c
+++ b/tests/repo/head.c
@@ -15,21 +15,42 @@ void test_repo_head__cleanup(void)
cl_git_sandbox_cleanup();
}
+static void check_last_reflog_entry(const char *email, const char *message)
+{
+ git_reflog *log;
+ const git_reflog_entry *entry;
+
+ cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
+ cl_assert(git_reflog_entrycount(log) > 0);
+ entry = git_reflog_entry_byindex(log, 0);
+ if (email)
+ cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email);
+ if (message)
+ cl_assert_equal_s(message, git_reflog_entry_message(entry));
+ git_reflog_free(log);
+}
+
void test_repo_head__head_detached(void)
{
git_reference *ref;
+ git_signature *sig;
- cl_git_pass(git_repository_head_detached(repo));
+ cl_git_pass(git_signature_now(&sig, "Foo Bar", "foo@example.com"));
- cl_git_pass(git_repository_detach_head(repo));
+ cl_assert_equal_i(false, git_repository_head_detached(repo));
+ cl_git_pass(git_repository_detach_head(repo, sig, "CABLE DETACHED"));
+ check_last_reflog_entry(sig->email, "CABLE DETACHED");
cl_assert_equal_i(true, git_repository_head_detached(repo));
- /* take the reop back to it's original state */
- cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1, NULL, NULL));
+ /* take the repo back to it's original state */
+ cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master",
+ true, sig, "REATTACH"));
git_reference_free(ref);
+ check_last_reflog_entry(sig->email, "REATTACH");
cl_assert_equal_i(false, git_repository_head_detached(repo));
+ git_signature_free(sig);
}
void test_repo_head__unborn_head(void)
@@ -147,7 +168,7 @@ void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_c
{
cl_assert_equal_i(false, git_repository_head_detached(repo));
- cl_git_pass(git_repository_detach_head(repo));
+ cl_git_pass(git_repository_detach_head(repo, NULL, NULL));
assert_head_is_correctly_detached();
}
@@ -158,7 +179,7 @@ void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void
cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1, NULL, NULL));
- cl_git_fail(git_repository_detach_head(repo));
+ cl_git_fail(git_repository_detach_head(repo, NULL, NULL));
git_reference_free(head);
}
@@ -167,7 +188,7 @@ void test_repo_head__detaching_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void)
{
make_head_unborn(repo, NON_EXISTING_HEAD);
- cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_detach_head(repo));
+ cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_detach_head(repo, NULL, NULL));
}
void test_repo_head__retrieving_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void)
@@ -195,10 +216,41 @@ void test_repo_head__can_tell_if_an_unborn_head_is_detached(void)
cl_assert_equal_i(false, git_repository_head_detached(repo));
}
-void test_repo_head__setting_head_updates_reflog(void)
+static void test_reflog(git_repository *repo, size_t idx,
+ const char *old_spec, const char *new_spec,
+ const char *email, const char *message)
{
git_reflog *log;
- const git_reflog_entry *entry1, *entry2, *entry3;
+ const git_reflog_entry *entry;
+
+ cl_git_pass(git_reflog_read(&log, repo, "HEAD"));
+ entry = git_reflog_entry_byindex(log, idx);
+
+ if (old_spec) {
+ git_object *obj;
+ cl_git_pass(git_revparse_single(&obj, repo, old_spec));
+ cl_assert_equal_i(0, git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)));
+ git_object_free(obj);
+ }
+ if (new_spec) {
+ git_object *obj;
+ cl_git_pass(git_revparse_single(&obj, repo, new_spec));
+ cl_assert_equal_i(0, git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)));
+ git_object_free(obj);
+ }
+
+ if (email) {
+ cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email);
+ }
+ if (message) {
+ cl_assert_equal_s(message, git_reflog_entry_message(entry));
+ }
+
+ git_reflog_free(log);
+}
+
+void test_repo_head__setting_head_updates_reflog(void)
+{
git_object *tag;
git_signature *sig;
@@ -208,19 +260,13 @@ void test_repo_head__setting_head_updates_reflog(void)
cl_git_pass(git_repository_set_head(repo, "refs/heads/unborn", sig, "message2"));
cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag), sig, "message3"));
+ cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, "message4"));
- cl_git_pass(git_reflog_read(&log, repo, "HEAD"));
- entry1 = git_reflog_entry_byindex(log, 2);
- entry2 = git_reflog_entry_byindex(log, 1);
- entry3 = git_reflog_entry_byindex(log, 0);
- cl_assert_equal_s("message1", git_reflog_entry_message(entry1));
- cl_assert_equal_s("message2", git_reflog_entry_message(entry2));
- cl_assert_equal_s("message3", git_reflog_entry_message(entry3));
- cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry1)->email);
- cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry2)->email);
- cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry3)->email);
+ test_reflog(repo, 3, NULL, "refs/heads/haacked", "foo@example.com", "message1");
+ test_reflog(repo, 2, "refs/heads/haacked", NULL, "foo@example.com", "message2");
+ test_reflog(repo, 1, NULL, "tags/test^{commit}", "foo@example.com", "message3");
+ test_reflog(repo, 0, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "message4");
- git_reflog_free(log);
git_object_free(tag);
git_signature_free(sig);
}
diff --git a/tests/repo/headtree.c b/tests/repo/headtree.c
index e899ac399..79d88c0a7 100644
--- a/tests/repo/headtree.c
+++ b/tests/repo/headtree.c
@@ -20,7 +20,7 @@ void test_repo_headtree__cleanup(void)
void test_repo_headtree__can_retrieve_the_root_tree_from_a_detached_head(void)
{
- cl_git_pass(git_repository_detach_head(repo));
+ cl_git_pass(git_repository_detach_head(repo, NULL, NULL));
cl_git_pass(git_repository_head_tree(&tree, repo));
diff --git a/tests/repo/iterator.c b/tests/repo/iterator.c
index 56b51852c..fb70a9ea0 100644
--- a/tests/repo/iterator.c
+++ b/tests/repo/iterator.c
@@ -156,7 +156,7 @@ void test_repo_iterator__index_icase(void)
{
git_iterator *i;
git_index *index;
- unsigned int caps;
+ int caps;
g_repo = cl_git_sandbox_init("icase");
diff --git a/tests/repo/message.c b/tests/repo/message.c
index 57e8e5f4d..87574590b 100644
--- a/tests/repo/message.c
+++ b/tests/repo/message.c
@@ -4,38 +4,36 @@
#include "posix.h"
static git_repository *_repo;
-static git_buf _path;
-static git_buf _actual;
void test_repo_message__initialize(void)
{
- _repo = cl_git_sandbox_init("testrepo.git");
- git_buf_init(&_actual, 0);
+ _repo = cl_git_sandbox_init("testrepo.git");
}
void test_repo_message__cleanup(void)
{
- cl_git_sandbox_cleanup();
- git_buf_free(&_path);
- git_buf_free(&_actual);
+ cl_git_sandbox_cleanup();
}
void test_repo_message__none(void)
{
- cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo));
+ git_buf actual = GIT_BUF_INIT;
+ cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&actual, _repo));
}
void test_repo_message__message(void)
{
+ git_buf path = GIT_BUF_INIT, actual = GIT_BUF_INIT;
const char expected[] = "Test\n\nThis is a test of the emergency broadcast system\n";
- cl_git_pass(git_buf_joinpath(&_path, git_repository_path(_repo), "MERGE_MSG"));
- cl_git_mkfile(git_buf_cstr(&_path), expected);
+ cl_git_pass(git_buf_joinpath(&path, git_repository_path(_repo), "MERGE_MSG"));
+ cl_git_mkfile(git_buf_cstr(&path), expected);
- cl_git_pass(git_repository_message(&_actual, _repo));
- cl_assert_equal_s(expected, _actual);
- git_buf_free(&_actual);
+ cl_git_pass(git_repository_message(&actual, _repo));
+ cl_assert_equal_s(expected, git_buf_cstr(&actual));
+ git_buf_free(&actual);
- cl_git_pass(p_unlink(git_buf_cstr(&_path)));
- cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&_actual, _repo));
+ cl_git_pass(p_unlink(git_buf_cstr(&path)));
+ cl_assert_equal_i(GIT_ENOTFOUND, git_repository_message(&actual, _repo));
+ git_buf_free(&path);
}
diff --git a/tests/repo/open.c b/tests/repo/open.c
index 7cfe041c2..f7420bd3a 100644
--- a/tests/repo/open.c
+++ b/tests/repo/open.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "fileops.h"
+#include "sysdir.h"
#include <ctype.h>
void test_repo_open__cleanup(void)
@@ -323,7 +324,7 @@ void test_repo_open__no_config(void)
git_repository_free(repo);
cl_fixture_cleanup("empty_standard_repo");
- git_futils_dirs_global_shutdown();
+ git_sysdir_global_shutdown();
}
void test_repo_open__force_bare(void)
diff --git a/tests/repo/shallow.c b/tests/repo/shallow.c
index 1cc66ae40..5aeaf2def 100644
--- a/tests/repo/shallow.c
+++ b/tests/repo/shallow.c
@@ -31,3 +31,9 @@ void test_repo_shallow__shallow_repo(void)
cl_assert_equal_i(1, git_repository_is_shallow(g_repo));
}
+void test_repo_shallow__clears_errors(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_assert_equal_i(0, git_repository_is_shallow(g_repo));
+ cl_assert_equal_p(NULL, giterr_last());
+}
diff --git a/tests/repo/state.c b/tests/repo/state.c
index 5a0a5f360..5e7227205 100644
--- a/tests/repo/state.c
+++ b/tests/repo/state.c
@@ -37,7 +37,7 @@ void test_repo_state__none_with_HEAD_attached(void)
void test_repo_state__none_with_HEAD_detached(void)
{
- cl_git_pass(git_repository_detach_head(_repo));
+ cl_git_pass(git_repository_detach_head(_repo, NULL, NULL));
assert_repo_state(GIT_REPOSITORY_STATE_NONE);
}
diff --git a/tests/reset/hard.c b/tests/reset/hard.c
index 0f80d32df..d4c7db45f 100644
--- a/tests/reset/hard.c
+++ b/tests/reset/hard.c
@@ -69,10 +69,9 @@ void test_reset_hard__resetting_reverts_modified_files(void)
cl_assert_equal_s(before[i], content.ptr);
}
- retrieve_target_from_oid(
- &target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f");
+ cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
- cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
for (i = 0; i < 4; ++i) {
cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
@@ -95,9 +94,9 @@ void test_reset_hard__cannot_reset_in_a_bare_repository(void)
cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
cl_assert(git_repository_is_bare(bare) == true);
- retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO);
+ cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
- cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD));
+ cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD, NULL, NULL));
git_repository_free(bare);
}
@@ -152,8 +151,8 @@ void test_reset_hard__resetting_reverts_unmerged(void)
unmerged_index_init(index, entries);
cl_git_pass(git_index_write(index));
- retrieve_target_from_oid(&target, repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f");
- cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
cl_assert(git_path_exists("status/conflicting_file") == 0);
@@ -183,8 +182,8 @@ void test_reset_hard__cleans_up_merge(void)
cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD"));
cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6");
- retrieve_target_from_oid(&target, repo, "0017bd4ab1ec30440b17bae1680cff124ab5f1f6");
- cl_git_pass(git_reset(repo, target, GIT_RESET_HARD));
+ cl_git_pass(git_revparse_single(&target, repo, "0017bd4"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path)));
cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path)));
@@ -198,3 +197,30 @@ void test_reset_hard__cleans_up_merge(void)
git_buf_free(&merge_mode_path);
git_buf_free(&orig_head_path);
}
+
+void test_reset_hard__reflog_is_correct(void)
+{
+ const char *exp_msg = "commit: Add a file which name should appear before the "
+ "\"subdir/\" folder while being dealt with by the treewalker";
+
+ reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
+
+ /* Branch not moving, no reflog entry */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
+ reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
+
+ /* Moved branch, expect default message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
+ reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 4, NULL, "reset: moving");
+
+ /* Moved branch, expect custom message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, "message1"));
+ reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 5, NULL, "message1");
+}
diff --git a/tests/reset/mixed.c b/tests/reset/mixed.c
index 7b90c23f1..25272a75c 100644
--- a/tests/reset/mixed.c
+++ b/tests/reset/mixed.c
@@ -27,9 +27,9 @@ void test_reset_mixed__cannot_reset_in_a_bare_repository(void)
cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
cl_assert(git_repository_is_bare(bare) == true);
- retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO);
+ cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
- cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED));
+ cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED, NULL, NULL));
git_repository_free(bare);
}
@@ -40,10 +40,36 @@ void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void)
cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_CURRENT);
- retrieve_target_from_oid(&target, repo, "605812ab7fe421fdd325a935d35cb06a9234a7d7");
+ cl_git_pass(git_revparse_single(&target, repo, "605812a"));
- cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_WT_NEW);
}
+
+void test_reset_mixed__reflog_is_correct(void)
+{
+ const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
+
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Branch not moving, no reflog entry */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Moved branch, expect default message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving");
+
+ /* Moved branch, expect custom message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, "message1"));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 11, NULL, "message1");
+}
diff --git a/tests/reset/reset_helpers.c b/tests/reset/reset_helpers.c
index 17edca4e9..7a335a600 100644
--- a/tests/reset/reset_helpers.c
+++ b/tests/reset/reset_helpers.c
@@ -1,10 +1,20 @@
#include "clar_libgit2.h"
#include "reset_helpers.h"
-void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha)
+void reflog_check(git_repository *repo, const char *refname,
+ size_t exp_count, const char *exp_email, const char *exp_msg)
{
- git_oid oid;
+ git_reflog *log;
+ const git_reflog_entry *entry;
- cl_git_pass(git_oid_fromstr(&oid, sha));
- cl_git_pass(git_object_lookup(object_out, repo, &oid, GIT_OBJ_ANY));
+ cl_git_pass(git_reflog_read(&log, repo, refname));
+ cl_assert_equal_i(exp_count, git_reflog_entrycount(log));
+ entry = git_reflog_entry_byindex(log, 0);
+
+ if (exp_email)
+ cl_assert_equal_s(exp_email, git_reflog_entry_committer(entry)->email);
+ if (exp_msg)
+ cl_assert_equal_s(exp_msg, git_reflog_entry_message(entry));
+
+ git_reflog_free(log);
}
diff --git a/tests/reset/reset_helpers.h b/tests/reset/reset_helpers.h
index 5dbe9d2c7..e7e048514 100644
--- a/tests/reset/reset_helpers.h
+++ b/tests/reset/reset_helpers.h
@@ -3,4 +3,5 @@
#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d"
#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0"
-extern void retrieve_target_from_oid(git_object **object_out, git_repository *repo, const char *sha);
+void reflog_check(git_repository *repo, const char *refname,
+ size_t exp_count, const char *exp_email, const char *exp_msg);
diff --git a/tests/reset/soft.c b/tests/reset/soft.c
index bd6fcc205..c889c0355 100644
--- a/tests/reset/soft.c
+++ b/tests/reset/soft.c
@@ -26,12 +26,11 @@ static void assert_reset_soft(bool should_be_detached)
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
-
- retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+ cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
cl_assert(git_repository_head_detached(repo) == should_be_detached);
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert(git_repository_head_detached(repo) == should_be_detached);
@@ -46,7 +45,7 @@ void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(vo
void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
{
- git_repository_detach_head(repo);
+ git_repository_detach_head(repo, NULL, NULL);
assert_reset_soft(true);
}
@@ -60,9 +59,9 @@ void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_ch
git_oid_fmt(raw_head_oid, &oid);
raw_head_oid[GIT_OID_HEXSZ] = '\0';
- retrieve_target_from_oid(&target, repo, raw_head_oid);
+ cl_git_pass(git_revparse_single(&target, repo, raw_head_oid));
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_pass(git_oid_streq(&oid, raw_head_oid));
@@ -73,9 +72,9 @@ void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void
git_oid oid;
/* b25fa35 is a tag, pointing to another tag which points to commit e90810b */
- retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
+ cl_git_pass(git_revparse_single(&target, repo, "b25fa35"));
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert(git_repository_head_detached(repo) == false);
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
@@ -85,27 +84,27 @@ void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void
void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
{
/* 53fc32d is the tree of commit e90810b */
- retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016");
+ cl_git_pass(git_revparse_single(&target, repo, "53fc32d"));
- cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
git_object_free(target);
/* 521d87c is an annotated tag pointing to a blob */
- retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
- cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_revparse_single(&target, repo, "521d87c"));
+ cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
}
void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void)
{
git_reference *head;
- retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+ cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_assert_equal_i(true, git_repository_head_unborn(repo));
- cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_assert_equal_i(false, git_repository_head_unborn(repo));
@@ -119,13 +118,13 @@ void test_reset_soft__fails_when_merging(void)
{
git_buf merge_head_path = GIT_BUF_INIT;
- cl_git_pass(git_repository_detach_head(repo));
+ cl_git_pass(git_repository_detach_head(repo, NULL, NULL));
cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");
- retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+ cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
- cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT));
+ cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
git_buf_free(&merge_head_path);
@@ -153,5 +152,31 @@ void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE
cl_git_pass(git_reference_peel(&target, head, GIT_OBJ_COMMIT));
git_reference_free(head);
- cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT));
+ cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+}
+
+void test_reset_soft_reflog_is_correct(void)
+{
+ const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
+
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Branch not moving, no reflog entry */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+ /* Moved branch, expect default message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving");
+
+ /* Moved branch, expect custom message */
+ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, "message1"));
+ reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+ reflog_check(repo, "refs/heads/master", 11, NULL, "message1");
}
diff --git a/tests/revert/workdir.c b/tests/revert/workdir.c
index 5be397c93..47abeb8f0 100644
--- a/tests/revert/workdir.c
+++ b/tests/revert/workdir.c
@@ -15,8 +15,15 @@ static git_index *repo_index;
// Fixture setup and teardown
void test_revert_workdir__initialize(void)
{
+ git_config *cfg;
+
repo = cl_git_sandbox_init(TEST_REPO_PATH);
git_repository_index(&repo_index, repo);
+
+ /* Ensure that the user's merge.conflictstyle doesn't interfere */
+ cl_git_pass(git_repository_config(&cfg, repo));
+ cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge"));
+ git_config_free(cfg);
}
void test_revert_workdir__cleanup(void)
@@ -41,7 +48,7 @@ void test_revert_workdir__automerge(void)
git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac");
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -74,7 +81,7 @@ void test_revert_workdir__conflicts(void)
cl_git_pass(git_repository_head(&head_ref, repo));
cl_git_pass(git_reference_peel((git_object **)&head, head_ref, GIT_OBJ_COMMIT));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
cl_git_pass(git_revert(repo, commit, NULL));
@@ -125,7 +132,7 @@ void test_revert_workdir__orphan(void)
git_oid_fromstr(&head_oid, "39467716290f6df775a91cdb9a4eb39295018145");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&revert_oid, "ebb03002cee5d66c7732dd06241119fe72ab96a5");
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -160,7 +167,7 @@ void test_revert_workdir__again(void)
cl_git_pass(git_repository_head(&head_ref, repo));
cl_git_pass(git_reference_peel((git_object **)&orig_head, head_ref, GIT_OBJ_COMMIT));
- cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_revert(repo, orig_head, NULL));
@@ -208,7 +215,7 @@ void test_revert_workdir__again_after_automerge(void)
git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac");
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -256,7 +263,7 @@ void test_revert_workdir__again_after_edit(void)
cl_git_pass(git_oid_fromstr(&orig_head_oid, "399fb3aba3d9d13f7d40a9254ce4402067ef3149"));
cl_git_pass(git_commit_lookup(&orig_head, repo, &orig_head_oid));
- cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)orig_head, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_oid_fromstr(&revert_oid, "2d440f2b3147d3dc7ad1085813478d6d869d5a4d"));
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -307,7 +314,7 @@ void test_revert_workdir__again_after_edit_two(void)
cl_git_pass(git_oid_fromstr(&head_commit_oid, "e34ef1afe54eb526fd92eec66084125f340f1d65"));
cl_git_pass(git_commit_lookup(&head_commit, repo, &head_commit_oid));
- cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_oid_fromstr(&revert_commit_oid, "71eb9c2b53dbbf3c45fb28b27c850db4b7fb8011"));
cl_git_pass(git_commit_lookup(&revert_commit, repo, &revert_commit_oid));
@@ -360,7 +367,7 @@ void test_revert_workdir__conflict_use_ours(void)
git_oid_fromstr(&head_oid, "72333f47d4e83616630ff3b0ffe4c0faebcc3c45");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&revert_oid, "d1d403d22cbe24592d725f442835cf46fe60c8ac");
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -396,7 +403,7 @@ void test_revert_workdir__rename_1_of_2(void)
git_oid_fromstr(&head_oid, "cef56612d71a6af8d8015691e4865f7fece905b5");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&revert_oid, "55568c8de5322ff9a95d72747a239cdb64a19965");
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -430,7 +437,7 @@ void test_revert_workdir__rename(void)
git_oid_fromstr(&head_oid, "55568c8de5322ff9a95d72747a239cdb64a19965");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&revert_oid, "0aa8c7e40d342fff78d60b29a4ba8e993ed79c51");
cl_git_pass(git_commit_lookup(&commit, repo, &revert_oid));
@@ -459,7 +466,7 @@ void test_revert_workdir__head(void)
/* HEAD is 2d440f2b3147d3dc7ad1085813478d6d869d5a4d */
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJ_COMMIT));
- cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_revert(repo, commit, NULL));
cl_assert(merge_test_index(repo_index, merge_index_entries, 4));
@@ -496,7 +503,7 @@ void test_revert_workdir__merge_fails_without_mainline_specified(void)
git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
cl_must_fail(git_revert(repo, head, NULL));
cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
@@ -523,7 +530,7 @@ void test_revert_workdir__merge_first_parent(void)
git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_revert(repo, head, &opts));
@@ -548,7 +555,7 @@ void test_revert_workdir__merge_second_parent(void)
git_oid_fromstr(&head_oid, "5acdc74af27172ec491d213ee36cea7eb9ef2579");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
- cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD));
+ cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_revert(repo, head, &opts));
diff --git a/tests/revwalk/basic.c b/tests/revwalk/basic.c
index 6d55aed54..9fe8a350b 100644
--- a/tests/revwalk/basic.c
+++ b/tests/revwalk/basic.c
@@ -252,3 +252,41 @@ void test_revwalk_basic__push_range(void)
cl_git_pass(git_revwalk_push_range(_walk, "9fd738e~2..9fd738e"));
cl_git_pass(test_walk_only(_walk, commit_sorting_segment, 1));
}
+
+void test_revwalk_basic__push_mixed(void)
+{
+ git_oid oid;
+ int i = 0;
+
+ revwalk_basic_setup_walk(NULL);
+
+ git_revwalk_reset(_walk);
+ git_revwalk_sorting(_walk, 0);
+ cl_git_pass(git_revwalk_push_glob(_walk, "tags"));
+
+ while (git_revwalk_next(&oid, _walk) == 0) {
+ i++;
+ }
+
+ /* git rev-list --count --glob=tags #=> 9 */
+ cl_assert_equal_i(9, i);
+}
+
+void test_revwalk_basic__push_all(void)
+{
+ git_oid oid;
+ int i = 0;
+
+ revwalk_basic_setup_walk(NULL);
+
+ git_revwalk_reset(_walk);
+ git_revwalk_sorting(_walk, 0);
+ cl_git_pass(git_revwalk_push_glob(_walk, "*"));
+
+ while (git_revwalk_next(&oid, _walk) == 0) {
+ i++;
+ }
+
+ /* git rev-list --count --all #=> 15 */
+ cl_assert_equal_i(15, i);
+}
diff --git a/tests/stash/save.c b/tests/stash/save.c
index 293a89a97..b5a793eef 100644
--- a/tests/stash/save.c
+++ b/tests/stash/save.c
@@ -196,7 +196,7 @@ void test_stash_save__cannot_stash_against_a_bare_repository(void)
void test_stash_save__can_stash_against_a_detached_head(void)
{
- git_repository_detach_head(repo);
+ git_repository_detach_head(repo, NULL, NULL);
cl_git_pass(git_stash_save(&stash_tip_oid, repo, signature, NULL, GIT_STASH_DEFAULT));