1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from translate.storage.versioncontrol import GenericRevisionControlSystem
24 from translate.storage.versioncontrol import run_command
25
26
28 """check if hg is installed"""
29 exitcode, output, error = run_command(["hg", "--version"])
30 return exitcode == 0
31
32
34 """return a tuple of (major, minor) for the installed bazaar client"""
35 import re
36 command = ["hg", "--version"]
37 exitcode, output, error = run_command(command)
38 if exitcode == 0:
39 version_line = output.splitlines()[0]
40 version_match = re.search(r"\d+\.\d+", version_line)
41 if version_match:
42 major, minor = version_match.group().split(".")
43 if (major.isdigit() and minor.isdigit()):
44 return (int(major), int(minor))
45
46 return (0, 0)
47
48
49 -class hg(GenericRevisionControlSystem):
50 """Class to manage items under revision control of mercurial."""
51
52 RCS_METADIR = ".hg"
53 SCAN_PARENTS = True
54
55 - def update(self, revision=None):
56 """Does a clean update of the given path
57
58 @param revision: ignored for hg
59 """
60
61 command = ["hg", "-R", self.root_dir, "revert",
62 "--all", self.location_abs]
63 exitcode, output_revert, error = run_command(command)
64 if exitcode != 0:
65 raise IOError("[Mercurial] error running '%s': %s" % (command, error))
66
67 command = ["hg", "-R", self.root_dir, "pull"]
68 exitcode, output_pull, error = run_command(command)
69 if exitcode != 0:
70 raise IOError("[Mercurial] error running '%s': %s" % (command, error))
71
72 command = ["hg", "-R", self.root_dir, "update"]
73 exitcode, output_update, error = run_command(command)
74 if exitcode != 0:
75 raise IOError("[Mercurial] error running '%s': %s" % (command, error))
76 return output_revert + output_pull + output_update
77
78 - def commit(self, message=None, author=None):
79 """Commits the file and supplies the given commit message if present"""
80 if message is None:
81 message = ""
82
83 command = ["hg", "-R", self.root_dir, "commit", "-m", message]
84
85 if author and (get_version() >= (1, 0)):
86 command.extend(["--user", author])
87
88 command.append(self.location_abs)
89 exitcode, output_commit, error = run_command(command)
90 if exitcode != 0:
91 raise IOError("[Mercurial] Error running '%s': %s" \
92 % (command, error))
93
94 command = ["hg", "-R", self.root_dir, "push"]
95 exitcode, output_push, error = run_command(command)
96 if exitcode != 0:
97 raise IOError("[Mercurial] Error running '%s': %s" \
98 % (command, error))
99 return output_commit + output_push
100
102 """Get a clean version of a file from the hg repository"""
103
104 command = ["hg", "-R", self.root_dir, "cat",
105 self.location_abs]
106 exitcode, output, error = run_command(command)
107 if exitcode != 0:
108 raise IOError("[Mercurial] Error running '%s': %s" \
109 % (command, error))
110 return output
111