Add "discard changes" feature
[hgct.git] / hg.py
blob64dabaef5b3c0a6cd2ab2f78828d9d82f2be86e5
1 import re
3 from ctcore import *
5 def doCommit(keepFiles, selFileNames, msg):
6 print "Commit message:" + msg
7 runProgram(['hg commit', '-A', '-l', '-'] + selFileNames, msg)
10 parseDiffRE = re.compile('([AMR?]) (.*)')
12 def __getPatch(file, otherFile = None):
13 if otherFile:
14 f = [file, otherFile]
15 else:
16 f = [file]
17 return runProgram(['hg diff'] + f)
19 def __parseStatus():
20 inp = runProgram('hg status')
21 print inp
22 ret = []
23 try:
24 recs = inp.split("\n")
25 recs.pop() # remove last entry (which is '')
26 it = recs.__iter__()
27 while True:
28 rec = it.next()
29 m = parseDiffRE.match(rec)
31 if not m:
32 print "Unknown output from hg status!: " + rec + "\n"
33 continue
35 f = File()
36 f.change = m.group(1)
37 f.srcName = f.dstName = m.group(2)
38 print "Getting patch for file " + f.srcName
40 f.patch = __getPatch(f.srcName)
42 ret.append(f)
43 except StopIteration:
44 pass
45 return ret
48 # HEAD is src in the returned File objects. That is, srcName is the
49 # name in HEAD and dstName is the name in the cache.
50 def getFiles():
51 files = __parseStatus()
52 for f in files:
53 c = f.change
54 if c == 'A':
55 f.text = 'Added file: ' + f.srcName
56 elif c == '?':
57 f.text = 'New file: ' + f.srcName
58 elif c == 'R':
59 f.text = 'Removed file: ' + f.srcName
60 else:
61 f.text = f.srcName
63 f.updated = True
64 return files
66 def repoValid():
67 def basicsFailed(msg):
68 print "hg status: " + msg
69 print "Make sure that the current working directory contains a '.hg' directory."
70 sys.exit(1)
72 try:
73 runProgram('hg status')
74 except OSError, e:
75 basicsFailed(e.strerror)
76 except ProgramError, e:
77 basicsFailed(e.error)
79 def doUpdatecache(file):
80 return
82 def discardFile(file):
83 # Not implemented yet.
84 return