Add instructions for installing hct.py in the python module directory.
[hgct.git] / hg.py
blobce8f85e4d4bcb8c2cef22b4f8a3909fa0a964e17
1 # Copyright (c) 2005 Mark Williamson <mark.williamson@cl.cam.ac.uk>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 import re
18 from ctcore import *
20 def doCommit(keepFiles, filesToCommit, msg):
21 commitFileNames = []
22 for f in filesToCommit:
23 commitFileNames.append(f.dstName)
24 runProgram(['hg', 'commit', '-A', '-l', '-'] + commitFileNames, msg)
27 parseDiffRE = re.compile('([AMR?]) (.*)')
29 def __getPatch(file, otherFile = None):
30 if file.change == 'M' or \
31 file.change == 'R' or \
32 file.change == 'A':
33 return runProgram(['hg', 'diff', file.dstName])
34 elif file.change == '?':
35 return runProgram(['diff', '-u', '/dev/null', file.dstName],
36 expectedexits=[0,1,2])
37 else:
38 assert(False)
40 def __parseStatus():
41 inp = runProgram(['hg', 'status'])
42 ret = []
43 try:
44 recs = inp.split("\n")
45 recs.pop() # remove last entry (which is '')
46 it = recs.__iter__()
47 while True:
48 rec = it.next()
49 m = parseDiffRE.match(rec)
51 if not m:
52 print "Unknown output from hg status!: " + rec + "\n"
53 continue
55 f = File()
56 f.change = m.group(1)
58 if f.change == '?' and not settings().showUnknown:
59 continue
61 f.srcName = f.dstName = m.group(2)
63 f.patch = __getPatch(f)
65 ret.append(f)
66 except StopIteration:
67 pass
68 return ret
71 # HEAD is src in the returned File objects. That is, srcName is the
72 # name in HEAD and dstName is the name in the cache.
73 def getFiles():
74 files = __parseStatus()
75 for f in files:
76 c = f.change
77 if c == 'A':
78 f.text = 'Added file: ' + f.srcName
79 elif c == '?':
80 f.text = 'New file: ' + f.srcName
81 elif c == 'R':
82 f.text = 'Removed file: ' + f.srcName
83 else:
84 f.text = f.srcName
86 return files
88 def initialize():
89 def basicsFailed(msg):
90 print "hg status: " + msg
91 print "Make sure that the current working directory contains a '.hg' directory."
92 sys.exit(1)
94 try:
95 runProgram(['hg', 'status'])
96 except OSError, e:
97 basicsFailed(e.strerror)
98 except ProgramError, e:
99 basicsFailed(e.error)
101 def doUpdateCache(file):
102 return
104 def discardFile(file):
105 if file.change == "M":
106 runProgram(['hg', 'revert', file.dstName])
107 elif file.change == "?":
108 runProgram(['rm', '-f', file.dstName])
109 return
111 def ignoreFile(file):
112 hgignore = open('.hgignore', 'a')
113 print >> hgignore, "^" + re.escape(file.dstName) + "$"
114 hgignore.close()
117 # Not yet implemented
118 def commitIsMerge():
119 return False
121 def mergeMessage():
122 return ''