Patchwork [6,of,8,V2] context: write dirstate out explicitly at the end of markcommitted

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 8, 2015, 8:08 a.m.
Message ID <1e4c4d9736bb1051f963.1436342922@feefifofum>
Download mbox | patch
Permalink /patch/9922/
State Accepted
Commit 4d1382fd96fff7df46873a6454c6107e35742ceb
Headers show

Comments

Katsunori FUJIWARA - July 8, 2015, 8:08 a.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1436342469 -32400
#      Wed Jul 08 17:01:09 2015 +0900
# Node ID 1e4c4d9736bb1051f963a9624320265d91703622
# Parent  12f303caa3d5beb009d47cae96285b8b6020aae3
context: write dirstate out explicitly at the end of markcommitted

To detect change of a file without redundant comparison of file
content, dirstate recognizes a file as certainly clean, if:

  (1) it is already known as "normal",
  (2) dirstate entry for it has valid (= not "-1") timestamp, and
  (3) mode, size and timestamp of it on the filesystem are as same as
      ones expected in dirstate

This works as expected in many cases, but doesn't in the corner case
that changing a file keeps mode, size and timestamp of it on the
filesystem.

The timetable below shows steps in one of typical such situations:

  ---- ----------------------------------- ----------------
                                           timestamp of "f"
                                           ----------------
                                           dirstate   file-
  time          action                     mem  file  system
  ---- ----------------------------------- ---- ----- -----
   *                                             ***    ***
       - `hg transplant REV1 REV2 ...`
         - transplanting REV1
           ....
   N
           - change "f", but keep size                   N
             (via `patch.patch()`)
           - `dirstate.normal("f")`          N   ***
             (via `repo.commit()`)

         - transplanting REV2
           - change "f", but keep size                   N
             (via `patch.patch()`)
           - aborted while patching
  N+1
         - release wlock
           - `dirstate.write()`              N    N      N

       - `hg status` shows "r1" as "clean"   N    N      N
  ---- ----------------------------------- ---- ----- -----

The most important point is that `dirstate.write()` is executed at N+1
or later. This causes writing dirstate timestamp N of "f" out
successfully. If it is executed at N, `parsers.pack_dirstate()`
replaces timestamp N with "-1" before actual writing dirstate out.

This issue can occur when `hg transplant` satisfies conditions below:

  - multiple revisions to be transplanted change the same file
  - those revisions don't change mode and size of the file, and
  - the 2nd or later revision of them fails after changing the file

The root cause of this issue is that files are changed without
flushing in-memory dirstate changes via `repo.commit()` (even though
omitting `dirstate.normallookup()` on files changed by `patch.patch()`
for efficiency also causes this issue).

To detect changes of files correctly, this patch writes in-memory
dirstate changes out explicitly after marking files as clean in
`committablectx.markcommitted()`, which is invoked via
`repo.commit()`.

After this change, timetable is changed as below:

  ---- ----------------------------------- ----------------
                                           timestamp of "f"
                                           ----------------
                                           dirstate   file-
  time          action                     mem  file  system
  ---- ----------------------------------- ---- ----- -----
   *                                             ***    ***
       - `hg transplant REV1 REV2 ...`
         - transplanting REV1
           ....
   N
           - change "f", but keep size                   N
             (via `patch.patch()`)
           - `dirstate.normal("f")`          N    ***
             (via `repo.commit()`)
       ----------------------------------- ---- ----- -----
           - `dirsttate.write()`            -1    -1
       ----------------------------------- ---- ----- -----
         - transplanting REV2
           - change "f", but keep size                   N
             (via `patch.patch()`)
           - aborted while patching
  N+1
         - release wlock
           - `dirstate.write()`              -1   -1     N

       - `hg status` shows "r1" as "clean"   -1   -1     N
  ---- ----------------------------------- ---- ----- -----

To reproduce this issue in tests certainly, this patch emulates some
timing critical actions as below:

  - change "f" at N

    `patch.patch()` with `fakepatchtime.py` explicitly changes mtime
    of patched files to "2000-01-01 00:00" (= N).

  - `dirstate.write()` via `repo.commit()` at N

    `fakedirstatewritetime.py` forces `pack_dirstate()` to use
    "2000-01-01 00:00" as "now", only if `pack_dirstate()` is invoked
    via `committablectx.markcommitted()`.

  - `dirstate.write()` via releasing wlock at N+1 (or "not at N")

    `pack_dirstate()` via releasing wlock uses actual timestamp at
    runtime as "now", and it should be different from the "2000-01-01
    00:00" of "f".

BTW, this patch doesn't test cases below, even though `patch.patch()`
is used similarly in these cases:

  1. failure of `hg import` or `hg qpush`

  2. success of `hg import`, `hg qpush` or `hg transplant`

Case (1) above doesn't cause this kind of issue, because:

  - if patching is aborted by conflicts, changed files are committed

    changed files are marked as CLEAN, even though they are partially
    patched.

  - otherwise, dirstate are fully restored by `dirstateguard`

    For example in timetable above, timestamp of "f" in .hg/dirstate
    is restored to -1 (or less than N), and subsequent `hg status` can
    detect changes correctly.

