for git v1.5.2 (and below): chdir to the directory of the target file before executin...
[translate_toolkit.git] / storage / versioncontrol / hg.py
blob0fb265e339879a9b0157e84c298f1c1ccbf23bc5
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2004-2008 Zuza Software Foundation
5 #
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 from translate.storage.versioncontrol import run_command
24 from translate.storage.versioncontrol import GenericRevisionControlSystem
27 def is_available():
28 """check if hg is installed"""
29 exitcode, output, error = run_command(["hg", "--version"])
30 return exitcode == 0
33 class hg(GenericRevisionControlSystem):
34 """Class to manage items under revision control of mercurial."""
36 RCS_METADIR = ".hg"
37 SCAN_PARENTS = True
39 def update(self, revision=None):
40 """Does a clean update of the given path
42 @param revision: ignored for hg
43 """
44 # revert local changes (avoids conflicts)
45 command = ["hg", "-R", self.root_dir, "revert",
46 "--all", self.location_abs]
47 exitcode, output_revert, error = run_command(command)
48 if exitcode != 0:
49 raise IOError("[Mercurial] error running '%s': %s" % (command, error))
50 # pull new patches
51 command = ["hg", "-R", self.root_dir, "pull"]
52 exitcode, output_pull, error = run_command(command)
53 if exitcode != 0:
54 raise IOError("[Mercurial] error running '%s': %s" % (command, error))
55 # update working directory
56 command = ["hg", "-R", self.root_dir, "update"]
57 exitcode, output_update, error = run_command(command)
58 if exitcode != 0:
59 raise IOError("[Mercurial] error running '%s': %s" % (command, error))
60 return output_revert + output_pull + output_update
62 def commit(self, message=None):
63 """Commits the file and supplies the given commit message if present"""
64 if message is None:
65 message = ""
66 # commit changes
67 command = ["hg", "-R", self.root_dir, "commit", "-m", message,
68 self.location_abs]
69 exitcode, output_commit, error = run_command(command)
70 if exitcode != 0:
71 raise IOError("[Mercurial] Error running '%s': %s" \
72 % (command, error))
73 # push changes
74 command = ["hg", "-R", self.root_dir, "push"]
75 exitcode, output_push, error = run_command(command)
76 if exitcode != 0:
77 raise IOError("[Mercurial] Error running '%s': %s" \
78 % (command, error))
79 return output_commit + output_push
81 def getcleanfile(self, revision=None):
82 """Get a clean version of a file from the hg repository"""
83 # run hg cat
84 command = ["hg", "-R", self.root_dir, "cat",
85 self.location_abs]
86 exitcode, output, error = run_command(command)
87 if exitcode != 0:
88 raise IOError("[Mercurial] Error running '%s': %s" \
89 % (command, error))
90 return output