Add an option to tune the auto adding file behaviour.
[hgct.git] / hg.py
blobf0e90b3fbdc5e3d68757ff5a05a7d74602d8ee34
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 file.change == 'R':
31 return runProgram(['hg', 'diff', file.dstName])
32 elif file.change == '?':
33 return runProgram(['diff', '-u', '/dev/null', file.dstName],
34 expectedexits=[0,1])
36 def __parseStatus():
37 inp = runProgram(['hg', 'status'])
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)
55 f.patch = __getPatch(f)
57 ret.append(f)
58 except StopIteration:
59 pass
60 return ret
63 # HEAD is src in the returned File objects. That is, srcName is the
64 # name in HEAD and dstName is the name in the cache.
65 def getFiles():
66 files = __parseStatus()
67 for f in files:
68 c = f.change
69 if c == 'A':
70 f.text = 'Added file: ' + f.srcName
71 elif c == '?':
72 f.text = 'New file: ' + f.srcName
73 elif c == 'R':
74 f.text = 'Removed file: ' + f.srcName
75 else:
76 f.text = f.srcName
78 return files
80 def repoValid():
81 def basicsFailed(msg):
82 print "hg status: " + msg
83 print "Make sure that the current working directory contains a '.hg' directory."
84 sys.exit(1)
86 try:
87 runProgram(['hg', 'status'])
88 except OSError, e:
89 basicsFailed(e.strerror)
90 except ProgramError, e:
91 basicsFailed(e.error)
93 def doUpdateCache(file):
94 return
96 def discardFile(file):
97 if file.change == "M":
98 runProgram(['hg', 'revert', file.dstName])
99 elif file.change == "?":
100 runProgram(['rm', '-f', file.dstName])
101 return
103 def ignoreFile(file):
104 hgignore = open('.hgignore', 'a')
105 print >> hgignore, "^" + re.escape(file.dstName) + "$"
106 hgignore.close()
109 # Not yet implemented
110 def commitIsMerge():
111 return False
113 def mergeMessage():
114 return ''