Case (2) always causes `repo.status()` invocation via `repo.commit()`
just after changing files inside same wlock scope.

  ---- ----------------------------------- ----------------
                                           timestamp of "f"
                                           ----------------
                                           dirstate   file-
  time          action                     mem  file  system
  ---- ----------------------------------- ---- ----- -----
  N                                              ***   ***
       - make file "f" clean                            N

       - execute `hg foobar`
         ....
         - `dirstate.normal("f")`           N    ***
           (e.g. via dirty check
            or previous `repo.commit()`)

         - change "f", but keep size                    N
         - `repo.status()` (*1)
           (via `repo.commit()`)
  ---- ----------------------------------- ---- ----- -----

At a glance, `repo.status()` at (*1) seems to cause similar issue (=
"changed files are treated as clean"), but actually doesn't.

`dirstate._lastnormaltime` should be N at (*1) above, because
`dirstate.normal()` via dirty check is finished at N.

Therefore, "f" changed at N (= `dirstate._lastnormaltime`) is forcibly
treated as "unsure" at (*1), and changes are detected as expected (see
`dirstate.status()` for detail).

If `hg import` is executed with `--no-commit`, `repo.status()` isn't
invoked just after changing files inside same wlock scope.

But preceding `dirstate.normal()` is invoked inside another wlock
scope via `cmdutil.bailifchanged()`, and in-memory changes should be
flushed at the end of that scope.

Therefore, timestamp N of clean "f" should be replaced by -1, if
`dirstate.write()` is invoked at N. It means that condition of this
issue isn't satisfied.

Patch

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -1316,6 +1316,11 @@ 
         self._repo.dirstate.setparents(node)
         self._repo.dirstate.endparentchange()
 
+        # write changes out explicitly, because nesting wlock at
+        # runtime may prevent `wlock.release()` in `repo.commit()`
+        # from immediately doing so for subsequent changing files
+        self._repo.dirstate.write()
+
 class workingctx(committablectx):
     """A workingctx object makes access to data related to
     the current working directory convenient.
diff --git a/tests/test-transplant.t b/tests/test-transplant.t
--- a/tests/test-transplant.t
+++ b/tests/test-transplant.t
@@ -791,3 +791,90 @@ 
 
   $ killdaemons.py
 
+Test that patch-ed files are treated as "modified", when transplant is
+aborted by failure of patching, even if none of mode, size and
+timestamp of them isn't changed on the filesystem (see also issue4583)
+
+  $ cd t
+
+  $ cat > $TESTTMP/abort.py <<EOF
+  > # emulate that patch.patch() is aborted at patching on "abort" file
+  > from mercurial import extensions, patch as patchmod
+  > def patch(orig, ui, repo, patchname,
+  >           strip=1, prefix='', files=None,
+  >           eolmode='strict', similarity=0):
+  >     if files is None:
+  >         files = set()
+  >     r = orig(ui, repo, patchname,
+  >         strip=strip, prefix=prefix, files=files,
+  >         eolmode=eolmode, similarity=similarity)
+  >     if 'abort' in files:
+  >         raise patchmod.PatchError('intentional error while patching')
+  >     return r
+  > def extsetup(ui):
+  >     extensions.wrapfunction(patchmod, 'patch', patch)
+  > EOF
+
+  $ echo X1 > r1
+  $ hg diff --nodates r1
+  diff -r a53251cdf717 r1
+  --- a/r1
+  +++ b/r1
+  @@ -1,1 +1,1 @@
+  -r1
+  +X1
+  $ hg commit -m "X1 as r1"
+
+  $ echo 'marking to abort patching' > abort
+  $ hg add abort
+  $ echo Y1 > r1
+  $ hg diff --nodates r1
+  diff -r 22c515968f13 r1
+  --- a/r1
+  +++ b/r1
+  @@ -1,1 +1,1 @@
+  -X1
+  +Y1
+  $ hg commit -m "Y1 as r1"
+
+  $ hg update -q -C d11e3596cc1a
+  $ cat r1
+  r1
+
+  $ cat >> .hg/hgrc <<EOF
+  > [fakedirstatewritetime]
+  > # emulate invoking dirstate.write() via repo.status() or markcommitted()
+  > # at 2000-01-01 00:00
+  > fakenow = 200001010000
+  > 
+  > # emulate invoking patch.internalpatch() at 2000-01-01 00:00
+  > [fakepatchtime]
+  > fakenow = 200001010000
+  > 
+  > [extensions]
+  > fakedirstatewritetime = $TESTDIR/fakedirstatewritetime.py
+  > fakepatchtime = $TESTDIR/fakepatchtime.py
+  > abort =  $TESTTMP/abort.py
+  > EOF
+  $ hg transplant "22c515968f13::"
+  applying 22c515968f13
+  22c515968f13 transplanted to * (glob)
+  applying e38700ba9dd3
+  intentional error while patching
+  abort: fix up the merge and run hg transplant --continue
+  [255]
+  $ cat >> .hg/hgrc <<EOF
+  > [hooks]
+  > fakedirstatewritetime = !
+  > fakepatchtime = !
+  > abort = !
+  > EOF
+
+  $ cat r1
+  Y1
+  $ hg debugstate | grep ' r1$'
+  n 644          3 unset               r1
+  $ hg status -A r1
+  M r1
+
+  $ cd ..