Commit 42c8f8f8 by Ben Straub

Merge remote-tracking branch 'libgit2/development' into blame

parents a7d28f40 5c50f22a
......@@ -68,5 +68,6 @@ Sven Strickroth
Tim Branyen
Tim Clem
Tim Harder
Torsten Bögershausen
Trent Mick
Vicent Marti
......@@ -27,9 +27,14 @@ OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF )
OPTION( TAGS "Generate tags" OFF )
OPTION( PROFILE "Generate profiling information" OFF )
OPTION( ENABLE_TRACE "Enables tracing support" OFF )
OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
OPTION( LIBGIT2_FILENAME "Name of the produced binary" OFF )
OPTION( ANDROID "Build for android NDK" OFF )
OPTION( ANDROID "Build for android NDK" OFF )
OPTION( USE_ICONV "Link with and use iconv library" OFF )
IF(APPLE)
SET( USE_ICONV ON )
ENDIF()
IF(MSVC)
# This option is only available when building with MSVC. By default, libgit2
......@@ -43,8 +48,21 @@ IF(MSVC)
# This option must match the settings used in your program, in particular if you
# are linking statically
OPTION( STATIC_CRT "Link the static CRT libraries" ON )
# By default, libgit2 is built with WinHTTP. To use the built-in
# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
OPTION( WINHTTP "Use Win32 WinHTTP routines" ON )
ENDIF()
# This variable will contain the libraries we need to put into
# libgit2.pc's Requires.private. That is, what we're linking to or
# what someone who's statically linking us needs to link to.
SET(LIBGIT2_PC_REQUIRES "")
# This will be set later if we use the system's http-parser library or
# use iconv (OSX) and will be written to the Libs.private field in the
# pc file.
SET(LIBGIT2_PC_LIBS "")
# Installation paths
#
SET(BIN_INSTALL_DIR bin CACHE PATH "Where to install binaries to.")
......@@ -56,9 +74,18 @@ FUNCTION(TARGET_OS_LIBRARIES target)
TARGET_LINK_LIBRARIES(${target} ws2_32)
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")
TARGET_LINK_LIBRARIES(${target} rt)
ENDIF ()
SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lrt" PARENT_SCOPE)
ENDIF()
IF(USE_ICONV)
TARGET_LINK_LIBRARIES(${target} iconv)
ADD_DEFINITIONS(-DGIT_USE_ICONV)
SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -liconv" PARENT_SCOPE)
ENDIF()
IF(THREADSAFE)
TARGET_LINK_LIBRARIES(${target} ${CMAKE_THREAD_LIBS_INIT})
ENDIF()
......@@ -97,7 +124,7 @@ SET(LIBGIT2_VERSION_STRING "${LIBGIT2_VERSION_MAJOR}.${LIBGIT2_VERSION_MINOR}.${
# Find required dependencies
INCLUDE_DIRECTORIES(src include)
IF (WIN32 AND NOT MINGW)
IF (WIN32 AND WINHTTP AND NOT MINGW)
ADD_DEFINITIONS(-DGIT_WINHTTP)
ELSE ()
IF (NOT AMIGA)
......@@ -108,6 +135,7 @@ ELSE ()
IF (HTTP_PARSER_FOUND AND HTTP_PARSER_VERSION_MAJOR EQUAL 2)
INCLUDE_DIRECTORIES(${HTTP_PARSER_INCLUDE_DIRS})
LINK_LIBRARIES(${HTTP_PARSER_LIBRARIES})
SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lhttp_parser")
ELSE()
MESSAGE("http-parser was not found or is too old; using bundled 3rd-party sources.")
INCLUDE_DIRECTORIES(deps/http-parser)
......@@ -121,6 +149,7 @@ IF (WIN32 AND NOT MINGW AND NOT SHA1_TYPE STREQUAL "builtin")
FILE(GLOB SRC_SHA1 src/hash/hash_win32.c)
ELSEIF (OPENSSL_FOUND AND NOT SHA1_TYPE STREQUAL "builtin")
ADD_DEFINITIONS(-DOPENSSL_SHA1)
SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} openssl")
ELSE()
FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
ENDIF()
......@@ -143,6 +172,11 @@ FIND_PACKAGE(ZLIB QUIET)
IF (ZLIB_FOUND)
INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIRS})
LINK_LIBRARIES(${ZLIB_LIBRARIES})
IF(APPLE)
SET(LIBGIT2_PC_LIBS "${LIBGIT2_PC_LIBS} -lz")
ELSE()
SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} zlib")
ENDIF()
# Fake the message CMake would have shown
MESSAGE("-- Found zlib: ${ZLIB_LIBRARY}")
ELSE()
......@@ -158,6 +192,7 @@ ENDIF()
IF (LIBSSH2_FOUND)
ADD_DEFINITIONS(-DGIT_SSH)
INCLUDE_DIRECTORIES(${LIBSSH2_INCLUDE_DIR})
SET(LIBGIT2_PC_REQUIRES "${LIBGIT2_PC_REQUIRES} libssh2")
SET(SSH_LIBRARIES ${LIBSSH2_LIBRARIES})
ENDIF()
......
libgit2 - the Git linkable library
======================
==================================
[![Build Status](https://secure.travis-ci.org/libgit2/libgit2.png?branch=development)](http://travis-ci.org/libgit2/libgit2)
libgit2 is a portable, pure C implementation of the Git core methods provided as a
`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).
`libgit2` is licensed under a **very permissive license** (GPLv2 with a special Linking Exception).
This basically means that you can link it (unmodified) with any kind of software without having to
release its source code.
* Website: <http://libgit2.github.com>
* Website: [libgit2.github.com](http://libgit2.github.com)
* StackOverflow Tag: [libgit2](http://stackoverflow.com/questions/tagged/libgit2)
* Issues: <https://github.com/libgit2/libgit2/issues>
* Issues: [GitHub Issues](https://github.com/libgit2/libgit2/issues) (Right here!)
* API documentation: <http://libgit2.github.com/libgit2>
* IRC: #libgit2 on irc.freenode.net.
* 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
[use StackOverflow](http://stackoverflow.com/questions/tagged/libgit2) instead for any questions regarding
......@@ -25,9 +25,9 @@ release its source code.
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
`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
......@@ -43,15 +43,26 @@ and also powering Microsoft's Visual Studio tools for Git. The library provides
* descriptive and detailed error messages
* ...and more (over 175 different API calls)
Optional dependencies
=====================
While the library provides git functionality without the need for
dependencies, it can make use of a few libraries to add to it:
- pthreads (non-Windows) to enable threadsafe access as well as multi-threaded pack generation
- OpenSSL (non-Windows) to talk over HTTPS and provide the SHA-1 functions
- LibSSH2 to enable the ssh transport
- iconv (OSX) to handle the HFS+ path encoding peculiarities
Building libgit2 - Using CMake
==============================
libgit2 builds cleanly on most platforms without any external dependencies.
`libgit2` builds cleanly on most platforms without any external dependencies.
Under Unix-like systems, like Linux, \*BSD and Mac OS X, libgit2 expects `pthreads` to be available;
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 2.6+` (<http://www.cmake.org>) on all platforms.
On most systems you can build the library using the following commands
......@@ -112,8 +123,8 @@ Android
-------
Extract toolchain from NDK using, `make-standalone-toolchain.sh` script.
Optionaly, crosscompile and install OpenSSL inside of it. Then create CMake
toolchain file that configures paths to your crosscompiler (substitude `{PATH}`
Optionally, crosscompile and install OpenSSL inside of it. Then create CMake
toolchain file that configures paths to your crosscompiler (substitute `{PATH}`
with full path to the toolchain):
SET(CMAKE_SYSTEM_NAME Linux)
......@@ -155,7 +166,7 @@ Here are the bindings to libgit2 that are currently available:
* luagit2 <https://github.com/libgit2/luagit2>
* .NET
* libgit2sharp <https://github.com/libgit2/libgit2sharp>
* libgit2net, low level bindings superceeded by libgit2sharp <https://github.com/txdv/libgit2net>
* libgit2net, low level bindings superseded by libgit2sharp <https://github.com/txdv/libgit2net>
* Node.js
* node-gitteh <https://github.com/libgit2/node-gitteh>
* nodegit <https://github.com/tbranyen/nodegit>
......@@ -187,9 +198,9 @@ Check the [contribution guidelines](CONTRIBUTING.md).
License
==================================
libgit2 is under GPL2 **with linking exemption**. This means you
can link to the library with any program, commercial, open source or
other. 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.
......@@ -45,25 +45,23 @@ char *colors[] = {
static int printer(
const git_diff_delta *delta,
const git_diff_range *range,
char usage,
const char *line,
size_t line_len,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *data)
{
int *last_color = data, color = 0;
(void)delta; (void)range; (void)line_len;
(void)delta; (void)hunk;
if (*last_color >= 0) {
switch (usage) {
case GIT_DIFF_LINE_ADDITION: color = 3; break;
case GIT_DIFF_LINE_DELETION: color = 2; break;
switch (line->origin) {
case GIT_DIFF_LINE_ADDITION: color = 3; break;
case GIT_DIFF_LINE_DELETION: color = 2; break;
case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break;
case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break;
case GIT_DIFF_LINE_FILE_HDR: color = 1; break;
case GIT_DIFF_LINE_HUNK_HDR: color = 4; break;
default: color = 0;
case GIT_DIFF_LINE_FILE_HDR: color = 1; break;
case GIT_DIFF_LINE_HUNK_HDR: color = 4; break;
default: break;
}
if (color != *last_color) {
if (*last_color == 1 || color == 1)
......@@ -73,7 +71,13 @@ static int printer(
}
}
fputs(line, stdout);
if (line->origin == GIT_DIFF_LINE_CONTEXT ||
line->origin == GIT_DIFF_LINE_ADDITION ||
line->origin == GIT_DIFF_LINE_DELETION)
fputc(line->origin, stdout);
fwrite(line->content, 1, line->content_len, stdout);
return 0;
}
......@@ -114,20 +118,15 @@ static void usage(const char *message, const char *arg)
exit(1);
}
enum {
FORMAT_PATCH = 0,
FORMAT_COMPACT = 1,
FORMAT_RAW = 2
};
int main(int argc, char *argv[])
{
git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
git_diff_list *diff;
int i, color = -1, format = FORMAT_PATCH, cached = 0;
git_diff *diff;
int i, color = -1, cached = 0;
git_diff_format_t format = GIT_DIFF_FORMAT_PATCH;
char *a, *treeish1 = NULL, *treeish2 = NULL;
const char *dir = ".";
......@@ -148,13 +147,15 @@ int main(int argc, char *argv[])
}
else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
!strcmp(a, "--patch"))
format = FORMAT_PATCH;
format = GIT_DIFF_FORMAT_PATCH;
else if (!strcmp(a, "--cached"))
cached = 1;
else if (!strcmp(a, "--name-only"))
format = GIT_DIFF_FORMAT_NAME_ONLY;
else if (!strcmp(a, "--name-status"))
format = FORMAT_COMPACT;
format = GIT_DIFF_FORMAT_NAME_STATUS;
else if (!strcmp(a, "--raw"))
format = FORMAT_RAW;
format = GIT_DIFF_FORMAT_RAW;
else if (!strcmp(a, "--color"))
color = 0;
else if (!strcmp(a, "--no-color"))
......@@ -218,11 +219,11 @@ int main(int argc, char *argv[])
else if (t1 && cached)
check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
else if (t1) {
git_diff_list *diff2;
git_diff *diff2;
check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
check(git_diff_index_to_workdir(&diff2, repo, NULL, &opts), "Diff");
check(git_diff_merge(diff, diff2), "Merge diffs");
git_diff_list_free(diff2);
git_diff_free(diff2);
}
else if (cached) {
check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD");
......@@ -238,22 +239,12 @@ int main(int argc, char *argv[])
if (color >= 0)
fputs(colors[0], stdout);
switch (format) {
case FORMAT_PATCH:
check(git_diff_print_patch(diff, printer, &color), "Displaying diff");
break;
case FORMAT_COMPACT:
check(git_diff_print_compact(diff, printer, &color), "Displaying diff");
break;
case FORMAT_RAW:
check(git_diff_print_raw(diff, printer, &color), "Displaying diff");
break;
}
check(git_diff_print(diff, format, printer, &color), "Displaying diff");
if (color >= 0)
fputs(colors[0], stdout);
git_diff_list_free(diff);
git_diff_free(diff);
git_tree_free(t1);
git_tree_free(t2);
git_repository_free(repo);
......
......@@ -184,14 +184,18 @@ static void print_commit(git_commit *commit)
static int print_diff(
const git_diff_delta *delta,
const git_diff_range *range,
char usage,
const char *line,
size_t line_len,
const git_diff_hunk *hunk,
const git_diff_line *line,
void *data)
{
(void)delta; (void)range; (void)usage; (void)line_len; (void)data;
fputs(line, stdout);
(void)delta; (void)hunk; (void)data;
if (line->origin == GIT_DIFF_LINE_CONTEXT ||
line->origin == GIT_DIFF_LINE_ADDITION ||
line->origin == GIT_DIFF_LINE_DELETION)
fputc(line->origin, stdout);
fwrite(line->content, 1, line->content_len, stdout);
return 0;
}
......@@ -218,7 +222,7 @@ static int match_with_parent(
{
git_commit *parent;
git_tree *a, *b;
git_diff_list *diff;
git_diff *diff;
int ndeltas;
check(git_commit_parent(&parent, commit, (size_t)i), "Get parent", NULL);
......@@ -229,7 +233,7 @@ static int match_with_parent(
ndeltas = (int)git_diff_num_deltas(diff);
git_diff_list_free(diff);
git_diff_free(diff);
git_tree_free(a);
git_tree_free(b);
git_commit_free(parent);
......@@ -373,7 +377,7 @@ int main(int argc, char *argv[])
if (opt.show_diff) {
git_tree *a = NULL, *b = NULL;
git_diff_list *diff = NULL;
git_diff *diff = NULL;
if (parents > 1)
continue;
......@@ -388,10 +392,10 @@ int main(int argc, char *argv[])
check(git_diff_tree_to_tree(
&diff, git_commit_owner(commit), a, b, &diffopts),
"Diff commit with parent", NULL);
check(git_diff_print_patch(diff, print_diff, NULL),
check(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, print_diff, NULL),
"Displaying diff", NULL);
git_diff_list_free(diff);
git_diff_free(diff);
git_tree_free(a);
git_tree_free(b);
}
......
......@@ -25,13 +25,19 @@ static void print_progress(const progress_data *pd)
: 0.f;
int kbytes = pd->fetch_progress.received_bytes / 1024;
printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
if (pd->fetch_progress.received_objects == pd->fetch_progress.total_objects) {
printf("Resolving deltas %d/%d\r",
pd->fetch_progress.indexed_deltas,
pd->fetch_progress.total_deltas);
} else {
printf("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d) / chk %3d%% (%4" PRIuZ "/%4" PRIuZ ") %s\n",
network_percent, kbytes,
pd->fetch_progress.received_objects, pd->fetch_progress.total_objects,
index_percent, pd->fetch_progress.indexed_objects, pd->fetch_progress.total_objects,
checkout_percent,
pd->completed_steps, pd->total_steps,
pd->path);
}
}
static int fetch_progress(const git_transfer_progress *stats, void *payload)
......
......@@ -72,6 +72,7 @@ int fetch(git_repository *repo, int argc, char **argv)
const git_transfer_progress *stats;
struct dl_data data;
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
int resolve_deltas_ln = 0;
#ifndef _WIN32
pthread_t worker;
#endif
......@@ -113,10 +114,14 @@ int fetch(git_repository *repo, int argc, char **argv)
do {
usleep(10000);
if (stats->total_objects > 0)
if (stats->received_objects == stats->total_objects) {
printf("Resolving deltas %d/%d\r",
stats->indexed_deltas, stats->total_deltas);
} else if (stats->total_objects > 0) {
printf("Received %d/%d objects (%d) in %" PRIuZ " bytes\r",
stats->received_objects, stats->total_objects,
stats->indexed_objects, stats->received_bytes);
}
} while (!data.finished);
if (data.ret < 0)
......@@ -125,8 +130,13 @@ int fetch(git_repository *repo, int argc, char **argv)
pthread_join(worker, NULL);
#endif
printf("\rReceived %d/%d objects in %zu bytes\n",
if (stats->local_objects > 0) {
printf("\rReceived %d/%d objects in %zu bytes (used %d local objects)\n",
stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
} else{
printf("\rReceived %d/%d objects in %zu bytes\n",
stats->indexed_objects, stats->total_objects, stats->received_bytes);
}
// Disconnect the underlying connection to prevent from idling.
git_remote_disconnect(remote);
......
......@@ -46,7 +46,7 @@ int index_pack(git_repository *repo, int argc, char **argv)
return EXIT_FAILURE;
}
if (git_indexer_stream_new(&idx, ".", NULL, NULL) < 0) {
if (git_indexer_stream_new(&idx, ".", NULL, NULL, NULL) < 0) {
puts("bad idx");
return -1;
}
......
......@@ -68,3 +68,4 @@ ok Sebastian Schuberth <sschuberth@gmail.com>
ok Shawn O. Pearce <spearce@spearce.org>
ok Steffen Prohaska <prohaska@zib.de>
ok Sven Verdoolaege <skimo@kotnet.org>
ok Torsten Bögershausen <tboegi@web.de>
......@@ -33,6 +33,7 @@
#include "git2/odb.h"
#include "git2/oid.h"
#include "git2/pack.h"
#include "git2/patch.h"
#include "git2/pathspec.h"
#include "git2/push.h"
#include "git2/refdb.h"
......
......@@ -131,6 +131,13 @@ typedef enum {
/** Don't refresh index/config/etc before doing checkout */
GIT_CHECKOUT_NO_REFRESH = (1u << 9),
/** Allow checkout to skip unmerged files */
GIT_CHECKOUT_SKIP_UNMERGED = (1u << 10),
/** For unmerged files, checkout stage 2 from index */
GIT_CHECKOUT_USE_OURS = (1u << 11),
/** For unmerged files, checkout stage 3 from index */
GIT_CHECKOUT_USE_THEIRS = (1u << 12),
/** Treat pathspec as simple list of exact match file paths */
GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH = (1u << 13),
......@@ -141,13 +148,6 @@ typedef enum {
* THE FOLLOWING OPTIONS ARE NOT YET IMPLEMENTED
*/
/** Allow checkout to skip unmerged files (NOT IMPLEMENTED) */
GIT_CHECKOUT_SKIP_UNMERGED = (1u << 10),
/** For unmerged files, checkout stage 2 from index (NOT IMPLEMENTED) */
GIT_CHECKOUT_USE_OURS = (1u << 11),
/** For unmerged files, checkout stage 3 from index (NOT IMPLEMENTED) */
GIT_CHECKOUT_USE_THEIRS = (1u << 12),
/** Recursively checkout submodules with same options (NOT IMPLEMENTED) */
GIT_CHECKOUT_UPDATE_SUBMODULES = (1u << 16),
/** Recursively checkout submodules if HEAD moved in super repo (NOT IMPLEMENTED) */
......@@ -238,6 +238,9 @@ typedef struct git_checkout_opts {
git_tree *baseline; /** expected content of workdir, defaults to HEAD */
const char *target_directory; /** alternative checkout path to workdir */
const char *our_label; /** the name of the "our" side of conflicts */
const char *their_label; /** the name of the "their" side of conflicts */
} git_checkout_opts;
#define GIT_CHECKOUT_OPTS_VERSION 1
......
......@@ -225,6 +225,9 @@ GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps);
* Update the contents of an existing index object in memory
* by reading from the hard disk.
*
* If the file doesn't exist on the filesystem, the index
* will be cleared from its current content.
*
* @param index an existing index object
* @return 0 or an error code
*/
......
......@@ -20,12 +20,16 @@ typedef struct git_indexer_stream git_indexer_stream;
*
* @param out where to store the indexer instance
* @param path to the directory where the packfile should be stored
* @param odb object database from which to read base objects when
* fixing thin packs. Pass NULL if no thin pack is expected (an error
* will be returned if there are bases missing)
* @param progress_cb function to call with progress information
* @param progress_cb_payload payload for the progress callback
*/
GIT_EXTERN(int) git_indexer_stream_new(
git_indexer_stream **out,
const char *path,
git_odb *odb,
git_transfer_progress_callback progress_cb,
void *progress_cb_payload);
......
......@@ -358,6 +358,20 @@ GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_oty
GIT_EXTERN(int) git_odb_hashfile(git_oid *out, const char *path, git_otype type);
/**
* Create a copy of an odb_object
*
* The returned copy must be manually freed with `git_odb_object_free`.
* Note that because of an implementation detail, the returned copy will be
* the same pointer as `source`: the object is internally refcounted, so the
* copy still needs to be freed twice.
*
* @param dest pointer where to store the copy
* @param source object to copy
* @return 0 or an error code
*/
GIT_EXTERN(int) git_odb_object_dup(git_odb_object **dest, git_odb_object *source);
/**
* Close an ODB object
*
* This method must always be called once a `git_odb_object` is no
......
......@@ -158,7 +158,7 @@ GIT_EXTERN(uint32_t) git_packbuilder_object_count(git_packbuilder *pb);
GIT_EXTERN(uint32_t) git_packbuilder_written(git_packbuilder *pb);
/** Packbuilder progress notification function */
typedef void (*git_packbuilder_progress)(
typedef int (*git_packbuilder_progress)(
int stage,
unsigned int current,
unsigned int total,
......
/*
* 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_git_patch_h__
#define INCLUDE_git_patch_h__
#include "common.h"
#include "types.h"
#include "oid.h"
#include "diff.h"
/**
* @file git2/patch.h
* @brief Patch handling routines.
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* The diff patch is used to store all the text diffs for a delta.
*
* You can easily loop over the content of patches and get information about
* them.
*/
typedef struct git_patch git_patch;
/**
* Return the diff delta and patch for an entry in the diff list.
*
* The `git_patch` is a newly created object contains the text diffs
* for the delta. You have to call `git_patch_free()` when you are
* done with it. You can use the patch object to loop over all the hunks
* and lines in the diff of the one delta.
*
* For an unchanged file or a binary file, no `git_patch` will be
* created, the output will be set to NULL, and the `binary` flag will be
* set true in the `git_diff_delta` structure.
*
* The `git_diff_delta` pointer points to internal data and you do not have
* to release it when you are done with it. It will go away when the
* `git_diff` and `git_patch` go away.
*
* It is okay to pass NULL for either of the output parameters; if you pass
* NULL for the `git_patch`, then the text diff will not be calculated.
*
* @param out Output parameter for the delta patch object
* @param diff Diff list object
* @param idx Index into diff list
* @return 0 on success, other value < 0 on error
*/
GIT_EXTERN(int) git_patch_from_diff(
git_patch **out, git_diff *diff, size_t idx);
/**
* Directly generate a patch from the difference between two blobs.
*
* This is just like `git_diff_blobs()` 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_blob Blob for old side of diff, or NULL for empty blob
* @param old_as_path Treat old blob as if it had this filename; can be NULL
* @param new_blob Blob for new side of diff, or NULL for empty blob
* @param new_as_path Treat new blob 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_blobs(
git_patch **out,
const git_blob *old_blob,
const char *old_as_path,
const git_blob *new_blob,
const char *new_as_path,
const git_diff_options *opts);
/**
* Directly generate a patch from the difference between a blob and a buffer.
*
* This is just like `git_diff_blob_to_buffer()` 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_blob Blob for old side of diff, or NULL for empty blob
* @param old_as_path Treat old blob as if it had this filename; can be NULL
* @param buffer Raw data for new side of diff, or NULL for empty
* @param buffer_len Length of raw data for new side of diff
* @param buffer_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_blob_and_buffer(
git_patch **out,
const git_blob *old_blob,
const char *old_as_path,
const char *buffer,
size_t buffer_len,
const char *buffer_as_path,
const git_diff_options *opts);
/**
* Free a git_patch object.
*/
GIT_EXTERN(void) git_patch_free(git_patch *patch);
/**
* Get the delta associated with a patch
*/
GIT_EXTERN(const git_diff_delta *) git_patch_get_delta(git_patch *patch);
/**
* Get the number of hunks in a patch
*/
GIT_EXTERN(size_t) git_patch_num_hunks(git_patch *patch);
/**
* Get line counts of each type in a patch.
*
* This helps imitate a diff --numstat type of output. For that purpose,
* you only need the `total_additions` and `total_deletions` values, but we
* include the `total_context` line count in case you want the total number
* of lines of diff output that will be generated.
*
* All outputs are optional. Pass NULL if you don't need a particular count.
*
* @param total_context Count of context lines in output, can be NULL.
* @param total_additions Count of addition lines in output, can be NULL.
* @param total_deletions Count of deletion lines in output, can be NULL.
* @param patch The git_patch object
* @return 0 on success, <0 on error
*/
GIT_EXTERN(int) git_patch_line_stats(
size_t *total_context,
size_t *total_additions,
size_t *total_deletions,
const git_patch *patch);
/**
* Get the information about a hunk in a patch
*
* Given a patch and a hunk index into the patch, this returns detailed
* information about that hunk. Any of the output pointers can be passed
* as NULL if you don't care about that particular piece of information.
*
* @param out Output pointer to git_diff_hunk of hunk
* @param lines_in_hunk Output count of total lines in this hunk
* @param patch Input pointer to patch object
* @param hunk_idx Input index of hunk to get information about
* @return 0 on success, GIT_ENOTFOUND if hunk_idx out of range, <0 on error
*/
GIT_EXTERN(int) git_patch_get_hunk(
const git_diff_hunk **out,
size_t *lines_in_hunk,
git_patch *patch,
size_t hunk_idx);
/**
* Get the number of lines in a hunk.
*
* @param patch The git_patch object
* @param hunk_idx Index of the hunk
* @return Number of lines in hunk or -1 if invalid hunk index
*/
GIT_EXTERN(int) git_patch_num_lines_in_hunk(
git_patch *patch,
size_t hunk_idx);
/**
* Get data about a line in a hunk of a patch.
*
* Given a patch, a hunk index, and a line index in the hunk, this
* will return a lot of details about that line. If you pass a hunk
* index larger than the number of hunks or a line index larger than
* the number of lines in the hunk, this will return -1.
*
* @param out The git_diff_line data for this line
* @param patch The patch to look in
* @param hunk_idx The index of the hunk
* @param line_of_hunk The index of the line in the hunk
* @return 0 on success, <0 on failure
*/
GIT_EXTERN(int) git_patch_get_line_in_hunk(
const git_diff_line **out,
git_patch *patch,
size_t hunk_idx,
size_t line_of_hunk);
/**
* Look up size of patch diff data in bytes
*
* This returns the raw size of the patch data. This only includes the
* actual data from the lines of the diff, not the file or hunk headers.
*
* If you pass `include_context` as true (non-zero), this will be the size
* of all of the diff output; if you pass it as false (zero), this will
* only include the actual changed lines (as if `context_lines` was 0).
*
* @param patch A git_patch representing changes to one file
* @param include_context Include context lines in size if non-zero
* @param include_hunk_headers Include hunk header lines if non-zero
* @param include_file_headers Include file header lines if non-zero
* @return The number of bytes of data
*/
GIT_EXTERN(size_t) git_patch_size(
git_patch *patch,
int include_context,
int include_hunk_headers,
int include_file_headers);
/**
* Serialize the patch to text via callback.
*
* Returning a non-zero value from the callback will terminate the iteration
* and cause this return `GIT_EUSER`.
*
* @param patch A git_patch representing changes to one file
* @param print_cb Callback function to output lines of the patch. Will be
* called for file headers, hunk headers, and diff lines.
* @param payload Reference pointer that will be passed to your callbacks.
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_patch_print(
git_patch *patch,
git_diff_line_cb print_cb,
void *payload);
/**
* Get the content of a patch as a single diff text.
*
* @param string Allocated string; caller must free.
* @param patch A git_patch representing changes to one file
* @return 0 on success, <0 on failure.
*/
GIT_EXTERN(int) git_patch_to_str(
char **string,
git_patch *patch);
GIT_END_DECL
/**@}*/
#endif
......@@ -187,7 +187,7 @@ GIT_EXTERN(int) git_pathspec_match_tree(
*/
GIT_EXTERN(int) git_pathspec_match_diff(
git_pathspec_match_list **out,
git_diff_list *diff,
git_diff *diff,
uint32_t flags,
git_pathspec *ps);
......
......@@ -40,7 +40,7 @@ typedef struct {
#define GIT_PUSH_OPTIONS_INIT { GIT_PUSH_OPTIONS_VERSION }
/** Push network progress notification function */
typedef void (*git_push_transfer_progress)(
typedef int (*git_push_transfer_progress)(
unsigned int current,
unsigned int total,
size_t bytes,
......
......@@ -31,10 +31,11 @@ GIT_BEGIN_DECL
* git_reflog_free().
*
* @param out pointer to reflog
* @param ref reference to read the reflog for
* @param repo the repostiory
* @param name reference to look up
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reflog_read(git_reflog **out, const git_reference *ref);
GIT_EXTERN(int) git_reflog_read(git_reflog **out, git_repository *repo, const char *name);
/**
* Write an existing in-memory reflog object back to disk
......@@ -59,26 +60,45 @@ GIT_EXTERN(int) git_reflog_write(git_reflog *reflog);
GIT_EXTERN(int) git_reflog_append(git_reflog *reflog, const git_oid *id, const git_signature *committer, const char *msg);
/**
* Rename the reflog for the given reference
* Add a new entry to the named reflog.
*
* This utility function loads the named reflog, appends to it and
* writes it back out to the backend.
*
* `msg` is optional and can be NULL.
*
* @param repo the repository to act on
* @param name the reflog's name
* @param id the OID the reference is now pointing to
* @param committer the signature of the committer
* @param msg the reflog message
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reflog_append_to(git_repository *repo, const char *name, const git_oid *id, const git_signature *committer, const char *msg);
/**
* Rename a reflog
*
* The reflog to be renamed is expected to already exist
*
* The new name will be checked for validity.
* See `git_reference_create_symbolic()` for rules about valid names.
*
* @param ref the reference
* @param name the new name of the reference
* @param repo the repository
* @param old_name the old name of the reference
* @param new_name the new name of the reference
* @return 0 on success, GIT_EINVALIDSPEC or an error code
*/
GIT_EXTERN(int) git_reflog_rename(git_reference *ref, const char *name);
GIT_EXTERN(int) git_reflog_rename(git_repository *repo, const char *old_name, const char *name);
/**
* Delete the reflog for the given reference
*
* @param ref the reference
* @param repo the repository
* @param name the reflog to delete
* @return 0 or an error code
*/
GIT_EXTERN(int) git_reflog_delete(git_reference *ref);
GIT_EXTERN(int) git_reflog_delete(git_repository *repo, const char *name);
/**
* Get the number of log entries in a reflog
......
......@@ -453,7 +453,7 @@ GIT_EXTERN(int) git_reference_is_remote(git_reference *ref);
GIT_EXTERN(int) git_reference_is_tag(git_reference *ref);
typedef enum {
GIT_REF_FORMAT_NORMAL = 0,
GIT_REF_FORMAT_NORMAL = 0u,
/**
* Control whether one-level refnames are accepted
......@@ -461,7 +461,7 @@ typedef enum {
* components). Those are expected to be written only using
* uppercase letters and underscore (FETCH_HEAD, ...)
*/
GIT_REF_FORMAT_ALLOW_ONELEVEL = (1 << 0),
GIT_REF_FORMAT_ALLOW_ONELEVEL = (1u << 0),
/**
* Interpret the provided name as a reference pattern for a
......@@ -470,14 +470,14 @@ typedef enum {
* in place of a one full pathname component
* (e.g., foo/<star>/bar but not foo/bar<star>).
*/
GIT_REF_FORMAT_REFSPEC_PATTERN = (1 << 1),
GIT_REF_FORMAT_REFSPEC_PATTERN = (1u << 1),
/**
* Interpret the name as part of a refspec in shorthand form
* so the `ONELEVEL` naming rules aren't enforced and 'master'
* becomes a valid name.
*/
GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1 << 2),
GIT_REF_FORMAT_REFSPEC_SHORTHAND = (1u << 2),
} git_reference_normalize_t;
/**
......
......@@ -410,7 +410,7 @@ struct git_remote_callbacks {
* progress side-band will be passed to this function (this is
* the 'counting objects' output.
*/
void (*progress)(const char *str, int len, void *data);
int (*progress)(const char *str, int len, void *data);
/**
* Completion is called when different parts of the download
......
......@@ -80,7 +80,7 @@ struct git_odb_backend {
git_odb_backend *, git_odb_foreach_cb cb, void *payload);
int (* writepack)(
git_odb_writepack **, git_odb_backend *,
git_odb_writepack **, git_odb_backend *, git_odb *odb,
git_transfer_progress_callback progress_cb, void *progress_payload);
void (* free)(git_odb_backend *);
......
......@@ -119,6 +119,26 @@ struct git_refdb_backend {
* provide this function; if it is not provided, nothing will be done.
*/
void (*free)(git_refdb_backend *backend);
/**
* Read the reflog for the given reference name.
*/
int (*reflog_read)(git_reflog **out, git_refdb_backend *backend, const char *name);
/**
* Write a reflog to disk.
*/
int (*reflog_write)(git_refdb_backend *backend, git_reflog *reflog);
/**
* Rename a reflog
*/
int (*reflog_rename)(git_refdb_backend *_backend, const char *old_name, const char *new_name);
/**
* Remove a reflog.
*/
int (*reflog_delete)(git_refdb_backend *backend, const char *name);
};
#define GIT_REFDB_BACKEND_VERSION 1
......
/*
* 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_sys_git_reflog_h__
#define INCLUDE_sys_git_reflog_h__
#include "git2/common.h"
#include "git2/types.h"
#include "git2/oid.h"
GIT_BEGIN_DECL
GIT_EXTERN(git_reflog_entry *) git_reflog_entry__alloc(void);
GIT_EXTERN(void) git_reflog_entry__free(git_reflog_entry *entry);
GIT_END_DECL
#endif
......@@ -27,7 +27,6 @@ GIT_BEGIN_DECL
*/
GIT_EXTERN(int) git_repository_new(git_repository **out);
/**
* Reset all the internal state in a repository.
*
......@@ -42,6 +41,25 @@ GIT_EXTERN(int) git_repository_new(git_repository **out);
GIT_EXTERN(void) git_repository__cleanup(git_repository *repo);
/**
* Update the filesystem config settings for an open repository
*
* When a repository is initialized, config values are set based on the
* properties of the filesystem that the repository is on, such as
* "core.ignorecase", "core.filemode", "core.symlinks", etc. If the
* repository is moved to a new filesystem, these properties may no
* longer be correct and API calls may not behave as expected. This
* call reruns the phase of repository initialization that sets those
* properties to compensate for the current filesystem of the repo.
*
* @param repo A repository object
* @param recurse_submodules Should submodules be updated recursively
* @returrn 0 on success, < 0 on error
*/
GIT_EXTERN(int) git_repository_reinit_filesystem(
git_repository *repo,
int recurse_submodules);
/**
* Set the configuration file for this repository
*
* This configuration file will be used for all configuration
......
......@@ -28,11 +28,16 @@ GIT_BEGIN_DECL
*** Begin interface for credentials acquisition ***
*/
/** Authentication type requested */
typedef enum {
/* git_cred_userpass_plaintext */
GIT_CREDTYPE_USERPASS_PLAINTEXT = 1,
GIT_CREDTYPE_SSH_KEYFILE_PASSPHRASE = 2,
GIT_CREDTYPE_SSH_PUBLICKEY = 3,
GIT_CREDTYPE_USERPASS_PLAINTEXT = (1u << 0),
/* git_cred_ssh_key */
GIT_CREDTYPE_SSH_KEY = (1u << 1),
/* git_cred_ssh_custom */
GIT_CREDTYPE_SSH_CUSTOM = (1u << 2),
} git_credtype_t;
/* The base structure for all credential types */
......@@ -56,24 +61,28 @@ typedef LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*git_cred_sign_callback));
typedef int (*git_cred_sign_callback)(void *, ...);
#endif
/* A ssh key file and passphrase */
typedef struct git_cred_ssh_keyfile_passphrase {
/**
* A ssh key from disk
*/
typedef struct git_cred_ssh_key {
git_cred parent;
char *username;
char *publickey;
char *privatekey;
char *passphrase;
} git_cred_ssh_keyfile_passphrase;
} git_cred_ssh_key;
/* A ssh public key and authentication callback */
typedef struct git_cred_ssh_publickey {
/**
* A key with a custom signature function
*/
typedef struct git_cred_ssh_custom {
git_cred parent;
char *username;
char *publickey;
size_t publickey_len;
void *sign_callback;
void *sign_data;
} git_cred_ssh_publickey;
} git_cred_ssh_custom;
/**
* Check whether a credential object contains username information.
......@@ -84,7 +93,7 @@ typedef struct git_cred_ssh_publickey {
GIT_EXTERN(int) git_cred_has_username(git_cred *cred);
/**
* Creates a new plain-text username and password credential object.
* Create a new plain-text username and password credential object.
* The supplied credential parameter will be internally duplicated.
*
* @param out The newly created credential object.
......@@ -98,7 +107,7 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new(
const char *password);
/**
* Creates a new ssh key file and passphrase credential object.
* Create a new passphrase-protected ssh key credential object.
* The supplied credential parameter will be internally duplicated.
*
* @param out The newly created credential object.
......@@ -108,32 +117,38 @@ GIT_EXTERN(int) git_cred_userpass_plaintext_new(
* @param passphrase The passphrase of the credential.
* @return 0 for success or an error code for failure
*/
GIT_EXTERN(int) git_cred_ssh_keyfile_passphrase_new(
GIT_EXTERN(int) git_cred_ssh_key_new(
git_cred **out,
const char *username,
const char *publickey,
const char *privatekey,
const char *passphrase);
const char *passphrase);
/**
* Creates a new ssh public key credential object.
* Create an ssh key credential with a custom signing function.
*
* This lets you use your own function to sign the challenge.
*
* This function and its credential type is provided for completeness
* and wraps `libssh2_userauth_publickey()`, which is undocumented.
*
* The supplied credential parameter will be internally duplicated.
*
* @param out The newly created credential object.
* @param username username to use to authenticate
* @param publickey The bytes of the public key.
* @param publickey_len The length of the public key in bytes.
* @param sign_fn The callback method for authenticating.
* @param sign_data The abstract data sent to the sign_callback method.
* @param sign_fn The callback method to sign the data during the challenge.
* @param sign_data The data to pass to the sign function.
* @return 0 for success or an error code for failure
*/
GIT_EXTERN(int) git_cred_ssh_publickey_new(
GIT_EXTERN(int) git_cred_ssh_custom_new(
git_cred **out,
const char *username,
const char *publickey,
size_t publickey_len,
git_cred_sign_callback sign_fn,
void *sign_data);
size_t publickey_len,
git_cred_sign_callback sign_fn,
void *sign_data);
/**
* Signature of a function which acquires a credential object.
......@@ -165,7 +180,7 @@ typedef enum {
GIT_TRANSPORTFLAGS_NO_CHECK_CERT = 1
} git_transport_flags_t;
typedef void (*git_transport_message_cb)(const char *str, int len, void *data);
typedef int (*git_transport_message_cb)(const char *str, int len, void *data);
typedef struct git_transport git_transport;
......
......@@ -199,6 +199,17 @@ GIT_EXTERN(git_otype) git_tree_entry_type(const git_tree_entry *entry);
GIT_EXTERN(git_filemode_t) git_tree_entry_filemode(const git_tree_entry *entry);
/**
* Get the raw UNIX file attributes of a tree entry
*
* This function does not perform any normalization and is only useful
* if you need to be able to recreate the original tree object.
*
* @param entry a tree entry
* @return filemode as an integer
*/
GIT_EXTERN(git_filemode_t) git_tree_entry_filemode_raw(const git_tree_entry *entry);
/**
* Compare two tree entries
*
* @param e1 first tree entry
......
......@@ -212,11 +212,21 @@ typedef struct git_remote_callbacks git_remote_callbacks;
/**
* This is passed as the first argument to the callback to allow the
* user to see the progress.
*
* - total_objects: number of objects in the packfile being downloaded
* - indexed_objects: received objects that have been hashed
* - received_objects: objects which have been downloaded
* - local_objects: locally-available objects that have been injected
* in order to fix a thin pack.
* - received-bytes: size of the packfile received up to now
*/
typedef struct git_transfer_progress {
unsigned int total_objects;
unsigned int indexed_objects;
unsigned int received_objects;
unsigned int local_objects;
unsigned int total_deltas;
unsigned int indexed_deltas;
size_t received_bytes;
} git_transfer_progress;
......
......@@ -4,6 +4,7 @@ includedir=@CMAKE_INSTALL_PREFIX@/@INCLUDE_INSTALL_DIR@
Name: libgit2
Description: The git library, take 2
Version: @LIBGIT2_VERSION_STRING@
Requires: libcrypto
Libs: -L${libdir} -lgit2 -lz -lcrypto
Requires.private: @LIBGIT2_PC_REQUIRES@
Libs.private: @LIBGIT2_PC_LIBS@
Libs: -L${libdir} -lgit2
Cflags: -I${includedir}
To build RPM pakcages for Fedora, follow these steps:
cp packaging/rpm/libgit2.spec ~/rpmbuild/SPECS
cd ~/rpmbuild/SOURCES
wget https://github.com/downloads/libgit2/libgit2/libgit2-0.16.0.tar.gz
cd ~/rpmbuild/SPECS
rpmbuild -ba libgit2.spec
#
# spec file for package libgit2
#
# Copyright (c) 2012 Saleem Ansari <tuxdna@gmail.com>
# Copyright (c) 2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
# Copyright (c) 2011, Sascha Peilicke <saschpe@gmx.de>
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.
# Please submit bugfixes or comments via http://bugs.opensuse.org/
#
Name: libgit2
Version: 0.16.0
Release: 1
Summary: C git library
License: GPL-2.0 with linking
Group: Development/Libraries/C and C++
Url: http://libgit2.github.com/
Source0: https://github.com/downloads/libgit2/libgit2/libgit2-0.16.0.tar.gz
BuildRequires: cmake
BuildRequires: pkgconfig
BuildRoot: %{_tmppath}/%{name}-%{version}-build
%if 0%{?fedora} || 0%{?rhel_version} || 0%{?centos_version}
BuildRequires: openssl-devel
%else
BuildRequires: libopenssl-devel
%endif
%description
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.
%package -n %{name}-0
Summary: C git library
Group: System/Libraries
%description -n %{name}-0
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.
%package devel
Summary: C git library
Group: Development/Libraries/C and C++
Requires: %{name}-0 >= %{version}
%description devel
This package contains all necessary include files and libraries needed
to compile and develop applications that use libgit2.
%prep
%setup -q
%build
cmake . \
-DCMAKE_C_FLAGS:STRING="%{optflags}" \
-DCMAKE_INSTALL_PREFIX:PATH=%{_prefix} \
-DLIB_INSTALL_DIR:PATH=%{_libdir}S
make %{?_smp_mflags}
%install
%make_install
%post -n %{name}-0 -p /sbin/ldconfig
%postun -n %{name}-0 -p /sbin/ldconfig
%files -n %{name}-0
%defattr (-,root,root)
%doc AUTHORS COPYING README.md
%{_libdir}/%{name}.so.*
%files devel
%defattr (-,root,root)
%doc CONVENTIONS examples
%{_libdir}/%{name}.so
%{_includedir}/git2*
%{_libdir}/pkgconfig/libgit2.pc
%changelog
* Tue Mar 04 2012 tuxdna@gmail.com
- Update to version 0.16.0
* Tue Jan 31 2012 jengelh@medozas.de
- Provide pkgconfig symbols
* Thu Oct 27 2011 saschpe@suse.de
- Change license to 'GPL-2.0 with linking', fixes bnc#726789
* Wed Oct 26 2011 saschpe@suse.de
- Update to version 0.15.0:
* Upstream doesn't provide changes
- Removed outdated %%clean section
* Tue Jan 18 2011 saschpe@gmx.de
- Proper Requires for devel package
* Tue Jan 18 2011 saschpe@gmx.de
- Set BuildRequires to "openssl-devel" also for RHEL and CentOS
* Tue Jan 18 2011 saschpe@gmx.de
- Initial commit (0.0.1)
- Added patch to fix shared library soname
#include "common.h"
#include "repository.h"
#include "fileops.h"
#include "config.h"
......
......@@ -391,11 +391,11 @@ int git_clone(
const char *local_path,
const git_clone_options *_options)
{
int retcode = GIT_ERROR;
int error = 0;
git_repository *repo = NULL;
git_remote *origin;
git_clone_options options = GIT_CLONE_OPTIONS_INIT;
int remove_directory_on_failure = 0;
uint32_t rmdir_flags = GIT_RMDIR_REMOVE_FILES;
assert(out && url && local_path);
......@@ -408,33 +408,29 @@ int git_clone(
if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
giterr_set(GITERR_INVALID,
"'%s' exists and is not an empty directory", local_path);
return GIT_ERROR;
return GIT_EEXISTS;
}
/* Only remove the directory on failure if we create it */
remove_directory_on_failure = !git_path_exists(local_path);
/* Only remove the root directory on failure if we create it */
if (git_path_exists(local_path))
rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
if ((retcode = git_repository_init(&repo, local_path, options.bare)) < 0)
return retcode;
if ((error = git_repository_init(&repo, local_path, options.bare)) < 0)
return error;
if ((retcode = create_and_configure_origin(&origin, repo, url, &options)) < 0)
goto cleanup;
if (!(error = create_and_configure_origin(&origin, repo, url, &options))) {
error = git_clone_into(
repo, origin, &options.checkout_opts, options.checkout_branch);
retcode = git_clone_into(repo, origin, &options.checkout_opts, options.checkout_branch);
git_remote_free(origin);
git_remote_free(origin);
}
if (retcode < 0)
goto cleanup;
if (error < 0) {
git_repository_free(repo);
repo = NULL;
(void)git_futils_rmdir_r(local_path, NULL, rmdir_flags);
}
*out = repo;
return 0;
cleanup:
git_repository_free(repo);
if (remove_directory_on_failure)
git_futils_rmdir_r(local_path, NULL, GIT_RMDIR_REMOVE_FILES);
else
git_futils_cleanupdir_r(local_path);
return retcode;
return error;
}
......@@ -47,7 +47,7 @@ extern int git_config_rename_section(
* @param out the new backend
* @param path where the config file is located
*/
extern int git_config_file__ondisk(struct git_config_backend **out, const char *path);
extern int git_config_file__ondisk(git_config_backend **out, const char *path);
extern int git_config__normalize_name(const char *in, char **out);
......
......@@ -67,6 +67,7 @@ static struct map_data _cvar_maps[] = {
{"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT },
{"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT },
{"core.abbrev", _cvar_map_int, 1, GIT_ABBREV_DEFAULT },
{"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT },
};
int git_repository__cvar(int *out, git_repository *repo, git_cvar_cached cvar)
......
......@@ -52,7 +52,7 @@ enum {
#define GIT_DIFF__VERBOSE (1 << 30)
struct git_diff_list {
struct git_diff {
git_refcount rc;
git_repository *repo;
git_diff_options opts;
......@@ -72,7 +72,7 @@ struct git_diff_list {
extern void git_diff__cleanup_modes(
uint32_t diffcaps, uint32_t *omode, uint32_t *nmode);
extern void git_diff_list_addref(git_diff_list *diff);
extern void git_diff_addref(git_diff *diff);
extern int git_diff_delta__cmp(const void *a, const void *b);
extern int git_diff_delta__casecmp(const void *a, const void *b);
......@@ -93,15 +93,15 @@ extern int git_diff__oid_for_file(
git_repository *, const char *, uint16_t, git_off_t, git_oid *);
extern int git_diff__from_iterators(
git_diff_list **diff_ptr,
git_diff **diff_ptr,
git_repository *repo,
git_iterator *old_iter,
git_iterator *new_iter,
const git_diff_options *opts);
extern int git_diff__paired_foreach(
git_diff_list *idx2head,
git_diff_list *wd2idx,
git_diff *idx2head,
git_diff *wd2idx,
int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
void *payload);
......
......@@ -88,7 +88,7 @@ static int diff_file_content_init_common(
int git_diff_file_content__init_from_diff(
git_diff_file_content *fc,
git_diff_list *diff,
git_diff *diff,
size_t delta_index,
bool use_old)
{
......@@ -110,7 +110,7 @@ int git_diff_file_content__init_from_diff(
has_data = use_old; break;
case GIT_DELTA_UNTRACKED:
has_data = !use_old &&
(diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) != 0;
(diff->opts.flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) != 0;
break;
case GIT_DELTA_MODIFIED:
case GIT_DELTA_COPIED:
......
......@@ -27,7 +27,7 @@ typedef struct {
extern int git_diff_file_content__init_from_diff(
git_diff_file_content *fc,
git_diff_list *diff,
git_diff *diff,
size_t delta_index,
bool use_old);
......
......@@ -11,19 +11,20 @@
#include "diff.h"
#include "diff_file.h"
#include "array.h"
#include "git2/patch.h"
extern git_diff_list *git_diff_patch__diff(git_diff_patch *);
extern git_diff *git_patch__diff(git_patch *);
extern git_diff_driver *git_diff_patch__driver(git_diff_patch *);
extern git_diff_driver *git_patch__driver(git_patch *);
extern void git_diff_patch__old_data(char **, size_t *, git_diff_patch *);
extern void git_diff_patch__new_data(char **, size_t *, git_diff_patch *);
extern void git_patch__old_data(char **, size_t *, git_patch *);
extern void git_patch__new_data(char **, size_t *, git_patch *);
extern int git_diff_patch__invoke_callbacks(
git_diff_patch *patch,
extern int git_patch__invoke_callbacks(
git_patch *patch,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb line_cb,
git_diff_line_cb line_cb,
void *payload);
typedef struct git_diff_output git_diff_output;
......@@ -31,7 +32,7 @@ struct git_diff_output {
/* these callbacks are issued with the diff data */
git_diff_file_cb file_cb;
git_diff_hunk_cb hunk_cb;
git_diff_data_cb data_cb;
git_diff_line_cb data_cb;
void *payload;
/* this records the actual error in cases where it may be obscured */
......@@ -40,7 +41,7 @@ struct git_diff_output {
/* this callback is used to do the diff and drive the other callbacks.
* see diff_xdiff.h for how to use this in practice for now.
*/
int (*diff_cb)(git_diff_output *output, git_diff_patch *patch);
int (*diff_cb)(git_diff_output *output, git_patch *patch);
};
#endif
......@@ -97,8 +97,8 @@ static git_diff_delta *diff_delta__merge_like_cgit(
}
int git_diff_merge(
git_diff_list *onto,
const git_diff_list *from)
git_diff *onto,
const git_diff *from)
{
int error = 0;
git_pool onto_pool;
......@@ -117,15 +117,15 @@ int git_diff_merge(
git_pool_init(&onto_pool, 1, 0) < 0)
return -1;
if ((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 ||
(from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0)
if ((onto->opts.flags & GIT_DIFF_IGNORE_CASE) != 0 ||
(from->opts.flags & GIT_DIFF_IGNORE_CASE) != 0)
{
ignore_case = true;
/* This function currently only supports merging diff lists that
* are sorted identically. */
assert((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 &&
(from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0);
assert((onto->opts.flags & GIT_DIFF_IGNORE_CASE) != 0 &&
(from->opts.flags & GIT_DIFF_IGNORE_CASE) != 0);
}
for (i = 0, j = 0; i < onto->deltas.length || j < from->deltas.length; ) {
......@@ -230,9 +230,9 @@ int git_diff_find_similar__calc_similarity(
#define DEFAULT_RENAME_LIMIT 200
static int normalize_find_opts(
git_diff_list *diff,
git_diff *diff,
git_diff_find_options *opts,
git_diff_find_options *given)
const git_diff_find_options *given)
{
git_config *cfg = NULL;
......@@ -328,7 +328,7 @@ static int normalize_find_opts(
}
static int apply_splits_and_deletes(
git_diff_list *diff, size_t expected_size, bool actually_split)
git_diff *diff, size_t expected_size, bool actually_split)
{
git_vector onto = GIT_VECTOR_INIT;
size_t i;
......@@ -350,6 +350,7 @@ static int apply_splits_and_deletes(
goto on_error;
deleted->status = GIT_DELTA_DELETED;
deleted->nfiles = 1;
memset(&deleted->new_file, 0, sizeof(deleted->new_file));
deleted->new_file.path = deleted->old_file.path;
deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
......@@ -361,6 +362,7 @@ static int apply_splits_and_deletes(
delta->status = GIT_DELTA_UNTRACKED;
else
delta->status = GIT_DELTA_ADDED;
delta->nfiles = 1;
memset(&delta->old_file, 0, sizeof(delta->old_file));
delta->old_file.path = delta->new_file.path;
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
......@@ -402,7 +404,7 @@ on_error:
return -1;
}
GIT_INLINE(git_diff_file *) similarity_get_file(git_diff_list *diff, size_t idx)
GIT_INLINE(git_diff_file *) similarity_get_file(git_diff *diff, size_t idx)
{
git_diff_delta *delta = git_vector_get(&diff->deltas, idx / 2);
return (idx & 1) ? &delta->new_file : &delta->old_file;
......@@ -419,7 +421,7 @@ typedef struct {
} similarity_info;
static int similarity_init(
similarity_info *info, git_diff_list *diff, size_t file_idx)
similarity_info *info, git_diff *diff, size_t file_idx)
{
info->idx = file_idx;
info->src = (file_idx & 1) ? diff->new_src : diff->old_src;
......@@ -509,7 +511,7 @@ static void similarity_unload(similarity_info *info)
*/
static int similarity_measure(
int *score,
git_diff_list *diff,
git_diff *diff,
const git_diff_find_options *opts,
void **cache,
size_t a_idx,
......@@ -595,7 +597,7 @@ cleanup:
}
static int calc_self_similarity(
git_diff_list *diff,
git_diff *diff,
const git_diff_find_options *opts,
size_t delta_idx,
void **cache)
......@@ -612,7 +614,7 @@ static int calc_self_similarity(
return error;
if (similarity >= 0) {
delta->similarity = (uint32_t)similarity;
delta->similarity = (uint16_t)similarity;
delta->flags |= GIT_DIFF_FLAG__HAS_SELF_SIMILARITY;
}
......@@ -620,7 +622,7 @@ static int calc_self_similarity(
}
static bool is_rename_target(
git_diff_list *diff,
git_diff *diff,
const git_diff_find_options *opts,
size_t delta_idx,
void **cache)
......@@ -675,7 +677,7 @@ static bool is_rename_target(
}
static bool is_rename_source(
git_diff_list *diff,
git_diff *diff,
const git_diff_find_options *opts,
size_t delta_idx,
void **cache)
......@@ -745,25 +747,27 @@ GIT_INLINE(bool) delta_is_new_only(git_diff_delta *delta)
}
GIT_INLINE(void) delta_make_rename(
git_diff_delta *to, const git_diff_delta *from, uint32_t similarity)
git_diff_delta *to, const git_diff_delta *from, uint16_t similarity)
{
to->status = GIT_DELTA_RENAMED;
to->similarity = similarity;
to->nfiles = 2;
memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
}
typedef struct {
uint32_t idx;
uint32_t similarity;
size_t idx;
uint16_t similarity;
} diff_find_match;
int git_diff_find_similar(
git_diff_list *diff,
git_diff_find_options *given_opts)
git_diff *diff,
const git_diff_find_options *given_opts)
{
size_t s, t;
int error = 0, similarity;
int error = 0, result;
uint16_t similarity;
git_diff_delta *src, *tgt;
git_diff_find_options opts;
size_t num_deltas, num_srcs = 0, num_tgts = 0;
......@@ -839,17 +843,18 @@ find_best_matches:
/* calculate similarity for this pair and find best match */
if (s == t)
similarity = -1; /* don't measure self-similarity here */
result = -1; /* don't measure self-similarity here */
else if ((error = similarity_measure(
&similarity, diff, &opts, sigcache, 2 * s, 2 * t + 1)) < 0)
&result, diff, &opts, sigcache, 2 * s, 2 * t + 1)) < 0)
goto cleanup;
if (similarity < 0)
if (result < 0)
continue;
similarity = (uint16_t)result;
/* is this a better rename? */
if (tgt2src[t].similarity < (uint32_t)similarity &&
src2tgt[s].similarity < (uint32_t)similarity)
if (tgt2src[t].similarity < similarity &&
src2tgt[s].similarity < similarity)
{
/* eject old mapping */
if (src2tgt[s].similarity > 0) {
......@@ -862,18 +867,18 @@ find_best_matches:
}
/* write new mapping */
tgt2src[t].idx = (uint32_t)s;
tgt2src[t].similarity = (uint32_t)similarity;
src2tgt[s].idx = (uint32_t)t;
src2tgt[s].similarity = (uint32_t)similarity;
tgt2src[t].idx = s;
tgt2src[t].similarity = similarity;
src2tgt[s].idx = t;
src2tgt[s].similarity = similarity;
}
/* keep best absolute match for copies */
if (tgt2src_copy != NULL &&
tgt2src_copy[t].similarity < (uint32_t)similarity)
tgt2src_copy[t].similarity < similarity)
{
tgt2src_copy[t].idx = (uint32_t)s;
tgt2src_copy[t].similarity = (uint32_t)similarity;
tgt2src_copy[t].idx = s;
tgt2src_copy[t].similarity = similarity;
}
if (++tried_srcs >= num_srcs)
......@@ -943,7 +948,7 @@ find_best_matches:
delta_make_rename(tgt, src, best_match->similarity);
num_rewrites--;
src->status = GIT_DELTA_DELETED;
assert(src->status == GIT_DELTA_DELETED);
memcpy(&src->old_file, &swap, sizeof(src->old_file));
memset(&src->new_file, 0, sizeof(src->new_file));
src->new_file.path = src->old_file.path;
......@@ -953,7 +958,7 @@ find_best_matches:
if (src2tgt[t].similarity > 0 && src2tgt[t].idx > t) {
/* what used to be at src t is now at src s */
tgt2src[src2tgt[t].idx].idx = (uint32_t)s;
tgt2src[src2tgt[t].idx].idx = s;
}
}
}
......@@ -969,6 +974,7 @@ find_best_matches:
src->status = (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ?
GIT_DELTA_UNTRACKED : GIT_DELTA_ADDED;
src->nfiles = 1;
memset(&src->old_file, 0, sizeof(src->old_file));
src->old_file.path = src->new_file.path;
src->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
......@@ -1006,7 +1012,7 @@ find_best_matches:
/* otherwise, if we just overwrote a source, update mapping */
else if (src2tgt[t].similarity > 0 && src2tgt[t].idx > t) {
/* what used to be at src t is now at src s */
tgt2src[src2tgt[t].idx].idx = (uint32_t)s;
tgt2src[src2tgt[t].idx].idx = s;
}
num_updates++;
......@@ -1026,6 +1032,7 @@ find_best_matches:
tgt->status = GIT_DELTA_COPIED;
tgt->similarity = best_match->similarity;
tgt->nfiles = 2;
memcpy(&tgt->old_file, &src->old_file, sizeof(tgt->old_file));
num_updates++;
......
......@@ -24,26 +24,26 @@ static int git_xdiff_scan_int(const char **str, int *value)
return (digits > 0) ? 0 : -1;
}
static int git_xdiff_parse_hunk(git_diff_range *range, const char *header)
static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header)
{
/* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
if (*header != '@')
return -1;
if (git_xdiff_scan_int(&header, &range->old_start) < 0)
if (git_xdiff_scan_int(&header, &hunk->old_start) < 0)
return -1;
if (*header == ',') {
if (git_xdiff_scan_int(&header, &range->old_lines) < 0)
if (git_xdiff_scan_int(&header, &hunk->old_lines) < 0)
return -1;
} else
range->old_lines = 1;
if (git_xdiff_scan_int(&header, &range->new_start) < 0)
hunk->old_lines = 1;
if (git_xdiff_scan_int(&header, &hunk->new_start) < 0)
return -1;
if (*header == ',') {
if (git_xdiff_scan_int(&header, &range->new_lines) < 0)
if (git_xdiff_scan_int(&header, &hunk->new_lines) < 0)
return -1;
} else
range->new_lines = 1;
if (range->old_start < 0 || range->new_start < 0)
hunk->new_lines = 1;
if (hunk->old_start < 0 || hunk->new_start < 0)
return -1;
return 0;
......@@ -51,38 +51,96 @@ static int git_xdiff_parse_hunk(git_diff_range *range, const char *header)
typedef struct {
git_xdiff_output *xo;
git_diff_patch *patch;
git_diff_range range;
git_patch *patch;
git_diff_hunk hunk;
int old_lineno, new_lineno;
} git_xdiff_info;
static int diff_update_lines(
git_xdiff_info *info,
git_diff_line *line,
const char *content,
size_t content_len)
{
const char *scan = content, *scan_end = content + content_len;
for (line->num_lines = 0; scan < scan_end; ++scan)
if (*scan == '\n')
++line->num_lines;
line->content = content;
line->content_len = content_len;
/* expect " "/"-"/"+", then data */
switch (line->origin) {
case GIT_DIFF_LINE_ADDITION:
case GIT_DIFF_LINE_DEL_EOFNL:
line->old_lineno = -1;
line->new_lineno = info->new_lineno;
info->new_lineno += (int)line->num_lines;
break;
case GIT_DIFF_LINE_DELETION:
case GIT_DIFF_LINE_ADD_EOFNL:
line->old_lineno = info->old_lineno;
line->new_lineno = -1;
info->old_lineno += (int)line->num_lines;
break;
case GIT_DIFF_LINE_CONTEXT:
case GIT_DIFF_LINE_CONTEXT_EOFNL:
line->old_lineno = info->old_lineno;
line->new_lineno = info->new_lineno;
info->old_lineno += (int)line->num_lines;
info->new_lineno += (int)line->num_lines;
break;
default:
giterr_set(GITERR_INVALID, "Unknown diff line origin %02x",
(unsigned int)line->origin);
return -1;
}
return 0;
}
static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
{
git_xdiff_info *info = priv;
git_diff_patch *patch = info->patch;
const git_diff_delta *delta = git_diff_patch_delta(patch);
git_patch *patch = info->patch;
const git_diff_delta *delta = git_patch_get_delta(patch);
git_diff_output *output = &info->xo->output;
git_diff_line line;
if (len == 1) {
output->error = git_xdiff_parse_hunk(&info->range, bufs[0].ptr);
output->error = git_xdiff_parse_hunk(&info->hunk, bufs[0].ptr);
if (output->error < 0)
return output->error;
info->hunk.header_len = bufs[0].size;
if (info->hunk.header_len >= sizeof(info->hunk.header))
info->hunk.header_len = sizeof(info->hunk.header) - 1;
memcpy(info->hunk.header, bufs[0].ptr, info->hunk.header_len);
info->hunk.header[info->hunk.header_len] = '\0';
if (output->hunk_cb != NULL &&
output->hunk_cb(delta, &info->range,
bufs[0].ptr, bufs[0].size, output->payload))
output->hunk_cb(delta, &info->hunk, output->payload))
output->error = GIT_EUSER;
info->old_lineno = info->hunk.old_start;
info->new_lineno = info->hunk.new_start;
}
if (len == 2 || len == 3) {
/* expect " "/"-"/"+", then data */
char origin =
line.origin =
(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION :
(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION :
GIT_DIFF_LINE_CONTEXT;
if (output->data_cb != NULL &&
output->data_cb(delta, &info->range,
origin, bufs[1].ptr, bufs[1].size, output->payload))
output->error = diff_update_lines(
info, &line, bufs[1].ptr, bufs[1].size);
if (!output->error &&
output->data_cb != NULL &&
output->data_cb(delta, &info->hunk, &line, output->payload))
output->error = GIT_EUSER;
}
......@@ -92,21 +150,24 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
* If we have a '-' and a third buf, then we have removed a line
* with out a newline but added a blank line, so ADD_EOFNL.
*/
char origin =
line.origin =
(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL :
(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL :
GIT_DIFF_LINE_CONTEXT_EOFNL;
if (output->data_cb != NULL &&
output->data_cb(delta, &info->range,
origin, bufs[2].ptr, bufs[2].size, output->payload))
output->error = diff_update_lines(
info, &line, bufs[2].ptr, bufs[2].size);
if (!output->error &&
output->data_cb != NULL &&
output->data_cb(delta, &info->hunk, &line, output->payload))
output->error = GIT_EUSER;
}
return output->error;
}
static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
static int git_xdiff(git_diff_output *output, git_patch *patch)
{
git_xdiff_output *xo = (git_xdiff_output *)output;
git_xdiff_info info;
......@@ -120,7 +181,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
xo->callback.priv = &info;
git_diff_find_context_init(
&xo->config.find_func, &findctxt, git_diff_patch__driver(patch));
&xo->config.find_func, &findctxt, git_patch__driver(patch));
xo->config.find_func_priv = &findctxt;
if (xo->config.find_func != NULL)
......@@ -132,8 +193,8 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
* updates are needed to xo->params.flags
*/
git_diff_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch);
git_diff_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch);
git_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch);
git_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch);
xdl_diff(&xd_old_data, &xd_new_data,
&xo->params, &xo->config, &xo->callback);
......@@ -145,7 +206,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch)
void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
{
uint32_t flags = opts ? opts->flags : GIT_DIFF_NORMAL;
uint32_t flags = opts ? opts->flags : 0;
xo->output.diff_cb = git_xdiff;
......@@ -161,6 +222,11 @@ void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
if (flags & GIT_DIFF_IGNORE_WHITESPACE_EOL)
xo->params.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
if (flags & GIT_DIFF_PATIENCE)
xo->params.flags |= XDF_PATIENCE_DIFF;
if (flags & GIT_DIFF_MINIMAL)
xo->params.flags |= XDF_NEED_MINIMAL;
memset(&xo->callback, 0, sizeof(xo->callback));
xo->callback.outf = git_xdiff_cb;
}
......@@ -116,4 +116,3 @@ const git_error *giterr_last(void)
{
return GIT_GLOBAL->last_error;
}
......@@ -78,11 +78,8 @@ int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, con
int git_futils_open_ro(const char *path)
{
int fd = p_open(path, O_RDONLY);
if (fd < 0) {
if (errno == ENOENT || errno == ENOTDIR)
fd = GIT_ENOTFOUND;
giterr_set(GITERR_OS, "Failed to open '%s'", path);
}
if (fd < 0)
return git_path_set_error(errno, path, "open");
return fd;
}
......@@ -138,7 +135,6 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
int git_futils_readbuffer_updated(
git_buf *buf, const char *path, time_t *mtime, size_t *size, int *updated)
{
int error = 0;
git_file fd;
struct stat st;
bool changed = false;
......@@ -148,15 +144,16 @@ int git_futils_readbuffer_updated(
if (updated != NULL)
*updated = 0;
if (p_stat(path, &st) < 0) {
error = errno;
giterr_set(GITERR_OS, "Failed to stat '%s'", path);
if (error == ENOENT || error == ENOTDIR)
return GIT_ENOTFOUND;
return -1;
if (p_stat(path, &st) < 0)
return git_path_set_error(errno, path, "stat");
if (S_ISDIR(st.st_mode)) {
giterr_set(GITERR_INVALID, "requested file is a directory");
return GIT_ENOTFOUND;
}
if (S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) {
if (!git__is_sizet(st.st_size+1)) {
giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path);
return -1;
}
......@@ -398,8 +395,11 @@ typedef struct {
size_t baselen;
uint32_t flags;
int error;
int depth;
} futils__rmdir_data;
#define FUTILS_MAX_DEPTH 100
static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
{
if (filemsg)
......@@ -438,51 +438,60 @@ static int futils__rm_first_parent(git_buf *path, const char *ceiling)
static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
{
struct stat st;
futils__rmdir_data *data = opaque;
int error = data->error;
struct stat st;
if (data->depth > FUTILS_MAX_DEPTH)
error = futils__error_cannot_rmdir(
path->ptr, "directory nesting too deep");
if ((data->error = p_lstat_posixly(path->ptr, &st)) < 0) {
else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
if (errno == ENOENT)
data->error = 0;
error = 0;
else if (errno == ENOTDIR) {
/* asked to remove a/b/c/d/e and a/b is a normal file */
if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
data->error = futils__rm_first_parent(path, data->base);
error = futils__rm_first_parent(path, data->base);
else
futils__error_cannot_rmdir(
path->ptr, "parent is not directory");
}
else
futils__error_cannot_rmdir(path->ptr, "cannot access");
error = git_path_set_error(errno, path->ptr, "rmdir");
}
else if (S_ISDIR(st.st_mode)) {
int error = git_path_direach(path, futils__rmdir_recurs_foreach, data);
data->depth++;
error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
if (error < 0)
return (error == GIT_EUSER) ? data->error : error;
data->error = p_rmdir(path->ptr);
data->depth--;
if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
return data->error;
if (data->error < 0) {
if ((error = p_rmdir(path->ptr)) < 0) {
if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
(errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
data->error = 0;
error = 0;
else
futils__error_cannot_rmdir(path->ptr, NULL);
error = git_path_set_error(errno, path->ptr, "rmdir");
}
}
else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
data->error = p_unlink(path->ptr);
if (data->error < 0)
futils__error_cannot_rmdir(path->ptr, "cannot be removed");
if (p_unlink(path->ptr) < 0)
error = git_path_set_error(errno, path->ptr, "remove");
}
else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
data->error = futils__error_cannot_rmdir(path->ptr, "still present");
error = futils__error_cannot_rmdir(path->ptr, "still present");
return data->error;
data->error = error;
return error;
}
static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
......@@ -505,7 +514,7 @@ static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
giterr_clear();
error = GIT_ITEROVER;
} else {
futils__error_cannot_rmdir(git_buf_cstr(path), NULL);
error = git_path_set_error(errno, git_buf_cstr(path), "rmdir");
}
}
......@@ -517,7 +526,7 @@ int git_futils_rmdir_r(
{
int error;
git_buf fullpath = GIT_BUF_INIT;
futils__rmdir_data data;
futils__rmdir_data data = { 0 };
/* build path and find "root" where we should start calling mkdir */
if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
......@@ -526,7 +535,6 @@ int git_futils_rmdir_r(
data.base = base ? base : "";
data.baselen = base ? strlen(base) : 0;
data.flags = flags;
data.error = 0;
error = futils__rmdir_recurs_foreach(&data, &fullpath);
......@@ -544,41 +552,6 @@ int git_futils_rmdir_r(
return error;
}
int git_futils_cleanupdir_r(const char *path)
{
int error;
git_buf fullpath = GIT_BUF_INIT;
futils__rmdir_data data;
if ((error = git_buf_put(&fullpath, path, strlen(path))) < 0)
goto clean_up;
data.base = "";
data.baselen = 0;
data.flags = GIT_RMDIR_REMOVE_FILES;
data.error = 0;
if (!git_path_exists(path)) {
giterr_set(GITERR_OS, "Path does not exist: %s" , path);
error = GIT_ERROR;
goto clean_up;
}
if (!git_path_isdir(path)) {
giterr_set(GITERR_OS, "Path is not a directory: %s" , path);
error = GIT_ERROR;
goto clean_up;
}
error = git_path_direach(&fullpath, futils__rmdir_recurs_foreach, &data);
if (error == GIT_EUSER)
error = data.error;
clean_up:
git_buf_free(&fullpath);
return error;
}
static int git_futils_guess_system_dirs(git_buf *out)
{
......@@ -836,11 +809,8 @@ int git_futils_cp(const char *from, const char *to, mode_t filemode)
return ifd;
if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
if (errno == ENOENT || errno == ENOTDIR)
ofd = GIT_ENOTFOUND;
giterr_set(GITERR_OS, "Failed to open '%s' for writing", to);
p_close(ifd);
return ofd;
return git_path_set_error(errno, to, "open for writing");
}
return cp_by_fd(ifd, ofd, true);
......@@ -923,15 +893,14 @@ static int _cp_r_callback(void *ref, git_buf *from)
goto exit;
}
if (p_lstat(info->to.ptr, &to_st) < 0) {
if (errno != ENOENT && errno != ENOTDIR) {
giterr_set(GITERR_OS,
"Could not access %s while copying files", info->to.ptr);
error = -1;
goto exit;
}
} else
if (!(error = git_path_lstat(info->to.ptr, &to_st)))
exists = true;
else if (error != GIT_ENOTFOUND)
goto exit;
else {
giterr_clear();
error = 0;
}
if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
goto exit;
......@@ -948,9 +917,12 @@ static int _cp_r_callback(void *ref, git_buf *from)
error = _cp_r_mkdir(info, from);
/* recurse onto target directory */
if (!error && (!exists || S_ISDIR(to_st.st_mode)) &&
((error = git_path_direach(from, _cp_r_callback, info)) == GIT_EUSER))
error = info->error;
if (!error && (!exists || S_ISDIR(to_st.st_mode))) {
error = git_path_direach(from, 0, _cp_r_callback, info);
if (error == GIT_EUSER)
error = info->error;
}
if (oldmode != 0)
info->dirmode = oldmode;
......
......@@ -115,12 +115,7 @@ extern int git_futils_mkpath2file(const char *path, const mode_t mode);
* * GIT_RMDIR_EMPTY_PARENTS - remove containing directories up to base
* if removing this item leaves them empty
* * GIT_RMDIR_REMOVE_BLOCKERS - remove blocking file that causes ENOTDIR
*
* The old values translate into the new as follows:
*
* * GIT_DIRREMOVAL_EMPTY_HIERARCHY == GIT_RMDIR_EMPTY_HIERARCHY
* * GIT_DIRREMOVAL_FILES_AND_DIRS ~= GIT_RMDIR_REMOVE_FILES
* * GIT_DIRREMOVAL_ONLY_EMPTY_DIRS == GIT_RMDIR_SKIP_NONEMPTY
* * GIT_RMDIR_SKIP_ROOT - don't remove root directory itself
*/
typedef enum {
GIT_RMDIR_EMPTY_HIERARCHY = 0,
......@@ -128,6 +123,7 @@ typedef enum {
GIT_RMDIR_SKIP_NONEMPTY = (1 << 1),
GIT_RMDIR_EMPTY_PARENTS = (1 << 2),
GIT_RMDIR_REMOVE_BLOCKERS = (1 << 3),
GIT_RMDIR_SKIP_ROOT = (1 << 4),
} git_futils_rmdir_flags;
/**
......@@ -141,14 +137,6 @@ typedef enum {
extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags);
/**
* Remove all files and directories beneath the specified path.
*
* @param path Path to the top level directory to process.
* @return 0 on success; -1 on error.
*/
extern int git_futils_cleanupdir_r(const char *path);
/**
* Create and open a temporary file with a `_git2_` suffix.
* Writes the filename into path_out.
* @return On success, an open file descriptor, else an error code < 0.
......
......@@ -73,47 +73,69 @@ static void git__shutdown(void)
#if defined(GIT_THREADS) && defined(GIT_WIN32)
static DWORD _tls_index;
static int _tls_init = 0;
static DWORD _mutex = 0;
static DWORD _n_inits = 0;
int git_threads_init(void)
static int synchronized_threads_init()
{
int error;
if (_tls_init)
return 0;
_tls_index = TlsAlloc();
if (git_mutex_init(&git__mwindow_mutex))
return -1;
/* Initialize any other subsystems that have global state */
if ((error = git_hash_global_init()) >= 0 &&
(error = git_futils_dirs_global_init()) >= 0)
_tls_init = 1;
GIT_MEMORY_BARRIER;
if ((error = git_hash_global_init()) >= 0)
error = git_futils_dirs_global_init();
win32_pthread_initialize();
return error;
}
void git_threads_shutdown(void)
int git_threads_init(void)
{
int error = 0;
/* Enter the lock */
while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
/* Only do work on a 0 -> 1 transition of the refcount */
if (1 == ++_n_inits)
error = synchronized_threads_init();
/* Exit the lock */
InterlockedExchange(&_mutex, 0);
return error;
}
static void synchronized_threads_shutdown()
{
/* Shut down any subsystems that have global state */
git__shutdown();
TlsFree(_tls_index);
_tls_init = 0;
git_mutex_free(&git__mwindow_mutex);
}
void git_threads_shutdown(void)
{
/* Enter the lock */
while (InterlockedCompareExchange(&_mutex, 1, 0)) { Sleep(0); }
/* Only do work on a 1 -> 0 transition of the refcount */
if (0 == --_n_inits)
synchronized_threads_shutdown();
/* Exit the lock */
InterlockedExchange(&_mutex, 0);
}
git_global_st *git__global_state(void)
{
void *ptr;
assert(_tls_init);
assert(_n_inits);
if ((ptr = TlsGetValue(_tls_index)) != NULL)
return ptr;
......@@ -130,55 +152,58 @@ git_global_st *git__global_state(void)
#elif defined(GIT_THREADS) && defined(_POSIX_THREADS)
static pthread_key_t _tls_key;
static int _tls_init = 0;
static pthread_once_t _once_init = PTHREAD_ONCE_INIT;
static git_atomic git__n_inits;
int init_error = 0;
static void cb__free_status(void *st)
{
git__free(st);
}
int git_threads_init(void)
static void init_once(void)
{
int error = 0;
if (_tls_init)
return 0;
if (git_mutex_init(&git__mwindow_mutex))
return -1;
if ((init_error = git_mutex_init(&git__mwindow_mutex)) != 0)
return;
pthread_key_create(&_tls_key, &cb__free_status);
/* Initialize any other subsystems that have global state */
if ((error = git_hash_global_init()) >= 0 &&
(error = git_futils_dirs_global_init()) >= 0)
_tls_init = 1;
if ((init_error = git_hash_global_init()) >= 0)
init_error = git_futils_dirs_global_init();
GIT_MEMORY_BARRIER;
}
return error;
int git_threads_init(void)
{
pthread_once(&_once_init, init_once);
git_atomic_inc(&git__n_inits);
return init_error;
}
void git_threads_shutdown(void)
{
pthread_once_t new_once = PTHREAD_ONCE_INIT;
if (git_atomic_dec(&git__n_inits) > 0) return;
/* Shut down any subsystems that have global state */
git__shutdown();
if (_tls_init) {
void *ptr = pthread_getspecific(_tls_key);
pthread_setspecific(_tls_key, NULL);
git__free(ptr);
}
void *ptr = pthread_getspecific(_tls_key);
pthread_setspecific(_tls_key, NULL);
git__free(ptr);
pthread_key_delete(_tls_key);
_tls_init = 0;
git_mutex_free(&git__mwindow_mutex);
_once_init = new_once;
}
git_global_st *git__global_state(void)
{
void *ptr;
assert(_tls_init);
assert(git__n_inits.val);
if ((ptr = pthread_getspecific(_tls_key)) != NULL)
return ptr;
......
#include "git2/ignore.h"
#include "common.h"
#include "ignore.h"
#include "attr.h"
#include "path.h"
......
......@@ -461,9 +461,10 @@ int git_index_read(git_index *index)
return create_index_error(-1,
"Failed to read index: The index is in-memory only");
if (!index->on_disk || git_path_exists(index->index_file_path) == false) {
index->on_disk = git_path_exists(index->index_file_path);
if (!index->on_disk) {
git_index_clear(index);
index->on_disk = 0;
return 0;
}
......@@ -579,7 +580,8 @@ const git_index_entry *git_index_get_bypath(
return git_index_get_byindex(index, pos);
}
void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st)
void git_index_entry__init_from_stat(
git_index_entry *entry, struct stat *st, bool trust_mode)
{
entry->ctime.seconds = (git_time_t)st->st_ctime;
entry->mtime.seconds = (git_time_t)st->st_mtime;
......@@ -587,7 +589,8 @@ void git_index_entry__init_from_stat(git_index_entry *entry, struct stat *st)
/* entry->ctime.nanoseconds = st->st_ctimensec; */
entry->dev = st->st_rdev;
entry->ino = st->st_ino;
entry->mode = index_create_mode(st->st_mode);
entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
index_create_mode(0666) : index_create_mode(st->st_mode);
entry->uid = st->st_uid;
entry->gid = st->st_gid;
entry->file_size = st->st_size;
......@@ -631,7 +634,7 @@ static int index_entry_init(
entry = git__calloc(1, sizeof(git_index_entry));
GITERR_CHECK_ALLOC(entry);
git_index_entry__init_from_stat(entry, &st);
git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
entry->oid = oid;
entry->path = git__strdup(rel_path);
......
......@@ -48,7 +48,7 @@ struct git_index_conflict_iterator {
};
extern void git_index_entry__init_from_stat(
git_index_entry *entry, struct stat *st);
git_index_entry *entry, struct stat *st, bool trust_mode);
extern size_t git_index__prefix_position(git_index *index, const char *path);
......
......@@ -893,6 +893,7 @@ struct fs_iterator {
git_index_entry entry;
git_buf path;
size_t root_len;
uint32_t dirload_flags;
int depth;
int (*enter_dir_cb)(fs_iterator *self);
......@@ -986,7 +987,7 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
GITERR_CHECK_ALLOC(ff);
error = git_path_dirload_with_stat(
fi->path.ptr, fi->root_len, iterator__ignore_case(fi),
fi->path.ptr, fi->root_len, fi->dirload_flags,
fi->base.start, fi->base.end, &ff->entries);
if (error < 0) {
......@@ -1174,7 +1175,7 @@ static int fs_iterator__update_entry(fs_iterator *fi)
return GIT_ITEROVER;
fi->entry.path = ps->path;
git_index_entry__init_from_stat(&fi->entry, &ps->st);
git_index_entry__init_from_stat(&fi->entry, &ps->st, true);
/* need different mode here to keep directories during iteration */
fi->entry.mode = git_futils_canonical_mode(ps->st.st_mode);
......@@ -1207,6 +1208,11 @@ static int fs_iterator__initialize(
}
fi->root_len = fi->path.size;
fi->dirload_flags =
(iterator__ignore_case(fi) ? GIT_PATH_DIR_IGNORE_CASE : 0) |
(iterator__flag(fi, PRECOMPOSE_UNICODE) ?
GIT_PATH_DIR_PRECOMPOSE_UNICODE : 0);
if ((error = fs_iterator__expand_dir(fi)) < 0) {
if (error == GIT_ENOTFOUND || error == GIT_ITEROVER) {
giterr_clear();
......@@ -1329,7 +1335,7 @@ int git_iterator_for_workdir_ext(
const char *start,
const char *end)
{
int error;
int error, precompose = 0;
workdir_iterator *wi;
if (!repo_workdir) {
......@@ -1356,6 +1362,12 @@ int git_iterator_for_workdir_ext(
return error;
}
/* try to look up precompose and set flag if appropriate */
if (git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) < 0)
giterr_clear();
else if (precompose)
wi->fi.base.flags |= GIT_ITERATOR_PRECOMPOSE_UNICODE;
return fs_iterator__initialize(out, &wi->fi, repo_workdir);
}
......
......@@ -24,13 +24,15 @@ typedef enum {
typedef enum {
/** ignore case for entry sort order */
GIT_ITERATOR_IGNORE_CASE = (1 << 0),
GIT_ITERATOR_IGNORE_CASE = (1u << 0),
/** force case sensitivity for entry sort order */
GIT_ITERATOR_DONT_IGNORE_CASE = (1 << 1),
GIT_ITERATOR_DONT_IGNORE_CASE = (1u << 1),
/** return tree items in addition to blob items */
GIT_ITERATOR_INCLUDE_TREES = (1 << 2),
GIT_ITERATOR_INCLUDE_TREES = (1u << 2),
/** don't flatten trees, requiring advance_into (implies INCLUDE_TREES) */
GIT_ITERATOR_DONT_AUTOEXPAND = (1 << 3),
GIT_ITERATOR_DONT_AUTOEXPAND = (1u << 3),
/** convert precomposed unicode to decomposed unicode */
GIT_ITERATOR_PRECOMPOSE_UNICODE = (1u << 4),
} git_iterator_flag_t;
typedef struct {
......
......@@ -47,7 +47,7 @@ GIT_INLINE(int) merge_file_best_mode(
* assume executable. Otherwise, if any mode changed from the ancestor,
* use that one.
*/
if (GIT_MERGE_FILE_SIDE_EXISTS(ancestor)) {
if (!GIT_MERGE_FILE_SIDE_EXISTS(ancestor)) {
if (ours->mode == GIT_FILEMODE_BLOB_EXECUTABLE ||
theirs->mode == GIT_FILEMODE_BLOB_EXECUTABLE)
return GIT_FILEMODE_BLOB_EXECUTABLE;
......
......@@ -124,6 +124,13 @@ git_otype git_odb_object_type(git_odb_object *object)
return object->cached.type;
}
int git_odb_object_dup(git_odb_object **dest, git_odb_object *source)
{
git_cached_obj_incref(source);
*dest = source;
return 0;
}
void git_odb_object_free(git_odb_object *object)
{
if (object == NULL)
......@@ -988,7 +995,7 @@ int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_transfer
if (b->writepack != NULL) {
++writes;
error = b->writepack(out, b, progress_cb, progress_payload);
error = b->writepack(out, b, db, progress_cb, progress_payload);
}
}
......
......@@ -544,7 +544,7 @@ static int locate_object_short_oid(
/* Explore directory to find a unique object matching short_oid */
error = git_path_direach(
object_location, fn_locate_object_short_oid, &state);
object_location, 0, fn_locate_object_short_oid, &state);
if (error && error != GIT_EUSER)
return error;
......@@ -745,7 +745,7 @@ static int foreach_cb(void *_state, git_buf *path)
{
struct foreach_state *state = (struct foreach_state *) _state;
return git_path_direach(path, foreach_object_dir_cb, state);
return git_path_direach(path, 0, foreach_object_dir_cb, state);
}
static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data)
......@@ -768,7 +768,7 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb
state.data = data;
state.dir_len = git_buf_len(&buf);
error = git_path_direach(&buf, foreach_cb, &state);
error = git_path_direach(&buf, 0, foreach_cb, &state);
git_buf_free(&buf);
......
......@@ -331,7 +331,7 @@ static int pack_backend__refresh(git_odb_backend *_backend)
git_buf_sets(&path, backend->pack_folder);
/* reload all packs */
error = git_path_direach(&path, packfile_load__cb, (void *)backend);
error = git_path_direach(&path, 0, packfile_load__cb, backend);
git_buf_free(&path);
......@@ -541,6 +541,7 @@ static void pack_backend__writepack_free(struct git_odb_writepack *_writepack)
static int pack_backend__writepack(struct git_odb_writepack **out,
git_odb_backend *_backend,
git_odb *odb,
git_transfer_progress_callback progress_cb,
void *progress_payload)
{
......@@ -557,7 +558,7 @@ static int pack_backend__writepack(struct git_odb_writepack **out,
GITERR_CHECK_ALLOC(writepack);
if (git_indexer_stream_new(&writepack->indexer_stream,
backend->pack_folder, progress_cb, progress_payload) < 0) {
backend->pack_folder, odb, progress_cb, progress_payload) < 0) {
git__free(writepack);
return -1;
}
......
......@@ -216,52 +216,22 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
assert(ret != 0);
kh_value(pb->object_ix, pos) = po;
pb->done = false;
if (pb->progress_cb) {
double current_time = git__timer();
if ((current_time - pb->last_progress_report_time) >= MIN_PROGRESS_UPDATE_INTERVAL) {
pb->last_progress_report_time = current_time;
pb->progress_cb(GIT_PACKBUILDER_ADDING_OBJECTS, pb->nr_objects, 0, pb->progress_cb_payload);
if (pb->progress_cb(GIT_PACKBUILDER_ADDING_OBJECTS, pb->nr_objects, 0, pb->progress_cb_payload)) {
giterr_clear();
return GIT_EUSER;
}
}
}
pb->done = false;
return 0;
}
/*
* The per-object header is a pretty dense thing, which is
* - first byte: low four bits are "size",
* then three bits of "type",
* with the high bit being "size continues".
* - each byte afterwards: low seven bits are size continuation,
* with the high bit being "size continues"
*/
static int gen_pack_object_header(
unsigned char *hdr,
unsigned long size,
git_otype type)
{
unsigned char *hdr_base;
unsigned char c;
assert(type >= GIT_OBJ_COMMIT && type <= GIT_OBJ_REF_DELTA);
/* TODO: add support for chunked objects; see git.git 6c0d19b1 */
c = (unsigned char)((type << 4) | (size & 15));
size >>= 4;
hdr_base = hdr;
while (size) {
*hdr++ = c | 0x80;
c = size & 0x7f;
size >>= 7;
}
*hdr++ = c;
return (int)(hdr - hdr_base);
}
static int get_delta(void **out, git_odb *odb, git_pobject *po)
{
git_odb_object *src = NULL, *trg = NULL;
......@@ -323,7 +293,7 @@ static int write_object(git_buf *buf, git_packbuilder *pb, git_pobject *po)
}
/* Write header */
hdr_len = gen_pack_object_header(hdr, size, type);
hdr_len = git_packfile__object_header(hdr, size, type);
if (git_buf_put(buf, (char *)hdr, hdr_len) < 0)
goto on_error;
......@@ -591,49 +561,50 @@ static int write_pack(git_packbuilder *pb,
enum write_one_status status;
struct git_pack_header ph;
unsigned int i = 0;
int error = 0;
write_order = compute_write_order(pb);
if (write_order == NULL)
goto on_error;
if (write_order == NULL) {
error = -1;
goto done;
}
/* Write pack header */
ph.hdr_signature = htonl(PACK_SIGNATURE);
ph.hdr_version = htonl(PACK_VERSION);
ph.hdr_entries = htonl(pb->nr_objects);
if (cb(&ph, sizeof(ph), data) < 0)
goto on_error;
if ((error = cb(&ph, sizeof(ph), data)) < 0)
goto done;
if (git_hash_update(&pb->ctx, &ph, sizeof(ph)) < 0)
goto on_error;
if ((error = git_hash_update(&pb->ctx, &ph, sizeof(ph))) < 0)
goto done;
pb->nr_remaining = pb->nr_objects;
do {
pb->nr_written = 0;
for ( ; i < pb->nr_objects; ++i) {
po = write_order[i];
if (write_one(&buf, pb, po, &status) < 0)
goto on_error;
if (cb(buf.ptr, buf.size, data) < 0)
goto on_error;
if ((error = write_one(&buf, pb, po, &status)) < 0)
goto done;
if ((error = cb(buf.ptr, buf.size, data)) < 0)
goto done;
git_buf_clear(&buf);
}
pb->nr_remaining -= pb->nr_written;
} while (pb->nr_remaining && i < pb->nr_objects);
git__free(write_order);
git_buf_free(&buf);
if (git_hash_final(&pb->pack_oid, &pb->ctx) < 0)
goto on_error;
if ((error = git_hash_final(&pb->pack_oid, &pb->ctx)) < 0)
goto done;
return cb(pb->pack_oid.id, GIT_OID_RAWSZ, data);
error = cb(pb->pack_oid.id, GIT_OID_RAWSZ, data);
on_error:
done:
git__free(write_order);
git_buf_free(&buf);
return -1;
return error;
}
static int write_pack_buf(void *buf, size_t size, void *data)
......@@ -1287,7 +1258,7 @@ int git_packbuilder_write(
PREPARE_PACK;
if (git_indexer_stream_new(
&indexer, path, progress_cb, progress_cb_payload) < 0)
&indexer, path, pb->odb, progress_cb, progress_cb_payload) < 0)
return -1;
ctx.indexer = indexer;
......
......@@ -364,6 +364,38 @@ static unsigned char *pack_window_open(
return git_mwindow_open(&p->mwf, w_cursor, offset, 20, left);
}
/*
* The per-object header is a pretty dense thing, which is
* - first byte: low four bits are "size",
* then three bits of "type",
* with the high bit being "size continues".
* - each byte afterwards: low seven bits are size continuation,
* with the high bit being "size continues"
*/
int git_packfile__object_header(unsigned char *hdr, unsigned long size, git_otype type)
{
unsigned char *hdr_base;
unsigned char c;
assert(type >= GIT_OBJ_COMMIT && type <= GIT_OBJ_REF_DELTA);
/* TODO: add support for chunked objects; see git.git 6c0d19b1 */
c = (unsigned char)((type << 4) | (size & 15));
size >>= 4;
hdr_base = hdr;
while (size) {
*hdr++ = c | 0x80;
c = size & 0x7f;
size >>= 7;
}
*hdr++ = c;
return (int)(hdr - hdr_base);
}
static int packfile_unpack_header1(
unsigned long *usedp,
size_t *sizep,
......
......@@ -112,6 +112,8 @@ typedef struct git_packfile_stream {
git_mwindow *mw;
} git_packfile_stream;
int git_packfile__object_header(unsigned char *hdr, unsigned long size, git_otype type);
int git_packfile_unpack_header(
size_t *size_p,
git_otype *type_p,
......
......@@ -483,23 +483,23 @@ bool git_path_isfile(const char *path)
bool git_path_is_empty_dir(const char *path)
{
git_buf pathbuf = GIT_BUF_INIT;
HANDLE hFind = INVALID_HANDLE_VALUE;
git_win32_path wbuf;
int wbufsz;
WIN32_FIND_DATAW ffd;
bool retval = true;
if (!git_path_isdir(path)) return false;
if (!git_path_isdir(path))
return false;
git_buf_printf(&pathbuf, "%s\\*", path);
git_win32_path_from_c(wbuf, git_buf_cstr(&pathbuf));
wbufsz = git_win32_path_from_c(wbuf, path);
if (!wbufsz || wbufsz + 2 > GIT_WIN_PATH_UTF16)
return false;
memcpy(&wbuf[wbufsz - 1], L"\\*", 3 * sizeof(wchar_t));
hFind = FindFirstFileW(wbuf, &ffd);
if (INVALID_HANDLE_VALUE == hFind) {
giterr_set(GITERR_OS, "Couldn't open '%s'", path);
git_buf_free(&pathbuf);
if (INVALID_HANDLE_VALUE == hFind)
return false;
}
do {
if (!git_path_is_dot_or_dotdotW(ffd.cFileName)) {
......@@ -509,50 +509,64 @@ bool git_path_is_empty_dir(const char *path)
} while (FindNextFileW(hFind, &ffd) != 0);
FindClose(hFind);
git_buf_free(&pathbuf);
return retval;
}
#else
bool git_path_is_empty_dir(const char *path)
static int path_found_entry(void *payload, git_buf *path)
{
DIR *dir = NULL;
struct dirent *e;
bool retval = true;
GIT_UNUSED(payload);
return !git_path_is_dot_or_dotdot(path->ptr);
}
if (!git_path_isdir(path)) return false;
bool git_path_is_empty_dir(const char *path)
{
int error;
git_buf dir = GIT_BUF_INIT;
dir = opendir(path);
if (!dir) {
giterr_set(GITERR_OS, "Couldn't open '%s'", path);
if (!git_path_isdir(path))
return false;
}
while ((e = readdir(dir)) != NULL) {
if (!git_path_is_dot_or_dotdot(e->d_name)) {
giterr_set(GITERR_INVALID,
"'%s' exists and is not an empty directory", path);
retval = false;
break;
}
}
closedir(dir);
if (!(error = git_buf_sets(&dir, path)))
error = git_path_direach(&dir, 0, path_found_entry, NULL);
return retval;
git_buf_free(&dir);
return !error;
}
#endif
int git_path_lstat(const char *path, struct stat *st)
int git_path_set_error(int errno_value, const char *path, const char *action)
{
int err = 0;
if (p_lstat(path, st) < 0) {
err = (errno == ENOENT) ? GIT_ENOTFOUND : -1;
giterr_set(GITERR_OS, "Failed to stat file '%s'", path);
switch (errno_value) {
case ENOENT:
case ENOTDIR:
giterr_set(GITERR_OS, "Could not find '%s' to %s", path, action);
return GIT_ENOTFOUND;
case EINVAL:
case ENAMETOOLONG:
giterr_set(GITERR_OS, "Invalid path for filesystem '%s'", path);
return GIT_EINVALIDSPEC;
case EEXIST:
giterr_set(GITERR_OS, "Failed %s - '%s' already exists", action, path);
return GIT_EEXISTS;
default:
giterr_set(GITERR_OS, "Could not %s '%s'", action, path);
return -1;
}
}
int git_path_lstat(const char *path, struct stat *st)
{
if (p_lstat(path, st) == 0)
return 0;
return err;
return git_path_set_error(errno, path, "stat");
}
static bool _check_dir_contents(
......@@ -724,14 +738,103 @@ int git_path_cmp(
return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
}
bool git_path_has_non_ascii(const char *path, size_t pathlen)
{
const uint8_t *scan = (const uint8_t *)path, *end;
for (end = scan + pathlen; scan < end; ++scan)
if (*scan & 0x80)
return true;
return false;
}
#ifdef GIT_USE_ICONV
int git_path_iconv_init_precompose(git_path_iconv_t *ic)
{
git_buf_init(&ic->buf, 0);
ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING);
return 0;
}
void git_path_iconv_clear(git_path_iconv_t *ic)
{
if (ic) {
if (ic->map != (iconv_t)-1)
iconv_close(ic->map);
git_buf_free(&ic->buf);
}
}
int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen)
{
char *nfd = *in, *nfc;
size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, rv;
int retry = 1;
if (!ic || ic->map == (iconv_t)-1 ||
!git_path_has_non_ascii(*in, *inlen))
return 0;
while (1) {
if (git_buf_grow(&ic->buf, wantlen) < 0)
return -1;
nfc = ic->buf.ptr + ic->buf.size;
nfclen = ic->buf.asize - ic->buf.size;
rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen);
ic->buf.size = (nfc - ic->buf.ptr);
if (rv != (size_t)-1)
break;
if (errno != E2BIG)
goto fail;
/* make space for 2x the remaining data to be converted
* (with per retry overhead to avoid infinite loops)
*/
wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
if (retry++ > 4)
goto fail;
}
ic->buf.ptr[ic->buf.size] = '\0';
*in = ic->buf.ptr;
*inlen = ic->buf.size;
return 0;
fail:
giterr_set(GITERR_OS, "Unable to convert unicode path data");
return -1;
}
#endif
#if defined(__sun) || defined(__GNU__)
typedef char path_dirent_data[sizeof(struct dirent) + FILENAME_MAX + 1];
#else
typedef struct dirent path_dirent_data;
#endif
int git_path_direach(
git_buf *path,
uint32_t flags,
int (*fn)(void *, git_buf *),
void *arg)
{
int error = 0;
ssize_t wd_len;
DIR *dir;
struct dirent *de, *de_buf;
path_dirent_data de_data;
struct dirent *de, *de_buf = (struct dirent *)&de_data;
git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
if (git_path_to_dir(path) < 0)
return -1;
......@@ -743,99 +846,98 @@ int git_path_direach(
return -1;
}
#if defined(__sun) || defined(__GNU__)
de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
#else
de_buf = git__malloc(sizeof(struct dirent));
#endif
if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
(void)git_path_iconv_init_precompose(&ic);
while (p_readdir_r(dir, de_buf, &de) == 0 && de != NULL) {
int result;
char *de_path = de->d_name;
size_t de_len = strlen(de_path);
if (git_path_is_dot_or_dotdot(de->d_name))
if (git_path_is_dot_or_dotdot(de_path))
continue;
if (git_buf_puts(path, de->d_name) < 0) {
closedir(dir);
git__free(de_buf);
return -1;
}
if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0 ||
(error = git_buf_put(path, de_path, de_len)) < 0)
break;
result = fn(arg, path);
error = fn(arg, path);
git_buf_truncate(path, wd_len); /* restore path */
if (result) {
closedir(dir);
git__free(de_buf);
return GIT_EUSER;
if (error) {
error = GIT_EUSER;
break;
}
}
closedir(dir);
git__free(de_buf);
return 0;
git_path_iconv_clear(&ic);
return error;
}
int git_path_dirload(
const char *path,
size_t prefix_len,
size_t alloc_extra,
unsigned int flags,
git_vector *contents)
{
int error, need_slash;
DIR *dir;
struct dirent *de, *de_buf;
size_t path_len;
path_dirent_data de_data;
struct dirent *de, *de_buf = (struct dirent *)&de_data;
git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
assert(path && contents);
assert(path != NULL && contents != NULL);
path_len = strlen(path);
assert(path_len > 0 && path_len >= prefix_len);
if (!path_len || path_len < prefix_len) {
giterr_set(GITERR_INVALID, "Invalid directory path '%s'", path);
return -1;
}
if ((dir = opendir(path)) == NULL) {
giterr_set(GITERR_OS, "Failed to open directory '%s'", path);
return -1;
}
#if defined(__sun) || defined(__GNU__)
de_buf = git__malloc(sizeof(struct dirent) + FILENAME_MAX + 1);
#else
de_buf = git__malloc(sizeof(struct dirent));
#endif
if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
(void)git_path_iconv_init_precompose(&ic);
path += prefix_len;
path_len -= prefix_len;
need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0;
while ((error = p_readdir_r(dir, de_buf, &de)) == 0 && de != NULL) {
char *entry_path;
size_t entry_len;
char *entry_path, *de_path = de->d_name;
size_t alloc_size, de_len = strlen(de_path);
if (git_path_is_dot_or_dotdot(de->d_name))
if (git_path_is_dot_or_dotdot(de_path))
continue;
entry_len = strlen(de->d_name);
if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
break;
entry_path = git__malloc(
path_len + need_slash + entry_len + 1 + alloc_extra);
GITERR_CHECK_ALLOC(entry_path);
alloc_size = path_len + need_slash + de_len + 1 + alloc_extra;
if ((entry_path = git__calloc(alloc_size, 1)) == NULL) {
error = -1;
break;
}
if (path_len)
memcpy(entry_path, path, path_len);
if (need_slash)
entry_path[path_len] = '/';
memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len);
entry_path[path_len + need_slash + entry_len] = '\0';
memcpy(&entry_path[path_len + need_slash], de_path, de_len);
if (git_vector_insert(contents, entry_path) < 0) {
closedir(dir);
git__free(de_buf);
return -1;
}
if ((error = git_vector_insert(contents, entry_path)) < 0)
break;
}
closedir(dir);
git__free(de_buf);
git_path_iconv_clear(&ic);
if (error != 0)
giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path);
......@@ -858,7 +960,7 @@ int git_path_with_stat_cmp_icase(const void *a, const void *b)
int git_path_dirload_with_stat(
const char *path,
size_t prefix_len,
bool ignore_case,
unsigned int flags,
const char *start_stat,
const char *end_stat,
git_vector *contents)
......@@ -875,13 +977,14 @@ int git_path_dirload_with_stat(
return -1;
error = git_path_dirload(
path, prefix_len, sizeof(git_path_with_stat) + 1, contents);
path, prefix_len, sizeof(git_path_with_stat) + 1, flags, contents);
if (error < 0) {
git_buf_free(&full);
return error;
}
strncomp = ignore_case ? git__strncasecmp : git__strncmp;
strncomp = (flags & GIT_PATH_DIR_IGNORE_CASE) != 0 ?
git__strncasecmp : git__strncmp;
/* stat struct at start of git_path_with_stat, so shift path text */
git_vector_foreach(contents, i, ps) {
......
......@@ -242,21 +242,28 @@ extern int git_path_resolve_relative(git_buf *path, size_t ceiling);
*/
extern int git_path_apply_relative(git_buf *target, const char *relpath);
enum {
GIT_PATH_DIR_IGNORE_CASE = (1u << 0),
GIT_PATH_DIR_PRECOMPOSE_UNICODE = (1u << 1),
};
/**
* Walk each directory entry, except '.' and '..', calling fn(state).
*
* @param pathbuf buffer the function reads the initial directory
* @param pathbuf Buffer the function reads the initial directory
* path from, and updates with each successive entry's name.
* @param fn function to invoke with each entry. The first arg is
* the input state and the second arg is pathbuf. The function
* may modify the pathbuf, but only by appending new text.
* @param state to pass to fn as the first arg.
* @param flags Combination of GIT_PATH_DIR flags.
* @param callback Callback for each entry. Passed the `payload` and each
* successive path inside the directory as a full path. This may
* safely append text to the pathbuf if needed.
* @param payload Passed to callback as first argument.
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
extern int git_path_direach(
git_buf *pathbuf,
int (*fn)(void *, git_buf *),
void *state);
uint32_t flags,
int (*callback)(void *payload, git_buf *path),
void *payload);
/**
* Sort function to order two paths
......@@ -276,19 +283,19 @@ extern int git_path_cmp(
* @param pathbuf Buffer the function reads the directory from and
* and updates with each successive name.
* @param ceiling Prefix of path at which to stop walking up. If NULL,
* this will walk all the way up to the root. If not a prefix of
* pathbuf, the callback will be invoked a single time on the
* original input path.
* @param fn Function to invoke on each path. The first arg is the
* input satte and the second arg is the pathbuf. The function
* should not modify the pathbuf.
* this will walk all the way up to the root. If not a prefix of
* pathbuf, the callback will be invoked a single time on the
* original input path.
* @param callback Function to invoke on each path. Passed the `payload`
* and the buffer containing the current path. The path should not
* be modified in any way.
* @param state Passed to fn as the first ath.
*/
extern int git_path_walk_up(
git_buf *pathbuf,
const char *ceiling,
int (*fn)(void *state, git_buf *),
void *state);
int (*callback)(void *payload, git_buf *path),
void *payload);
/**
* Load all directory entries (except '.' and '..') into a vector.
......@@ -304,12 +311,14 @@ extern int git_path_walk_up(
* prefix_len 3, the entries will look like "b/e1", "b/e2", etc.
* @param alloc_extra Extra bytes to add to each string allocation in
* case you want to append anything funny.
* @param flags Combination of GIT_PATH_DIR flags.
* @param contents Vector to fill with directory entry names.
*/
extern int git_path_dirload(
const char *path,
size_t prefix_len,
size_t alloc_extra,
uint32_t flags,
git_vector *contents);
......@@ -336,7 +345,7 @@ extern int git_path_with_stat_cmp_icase(const void *a, const void *b);
*
* @param path The directory to read from
* @param prefix_len The trailing part of path to prefix to entry paths
* @param ignore_case How to sort and compare paths with start/end limits
* @param flags GIT_PATH_DIR flags from above
* @param start_stat As optimization, only stat values after this prefix
* @param end_stat As optimization, only stat values before this prefix
* @param contents Vector to fill with git_path_with_stat structures
......@@ -344,9 +353,88 @@ extern int git_path_with_stat_cmp_icase(const void *a, const void *b);
extern int git_path_dirload_with_stat(
const char *path,
size_t prefix_len,
bool ignore_case,
uint32_t flags,
const char *start_stat,
const char *end_stat,
git_vector *contents);
enum { GIT_PATH_NOTEQUAL = 0, GIT_PATH_EQUAL = 1, GIT_PATH_PREFIX = 2 };
/*
* Determines if a path is equal to or potentially a child of another.
* @param parent The possible parent
* @param child The possible child
*/
GIT_INLINE(int) git_path_equal_or_prefixed(
const char *parent,
const char *child)
{
const char *p = parent, *c = child;
while (*p && *c) {
if (*p++ != *c++)
return GIT_PATH_NOTEQUAL;
}
if (*p != '\0')
return GIT_PATH_NOTEQUAL;
if (*c == '\0')
return GIT_PATH_EQUAL;
if (*c == '/')
return GIT_PATH_PREFIX;
return GIT_PATH_NOTEQUAL;
}
/* translate errno to libgit2 error code and set error message */
extern int git_path_set_error(
int errno_value, const char *path, const char *action);
/* check if non-ascii characters are present in filename */
extern bool git_path_has_non_ascii(const char *path, size_t pathlen);
#define GIT_PATH_REPO_ENCODING "UTF-8"
#ifdef __APPLE__
#define GIT_PATH_NATIVE_ENCODING "UTF-8-MAC"
#else
#define GIT_PATH_NATIVE_ENCODING "UTF-8"
#endif
#ifdef GIT_USE_ICONV
#include <iconv.h>
typedef struct {
iconv_t map;
git_buf buf;
} git_path_iconv_t;
#define GIT_PATH_ICONV_INIT { (iconv_t)-1, GIT_BUF_INIT }
/* Init iconv data for converting decomposed UTF-8 to precomposed */
extern int git_path_iconv_init_precompose(git_path_iconv_t *ic);
/* Clear allocated iconv data */
extern void git_path_iconv_clear(git_path_iconv_t *ic);
/*
* Rewrite `in` buffer using iconv map if necessary, replacing `in`
* pointer internal iconv buffer if rewrite happened. The `in` pointer
* will be left unchanged if no rewrite was needed.
*/
extern int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen);
#else
typedef struct {
int unused;
} git_path_iconv_t;
#define GIT_PATH_ICONV_INIT { 0 }
#define git_path_iconv_init_precompose(X) 0
#define git_path_iconv_clear(X) (void)(X)
#define git_path_iconv(X,Y,Z) 0
#endif /* GIT_USE_ICONV */
#endif
......@@ -585,7 +585,7 @@ int git_pathspec_match_tree(
int git_pathspec_match_diff(
git_pathspec_match_list **out,
git_diff_list *diff,
git_diff *diff,
uint32_t flags,
git_pathspec *ps)
{
......
......@@ -582,7 +582,7 @@ static int calculate_work(git_push *push)
static int do_push(git_push *push)
{
int error;
int error = 0;
git_transport *transport = push->remote->transport;
if (!transport->push) {
......@@ -611,8 +611,6 @@ static int do_push(git_push *push)
(error = transport->push(transport, push)) < 0)
goto on_error;
error = 0;
on_error:
git_packbuilder_free(push->pb);
return error;
......
......@@ -16,6 +16,7 @@
#include "hash.h"
#include "refdb.h"
#include "refs.h"
#include "reflog.h"
int git_refdb_new(git_refdb **out, git_repository *repo)
{
......@@ -203,3 +204,18 @@ int git_refdb_delete(struct git_refdb *db, const char *ref_name)
assert(db && db->backend);
return db->backend->del(db->backend, ref_name);
}
int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name)
{
int error;
assert(db && db->backend);
if ((error = db->backend->reflog_read(out, db->backend, name)) < 0)
return error;
GIT_REFCOUNT_INC(db);
(*out)->db = db;
return 0;
}
......@@ -43,4 +43,8 @@ void git_refdb_iterator_free(git_reference_iterator *iter);
int git_refdb_write(git_refdb *refdb, git_reference *ref, int force);
int git_refdb_delete(git_refdb *refdb, const char *ref_name);
int git_refdb_reflog_read(git_reflog **out, git_refdb *db, const char *name);
int git_refdb_reflog_write(git_reflog *reflog);
#endif
......@@ -27,9 +27,14 @@ struct git_reflog_entry {
};
struct git_reflog {
git_refdb *db;
char *ref_name;
git_repository *owner;
git_vector entries;
};
GIT_INLINE(size_t) reflog_inverse_index(size_t idx, size_t total)
{
return (total - 1) - idx;
}
#endif /* INCLUDE_reflog_h__ */
......@@ -138,6 +138,22 @@ int git_reference_name_to_id(
return 0;
}
static int reference_normalize_for_repo(
char *out,
size_t out_size,
git_repository *repo,
const char *name)
{
int precompose;
unsigned int flags = GIT_REF_FORMAT_ALLOW_ONELEVEL;
if (!git_repository__cvar(&precompose, repo, GIT_CVAR_PRECOMPOSE) &&
precompose)
flags |= GIT_REF_FORMAT__PRECOMPOSE_UNICODE;
return git_reference_normalize_name(out, out_size, name, flags);
}
int git_reference_lookup_resolved(
git_reference **ref_out,
git_repository *repo,
......@@ -159,13 +175,13 @@ int git_reference_lookup_resolved(
else if (max_nesting < 0)
max_nesting = DEFAULT_NESTING_LEVEL;
strncpy(scan_name, name, GIT_REFNAME_MAX);
scan_type = GIT_REF_SYMBOLIC;
if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
return -1;
if ((error = reference_normalize_for_repo(
scan_name, sizeof(scan_name), repo, name)) < 0)
return error;
if ((error = git_reference__normalize_name_lax(scan_name, GIT_REFNAME_MAX, name)) < 0)
if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
return error;
for (nesting = max_nesting;
......@@ -173,7 +189,7 @@ int git_reference_lookup_resolved(
nesting--)
{
if (nesting != max_nesting) {
strncpy(scan_name, ref->target.symbolic, GIT_REFNAME_MAX);
strncpy(scan_name, ref->target.symbolic, sizeof(scan_name));
git_reference_free(ref);
}
......@@ -467,7 +483,7 @@ int git_reference_rename(
if (reference_has_log < 0)
return reference_has_log;
if (reference_has_log && (error = git_reflog_rename(ref, new_name)) < 0)
if (reference_has_log && (error = git_reflog_rename(git_reference_owner(ref), git_reference_name(ref), new_name)) < 0)
return error;
return 0;
......@@ -711,17 +727,18 @@ static bool is_all_caps_and_underscore(const char *name, size_t len)
return true;
}
/* Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100 */
int git_reference__normalize_name(
git_buf *buf,
const char *name,
unsigned int flags)
{
// Inspired from https://github.com/git/git/blob/f06d47e7e0d9db709ee204ed13a8a7486149f494/refs.c#L36-100
char *current;
int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC;
unsigned int process_flags;
bool normalize = (buf != NULL);
git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
assert(name);
process_flags = flags;
......@@ -733,6 +750,14 @@ int git_reference__normalize_name(
if (normalize)
git_buf_clear(buf);
if ((flags & GIT_REF_FORMAT__PRECOMPOSE_UNICODE) != 0) {
size_t namelen = strlen(current);
if ((error = git_path_iconv_init_precompose(&ic)) < 0 ||
(error = git_path_iconv(&ic, &current, &namelen)) < 0)
goto cleanup;
error = GIT_EINVALIDSPEC;
}
while (true) {
segment_len = ensure_segment_validity(current);
if (segment_len < 0) {
......@@ -809,6 +834,8 @@ cleanup:
if (error && normalize)
git_buf_free(buf);
git_path_iconv_clear(&ic);
return error;
}
......@@ -983,9 +1010,9 @@ static int peel_error(int error, git_reference *ref, const char* msg)
}
int git_reference_peel(
git_object **peeled,
git_reference *ref,
git_otype target_type)
git_object **peeled,
git_reference *ref,
git_otype target_type)
{
git_reference *resolved = NULL;
git_object *target = NULL;
......@@ -1027,24 +1054,19 @@ cleanup:
return error;
}
int git_reference__is_valid_name(
const char *refname,
unsigned int flags)
int git_reference__is_valid_name(const char *refname, unsigned int flags)
{
int error;
error = git_reference__normalize_name(NULL, refname, flags) == 0;
giterr_clear();
if (git_reference__normalize_name(NULL, refname, flags) < 0) {
giterr_clear();
return false;
}
return error;
return true;
}
int git_reference_is_valid_name(
const char *refname)
int git_reference_is_valid_name(const char *refname)
{
return git_reference__is_valid_name(
refname,
GIT_REF_FORMAT_ALLOW_ONELEVEL);
return git_reference__is_valid_name(refname, GIT_REF_FORMAT_ALLOW_ONELEVEL);
}
const char *git_reference_shorthand(git_reference *ref)
......
......@@ -46,6 +46,8 @@
#define GIT_STASH_FILE "stash"
#define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE
#define GIT_REF_FORMAT__PRECOMPOSE_UNICODE (1u << 16)
#define GIT_REFNAME_MAX 1024
struct git_reference {
......
......@@ -10,6 +10,7 @@
#include "git2/oid.h"
#include "git2/net.h"
#include "common.h"
#include "config.h"
#include "repository.h"
#include "remote.h"
......
......@@ -37,6 +37,7 @@ typedef enum {
GIT_CVAR_IGNORESTAT, /* core.ignorestat */
GIT_CVAR_TRUSTCTIME, /* core.trustctime */
GIT_CVAR_ABBREV, /* core.abbrev */
GIT_CVAR_PRECOMPOSE, /* core.precomposeunicode */
GIT_CVAR_CACHE_MAX
} git_cvar_cached;
......@@ -86,6 +87,8 @@ typedef enum {
GIT_TRUSTCTIME_DEFAULT = GIT_CVAR_TRUE,
/* core.abbrev */
GIT_ABBREV_DEFAULT = 7,
/* core.precomposeunicode */
GIT_PRECOMPOSE_DEFAULT = GIT_CVAR_FALSE,
} git_cvar_value;
......
......@@ -24,10 +24,9 @@ int git_reset_default(
{
git_object *commit = NULL;
git_tree *tree = NULL;
git_diff_list *diff = NULL;
git_diff *diff = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
size_t i;
git_diff_delta *delta;
size_t i, max_i;
git_index_entry entry;
int error;
git_index *index = NULL;
......@@ -58,7 +57,9 @@ int git_reset_default(
&diff, repo, tree, index, &opts)) < 0)
goto cleanup;
git_vector_foreach(&diff->deltas, i, delta) {
for (i = 0, max_i = git_diff_num_deltas(diff); i < max_i; ++i) {
const git_diff_delta *delta = git_diff_get_delta(diff, i);
if ((error = git_index_conflict_remove(index, delta->old_file.path)) < 0)
goto cleanup;
......@@ -85,7 +86,7 @@ cleanup:
git_object_free(commit);
git_tree_free(tree);
git_index_free(index);
git_diff_list_free(diff);
git_diff_free(diff);
return error;
}
......@@ -135,7 +136,7 @@ int git_reset(
if (reset_type == GIT_RESET_HARD) {
/* overwrite working directory with HEAD */
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_SKIP_UNMERGED;
if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0)
goto cleanup;
......
......@@ -160,7 +160,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out,
if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
goto cleanup;
if (git_reflog_read(&reflog, ref) < 0)
if (git_reflog_read(&reflog, repo, GIT_HEAD_FILE) < 0)
goto cleanup;
numentries = git_reflog_entrycount(reflog);
......@@ -208,7 +208,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t ide
const git_reflog_entry *entry;
bool search_by_pos = (identifier <= 100000000);
if (git_reflog_read(&reflog, ref) < 0)
if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0)
return -1;
numentries = git_reflog_entrycount(reflog);
......
......@@ -153,65 +153,61 @@ cleanup:
return error;
}
struct cb_data {
git_index *index;
int error;
struct stash_update_rules {
bool include_changed;
bool include_untracked;
bool include_ignored;
};
static int update_index_cb(
const git_diff_delta *delta,
float progress,
void *payload)
static int stash_update_index_from_diff(
git_index *index,
const git_diff *diff,
struct stash_update_rules *data)
{
struct cb_data *data = (struct cb_data *)payload;
const char *add_path = NULL;
GIT_UNUSED(progress);
switch (delta->status) {
case GIT_DELTA_IGNORED:
if (data->include_ignored)
add_path = delta->new_file.path;
break;
case GIT_DELTA_UNTRACKED:
if (data->include_untracked)
add_path = delta->new_file.path;
break;
case GIT_DELTA_ADDED:
case GIT_DELTA_MODIFIED:
if (data->include_changed)
add_path = delta->new_file.path;
break;
case GIT_DELTA_DELETED:
if (!data->include_changed)
int error = 0;
size_t d, max_d = git_diff_num_deltas(diff);
for (d = 0; !error && d < max_d; ++d) {
const char *add_path = NULL;
const git_diff_delta *delta = git_diff_get_delta(diff, d);
switch (delta->status) {
case GIT_DELTA_IGNORED:
if (data->include_ignored)
add_path = delta->new_file.path;
break;
if (git_index_find(NULL, data->index, delta->old_file.path) == 0)
data->error = git_index_remove(
data->index, delta->old_file.path, 0);
break;
default:
/* Unimplemented */
giterr_set(
GITERR_INVALID,
"Cannot update index. Unimplemented status (%d)",
delta->status);
data->error = -1;
break;
}
if (add_path != NULL)
data->error = git_index_add_bypath(data->index, add_path);
case GIT_DELTA_UNTRACKED:
if (data->include_untracked)
add_path = delta->new_file.path;
break;
case GIT_DELTA_ADDED:
case GIT_DELTA_MODIFIED:
if (data->include_changed)
add_path = delta->new_file.path;
break;
case GIT_DELTA_DELETED:
if (data->include_changed &&
!git_index_find(NULL, index, delta->old_file.path))
error = git_index_remove(index, delta->old_file.path, 0);
break;
default:
/* Unimplemented */
giterr_set(
GITERR_INVALID,
"Cannot update index. Unimplemented status (%d)",
delta->status);
return -1;
}
return data->error;
if (add_path != NULL)
error = git_index_add_bypath(index, add_path);
}
return error;
}
static int build_untracked_tree(
......@@ -221,15 +217,13 @@ static int build_untracked_tree(
uint32_t flags)
{
git_tree *i_tree = NULL;
git_diff_list *diff = NULL;
git_diff *diff = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
struct cb_data data = {0};
struct stash_update_rules data = {0};
int error;
git_index_clear(index);
data.index = index;
if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
GIT_DIFF_RECURSE_UNTRACKED_DIRS;
......@@ -248,18 +242,13 @@ static int build_untracked_tree(
&diff, git_index_owner(index), i_tree, &opts)) < 0)
goto cleanup;
if ((error = git_diff_foreach(
diff, update_index_cb, NULL, NULL, &data)) < 0)
{
if (error == GIT_EUSER)
error = data.error;
if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
goto cleanup;
}
error = build_tree_from_index(tree_out, index);
cleanup:
git_diff_list_free(diff);
git_diff_free(diff);
git_tree_free(i_tree);
return error;
}
......@@ -311,9 +300,9 @@ static int build_workdir_tree(
{
git_repository *repo = git_index_owner(index);
git_tree *b_tree = NULL;
git_diff_list *diff = NULL, *diff2 = NULL;
git_diff *diff = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
struct cb_data data = {0};
struct stash_update_rules data = {0};
int error;
opts.flags = GIT_DIFF_IGNORE_SUBMODULES;
......@@ -321,33 +310,19 @@ static int build_workdir_tree(
if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
goto cleanup;
if ((error = git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts)) < 0)
goto cleanup;
if ((error = git_diff_index_to_workdir(&diff2, repo, NULL, &opts)) < 0)
goto cleanup;
if ((error = git_diff_merge(diff, diff2)) < 0)
if ((error = git_diff_tree_to_workdir_with_index(
&diff, repo, b_tree, &opts)) < 0)
goto cleanup;
data.index = index;
data.include_changed = true;
if ((error = git_diff_foreach(
diff, update_index_cb, NULL, NULL, &data)) < 0)
{
if (error == GIT_EUSER)
error = data.error;
if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
goto cleanup;
}
if ((error = build_tree_from_index(tree_out, index)) < 0)
goto cleanup;
error = build_tree_from_index(tree_out, index);
cleanup:
git_diff_list_free(diff);
git_diff_list_free(diff2);
git_diff_free(diff);
git_tree_free(b_tree);
return error;
......@@ -436,14 +411,16 @@ static int update_reflog(
const git_signature *stasher,
const char *message)
{
git_reference *stash = NULL;
git_reference *stash;
git_reflog *reflog = NULL;
int error;
if ((error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0)
goto cleanup;
if ((error = git_reflog_read(&reflog, stash)) < 0)
git_reference_free(stash);
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE) < 0))
goto cleanup;
if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0)
......@@ -453,7 +430,6 @@ static int update_reflog(
goto cleanup;
cleanup:
git_reference_free(stash);
git_reflog_free(reflog);
return error;
}
......@@ -599,7 +575,7 @@ int git_stash_foreach(
if (error < 0)
goto cleanup;
if ((error = git_reflog_read(&reflog, stash)) < 0)
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
goto cleanup;
max = git_reflog_entrycount(reflog);
......@@ -633,7 +609,7 @@ int git_stash_drop(
if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
return error;
if ((error = git_reflog_read(&reflog, stash)) < 0)
if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
goto cleanup;
max = git_reflog_entrycount(reflog);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment