diff --git a/pygit2.c b/pygit2.c index 614dc95..37a3c24 100644 --- a/pygit2.c +++ b/pygit2.c @@ -761,6 +761,30 @@ Repository_packall_references(Repository *self, PyObject *args) Py_RETURN_NONE; } +static int read_status_cb(const char *path, unsigned int status_flags, + void *payload_dict) +{ + /* This is the callback that will be called in git_status_foreach. It + * will be called for every path.*/ + PyObject *flags; + + flags = PyInt_FromLong((long) status_flags); + PyDict_SetItemString(payload_dict, path, flags); + + return GIT_SUCCESS; +} + +static PyObject * +Repository_status(Repository *self, PyObject *args) +{ + PyObject *payload_dict; + + payload_dict = PyDict_New(); + git_status_foreach(self->repo, read_status_cb, payload_dict); + + return payload_dict; +} + static PyMethodDef Repository_methods[] = { {"create_commit", (PyCFunction)Repository_create_commit, METH_VARARGS, "Create a new commit object, return its SHA."}, @@ -789,6 +813,9 @@ static PyMethodDef Repository_methods[] = { "\"target\"."}, {"packall_references", (PyCFunction)Repository_packall_references, METH_NOARGS, "Pack all the loose references in the repository."}, + {"status", (PyCFunction)Repository_status, METH_NOARGS, "Reads the " + "status of the repository and returns a dictionnary with file paths " + "as keys and status flags as values.\nSee pygit2.GIT_STATUS_*."}, {NULL} }; @@ -2534,4 +2561,23 @@ initpygit2(void) PyModule_AddIntConstant(m,"GIT_REF_OID", GIT_REF_OID); PyModule_AddIntConstant(m,"GIT_REF_SYMBOLIC", GIT_REF_SYMBOLIC); PyModule_AddIntConstant(m,"GIT_REF_PACKED", GIT_REF_PACKED); + + /** Git status flags **/ + PyModule_AddIntConstant(m,"GIT_STATUS_CURRENT", GIT_STATUS_CURRENT); + + /* Flags for index status */ + PyModule_AddIntConstant(m,"GIT_STATUS_INDEX_NEW", GIT_STATUS_INDEX_NEW); + PyModule_AddIntConstant(m,"GIT_STATUS_INDEX_MODIFIED", + GIT_STATUS_INDEX_MODIFIED); + PyModule_AddIntConstant(m,"GIT_STATUS_INDEX_DELETED" , + GIT_STATUS_INDEX_DELETED); + + /* Flags for worktree status */ + PyModule_AddIntConstant(m,"GIT_STATUS_WT_NEW", GIT_STATUS_WT_NEW); + PyModule_AddIntConstant(m,"GIT_STATUS_WT_MODIFIED" , + GIT_STATUS_WT_MODIFIED); + PyModule_AddIntConstant(m,"GIT_STATUS_WT_DELETED", GIT_STATUS_WT_DELETED); + + /* Flags for ignored files */ + PyModule_AddIntConstant(m,"GIT_STATUS_IGNORED", GIT_STATUS_IGNORED); } diff --git a/test/__init__.py b/test/__init__.py index cebf864..ac53525 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -36,7 +36,7 @@ import unittest names = ['blob', 'commit', 'index', 'refs', 'repository', 'revwalk', 'tag', - 'tree'] + 'tree', 'status'] def test_suite(): modules = ['test.test_%s' % n for n in names] return unittest.defaultTestLoader.loadTestsFromNames(modules) diff --git a/test/data/dirtyrepo.tar b/test/data/dirtyrepo.tar new file mode 100644 index 0000000..d9ac442 Binary files /dev/null and b/test/data/dirtyrepo.tar differ diff --git a/test/test_status.py b/test/test_status.py new file mode 100644 index 0000000..41dc4db --- /dev/null +++ b/test/test_status.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +# +# Copyright 2011 Julien Miotte +# +# 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 revision walk.""" + +__author__ = 'mike.perdide@gmail.com (Julien Miotte)' + +import unittest + +import pygit2 +import utils + +EXPECTED = { + "current_file": pygit2.GIT_STATUS_CURRENT, + "file_deleted": pygit2.GIT_STATUS_WT_DELETED, + "modified_file": pygit2.GIT_STATUS_WT_MODIFIED, + "new_file": pygit2.GIT_STATUS_WT_NEW, + + "staged_changes": pygit2.GIT_STATUS_INDEX_MODIFIED, + "staged_changes_file_deleted": pygit2.GIT_STATUS_INDEX_MODIFIED | + pygit2.GIT_STATUS_WT_DELETED, + "staged_changes_file_modified": pygit2.GIT_STATUS_INDEX_MODIFIED | + pygit2.GIT_STATUS_WT_MODIFIED, + + "staged_delete": pygit2.GIT_STATUS_INDEX_DELETED, + "staged_delete_file_modified": pygit2.GIT_STATUS_INDEX_DELETED | + pygit2.GIT_STATUS_WT_NEW, + "staged_new": pygit2.GIT_STATUS_INDEX_NEW, + + "staged_new_file_deleted": pygit2.GIT_STATUS_INDEX_NEW | + pygit2.GIT_STATUS_WT_DELETED, + "staged_new_file_modified": pygit2.GIT_STATUS_INDEX_NEW | + pygit2.GIT_STATUS_WT_MODIFIED, + + "subdir/current_file": pygit2.GIT_STATUS_CURRENT, + "subdir/deleted_file": pygit2.GIT_STATUS_WT_DELETED, + "subdir/modified_file": pygit2.GIT_STATUS_WT_MODIFIED, + "subdir/new_file": pygit2.GIT_STATUS_WT_NEW, +} + +class StatusTest(utils.DirtyRepoTestCase): + + def test_status(self): + """ + For every file in the status, check that the flags are correct. + """ + git_status = self.repo.status() + for filepath, status in git_status.items(): + self.assertTrue(filepath in git_status) + self.assertEqual(status, git_status[filepath]) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/utils.py b/test/utils.py index 3245694..0fcfc1c 100644 --- a/test/utils.py +++ b/test/utils.py @@ -66,8 +66,10 @@ class BareRepoTestCase(BaseTestCase): class RepoTestCase(BaseTestCase): + repo_dir = 'testrepo' + def setUp(self): - repo_dir = 'testrepo' + repo_dir = self.repo_dir repo_path = os.path.join(os.path.dirname(__file__), 'data', repo_dir) temp_dir = tempfile.mkdtemp() tar = tarfile.open(repo_path + '.tar') @@ -76,3 +78,7 @@ class RepoTestCase(BaseTestCase): self._temp_dir = temp_dir temp_repo_path = os.path.join(temp_dir, repo_dir, '.git') self.repo = pygit2.Repository(temp_repo_path) + +class DirtyRepoTestCase(RepoTestCase): + + repo_dir = 'dirtyrepo'