hg rawcommit command
authormpm@selenic.com
Tue, 31 May 2005 21:33:27 -0800
changeset 203 0b486b5e0796f9709915543a7971cdd84f40f459
parent 202 e875a0cf7f3a4c57e0b8e783e08a4434fd80ab12
child 204 161cef501e8d293067cae5dd962042eb20556fc5
push id1
push usergszorc@mozilla.com
push dateWed, 18 Mar 2015 16:34:57 +0000
hg rawcommit command -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 hg rawcommit command From: Christopher Li <hg@chrisli.org> This allows direct access to the commit command, primarily for importing from other SCMs. manifest hash: bea39fa8207582c9fa7ba0904721eb5113c61cf4 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCnUinywK+sNU5EO8RAhWqAJ9PiafRbfEIA3VsO07BbGZr5adNvgCfT2k7 blYTdkrIiRzzCxn6yPq8Yu4= =o8k0 -----END PGP SIGNATURE-----
hg
mercurial/hg.py
--- a/hg
+++ b/hg
@@ -145,17 +145,43 @@ elif cmd == "remove" or cmd == "rm" or c
     repo.remove(args)
 
 elif cmd == "commit" or cmd == "checkin" or cmd == "ci":
     if 1:
         if len(args) > 0:
             repo.commit(repo.current, args)
         else:
             repo.commit(repo.current)
-
+elif cmd == "rawcommit":
+    "raw commit interface"
+    rc = {}
+    opts = [('p', 'parent', [], 'parent'),
+            ('d', 'date', "", 'data'),
+            ('u', 'user', "", 'user'),
+            ('F', 'files', "", 'file list'),
+            ('t', 'text', "", 'commit text'),
+            ('l', 'logfile', "", 'commit text file')
+            ]
+    args = fancyopts.fancyopts(args, opts, rc,
+                               "hg rawcommit [options] files")
+    text = rc['text']
+    if not text and rc['logfile']:
+        try: text = open(rc['logfile']).read()
+        except IOError: pass
+    if not text and not rc['logfile']:
+        print "missing commit text"
+        sys.exit(0)
+    if rc['files']:
+        files = open(rc['files']).read().splitlines()
+    else:
+        files = args
+        
+    repo.rawcommit(files, text, rc['user'], rc['date'], *rc['parent'])
+    
+ 
 elif cmd == "import" or cmd == "patch":
     try:
         import psyco
         psyco.full()
     except:
         pass
     
     ioptions = {}
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -132,21 +132,23 @@ class changelog(revlog):
         user = l[1]
         date = l[2]
         files = l[3:]
         return (manifest, user, date, files, desc)
 
     def read(self, node):
         return self.extract(self.revision(node))
 
-    def add(self, manifest, list, desc, transaction, p1=None, p2=None):
-        user = (os.environ.get("HGUSER") or
+    def add(self, manifest, list, desc, transaction, p1=None, p2=None,
+                  user=None, date=None):
+        user = (user or
+                os.environ.get("HGUSER") or
                 os.environ.get("EMAIL") or
                 os.environ.get("LOGNAME", "unknown") + '@' + socket.getfqdn())
-        date = "%d %d" % (time.time(), time.timezone)
+        date = date or "%d %d" % (time.time(), time.timezone)
         list.sort()
         l = [hex(manifest), user, date] + list + ["", desc]
         text = "\n".join(l)
         return self.addrevision(text, transaction, self.count(), p1, p2)
 
 class dircache:
     def __init__(self, opener, ui):
         self.opener = opener
@@ -339,16 +341,40 @@ class localrepository:
         try:
             return lock.lock(self.join("lock"), 0)
         except lock.LockHeld, inst:
             if wait:
                 self.ui.warn("waiting for lock held by %s\n" % inst.args[0])
                 return lock.lock(self.join("lock"), wait)
             raise inst
 
+    def rawcommit(self, files, text, user, date, p1=None, p2=None):
+        p1 = p1 or self.current or nullid
+        pchange = self.changelog.read(p1)
+        pmmap = self.manifest.read(pchange[0])
+        tr = self.transaction()
+        mmap = {}
+        linkrev = self.changelog.count()
+        for f in files:
+            try:
+                t = file(f).read()
+            except IOError:
+                self.ui.warn("Read file %s error, skipped\n" % f)
+                continue
+            r = self.file(f)
+            prev = pmmap.get(f, nullid)
+            mmap[f] = r.add(t, tr, linkrev, prev)
+
+        mnode = self.manifest.add(mmap, tr, linkrev, pchange[0])
+        n = self.changelog.add(mnode, files, text, tr, p1, p2, user ,date, )
+        tr.close()
+        self.setcurrent(n)
+        self.dircache.clear()
+        self.dircache.update(mmap)
+
     def commit(self, parent, update = None, text = ""):
         self.lock()
         try:
             remove = [ l[:-1] for l in self.opener("to-remove") ]
             os.unlink(self.join("to-remove"))
 
         except IOError:
             remove = []