Commits

Sheng Mao committed 615bbfa

finish modify status in .hgissue

Comments (0)

Files changed (1)

 status:%s
 '''
 
+issue_anchor = 'issue #'
+
 labels = [
     'ID:', # id_label
     'node:', # node_label
 
 issue_file_name = '.hgissue'
 
-status_list = [
-    'ON', # for open
-    'OF'  # for close
-    ]
+status_dict = {
+    'open':'ON',
+    'close':'OF',
+    'fix':'FX'
+    }
 
 #classes
 class ArgumentError(StandardError):
         self.node = node
         self.status = status
 
-# functions
-def pretxncommithook(ui, repo, **kwargs):
-    ctx = repo[kwargs['node']]
-    print ctx.rev()
+# hook
+def pretxncommithook(ui, repo, **args):
+    ctx = repo[args['node']]
+    if not ctx:
+        raise LookupNodeError('no node %s' % args['node'])
+    log = ctx.description()
+    pos = log.find(issue_anchor)
+    pos_line = log.find('\n')
+    if pos_line == -1:
+        pos_line = len(log)
+    if -1 < pos < pos_line:
+        verb = log[:pos-1]
+    else:
+        return False
+    status = status_dict[verb]
+    if status:
+        issues = parse_hgissue_file()
+        id = log[pos+len(issue_anchor):pos_line]
+        iss = find_issue(issues, id)
+        if not iss:
+            raise LookupNodeError('no issue %s' % id)
+        iss.status = status
+        serialize_issues(issues)
 
 def reposetup(ui, repo):
     # Repository-specific hooks can be configured here. These include
     import sys, os
     sys.path.append(os.environ['PYTHONLIB'])
 
+# functions
 def issue(ui, repo, arg = None, **opts):
     ''' manage issues '''
     #default
     import uuid
     id = uuid.uuid1()
     log_msg = log_template % (id, msg)
-    file_msg = file_template % (id, node.hex(repo['.'].node()), "ON")
+    file_msg = file_template % (id, node.hex(repo['.'].node()),
+                                status_dict['open'])
     fp = open(issue_file_name, 'a+')
     fp.write(file_msg)
     fp.close()
     commands.commit(ui, repo, issue_file_name, message=log_msg, addremove=True)
-    pass
 
 def issue_list(ui, repo, id, rev, use_id):
     import os
         issues.append(Issue(items[j], items[j+1], items[j+2]))
     return issues
 
+def serialize_issues(issues):
+    with open(issue_file_name, 'w') as fp:
+        for i in issues:
+            fp.write(file_template % (i.id, i.node, i.status))
+
+def find_issue(issues, id):
+    for i in issues:
+        if i.id == id:
+            return i
+    return None
+
 cmdtable = {
     # "command-name": (function-call, options-list, help-string)
     'issue':