Commit a50086d1 by Vicent Marti

Merge branch 'development'

parents 5b9fac39 f124ebd4

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.

/tests-clar/clar.h
/tests-clar/clar_main.c
/tests-clar/clar.suite
/tests-clar/clar.suite.rule
/tests-clar/.clarcache
/apidocs
/trash-*.exe
/libgit2.pc
......@@ -13,6 +14,7 @@
.lock-wafbuild
.waf*
build/
build-amiga/
tests/tmp/
msvc/Debug/
msvc/Release/
......@@ -26,3 +28,5 @@ CMake*
*.cmake
.DS_Store
*~
tags
mkmf.log
Vicent Martí <vicent@github.com> Vicent Marti <tanoku@gmail.com>
Vicent Martí <vicent@github.com> Vicent Martí <tanoku@gmail.com>
Michael Schubert <schu@schu.io> schu <schu-github@schulog.org>
......@@ -2,16 +2,26 @@
# see travis-ci.org for details
# As CMake is not officially supported we use erlang VMs
language: erlang
language: c
compiler:
- gcc
- clang
# Settings to try
env:
- OPTIONS="-DTHREADSAFE=ON -DCMAKE_BUILD_TYPE=Release"
- OPTIONS="-DBUILD_CLAR=ON"
- OPTIONS="-DBUILD_CLAR=ON -DBUILD_EXAMPLES=ON"
matrix:
include:
- compiler: i586-mingw32msvc-gcc
env: OPTIONS="-DBUILD_CLAR=OFF -DWIN32=ON -DMINGW=ON"
# Make sure CMake is installed
install:
- sudo apt-get install cmake
- sudo apt-get update >/dev/null
- sudo apt-get -q install cmake valgrind
# Run the Build script
script:
......@@ -19,20 +29,28 @@ script:
- cd _build
- cmake .. -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS
- cmake --build . --target install
- ctest -V .
# Run Tests
after_script:
- ctest -V .
after_success:
- valgrind --leak-check=full --show-reachable=yes --suppressions=../libgit2_clar.supp ./libgit2_clar -ionline
# Only watch the development branch
branches:
only:
- development
# Notify development list when needed
notifications:
recipients:
- vicent@github.com
email:
on_success: change
on_failure: always
irc:
channels:
- irc.freenode.net#libgit2
on_success: change
on_failure: always
use_notice: true
skip_join: true
campfire:
on_success: always
on_failure: always
rooms:
- secure: "sH0dpPWMirbEe7AvLddZ2yOp8rzHalGmv0bYL/LIhVw3JDI589HCYckeLMSB\n3e/FeXw4bn0EqXWEXijVa4ijbilVY6d8oprdqMdWHEodng4KvY5vID3iZSGT\nxylhahO1XHmRynKQLOAvxlc93IlpVW38vQfby8giIY1nkpspb2w="
......@@ -4,9 +4,12 @@ to the libgit2 project (sorted alphabetically):
Alex Budovski
Alexei Sholik
Andreas Ericsson
Anton "antong" Gyllenberg
Ankur Sethi
Ben Noordhuis
Ben Straub
Benjamin C Meyer
Brian Downing
Brian Lopez
Carlos Martín Nieto
Colin Timmermans
......@@ -14,9 +17,12 @@ Daniel Huckstep
Dave Borowitz
David Boyce
David Glesser
Dmitry Kakurin
Dmitry Kovega
Emeric Fermas
Emmanuel Rodriguez
Florian Forster
Holger Weiss
Ingmar Vanhassel
J. David Ibáñez
Jakob Pfender
......@@ -29,12 +35,15 @@ Jonathan "Duke" Leto
Julien Miotte
Julio Espinoza-Sokal
Justin Love
Kelly "kelly.leahy" Leahy
Kirill A. Shutemov
Lambert CLARA
Luc Bertrand
Marc Pegon
Marcel Groothuis
Marco Villegas
Michael "schu" Schubert
Microsoft Corporation
Olivier Ramonat
Peter Drahoš
Pierre Habouzit
......@@ -45,8 +54,9 @@ Romain Geissler
Romain Muller
Russell Belfer
Sakari Jokinen
Sam
Samuel Charles "Sam" Day
Sarath Lakshman
Sascha Cunz
Sascha Peilicke
Scott Chacon
Sebastian Schuberth
......@@ -54,11 +64,9 @@ Sergey Nikishin
Shawn O. Pearce
Shuhei Tanuma
Steve Frécinaux
Sven Strickroth
Tim Branyen
Tim Clem
Tim Harder
Trent Mick
Vicent Marti
antong
kelly.leahy
schu
# Welcome to libgit2!
We're making it easy to do interesting things with git, and we'd love to have
your help.
## Discussion & Chat
We hang out in the #libgit2 channel on irc.freenode.net.
Also, feel free to open an
[Issue](https://github.com/libgit2/libgit2/issues/new) to start a discussion
about any concerns you have. We like to use Issues for that so there is an
easily accessible permanent record of the conversation.
## Reporting Bugs
First, know which version of libgit2 your problem is in and include it in
your bug report. This can either be a tag (e.g.
[v0.17.0](https://github.com/libgit2/libgit2/tree/v0.17.0) ) or a commit
SHA (e.g.
[01be7863](https://github.com/libgit2/libgit2/commit/01be786319238fd6507a08316d1c265c1a89407f)
). Using [`git describe`](http://git-scm.com/docs/git-describe) is a great
way to tell us what version you're working with.
If you're not running against the latest `development` branch version,
please compile and test against that to avoid re-reporting an issue that's
already been fixed.
It's *incredibly* helpful to be able to reproduce the problem. Please
include a list of steps, a bit of code, and/or a zipped repository (if
possible). Note that some of the libgit2 developers are employees of
GitHub, so if your repository is private, find us on IRC and we'll figure
out a way to help you.
## Pull Requests
Our work flow is a typical GitHub flow, where contributors fork the
[libgit2 repository](https://github.com/libgit2/libgit2), make their changes
on branch, and submit a
[Pull Request](https://help.github.com/articles/using-pull-requests)
(a.k.a. "PR").
Life will be a lot easier for you (and us) if you follow this pattern
(i.e. fork, named branch, submit PR). If you use your fork's `development`
branch, things can get messy.
Please include a nice description of your changes with your PR; if we have
to read the whole diff to figure out why you're contributing in the first
place, you're less likely to get feedback and have your change merged in.
## Porting Code From Other Open-Source Projects
`libgit2` is licensed under the terms of the GPL v2 with a linking
exception. Any code brought in must be compatible with those terms.
The most common case is porting code from core Git. Git is a pure GPL
project, which means that in order to port code to this project, we need the
explicit permission of the author. Check the
[`git.git-authors`](https://github.com/libgit2/libgit2/blob/development/git.git-authors)
file for authors who have already consented; feel free to add someone if
you've obtained their consent.
Other licenses have other requirements; check the license of the library
you're porting code *from* to see what you need to do. As a general rule,
MIT and BSD (3-clause) licenses are typically no problem. Apache 2.0
license typically doesn't work due to GPL incompatibility.
## Style Guide
`libgit2` is written in [ANSI C](http://en.wikipedia.org/wiki/ANSI_C)
(a.k.a. C89) with some specific conventions for function and type naming,
code formatting, and testing.
We like to keep the source code consistent and easy to read. Maintaining
this takes some discipline, but it's been more than worth it. Take a look
at the
[conventions file](https://github.com/libgit2/libgit2/blob/development/CONVENTIONS.md).
## Starter Projects
So, you want to start helping out with `libgit2`? That's fantastic? We
welcome contributions and we promise we'll try to be nice.
If you want to jump in, you can look at our issues list to see if there
are any unresolved issues to jump in on. Also, here is a list of some
smaller project ideas that could help you become familiar with the code
base and make a nice first step:
* Convert a `git_*modulename*_foreach()` callback-based iteration API
into a `git_*modulename*_iterator` object with a create/advance style
of API. This helps folks writing language bindings and usually isn't
too complicated.
* Write a new `examples/` program that mirrors a particular core git
command. (See `examples/diff.c` for example.) This lets you (and us)
easily exercise a particular facet of the API and measure compatability
and feature parity with core git.
* Submit a PR to clarify documentation! While we do try to document all of
the APIs, your fresh eyes on the documentation will find areas that are
confusing much more easily.
libgit2 conventions
===================
Namespace Prefixes
------------------
All types and functions start with 'git_'.
All #define macros start with 'GIT_'.
Type Definitions
----------------
Most types should be opaque, e.g.:
----
typedef struct git_odb git_odb;
----
with allocation functions returning an "instance" created within
the library, and not within the application. This allows the type
to grow (or shrink) in size without rebuilding client code.
Public Exported Function Definitions
------------------------------------
All exported functions must be declared as:
----
GIT_EXTERN(result_type) git_modulename_functionname(arg_list);
----
Semi-Private Exported Functions
-------------------------------
Functions whose modulename is followed by two underscores,
for example 'git_odb__read_packed', are semi-private functions.
They are primarily intended for use within the library itself,
and may disappear or change their signature in a future release.
Calling Conventions
-------------------
Functions should prefer to return a 'int' to indicate success or
failure and supply any output through the first argument (or first
few arguments if multiple outputs are supplied).
int status codes are 0 for GIT_SUCCESS and < 0 for an error.
This permits common POSIX result testing:
----
if (git_odb_open(&odb, path))
abort("odb open failed");
----
Functions returning a pointer may return NULL instead of an int
if there is only one type of failure (ENOMEM).
Functions returning a pointer may also return NULL if the common
case needed by the application is strictly success/failure and a
(possibly slower) function exists that the caller can use to get
more detailed information. Parsing common data structures from
on-disk formats is a good example of this pattern; in general a
"corrupt" entity can be treated as though it does not exist but
a more sophisticated "fsck" support function can report how the
entity is malformed.
Documentation Fomatting
-----------------------
All comments should conform to Doxygen "javadoc" style conventions
for formatting the public API documentation.
Public Header Format
--------------------
All public headers defining types, functions or macros must use
the following form, where ${filename} is the name of the file,
after replacing non-identifier characters with '_'.
----
#ifndef INCLUDE_git_${filename}_h__
#define INCLUDE_git_${filename}_h__
#include "git/common.h"
/**
* @file git/${filename}.h
* @brief Git some description
* @defgroup git_${filename} some description routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
... definitions ...
/** @} */
GIT_END_DECL
#endif
----
# Libgit2 Conventions
We like to keep the source consistent and readable. Herein are some
guidelines that should help with that.
## Compatibility
`libgit2` runs on many different platforms with many different compilers.
It is written in [ANSI C](http://en.wikipedia.org/wiki/ANSI_C) (a.k.a. C89)
with some specific standards for function and type naming, code formatting,
and testing.
We try to avoid more recent extensions to maximize portability. We also, to
the greatest extent possible, try to avoid lots of `#ifdef`s inside the core
code base. This is somewhat unavoidable, but since it can really hamper
maintainability, we keep it to a minimum.
## Match Surrounding Code
If there is one rule to take away from this document, it is *new code should
match the surrounding code in a way that makes it impossible to distinguish
the new from the old.* Consistency is more important to us than anyone's
personal opinion about where braces should be placed or spaces vs. tabs.
If a section of code is being completely rewritten, it is okay to bring it
in line with the standards that are laid out here, but we will not accept
submissions that contain a large number of changes that are merely
reformatting.
## Naming Things
All external types and functions start with `git_` and all `#define` macros
start with `GIT_`. The `libgit2` API is mostly broken into related
functional modules each with a corresponding header. All functions in a
module should be named like `git_modulename_functioname()`
(e.g. `git_repository_open()`).
Functions with a single output parameter should name that parameter `out`.
Multiple outputs should be named `foo_out`, `bar_out`, etc.
Parameters of type `git_oid` should be named `id`, or `foo_id`. Calls that
return an OID should be named `git_foo_id`.
Where a callback function is used, the function should also include a
user-supplied extra input that is a `void *` named "payload" that will be
passed through to the callback at each invocation.
## Typedefs
Wherever possible, use `typedef`. In some cases, if a structure is just a
collection of function pointers, the pointer types don't need to be
separately typedef'd, but loose function pointer types should be.
## Exports
All exported functions must be declared as:
```c
GIT_EXTERN(result_type) git_modulename_functionname(arg_list);
```
## Internals
Functions whose *modulename* is followed by two underscores,
for example `git_odb__read_packed`, are semi-private functions.
They are primarily intended for use within the library itself,
and may disappear or change their signature in a future release.
## Parameters
Out parameters come first.
Whenever possible, pass argument pointers as `const`. Some structures (such
as `git_repository` and `git_index`) have mutable internal structure that
prevents this.
Callbacks should always take a `void *` payload as their last parameter.
Callback pointers are grouped with their payloads, and typically come last
when passed as arguments:
```c
int git_foo(git_repository *repo, git_foo_cb callback, void *payload);
```
## Memory Ownership
Some APIs allocate memory which the caller is responsible for freeing; others
return a pointer into a buffer that's owned by some other object. Make this
explicit in the documentation.
## Return codes
Most public APIs should return an `int` error code. As is typical with most
C library functions, a zero value indicates success and a negative value
indicates failure.
Some bindings will transform these returned error codes into exception
types, so returning a semantically appropriate error code is important.
Check
[`include/git2/errors.h`](https://github.com/libgit2/libgit2/blob/development/include/git2/errors.h)
for the return codes already defined.
In your implementation, use `giterr_set()` to provide extended error
information to callers.
If a `libgit2` function internally invokes another function that reports an
error, but the error is not propagated up, use `giterr_clear()` to prevent
callers from getting the wrong error message later on.
## Structs
Most public types should be opaque, e.g.:
```C
typedef struct git_odb git_odb;
```
...with allocation functions returning an "instance" created within
the library, and not within the application. This allows the type
to grow (or shrink) in size without rebuilding client code.
To preserve ABI compatibility, include an `int version` field in all opaque
structures, and initialize to the latest version in the construction call.
Increment the "latest" version whenever the structure changes, and try to only
append to the end of the structure.
## Option Structures
If a function's parameter count is too high, it may be desirable to package
up the options in a structure. Make them transparent, include a version
field, and provide an initializer constant or constructor. Using these
structures should be this easy:
```C
git_foo_options opts = GIT_FOO_OPTIONS_INIT;
opts.baz = BAZ_OPTION_ONE;
git_foo(&opts);
```
## Enumerations
Typedef all enumerated types. If each option stands alone, use the enum
type for passing them as parameters; if they are flags to be OR'ed together,
pass them as `unsigned int` or `uint32_t` or some appropriate type.
## Code Layout
Try to keep lines less than 80 characters long. This is a loose
requirement, but going significantly over 80 columns is not nice.
Use common sense to wrap most code lines; public function declarations
can use a couple of different styles:
```c
/** All on one line is okay if it fits */
GIT_EXTERN(int) git_foo_simple(git_oid *id);
/** Otherwise one argument per line is a good next step */
GIT_EXTERN(int) git_foo_id(
git_oid **out,
int a,
int b);
```
Indent with tabs; set your editor's tab width to 4 for best effect.
Avoid trailing whitespace and only commit Unix-style newlines (i.e. no CRLF
in the repository - just set `core.autocrlf` to true if you are writing code
on a Windows machine).
## Documentation
All comments should conform to Doxygen "javadoc" style conventions for
formatting the public API documentation. Try to document every parameter,
and keep the comments up to date if you change the parameter list.
## Public Header Template
Use this template when creating a new public header.
```C
#ifndef INCLUDE_git_${filename}_h__
#define INCLUDE_git_${filename}_h__
#include "git/common.h"
/**
* @file git/${filename}.h
* @brief Git some description
* @defgroup git_${filename} some description routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/* ... definitions ... */
/** @} */
GIT_END_DECL
#endif
```
## Inlined functions
All inlined functions must be declared as:
```C
GIT_INLINE(result_type) git_modulename_functionname(arg_list);
```
## Tests
`libgit2` uses the [clar](https://github.com/vmg/clar) testing framework.
All PRs should have corresponding tests.
* If the PR fixes an existing issue, the test should fail prior to applying
the PR and succeed after applying it.
* If the PR is for new functionality, then the tests should exercise that
new functionality to a certain extent. We don't require 100% coverage
right now (although we are getting stricter over time).
When adding new tests, we prefer if you attempt to reuse existing test data
(in `tests-clar/resources/`) if possible. If you are going to add new test
repositories, please try to strip them of unnecessary files (e.g. sample
hooks, etc).
libgit2 is Copyright (C) 2009-2012 the libgit2 contributors,
libgit2 is Copyright (C) the libgit2 contributors,
unless otherwise stated. See the AUTHORS file for details.
Note that the only valid version of the GPL as far as this project
......
PLATFORM=$(shell uname -o)
rm=rm -f
CC=cc
AR=ar cq
RANLIB=ranlib
LIBNAME=libgit2.a
ifeq ($(PLATFORM),Msys)
CC=gcc
else
CC=cc
endif
INCLUDES= -I. -Isrc -Iinclude -Ideps/http-parser -Ideps/zlib
DEFINES= $(INCLUDES) -DNO_VIZ -DSTDC -DNO_GZIP -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
CFLAGS= -g $(DEFINES) -Wall -Wextra -fPIC -O2
DEFINES= $(INCLUDES) -DNO_VIZ -DSTDC -DNO_GZIP -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE $(EXTRA_DEFINES)
CFLAGS= -g $(DEFINES) -Wall -Wextra -O2 $(EXTRA_CFLAGS)
SRCS = $(wildcard src/*.c) $(wildcard src/transports/*.c) $(wildcard src/xdiff/*.c) $(wildcard deps/http-parser/*.c) $(wildcard deps/zlib/*.c) src/hash/hash_generic.c
ifeq ($(PLATFORM),Msys)
SRCS += $(wildcard src/win32/*.c) $(wildcard src/compat/*.c) deps/regex/regex.c
INCLUDES += -Ideps/regex
DEFINES += -DWIN32 -D_WIN32_WINNT=0x0501
else
SRCS += $(wildcard src/unix/*.c)
CFLAGS += -fPIC
endif
SRCS = $(wildcard src/*.c) $(wildcard src/transports/*.c) $(wildcard src/unix/*.c) $(wildcard deps/http-parser/*.c) $(wildcard deps/zlib/*.c)
OBJS = $(patsubst %.c,%.o,$(SRCS))
%.c.o:
......
......@@ -11,11 +11,14 @@ libgit2 is licensed under a **very permissive license** (GPLv2 with a special Li
This basically means that you can link it (unmodified) with any kind of software without having to
release its source code.
* Mailing list: <libgit2@librelist.org>
* Mailing list: ~~<libgit2@librelist.org>~~
The libgit2 mailing list has
traditionally been hosted in Librelist, but Librelist is and has always
been a shitshow. We encourage you to [open an issue](https://github.com/libgit2/libgit2/issues)
on GitHub instead for any questions regarding the library.
* Archives: <http://librelist.com/browser/libgit2/>
* Website: <http://libgit2.github.com>
* API documentation: <http://libgit2.github.com/libgit2>
* Usage guide: <http://libgit2.github.com/api.html>
What It Can Do
==================================
......@@ -58,20 +61,47 @@ To install the library you can specify the install prefix by setting:
$ cmake .. -DCMAKE_INSTALL_PREFIX=/install/prefix
$ cmake --build . --target install
If you want to build a universal binary for Mac OS X, CMake sets it
all up for you if you use `-DCMAKE_OSX_ARCHITECTURES="i386;x86_64"`
when configuring.
For more advanced use or questions about CMake please read <http://www.cmake.org/Wiki/CMake_FAQ>.
The following CMake variables are declared:
- `INSTALL_BIN`: Where to install binaries to.
- `INSTALL_LIB`: Where to install libraries to.
- `INSTALL_INC`: Where to install headers to.
- `BIN_INSTALL_DIR`: Where to install binaries to.
- `LIB_INSTALL_DIR`: Where to install libraries to.
- `INCLUDE_INSTALL_DIR`: Where to install headers to.
- `BUILD_SHARED_LIBS`: Build libgit2 as a Shared Library (defaults to ON)
- `BUILD_CLAR`: Build [Clar](https://github.com/tanoku/clar)-based test suite (defaults to ON)
- `BUILD_CLAR`: Build [Clar](https://github.com/vmg/clar)-based test suite (defaults to ON)
- `THREADSAFE`: Build libgit2 with threading support (defaults to OFF)
- `STDCALL`: Build libgit2 as `stdcall`. Turn off for `cdecl` (Windows; defaults to ON)
Compiler and linker options
---------------------------
CMake lets you specify a few variables to control the behavior of the
compiler and linker. These flags are rarely used but can be useful for
64-bit to 32-bit cross-compilation.
- `CMAKE_C_FLAGS`: Set your own compiler flags
- `CMAKE_FIND_ROOT_PATH`: Override the search path for libraries
- `ZLIB_LIBRARY`, `OPENSSL_SSL_LIBRARY` AND `OPENSSL_CRYPTO_LIBRARY`:
Tell CMake where to find those specific libraries
MacOS X
-------
If you want to build a universal binary for Mac OS X, CMake sets it
all up for you if you use `-DCMAKE_OSX_ARCHITECTURES="i386;x86_64"`
when configuring.
Windows
-------
You need to run the CMake commands from the Visual Studio command
prompt, not the regular or Windows SDK one. Select the right generator
for your version with the `-G "Visual Studio X" option.
See [the wiki]
(https://github.com/libgit2/libgit2/wiki/Building-libgit2-on-Windows)
for more detailed instructions.
Language Bindings
==================================
......@@ -82,6 +112,8 @@ Here are the bindings to libgit2 that are currently available:
* libqgit2, Qt bindings <https://projects.kde.org/projects/playground/libs/libqgit2/>
* Chicken Scheme
* chicken-git <https://wiki.call-cc.org/egg/git>
* D
* dlibgit <https://github.com/AndrejMitrovic/dlibgit>
* Delphi
* GitForDelphi <https://github.com/libgit2/GitForDelphi>
* Erlang
......@@ -89,9 +121,9 @@ Here are the bindings to libgit2 that are currently available:
* Go
* go-git <https://github.com/str1ngs/go-git>
* GObject
* libgit2-glib <https://github.com/nacho/libgit2-glib>
* libgit2-glib <https://live.gnome.org/Libgit2-glib>
* Haskell
* hgit2 <https://github.com/norm2782/hgit2>
* hgit2 <https://github.com/fpco/gitlib>
* Lua
* luagit2 <https://github.com/libgit2/luagit2>
* .NET
......@@ -124,7 +156,9 @@ How Can I Contribute?
==================================
Fork libgit2/libgit2 on GitHub, add your improvement, push it to a branch
in your fork named for the topic, send a pull request.
in your fork named for the topic, send a pull request. If you change the
API or make other large changes, make a note of it in docs/rel-notes/ in a
file named after the next release.
You can also file bugs or feature requests under the libgit2 project on
GitHub, or join us on the mailing list by sending an email to:
......
......@@ -24,16 +24,25 @@
extern "C" {
#endif
#define HTTP_PARSER_VERSION_MAJOR 1
#define HTTP_PARSER_VERSION_MAJOR 2
#define HTTP_PARSER_VERSION_MINOR 0
#ifdef _MSC_VER
/* disable silly warnings */
# pragma warning(disable: 4127 4214)
#endif
#include <sys/types.h>
#include "git2/common.h"
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
#include <BaseTsd.h>
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
typedef SIZE_T size_t;
typedef SSIZE_T ssize_t;
#else
#include <stdint.h>
#endif
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
* faster
......@@ -42,21 +51,12 @@ extern "C" {
# define HTTP_PARSER_STRICT 1
#endif
/* Compile with -DHTTP_PARSER_DEBUG=1 to add extra debugging information to
* the error reporting facility.
*/
#ifndef HTTP_PARSER_DEBUG
# define HTTP_PARSER_DEBUG 0
#endif
/* Maximium header size allowed */
#define HTTP_MAX_HEADER_SIZE (80*1024)
typedef struct http_parser http_parser;
typedef struct http_parser_settings http_parser_settings;
typedef struct http_parser_result http_parser_result;
/* Callbacks should return non-zero to indicate an error. The parser will
......@@ -69,7 +69,7 @@ typedef struct http_parser_result http_parser_result;
* chunked' headers that indicate the presence of a body.
*
* http_data_cb does not return data chunks. It will be call arbitrarally
* many times for each string. E.G. you might get 10 callbacks for "on_path"
* many times for each string. E.G. you might get 10 callbacks for "on_url"
* each providing just a few characters more data.
*/
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
......@@ -77,36 +77,44 @@ typedef int (*http_cb) (http_parser*);
/* Request Methods */
#define HTTP_METHOD_MAP(XX) \
XX(0, DELETE, DELETE) \
XX(1, GET, GET) \
XX(2, HEAD, HEAD) \
XX(3, POST, POST) \
XX(4, PUT, PUT) \
/* pathological */ \
XX(5, CONNECT, CONNECT) \
XX(6, OPTIONS, OPTIONS) \
XX(7, TRACE, TRACE) \
/* webdav */ \
XX(8, COPY, COPY) \
XX(9, LOCK, LOCK) \
XX(10, MKCOL, MKCOL) \
XX(11, MOVE, MOVE) \
XX(12, PROPFIND, PROPFIND) \
XX(13, PROPPATCH, PROPPATCH) \
XX(14, SEARCH, SEARCH) \
XX(15, UNLOCK, UNLOCK) \
/* subversion */ \
XX(16, REPORT, REPORT) \
XX(17, MKACTIVITY, MKACTIVITY) \
XX(18, CHECKOUT, CHECKOUT) \
XX(19, MERGE, MERGE) \
/* upnp */ \
XX(20, MSEARCH, M-SEARCH) \
XX(21, NOTIFY, NOTIFY) \
XX(22, SUBSCRIBE, SUBSCRIBE) \
XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
/* RFC-5789 */ \
XX(24, PATCH, PATCH) \
XX(25, PURGE, PURGE) \
enum http_method
{ HTTP_DELETE = 0
, HTTP_GET
, HTTP_HEAD
, HTTP_POST
, HTTP_PUT
/* pathological */
, HTTP_CONNECT
, HTTP_OPTIONS
, HTTP_TRACE
/* webdav */
, HTTP_COPY
, HTTP_LOCK
, HTTP_MKCOL
, HTTP_MOVE
, HTTP_PROPFIND
, HTTP_PROPPATCH
, HTTP_UNLOCK
/* subversion */
, HTTP_REPORT
, HTTP_MKACTIVITY
, HTTP_CHECKOUT
, HTTP_MERGE
/* upnp */
, HTTP_MSEARCH
, HTTP_NOTIFY
, HTTP_SUBSCRIBE
, HTTP_UNSUBSCRIBE
/* RFC-5789 */
, HTTP_PATCH
{
#define XX(num, name, string) HTTP_##name = num,
HTTP_METHOD_MAP(XX)
#undef XX
};
......@@ -134,10 +142,7 @@ enum flags
\
/* Callback-related errors */ \
XX(CB_message_begin, "the on_message_begin callback failed") \
XX(CB_path, "the on_path callback failed") \
XX(CB_query_string, "the on_query_string callback failed") \
XX(CB_url, "the on_url callback failed") \
XX(CB_fragment, "the on_fragment callback failed") \
XX(CB_header_field, "the on_header_field callback failed") \
XX(CB_header_value, "the on_header_value callback failed") \
XX(CB_headers_complete, "the on_headers_complete callback failed") \
......@@ -168,6 +173,7 @@ enum flags
XX(INVALID_CONSTANT, "invalid constant string") \
XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
XX(STRICT, "strict mode assertion failed") \
XX(PAUSED, "parser is paused") \
XX(UNKNOWN, "an unknown error occurred")
......@@ -182,30 +188,23 @@ enum http_errno {
/* Get an http_errno value from an http_parser */
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
/* Get the line number that generated the current error */
#if HTTP_PARSER_DEBUG
#define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
#else
#define HTTP_PARSER_ERRNO_LINE(p) 0
#endif
struct http_parser {
/** PRIVATE **/
unsigned char type : 2;
unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
unsigned char state;
unsigned char header_state;
unsigned char index;
unsigned char type : 2; /* enum http_parser_type */
unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
unsigned char state; /* enum state from http_parser.c */
unsigned char header_state; /* enum header_state from http_parser.c */
unsigned char index; /* index into current matcher */
size_t nread;
int64_t content_length;
uint32_t nread; /* # bytes read in various scenarios */
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
/** READ-ONLY **/
unsigned short http_major;
unsigned short http_minor;
unsigned short status_code; /* responses only */
unsigned char method; /* requests only */
unsigned char method; /* requests only */
unsigned char http_errno : 7;
/* 1 = Upgrade header was present and the parser has exited because of that.
......@@ -215,10 +214,6 @@ struct http_parser {
*/
unsigned char upgrade : 1;
#if HTTP_PARSER_DEBUG
uint32_t error_lineno;
#endif
/** PUBLIC **/
void *data; /* A pointer to get hook to the "connection" or "socket" object */
};
......@@ -235,6 +230,36 @@ struct http_parser_settings {
};
enum http_parser_url_fields
{ UF_SCHEMA = 0
, UF_HOST = 1
, UF_PORT = 2
, UF_PATH = 3
, UF_QUERY = 4
, UF_FRAGMENT = 5
, UF_USERINFO = 6
, UF_MAX = 7
};
/* Result structure for http_parser_parse_url().
*
* Callers should index into field_data[] with UF_* values iff field_set
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
* because we probably have padding left over), we convert any port to
* a uint16_t.
*/
struct http_parser_url {
uint16_t field_set; /* Bitmask of (1 << UF_*) values */
uint16_t port; /* Converted UF_PORT string */
struct {
uint16_t off; /* Offset into buffer in which field starts */
uint16_t len; /* Length of run in buffer */
} field_data[UF_MAX];
};
void http_parser_init(http_parser *parser, enum http_parser_type type);
......@@ -245,12 +270,12 @@ size_t http_parser_execute(http_parser *parser,
/* If http_should_keep_alive() in the on_headers_complete or
* on_message_complete callback returns true, then this will be should be
* on_message_complete callback returns 0, then this should be
* the last message on the connection.
* If you are the server, respond with the "Connection: close" header.
* If you are the client, close the connection.
*/
int http_should_keep_alive(http_parser *parser);
int http_should_keep_alive(const http_parser *parser);
/* Returns a string version of the HTTP method. */
const char *http_method_str(enum http_method m);
......@@ -261,6 +286,17 @@ const char *http_errno_name(enum http_errno err);
/* Return a string description of the given error */
const char *http_errno_description(enum http_errno err);
/* Parse a URL; return nonzero on failure */
int http_parser_parse_url(const char *buf, size_t buflen,
int is_connect,
struct http_parser_url *u);
/* Pause or un-pause the parser; a nonzero value pauses */
void http_parser_pause(http_parser *parser, int paused);
/* Checks if this is the final chunk of the body. */
int http_body_is_final(const http_parser *parser);
#ifdef __cplusplus
}
#endif
......
......@@ -542,7 +542,7 @@ weak_alias (__regcomp, regcomp)
from either regcomp or regexec. We don't use PREG here. */
size_t
regerror(int errcode, const regex_t *__restrict preg,
regerror(int errcode, UNUSED const regex_t *__restrict preg,
char *__restrict errbuf, size_t errbuf_size)
{
const char *msg;
......@@ -1140,7 +1140,7 @@ analyze (regex_t *preg)
dfa->subexp_map[i] = i;
preorder (dfa->str_tree, optimize_subexps, dfa);
for (i = 0; i < preg->re_nsub; i++)
if (dfa->subexp_map[i] != i)
if (dfa->subexp_map[i] != (int)i)
break;
if (i == preg->re_nsub)
{
......@@ -1358,7 +1358,7 @@ calc_first (void *extra, bin_tree_t *node)
/* Pass 2: compute NEXT on the tree. Preorder visit. */
static reg_errcode_t
calc_next (void *extra, bin_tree_t *node)
calc_next (UNUSED void *extra, bin_tree_t *node)
{
switch (node->token.type)
{
......@@ -1609,7 +1609,8 @@ calc_inveclosure (re_dfa_t *dfa)
static reg_errcode_t
calc_eclosure (re_dfa_t *dfa)
{
int node_idx, incomplete;
size_t node_idx;
int incomplete;
#ifdef DEBUG
assert (dfa->nodes_len > 0);
#endif
......@@ -3308,7 +3309,7 @@ parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
static reg_errcode_t
parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
re_token_t *token, int token_len, re_dfa_t *dfa,
re_token_t *token, int token_len, UNUSED re_dfa_t *dfa,
reg_syntax_t syntax, int accept_hyphen)
{
#ifdef RE_ENABLE_I18N
......@@ -3803,7 +3804,7 @@ free_token (re_token_t *node)
and its children. */
static reg_errcode_t
free_tree (void *extra, bin_tree_t *node)
free_tree (UNUSED void *extra, bin_tree_t *node)
{
free_token (&node->token);
return REG_NOERROR;
......
......@@ -32,7 +32,7 @@ static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
#ifdef GAWK
#undef MAX /* safety */
static int
static size_t
MAX(size_t a, size_t b)
{
return (a > b ? a : b);
......
......@@ -27,6 +27,14 @@
#include <stdlib.h>
#include <string.h>
#ifndef UNUSED
# ifdef __GNUC__
# define UNUSED __attribute__((unused))
# else
# define UNUSED
# endif
#endif
#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
# include <langinfo.h>
#endif
......@@ -63,7 +71,7 @@
#endif
#else /* GAWK */
/*
* This is a freaking mess. On glibc systems you have to define
* This is a mess. On glibc systems you have to define
* a magic constant to get isblank() out of <ctype.h>, since it's
* a C99 function. To heck with all that and borrow a page from
* dfa.c's book.
......@@ -171,8 +179,9 @@ extern const size_t __re_error_msgid_idx[] attribute_hidden;
typedef unsigned long int bitset_word_t;
/* All bits set in a bitset_word_t. */
#define BITSET_WORD_MAX ULONG_MAX
/* Number of bits in a bitset_word_t. */
#define BITSET_WORD_BITS (sizeof (bitset_word_t) * CHAR_BIT)
/* Number of bits in a bitset_word_t. Cast to int as most code use it
* like that for counting */
#define BITSET_WORD_BITS ((int)(sizeof (bitset_word_t) * CHAR_BIT))
/* Number of bitset_word_t in a bit_set. */
#define BITSET_WORDS (SBC_MAX / BITSET_WORD_BITS)
typedef bitset_word_t bitset_t[BITSET_WORDS];
......
......@@ -689,7 +689,7 @@ re_search_internal (const regex_t *preg,
if (nmatch > 1 || dfa->has_mb_node)
{
/* Avoid overflow. */
if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= mctx.input.bufs_len, 0))
if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= (size_t)mctx.input.bufs_len, 0))
{
err = REG_ESPACE;
goto free_return;
......@@ -920,7 +920,7 @@ re_search_internal (const regex_t *preg,
if (dfa->subexp_map)
for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
if (dfa->subexp_map[reg_idx] != reg_idx)
if (dfa->subexp_map[reg_idx] != (int)reg_idx)
{
pmatch[reg_idx + 1].rm_so
= pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
......@@ -953,7 +953,7 @@ prune_impossible_nodes (re_match_context_t *mctx)
halt_node = mctx->last_node;
/* Avoid overflow. */
if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= match_last, 0))
if (BE (SIZE_MAX / sizeof (re_dfastate_t *) <= (size_t)match_last, 0))
return REG_ESPACE;
sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
......@@ -3375,7 +3375,7 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
/* Avoid arithmetic overflow in size calculation. */
if (BE ((((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX)
/ (3 * sizeof (re_dfastate_t *)))
< ndests),
< (size_t)ndests),
0))
goto out_free;
......@@ -4099,7 +4099,7 @@ extend_buffers (re_match_context_t *mctx)
re_string_t *pstr = &mctx->input;
/* Avoid overflow. */
if (BE (INT_MAX / 2 / sizeof (re_dfastate_t *) <= pstr->bufs_len, 0))
if (BE (INT_MAX / 2 / sizeof (re_dfastate_t *) <= (size_t)pstr->bufs_len, 0))
return REG_ESPACE;
/* Double the lengthes of the buffers. */
......
Checkout Internals
==================
Checkout has to handle a lot of different cases. It examines the
differences between the target tree, the baseline tree and the working
directory, plus the contents of the index, and groups files into five
categories:
1. UNMODIFIED - Files that match in all places.
2. SAFE - Files where the working directory and the baseline content
match that can be safely updated to the target.
3. DIRTY/MISSING - Files where the working directory differs from the
baseline but there is no conflicting change with the target. One
example is a file that doesn't exist in the working directory - no
data would be lost as a result of writing this file. Which action
will be taken with these files depends on the options you use.
4. CONFLICTS - Files where changes in the working directory conflict
with changes to be applied by the target. If conflicts are found,
they prevent any other modifications from being made (although there
are options to override that and force the update, of course).
5. UNTRACKED/IGNORED - Files in the working directory that are untracked
or ignored (i.e. only in the working directory, not the other places).
Right now, this classification is done via 3 iterators (for the three
trees), with a final lookup in the index. At some point, this may move to
a 4 iterator version to incorporate the index better.
The actual checkout is done in five phases (at least right now).
1. The diff between the baseline and the target tree is used as a base
list of possible updates to be applied.
2. Iterate through the diff and the working directory, building a list of
actions to be taken (and sending notifications about conflicts and
dirty files).
3. Remove any files / directories as needed (because alphabetical
iteration means that an untracked directory will end up sorted *after*
a blob that should be checked out with the same name).
4. Update all blobs.
5. Update all submodules (after 4 in case a new .gitmodules blob was
checked out)
Checkout could be driven either off a target-to-workdir diff or a
baseline-to-target diff. There are pros and cons of each.
Target-to-workdir means the diff includes every file that could be
modified, which simplifies bookkeeping, but the code to constantly refer
back to the baseline gets complicated.
Baseline-to-target has simpler code because the diff defines the action to
take, but needs special handling for untracked and ignored files, if they
need to be removed.
The current checkout implementation is based on a baseline-to-target diff.
Picking Actions
===============
The most interesting aspect of this is phase 2, picking the actions that
should be taken. There are a lot of corner cases, so it may be easier to
start by looking at the rules for a simple 2-iterator diff:
Key
---
- B1,B2,B3 - blobs with different SHAs,
- Bi - ignored blob (WD only)
- T1,T2,T3 - trees with different SHAs,
- Ti - ignored tree (WD only)
- x - nothing
Diff with 2 non-workdir iterators
---------------------------------
Old New
--- ---
0 x x - nothing
1 x B1 - added blob
2 x T1 - added tree
3 B1 x - removed blob
4 B1 B1 - unmodified blob
5 B1 B2 - modified blob
6 B1 T1 - typechange blob -> tree
7 T1 x - removed tree
8 T1 B1 - typechange tree -> blob
9 T1 T1 - unmodified tree
10 T1 T2 - modified tree (implies modified/added/removed blob inside)
Now, let's make the "New" iterator into a working directory iterator, so
we replace "added" items with either untracked or ignored, like this:
Diff with non-work & workdir iterators
--------------------------------------
Old New-WD
--- ------
0 x x - nothing
1 x B1 - untracked blob
2 x Bi - ignored file
3 x T1 - untracked tree
4 x Ti - ignored tree
5 B1 x - removed blob
6 B1 B1 - unmodified blob
7 B1 B2 - modified blob
8 B1 T1 - typechange blob -> tree
9 B1 Ti - removed blob AND ignored tree as separate items
10 T1 x - removed tree
11 T1 B1 - typechange tree -> blob
12 T1 Bi - removed tree AND ignored blob as separate items
13 T1 T1 - unmodified tree
14 T1 T2 - modified tree (implies modified/added/removed blob inside)
Note: if there is a corresponding entry in the old tree, then a working
directory item won't be ignored (i.e. no Bi or Ti for tracked items).
Now, expand this to three iterators: a baseline tree, a target tree, and
an actual working directory tree:
Checkout From 3 Iterators (2 not workdir, 1 workdir)
----------------------------------------------------
(base == old HEAD; target == what to checkout; actual == working dir)
base target actual/workdir
---- ------ ------
0 x x x - nothing
1 x x B1/Bi/T1/Ti - untracked/ignored blob/tree (SAFE)
2+ x B1 x - add blob (SAFE)
3 x B1 B1 - independently added blob (FORCEABLE-2)
4* x B1 B2/Bi/T1/Ti - add blob with content conflict (FORCEABLE-2)
5+ x T1 x - add tree (SAFE)
6* x T1 B1/Bi - add tree with blob conflict (FORCEABLE-2)
7 x T1 T1/i - independently added tree (SAFE+MISSING)
8 B1 x x - independently deleted blob (SAFE+MISSING)
9- B1 x B1 - delete blob (SAFE)
10- B1 x B2 - delete of modified blob (FORCEABLE-1)
11 B1 x T1/Ti - independently deleted blob AND untrack/ign tree (SAFE+MISSING !!!)
12 B1 B1 x - locally deleted blob (DIRTY || SAFE+CREATE)
13+ B1 B2 x - update to deleted blob (SAFE+MISSING)
14 B1 B1 B1 - unmodified file (SAFE)
15 B1 B1 B2 - locally modified file (DIRTY)
16+ B1 B2 B1 - update unmodified blob (SAFE)
17 B1 B2 B2 - independently updated blob (FORCEABLE-1)
18+ B1 B2 B3 - update to modified blob (FORCEABLE-1)
19 B1 B1 T1/Ti - locally deleted blob AND untrack/ign tree (DIRTY)
20* B1 B2 T1/Ti - update to deleted blob AND untrack/ign tree (F-1)
21+ B1 T1 x - add tree with locally deleted blob (SAFE+MISSING)
22* B1 T1 B1 - add tree AND deleted blob (SAFE)
23* B1 T1 B2 - add tree with delete of modified blob (F-1)
24 B1 T1 T1 - add tree with deleted blob (F-1)
25 T1 x x - independently deleted tree (SAFE+MISSING)
26 T1 x B1/Bi - independently deleted tree AND untrack/ign blob (F-1)
27- T1 x T1 - deleted tree (MAYBE SAFE)
28+ T1 B1 x - deleted tree AND added blob (SAFE+MISSING)
29 T1 B1 B1 - independently typechanged tree -> blob (F-1)
30+ T1 B1 B2 - typechange tree->blob with conflicting blob (F-1)
31* T1 B1 T1/T2 - typechange tree->blob (MAYBE SAFE)
32+ T1 T1 x - restore locally deleted tree (SAFE+MISSING)
33 T1 T1 B1/Bi - locally typechange tree->untrack/ign blob (DIRTY)
34 T1 T1 T1/T2 - unmodified tree (MAYBE SAFE)
35+ T1 T2 x - update locally deleted tree (SAFE+MISSING)
36* T1 T2 B1/Bi - update to tree with typechanged tree->blob conflict (F-1)
37 T1 T2 T1/T2/T3 - update to existing tree (MAYBE SAFE)
The number is followed by ' ' if no change is needed or '+' if the case
needs to write to disk or '-' if something must be deleted and '*' if
there should be a delete followed by an write.
There are four tiers of safe cases:
- SAFE == completely safe to update
- SAFE+MISSING == safe except the workdir is missing the expect content
- MAYBE SAFE == safe if workdir tree matches (or is missing) baseline
content, which is unknown at this point
- FORCEABLE == conflict unless FORCE is given
- DIRTY == no conflict but change is not applied unless FORCE
Some slightly unusual circumstances:
8 - parent dir is only deleted when file is, so parent will be left if
empty even though it would be deleted if the file were present
11 - core git does not consider this a conflict but attempts to delete T1
and gives "unable to unlink file" error yet does not skip the rest
of the operation
12 - without FORCE file is left deleted (i.e. not restored) so new wd is
dirty (and warning message "D file" is printed), with FORCE, file is
restored.
24 - This should be considered MAYBE SAFE since effectively it is 7 and 8
combined, but core git considers this a conflict unless forced.
26 - This combines two cases (1 & 25) (and also implied 8 for tree content)
which are ok on their own, but core git treat this as a conflict.
If not forced, this is a conflict. If forced, this actually doesn't
have to write anything and leaves the new blob as an untracked file.
32 - This is the only case where the baseline and target values match
and yet we will still write to the working directory. In all other
cases, if baseline == target, we don't touch the workdir (it is
either already right or is "dirty"). However, since this case also
implies that a ?/B1/x case will exist as well, it can be skipped.
Cases 3, 17, 24, 26, and 29 are all considered conflicts even though
none of them will require making any updates to the working directory.
......@@ -29,7 +29,7 @@ The simple error API
- `void giterr_set(git_error **, int, const char *, ...)`: the main function used to set an error. It allocates a new error object and stores it in the passed error pointer. It has no return value. The arguments for `giterr_set` are as follows:
- `git_error **error_ptr`: the pointer where the error will be created.
- `int error_class`: the class for the error. This is **not** an error code: this is an speficic enum that specifies the error family. The point is to map these families 1-1 with Exception types on higher level languages (e.g. GitRepositoryException)
- `int error_class`: the class for the error. This is **not** an error code: this is an specific enum that specifies the error family. The point is to map these families 1-1 with Exception types on higher level languages (e.g. GitRepositoryException)
- `const char *error_str, ...`: the error string, with optional formatting arguments
- `void giterr_free(git_error *)`: takes an error and frees it. This function is available in the external API.
......@@ -56,7 +56,7 @@ Here are some guidelines when writing error messages:
- Use short, direct and objective messages. **One line, max**. libgit2 is a low level library: think that all the messages reported will be thrown as Ruby or Python exceptions. Think how long are common exception messages in those languages.
- **Do not add redundant information to the error message**, specially information that can be infered from the context.
- **Do not add redundant information to the error message**, specially information that can be inferred from the context.
E.g. in `git_repository_open`, do not report a message like "Failed to open repository: path not found". Somebody is
calling that function. If it fails, he already knows that the repository failed to open!
......
general
showindex
diff
rev-list
*.dSYM
.PHONY: all
CC = gcc
CFLAGS = -g -I../include -I../src
CFLAGS = -g -I../include -I../src -Wall -Wextra -Wmissing-prototypes -Wno-missing-field-initializers
LFLAGS = -L../build -lgit2 -lz
APPS = general showindex diff
APPS = general showindex diff rev-list
all: $(APPS)
......
libgit2 examples
================
These examples are meant as thin, easy-to-read snippets for Docurium
(https://github.com/github/docurium) rather than full-blown
implementations of Git commands. They are not vetted as carefully
for bugs, error handling, or cross-platform compatibility as the
rest of the code in libgit2, so copy with some caution.
For HTML versions, check "Examples" at http://libgit2.github.com/libgit2
......@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>
void check(int error, const char *message)
static void check(int error, const char *message)
{
if (error) {
fprintf(stderr, "%s (%d)\n", message, error);
......@@ -11,32 +11,14 @@ void check(int error, const char *message)
}
}
int resolve_to_tree(git_repository *repo, const char *identifier, git_tree **tree)
static int resolve_to_tree(
git_repository *repo, const char *identifier, git_tree **tree)
{
int err = 0;
size_t len = strlen(identifier);
git_oid oid;
git_object *obj = NULL;
/* try to resolve as OID */
if (git_oid_fromstrn(&oid, identifier, len) == 0)
git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY);
/* try to resolve as reference */
if (obj == NULL) {
git_reference *ref, *resolved;
if (git_reference_lookup(&ref, repo, identifier) == 0) {
git_reference_resolve(&resolved, ref);
git_reference_free(ref);
if (resolved) {
git_object_lookup(&obj, repo, git_reference_oid(resolved), GIT_OBJ_ANY);
git_reference_free(resolved);
}
}
}
if (obj == NULL)
return GIT_ENOTFOUND;
if ((err = git_revparse_single(&obj, repo, identifier)) < 0)
return err;
switch (git_object_type(obj)) {
case GIT_OBJ_TREE:
......@@ -61,16 +43,18 @@ char *colors[] = {
"\033[36m" /* cyan */
};
int printer(
void *data,
git_diff_delta *delta,
git_diff_range *range,
static int printer(
const git_diff_delta *delta,
const git_diff_range *range,
char usage,
const char *line,
size_t line_len)
size_t line_len,
void *data)
{
int *last_color = data, color = 0;
(void)delta; (void)range; (void)line_len;
if (*last_color >= 0) {
switch (usage) {
case GIT_DIFF_LINE_ADDITION: color = 3; break;
......@@ -93,7 +77,7 @@ int printer(
return 0;
}
int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
{
size_t len = strlen(pattern);
uint16_t strval;
......@@ -107,16 +91,16 @@ int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
return 1;
}
int check_str_param(const char *arg, const char *pattern, char **val)
static int check_str_param(const char *arg, const char *pattern, const char **val)
{
size_t len = strlen(pattern);
if (strncmp(arg, pattern, len))
return 0;
*val = (char *)(arg + len);
*val = (const char *)(arg + len);
return 1;
}
void usage(const char *message, const char *arg)
static void usage(const char *message, const char *arg)
{
if (message && arg)
fprintf(stderr, "%s: %s\n", message, arg);
......@@ -128,10 +112,9 @@ void usage(const char *message, const char *arg)
int main(int argc, char *argv[])
{
char path[GIT_PATH_MAX];
git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL;
git_diff_options opts = {0};
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_list *diff;
int i, color = -1, compact = 0, cached = 0;
char *a, *dir = ".", *treeish1 = NULL, *treeish2 = NULL;
......@@ -185,9 +168,7 @@ int main(int argc, char *argv[])
/* open repo */
check(git_repository_discover(path, sizeof(path), dir, 0, "/"),
"Could not discover repository");
check(git_repository_open(&repo, path),
check(git_repository_open_ext(&repo, dir, 0, NULL),
"Could not open repository");
if (treeish1)
......@@ -202,30 +183,30 @@ int main(int argc, char *argv[])
/* nothing */
if (t1 && t2)
check(git_diff_tree_to_tree(repo, &opts, t1, t2, &diff), "Diff");
check(git_diff_tree_to_tree(&diff, repo, t1, t2, &opts), "Diff");
else if (t1 && cached)
check(git_diff_index_to_tree(repo, &opts, t1, &diff), "Diff");
check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
else if (t1) {
git_diff_list *diff2;
check(git_diff_index_to_tree(repo, &opts, t1, &diff), "Diff");
check(git_diff_workdir_to_index(repo, &opts, &diff2), "Diff");
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);
}
else if (cached) {
check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD");
check(git_diff_index_to_tree(repo, &opts, t1, &diff), "Diff");
check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
}
else
check(git_diff_workdir_to_index(repo, &opts, &diff), "Diff");
check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff");
if (color >= 0)
fputs(colors[0], stdout);
if (compact)
check(git_diff_print_compact(diff, &color, printer), "Displaying diff");
check(git_diff_print_compact(diff, printer, &color), "Displaying diff");
else
check(git_diff_print_patch(diff, &color, printer), "Displaying diff");
check(git_diff_print_patch(diff, printer, &color), "Displaying diff");
if (color >= 0)
fputs(colors[0], stdout);
......
......@@ -2,13 +2,20 @@ default: all
CC = gcc
CFLAGS += -g
CFLAGS += -I../../include -L../../ -lgit2 -lpthread
CFLAGS += -I../../include
LDFLAGS += -L../../build -L../..
LIBRARIES += -lgit2 -lpthread
OBJECTS = \
git2.o \
ls-remote.o \
fetch.o \
clone.o \
index-pack.o
all: $(OBJECTS)
$(CC) $(CFLAGS) -o git2 $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -o git2 $(OBJECTS) $(LIBRARIES)
clean:
$(RM) $(OBJECTS)
$(RM) git2
#include "common.h"
#include <git2.h>
#include <git2/clone.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
# include <pthread.h>
# include <unistd.h>
#endif
/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
* with permission of the original author, Martin Pool.
* http://sourcefrog.net/weblog/software/languages/C/unused.html
*/
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
typedef struct progress_data {
git_transfer_progress fetch_progress;
size_t completed_steps;
size_t total_steps;
const char *path;
} progress_data;
static void print_progress(const progress_data *pd)
{
int network_percent = (100*pd->fetch_progress.received_objects) / pd->fetch_progress.total_objects;
int index_percent = (100*pd->fetch_progress.indexed_objects) / pd->fetch_progress.total_objects;
int checkout_percent = pd->total_steps > 0
? (100 * pd->completed_steps) / pd->total_steps
: 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",
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)
{
progress_data *pd = (progress_data*)payload;
pd->fetch_progress = *stats;
print_progress(pd);
return 0;
}
static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
{
progress_data *pd = (progress_data*)payload;
pd->completed_steps = cur;
pd->total_steps = tot;
pd->path = path;
print_progress(pd);
}
static int cred_acquire(git_cred **out,
const char * UNUSED(url),
const char * UNUSED(username_from_url),
unsigned int UNUSED(allowed_types),
void * UNUSED(payload))
{
char username[128] = {0};
char password[128] = {0};
printf("Username: ");
scanf("%s", username);
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
printf("Password: ");
scanf("%s", password);
return git_cred_userpass_plaintext_new(out, username, password);
}
int do_clone(git_repository *repo, int argc, char **argv)
{
progress_data pd = {{0}};
git_repository *cloned_repo = NULL;
git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
const char *url = argv[1];
const char *path = argv[2];
int error;
(void)repo; // unused
// Validate args
if (argc < 3) {
printf ("USAGE: %s <url> <path>\n", argv[0]);
return -1;
}
// Set up options
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
checkout_opts.progress_cb = checkout_progress;
checkout_opts.progress_payload = &pd;
clone_opts.checkout_opts = checkout_opts;
clone_opts.fetch_progress_cb = &fetch_progress;
clone_opts.fetch_progress_payload = &pd;
clone_opts.cred_acquire_cb = cred_acquire;
// Do the clone
error = git_clone(&cloned_repo, url, path, &clone_opts);
printf("\n");
if (error != 0) {
const git_error *err = giterr_last();
if (err) printf("ERROR %d: %s\n", err->klass, err->message);
else printf("ERROR %d: no detailed info\n", error);
}
else if (cloned_repo) git_repository_free(cloned_repo);
return error;
}
......@@ -10,5 +10,15 @@ int parse_pkt_line(git_repository *repo, int argc, char **argv);
int show_remote(git_repository *repo, int argc, char **argv);
int fetch(git_repository *repo, int argc, char **argv);
int index_pack(git_repository *repo, int argc, char **argv);
int do_clone(git_repository *repo, int argc, char **argv);
#ifndef PRIuZ
/* Define the printf format specifer to use for size_t output */
#if defined(_MSC_VER) || defined(__MINGW32__)
# define PRIuZ "Iu"
#else
# define PRIuZ "zu"
#endif
#endif
#endif /* __COMMON_H__ */
......@@ -3,23 +3,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#ifndef _WIN32
# include <pthread.h>
# include <unistd.h>
#endif
struct dl_data {
git_remote *remote;
git_off_t *bytes;
git_indexer_stats *stats;
int ret;
int finished;
};
static void progress_cb(const char *str, int len, void *data)
{
(void)data;
printf("remote: %.*s", len, str);
fflush(stdout); /* We don't have the \n to force the flush */
}
static void *download(void *ptr)
{
struct dl_data *data = (struct dl_data *)ptr;
// Connect to the remote end specifying that we want to fetch
// information from it.
if (git_remote_connect(data->remote, GIT_DIR_FETCH) < 0) {
if (git_remote_connect(data->remote, GIT_DIRECTION_FETCH) < 0) {
data->ret = -1;
goto exit;
}
......@@ -27,7 +35,7 @@ static void *download(void *ptr)
// Download the packfile and index it. This function updates the
// amount of received data and the indexer stats which lets you
// inform the user about progress.
if (git_remote_download(data->remote, data->bytes, data->stats) < 0) {
if (git_remote_download(data->remote, NULL, NULL) < 0) {
data->ret = -1;
goto exit;
}
......@@ -36,13 +44,13 @@ static void *download(void *ptr)
exit:
data->finished = 1;
pthread_exit(&data->ret);
return &data->ret;
}
int update_cb(const char *refname, const git_oid *a, const git_oid *b)
static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
const char *action;
char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
(void)data;
git_oid_fmt(b_str, b);
b_str[GIT_OID_HEXSZ] = '\0';
......@@ -60,54 +68,80 @@ int update_cb(const char *refname, const git_oid *a, const git_oid *b)
int fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
git_off_t bytes = 0;
git_indexer_stats stats;
pthread_t worker;
struct dl_data data;
// Figure out whether it's a named remote or a URL
printf("Fetching %s\n", argv[1]);
if (git_remote_load(&remote, repo, argv[1]) < 0) {
if (git_remote_new(&remote, repo, NULL, argv[1], NULL) < 0)
return -1;
}
// Set up the information for the background worker thread
data.remote = remote;
data.bytes = &bytes;
data.stats = &stats;
data.ret = 0;
data.finished = 0;
memset(&stats, 0, sizeof(stats));
pthread_create(&worker, NULL, download, &data);
// Loop while the worker thread is still running. Here we show processed
// and total objects in the pack and the amount of received
// data. Most frontends will probably want to show a percentage and
// the download rate.
do {
usleep(10000);
printf("\rReceived %d/%d objects in %d bytes", stats.processed, stats.total, bytes);
} while (!data.finished);
printf("\rReceived %d/%d objects in %d bytes\n", stats.processed, stats.total, bytes);
// Disconnect the underlying connection to prevent from idling.
git_remote_disconnect(remote);
// Update the references in the remote's namespace to point to the
// right commits. This may be needed even if there was no packfile
// to download, which can happen e.g. when the branches have been
// changed but all the neede objects are available locally.
if (git_remote_update_tips(remote, update_cb) < 0)
return -1;
git_remote_free(remote);
return 0;
on_error:
git_remote_free(remote);
return -1;
git_remote *remote = NULL;
const git_transfer_progress *stats;
struct dl_data data;
git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
#ifndef _WIN32
pthread_t worker;
#endif
if (argc < 2) {
fprintf(stderr, "usage: %s fetch <repo>\n", argv[-1]);
return EXIT_FAILURE;
}
// Figure out whether it's a named remote or a URL
printf("Fetching %s for repo %p\n", argv[1], repo);
if (git_remote_load(&remote, repo, argv[1]) < 0) {
if (git_remote_create_inmemory(&remote, repo, NULL, argv[1]) < 0)
return -1;
}
// Set up the callbacks (only update_tips for now)
callbacks.update_tips = &update_cb;
callbacks.progress = &progress_cb;
git_remote_set_callbacks(remote, &callbacks);
// Set up the information for the background worker thread
data.remote = remote;
data.ret = 0;
data.finished = 0;
stats = git_remote_stats(remote);
#ifdef _WIN32
download(&data);
#else
pthread_create(&worker, NULL, download, &data);
// Loop while the worker thread is still running. Here we show processed
// and total objects in the pack and the amount of received
// data. Most frontends will probably want to show a percentage and
// the download rate.
do {
usleep(10000);
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)
goto on_error;
pthread_join(worker, NULL);
#endif
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);
// Update the references in the remote's namespace to point to the
// right commits. This may be needed even if there was no packfile
// to download, which can happen e.g. when the branches have been
// changed but all the neede objects are available locally.
if (git_remote_update_tips(remote) < 0)
return -1;
git_remote_free(remote);
return 0;
on_error:
git_remote_free(remote);
return -1;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
......@@ -12,11 +13,12 @@ struct {
} commands[] = {
{"ls-remote", ls_remote},
{"fetch", fetch},
{"clone", do_clone},
{"index-pack", index_pack},
{ NULL, NULL}
};
int run_command(git_cb fn, int argc, char **argv)
static int run_command(git_cb fn, int argc, char **argv)
{
int error;
git_repository *repo;
......@@ -45,7 +47,7 @@ int run_command(git_cb fn, int argc, char **argv)
int main(int argc, char **argv)
{
int i, error;
int i;
if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
......
......@@ -2,101 +2,87 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _WIN32
# include <io.h>
# include <Windows.h>
# define open _open
# define read _read
# define close _close
#define ssize_t unsigned int
#else
# include <unistd.h>
#endif
#include "common.h"
// This could be run in the main loop whilst the application waits for
// the indexing to finish in a worker thread
int index_cb(const git_indexer_stats *stats, void *data)
static int index_cb(const git_transfer_progress *stats, void *data)
{
printf("\rProcessing %d of %d", stats->processed, stats->total);
}
(void)data;
printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects);
int index_pack(git_repository *repo, int argc, char **argv)
{
git_indexer_stream *idx;
git_indexer_stats stats = {0, 0};
int error, fd;
char hash[GIT_OID_HEXSZ + 1] = {0};
ssize_t read_bytes;
char buf[512];
if (argc < 2) {
fprintf(stderr, "I need a packfile\n");
return EXIT_FAILURE;
}
if (git_indexer_stream_new(&idx, ".git") < 0) {
puts("bad idx");
return -1;
}
if ((fd = open(argv[1], 0)) < 0) {
perror("open");
return -1;
}
do {
read_bytes = read(fd, buf, sizeof(buf));
if (read_bytes < 0)
break;
if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0)
goto cleanup;
printf("\rIndexing %d of %d", stats.processed, stats.total);
} while (read_bytes > 0);
if (read_bytes < 0) {
error = -1;
perror("failed reading");
goto cleanup;
}
if ((error = git_indexer_stream_finalize(idx, &stats)) < 0)
goto cleanup;
printf("\rIndexing %d of %d\n", stats.processed, stats.total);
git_oid_fmt(hash, git_indexer_stream_hash(idx));
puts(hash);
cleanup:
close(fd);
git_indexer_stream_free(idx);
return error;
return 0;
}
int index_pack_old(git_repository *repo, int argc, char **argv)
int index_pack(git_repository *repo, int argc, char **argv)
{
git_indexer *indexer;
git_indexer_stats stats;
int error;
char hash[GIT_OID_HEXSZ + 1] = {0};
if (argc < 2) {
fprintf(stderr, "I need a packfile\n");
return EXIT_FAILURE;
}
// Create a new indexer
error = git_indexer_new(&indexer, argv[1]);
if (error < 0)
return error;
// Index the packfile. This function can take a very long time and
// should be run in a worker thread.
error = git_indexer_run(indexer, &stats);
if (error < 0)
return error;
// Write the information out to an index file
error = git_indexer_write(indexer);
// Get the packfile's hash (which should become it's filename)
git_oid_fmt(hash, git_indexer_hash(indexer));
puts(hash);
git_indexer_free(indexer);
return 0;
git_indexer_stream *idx;
git_transfer_progress stats = {0, 0};
int error;
char hash[GIT_OID_HEXSZ + 1] = {0};
int fd;
ssize_t read_bytes;
char buf[512];
(void)repo;
if (argc < 2) {
fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
return EXIT_FAILURE;
}
if (git_indexer_stream_new(&idx, ".", NULL, NULL) < 0) {
puts("bad idx");
return -1;
}
if ((fd = open(argv[1], 0)) < 0) {
perror("open");
return -1;
}
do {
read_bytes = read(fd, buf, sizeof(buf));
if (read_bytes < 0)
break;
if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0)
goto cleanup;
index_cb(&stats, NULL);
} while (read_bytes > 0);
if (read_bytes < 0) {
error = -1;
perror("failed reading");
goto cleanup;
}
if ((error = git_indexer_stream_finalize(idx, &stats)) < 0)
goto cleanup;
printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects);
git_oid_fmt(hash, git_indexer_stream_hash(idx));
puts(hash);
cleanup:
close(fd);
git_indexer_stream_free(idx);
return error;
}
......@@ -7,25 +7,27 @@
static int show_ref__cb(git_remote_head *head, void *payload)
{
char oid[GIT_OID_HEXSZ + 1] = {0};
(void)payload;
git_oid_fmt(oid, &head->oid);
printf("%s\t%s\n", oid, head->name);
return 0;
}
int use_unnamed(git_repository *repo, const char *url)
static int use_unnamed(git_repository *repo, const char *url)
{
git_remote *remote = NULL;
int error;
// Create an instance of a remote from the URL. The transport to use
// is detected from the URL
error = git_remote_new(&remote, repo, NULL, url, NULL);
error = git_remote_create_inmemory(&remote, repo, NULL, url);
if (error < 0)
goto cleanup;
// When connecting, the underlying code needs to know wether we
// want to push or fetch
error = git_remote_connect(remote, GIT_DIR_FETCH);
error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
if (error < 0)
goto cleanup;
......@@ -37,7 +39,7 @@ cleanup:
return error;
}
int use_remote(git_repository *repo, char *name)
static int use_remote(git_repository *repo, char *name)
{
git_remote *remote = NULL;
int error;
......@@ -47,7 +49,7 @@ int use_remote(git_repository *repo, char *name)
if (error < 0)
goto cleanup;
error = git_remote_connect(remote, GIT_DIR_FETCH);
error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
if (error < 0)
goto cleanup;
......@@ -63,7 +65,12 @@ cleanup:
int ls_remote(git_repository *repo, int argc, char **argv)
{
int error, i;
int error;
if (argc < 2) {
fprintf(stderr, "usage: %s ls-remote <remote>\n", argv[-1]);
return EXIT_FAILURE;
}
/* If there's a ':' in the name, assume it's an URL */
if (strchr(argv[1], ':') != NULL) {
......
#include <stdio.h>
#include <string.h>
#include <git2.h>
static void check_error(int error_code, const char *action)
{
if (!error_code)
return;
const git_error *error = giterr_last();
fprintf(stderr, "Error %d %s: %s\n", -error_code, action,
(error && error->message) ? error->message : "???");
exit(1);
}
static int push_commit(git_revwalk *walk, const git_oid *oid, int hide)
{
if (hide)
return git_revwalk_hide(walk, oid);
else
return git_revwalk_push(walk, oid);
}
static int push_spec(git_repository *repo, git_revwalk *walk, const char *spec, int hide)
{
int error;
git_object *obj;
if ((error = git_revparse_single(&obj, repo, spec)) < 0)
return error;
error = push_commit(walk, git_object_id(obj), hide);
git_object_free(obj);
return error;
}
static int push_range(git_repository *repo, git_revwalk *walk, const char *range, int hide)
{
git_revspec revspec;
int error = 0;
if ((error = git_revparse(&revspec, repo, range)))
return error;
if (revspec.flags & GIT_REVPARSE_MERGE_BASE) {
/* TODO: support "<commit>...<commit>" */
return GIT_EINVALIDSPEC;
}
if ((error = push_commit(walk, git_object_id(revspec.from), !hide)))
goto out;
error = push_commit(walk, git_object_id(revspec.to), hide);
out:
git_object_free(revspec.from);
git_object_free(revspec.to);
return error;
}
static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts)
{
int hide, i, error;
unsigned int sorting = GIT_SORT_NONE;
hide = 0;
for (i = 0; i < nopts; i++) {
if (!strcmp(opts[i], "--topo-order")) {
sorting = GIT_SORT_TOPOLOGICAL | (sorting & GIT_SORT_REVERSE);
git_revwalk_sorting(walk, sorting);
} else if (!strcmp(opts[i], "--date-order")) {
sorting = GIT_SORT_TIME | (sorting & GIT_SORT_REVERSE);
git_revwalk_sorting(walk, sorting);
} else if (!strcmp(opts[i], "--reverse")) {
sorting = (sorting & ~GIT_SORT_REVERSE)
| ((sorting & GIT_SORT_REVERSE) ? 0 : GIT_SORT_REVERSE);
git_revwalk_sorting(walk, sorting);
} else if (!strcmp(opts[i], "--not")) {
hide = !hide;
} else if (opts[i][0] == '^') {
if ((error = push_spec(repo, walk, opts[i] + 1, !hide)))
return error;
} else if (strstr(opts[i], "..")) {
if ((error = push_range(repo, walk, opts[i], hide)))
return error;
} else {
if ((error = push_spec(repo, walk, opts[i], hide)))
return error;
}
}
return 0;
}
int main (int argc, char **argv)
{
int error;
git_repository *repo;
git_revwalk *walk;
git_oid oid;
char buf[41];
error = git_repository_open_ext(&repo, ".", 0, NULL);
check_error(error, "opening repository");
error = git_revwalk_new(&walk, repo);
check_error(error, "allocating revwalk");
error = revwalk_parseopts(repo, walk, argc-1, argv+1);
check_error(error, "parsing options");
while (!git_revwalk_next(&oid, walk)) {
git_oid_fmt(buf, &oid);
buf[40] = '\0';
printf("%s\n", buf);
}
return 0;
}
#include <git2.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char** argv)
{
git_repository *repo;
git_index *index;
unsigned int i, e, ecount;
git_index_entry **entries;
git_oid oid;
char out[41];
out[40] = '\0';
git_repository_open(&repo, "/opt/libgit2-test/.git");
git_repository_index(&index, repo);
git_index_read(index);
ecount = git_index_entrycount(index);
for (i = 0; i < ecount; ++i) {
git_index_entry *e = git_index_get(index, i);
oid = e->oid;
git_oid_fmt(out, &oid);
printf("File Path: %s\n", e->path);
printf(" Blob SHA: %s\n", out);
printf("File Size: %d\n", (int)e->file_size);
printf(" Device: %d\n", (int)e->dev);
printf(" Inode: %d\n", (int)e->ino);
printf(" UID: %d\n", (int)e->uid);
printf(" GID: %d\n", (int)e->gid);
printf(" ctime: %d\n", (int)e->ctime.seconds);
printf(" mtime: %d\n", (int)e->mtime.seconds);
printf("\n");
}
git_index_free(index);
git_repository_free(repo);
git_repository *repo = NULL;
git_index *index;
unsigned int i, ecount;
char *dir = ".";
size_t dirlen;
char out[41];
out[40] = '\0';
if (argc > 1)
dir = argv[1];
if (!dir || argc > 2) {
fprintf(stderr, "usage: showindex [<repo-dir>]\n");
return 1;
}
dirlen = strlen(dir);
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
if (git_index_open(&index, dir) < 0) {
fprintf(stderr, "could not open index: %s\n", dir);
return 1;
}
} else {
if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) {
fprintf(stderr, "could not open repository: %s\n", dir);
return 1;
}
if (git_repository_index(&index, repo) < 0) {
fprintf(stderr, "could not open repository index\n");
return 1;
}
}
git_index_read(index);
ecount = git_index_entrycount(index);
if (!ecount)
printf("Empty index\n");
for (i = 0; i < ecount; ++i) {
const git_index_entry *e = git_index_get_byindex(index, i);
git_oid_fmt(out, &e->oid);
printf("File Path: %s\n", e->path);
printf(" Stage: %d\n", git_index_entry_stage(e));
printf(" Blob SHA: %s\n", out);
printf("File Mode: %07o\n", e->mode);
printf("File Size: %d bytes\n", (int)e->file_size);
printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
printf(" UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
printf(" ctime: %d\n", (int)e->ctime.seconds);
printf(" mtime: %d\n", (int)e->mtime.seconds);
printf("\n");
}
git_index_free(index);
git_repository_free(repo);
return 0;
}
#!/bin/bash
THIS_FILE="$(readlink -f "$0")"
ROOT="$(dirname "$(dirname "$(dirname "$THIS_FILE")")")"
PROGRAM="$ROOT"/examples/rev-list
LIBDIR="$ROOT"/build
REPO="$ROOT"/tests-clar/resources/testrepo.git
cd "$REPO"
run () {
LD_LIBRARY_PATH="$LIBDIR" "$PROGRAM" "$@"
}
diff -u - <(run --date-order a4a7dce) <<EOF
a4a7dce85cf63874e984719f4fdd239f5145052f
c47800c7266a2be04c571c04d5a6614691ea99bd
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
out="$(run --topo-order a4a7dce)"
diff -q - <(echo -n "$out") <<EOF >/dev/null ||
a4a7dce85cf63874e984719f4fdd239f5145052f
c47800c7266a2be04c571c04d5a6614691ea99bd
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(echo "$out") <<EOF
a4a7dce85cf63874e984719f4fdd239f5145052f
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
c47800c7266a2be04c571c04d5a6614691ea99bd
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(run --date-order --reverse a4a7dce) <<EOF
8496071c1b46c854b31185ea97743be6a8774479
5b5b025afb0b4c913b4c338a42934a3863bf3644
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
9fd738e8f7967c078dceed8190330fc8648ee56a
c47800c7266a2be04c571c04d5a6614691ea99bd
a4a7dce85cf63874e984719f4fdd239f5145052f
EOF
out=$(run --topo-order --reverse a4a7dce)
diff -q - <(echo -n "$out") <<EOF >/dev/null ||
8496071c1b46c854b31185ea97743be6a8774479
5b5b025afb0b4c913b4c338a42934a3863bf3644
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
9fd738e8f7967c078dceed8190330fc8648ee56a
c47800c7266a2be04c571c04d5a6614691ea99bd
a4a7dce85cf63874e984719f4fdd239f5145052f
EOF
diff -u - <(echo "$out") <<EOF
8496071c1b46c854b31185ea97743be6a8774479
5b5b025afb0b4c913b4c338a42934a3863bf3644
c47800c7266a2be04c571c04d5a6614691ea99bd
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
9fd738e8f7967c078dceed8190330fc8648ee56a
a4a7dce85cf63874e984719f4fdd239f5145052f
EOF
out="$(run --date-order --topo-order --reverse --reverse a4a7dce)"
diff -q - <(echo -n "$out") <<EOF >/dev/null ||
a4a7dce85cf63874e984719f4fdd239f5145052f
c47800c7266a2be04c571c04d5a6614691ea99bd
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(echo "$out") <<EOF
a4a7dce85cf63874e984719f4fdd239f5145052f
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
c47800c7266a2be04c571c04d5a6614691ea99bd
5b5b025afb0b4c913b4c338a42934a3863bf3644
8496071c1b46c854b31185ea97743be6a8774479
EOF
diff -u - <(run ^9fd738e~2 9fd738e) <<EOF
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
EOF
diff -u - <(run --not 9fd738e..9fd738e~2) <<EOF
9fd738e8f7967c078dceed8190330fc8648ee56a
4a202b346bb0fb0db7eff3cffeb3c70babbd2045
EOF
......@@ -41,9 +41,12 @@
ok Adam Simpkins <adam@adamsimpkins.net> (http transport)
ok Andreas Ericsson <ae@op5.se>
ok Boyd Lynn Gerber <gerberb@zenez.com>
ok Brian Downing <bdowning@lavos.net>
ok Brian Gernhardt <benji@silverinsanity.com>
ok Christian Couder <chriscool@tuxfamily.org>
ok Daniel Barkalow <barkalow@iabervon.org>
ok Florian Forster <octo@verplant.org>
ok Holger Weiss <holger@zedat.fu-berlin.de>
ok Jeff King <peff@peff.net>
ok Johannes Schindelin <Johannes.Schindelin@gmx.de>
ok Johannes Sixt <j6t@kdbg.org>
......@@ -53,7 +56,7 @@ ok Linus Torvalds <torvalds@linux-foundation.org>
ok Lukas Sandström <lukass@etek.chalmers.se>
ok Matthieu Moy <Matthieu.Moy@imag.fr>
ign Mike McCormack <mike@codeweavers.com> (imap-send)
ok Nicolas Pitre <nico@cam.org>
ok Nicolas Pitre <nico@fluxnic.net> <nico@cam.org>
ok Paolo Bonzini <bonzini@gnu.org>
ok Paul Kocher <paul@cryptography.com>
ok Peter Hagervall <hager@cs.umu.se>
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -23,8 +23,10 @@
#include "git2/repository.h"
#include "git2/revwalk.h"
#include "git2/merge.h"
#include "git2/graph.h"
#include "git2/refs.h"
#include "git2/reflog.h"
#include "git2/revparse.h"
#include "git2/object.h"
#include "git2/blob.h"
......@@ -35,13 +37,24 @@
#include "git2/index.h"
#include "git2/config.h"
#include "git2/transport.h"
#include "git2/remote.h"
#include "git2/clone.h"
#include "git2/checkout.h"
#include "git2/push.h"
#include "git2/attr.h"
#include "git2/ignore.h"
#include "git2/branch.h"
#include "git2/refspec.h"
#include "git2/net.h"
#include "git2/status.h"
#include "git2/indexer.h"
#include "git2/submodule.h"
#include "git2/notes.h"
#include "git2/reset.h"
#include "git2/message.h"
#include "git2/pack.h"
#include "git2/stash.h"
#endif
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -30,7 +30,7 @@ GIT_BEGIN_DECL
* Then for file `xyz.c` looking up attribute "foo" gives a value for
* which `GIT_ATTR_TRUE(value)` is true.
*/
#define GIT_ATTR_TRUE(attr) ((attr) == git_attr__true)
#define GIT_ATTR_TRUE(attr) (git_attr_value(attr) == GIT_ATTR_TRUE_T)
/**
* GIT_ATTR_FALSE checks if an attribute is set off. In core git
......@@ -44,7 +44,7 @@ GIT_BEGIN_DECL
* Then for file `zyx.h` looking up attribute "foo" gives a value for
* which `GIT_ATTR_FALSE(value)` is true.
*/
#define GIT_ATTR_FALSE(attr) ((attr) == git_attr__false)
#define GIT_ATTR_FALSE(attr) (git_attr_value(attr) == GIT_ATTR_FALSE_T)
/**
* GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This
......@@ -62,11 +62,11 @@ GIT_BEGIN_DECL
* file `onefile.rb` or looking up "bar" on any file will all give
* `GIT_ATTR_UNSPECIFIED(value)` of true.
*/
#define GIT_ATTR_UNSPECIFIED(attr) (!(attr) || (attr) == git_attr__unset)
#define GIT_ATTR_UNSPECIFIED(attr) (git_attr_value(attr) == GIT_ATTR_UNSPECIFIED_T)
/**
* GIT_ATTR_HAS_VALUE checks if an attribute is set to a value (as
* opposied to TRUE, FALSE or UNSPECIFIED). This would be the case if
* opposed to TRUE, FALSE or UNSPECIFIED). This would be the case if
* for a file with something like:
*
* *.txt eol=lf
......@@ -74,13 +74,29 @@ GIT_BEGIN_DECL
* Given this, looking up "eol" for `onefile.txt` will give back the
* string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true.
*/
#define GIT_ATTR_HAS_VALUE(attr) \
((attr) && (attr) != git_attr__unset && \
(attr) != git_attr__true && (attr) != git_attr__false)
#define GIT_ATTR_HAS_VALUE(attr) (git_attr_value(attr) == GIT_ATTR_VALUE_T)
GIT_EXTERN(const char *) git_attr__true;
GIT_EXTERN(const char *) git_attr__false;
GIT_EXTERN(const char *) git_attr__unset;
typedef enum {
GIT_ATTR_UNSPECIFIED_T = 0,
GIT_ATTR_TRUE_T,
GIT_ATTR_FALSE_T,
GIT_ATTR_VALUE_T,
} git_attr_t;
/*
* Return the value type for a given attribute.
*
* This can be either `TRUE`, `FALSE`, `UNSPECIFIED` (if the attribute
* was not set at all), or `VALUE`, if the attribute was set to
* an actual string.
*
* If the attribute has a `VALUE` string, it can be accessed normally
* as a NULL-terminated C string.
*
* @param attr The attribute
* @return the value type for the attribute
*/
GIT_EXTERN(git_attr_t) git_attr_value(const char *attr);
/**
* Check attribute flags: Reading values from index and working directory.
......@@ -167,29 +183,30 @@ GIT_EXTERN(int) git_attr_get_many(
size_t num_attr,
const char **names);
typedef int (*git_attr_foreach_cb)(const char *name, const char *value, void *payload);
/**
* Loop over all the git attributes for a path.
*
* @param repo The repository containing the path.
* @param flags A combination of GIT_ATTR_CHECK... flags.
* @param path The path inside the repo to check attributes. This
* does not have to exist, but if it does not, then
* it will be treated as a plain file (i.e. not a directory).
* @param callback The function that will be invoked on each attribute
* and attribute value. The name parameter will be the name
* of the attribute and the value will be the value it is
* set to, including possibly NULL if the attribute is
* explicitly set to UNSPECIFIED using the ! sign. This
* will be invoked only once per attribute name, even if
* there are multiple rules for a given file. The highest
* priority rule will be used.
* @param path Path inside the repo to check attributes. This does not have
* to exist, but if it does not, then it will be treated as a
* plain file (i.e. not a directory).
* @param callback Function to invoke on each attribute name and value. The
* value may be NULL is the attribute is explicitly set to
* UNSPECIFIED using the '!' sign. Callback will be invoked
* only once per attribute name, even if there are multiple
* rules for a given file. The highest priority rule will be
* used. Return a non-zero value from this to stop looping.
* @param payload Passed on as extra parameter to callback function.
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_attr_foreach(
git_repository *repo,
uint32_t flags,
const char *path,
int (*callback)(const char *name, const char *value, void *payload),
git_attr_foreach_cb callback,
void *payload);
/**
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -46,7 +46,7 @@ GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git
* @param len the length of the short identifier
* @return 0 or an error code
*/
GIT_INLINE(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, unsigned int len)
GIT_INLINE(int) git_blob_lookup_prefix(git_blob **blob, git_repository *repo, const git_oid *id, size_t len)
{
return git_object_lookup_prefix((git_object **)blob, repo, id, len, GIT_OBJ_BLOB);
}
......@@ -68,6 +68,17 @@ GIT_INLINE(void) git_blob_free(git_blob *blob)
git_object_free((git_object *) blob);
}
/**
* Get the id of a blob.
*
* @param blob a previously loaded blob.
* @return SHA1 hash for this blob.
*/
GIT_INLINE(const git_oid *) git_blob_id(const git_blob *blob)
{
return git_object_id((const git_object *)blob);
}
/**
* Get a read-only buffer with the raw content of a blob.
......@@ -80,7 +91,7 @@ GIT_INLINE(void) git_blob_free(git_blob *blob)
* @param blob pointer to the blob
* @return the pointer; NULL if the blob has no contents
*/
GIT_EXTERN(const void *) git_blob_rawcontent(git_blob *blob);
GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
/**
* Get the size in bytes of the contents of a blob
......@@ -88,34 +99,79 @@ GIT_EXTERN(const void *) git_blob_rawcontent(git_blob *blob);
* @param blob pointer to the blob
* @return size on bytes
*/
GIT_EXTERN(size_t) git_blob_rawsize(git_blob *blob);
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
/**
* Read a file from the working folder of a repository
* and write it to the Object Database as a loose blob
*
* @param oid return the id of the written blob
* @param id return the id of the written blob
* @param repo repository where the blob will be written.
* this repository cannot be bare
* @param path file from which the blob will be created,
* @param relative_path file from which the blob will be created,
* relative to the repository's working dir
* @return 0 or an error code
*/
GIT_EXTERN(int) git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path);
GIT_EXTERN(int) git_blob_create_fromworkdir(git_oid *id, git_repository *repo, const char *relative_path);
/**
* Read a file from the filesystem and write its content
* to the Object Database as a loose blob
*
* @param oid return the id of the written blob
* @param id return the id of the written blob
* @param repo repository where the blob will be written.
* this repository can be bare or not
* @param path file from which the blob will be created
* @return 0 or an error code
*/
GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *oid, git_repository *repo, const char *path);
GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *id, git_repository *repo, const char *path);
typedef int (*git_blob_chunk_cb)(char *content, size_t max_length, void *payload);
/**
* Write a loose blob to the Object Database from a
* provider of chunks of data.
*
* Provided the `hintpath` parameter is filled, its value
* will help to determine what git filters should be applied
* to the object before it can be placed to the object database.
*
*
* The implementation of the callback has to respect the
* following rules:
*
* - `content` will have to be filled by the consumer. The maximum number
* of bytes that the buffer can accept per call is defined by the
* `max_length` parameter. Allocation and freeing of the buffer will be taken
* care of by the function.
*
* - The callback is expected to return the number of bytes
* that `content` have been filled with.
*
* - When there is no more data to stream, the callback should
* return 0. This will prevent it from being invoked anymore.
*
* - When an error occurs, the callback should return -1.
*
*
* @param id Return the id of the written blob
*
* @param repo repository where the blob will be written.
* This repository can be bare or not.
*
* @param hintpath if not NULL, will help selecting the filters
* to apply onto the content of the blob to be created.
*
* @return 0 or an error code
*/
GIT_EXTERN(int) git_blob_create_fromchunks(
git_oid *id,
git_repository *repo,
const char *hintpath,
git_blob_chunk_cb callback,
void *payload);
/**
* Write an in-memory buffer to the ODB as a blob
*
......@@ -127,6 +183,19 @@ GIT_EXTERN(int) git_blob_create_fromdisk(git_oid *oid, git_repository *repo, con
*/
GIT_EXTERN(int) git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len);
/**
* Determine if the blob content is most certainly binary or not.
*
* The heuristic used to guess if a file is binary is taken from core git:
* Searching for NUL bytes and looking for a reasonable ratio of printable
* to non-printable characters among the first 4000 bytes.
*
* @param blob The blob which content should be analyzed
* @return 1 if the content of the blob is detected
* as binary; 0 otherwise.
*/
GIT_EXTERN(int) git_blob_is_binary(git_blob *blob);
/** @} */
GIT_END_DECL
#endif
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -8,6 +8,7 @@
#define INCLUDE_git_branch_h__
#include "common.h"
#include "oid.h"
#include "types.h"
/**
......@@ -26,61 +27,53 @@ GIT_BEGIN_DECL
* this target commit. If `force` is true and a reference
* already exists with the given name, it'll be replaced.
*
* @param oid_out Pointer where to store the OID of the target commit.
* The returned reference must be freed by the user.
*
* @param repo Repository where to store the branch.
* The branch name will be checked for validity.
* See `git_tag_create()` for rules about valid names.
*
* @param out Pointer where to store the underlying reference.
*
* @param branch_name Name for the branch; this name is
* validated for consistency. It should also not conflict with
* an already existing branch name.
*
* @param target Object to which this branch should point. This object
* must belong to the given `repo` and can either be a git_commit or a
* git_tag. When a git_tag is being passed, it should be dereferencable
* to a git_commit which oid will be used as the target of the branch.
* @param target Commit to which this branch should point. This object
* must belong to the given `repo`.
*
* @param force Overwrite existing branch.
*
* @return 0 or an error code.
* @return 0, GIT_EINVALIDSPEC or an error code.
* A proper reference is written in the refs/heads namespace
* pointing to the provided target commit.
*/
GIT_EXTERN(int) git_branch_create(
git_oid *oid_out,
git_repository *repo,
const char *branch_name,
const git_object *target,
int force);
git_reference **out,
git_repository *repo,
const char *branch_name,
const git_commit *target,
int force);
/**
* Delete an existing branch reference.
*
* @param repo Repository where lives the branch.
*
* @param branch_name Name of the branch to be deleted;
* this name is validated for consistency.
*
* @param branch_type Type of the considered branch. This should
* be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE.
* If the branch is successfully deleted, the passed reference
* object will be freed and invalidated.
*
* @return 0 on success, GIT_ENOTFOUND if the branch
* doesn't exist or an error code.
* @param branch A valid reference representing a branch
* @return 0 on success, or an error code.
*/
GIT_EXTERN(int) git_branch_delete(
git_repository *repo,
const char *branch_name,
git_branch_t branch_type);
GIT_EXTERN(int) git_branch_delete(git_reference *branch);
typedef int (*git_branch_foreach_cb)(
const char *branch_name,
git_branch_t branch_type,
void *payload);
/**
* Fill a list with all the branches in the Repository
*
* The string array will be filled with the names of the
* matching branches; these values are owned by the user and
* should be free'd manually when no longer needed, using
* `git_strarray_free`.
* Loop over all the branches and issue a callback for each one.
*
* @param branch_names Pointer to a git_strarray structure
* where the branch names will be stored.
* If the callback returns a non-zero value, this will stop looping.
*
* @param repo Repository where to find the branches.
*
......@@ -88,34 +81,171 @@ GIT_EXTERN(int) git_branch_delete(
* listing. Valid values are GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE
* or a combination of the two.
*
* @return 0 or an error code.
* @param branch_cb Callback to invoke per found branch.
*
* @param payload Extra parameter to callback function.
*
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_branch_list(
git_strarray *branch_names,
git_repository *repo,
unsigned int list_flags);
GIT_EXTERN(int) git_branch_foreach(
git_repository *repo,
unsigned int list_flags,
git_branch_foreach_cb branch_cb,
void *payload);
/**
* Move/rename an existing branch reference.
* Move/rename an existing local branch reference.
*
* @param repo Repository where lives the branch.
* The new branch name will be checked for validity.
* See `git_tag_create()` for rules about valid names.
*
* @param old_branch_name Current name of the branch to be moved;
* this name is validated for consistency.
* @param branch Current underlying reference of the branch.
*
* @param new_branch_name Target name of the branch once the move
* is performed; this name is validated for consistency.
*
* @param force Overwrite existing branch.
*
* @return 0 on success, GIT_ENOTFOUND if the branch
* doesn't exist or an error code.
* @return 0 on success, GIT_EINVALIDSPEC or an error code.
*/
GIT_EXTERN(int) git_branch_move(
git_repository *repo,
const char *old_branch_name,
const char *new_branch_name,
int force);
git_reference **out,
git_reference *branch,
const char *new_branch_name,
int force);
/**
* Lookup a branch by its name in a repository.
*
* The generated reference must be freed by the user.
*
* The branch name will be checked for validity.
* See `git_tag_create()` for rules about valid names.
*
* @param out pointer to the looked-up branch reference
*
* @param repo the repository to look up the branch
*
* @param branch_name Name of the branch to be looked-up;
* this name is validated for consistency.
*
* @param branch_type Type of the considered branch. This should
* be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE.
*
* @return 0 on success; GIT_ENOTFOUND when no matching branch
* exists, GIT_EINVALIDSPEC, otherwise an error code.
*/
GIT_EXTERN(int) git_branch_lookup(
git_reference **out,
git_repository *repo,
const char *branch_name,
git_branch_t branch_type);
/**
* Return the name of the given local or remote branch.
*
* The name of the branch matches the definition of the name
* for git_branch_lookup. That is, if the returned name is given
* to git_branch_lookup() then the reference is returned that
* was given to this function.
*
* @param out where the pointer of branch name is stored;
* this is valid as long as the ref is not freed.
* @param ref the reference ideally pointing to a branch
*
* @return 0 on success; otherwise an error code (e.g., if the
* ref is no local or remote branch).
*/
GIT_EXTERN(int) git_branch_name(const char **out,
git_reference *ref);
/**
* Return the reference supporting the remote tracking branch,
* given a local branch reference.
*
* @param out Pointer where to store the retrieved
* reference.
*
* @param branch Current underlying reference of the branch.
*
* @return 0 on success; GIT_ENOTFOUND when no remote tracking
* reference exists, otherwise an error code.
*/
GIT_EXTERN(int) git_branch_upstream(
git_reference **out,
git_reference *branch);
/**
* Set the upstream configuration for a given local branch
*
* @param branch the branch to configure
*
* @param upstream_name remote-tracking or local branch to set as
* upstream. Pass NULL to unset.
*
* @return 0 or an error code
*/
GIT_EXTERN(int) git_branch_set_upstream(git_reference *branch, const char *upstream_name);
/**
* Return the name of the reference supporting the remote tracking branch,
* given the name of a local branch reference.
*
* @param tracking_branch_name_out The user-allocated buffer which will be
* filled with the name of the reference. Pass NULL if you just want to
* get the needed size of the name of the reference as the output value.
*
* @param buffer_size Size of the `out` buffer in bytes.
*
* @param repo the repository where the branches live
*
* @param canonical_branch_name name of the local branch.
*
* @return number of characters in the reference name
* including the trailing NUL byte; GIT_ENOTFOUND when no remote tracking
* reference exists, otherwise an error code.
*/
GIT_EXTERN(int) git_branch_upstream_name(
char *tracking_branch_name_out,
size_t buffer_size,
git_repository *repo,
const char *canonical_branch_name);
/**
* Determine if the current local branch is pointed at by HEAD.
*
* @param branch Current underlying reference of the branch.
*
* @return 1 if HEAD points at the branch, 0 if it isn't,
* error code otherwise.
*/
GIT_EXTERN(int) git_branch_is_head(
git_reference *branch);
/**
* Return the name of remote that the remote tracking branch belongs to.
*
* @param remote_name_out The user-allocated buffer which will be
* filled with the name of the remote. Pass NULL if you just want to
* get the needed size of the name of the remote as the output value.
*
* @param buffer_size Size of the `out` buffer in bytes.
*
* @param repo The repository where the branch lives.
*
* @param canonical_branch_name name of the remote tracking branch.
*
* @return Number of characters in the reference name
* including the trailing NUL byte; GIT_ENOTFOUND
* when no remote matching remote was gound,
* GIT_EAMBIGUOUS when the branch maps to several remotes,
* otherwise an error code.
*/
GIT_EXTERN(int) git_branch_remote_name(
char *remote_name_out,
size_t buffer_size,
git_repository *repo,
const char *canonical_branch_name);
/** @} */
GIT_END_DECL
......
/*
* 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_clone_h__
#define INCLUDE_git_clone_h__
#include "common.h"
#include "types.h"
#include "indexer.h"
#include "checkout.h"
#include "remote.h"
/**
* @file git2/clone.h
* @brief Git cloning routines
* @defgroup git_clone Git cloning routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Clone options structure
*
* Use zeros to indicate default settings. It's easiest to use the
* `GIT_CLONE_OPTIONS_INIT` macro:
*
* git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
*
* - `checkout_opts` is options for the checkout step. To disable checkout,
* set the `checkout_strategy` to GIT_CHECKOUT_DEFAULT.
* - `bare` should be set to zero to create a standard repo, non-zero for
* a bare repo
* - `fetch_progress_cb` is optional callback for fetch progress. Be aware that
* this is called inline with network and indexing operations, so performance
* may be affected.
* - `fetch_progress_payload` is payload for fetch_progress_cb
*
* ** "origin" remote options: **
* - `remote_name` is the name given to the "origin" remote. The default is
* "origin".
* - `pushurl` is a URL to be used for pushing. NULL means use the fetch url.
* - `fetch_spec` is the fetch specification to be used for fetching. NULL
* results in the same behavior as GIT_REMOTE_DEFAULT_FETCH.
* - `push_spec` is the fetch specification to be used for pushing. NULL means
* use the same spec as for fetching.
* - `cred_acquire_cb` is a callback to be used if credentials are required
* during the initial fetch.
* - `cred_acquire_payload` is the payload for the above callback.
* - `transport` is a custom transport to be used for the initial fetch. NULL
* means use the transport autodetected from the URL.
* - `remote_callbacks` may be used to specify custom progress callbacks for
* the origin remote before the fetch is initiated.
* - `remote_autotag` may be used to specify the autotag setting before the
* initial fetch. The default is GIT_REMOTE_DOWNLOAD_TAGS_ALL.
* - `checkout_branch` gives the name of the branch to checkout. NULL means
* use the remote's HEAD.
*/
typedef struct git_clone_options {
unsigned int version;
git_checkout_opts checkout_opts;
int bare;
git_transfer_progress_callback fetch_progress_cb;
void *fetch_progress_payload;
const char *remote_name;
const char *pushurl;
const char *fetch_spec;
const char *push_spec;
git_cred_acquire_cb cred_acquire_cb;
void *cred_acquire_payload;
git_transport *transport;
git_remote_callbacks *remote_callbacks;
git_remote_autotag_option_t remote_autotag;
const char* checkout_branch;
} git_clone_options;
#define GIT_CLONE_OPTIONS_VERSION 1
#define GIT_CLONE_OPTIONS_INIT {GIT_CLONE_OPTIONS_VERSION, {GIT_CHECKOUT_OPTS_VERSION, GIT_CHECKOUT_SAFE_CREATE}}
/**
* Clone a remote repository, and checkout the branch pointed to by the remote
* HEAD.
*
* @param out pointer that will receive the resulting repository object
* @param url the remote repository to clone
* @param local_path local directory to clone to
* @param options configuration options for the clone. If NULL, the function
* works as though GIT_OPTIONS_INIT were passed.
* @return 0 on success, GIT_ERROR otherwise (use giterr_last for information
* about the error)
*/
GIT_EXTERN(int) git_clone(
git_repository **out,
const char *url,
const char *local_path,
const git_clone_options *options);
/** @} */
GIT_END_DECL
#endif
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -48,7 +48,7 @@ GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, con
* @param len the length of the short identifier
* @return 0 or an error code
*/
GIT_INLINE(int) git_commit_lookup_prefix(git_commit **commit, git_repository *repo, const git_oid *id, unsigned len)
GIT_INLINE(int) git_commit_lookup_prefix(git_commit **commit, git_repository *repo, const git_oid *id, size_t len)
{
return git_object_lookup_prefix((git_object **)commit, repo, id, len, GIT_OBJ_COMMIT);
}
......@@ -76,7 +76,10 @@ GIT_INLINE(void) git_commit_free(git_commit *commit)
* @param commit a previously loaded commit.
* @return object identity for the commit.
*/
GIT_EXTERN(const git_oid *) git_commit_id(git_commit *commit);
GIT_INLINE(const git_oid *) git_commit_id(const git_commit *commit)
{
return git_object_id((const git_object *)commit);
}
/**
* Get the encoding for the message of a commit,
......@@ -88,7 +91,7 @@ GIT_EXTERN(const git_oid *) git_commit_id(git_commit *commit);
* @param commit a previously loaded commit.
* @return NULL, or the encoding
*/
GIT_EXTERN(const char *) git_commit_message_encoding(git_commit *commit);
GIT_EXTERN(const char *) git_commit_message_encoding(const git_commit *commit);
/**
* Get the full message of a commit.
......@@ -96,7 +99,7 @@ GIT_EXTERN(const char *) git_commit_message_encoding(git_commit *commit);
* @param commit a previously loaded commit.
* @return the message of a commit
*/
GIT_EXTERN(const char *) git_commit_message(git_commit *commit);
GIT_EXTERN(const char *) git_commit_message(const git_commit *commit);
/**
* Get the commit time (i.e. committer time) of a commit.
......@@ -104,7 +107,7 @@ GIT_EXTERN(const char *) git_commit_message(git_commit *commit);
* @param commit a previously loaded commit.
* @return the time of a commit
*/
GIT_EXTERN(git_time_t) git_commit_time(git_commit *commit);
GIT_EXTERN(git_time_t) git_commit_time(const git_commit *commit);
/**
* Get the commit timezone offset (i.e. committer's preferred timezone) of a commit.
......@@ -112,7 +115,7 @@ GIT_EXTERN(git_time_t) git_commit_time(git_commit *commit);
* @param commit a previously loaded commit.
* @return positive or negative timezone offset, in minutes from UTC
*/
GIT_EXTERN(int) git_commit_time_offset(git_commit *commit);
GIT_EXTERN(int) git_commit_time_offset(const git_commit *commit);
/**
* Get the committer of a commit.
......@@ -120,7 +123,7 @@ GIT_EXTERN(int) git_commit_time_offset(git_commit *commit);
* @param commit a previously loaded commit.
* @return the committer of a commit
*/
GIT_EXTERN(const git_signature *) git_commit_committer(git_commit *commit);
GIT_EXTERN(const git_signature *) git_commit_committer(const git_commit *commit);
/**
* Get the author of a commit.
......@@ -128,7 +131,7 @@ GIT_EXTERN(const git_signature *) git_commit_committer(git_commit *commit);
* @param commit a previously loaded commit.
* @return the author of a commit
*/
GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit);
GIT_EXTERN(const git_signature *) git_commit_author(const git_commit *commit);
/**
* Get the tree pointed to by a commit.
......@@ -137,7 +140,7 @@ GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit);
* @param commit a previously loaded commit.
* @return 0 or an error code
*/
GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, git_commit *commit);
GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, const git_commit *commit);
/**
* Get the id of the tree pointed to by a commit. This differs from
......@@ -147,7 +150,7 @@ GIT_EXTERN(int) git_commit_tree(git_tree **tree_out, git_commit *commit);
* @param commit a previously loaded commit.
* @return the id of tree pointed to by commit.
*/
GIT_EXTERN(const git_oid *) git_commit_tree_oid(git_commit *commit);
GIT_EXTERN(const git_oid *) git_commit_tree_id(const git_commit *commit);
/**
* Get the number of parents of this commit
......@@ -155,17 +158,17 @@ GIT_EXTERN(const git_oid *) git_commit_tree_oid(git_commit *commit);
* @param commit a previously loaded commit.
* @return integer of count of parents
*/
GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit);
GIT_EXTERN(unsigned int) git_commit_parentcount(const git_commit *commit);
/**
* Get the specified parent of the commit.
*
* @param parent Pointer where to store the parent commit
* @param out Pointer where to store the parent commit
* @param commit a previously loaded commit.
* @param n the position of the parent (from 0 to `parentcount`)
* @return 0 or an error code
*/
GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n);
GIT_EXTERN(int) git_commit_parent(git_commit **out, git_commit *commit, unsigned int n);
/**
* Get the oid of a specified parent for a commit. This is different from
......@@ -176,16 +179,35 @@ GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsig
* @param n the position of the parent (from 0 to `parentcount`)
* @return the id of the parent, NULL on error.
*/
GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned int n);
GIT_EXTERN(const git_oid *) git_commit_parent_id(git_commit *commit, unsigned int n);
/**
* Get the commit object that is the <n>th generation ancestor
* of the named commit object, following only the first parents.
* The returned commit has to be freed by the caller.
*
* Passing `0` as the generation number returns another instance of the
* base commit itself.
*
* @param ancestor Pointer where to store the ancestor commit
* @param commit a previously loaded commit.
* @param n the requested generation
* @return 0 on success; GIT_ENOTFOUND if no matching ancestor exists
* or an error code
*/
GIT_EXTERN(int) git_commit_nth_gen_ancestor(
git_commit **ancestor,
const git_commit *commit,
unsigned int n);
/**
* Create a new commit in the repository using `git_object`
* instances as parameters.
*
* The message will be cleaned up from excess whitespace
* it will be made sure that the last line ends with a '\n'.
* The message will not be cleaned up. This can be achieved
* through `git_message_prettify()`.
*
* @param oid Pointer where to store the OID of the
* @param id Pointer where to store the OID of the
* newly created commit
*
* @param repo Repository where to store the commit
......@@ -226,7 +248,7 @@ GIT_EXTERN(const git_oid *) git_commit_parent_oid(git_commit *commit, unsigned i
* the given reference will be updated to point to it
*/
GIT_EXTERN(int) git_commit_create(
git_oid *oid,
git_oid *id,
git_repository *repo,
const char *update_ref,
const git_signature *author,
......@@ -254,7 +276,7 @@ GIT_EXTERN(int) git_commit_create(
* @see git_commit_create
*/
GIT_EXTERN(int) git_commit_create_v(
git_oid *oid,
git_oid *id,
git_repository *repo,
const char *update_ref,
const git_signature *author,
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -55,6 +55,10 @@
#define GIT_WIN32 1
#endif
#ifdef __amigaos4__
#include <netinet/in.h>
#endif
/**
* @file git2/common.h
* @brief Git common platform definitions
......@@ -81,13 +85,10 @@ GIT_BEGIN_DECL
*/
#define GIT_PATH_MAX 4096
typedef struct {
char **strings;
size_t count;
} git_strarray;
GIT_EXTERN(void) git_strarray_free(git_strarray *array);
GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src);
/**
* The string representation of the null object ID.
*/
#define GIT_OID_HEX_ZERO "0000000000000000000000000000000000000000"
/**
* Return the version of the libgit2 library
......@@ -99,6 +100,91 @@ GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src);
*/
GIT_EXTERN(void) git_libgit2_version(int *major, int *minor, int *rev);
/**
* Combinations of these values describe the capabilities of libgit2.
*/
enum {
GIT_CAP_THREADS = ( 1 << 0 ),
GIT_CAP_HTTPS = ( 1 << 1 )
};
/**
* Query compile time options for libgit2.
*
* @return A combination of GIT_CAP_* values.
*
* - GIT_CAP_THREADS
* Libgit2 was compiled with thread support. Note that thread support is still to be seen as a
* 'work in progress'.
*
* - GIT_CAP_HTTPS
* Libgit2 supports the https:// protocol. This requires the open ssl library to be
* found when compiling libgit2.
*/
GIT_EXTERN(int) git_libgit2_capabilities(void);
enum {
GIT_OPT_GET_MWINDOW_SIZE,
GIT_OPT_SET_MWINDOW_SIZE,
GIT_OPT_GET_MWINDOW_MAPPED_LIMIT,
GIT_OPT_SET_MWINDOW_MAPPED_LIMIT,
GIT_OPT_GET_SEARCH_PATH,
GIT_OPT_SET_SEARCH_PATH,
GIT_OPT_GET_ODB_CACHE_SIZE,
GIT_OPT_SET_ODB_CACHE_SIZE,
};
/**
* Set or query a library global option
*
* Available options:
*
* opts(GIT_OPT_GET_MWINDOW_SIZE, size_t *):
* Get the maximum mmap window size
*
* opts(GIT_OPT_SET_MWINDOW_SIZE, size_t):
* Set the maximum mmap window size
*
* opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, size_t *):
* Get the maximum memory that will be mapped in total by the library
*
* opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, size_t):
* Set the maximum amount of memory that can be mapped at any time
* by the library
*
* opts(GIT_OPT_GET_SEARCH_PATH, int level, char *out, size_t len)
* Get the search path for a given level of config data. "level" must
* be one of GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_GLOBAL, or
* GIT_CONFIG_LEVEL_XDG. The search path is written to the `out`
* buffer up to size `len`. Returns GIT_EBUFS if buffer is too small.
*
* opts(GIT_OPT_SET_SEARCH_PATH, int level, const char *path)
* Set the search path for a level of config data. The search path
* applied to shared attributes and ignore files, too.
* - `path` lists directories delimited by GIT_PATH_LIST_SEPARATOR.
* Pass NULL to reset to the default (generally based on environment
* variables). Use magic path `$PATH` to include the old value
* of the path (if you want to prepend or append, for instance).
* - `level` must be GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_GLOBAL,
* or GIT_CONFIG_LEVEL_XDG.
*
* opts(GIT_OPT_GET_ODB_CACHE_SIZE):
* Get the size of the libgit2 odb cache.
*
* opts(GIT_OPT_SET_ODB_CACHE_SIZE):
* Set the size of the of the libgit2 odb cache. This needs
* to be done before git_repository_open is called, since
* git_repository_open initializes the odb layer. Defaults
* to 128.
*
* @param option Option key
* @param ... value to set the option
* @return 0 on success, <0 on failure
*/
GIT_EXTERN(int) git_libgit2_opts(int option, ...);
/** @} */
GIT_END_DECL
#endif
/*
* 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_cred_helpers_h__
#define INCLUDE_git_cred_helpers_h__
#include "git2/transport.h"
/**
* @file git2/cred_helpers.h
* @brief Utility functions for credential management
* @defgroup git_cred_helpers credential management helpers
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Payload for git_cred_stock_userpass_plaintext.
*/
typedef struct git_cred_userpass_payload {
char *username;
char *password;
} git_cred_userpass_payload;
/**
* Stock callback usable as a git_cred_acquire_cb. This calls
* git_cred_userpass_plaintext_new unless the protocol has not specified
* GIT_CREDTYPE_USERPASS_PLAINTEXT as an allowed type.
*
* @param cred The newly created credential object.
* @param url The resource for which we are demanding a credential.
* @param username_from_url The username that was embedded in a "user@host"
* remote url, or NULL if not included.
* @param allowed_types A bitmask stating which cred types are OK to return.
* @param payload The payload provided when specifying this callback. (This is
* interpreted as a `git_cred_userpass_payload*`.)
*/
GIT_EXTERN(int) git_cred_userpass(
git_cred **cred,
const char *url,
const char *user_from_url,
unsigned int allowed_types,
void *payload);
/** @} */
GIT_END_DECL
#endif
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -17,43 +17,6 @@
*/
GIT_BEGIN_DECL
#ifdef GIT_OLD_ERRORS
enum {
GIT_SUCCESS = 0,
GIT_ENOTOID = -2,
GIT_ENOTFOUND = -3,
GIT_ENOMEM = -4,
GIT_EOSERR = -5,
GIT_EOBJTYPE = -6,
GIT_ENOTAREPO = -7,
GIT_EINVALIDTYPE = -8,
GIT_EMISSINGOBJDATA = -9,
GIT_EPACKCORRUPTED = -10,
GIT_EFLOCKFAIL = -11,
GIT_EZLIB = -12,
GIT_EBUSY = -13,
GIT_EBAREINDEX = -14,
GIT_EINVALIDREFNAME = -15,
GIT_EREFCORRUPTED = -16,
GIT_ETOONESTEDSYMREF = -17,
GIT_EPACKEDREFSCORRUPTED = -18,
GIT_EINVALIDPATH = -19,
GIT_EREVWALKOVER = -20,
GIT_EINVALIDREFSTATE = -21,
GIT_ENOTIMPLEMENTED = -22,
GIT_EEXISTS = -23,
GIT_EOVERFLOW = -24,
GIT_ENOTNUM = -25,
GIT_ESTREAM = -26,
GIT_EINVALIDARGS = -27,
GIT_EOBJCORRUPTED = -28,
GIT_EAMBIGUOUS = -29,
GIT_EPASSTHROUGH = -30,
GIT_ENOMATCH = -31,
GIT_ESHORTBUFFER = -32,
};
#endif
/** Generic return codes */
enum {
GIT_OK = 0,
......@@ -62,9 +25,16 @@ enum {
GIT_EEXISTS = -4,
GIT_EAMBIGUOUS = -5,
GIT_EBUFS = -6,
GIT_EUSER = -7,
GIT_EBAREREPO = -8,
GIT_EORPHANEDHEAD = -9,
GIT_EUNMERGED = -10,
GIT_ENONFASTFORWARD = -11,
GIT_EINVALIDSPEC = -12,
GIT_EMERGECONFLICT = -13,
GIT_PASSTHROUGH = -30,
GIT_REVWALKOVER = -31,
GIT_ITEROVER = -31,
};
typedef struct {
......@@ -72,6 +42,7 @@ typedef struct {
int klass;
} git_error;
/** Error classes */
typedef enum {
GITERR_NOMEMORY,
GITERR_OS,
......@@ -88,6 +59,13 @@ typedef enum {
GITERR_TAG,
GITERR_TREE,
GITERR_INDEXER,
GITERR_SSL,
GITERR_SUBMODULE,
GITERR_THREAD,
GITERR_STASH,
GITERR_CHECKOUT,
GITERR_FETCHHEAD,
GITERR_MERGE,
} git_error_t;
/**
......@@ -103,6 +81,40 @@ GIT_EXTERN(const git_error *) giterr_last(void);
*/
GIT_EXTERN(void) giterr_clear(void);
/**
* Set the error message string for this thread.
*
* This function is public so that custom ODB backends and the like can
* relay an error message through libgit2. Most regular users of libgit2
* will never need to call this function -- actually, calling it in most
* circumstances (for example, calling from within a callback function)
* will just end up having the value overwritten by libgit2 internals.
*
* This error message is stored in thread-local storage and only applies
* to the particular thread that this libgit2 call is made from.
*
* NOTE: Passing the `error_class` as GITERR_OS has a special behavior: we
* attempt to append the system default error message for the last OS error
* that occurred and then clear the last error. The specific implementation
* of looking up and clearing this last OS error will vary by platform.
*
* @param error_class One of the `git_error_t` enum above describing the
* general subsystem that is responsible for the error.
* @param message The formatted error message to keep
*/
GIT_EXTERN(void) giterr_set_str(int error_class, const char *string);
/**
* Set the error message to a special value for memory allocation failure.
*
* The normal `giterr_set_str()` function attempts to `strdup()` the string
* that is passed in. This is not a good idea when the error in question
* is a memory allocation failure. That circumstance has a special setter
* function that sets the error string to a known and statically allocated
* internal value.
*/
GIT_EXTERN(void) giterr_set_oom(void);
/** @} */
GIT_END_DECL
#endif
/*
* 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_graph_h__
#define INCLUDE_git_graph_h__
#include "common.h"
#include "types.h"
#include "oid.h"
/**
* @file git2/graph.h
* @brief Git graph traversal routines
* @defgroup git_revwalk Git graph traversal routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Count the number of unique commits between two commit objects
*
* There is no need for branches containing the commits to have any
* upstream relationship, but it helps to think of one as a branch and
* the other as its upstream, the `ahead` and `behind` values will be
* what git would report for the branches.
*
* @param ahead number of unique from commits in `upstream`
* @param behind number of unique from commits in `local`
* @param repo the repository where the commits exist
* @param local the commit for local
* @param upstream the commit for upstream
*/
GIT_EXTERN(int) git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo, const git_oid *local, const git_oid *upstream);
/** @} */
GIT_END_DECL
#endif
/*
* 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_ignore_h__
#define INCLUDE_git_ignore_h__
#include "common.h"
#include "types.h"
GIT_BEGIN_DECL
/**
* Add ignore rules for a repository.
*
* Excludesfile rules (i.e. .gitignore rules) are generally read from
* .gitignore files in the repository tree or from a shared system file
* only if a "core.excludesfile" config value is set. The library also
* keeps a set of per-repository internal ignores that can be configured
* in-memory and will not persist. This function allows you to add to
* that internal rules list.
*
* Example usage:
*
* error = git_ignore_add_rule(myrepo, "*.c\ndir/\nFile with space\n");
*
* This would add three rules to the ignores.
*
* @param repo The repository to add ignore rules to.
* @param rules Text of rules, a la the contents of a .gitignore file.
* It is okay to have multiple rules in the text; if so,
* each rule should be terminated with a newline.
* @return 0 on success
*/
GIT_EXTERN(int) git_ignore_add_rule(
git_repository *repo,
const char *rules);
/**
* Clear ignore rules that were explicitly added.
*
* Resets to the default internal ignore rules. This will not turn off
* rules in .gitignore files that actually exist in the filesystem.
*
* The default internal ignores ignore ".", ".." and ".git" entries.
*
* @param repo The repository to remove ignore rules from.
* @return 0 on success
*/
GIT_EXTERN(int) git_ignore_clear_internal_rules(
git_repository *repo);
/**
* Test if the ignore rules apply to a given path.
*
* This function checks the ignore rules to see if they would apply to the
* given file. This indicates if the file would be ignored regardless of
* whether the file is already in the index or committed to the repository.
*
* One way to think of this is if you were to do "git add ." on the
* directory containing the file, would it be added or not?
*
* @param ignored boolean returning 0 if the file is not ignored, 1 if it is
* @param repo a repository object
* @param path the file to check ignores for, relative to the repo's workdir.
* @return 0 if ignore rules could be processed for the file (regardless
* of whether it exists or not), or an error < 0 if they could not.
*/
GIT_EXTERN(int) git_ignore_path_is_ignored(
int *ignored,
git_repository *repo,
const char *path);
GIT_END_DECL
#endif
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -16,32 +16,48 @@ GIT_BEGIN_DECL
* This is passed as the first argument to the callback to allow the
* user to see the progress.
*/
typedef struct git_indexer_stats {
unsigned int total;
unsigned int processed;
} git_indexer_stats;
typedef struct git_transfer_progress {
unsigned int total_objects;
unsigned int indexed_objects;
unsigned int received_objects;
size_t received_bytes;
} git_transfer_progress;
typedef struct git_indexer git_indexer;
/**
* Type for progress callbacks during indexing. Return a value less than zero
* to cancel the transfer.
*
* @param stats Structure containing information about the state of the transfer
* @param payload Payload provided by caller
*/
typedef int (*git_transfer_progress_callback)(const git_transfer_progress *stats, void *payload);
typedef struct git_indexer_stream git_indexer_stream;
/**
* Create a new streaming indexer instance
*
* @param out where to store the inexer instance
* @param path to the gitdir (metadata directory)
* @param out where to store the indexer instance
* @param path to the directory where the packfile should be stored
* @param progress_cb function to call with progress information
* @param progress_payload payload for the progress callback
*/
GIT_EXTERN(int) git_indexer_stream_new(git_indexer_stream **out, const char *gitdir);
GIT_EXTERN(int) git_indexer_stream_new(
git_indexer_stream **out,
const char *path,
git_transfer_progress_callback progress_cb,
void *progress_cb_payload);
/**
* Add data to the indexer
*
* @param idx the indexer
* @param data the data to add
* @param size the size of the data
* @param size the size of the data in bytes
* @param stats stat storage
*/
GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_indexer_stats *stats);
GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data, size_t size, git_transfer_progress *stats);
/**
* Finalize the pack and index
......@@ -50,7 +66,7 @@ GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, const void *data
*
* @param idx the indexer
*/
GIT_EXTERN(int) git_indexer_stream_finalize(git_indexer_stream *idx, git_indexer_stats *stats);
GIT_EXTERN(int) git_indexer_stream_finalize(git_indexer_stream *idx, git_transfer_progress *stats);
/**
* Get the packfile's hash
......@@ -60,7 +76,7 @@ GIT_EXTERN(int) git_indexer_stream_finalize(git_indexer_stream *idx, git_indexer
*
* @param idx the indexer instance
*/
GIT_EXTERN(const git_oid *) git_indexer_stream_hash(git_indexer_stream *idx);
GIT_EXTERN(const git_oid *) git_indexer_stream_hash(const git_indexer_stream *idx);
/**
* Free the indexer and its resources
......@@ -69,53 +85,6 @@ GIT_EXTERN(const git_oid *) git_indexer_stream_hash(git_indexer_stream *idx);
*/
GIT_EXTERN(void) git_indexer_stream_free(git_indexer_stream *idx);
/**
* Create a new indexer instance
*
* @param out where to store the indexer instance
* @param packname the absolute filename of the packfile to index
*/
GIT_EXTERN(int) git_indexer_new(git_indexer **out, const char *packname);
/**
* Iterate over the objects in the packfile and extract the information
*
* Indexing a packfile can be very expensive so this function is
* expected to be run in a worker thread and the stats used to provide
* feedback the user.
*
* @param idx the indexer instance
* @param stats storage for the running state
*/
GIT_EXTERN(int) git_indexer_run(git_indexer *idx, git_indexer_stats *stats);
/**
* Write the index file to disk.
*
* The file will be stored as pack-$hash.idx in the same directory as
* the packfile.
*
* @param idx the indexer instance
*/
GIT_EXTERN(int) git_indexer_write(git_indexer *idx);
/**
* Get the packfile's hash
*
* A packfile's name is derived from the sorted hashing of all object
* names. This is only correct after the index has been written to disk.
*
* @param idx the indexer instance
*/
GIT_EXTERN(const git_oid *) git_indexer_hash(git_indexer *idx);
/**
* Free the indexer and its resources
*
* @param idx the indexer to free
*/
GIT_EXTERN(void) git_indexer_free(git_indexer *idx);
GIT_END_DECL
#endif
......@@ -40,7 +40,11 @@
#pragma once
#endif
#if _MSC_VER >= 1600
#include <stdint.h>
#else
#include "stdint.h"
#endif
// 7.8 Format conversion of integer types
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -27,8 +27,28 @@ GIT_BEGIN_DECL
* @param repo the repository where the commits exist
* @param one one of the commits
* @param two the other commit
* @return Zero on success; GIT_ENOTFOUND or -1 on failure.
*/
GIT_EXTERN(int) git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *two);
GIT_EXTERN(int) git_merge_base(
git_oid *out,
git_repository *repo,
const git_oid *one,
const git_oid *two);
/**
* Find a merge base given a list of commits
*
* @param out the OID of a merge base considering all the commits
* @param repo the repository where the commits exist
* @param input_array oids of the commits
* @param length The number of commits in the provided `input_array`
* @return Zero on success; GIT_ENOTFOUND or -1 on failure.
*/
GIT_EXTERN(int) git_merge_base_many(
git_oid *out,
git_repository *repo,
const git_oid input_array[],
size_t length);
/** @} */
GIT_END_DECL
......
/*
* 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_message_h__
#define INCLUDE_git_message_h__
#include "common.h"
/**
* @file git2/message.h
* @brief Git message management routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Clean up message from excess whitespace and make sure that the last line
* ends with a '\n'.
*
* Optionally, can remove lines starting with a "#".
*
* @param out The user-allocated buffer which will be filled with the
* cleaned up message. Pass NULL if you just want to get the needed
* size of the prettified message as the output value.
*
* @param out_size Size of the `out` buffer in bytes.
*
* @param message The message to be prettified.
*
* @param strip_comments Non-zero to remove lines starting with "#", 0 to
* leave them in.
*
* @return -1 on error, else number of characters in prettified message
* including the trailing NUL byte
*/
GIT_EXTERN(int) git_message_prettify(
char *out,
size_t out_size,
const char *message,
int strip_comments);
/** @} */
GIT_END_DECL
#endif /* INCLUDE_git_message_h__ */
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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_net_h__
#define INCLUDE_net_h__
#ifndef INCLUDE_git_net_h__
#define INCLUDE_git_net_h__
#include "common.h"
#include "oid.h"
......@@ -27,15 +27,17 @@ GIT_BEGIN_DECL
* gets called.
*/
#define GIT_DIR_FETCH 0
#define GIT_DIR_PUSH 1
typedef enum {
GIT_DIRECTION_FETCH = 0,
GIT_DIRECTION_PUSH = 1
} git_direction;
/**
* Remote head description, given out on `ls` calls.
*/
struct git_remote_head {
int local:1; /* available locally */
int local; /* available locally */
git_oid oid;
git_oid loid;
char *name;
......@@ -44,7 +46,7 @@ struct git_remote_head {
/**
* Callback for listing the remote heads
*/
typedef int (*git_headlist_cb)(git_remote_head *, void *);
typedef int (*git_headlist_cb)(git_remote_head *rhead, void *payload);
/** @} */
GIT_END_DECL
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -19,19 +19,82 @@
GIT_BEGIN_DECL
/**
* Callback for git_note_foreach.
*
* Receives:
* - blob_id: Oid of the blob containing the message
* - annotated_object_id: Oid of the git object being annotated
* - payload: Payload data passed to `git_note_foreach`
*/
typedef int (*git_note_foreach_cb)(
const git_oid *blob_id, const git_oid *annotated_object_id, void *payload);
/**
* note iterator
*/
typedef struct git_iterator git_note_iterator;
/**
* Creates a new iterator for notes
*
* The iterator must be freed manually by the user.
*
* @param out pointer to the iterator
* @param repo repository where to look up the note
* @param notes_ref canonical name of the reference to use (optional); defaults to
* "refs/notes/commits"
*
* @return 0 or an error code
*/
GIT_EXTERN(int) git_note_iterator_new(
git_note_iterator **out,
git_repository *repo,
const char *notes_ref);
/**
* Frees an git_note_iterator
*
* @param it pointer to the iterator
*/
GIT_EXTERN(void) git_note_iterator_free(git_note_iterator *it);
/**
* Returns the current item (note_id and annotated_id) and advance the iterator
* internally to the next value
*
* The notes must not be freed manually by the user.
*
* @param it pointer to the iterator
* @param note_id id of blob containing the message
* @param annotated_id id of the git object being annotated
*
* @return 0 (no error), GIT_ITEROVER (iteration is done) or an error code
* (negative value)
*/
GIT_EXTERN(int) git_note_next(
git_oid* note_id,
git_oid* annotated_id,
git_note_iterator *it);
/**
* Read the note for an object
*
* The note must be freed manually by the user.
*
* @param note the note; NULL in case of error
* @param repo the Git repository
* @param notes_ref OID reference to use (optional); defaults to "refs/notes/commits"
* @param oid OID of the object
* @param out pointer to the read note; NULL in case of error
* @param repo repository where to look up the note
* @param notes_ref canonical name of the reference to use (optional); defaults to
* "refs/notes/commits"
* @param oid OID of the git object to read the note from
*
* @return 0 or an error code
*/
GIT_EXTERN(int) git_note_read(git_note **note, git_repository *repo,
const char *notes_ref, const git_oid *oid);
GIT_EXTERN(int) git_note_read(
git_note **out,
git_repository *repo,
const char *notes_ref,
const git_oid *oid);
/**
* Get the note message
......@@ -39,7 +102,7 @@ GIT_EXTERN(int) git_note_read(git_note **note, git_repository *repo,
* @param note
* @return the note message
*/
GIT_EXTERN(const char *) git_note_message(git_note *note);
GIT_EXTERN(const char *) git_note_message(const git_note *note);
/**
......@@ -48,42 +111,52 @@ GIT_EXTERN(const char *) git_note_message(git_note *note);
* @param note
* @return the note object OID
*/
GIT_EXTERN(const git_oid *) git_note_oid(git_note *note);
GIT_EXTERN(const git_oid *) git_note_oid(const git_note *note);
/**
* Add a note for an object
*
* @param oid pointer to store the OID (optional); NULL in case of error
* @param repo the Git repository
* @param out pointer to store the OID (optional); NULL in case of error
* @param repo repository where to store the note
* @param author signature of the notes commit author
* @param committer signature of the notes commit committer
* @param notes_ref OID reference to update (optional); defaults to "refs/notes/commits"
* @param oid The OID of the object
* @param oid The note to add for object oid
* @param notes_ref canonical name of the reference to use (optional);
* defaults to "refs/notes/commits"
* @param oid OID of the git object to decorate
* @param note Content of the note to add for object oid
* @param force Overwrite existing note
*
* @return 0 or an error code
*/
GIT_EXTERN(int) git_note_create(git_oid *out, git_repository *repo,
git_signature *author, git_signature *committer,
const char *notes_ref, const git_oid *oid,
const char *note);
GIT_EXTERN(int) git_note_create(
git_oid *out,
git_repository *repo,
const git_signature *author,
const git_signature *committer,
const char *notes_ref,
const git_oid *oid,
const char *note,
int force);
/**
* Remove the note for an object
*
* @param repo the Git repository
* @param notes_ref OID reference to use (optional); defaults to "refs/notes/commits"
* @param repo repository where the note lives
* @param notes_ref canonical name of the reference to use (optional);
* defaults to "refs/notes/commits"
* @param author signature of the notes commit author
* @param committer signature of the notes commit committer
* @param oid the oid which note's to be removed
* @param oid OID of the git object to remove the note from
*
* @return 0 or an error code
*/
GIT_EXTERN(int) git_note_remove(git_repository *repo, const char *notes_ref,
git_signature *author, git_signature *committer,
const git_oid *oid);
GIT_EXTERN(int) git_note_remove(
git_repository *repo,
const char *notes_ref,
const git_signature *author,
const git_signature *committer,
const git_oid *oid);
/**
* Free a git_note object
......@@ -103,36 +176,26 @@ GIT_EXTERN(void) git_note_free(git_note *note);
GIT_EXTERN(int) git_note_default_ref(const char **out, git_repository *repo);
/**
* Basic components of a note
*
* - Oid of the blob containing the message
* - Oid of the git object being annotated
*/
typedef struct {
git_oid blob_oid;
git_oid annotated_object_oid;
} git_note_data;
/**
* Loop over all the notes within a specified namespace
* and issue a callback for each one.
*
* @param repo Repository where to find the notes.
*
* @param notes_ref OID reference to read from (optional); defaults to "refs/notes/commits".
* @param notes_ref Reference to read from (optional); defaults to
* "refs/notes/commits".
*
* @param note_cb Callback to invoke per found annotation.
* @param note_cb Callback to invoke per found annotation. Return non-zero
* to stop looping.
*
* @param payload Extra parameter to callback function.
*
* @return 0 or an error code.
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_note_foreach(
git_repository *repo,
const char *notes_ref,
int (*note_cb)(git_note_data *note_data, void *payload),
void *payload
);
git_repository *repo,
const char *notes_ref,
git_note_foreach_cb note_cb,
void *payload);
/** @} */
GIT_END_DECL
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -21,7 +21,7 @@
GIT_BEGIN_DECL
/**
* Lookup a reference to one of the objects in a repostory.
* Lookup a reference to one of the objects in a repository.
*
* The generated reference is owned by the repository and
* should be closed with the `git_object_free` method
......@@ -45,7 +45,7 @@ GIT_EXTERN(int) git_object_lookup(
git_otype type);
/**
* Lookup a reference to one of the objects in a repostory,
* Lookup a reference to one of the objects in a repository,
* given a prefix of its identifier (short id).
*
* The object obtained will be so that its identifier
......@@ -75,7 +75,7 @@ GIT_EXTERN(int) git_object_lookup_prefix(
git_object **object_out,
git_repository *repo,
const git_oid *id,
unsigned int len,
size_t len,
git_otype type);
/**
......@@ -114,7 +114,7 @@ GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj);
* This method instructs the library to close an existing
* object; note that git_objects are owned and cached by the repository
* so the object may or may not be freed after this library call,
* depending on how agressive is the caching mechanism used
* depending on how aggressive is the caching mechanism used
* by the repository.
*
* IMPORTANT:
......@@ -167,6 +167,36 @@ GIT_EXTERN(int) git_object_typeisloose(git_otype type);
*/
GIT_EXTERN(size_t) git_object__size(git_otype type);
/**
* Recursively peel an object until an object of the specified type is met.
*
* The retrieved `peeled` object is owned by the repository and should be
* closed with the `git_object_free` method.
*
* If you pass `GIT_OBJ_ANY` as the target type, then the object will be
* peeled until the type changes (e.g. a tag will be chased until the
* referenced object is no longer a tag).
*
* @param peeled Pointer to the peeled git_object
* @param object The object to be processed
* @param target_type The type of the requested object (GIT_OBJ_COMMIT,
* GIT_OBJ_TAG, GIT_OBJ_TREE, GIT_OBJ_BLOB or GIT_OBJ_ANY).
* @return 0 on success, GIT_EAMBIGUOUS, GIT_ENOTFOUND or an error code
*/
GIT_EXTERN(int) git_object_peel(
git_object **peeled,
const git_object *object,
git_otype target_type);
/**
* Create an in-memory copy of a Git object. The copy must be
* explicitly free'd or it will leak.
*
* @param dest Pointer to store the copy of the object
* @param source Original object to copy
*/
GIT_EXTERN(int) git_object_dup(git_object **dest, git_object *source);
/** @} */
GIT_END_DECL
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -11,6 +11,7 @@
#include "types.h"
#include "oid.h"
#include "odb_backend.h"
#include "indexer.h"
/**
* @file git2/odb.h
......@@ -62,7 +63,7 @@ GIT_EXTERN(int) git_odb_open(git_odb **out, const char *objects_dir);
* @param odb database to add the backend to
* @param backend pointer to a git_odb_backend instance
* @param priority Value for ordering the backends queue
* @return 0 on sucess; error code otherwise
* @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int priority);
......@@ -83,11 +84,28 @@ GIT_EXTERN(int) git_odb_add_backend(git_odb *odb, git_odb_backend *backend, int
* @param odb database to add the backend to
* @param backend pointer to a git_odb_backend instance
* @param priority Value for ordering the backends queue
* @return 0 on sucess; error code otherwise
* @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, int priority);
/**
* Add an on-disk alternate to an existing Object DB.
*
* Note that the added path must point to an `objects`, not
* to a full repository, to use it as an alternate store.
*
* Alternate backends are always checked for objects *after*
* all the main backends have been exhausted.
*
* Writing is disabled on alternate backends.
*
* @param odb database to add the backend to
* @param path path to the objects folder for the alternate
* @return 0 on success; error code otherwise
*/
GIT_EXTERN(int) git_odb_add_disk_alternate(git_odb *odb, const char *path);
/**
* Close an open object database.
*
* @param db database pointer to close. If NULL no action is taken.
......@@ -135,11 +153,12 @@ GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *i
* @param db database to search for the object in.
* @param short_id a prefix of the id of the object to read.
* @param len the length of the prefix
* @return 0 if the object was read;
* GIT_ENOTFOUND if the object is not in the database.
* GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
* @return
* - 0 if the object was read;
* - GIT_ENOTFOUND if the object is not in the database.
* - GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
*/
GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, unsigned int len);
GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, size_t len);
/**
* Read the header of an object from the database, without
......@@ -151,15 +170,15 @@ GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git
* of an object, so the whole object will be read and then the
* header will be returned.
*
* @param len_p pointer where to store the length
* @param type_p pointer where to store the type
* @param len_out pointer where to store the length
* @param type_out pointer where to store the type
* @param db database to search for the object in.
* @param id identity of the object to read.
* @return
* - 0 if the object was read;
* - GIT_ENOTFOUND if the object is not in the database.
*/
GIT_EXTERN(int) git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id);
GIT_EXTERN(int) git_odb_read_header(size_t *len_out, git_otype *type_out, git_odb *db, const git_oid *id);
/**
* Determine if the given object can be found in the object database.
......@@ -173,6 +192,41 @@ GIT_EXTERN(int) git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *d
GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
/**
* Refresh the object database to load newly added files.
*
* If the object databases have changed on disk while the library
* is running, this function will force a reload of the underlying
* indexes.
*
* Use this function when you're confident that an external
* application has tampered with the ODB.
*
* NOTE that it is not necessary to call this function at all. The
* library will automatically attempt to refresh the ODB
* when a lookup fails, to see if the looked up object exists
* on disk but hasn't been loaded yet.
*
* @param db database to refresh
* @return 0 on success, error code otherwise
*/
GIT_EXTERN(int) git_odb_refresh(struct git_odb *db);
/**
* List all objects available in the database
*
* The callback will be called for each object available in the
* database. Note that the objects are likely to be returned in the index
* order, which would make accessing the objects in that order inefficient.
* Return a non-zero value from the callback to stop looping.
*
* @param db database to use
* @param cb the callback to call for each object
* @param payload data to pass to the callback
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload);
/**
* Write an object directly into the ODB
*
* This method writes a full object straight into the ODB.
......@@ -183,14 +237,14 @@ GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
* This method is provided for compatibility with custom backends
* which are not able to support streaming writes
*
* @param oid pointer to store the OID result of the write
* @param out pointer to store the OID result of the write
* @param odb object database where to store the object
* @param data buffer with the data to storr
* @param data buffer with the data to store
* @param len size of the buffer
* @param type type of the data to store
* @return 0 or an error code
*/
GIT_EXTERN(int) git_odb_write(git_oid *oid, git_odb *odb, const void *data, size_t len, git_otype type);
GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size_t len, git_otype type);
/**
* Open a stream to write an object into the ODB
......@@ -213,13 +267,13 @@ GIT_EXTERN(int) git_odb_write(git_oid *oid, git_odb *odb, const void *data, size
*
* @see git_odb_stream
*
* @param stream pointer where to store the stream
* @param out pointer where to store the stream
* @param db object database where the stream will write
* @param size final size of the object that will be written
* @param type type of the object that will be written
* @return 0 if the stream was created; error code otherwise
*/
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_otype type);
GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, size_t size, git_otype type);
/**
* Open a stream to read an object from the ODB
......@@ -240,32 +294,58 @@ GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_
*
* @see git_odb_stream
*
* @param stream pointer where to store the stream
* @param out pointer where to store the stream
* @param db object database where the stream will read from
* @param oid oid of the object the stream will read from
* @return 0 if the stream was created; error code otherwise
*/
GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid);
GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **out, git_odb *db, const git_oid *oid);
/**
* Open a stream for writing a pack file to the ODB.
*
* If the ODB layer understands pack files, then the given
* packfile will likely be streamed directly to disk (and a
* corresponding index created). If the ODB layer does not
* understand pack files, the objects will be stored in whatever
* format the ODB layer uses.
*
* @see git_odb_writepack
*
* @param out pointer to the writepack functions
* @param db object database where the stream will read from
* @param progress_cb function to call with progress information.
* Be aware that this is called inline with network and indexing operations,
* so performance may be affected.
* @param progress_payload payload for the progress callback
*/
GIT_EXTERN(int) git_odb_write_pack(
git_odb_writepack **out,
git_odb *db,
git_transfer_progress_callback progress_cb,
void *progress_payload);
/**
* Determine the object-ID (sha1 hash) of a data buffer
*
* The resulting SHA-1 OID will the itentifier for the data
* The resulting SHA-1 OID will be the identifier for the data
* buffer as if the data buffer it were to written to the ODB.
*
* @param id the resulting object-ID.
* @param out the resulting object-ID.
* @param data data to hash
* @param len size of the data
* @param type of the data to hash
* @return 0 or an error code
*/
GIT_EXTERN(int) git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type);
GIT_EXTERN(int) git_odb_hash(git_oid *out, const void *data, size_t len, git_otype type);
/**
* Read a file from disk and fill a git_oid with the object id
* that the file would have if it were written to the Object
* Database as an object of the given type. Similar functionality
* to git.git's `git hash-object` without the `-w` flag.
* Database as an object of the given type (w/o applying filters).
* Similar functionality to git.git's `git hash-object` without
* the `-w` flag, however, with the --no-filters flag.
* If you need filters, see git_repository_hashfile.
*
* @param out oid structure the result is written into.
* @param path file to read and determine object id for
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -10,6 +10,7 @@
#include "common.h"
#include "types.h"
#include "oid.h"
#include "indexer.h"
/**
* @file git2/backend.h
......@@ -21,11 +22,24 @@
GIT_BEGIN_DECL
struct git_odb_stream;
struct git_odb_writepack;
/** An instance for a custom backend */
/**
* Function type for callbacks from git_odb_foreach.
*/
typedef int (*git_odb_foreach_cb)(const git_oid *id, void *payload);
/**
* An instance for a custom backend
*/
struct git_odb_backend {
unsigned int version;
git_odb *odb;
/* read and read_prefix each return to libgit2 a buffer which
* will be freed later. The buffer should be allocated using
* the function git_odb_backend_malloc to ensure that it can
* be safely freed later. */
int (* read)(
void **, size_t *, git_otype *,
struct git_odb_backend *,
......@@ -42,13 +56,17 @@ struct git_odb_backend {
void **, size_t *, git_otype *,
struct git_odb_backend *,
const git_oid *,
unsigned int);
size_t);
int (* read_header)(
size_t *, git_otype *,
struct git_odb_backend *,
const git_oid *);
/* The writer may assume that the object
* has already been hashed and is passed
* in the first parameter.
*/
int (* write)(
git_oid *,
struct git_odb_backend *,
......@@ -71,9 +89,25 @@ struct git_odb_backend {
struct git_odb_backend *,
const git_oid *);
int (* refresh)(struct git_odb_backend *);
int (* foreach)(
struct git_odb_backend *,
git_odb_foreach_cb cb,
void *payload);
int (* writepack)(
struct git_odb_writepack **,
struct git_odb_backend *,
git_transfer_progress_callback progress_cb,
void *progress_payload);
void (* free)(struct git_odb_backend *);
};
#define GIT_ODB_BACKEND_VERSION 1
#define GIT_ODB_BACKEND_INIT {GIT_ODB_BACKEND_VERSION}
/** Streaming mode */
enum {
GIT_STREAM_RDONLY = (1 << 1),
......@@ -84,7 +118,7 @@ enum {
/** A stream to read/write from a backend */
struct git_odb_stream {
struct git_odb_backend *backend;
int mode;
unsigned int mode;
int (*read)(struct git_odb_stream *stream, char *buffer, size_t len);
int (*write)(struct git_odb_stream *stream, const char *buffer, size_t len);
......@@ -92,8 +126,23 @@ struct git_odb_stream {
void (*free)(struct git_odb_stream *stream);
};
GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir);
GIT_EXTERN(int) git_odb_backend_loose(git_odb_backend **backend_out, const char *objects_dir, int compression_level, int do_fsync);
/** A stream to write a pack file to the ODB */
struct git_odb_writepack {
struct git_odb_backend *backend;
int (*add)(struct git_odb_writepack *writepack, const void *data, size_t size, git_transfer_progress *stats);
int (*commit)(struct git_odb_writepack *writepack, git_transfer_progress *stats);
void (*free)(struct git_odb_writepack *writepack);
};
GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len);
/**
* Constructors for in-box ODB backends.
*/
GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **out, const char *objects_dir);
GIT_EXTERN(int) git_odb_backend_loose(git_odb_backend **out, const char *objects_dir, int compression_level, int do_fsync);
GIT_EXTERN(int) git_odb_backend_one_pack(git_odb_backend **out, const char *index_file);
GIT_END_DECL
......
/*
* Copyright (C) 2009-2012 the libgit2 contributors
* 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.
......@@ -30,11 +30,10 @@ GIT_BEGIN_DECL
#define GIT_OID_MINPREFIXLEN 4
/** Unique identity of any object (commit, tree, blob, tag). */
typedef struct _git_oid git_oid;
struct _git_oid {
typedef struct git_oid {
/** raw binary formatted id */
unsigned char id[GIT_OID_RAWSZ];
};
} git_oid;
/**
* Parse a hex formatted object id into a git_oid.
......@@ -48,6 +47,16 @@ struct _git_oid {
GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
/**
* Parse a hex formatted null-terminated string into a git_oid.
*
* @param out oid structure the result is written into.
* @param str input hex string; must be at least 4 characters
* long and null-terminated.
* @return 0 or an error code
*/
GIT_EXTERN(int) git_oid_fromstrp(git_oid *out, const char *str);
/**
* Parse N characters of a hex formatted object id into a git_oid
*
* If N is odd, N-1 characters will be parsed instead.
......@@ -71,29 +80,29 @@ GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw);
/**
* Format a git_oid into a hex string.
*
* @param str output hex string; must be pointing at the start of
* @param out output hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes
* needed for an oid encoded in hex (40 bytes). Only the
* oid digits are written; a '\\0' terminator must be added
* by the caller if it is required.
* @param oid oid structure to format.
*/
GIT_EXTERN(void) git_oid_fmt(char *str, const git_oid *oid);
GIT_EXTERN(void) git_oid_fmt(char *out, const git_oid *id);
/**
* Format a git_oid into a loose-object path string.
*
* The resulting string is "aa/...", where "aa" is the first two
* hex digitis of the oid and "..." is the remaining 38 digits.
* hex digits of the oid and "..." is the remaining 38 digits.
*
* @param str output hex string; must be pointing at the start of
* @param out output hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes
* needed for an oid encoded in hex (41 bytes). Only the
* oid digits are written; a '\\0' terminator must be added
* by the caller if it is required.
* @param oid oid structure to format.
* @param id oid structure to format.
*/
GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid);
GIT_EXTERN(void) git_oid_pathfmt(char *out, const git_oid *id);
/**
* Format a git_oid into a newly allocated c-string.
......@@ -102,7 +111,7 @@ GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid);
* @return the c-string; NULL if memory is exhausted. Caller must
* deallocate the string with git__free().
*/
GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *oid);
GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *id);
/**
* Format a git_oid into a buffer as a hex format c-string.
......@@ -115,11 +124,11 @@ GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *oid);
*
* @param out the buffer into which the oid string is output.
* @param n the size of the out buffer.
* @param oid the oid structure to format.
* @param id the oid structure to format.
* @return the out buffer pointer, assuming no input parameter
* errors, otherwise a pointer to an empty string.
*/
GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *oid);
GIT_EXTERN(char *) git_oid_tostr(char *out, size_t n, const git_oid *id);
/**
* Copy an oid from one structure to another.
......@@ -136,7 +145,31 @@ GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src);
* @param b second oid structure.
* @return <0, 0, >0 if a < b, a == b, a > b.
*/
GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b);
GIT_INLINE(int) git_oid_cmp(const git_oid *a, const git_oid *b)
{
const unsigned char *sha1 = a->id;
const unsigned char *sha2 = b->id;
int i;
for (i = 0; i < GIT_OID_RAWSZ; i++, sha1++, sha2++) {
if (*sha1 != *sha2)
return *sha1 - *sha2;
}
return 0;
}
/**
* Compare two oid structures for equality
*
* @param a first oid structure.
* @param b second oid structure.
* @return true if equal, false otherwise
*/
GIT_INLINE(int) git_oid_equal(const git_oid *a, const git_oid *b)
{
return !git_oid_cmp(a, b);
}
/**
* Compare the first 'len' hexadecimal characters (packets of 4 bits)
......@@ -147,22 +180,24 @@ GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b);
* @param len the number of hex chars to compare
* @return 0 in case of a match
*/
GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, unsigned int len);
GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, size_t len);
/**
* Check if an oid equals an hex formatted object id.
*
* @param a oid structure.
* @param id oid structure.
* @param str input hex string of an object id.
* @return GIT_ENOTOID if str is not a valid hex string,
* 0 in case of a match, GIT_ERROR otherwise.
*/
GIT_EXTERN(int) git_oid_streq(const git_oid *a, const char *str);
GIT_EXTERN(int) git_oid_streq(const git_oid *id, const char *str);
/**
* Check is an oid is all zeros.
*
* @return 1 if all zeros, 0 otherwise.
*/
GIT_EXTERN(int) git_oid_iszero(const git_oid *a);
GIT_EXTERN(int) git_oid_iszero(const git_oid *id);
/**
* OID Shortener object
......@@ -204,12 +239,12 @@ GIT_EXTERN(git_oid_shorten *) git_oid_shorten_new(size_t min_length);
* GIT_ENOMEM error
*
* @param os a `git_oid_shorten` instance
* @param text_oid an OID in text form
* @param text_id an OID in text form
* @return the minimal length to uniquely identify all OIDs
* added so far to the set; or an error code (<0) if an
* error occurs.
*/
GIT_EXTERN(int) git_oid_shorten_add(git_oid_shorten *os, const char *text_oid);
GIT_EXTERN(int) git_oid_shorten_add(git_oid_shorten *os, const char *text_id);
/**
* Free an OID shortener instance
......
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
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