Fix hgct diffs for removed files.
[hgct.git] / hg.py
blobf08f33078eca8cc8781235fa388595584f63476f
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 print inp
39 ret = []
40 try:
41 recs = inp.split("\n")
42 recs.pop() # remove last entry (which is '')
43 it = recs.__iter__()
44 while True:
45 rec = it.next()
46 m = parseDiffRE.match(rec)
48 if not m:
49 print "Unknown output from hg status!: " + rec + "\n"
50 continue
52 f = File()
53 f.change = m.group(1)
54 f.srcName = f.dstName = m.group(2)
55 print "Getting patch for file " + f.srcName
57 f.patch = __getPatch(f)
59 ret.append(f)
60 except StopIteration:
61 pass
62 return ret
65 # HEAD is src in the returned File objects. That is, srcName is the
66 # name in HEAD and dstName is the name in the cache.
67 def getFiles():
68 files = __parseStatus()
69 for f in files:
70 c = f.change
71 if c == 'A':
72 f.text = 'Added file: ' + f.srcName
73 elif c == '?':
74 f.text = 'New file: ' + f.srcName
75 elif c == 'R':
76 f.text = 'Removed file: ' + f.srcName
77 else:
78 f.text = f.srcName
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 if file.change == "M":
100 runProgram(['hg', 'revert', file.dstName])
101 elif file.change == "?":
102 runProgram(['rm', '-f', file.dstName])
103 return
105 def ignoreFile(file):
106 hgignore = open('.hgignore', 'a')
107 print >> hgignore, "^" + re.escape(file.dstName) + "$"
108 hgignore.close()
111 # Not yet implemented
112 def commitIsMerge():
113 return False
115 def mergeMessage():
116 return ''