Merge http://www.cl.cam.ac.uk/~maw48/gct.git
[hgct.git] / hg.py
blob84dad8892492e0225c95747ee69014721c0a7b22
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, selFileNames, msg):
21 print "Commit message:" + msg
22 runProgram(['hg', 'commit', '-A', '-l', '-'] + selFileNames, msg)
25 parseDiffRE = re.compile('([AMR?]) (.*)')
27 def __getPatch(file, otherFile = None):
28 if otherFile:
29 f = [file, otherFile]
30 else:
31 f = [file]
33 return runProgram(['hg', 'diff'] + f)
35 def __parseStatus():
36 inp = runProgram(['hg', 'status'])
37 print inp
38 ret = []
39 try:
40 recs = inp.split("\n")
41 recs.pop() # remove last entry (which is '')
42 it = recs.__iter__()
43 while True:
44 rec = it.next()
45 m = parseDiffRE.match(rec)
47 if not m:
48 print "Unknown output from hg status!: " + rec + "\n"
49 continue
51 f = File()
52 f.change = m.group(1)
53 f.srcName = f.dstName = m.group(2)
54 print "Getting patch for file " + f.srcName
56 f.patch = __getPatch(f.srcName)
58 ret.append(f)
59 except StopIteration:
60 pass
61 return ret
64 # HEAD is src in the returned File objects. That is, srcName is the
65 # name in HEAD and dstName is the name in the cache.
66 def getFiles():
67 files = __parseStatus()
68 for f in files:
69 c = f.change
70 if c == 'A':
71 f.text = 'Added file: ' + f.srcName
72 elif c == '?':
73 f.text = 'New file: ' + f.srcName
74 elif c == 'R':
75 f.text = 'Removed file: ' + f.srcName
76 else:
77 f.text = f.srcName
79 f.updated = True
80 return files
82 def repoValid():
83 def basicsFailed(msg):
84 print "hg status: " + msg
85 print "Make sure that the current working directory contains a '.hg' directory."
86 sys.exit(1)
88 try:
89 runProgram(['hg', 'status'])
90 except OSError, e:
91 basicsFailed(e.strerror)
92 except ProgramError, e:
93 basicsFailed(e.error)
95 def doUpdateCache(file):
96 return
98 def discardFile(file):
99 runProgram(['hg', 'revert', file.dstName])
100 return
102 def ignoreFile(file):
103 hgignore = open('.hgignore', 'a')
104 print >> hgignore, "^" + re.escape(file.dstName) + "$"
105 hgignore.close()