From e0afe1101012e0e2feb5ea8b1a6c14bbc4573e91 Mon Sep 17 00:00:00 2001 From: sumpfralle Date: Fri, 4 Jul 2008 03:37:38 +0000 Subject: [PATCH] for git v1.5.2 (and below): chdir to the directory of the target file before executing git git-svn-id: http://translate.svn.sourceforge.net/svnroot/translate/src/trunk/translate@7717 54714841-351b-0410-a198-e36a94b762f5 --- storage/versioncontrol/git.py | 2 +- storage/versioncontrol/git_old.py | 92 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/storage/versioncontrol/git.py b/storage/versioncontrol/git.py index 442f4ac..f57dae3 100644 --- a/storage/versioncontrol/git.py +++ b/storage/versioncontrol/git.py @@ -62,7 +62,7 @@ class git(GenericRevisionControlSystem): return os.path.join(self.root_dir, self.RCS_METADIR) def _get_git_command(self, args): - """prepends generic git arguments to conrete ones + """prepends generic git arguments to default ones """ command = ["git", "--git-dir", self._get_git_dir(), "--work-tree", self.root_dir] command.extend(args) diff --git a/storage/versioncontrol/git_old.py b/storage/versioncontrol/git_old.py index e04804a..283e122 100644 --- a/storage/versioncontrol/git_old.py +++ b/storage/versioncontrol/git_old.py @@ -31,6 +31,7 @@ from translate.storage.versioncontrol import run_command from translate.storage.versioncontrol import GenericRevisionControlSystem import re +import os def is_available(): @@ -61,36 +62,85 @@ class git_old(GenericRevisionControlSystem): def _get_git_dir(self): """git requires the git metadata directory for every operation """ - import os return os.path.join(self.root_dir, self.RCS_METADIR) def _get_git_command(self, args): - """prepends generic git arguments to conrete ones + """prepends generic git arguments to default ones + + This function was only useful for "git.py", but we keep it here for + simplicity. """ - command = ["git", "--git-dir", self._get_git_dir(), "--work-tree", self.root_dir] + command = ["git"] command.extend(args) return command def update(self, revision=None): """Does a clean update of the given path""" + working_dir = os.path.dirname(self.location_abs) + original_dir = os.getcwd() + if working_dir: + try: + # first: check if we are allowed to _change_ to the current dir + # (of course, we are already here, but that does not mean so much) + os.chdir(original_dir) + except OSError, error: + raise IOError("[GIT] could not change to directory (%s): %s" \ + % (original_dir, error)) + try: + # change to the parent directory of the CVS managed file + os.chdir(working_dir) + except OSError, error: + raise IOError("[GIT] could not change to directory (%s): %s" \ + % (working_dir, error)) # git checkout command = self._get_git_command(["checkout", self.location_rel]) exitcode, output_checkout, error = run_command(command) if exitcode != 0: + # something went wrong - go back to the original directory + try: + os.chdir(original_dir) + except OSError: + pass raise IOError("[GIT] checkout failed (%s): %s" % (command, error)) # pull changes command = self._get_git_command(["pull"]) exitcode, output_pull, error = run_command(command) + # always go back to the original directory + try: + os.chdir(original_dir) + except OSError: + pass if exitcode != 0: raise IOError("[GIT] pull failed (%s): %s" % (command, error)) return output_checkout + output_pull def commit(self, message=None): """Commits the file and supplies the given commit message if present""" + working_dir = os.path.dirname(self.location_abs) + original_dir = os.getcwd() + if working_dir: + try: + # first: check if we are allowed to _change_ to the current dir + # (of course, we are already here, but that does not mean so much) + os.chdir(original_dir) + except OSError, error: + raise IOError("[GIT] could not change to directory (%s): %s" \ + % (original_dir, error)) + try: + # change to the parent directory of the CVS managed file + os.chdir(working_dir) + except OSError, error: + raise IOError("[GIT] could not change to directory (%s): %s" \ + % (working_dir, error)) # add the file command = self._get_git_command(["add", self.location_rel]) exitcode, output_add, error = run_command(command) if exitcode != 0: + # something went wrong - go back to the original directory + try: + os.chdir(original_dir) + except OSError: + pass raise IOError("[GIT] add of ('%s', '%s') failed: %s" \ % (self.root_dir, self.location_rel, error)) # commit file @@ -99,6 +149,11 @@ class git_old(GenericRevisionControlSystem): command.extend(["-m", message]) exitcode, output_commit, error = run_command(command) if exitcode != 0: + # something went wrong - go back to the original directory + try: + os.chdir(original_dir) + except OSError: + pass if len(error): msg = error else: @@ -108,6 +163,11 @@ class git_old(GenericRevisionControlSystem): # push changes command = self._get_git_command(["push"]) exitcode, output_push, error = run_command(command) + # always go back to the original directory + try: + os.chdir(original_dir) + except OSError: + pass if exitcode != 0: raise IOError("[GIT] push of ('%s', '%s') failed: %s" \ % (self.root_dir, self.location_rel, error)) @@ -115,11 +175,33 @@ class git_old(GenericRevisionControlSystem): def getcleanfile(self, revision=None): """Get a clean version of a file from the git repository""" + working_dir = os.path.dirname(self.location_abs) + original_dir = os.getcwd() + if working_dir: + try: + # first: check if we are allowed to _change_ to the current dir + # (of course, we are already here, but that does not mean so much) + os.chdir(original_dir) + except OSError, error: + raise IOError("[GIT] could not change to directory (%s): %s" \ + % (original_dir, error)) + try: + # change to the parent directory of the CVS managed file + os.chdir(working_dir) + except OSError, error: + raise IOError("[GIT] could not change to directory (%s): %s" \ + % (working_dir, error)) # run git-show - command = self._get_git_command(["show", "HEAD:%s" % self.location_rel]) + command = self._get_git_command(["cat-file", "blob", + "HEAD:%s" % self.location_rel]) exitcode, output, error = run_command(command) + # always go back to the original directory + try: + os.chdir(original_dir) + except OSError: + pass if exitcode != 0: - raise IOError("[GIT] 'show' failed for ('%s', %s): %s" \ + raise IOError("[GIT] 'cat-file' failed for ('%s', %s): %s" \ % (self.root_dir, self.location_rel, error)) return output -- 2.11.4.GIT