From f9133376f2262f9f4067e41d9ed6da420f83437b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B4she=20van=20der=20Sterre?= Date: Tue, 31 Dec 2013 01:44:27 +0100 Subject: Test if it is possible to add files to the index of a bare repo. Adding using a path should still require a non-bare repository. --- git/test/test_index.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'git') diff --git a/git/test/test_index.py b/git/test/test_index.py index 7d65cb9b..c512468b 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -20,6 +20,12 @@ import shutil import time from stat import * +from StringIO import StringIO +from gitdb.base import IStream +from git.objects import Blob +from git.index.typ import BaseIndexEntry + + class TestIndex(TestBase): def __init__(self, *args): @@ -646,6 +652,34 @@ class TestIndex(TestBase): for absfile in absfiles: assert os.path.isfile(absfile) + @with_rw_repo('HEAD', bare=True) + def test_index_bare_add(self, rw_bare_repo): + # Something is wrong after cloning to a bare repo, reading the + # property rw_bare_repo.working_tree_dir will return '/tmp' + # instead of throwing the Exception we are expecting. This is + # a quick hack to make this test fail when expected. + rw_bare_repo._working_tree_dir = None + contents = 'This is a StringIO file' + filesize = len(contents) + fileobj = StringIO(contents) + filename = 'my-imaginary-file' + istream = rw_bare_repo.odb.store( + IStream(Blob.type, filesize, fileobj)) + entry = BaseIndexEntry((100644, istream.binsha, 0, filename)) + try: + rw_bare_repo.index.add([entry]) + except AssertionError, e: + self.fail("Adding to the index of a bare repo is not allowed.") + + # Adding using a path should still require a non-bare repository. + asserted = False + path = os.path.join('git', 'test', 'test_index.py') + try: + rw_bare_repo.index.add([path]) + except Exception, e: + asserted = "does not have a working tree" in e.message + assert asserted, "Adding using a filename is not correctly asserted." + @with_rw_repo('HEAD') def test_compare_write_tree(self, rw_repo): -- cgit v1.2.3 From 4d4138c16299ce51541cb752672da1ae467cc5ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B4she=20van=20der=20Sterre?= Date: Tue, 31 Dec 2013 02:38:37 +0100 Subject: Refactored the working tree dependent code for add. Adding to a bare repository is now possible because @git_working_dir is only used on the relevant methods. --- git/index/base.py | 84 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 40 deletions(-) (limited to 'git') diff --git a/git/index/base.py b/git/index/base.py index 12097922..c834971a 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -562,7 +562,48 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END for each item return (paths, entries) + + @git_working_dir + def _store_path(self, filepath, fprogress): + """Store file at filepath in the database and return the base index entry""" + st = os.lstat(filepath) # handles non-symlinks as well + stream = None + if S_ISLNK(st.st_mode): + stream = StringIO(os.readlink(filepath)) + else: + stream = open(filepath, 'rb') + # END handle stream + fprogress(filepath, False, filepath) + istream = self.repo.odb.store(IStream(Blob.type, st.st_size, stream)) + fprogress(filepath, True, filepath) + return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode), + istream.binsha, 0, to_native_path_linux(filepath))) + @git_working_dir + def _entries_for_paths(self, paths, path_rewriter, fprogress): + entries_added = list() + if path_rewriter: + for path in paths: + abspath = os.path.abspath(path) + gitrelative_path = abspath[len(self.repo.working_tree_dir)+1:] + blob = Blob(self.repo, Blob.NULL_BIN_SHA, + stat_mode_to_index_mode(os.stat(abspath).st_mode), + to_native_path_linux(gitrelative_path)) + entries.append(BaseIndexEntry.from_blob(blob)) + # END for each path + del(paths[:]) + # END rewrite paths + + # HANDLE PATHS + assert len(entries_added) == 0 + added_files = list() + for filepath in self._iter_expand_paths(paths): + entries_added.append(self._store_path(filepath, fprogress)) + # END for each filepath + # END path handling + return entries_added + + def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None, write=True): """Add files from the working tree, specific blobs or BaseIndexEntries @@ -651,47 +692,10 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # sort the entries into strings and Entries, Blobs are converted to entries # automatically # paths can be git-added, for everything else we use git-update-index - entries_added = list() paths, entries = self._preprocess_add_items(items) - if paths and path_rewriter: - for path in paths: - abspath = os.path.abspath(path) - gitrelative_path = abspath[len(self.repo.working_tree_dir)+1:] - blob = Blob(self.repo, Blob.NULL_BIN_SHA, - stat_mode_to_index_mode(os.stat(abspath).st_mode), - to_native_path_linux(gitrelative_path)) - entries.append(BaseIndexEntry.from_blob(blob)) - # END for each path - del(paths[:]) - # END rewrite paths - - - def store_path(filepath): - """Store file at filepath in the database and return the base index entry""" - st = os.lstat(filepath) # handles non-symlinks as well - stream = None - if S_ISLNK(st.st_mode): - stream = StringIO(os.readlink(filepath)) - else: - stream = open(filepath, 'rb') - # END handle stream - fprogress(filepath, False, filepath) - istream = self.repo.odb.store(IStream(Blob.type, st.st_size, stream)) - fprogress(filepath, True, filepath) - return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode), - istream.binsha, 0, to_native_path_linux(filepath))) - # END utility method - - - # HANDLE PATHS + entries_added = list() if paths: - assert len(entries_added) == 0 - added_files = list() - for filepath in self._iter_expand_paths(paths): - entries_added.append(store_path(filepath)) - # END for each filepath - # END path handling - + entries_added.extend(self._entries_for_paths(paths, path_rewriter, fprogress)) # HANDLE ENTRIES if entries: @@ -706,7 +710,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): if null_entries_indices: for ei in null_entries_indices: null_entry = entries[ei] - new_entry = store_path(null_entry.path) + new_entry = self._store_path(null_entry.path, fprogress) # update null entry entries[ei] = BaseIndexEntry((null_entry.mode, new_entry.binsha, null_entry.stage, null_entry.path)) -- cgit v1.2.3 From 93ae6ecfffda7923b9f7f82d122d39c362be9c44 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 17 Nov 2014 11:39:05 +0100 Subject: Fixed regression that would possibly have caused an abundance of chdir calls. --- git/index/base.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'git') diff --git a/git/index/base.py b/git/index/base.py index 1dada729..43f0be84 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -559,9 +559,9 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END for each item return (paths, entries) - @git_working_dir def _store_path(self, filepath, fprogress): - """Store file at filepath in the database and return the base index entry""" + """Store file at filepath in the database and return the base index entry + Needs the git_working_dir decorator active ! This must be assured in the calling code""" st = os.lstat(filepath) # handles non-symlinks as well stream = None if S_ISLNK(st.st_mode): @@ -707,13 +707,17 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # create objects if required, otherwise go with the existing shas null_entries_indices = [ i for i,e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA ] if null_entries_indices: - for ei in null_entries_indices: - null_entry = entries[ei] - new_entry = self._store_path(null_entry.path, fprogress) - - # update null entry - entries[ei] = BaseIndexEntry((null_entry.mode, new_entry.binsha, null_entry.stage, null_entry.path)) - # END for each entry index + @git_working_dir + def handle_null_entries(self): + for ei in null_entries_indices: + null_entry = entries[ei] + new_entry = self._store_path(null_entry.path, fprogress) + + # update null entry + entries[ei] = BaseIndexEntry((null_entry.mode, new_entry.binsha, null_entry.stage, null_entry.path)) + # END for each entry index + # end closure + handle_null_entries(self) # END null_entry handling # REWRITE PATHS -- cgit v1.2.3 From d148a0de467d20bf18560374632b1f069659f8f3 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 17 Nov 2014 11:49:09 +0100 Subject: Fixed indentation error --- git/test/test_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git') diff --git a/git/test/test_index.py b/git/test/test_index.py index a4e26977..3ce1bfaf 100644 --- a/git/test/test_index.py +++ b/git/test/test_index.py @@ -692,7 +692,7 @@ class TestIndex(TestBase): IStream(Blob.type, filesize, fileobj)) entry = BaseIndexEntry((100644, istream.binsha, 0, filename)) try: - rw_bare_repo.index.add([entry]) + rw_bare_repo.index.add([entry]) except AssertionError, e: self.fail("Adding to the index of a bare repo is not allowed.") -- cgit v1.2.3