diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-12-03 22:11:34 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-12-03 22:11:34 +0100 |
commit | 12276fedec49c855401262f0929f088ebf33d2cf (patch) | |
tree | fd1adacb07ff59d279936dffc9c879bdecb82360 /lib/git | |
parent | 7d00fa4f6cad398250ce868cef34357b45abaad3 (diff) | |
download | gitpython-12276fedec49c855401262f0929f088ebf33d2cf.tar.gz |
refs: Implemented low-level (Symbolic)Reference renaming as some references cannot be reamed using the git-branch command if they are not in refs/heads, that is in a non-standard refs folder
Diffstat (limited to 'lib/git')
-rw-r--r-- | lib/git/refs.py | 47 | ||||
-rw-r--r-- | lib/git/utils.py | 14 |
2 files changed, 46 insertions, 15 deletions
diff --git a/lib/git/refs.py b/lib/git/refs.py index f826691d..0b8b2a98 100644 --- a/lib/git/refs.py +++ b/lib/git/refs.py @@ -309,6 +309,47 @@ class SymbolicReference(object): This does not alter the current HEAD, index or Working Tree """ return cls._create(repo, path, False, reference, force) + + def rename(self, new_path, force=False): + """ + Rename self to a new path + + ``new_path`` + Either a simple name or a full path, i.e. new_name or features/new_name. + The prefix refs/ is implied for references and will be set as needed. + In case this is a symbolic ref, there is no implied prefix + + ``force`` + If True, the rename will succeed even if a head with the target name + already exists. It will be overwritten in that case + + Returns + self + + Raises OSError: + In case a file at path with that name already exists + """ + new_path = self._to_full_path(self.repo, new_path) + if self.path == new_path: + return self + + new_abs_path = os.path.join(self.repo.git_dir, new_path) + if os.path.isfile(new_abs_path): + if not force: + raise OSError("File at path %r already exists" % new_abs_path) + os.remove(new_abs_path) + # END handle existing target file + + dirname = os.path.dirname(new_abs_path) + if not os.path.isdir(dirname): + os.makedirs(dirname) + # END create directory + + cur_abs_path = os.path.join(self.repo.git_dir, self.path) + os.rename(cur_abs_path, new_abs_path) + self.path = new_path + + return self class Reference(SymbolicReference, LazyMixin, Iterable): @@ -330,7 +371,7 @@ class Reference(SymbolicReference, LazyMixin, Iterable): refs/heads/master """ - if not path.startswith(self._common_path_default): + if not path.startswith(self._common_path_default+'/'): raise ValueError("Cannot instantiate %s from path %s" % ( self.__class__.__name__, path )) super(Reference, self).__init__(repo, path) @@ -472,6 +513,7 @@ class Reference(SymbolicReference, LazyMixin, Iterable): """ return cls._create(repo, path, True, commit, force) + class HEAD(SymbolicReference): """ @@ -623,6 +665,9 @@ class Head(Reference): Returns self + + Note + respects the ref log as git commands are used """ flag = "-m" if force: diff --git a/lib/git/utils.py b/lib/git/utils.py index 433f96d5..27ce0804 100644 --- a/lib/git/utils.py +++ b/lib/git/utils.py @@ -362,18 +362,4 @@ class Iterable(object): """ raise NotImplementedError("To be implemented by Subclass") -def needs_working_tree(func): - """ - Decorator assuring the wrapped method may only run if the repository has a - working tree, hence it is not bare. - """ - def check_default_index(self, *args, **kwargs): - if self.repo.working_tree_dir is None: - raise AssertionError( "Cannot call %r bare git repositories" % func.__name__ ) - return func(self, *args, **kwargs) - # END wrpaper method - - check_default_index.__name__ = func.__name__ - return check_default_index - |