Merge branch 'master' into next

Conflicts:
	src/repository.c
This commit is contained in:
J. David Ibáñez
2013-03-12 08:24:56 +01:00
30 changed files with 1212 additions and 314 deletions

View File

@@ -32,6 +32,7 @@
#include <Python.h>
#include <git2.h>
PyObject* wrap_config(char *c_path);
PyObject* Config_get_global_config(void);
PyObject* Config_get_system_config(void);
PyObject* Config_add_file(Config *self, PyObject *args, PyObject *kwds);
@@ -39,6 +40,6 @@ PyObject* Config_getitem(Config *self, PyObject *key);
PyObject* Config_foreach(Config *self, PyObject *args);
PyObject* Config_get_multivar(Config *self, PyObject *args);
PyObject* Config_set_multivar(Config *self, PyObject *args);
int Config_init(Config *self, PyObject *args, PyObject *kwds);
int Config_setitem(Config *self, PyObject *key, PyObject *value);
#endif

37
include/pygit2/note.h Normal file
View File

@@ -0,0 +1,37 @@
/*
* Copyright 2010-2013 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDE_pygit2_note_h
#define INCLUDE_pygit2_note_h
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <git2.h>
PyObject* wrap_note(Repository* repo, git_oid* annotated_id, const char* ref);
#endif

38
include/pygit2/remote.h Normal file
View File

@@ -0,0 +1,38 @@
/*
* Copyright 2010-2013 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDE_pygit2_remote_h
#define INCLUDE_pygit2_remote_h
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <git2.h>
PyObject* Remote_init(Remote *self, PyObject *args, PyObject *kwds);
PyObject* Remote_fetch(Remote *self, PyObject *args);
#endif

View File

@@ -57,22 +57,46 @@ OBJECT_STRUCT(Blob, git_blob, blob)
OBJECT_STRUCT(Tag, git_tag, tag)
OBJECT_STRUCT(Index, git_index, index)
OBJECT_STRUCT(Walker, git_revwalk, walk)
OBJECT_STRUCT(Config, git_config, config)
OBJECT_STRUCT(Remote, git_remote, remote)
OBJECT_STRUCT(Diff, git_diff_list, list)
typedef struct {
PyObject_HEAD
git_diff_list* list;
git_config* config;
} Config;
typedef struct {
PyObject_HEAD
Repository *repo;
git_note *note;
char* annotated_id;
} Note;
typedef struct {
PyObject_HEAD
Repository *repo;
git_note_iterator* iter;
char* ref;
} NoteIter;
typedef struct {
PyObject_HEAD
Diff* diff;
size_t i;
size_t n;
} DiffIter;
typedef struct {
PyObject_HEAD
PyObject* files;
PyObject* hunks;
} DiffEntry;
const char * old_file_path;
const char * new_file_path;
char* old_oid;
char* new_oid;
unsigned status;
unsigned similarity;
} Patch;
typedef struct {
PyObject_HEAD
@@ -82,18 +106,11 @@ typedef struct {
typedef struct {
PyObject_HEAD
const char *header;
PyObject* lines;
int old_start;
int old_lines;
char* old_oid;
int old_mode;
const char* old_file;
int new_start;
int new_lines;
char* new_oid;
int new_mode;
const char* new_file;
PyObject *data;
} Hunk;
typedef struct {
@@ -128,7 +145,6 @@ typedef struct {
typedef struct {
PyObject_HEAD
Reference *reference;
git_reflog *reflog;
int i;
int size;

View File

@@ -95,9 +95,6 @@ char * py_str_to_c_str(PyObject *value, const char *encoding);
#define py_path_to_c_str(py_path) \
py_str_to_c_str(py_path, Py_FileSystemDefaultEncoding)
#define INSTANCIATE_CLASS(type, arglist) \
PyObject_CallObject(PyType_GenericNew(&type, NULL, NULL), arglist);
/* Helpers to make shorter PyMethodDef and PyGetSetDef blocks */
#define METHOD(type, name, args)\
{#name, (PyCFunction) type ## _ ## name, args, type ## _ ## name ## __doc__}
@@ -119,21 +116,22 @@ char * py_str_to_c_str(PyObject *value, const char *encoding);
#define MEMBER(type, attr, attr_type, docstr)\
{#attr, attr_type, offsetof(type, attr), 0, PyDoc_STR(docstr)}
/* Helpers for memory allocation */
#define MALLOC(ptr, size, label) \
ptr = realloc(ptr, size * sizeof(char));\
#define CALLOC(ptr, num, size, label) \
ptr = calloc((num), size);\
if (ptr == NULL) {\
err = GIT_ERROR;\
giterr_set_oom();\
goto label;\
}\
#define FREE(to_free)\
if (to_free != NULL) { free(to_free); to_free = NULL; }
#define FREE_FUNC(to_free, fnct)\
if (to_free != NULL) { fnct(to_free); to_free = NULL; }
}
#define MALLOC(ptr, size, label) \
ptr = malloc(size);\
if (ptr == NULL) {\
err = GIT_ERROR;\
giterr_set_oom();\
goto label;\
}
#endif

391
misc/valgrind-python.supp Normal file
View File

@@ -0,0 +1,391 @@
#
# This is a valgrind suppression file that should be used when using valgrind.
#
# Here's an example of running valgrind:
#
# cd python/dist/src
# valgrind --tool=memcheck --suppressions=Misc/valgrind-python.supp \
# ./python -E -tt ./Lib/test/regrtest.py -u bsddb,network
#
# You must edit Objects/obmalloc.c and uncomment Py_USING_MEMORY_DEBUGGER
# to use the preferred suppressions with Py_ADDRESS_IN_RANGE.
#
# If you do not want to recompile Python, you can uncomment
# suppressions for PyObject_Free and PyObject_Realloc.
#
# See Misc/README.valgrind for more information.
# all tool names: Addrcheck,Memcheck,cachegrind,helgrind,massif
{
ADDRESS_IN_RANGE/Invalid read of size 4
Memcheck:Addr4
fun:Py_ADDRESS_IN_RANGE
}
{
ADDRESS_IN_RANGE/Invalid read of size 4
Memcheck:Value4
fun:Py_ADDRESS_IN_RANGE
}
{
ADDRESS_IN_RANGE/Invalid read of size 8 (x86_64 aka amd64)
Memcheck:Value8
fun:Py_ADDRESS_IN_RANGE
}
{
ADDRESS_IN_RANGE/Conditional jump or move depends on uninitialised value
Memcheck:Cond
fun:Py_ADDRESS_IN_RANGE
}
#
# Leaks (including possible leaks)
# Hmmm, I wonder if this masks some real leaks. I think it does.
# Will need to fix that.
#
{
Suppress leaking the GIL. Happens once per process, see comment in ceval.c.
Memcheck:Leak
fun:malloc
fun:PyThread_allocate_lock
fun:PyEval_InitThreads
}
{
Suppress leaking the GIL after a fork.
Memcheck:Leak
fun:malloc
fun:PyThread_allocate_lock
fun:PyEval_ReInitThreads
}
{
Suppress leaking the autoTLSkey. This looks like it shouldn't leak though.
Memcheck:Leak
fun:malloc
fun:PyThread_create_key
fun:_PyGILState_Init
fun:Py_InitializeEx
fun:Py_Main
}
{
Hmmm, is this a real leak or like the GIL?
Memcheck:Leak
fun:malloc
fun:PyThread_ReInitTLS
}
{
Handle PyMalloc confusing valgrind (possibly leaked)
Memcheck:Leak
fun:realloc
fun:_PyObject_GC_Resize
fun:COMMENT_THIS_LINE_TO_DISABLE_LEAK_WARNING
}
{
Handle PyMalloc confusing valgrind (possibly leaked)
Memcheck:Leak
fun:malloc
fun:_PyObject_GC_New
fun:COMMENT_THIS_LINE_TO_DISABLE_LEAK_WARNING
}
{
Handle PyMalloc confusing valgrind (possibly leaked)
Memcheck:Leak
fun:malloc
fun:_PyObject_GC_NewVar
fun:COMMENT_THIS_LINE_TO_DISABLE_LEAK_WARNING
}
#
# Non-python specific leaks
#
{
Handle pthread issue (possibly leaked)
Memcheck:Leak
fun:calloc
fun:allocate_dtv
fun:_dl_allocate_tls_storage
fun:_dl_allocate_tls
}
{
Handle pthread issue (possibly leaked)
Memcheck:Leak
fun:memalign
fun:_dl_allocate_tls_storage
fun:_dl_allocate_tls
}
{
ADDRESS_IN_RANGE/Invalid read of size 4
Memcheck:Addr4
fun:PyObject_Free
}
{
ADDRESS_IN_RANGE/Invalid read of size 4
Memcheck:Value4
fun:PyObject_Free
}
{
ADDRESS_IN_RANGE/Conditional jump or move depends on uninitialised value
Memcheck:Cond
fun:PyObject_Free
}
{
ADDRESS_IN_RANGE/Invalid read of size 4
Memcheck:Addr4
fun:PyObject_Realloc
}
{
ADDRESS_IN_RANGE/Invalid read of size 4
Memcheck:Value4
fun:PyObject_Realloc
}
{
ADDRESS_IN_RANGE/Conditional jump or move depends on uninitialised value
Memcheck:Cond
fun:PyObject_Realloc
}
###
### All the suppressions below are for errors that occur within libraries
### that Python uses. The problems to not appear to be related to Python's
### use of the libraries.
###
{
Generic ubuntu ld problems
Memcheck:Addr8
obj:/lib/ld-2.4.so
obj:/lib/ld-2.4.so
obj:/lib/ld-2.4.so
obj:/lib/ld-2.4.so
}
{
Generic gentoo ld problems
Memcheck:Cond
obj:/lib/ld-2.3.4.so
obj:/lib/ld-2.3.4.so
obj:/lib/ld-2.3.4.so
obj:/lib/ld-2.3.4.so
}
{
DBM problems, see test_dbm
Memcheck:Param
write(buf)
fun:write
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
fun:dbm_close
}
{
DBM problems, see test_dbm
Memcheck:Value8
fun:memmove
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
fun:dbm_store
fun:dbm_ass_sub
}
{
DBM problems, see test_dbm
Memcheck:Cond
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
fun:dbm_store
fun:dbm_ass_sub
}
{
DBM problems, see test_dbm
Memcheck:Cond
fun:memmove
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
obj:/usr/lib/libdb1.so.2
fun:dbm_store
fun:dbm_ass_sub
}
{
GDBM problems, see test_gdbm
Memcheck:Param
write(buf)
fun:write
fun:gdbm_open
}
{
ZLIB problems, see test_gzip
Memcheck:Cond
obj:/lib/libz.so.1.2.3
obj:/lib/libz.so.1.2.3
fun:deflate
}
{
Avoid problems w/readline doing a putenv and leaking on exit
Memcheck:Leak
fun:malloc
fun:xmalloc
fun:sh_set_lines_and_columns
fun:_rl_get_screen_size
fun:_rl_init_terminal_io
obj:/lib/libreadline.so.4.3
fun:rl_initialize
}
###
### These occur from somewhere within the SSL, when running
### test_socket_sll. They are too general to leave on by default.
###
###{
### somewhere in SSL stuff
### Memcheck:Cond
### fun:memset
###}
###{
### somewhere in SSL stuff
### Memcheck:Value4
### fun:memset
###}
###
###{
### somewhere in SSL stuff
### Memcheck:Cond
### fun:MD5_Update
###}
###
###{
### somewhere in SSL stuff
### Memcheck:Value4
### fun:MD5_Update
###}
#
# All of these problems come from using test_socket_ssl
#
{
from test_socket_ssl
Memcheck:Cond
fun:BN_bin2bn
}
{
from test_socket_ssl
Memcheck:Cond
fun:BN_num_bits_word
}
{
from test_socket_ssl
Memcheck:Value4
fun:BN_num_bits_word
}
{
from test_socket_ssl
Memcheck:Cond
fun:BN_mod_exp_mont_word
}
{
from test_socket_ssl
Memcheck:Cond
fun:BN_mod_exp_mont
}
{
from test_socket_ssl
Memcheck:Param
write(buf)
fun:write
obj:/usr/lib/libcrypto.so.0.9.7
}
{
from test_socket_ssl
Memcheck:Cond
fun:RSA_verify
}
{
from test_socket_ssl
Memcheck:Value4
fun:RSA_verify
}
{
from test_socket_ssl
Memcheck:Value4
fun:DES_set_key_unchecked
}
{
from test_socket_ssl
Memcheck:Value4
fun:DES_encrypt2
}
{
from test_socket_ssl
Memcheck:Cond
obj:/usr/lib/libssl.so.0.9.7
}
{
from test_socket_ssl
Memcheck:Value4
obj:/usr/lib/libssl.so.0.9.7
}
{
from test_socket_ssl
Memcheck:Cond
fun:BUF_MEM_grow_clean
}
{
from test_socket_ssl
Memcheck:Cond
fun:memcpy
fun:ssl3_read_bytes
}
{
from test_socket_ssl
Memcheck:Cond
fun:SHA1_Update
}
{
from test_socket_ssl
Memcheck:Value4
fun:SHA1_Update
}

View File

@@ -34,10 +34,28 @@
extern PyTypeObject ConfigType;
PyObject *
wrap_config(char *c_path) {
int err;
PyObject *py_path;
Config *py_config;
py_path = Py_BuildValue("(s)", c_path);
py_config = PyObject_New(Config, &ConfigType);
err = Config_init(py_config, py_path, NULL);
if (err < 0)
return NULL;
return (PyObject*) py_config;
}
int
Config_init(Config *self, PyObject *args, PyObject *kwds)
{
char *path;
char *path = NULL;
int err;
if (kwds) {
@@ -46,18 +64,17 @@ Config_init(Config *self, PyObject *args, PyObject *kwds)
return -1;
}
if (PySequence_Length(args) > 0) {
if (!PyArg_ParseTuple(args, "s", &path)) {
return -1;
}
if (!PyArg_ParseTuple(args, "|s", &path))
return -1;
if (path == NULL)
err = git_config_new(&self->config);
else
err = git_config_open_ondisk(&self->config, path);
} else {
err = git_config_new(&self->config);
}
if (err < 0) {
git_config_free(self->config);
if (err == GIT_ENOTFOUND) {
Error_set_exc(PyExc_IOError);
} else {
@@ -70,35 +87,14 @@ Config_init(Config *self, PyObject *args, PyObject *kwds)
return 0;
}
void
Config_dealloc(Config *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->repo);
git_config_free(self->config);
PyObject_GC_Del(self);
PyObject_Del(self);
}
int
Config_traverse(Config *self, visitproc visit, void *arg)
{
Py_VISIT(self->repo);
return 0;
}
PyObject *
Config_open(char *c_path) {
PyObject *py_path = Py_BuildValue("(s)", c_path);
Config *config = PyObject_GC_New(Config, &ConfigType);
Config_init(config, py_path, NULL);
Py_INCREF(config);
return (PyObject *)config;
}
PyDoc_STRVAR(Config_get_global_config__doc__,
"get_global_config() -> Config\n"
"\n"
@@ -116,10 +112,11 @@ Config_get_global_config(void)
PyErr_SetString(PyExc_IOError, "Global config file not found.");
return NULL;
}
return Error_set(err);
}
return Config_open(path);
return wrap_config(path);
}
@@ -143,9 +140,10 @@ Config_get_system_config(void)
return Error_set(err);
}
return Config_open(path);
return wrap_config(path);
}
int
Config_contains(Config *self, PyObject *py_key) {
int err;
@@ -158,9 +156,11 @@ Config_contains(Config *self, PyObject *py_key) {
err = git_config_get_string(&c_value, self->config, c_key);
free(c_key);
if (err == GIT_ENOTFOUND)
return 0;
if (err < 0) {
if (err == GIT_ENOTFOUND)
return 0;
Error_set(err);
return -1;
}
@@ -168,69 +168,71 @@ Config_contains(Config *self, PyObject *py_key) {
return 1;
}
PyObject *
Config_getitem(Config *self, PyObject *py_key)
{
int err;
int64_t c_intvalue;
int c_boolvalue;
const char *c_charvalue;
char *c_key;
long value_int;
int err, value_bool;
const char *value_str;
char *key;
PyObject* py_value;
if (!(c_key = py_str_to_c_str(py_key, NULL)))
key = py_str_to_c_str(py_key, NULL);
if (key == NULL)
return NULL;
err = git_config_get_int64(&c_intvalue, self->config, c_key);
if (err == GIT_OK) {
free(c_key);
return PyInt_FromLong((long)c_intvalue);
}
err = git_config_get_string(&value_str, self->config, key);
if (err < 0)
goto cleanup;
err = git_config_get_bool(&c_boolvalue, self->config, c_key);
if (err == GIT_OK) {
free(c_key);
return PyBool_FromLong((long)c_boolvalue);
}
if (git_config_parse_int64(&value_int, value_str) == 0)
py_value = PyLong_FromLong(value_int);
else if(git_config_parse_bool(&value_bool, value_str) == 0)
py_value = PyBool_FromLong(value_bool);
else
py_value = PyUnicode_FromString(value_str);
cleanup:
free(key);
err = git_config_get_string(&c_charvalue, self->config, c_key);
free(c_key);
if (err < 0) {
if (err == GIT_ENOTFOUND) {
PyErr_SetObject(PyExc_KeyError, py_key);
return NULL;
}
return Error_set(err);
}
return PyUnicode_FromString(c_charvalue);
return py_value;
}
int
Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
{
int err;
char *c_key;
char *py_str;
char *key, *value;
if (!(c_key = py_str_to_c_str(py_key, NULL)))
key = py_str_to_c_str(py_key, NULL);
if (key == NULL)
return -1;
if (!py_value) {
err = git_config_delete_entry(self->config, c_key);
} else if (PyBool_Check(py_value)) {
err = git_config_set_bool(self->config, c_key,
if (py_value == NULL)
err = git_config_delete_entry(self->config, key);
else if (PyBool_Check(py_value)) {
err = git_config_set_bool(self->config, key,
(int)PyObject_IsTrue(py_value));
} else if (PyInt_Check(py_value)) {
err = git_config_set_int64(self->config, c_key,
err = git_config_set_int64(self->config, key,
(int64_t)PyInt_AsLong(py_value));
} else {
py_value = PyObject_Str(py_value);
py_str = py_str_to_c_str(py_value, NULL);
err = git_config_set_string(self->config, c_key, py_str);
free(py_str);
value = py_str_to_c_str(py_value, NULL);
err = git_config_set_string(self->config, key, value);
free(value);
}
free(c_key);
free(key);
if (err < 0) {
Error_set(err);
return -1;
@@ -263,6 +265,8 @@ Config_foreach_callback_wrapper(const git_config_entry *entry, void *c_payload)
if ((c_result = PyLong_AsLong(py_result) == -1))
return -1;
Py_CLEAR(args);
return c_result;
}
@@ -282,7 +286,7 @@ Config_foreach(Config *self, PyObject *args)
{
int ret;
PyObject *py_callback;
PyObject *py_payload;
PyObject *py_payload = NULL;
if (!PyArg_ParseTuple(args, "O|O", &py_callback, &py_payload))
return NULL;
@@ -338,13 +342,14 @@ PyDoc_STRVAR(Config_get_multivar__doc__,
int
Config_get_multivar_fn_wrapper(const git_config_entry *value, void *data)
{
PyObject *list = (PyObject *)data;
PyObject *item = NULL;
if (!(item = PyUnicode_FromString(value->value)))
return -2;
PyList_Append(list, item);
PyList_Append((PyObject *)data, item);
Py_CLEAR(item);
return 0;
}
@@ -353,17 +358,20 @@ PyObject *
Config_get_multivar(Config *self, PyObject *args)
{
int err;
PyObject *list = PyList_New(0);
PyObject *list;
const char *name = NULL;
const char *regex = NULL;
if (!PyArg_ParseTuple(args, "s|s", &name, &regex))
return NULL;
list = PyList_New(0);
err = git_config_get_multivar(self->config, name, regex,
Config_get_multivar_fn_wrapper,
(void *)list);
if (err < 0) {
Py_CLEAR(list);
if (err == GIT_ENOTFOUND)
Error_set(err);
else
@@ -454,9 +462,9 @@ PyTypeObject ConfigType = {
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Py_TPFLAGS_DEFAULT, /* tp_flags */
Config__doc__, /* tp_doc */
(traverseproc)Config_traverse, /* tp_traverse */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */

View File

@@ -40,123 +40,106 @@ extern PyTypeObject IndexType;
extern PyTypeObject DiffType;
extern PyTypeObject HunkType;
PyTypeObject DiffEntryType;
PyTypeObject PatchType;
PyObject*
diff_get_patch_byindex(git_diff_list* list, size_t i)
diff_get_patch_byindex(git_diff_list* list, size_t idx)
{
const git_diff_delta* delta;
const git_diff_range* range;
git_diff_patch* patch = NULL;
char buffer[41];
const char* hunk_content;
size_t hunk_amounts, j, hunk_header_len, hunk_lines;
size_t i, j, hunk_amounts, lines_in_hunk, line_len, header_len;
const char* line, *header;
int err;
Hunk *py_hunk = NULL;
Patch *py_patch = NULL;
PyObject *file;
Hunk *py_hunk;
DiffEntry *py_entry = NULL;
err = git_diff_get_patch(&patch, &delta, list, idx);
if (err < 0)
return Error_set(err);
err = git_diff_get_patch(&patch, &delta, list, i);
py_patch = (Patch*) PyType_GenericNew(&PatchType, NULL, NULL);
if (py_patch != NULL) {
py_patch->old_file_path = delta->old_file.path;
py_patch->new_file_path = delta->new_file.path;
py_patch->status = delta->status;
py_patch->similarity = delta->similarity;
py_patch->old_oid = git_oid_allocfmt(&delta->old_file.oid);
py_patch->new_oid = git_oid_allocfmt(&delta->new_file.oid);
if (err == GIT_OK) {
py_entry = (DiffEntry*) INSTANCIATE_CLASS(DiffEntryType, NULL);
if (py_entry != NULL) {
if (err == GIT_OK) {
file = Py_BuildValue("(s,s,i,i)",
delta->old_file.path,
delta->new_file.path,
delta->status,
delta->similarity
);
PyList_Append((PyObject*) py_entry->files, file);
}
hunk_amounts = git_diff_patch_num_hunks(patch);
py_patch->hunks = PyList_New(hunk_amounts);
for (i=0; i < hunk_amounts; ++i) {
err = git_diff_patch_get_hunk(&range, &header, &header_len,
&lines_in_hunk, patch, i);
hunk_amounts = git_diff_patch_num_hunks(patch);
if (err < 0)
goto cleanup;
for (j=0; j < hunk_amounts; ++j) {
err = git_diff_patch_get_hunk(&range, &hunk_content,
&hunk_header_len, &hunk_lines, patch, j);
py_hunk = (Hunk*)PyType_GenericNew(&HunkType, NULL, NULL);
if (py_hunk != NULL) {
py_hunk->old_start = range->old_start;
py_hunk->old_lines = range->old_lines;
py_hunk->new_start = range->new_start;
py_hunk->new_lines = range->new_lines;
if (err == GIT_OK) {
py_hunk = (Hunk*)PyType_GenericNew(&HunkType, NULL, NULL);
if (py_hunk != NULL) {
py_hunk->old_file = delta->old_file.path;
py_hunk->new_file = delta->new_file.path;
py_hunk->header = hunk_content;
py_hunk->old_start = range->old_start;
py_hunk->old_lines = range->old_lines;
py_hunk->new_start = range->new_start;
py_hunk->new_lines = range->new_lines;
py_hunk->lines = PyList_New(lines_in_hunk + 1);
PyList_SetItem(py_hunk->lines, 0,
PyUnicode_FromStringAndSize(header, header_len));
for (j=1; j < lines_in_hunk + 1; ++j) {
err = git_diff_patch_get_line_in_hunk(NULL, &line,
&line_len, NULL, NULL, patch, i, j - 1);
git_oid_fmt(buffer, &delta->old_file.oid);
py_hunk->old_oid = calloc(41, sizeof(char));
memcpy(py_hunk->old_oid, buffer, 40);
if (err < 0)
goto cleanup;
git_oid_fmt(buffer, &delta->new_file.oid);
py_hunk->new_oid = calloc(41, sizeof(char));
memcpy(py_hunk->new_oid, buffer, 40);
py_hunk->data = Py_BuildValue("(s#,i)",
hunk_content, hunk_header_len,
hunk_lines);
PyList_Append((PyObject*) py_entry->hunks,
(PyObject*) py_hunk);
}
PyList_SetItem(py_hunk->lines, j,
PyUnicode_FromStringAndSize(line, line_len));
}
PyList_SetItem((PyObject*) py_patch->hunks, i,
(PyObject*) py_hunk);
}
}
}
if (err < 0)
return Error_set(err);
cleanup:
git_diff_patch_free(patch);
return (PyObject*) py_entry;
}
PyObject *
DiffEntry_call(DiffEntry *self, PyObject *args, PyObject *kwds)
{
self->files = PyList_New(0);
if (self->files == NULL) {
Py_XDECREF(self);
return NULL;
}
self->hunks = PyList_New(0);
if (self->hunks == NULL) {
Py_XDECREF(self);
return NULL;
}
return (PyObject*) self;
return (err < 0) ? Error_set(err) : (PyObject*) py_patch;
}
static void
DiffEntry_dealloc(DiffEntry *self)
Patch_dealloc(Patch *self)
{
Py_DECREF((PyObject*) self->files);
Py_DECREF((PyObject*) self->hunks);
Py_CLEAR(self->hunks);
free(self->old_oid);
free(self->new_oid);
// we do not have to free old_file_path and new_file_path, they will
// be freed by git_diff_list_free in Diff_dealloc
PyObject_Del(self);
}
PyMemberDef DiffEntry_members[] = {
MEMBER(DiffEntry, files, T_OBJECT, "files"),
MEMBER(DiffEntry, hunks, T_OBJECT, "hunks"),
PyMemberDef Patch_members[] = {
MEMBER(Patch, old_file_path, T_STRING, "old file path"),
MEMBER(Patch, new_file_path, T_STRING, "new file path"),
MEMBER(Patch, old_oid, T_STRING, "old oid"),
MEMBER(Patch, new_oid, T_STRING, "new oid"),
MEMBER(Patch, status, T_INT, "status"),
MEMBER(Patch, similarity, T_INT, "similarity"),
MEMBER(Patch, hunks, T_OBJECT, "hunks"),
{NULL}
};
PyDoc_STRVAR(DiffEntry__doc__, "Diff entry object.");
PyDoc_STRVAR(Patch__doc__, "Diff patch object.");
PyTypeObject DiffEntryType = {
PyTypeObject PatchType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pygit2.DiffEntry", /* tp_name */
sizeof(DiffEntry), /* tp_basicsize */
"_pygit2.Patch", /* tp_name */
sizeof(Patch), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)DiffEntry_dealloc, /* tp_dealloc */
(destructor)Patch_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
@@ -166,13 +149,13 @@ PyTypeObject DiffEntryType = {
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc) DiffEntry_call, /* tp_call */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
DiffEntry__doc__, /* tp_doc */
Patch__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
@@ -180,7 +163,7 @@ PyTypeObject DiffEntryType = {
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
DiffEntry_members, /* tp_members */
Patch_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
@@ -197,7 +180,7 @@ PyObject *
DiffIter_iternext(DiffIter *self)
{
if (self->i < self->n)
return diff_get_patch_byindex(self->list, self->i++);
return diff_get_patch_byindex(self->diff->list, self->i++);
PyErr_SetNone(PyExc_StopIteration);
return NULL;
@@ -206,7 +189,7 @@ DiffIter_iternext(DiffIter *self)
void
DiffIter_dealloc(DiffIter *self)
{
Py_CLEAR(self->list);
Py_CLEAR(self->diff);
PyObject_Del(self);
}
@@ -251,37 +234,39 @@ Diff_patch__get__(Diff *self)
{
const git_diff_delta* delta;
git_diff_patch* patch;
char* str = NULL, *buffer = NULL;
int err = 0;
size_t i, len, num, size;
char **strings = NULL;
char *buffer = NULL;
int err = GIT_ERROR;
size_t i, len, num;
PyObject *py_patch = NULL;
num = git_diff_num_deltas(self->list);
for (i = 0; i < num ; ++i) {
MALLOC(strings, num * sizeof(char*), cleanup);
for (i = 0, len = 1; i < num ; ++i) {
err = git_diff_get_patch(&patch, &delta, self->list, i);
if (err < 0)
goto cleanup;
err = git_diff_patch_to_str(&(strings[i]), patch);
if (err < 0)
goto cleanup;
if (err < 0 || (err = git_diff_patch_to_str(&str, patch)) < 0)
goto error;
len = strlen(str) + 1;
size = (buffer == NULL) ? len : strlen(buffer) + len;
MALLOC(buffer, size, error);
if (len == size)
strcpy(buffer, str);
else
strcat(buffer, str);
FREE(str);
len += strlen(strings[i]);
git_diff_patch_free(patch);
}
CALLOC(buffer, (len + 1), sizeof(char), cleanup);
for (i = 0; i < num; ++i) {
strcat(buffer, strings[i]);
free(strings[i]);
}
free(strings);
py_patch = PyUnicode_FromString(buffer);
free(buffer);
error:
FREE(str);
FREE(buffer);
FREE_FUNC(patch, git_diff_patch_free);
cleanup:
return (err < 0) ? Error_set(err) : py_patch;
}
@@ -289,34 +274,16 @@ error:
static void
Hunk_dealloc(Hunk *self)
{
if (self->header != NULL) {
free((void*) self->header);
}
if (self->new_file != NULL) {
free((void*) self->new_file);
}
if (self->old_file != NULL) {
free((void*) self->old_file);
}
Py_XDECREF(self->old_oid);
Py_XDECREF(self->new_oid);
Py_XDECREF(self->data);
Py_CLEAR(self->lines);
PyObject_Del(self);
}
PyMemberDef Hunk_members[] = {
MEMBER(Hunk, header, T_STRING, "Header."),
MEMBER(Hunk, old_start, T_INT, "Old start."),
MEMBER(Hunk, old_lines, T_INT, "Old lines."),
MEMBER(Hunk, old_mode, T_INT, "Old mode."),
MEMBER(Hunk, old_file, T_STRING, "Old file."),
MEMBER(Hunk, old_oid, T_STRING, "Old oid."),
MEMBER(Hunk, new_start, T_INT, "New start."),
MEMBER(Hunk, new_lines, T_INT, "New lines."),
MEMBER(Hunk, new_mode, T_INT, "New mode."),
MEMBER(Hunk, new_file, T_STRING, "New file."),
MEMBER(Hunk, new_oid, T_STRING, "New oid."),
MEMBER(Hunk, data, T_OBJECT, "Data."),
MEMBER(Hunk, lines, T_OBJECT, "Lines."),
{NULL}
};
@@ -393,7 +360,7 @@ Diff_merge(Diff *self, PyObject *args)
PyDoc_STRVAR(Diff_find_similar__doc__,
"find_similar([flags])\n"
"\n"
"Find renamed files in diff.");
"Find renamed files in diff and updates them in-place in the diff itself.");
PyObject *
Diff_find_similar(Diff *self, PyObject *args)
@@ -417,9 +384,9 @@ Diff_iter(Diff *self)
DiffIter *iter;
iter = PyObject_New(DiffIter, &DiffIterType);
if (iter) {
if (iter != NULL) {
Py_INCREF(self);
iter->list = self->list;
iter->diff = self;
iter->i = 0;
iter->n = git_diff_num_deltas(self->list);
}
@@ -444,7 +411,7 @@ static void
Diff_dealloc(Diff *self)
{
git_diff_list_free(self->list);
Py_XDECREF(self->repo);
Py_CLEAR(self->repo);
PyObject_Del(self);
}

236
src/note.c Normal file
View File

@@ -0,0 +1,236 @@
/*
* Copyright 2010-2013 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include <pygit2/error.h>
#include <pygit2/utils.h>
#include <pygit2/types.h>
#include <pygit2/oid.h>
#include <pygit2/note.h>
extern PyTypeObject SignatureType;
PyDoc_STRVAR(Note_remove__doc__,
"Removes a note for an annotated object");
PyObject*
Note_remove(Note *self, PyObject* args)
{
char *ref = "refs/notes/commits";
int err = GIT_ERROR;
git_oid annotated_id;
Signature *py_author, *py_committer;
if (!PyArg_ParseTuple(args, "O!O!|s",
&SignatureType, &py_author,
&SignatureType, &py_committer,
&ref))
return NULL;
err = git_oid_fromstr(&annotated_id, self->annotated_id);
if (err < 0)
return Error_set(err);
err = git_note_remove(self->repo->repo, ref, py_author->signature,
py_committer->signature, &annotated_id);
if (err < 0)
return Error_set(err);
Py_RETURN_NONE;
}
PyDoc_STRVAR(Note_oid__doc__,
"Gets the id of the blob containing the note message\n");
PyObject *
Note_oid__get__(Note *self)
{
return git_oid_to_py_str(git_note_oid(self->note));
}
PyDoc_STRVAR(Note_message__doc__,
"Gets message of the note\n");
PyObject *
Note_message__get__(Note *self)
{
return PyUnicode_FromString(git_note_message(self->note));
}
static void
Note_dealloc(Note *self)
{
Py_CLEAR(self->repo);
free(self->annotated_id);
git_note_free(self->note);
PyObject_Del(self);
}
PyMethodDef Note_methods[] = {
METHOD(Note, remove, METH_VARARGS),
{NULL}
};
PyMemberDef Note_members[] = {
MEMBER(Note, annotated_id, T_STRING, "id of the annotated object."),
{NULL}
};
PyGetSetDef Note_getseters[] = {
GETTER(Note, message),
GETTER(Note, oid),
{NULL}
};
PyDoc_STRVAR(Note__doc__, "Note object.");
PyTypeObject NoteType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pygit2.Note", /* tp_name */
sizeof(Note), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Note_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Note__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Note_methods, /* tp_methods */
Note_members, /* tp_members */
Note_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
PyObject *
NoteIter_iternext(NoteIter *self)
{
int err;
git_oid note_id, annotated_id;
err = git_note_next(&note_id, &annotated_id, self->iter);
if (err < 0)
return Error_set(err);
return (PyObject*) wrap_note(self->repo, &annotated_id, self->ref);
}
void
NoteIter_dealloc(NoteIter *self)
{
git_note_iterator_free(self->iter);
PyObject_Del(self);
}
PyDoc_STRVAR(NoteIter__doc__, "Note iterator object.");
PyTypeObject NoteIterType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_pygit2.NoteIter", /* tp_name */
sizeof(NoteIter), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)NoteIter_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
NoteIter__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc) NoteIter_iternext, /* tp_iternext */
};
PyObject*
wrap_note(Repository* repo, git_oid* annotated_id, const char* ref)
{
Note* py_note = NULL;
int err = GIT_ERROR;
py_note = (Note*) PyType_GenericNew(&NoteType, NULL, NULL);
if (py_note == NULL) {
PyErr_NoMemory();
return NULL;
}
err = git_note_read(&py_note->note, repo->repo, ref, annotated_id);
if (err < 0)
return Error_set(err);
py_note->repo = repo;
Py_INCREF(repo);
py_note->annotated_id = git_oid_allocfmt(annotated_id);
return (PyObject*) py_note;
}

View File

@@ -43,8 +43,8 @@ extern PyTypeObject TagType;
void
Object_dealloc(Object* self)
{
Py_CLEAR(self->repo);
git_object_free(self->obj);
Py_XDECREF(self->repo);
PyObject_Del(self);
}

View File

@@ -42,7 +42,7 @@ extern PyTypeObject ObjectType;
extern PyTypeObject CommitType;
extern PyTypeObject DiffType;
extern PyTypeObject DiffIterType;
extern PyTypeObject DiffEntryType;
extern PyTypeObject PatchType;
extern PyTypeObject HunkType;
extern PyTypeObject TreeType;
extern PyTypeObject TreeBuilderType;
@@ -60,6 +60,8 @@ extern PyTypeObject RefLogIterType;
extern PyTypeObject RefLogEntryType;
extern PyTypeObject SignatureType;
extern PyTypeObject RemoteType;
extern PyTypeObject NoteType;
extern PyTypeObject NoteIterType;
@@ -69,7 +71,7 @@ PyDoc_STRVAR(init_repository__doc__,
"Creates a new Git repository in the given path.");
PyObject *
init_repository(PyObject *self, PyObject *args, PyObject *kw) {
init_repository(PyObject *self, PyObject *args) {
git_repository *repo;
const char *path;
unsigned int bare;
@@ -201,7 +203,7 @@ moduleinit(PyObject* m)
return NULL;
if (PyType_Ready(&DiffIterType) < 0)
return NULL;
if (PyType_Ready(&DiffEntryType) < 0)
if (PyType_Ready(&PatchType) < 0)
return NULL;
if (PyType_Ready(&HunkType) < 0)
return NULL;
@@ -236,6 +238,11 @@ moduleinit(PyObject* m)
if (PyType_Ready(&RemoteType) < 0)
return NULL;
if (PyType_Ready(&NoteType) < 0)
return NULL;
if (PyType_Ready(&NoteIterType) < 0)
return NULL;
Py_INCREF(GitError);
PyModule_AddObject(m, "GitError", GitError);
@@ -281,6 +288,9 @@ moduleinit(PyObject* m)
Py_INCREF(&RemoteType);
PyModule_AddObject(m, "Remote", (PyObject *)&RemoteType);
Py_INCREF(&NoteType);
PyModule_AddObject(m, "Note", (PyObject *)&NoteType);
PyModule_AddIntConstant(m, "GIT_OBJ_ANY", GIT_OBJ_ANY);
PyModule_AddIntConstant(m, "GIT_OBJ_COMMIT", GIT_OBJ_COMMIT);
PyModule_AddIntConstant(m, "GIT_OBJ_TREE", GIT_OBJ_TREE);

View File

@@ -43,9 +43,8 @@ extern PyTypeObject RefLogEntryType;
void RefLogIter_dealloc(RefLogIter *self)
{
Py_XDECREF(self->reference);
git_reflog_free(self->reflog);
PyObject_GC_Del(self);
PyObject_Del(self);
}
PyObject* RefLogIter_iternext(PyObject *self)
@@ -146,7 +145,9 @@ Reference_delete(Reference *self, PyObject *args)
if (err < 0)
return Error_set(err);
git_reference_free(self->reference);
self->reference = NULL; /* Invalidate the pointer */
Py_RETURN_NONE;
}
@@ -172,6 +173,7 @@ Reference_rename(Reference *self, PyObject *py_name)
/* Rename */
err = git_reference_rename(&new_reference, self->reference, c_name, 0);
git_reference_free(self->reference);
free(c_name);
if (err < 0)
return Error_set(err);
@@ -255,6 +257,7 @@ Reference_target__set__(Reference *self, PyObject *py_name)
return -1;
}
git_reference_free(self->reference);
self->reference = new_ref;
return 0;
}
@@ -316,6 +319,7 @@ Reference_oid__set__(Reference *self, PyObject *py_hex)
return -1;
}
git_reference_free(self->reference);
self->reference = new_ref;
return 0;
}
@@ -371,14 +375,10 @@ Reference_log(Reference *self)
CHECK_REFERENCE(self);
iter = PyObject_New(RefLogIter, &RefLogIterType);
if (iter) {
iter->reference = self;
if (iter != NULL) {
git_reflog_read(&iter->reflog, self->reference);
iter->size = git_reflog_entrycount(iter->reflog);
iter->i = 0;
Py_INCREF(self);
Py_INCREF(iter);
}
return (PyObject*)iter;
}
@@ -398,9 +398,9 @@ RefLogEntry_init(RefLogEntry *self, PyObject *args, PyObject *kwds)
static void
RefLogEntry_dealloc(RefLogEntry *self)
{
Py_XDECREF(self->oid_old);
Py_XDECREF(self->oid_new);
Py_XDECREF(self->committer);
Py_CLEAR(self->oid_old);
Py_CLEAR(self->oid_new);
Py_CLEAR(self->committer);
free(self->message);
PyObject_Del(self);
}

View File

@@ -31,12 +31,13 @@
#include <pygit2/error.h>
#include <pygit2/utils.h>
#include <pygit2/types.h>
#include <pygit2/remote.h>
extern PyObject *GitError;
extern PyTypeObject RepositoryType;
PyObject *
Remote_call(Remote *self, PyObject *args, PyObject *kwds)
Remote_init(Remote *self, PyObject *args, PyObject *kwds)
{
Repository* py_repo = NULL;
char *name = NULL;
@@ -46,6 +47,7 @@ Remote_call(Remote *self, PyObject *args, PyObject *kwds)
return NULL;
self->repo = py_repo;
Py_INCREF(self->repo);
err = git_remote_load(&self->remote, py_repo->repo, name);
if (err < 0)
@@ -58,6 +60,7 @@ Remote_call(Remote *self, PyObject *args, PyObject *kwds)
static void
Remote_dealloc(Remote *self)
{
Py_CLEAR(self->repo);
git_remote_free(self->remote);
PyObject_Del(self);
}
@@ -80,6 +83,7 @@ Remote_name__set__(Remote *self, PyObject* py_name)
name = py_str_to_c_str(py_name, NULL);
if (name != NULL) {
err = git_remote_rename(self->remote, name, NULL, NULL);
free(name);
if (err == GIT_OK)
return 0;
@@ -104,11 +108,12 @@ int
Remote_url__set__(Remote *self, PyObject* py_url)
{
int err;
char* url;
char* url = NULL;
url = py_str_to_c_str(py_url, NULL);
if (url != NULL) {
err = git_remote_set_url(self->remote, url);
free(url);
if (err == GIT_OK)
return 0;
@@ -239,7 +244,7 @@ PyTypeObject RemoteType = {
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
(ternaryfunc) Remote_call, /* tp_call */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
@@ -260,7 +265,7 @@ PyTypeObject RemoteType = {
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
(initproc)Remote_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};

View File

@@ -33,7 +33,9 @@
#include <pygit2/utils.h>
#include <pygit2/object.h>
#include <pygit2/oid.h>
#include <pygit2/note.h>
#include <pygit2/repository.h>
#include <pygit2/remote.h>
extern PyObject *GitError;
@@ -46,6 +48,8 @@ extern PyTypeObject ConfigType;
extern PyTypeObject DiffType;
extern PyTypeObject RemoteType;
extern PyTypeObject ReferenceType;
extern PyTypeObject NoteType;
extern PyTypeObject NoteIterType;
git_otype
int_to_loose_object_type(int type_id)
@@ -94,6 +98,9 @@ Repository_init(Repository *self, PyObject *args, PyObject *kwds)
return -1;
}
self->config = NULL;
self->index = NULL;
return 0;
}
@@ -101,7 +108,8 @@ void
Repository_dealloc(Repository *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->index);
Py_CLEAR(self->index);
Py_CLEAR(self->config);
git_repository_free(self->repo);
PyObject_GC_Del(self);
}
@@ -464,7 +472,7 @@ PyDoc_STRVAR(Repository_config__doc__,
"(if they are available).");
PyObject *
Repository_config__get__(Repository *self, void *closure)
Repository_config__get__(Repository *self)
{
int err;
git_config *config;
@@ -477,20 +485,18 @@ Repository_config__get__(Repository *self, void *closure)
if (err < 0)
return Error_set(err);
py_config = PyObject_GC_New(Config, &ConfigType);
if (!py_config) {
py_config = PyObject_New(Config, &ConfigType);
if (py_config == NULL) {
git_config_free(config);
return NULL;
}
Py_INCREF(self);
py_config->repo = self;
py_config->config = config;
PyObject_GC_Track(py_config);
self->config = (PyObject*)py_config;
} else {
Py_INCREF(self->config);
}
Py_INCREF(self->config);
return self->config;
}
@@ -785,6 +791,7 @@ Repository_lookup_reference(Repository *self, PyObject *py_name)
return err_obj;
}
free(c_name);
/* 3- Make an instance of Reference and return it */
return wrap_reference(c_reference);
}
@@ -810,7 +817,7 @@ Repository_git_reference_create(Repository *self, PyObject *args,
{
PyObject *py_obj;
git_reference *c_reference;
char *c_name, *c_target;
char *c_name;
git_oid oid;
int err, force;
@@ -850,7 +857,6 @@ Repository_git_reference_symbolic_create(Repository *self, PyObject *args,
PyObject *py_obj;
git_reference *c_reference;
char *c_name, *c_target;
git_oid oid;
int err, force;
if (!PyArg_ParseTuple(args, "sOi", &c_name, &py_obj, &force))
@@ -859,13 +865,19 @@ Repository_git_reference_symbolic_create(Repository *self, PyObject *args,
#if PY_MAJOR_VERSION == 2
c_target = PyString_AsString(py_obj);
#else
c_target = PyString_AsString(PyUnicode_AsASCIIString(py_obj));
// increases ref counter, so we have to release it afterwards
PyObject* py_str = PyUnicode_AsASCIIString(py_obj);
c_target = PyString_AsString(py_str);
#endif
if (c_target == NULL)
return NULL;
err = git_reference_symbolic_create(&c_reference, self->repo, c_name,
c_target, force);
#if PY_MAJOR_VERSION > 2
Py_CLEAR(py_str);
#endif
if (err < 0)
return Error_set(err);
@@ -885,9 +897,14 @@ read_status_cb(const char *path, unsigned int status_flags, void *payload)
/* This is the callback that will be called in git_status_foreach. It
* will be called for every path.*/
PyObject *flags;
int err;
flags = PyInt_FromLong((long) status_flags);
PyDict_SetItemString(payload, path, flags);
err = PyDict_SetItemString(payload, path, flags);
Py_CLEAR(flags);
if (err < 0)
return GIT_ERROR;
return GIT_OK;
}
@@ -1021,20 +1038,23 @@ PyObject *
Repository_remotes__get__(Repository *self)
{
git_strarray remotes;
PyObject* py_list = NULL, *py_tmp;
PyObject* py_list = NULL, *py_args = NULL;
Remote *py_remote;
size_t i;
git_remote_list(&remotes, self->repo);
py_list = PyList_New(remotes.count);
for (i=0; i < remotes.count; ++i) {
py_tmp = INSTANCIATE_CLASS(RemoteType, Py_BuildValue("Os", self, remotes.strings[i]));
PyList_SetItem(py_list, i, py_tmp);
py_remote = PyObject_New(Remote, &RemoteType);
py_args = Py_BuildValue("Os", self, remotes.strings[i]);
Remote_init(py_remote, py_args, NULL);
PyList_SetItem(py_list, i, (PyObject*) py_remote);
}
git_strarray_free(&remotes);
return py_list;
return (PyObject*) py_list;
}
@@ -1071,6 +1091,7 @@ Repository_checkout(Repository *self, PyObject *args, PyObject *kw)
err = git_repository_set_head(self->repo,
git_reference_name(ref->reference));
}
git_object_free(object);
}
} else { /* checkout from head / index */
opts.checkout_strategy = strategy;
@@ -1085,6 +1106,93 @@ Repository_checkout(Repository *self, PyObject *args, PyObject *kw)
}
PyDoc_STRVAR(Repository_notes__doc__, "");
PyObject *
Repository_notes(Repository *self, PyObject* args)
{
NoteIter *iter = NULL;
char *ref = "refs/notes/commits";
int err = GIT_ERROR;
if (!PyArg_ParseTuple(args, "|s", &ref))
return NULL;
iter = PyObject_New(NoteIter, &NoteIterType);
if (iter != NULL) {
iter->repo = self;
iter->ref = ref;
err = git_note_iterator_new(&iter->iter, self->repo, iter->ref);
if (err == GIT_OK) {
Py_INCREF(self);
return (PyObject*)iter;
}
}
return Error_set(err);
}
PyDoc_STRVAR(Repository_create_note__doc__,
"create_note(message, author, committer, annotated_id [,ref, force]) -> ID\n"
"\n"
"Create a new note for an object, return its SHA-ID."
"If no ref is given 'refs/notes/commits' will be used.");
PyObject *
Repository_create_note(Repository *self, PyObject* args)
{
git_oid note_id, annotated_id;
char *annotated = NULL, *message = NULL, *ref = "refs/notes/commits";
int err = GIT_ERROR;
unsigned int force = 0;
Signature *py_author, *py_committer;
if (!PyArg_ParseTuple(args, "sO!O!s|si",
&message,
&SignatureType, &py_author,
&SignatureType, &py_committer,
&annotated, &ref, &force))
return NULL;
err = git_oid_fromstr(&annotated_id, annotated);
if (err < 0)
return Error_set(err);
err = git_note_create(&note_id, self->repo, py_author->signature,
py_committer->signature, ref,
&annotated_id, message, force);
if (err < 0)
return Error_set(err);
return git_oid_to_python(note_id.id);
}
PyDoc_STRVAR(Repository_lookup_note__doc__,
"lookup_note(annotated_id [, ref]) -> Note\n"
"\n"
"Lookup a note for an annotated object in a repository.");
PyObject *
Repository_lookup_note(Repository *self, PyObject* args)
{
git_oid annotated_id;
char* annotated = NULL, *ref = "refs/notes/commits";
int err;
if (!PyArg_ParseTuple(args, "s|s", &annotated, &ref))
return NULL;
err = git_oid_fromstr(&annotated_id, annotated);
if (err < 0)
return Error_set(err);
return (PyObject*) wrap_note(self, &annotated_id, ref);
}
PyMethodDef Repository_methods[] = {
METHOD(Repository, create_blob, METH_VARARGS),
METHOD(Repository, create_blob_fromfile, METH_VARARGS),
@@ -1103,6 +1211,9 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, status_file, METH_O),
METHOD(Repository, create_remote, METH_VARARGS),
METHOD(Repository, checkout, METH_VARARGS|METH_KEYWORDS),
METHOD(Repository, notes, METH_VARARGS),
METHOD(Repository, create_note, METH_VARARGS),
METHOD(Repository, lookup_note, METH_VARARGS),
METHOD(Repository, git_object_lookup_prefix, METH_O),
{NULL}
};

View File

@@ -82,12 +82,13 @@ void
Signature_dealloc(Signature *self)
{
if (self->obj)
Py_DECREF(self->obj);
Py_CLEAR(self->obj);
else {
git_signature_free((git_signature*)self->signature);
free((char*)self->encoding);
}
Py_TYPE(self)->tp_free((PyObject*)self);
PyObject_Del(self);
}
@@ -221,6 +222,7 @@ build_signature(Object *obj, const git_signature *signature,
Signature *py_signature;
py_signature = PyObject_New(Signature, &SignatureType);
if (py_signature) {
Py_INCREF(obj);
py_signature->obj = obj;

View File

@@ -42,7 +42,7 @@ extern PyTypeObject IndexType;
void
TreeEntry_dealloc(TreeEntry *self)
{
Py_XDECREF(self->owner);
Py_CLEAR(self->owner);
git_tree_entry_free((git_tree_entry*)self->entry);
PyObject_Del(self);
}
@@ -339,6 +339,7 @@ Tree_diff(Tree *self, PyObject *args)
PyErr_SetObject(PyExc_TypeError, py_obj);
return NULL;
}
if (err < 0)
return Error_set(err);
@@ -352,6 +353,7 @@ Tree_diff(Tree *self, PyObject *args)
return (PyObject*)py_diff;
}
PySequenceMethods Tree_as_sequence = {
0, /* sq_length */
0, /* sq_concat */

View File

@@ -37,7 +37,7 @@
void
TreeBuilder_dealloc(TreeBuilder *self)
{
Py_XDECREF(self->repo);
Py_CLEAR(self->repo);
git_treebuilder_free(self->bld);
PyObject_Del(self);
}

View File

@@ -36,13 +36,13 @@ extern PyTypeObject ReferenceType;
* the string contained in the value argument. */
char * py_str_to_c_str(PyObject *value, const char *encoding)
{
char *c_str = NULL;
/* Case 1: byte string */
if (PyString_Check(value))
return strdup(PyString_AsString(value));
/* Case 2: text string */
if (PyUnicode_Check(value)) {
char *c_str = NULL;
if (encoding == NULL)
value = PyUnicode_AsUTF8String(value);

View File

@@ -38,8 +38,8 @@ extern PyTypeObject CommitType;
void
Walker_dealloc(Walker *self)
{
Py_CLEAR(self->repo);
git_revwalk_free(self->walk);
Py_DECREF(self->repo);
PyObject_Del(self);
}

View File

@@ -37,7 +37,7 @@ import unittest
names = ['blob', 'commit', 'config', 'diff', 'index', 'refs', 'remote',
'repository', 'revwalk', 'signature', 'status', 'tag', 'tree',
'treebuilder']
'treebuilder', 'note']
def test_suite():
# Sometimes importing pygit2 fails, we try this first to get an

View File

@@ -0,0 +1 @@
a075f5a7394b0838a9f54dfc511e1a3fbbb3b973

View File

@@ -31,7 +31,6 @@ from __future__ import absolute_import
from __future__ import unicode_literals
import unittest
import pygit2
import itertools
from pygit2 import GIT_DIFF_INCLUDE_UNMODIFIED
from . import utils
@@ -84,22 +83,27 @@ DIFF_WORKDIR_EXPECTED = [
'subdir/modified_file'
]
HUNK_EXPECTED = """@@ -1 +1 @@
a contents 2
a contents
"""
class DiffDirtyTest(utils.DirtyRepoTestCase):
def test_diff_empty_index(self):
repo = self.repo
head = repo[repo.lookup_reference('HEAD').resolve().oid]
diff = head.tree.diff(repo.index)
files = [[x[0] for x in entry.files] for entry in diff]
self.assertEqual(DIFF_INDEX_EXPECTED, list(itertools.chain(*files)))
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_INDEX_EXPECTED, files)
def test_workdir_to_tree(self):
repo = self.repo
head = repo[repo.lookup_reference('HEAD').resolve().oid]
diff = head.tree.diff()
files = [[x[0] for x in entry.files] for entry in diff]
self.assertEqual(DIFF_WORKDIR_EXPECTED, list(itertools.chain(*files)))
files = [patch.new_file_path for patch in diff]
self.assertEqual(DIFF_WORKDIR_EXPECTED, files)
class DiffTest(utils.BareRepoTestCase):
@@ -113,8 +117,8 @@ class DiffTest(utils.BareRepoTestCase):
head = repo[repo.lookup_reference('HEAD').resolve().oid]
diff = head.tree.diff(repo.index)
files = [[x[0].split('/')[0] for x in entry.files] for entry in diff]
self.assertEqual([x.name for x in head.tree], list(itertools.chain(*files)))
files = [patch.new_file_path.split('/')[0] for patch in diff]
self.assertEqual([x.name for x in head.tree], files)
def test_diff_tree(self):
commit_a = self.repo[COMMIT_SHA1_1]
@@ -125,20 +129,17 @@ class DiffTest(utils.BareRepoTestCase):
# self.assertIsNotNone is 2.7 only
self.assertTrue(diff is not None)
# self.assertIn is 2.7 only
self.assertAny(lambda x: ('a', 'a', 3, 0) in x.files, diff)
self.assertEqual(2, sum(map(lambda x: len(x.hunks), diff)))
hunk = diff[0].hunks[0]
patch = diff[0]
hunk = patch.hunks[0]
self.assertEqual(hunk.old_start, 1)
self.assertEqual(hunk.old_lines, 1)
self.assertEqual(hunk.new_start, 1)
self.assertEqual(hunk.new_lines, 1)
self.assertEqual(hunk.old_file, 'a')
self.assertEqual(hunk.new_file, 'a')
#self.assertEqual(hunk.data[0][0], b'a contents 2\n')
#self.assertEqual(hunk.data[1][0], b'a contents\n')
self.assertEqual(patch.old_file_path, 'a')
self.assertEqual(patch.new_file_path, 'a')
def test_diff_tree_opts(self):
commit_c = self.repo[COMMIT_SHA1_3]
@@ -168,25 +169,23 @@ class DiffTest(utils.BareRepoTestCase):
self.assertTrue(diff_c is not None)
# assertIn / assertNotIn are 2.7 only
self.assertAll(lambda x:('b', 'b', 3, 0) not in x.files, diff_b)
self.assertAny(lambda x:('b', 'b', 3, 0) in x.files, diff_c)
self.assertFalse('b' in [patch.new_file_path for patch in diff_b])
self.assertTrue('b' in [patch.new_file_path for patch in diff_c])
diff_b.merge(diff_c)
# assertIn is 2.7 only
self.assertAny(lambda x:('b', 'b', 3, 0) in x.files, diff_b)
self.assertTrue('b' in [patch.new_file_path for patch in diff_b])
hunk = diff_b[1].hunks[0]
patch = diff_b[0]
hunk = patch.hunks[0]
self.assertEqual(hunk.old_start, 1)
self.assertEqual(hunk.old_lines, 1)
self.assertEqual(hunk.new_start, 1)
self.assertEqual(hunk.new_lines, 1)
self.assertEqual(hunk.old_file, 'b')
self.assertEqual(hunk.new_file, 'b')
#self.assertEqual(hunk.data[0][0], b'b contents\n')
#self.assertEqual(hunk.data[1][0], b'b contents 2\n')
self.assertEqual(patch.old_file_path, 'a')
self.assertEqual(patch.new_file_path, 'a')
def test_diff_patch(self):
commit_a = self.repo[COMMIT_SHA1_1]
@@ -195,23 +194,22 @@ class DiffTest(utils.BareRepoTestCase):
diff = commit_a.tree.diff(commit_b.tree)
self.assertEqual(diff.patch, PATCH)
def test_diff_header(self):
commit_a = self.repo[COMMIT_SHA1_1]
commit_b = self.repo[COMMIT_SHA1_2]
diff = commit_a.tree.diff(commit_b.tree)
self.assertEqual(diff[0].hunks[0].header, "@@ -1 +1 @@\n")
def test_diff_oids(self):
commit_a = self.repo[COMMIT_SHA1_1]
commit_b = self.repo[COMMIT_SHA1_2]
diff = commit_a.tree.diff(commit_b.tree)
hunk = diff[0].hunks[0]
self.assertEqual(hunk.old_oid,
patch = commit_a.tree.diff(commit_b.tree)[0]
self.assertEqual(patch.old_oid,
'7f129fd57e31e935c6d60a0c794efe4e6927664b')
self.assertEqual(hunk.new_oid,
self.assertEqual(patch.new_oid,
'af431f20fc541ed6d5afede3e2dc7160f6f01f16')
def test_hunk_content(self):
commit_a = self.repo[COMMIT_SHA1_1]
commit_b = self.repo[COMMIT_SHA1_2]
patch = commit_a.tree.diff(commit_b.tree)[0]
hunk = patch.hunks[0]
self.assertEqual(HUNK_EXPECTED, ''.join(hunk.lines))
def test_find_similar(self):
commit_a = self.repo[COMMIT_SHA1_6]
commit_b = self.repo[COMMIT_SHA1_7]
@@ -219,10 +217,9 @@ class DiffTest(utils.BareRepoTestCase):
#~ Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate
#~ --find-copies-harder during rename transformion...
diff = commit_a.tree.diff(commit_b.tree, GIT_DIFF_INCLUDE_UNMODIFIED)
entry = ('lorem', 'ipsum', pygit2.GIT_DELTA_RENAMED, 100)
self.assertAll(lambda x: entry not in x.files, diff)
self.assertAll(lambda x: x.status is not pygit2.GIT_DELTA_RENAMED, diff)
diff.find_similar()
self.assertAny(lambda x: entry in x.files, diff)
self.assertAny(lambda x: x.status is pygit2.GIT_DELTA_RENAMED, diff)
if __name__ == '__main__':
unittest.main()

78
test/test_note.py Normal file
View File

@@ -0,0 +1,78 @@
# -*- coding: UTF-8 -*-
#
# Copyright 2010-2013 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file. (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Tests for note objects."""
from __future__ import absolute_import
from __future__ import unicode_literals
import unittest
from pygit2 import Signature
from . import utils
NOTE = ('6c8980ba963cad8b25a9bcaf68d4023ee57370d8', 'note message')
NOTES = [
('ab533997b80705767be3dae8cbb06a0740809f79', 'First Note - HEAD\n',
'784855caf26449a1914d2cf62d12b9374d76ae78'),
('d879714d880671ed84f8aaed8b27fca23ba01f27', 'Second Note - HEAD~1\n',
'f5e5aa4e36ab0fe62ee1ccc6eb8f79b866863b87')
]
class NotesTest(utils.BareRepoTestCase):
def test_create_note(self):
annotated_id = self.repo.revparse_single('HEAD~3').hex
author = committer = Signature('Foo bar', 'foo@bar.com', 12346, 0)
note_id = self.repo.create_note(NOTE[1], author, committer, annotated_id)
self.assertEqual(NOTE[0], utils.oid_to_hex(note_id))
# check the note blob
self.assertEqual(NOTE[1].encode(), self.repo[note_id].data)
def test_lookup_note(self):
annotated_id = self.repo.head.hex
note = self.repo.lookup_note(annotated_id)
self.assertEqual(NOTES[0][0], note.oid)
self.assertEqual(NOTES[0][1], note.message)
def test_remove_note(self):
note = self.repo.lookup_note(self.repo.head.hex)
author = committer = Signature('Foo bar', 'foo@bar.com', 12346, 0)
note.remove(author, committer)
self.assertRaises(KeyError, lambda: self.repo.lookup_note(self.repo.head.hex))
def test_iterate_notes(self):
for i, note in enumerate(self.repo.notes()):
entry = (note.oid, note.message, note.annotated_id)
self.assertEqual(NOTES[i],entry)
def test_iterate_non_existing_ref(self):
self.assertRaises(KeyError, lambda: self.repo.notes("refs/notes/bad_ref"))
if __name__ == '__main__':
unittest.main()

View File

@@ -35,8 +35,8 @@ REMOTE_NAME = 'origin'
REMOTE_URL = 'git://github.com/libgit2/pygit2.git'
REMOTE_FETCHSPEC_SRC = 'refs/heads/*'
REMOTE_FETCHSPEC_DST = 'refs/remotes/origin/*'
REMOTE_REPO_OBJECTS = 24
REMOTE_REPO_BYTES = 2253
REMOTE_REPO_OBJECTS = 30
REMOTE_REPO_BYTES = 2758
class RepositoryTest(utils.RepoTestCase):
def test_remote_create(self):