changeset 102:c44e2c19caaa

- handle modifications made to newly-added files ; fixes issue #13
author Mark Edgington <edgimar@gmail.com>
date Tue, 18 Mar 2014 12:36:27 -0400
parents af245f45c93d
children 679dc6c2df5d
files crecord/crecord_core.py
diffstat 1 files changed, 21 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/crecord/crecord_core.py
+++ b/crecord/crecord_core.py
@@ -46,7 +46,11 @@
             raise util.Abort(_('cannot partially commit a merge '
                                '(use hg commit instead)'))
 
+        # status gives back
+        #   modified, added, removed, deleted, unknown, ignored, clean
+        # we take only the first 3 of these
         changes = repo.status(match=match)[:3]
+        modified, added, removed = changes
         diffopts = opts.copy()
         diffopts['nodates'] = True
         diffopts['git'] = True
@@ -76,10 +80,10 @@
             ui.status(_('no changes to record\n'))
             return 0
 
-        modified = set(changes[0])
 
         # 2. backup changed files, so we can restore them in the end
         backups = {}
+        newly_added_backups = {}
         backupdir = repo.join('record-backups')
         try:
             os.mkdir(backupdir)
@@ -89,18 +93,24 @@
         try:
             # backup continues
             for f in newfiles:
-                if f not in modified:
+                if f not in (modified + added):
                     continue
                 fd, tmpname = tempfile.mkstemp(prefix=f.replace('/', '_')+'.',
                                                dir=backupdir)
                 os.close(fd)
                 ui.debug('backup %r as %r\n' % (f, tmpname))
                 util.copyfile(repo.wjoin(f), tmpname)
-                backups[f] = tmpname
+                if f in modified:
+                    backups[f] = tmpname
+                elif f in added:
+                    newly_added_backups[f] = tmpname
 
             fp = cStringIO.StringIO()
+            all_backups = {}
+            all_backups.update(backups)
+            all_backups.update(newly_added_backups)
             for c in chunks:
-                if c.filename() in backups:
+                if c.filename() in all_backups:
                     c.write(fp)
             dopatch = fp.tell()
             fp.seek(0)
@@ -117,6 +127,9 @@
             if backups:
                 hg.revert(repo, repo.dirstate.parents()[0],
                           lambda key: key in backups)
+            # remove newly added files from 'clean' repo (so patch can apply)
+            for f in newly_added_backups:
+                os.unlink(f)
 
             # 3b. (apply)
             if dopatch:
@@ -172,6 +185,10 @@
                     ui.debug('restoring %r to %r\n' % (tmpname, realname))
                     util.copyfile(tmpname, repo.wjoin(realname))
                     os.unlink(tmpname)
+                for realname, tmpname in newly_added_backups.iteritems():
+                    ui.debug('restoring %r to %r\n' % (tmpname, realname))
+                    util.copyfile(tmpname, repo.wjoin(realname))
+                    os.unlink(tmpname)
                 os.rmdir(backupdir)
             except OSError:
                 pass