Fix Git support for rename diffs.
[hgct.git] / hg.py
blobd746f5b7a3affabec90d25dd3a134407b0461a84
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License version 2 as
3 # published by the Free Software Foundation.
4 #
5 # This program is distributed in the hope that it will be useful,
6 # but WITHOUT ANY WARRANTY; without even the implied warranty of
7 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 # GNU General Public License for more details.
9 #
10 # You should have received a copy of the GNU General Public License
11 # along with this program; if not, write to the Free Software
12 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 import re
16 from ctcore import *
18 def doCommit(keepFiles, selFileNames, msg):
19 print "Commit message:" + msg
20 runProgram(['hg', 'commit', '-A', '-l', '-'] + selFileNames, msg)
23 parseDiffRE = re.compile('([AMR?]) (.*)')
25 def __getPatch(file, otherFile = None):
26 if otherFile:
27 f = [file, otherFile]
28 else:
29 f = [file]
31 return runProgram(['hg', 'diff'] + f)
33 def __parseStatus():
34 inp = runProgram(['hg', 'status'])
35 print inp
36 ret = []
37 try:
38 recs = inp.split("\n")
39 recs.pop() # remove last entry (which is '')
40 it = recs.__iter__()
41 while True:
42 rec = it.next()
43 m = parseDiffRE.match(rec)
45 if not m:
46 print "Unknown output from hg status!: " + rec + "\n"
47 continue
49 f = File()
50 f.change = m.group(1)
51 f.srcName = f.dstName = m.group(2)
52 print "Getting patch for file " + f.srcName
54 f.patch = __getPatch(f.srcName)
56 ret.append(f)
57 except StopIteration:
58 pass
59 return ret
62 # HEAD is src in the returned File objects. That is, srcName is the
63 # name in HEAD and dstName is the name in the cache.
64 def getFiles():
65 files = __parseStatus()
66 for f in files:
67 c = f.change
68 if c == 'A':
69 f.text = 'Added file: ' + f.srcName
70 elif c == '?':
71 f.text = 'New file: ' + f.srcName
72 elif c == 'R':
73 f.text = 'Removed file: ' + f.srcName
74 else:
75 f.text = f.srcName
77 f.updated = True
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 runProgram(['hg', 'revert', file.dstName])
98 return
100 def ignoreFile(file):
101 hgignore = open('.hgignore', 'a')
102 print >> hgignore, "^" + re.escape(file.dstName) + "$"
103 hgignore.close()