[PATCH] record: hg record can't edit new files (issue 4304)

Laurent Charignon lcharignon at fb.com
Mon Mar 9 18:34:04 CDT 2015


# HG changeset patch
# User Laurent Charignon <lcharignon at fb.com>
# Date 1425686263 28800
#      Fri Mar 06 15:57:43 2015 -0800
# Node ID 46dc074811e37b142f46b961aad03463d065eae5
# Parent  4e865115566e75f938cbff9dcf081da39008a161
record: hg record can't edit new files (issue 4304)

While using the record extension to select changes, the user couldn't see the
content of newly added files and had to select/reject them based on filename.
The test is changed accordingly in two places.

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -76,7 +76,7 @@
     """
     diffgit_re = re.compile('diff --git a/(.*) b/(.*)$')
     diff_re = re.compile('diff -r .* (.*)$')
-    allhunks_re = re.compile('(?:index|new file|deleted file) ')
+    allhunks_re = re.compile('(?:index|deleted file) ')
     pretty_re = re.compile('(?:new file|deleted file) ')
     special_re = re.compile('(?:index|new|deleted|copy|rename) ')
 
@@ -522,9 +522,9 @@
         diffopts = patch.difffeatureopts(ui, opts=opts, whitespace=True)
         diffopts.nodates = True
         diffopts.git = True
-        chunks = patch.diff(repo, changes=status, opts=diffopts)
+        originalChunks = patch.diff(repo, changes=status, opts=diffopts)
         fp = cStringIO.StringIO()
-        fp.write(''.join(chunks))
+        fp.write(''.join(originalChunks))
         fp.seek(0)
 
         # 1. filter patch, so we have intending-to apply subset of it
@@ -548,13 +548,21 @@
             ui.status(_('no changes to record\n'))
             return 0
 
+        newAndModifiedFiles = set()
+        for h in chunks:
+            isHunk = isinstance(h, hunk)
+            isNew = h.filename() in status.added
+            if isHunk and isNew and not h in originalChunks:
+                newAndModifiedFiles.add(h.filename())
+
         modified = set(status.modified)
 
         # 2. backup changed files, so we can restore them in the end
+
         if backupall:
             tobackup = changed
         else:
-            tobackup = [f for f in newfiles if f in modified]
+            tobackup = [f for f in newfiles if f in modified or f in newAndModifiedFiles]
 
         backups = {}
         if tobackup:
@@ -577,11 +585,13 @@
 
             fp = cStringIO.StringIO()
             for c in chunks:
-                if c.filename() in backups:
+                if c.filename() in backups or c.filename() in newAndModifiedFiles:
                     c.write(fp)
             dopatch = fp.tell()
             fp.seek(0)
 
+            [os.unlink(c) for c in newAndModifiedFiles]
+
             # 3a. apply filtered patch to clean repo  (clean)
             if backups:
                 hg.revert(repo, repo.dirstate.p1(),
diff --git a/tests/test-keyword.t b/tests/test-keyword.t
--- a/tests/test-keyword.t
+++ b/tests/test-keyword.t
@@ -473,18 +473,24 @@
 
   $ hg -v record -l msg -d '12 2' r<<EOF
   > y
+  > y
   > EOF
   diff --git a/r b/r
   new file mode 100644
   examine changes to 'r'? [Ynesfdaq?] y
   
+  @@ -0,0 +1,1 @@
+  +$Id$
+  record this change to 'r'? [Ynesfdaq?] y
+  
+  resolving manifests
+  patching file r
   committing files:
   r
   committing manifest
   committing changelog
   committed changeset 3:82a2f715724d
   overwriting r expanding keywords
- - status call required for dirstate.normallookup() check
   $ hg status r
   $ hg --verbose rollback
   repository tip rolled back to revision 2 (undo commit)
@@ -501,11 +507,18 @@
   $ hg add i
   $ hg --verbose record -d '13 1' -m recignored<<EOF
   > y
+  > y
   > EOF
   diff --git a/i b/i
   new file mode 100644
   examine changes to 'i'? [Ynesfdaq?] y
   
+  @@ -0,0 +1,1 @@
+  +$Id$
+  record this change to 'i'? [Ynesfdaq?] y
+  
+  resolving manifests
+  patching file i
   committing files:
   i
   committing manifest
diff --git a/tests/test-mq-subrepo.t b/tests/test-mq-subrepo.t
--- a/tests/test-mq-subrepo.t
+++ b/tests/test-mq-subrepo.t
@@ -295,6 +295,11 @@
   new file mode 100644
   examine changes to '.hgsub'? [Ynesfdaq?] y
   
+  @@ -0,0 +1,1 @@
+  +sub = sub
+  record this change to '.hgsub'? [Ynesfdaq?] y
+  
+  warning: subrepo spec file .hgsub not found
   abort: uncommitted changes in subrepository sub
   [255]
   % update substate when adding .hgsub w/clean updated subrepo
@@ -304,10 +309,14 @@
   new file mode 100644
   examine changes to '.hgsub'? [Ynesfdaq?] y
   
+  @@ -0,0 +1,1 @@
+  +sub = sub
+  record this change to '.hgsub'? [Ynesfdaq?] y
+  
+  warning: subrepo spec file .hgsub not found
   path sub
    source   sub
    revision b2fdb12cd82b021c3b7053d67802e77b6eeaee31
-
   $ testmod qrecord --config ui.interactive=1 -m1 1.diff <<EOF
   > y
   > y
diff --git a/tests/test-record.t b/tests/test-record.t
--- a/tests/test-record.t
+++ b/tests/test-record.t
@@ -235,7 +235,19 @@
   new file mode 100644
   examine changes to 'plain'? [Ynesfdaq?] y
   
-
+  @@ -0,0 +1,10 @@
+  +1
+  +2
+  +3
+  +4
+  +5
+  +6
+  +7
+  +8
+  +9
+  +10
+  record this change to 'plain'? [Ynesfdaq?] y
+  
   $ hg tip -p
   changeset:   7:11fb457c1be4
   tag:         tip
@@ -317,6 +329,7 @@
   > y
   > y
   > y
+  > y
   > EOF
   diff --git a/plain b/plain
   1 hunks, 1 lines changed
@@ -335,7 +348,10 @@
   new file mode 100644
   examine changes to 'plain2'? [Ynesfdaq?] y
   
-
+  @@ -0,0 +1,1 @@
+  +1
+  record change 2/2 to 'plain2'? [Ynesfdaq?] y
+  
 Modify beginning, trim end, record both, add another file to test
 changes numbering
 
@@ -1353,6 +1369,8 @@
    c
   +d
   
+
+
 Test --user when ui.username not set
   $ unset HGUSER
   $ echo e >> subdir/f1
@@ -1376,4 +1394,41 @@
   $ HGUSER="test"
   $ export HGUSER
 
+
+Editing patch of newly added file
+
+  $ cat > editor.sh << '__EOF__'
+  > cat "$1"  | sed "s/first/very/g"  > tt
+  > mv tt  "$1"
+  > __EOF__
+  $ cat > newfile << '__EOF__'
+  > This is the first line
+  > This is the second line
+  > This is the third line
+  > __EOF__
+  $ hg add newfile
+  $ HGEDITOR="\"sh\" \"`pwd`/editor.sh\"" hg record -d '23 0' -medit-patch-new <<EOF
+  > y
+  > e
+  > EOF
+  diff --git a/newfile b/newfile
+  new file mode 100644
+  examine changes to 'newfile'? [Ynesfdaq?] y
+  
+  @@ -0,0 +1,3 @@
+  +This is the first line
+  +This is the second line
+  +This is the third line
+  record this change to 'newfile'? [Ynesfdaq?] e
+  
+  $ hg cat -r tip newfile
+  This is the very line
+  This is the second line
+  This is the third line
+
+  $ cat newfile
+  This is the first line
+  This is the second line
+  This is the third line
+
   $ cd ..


More information about the Mercurial-devel mailing list