Patchwork [1,of,2,V2] subrepos: support adding files in git subrepos

login
register
mail settings
Submitter Matt Harbison
Date Feb. 27, 2015, 1:42 a.m.
Message ID <op.xuo0dobk9lwrgf@envy>
Download mbox | patch
Permalink /patch/7844/
State Superseded
Headers show

Comments

Matt Harbison - Feb. 27, 2015, 1:42 a.m.
On Thu, 26 Feb 2015 13:50:21 -0500, Mathias De Maré  
<mathias.demare@gmail.com> wrote:

> # HG changeset patch
> # User Mathias De Maré <mathias.demare@gmail.com>
> # Date 1424764162 -3600
> #      Tue Feb 24 08:49:22 2015 +0100
> # Node ID ad021801d5dcef705a942c638430cf1a83ef2e4d
> # Parent  ff5caa8dfd993680d9602ca6ebb14da9de10d5f4
> subrepos: support adding files in git subrepos
>
> This support includes correct matching, so includes,
> excludes and patterns are all supported.
>

> diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
> --- a/mercurial/subrepo.py
> +++ b/mercurial/subrepo.py
> @@ -1524,6 +1524,30 @@
>              return False
>     @annotatesubrepoerror
> +    def add(self, ui, match, prefix, explicitonly, **opts):
> +        if self._gitmissing():
> +            return []
> +        rev = self._state[1]
> +        if match.files():
> +            files = match.files()
> +        else:
> +            (modified, added, removed,
> +             deleted, unknown, ignored, clean) = self.status(None)
> +            files = unknown
> +
> +        files = [f for f in files if match(f)]
> +        for f in files:
> +            exact = match.exact(f)
> +            command = ["add"]
> +            if exact:
> +                command.append("-f") #should be added, even if ignored
> +            if ui.verbose or not exact:
> +                ui.status(_('adding %s\n') % match.rel(f))
> +            if not opts.get('dry_run'):
> +                self._gitcommand(command + [f])
> +        return []
> +
> +    @annotatesubrepoerror
>      def remove(self):
>          if self._gitmissing():
>              return

Good catches on the status messages and handling dry runs.  I missed that  
the first time.

Here is a patch to fix up the globs in the test, and another to make  
bypassing .hgignore work in more (all?) cases, with a more robust test.   
Feel free to fold in and modify as you see fit, or I can follow up with  
the .hgignore patch later- whatever is easiest.

Always returning [] means 'hg add' won't return 1 if adds in the subrepo  
fails, but since _gitcommand() seems to throw away the exit code, I don't  
see how to fix that.  This is still a nice step forward even without that.

--Matt


# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1424981760 18000
#      Thu Feb 26 15:16:00 2015 -0500
# Node ID dbbf78fc43e049af6e2d7a9edc23c4ae5553686a
# Parent  f6e1bdcdf1b4d51252763b4e1b6785094eadcc84
subrepo: add globs to tests
Mathias De Maré - Feb. 27, 2015, 7:01 p.m.
On Fri, Feb 27, 2015 at 2:42 AM, Matt Harbison <mharbison72@gmail.com>
wrote:

> On Thu, 26 Feb 2015 13:50:21 -0500, Mathias De Maré <
> mathias.demare@gmail.com> wrote:
>
>  # HG changeset patch
>> # User Mathias De Maré <mathias.demare@gmail.com>
>> # Date 1424764162 -3600
>> #      Tue Feb 24 08:49:22 2015 +0100
>> # Node ID ad021801d5dcef705a942c638430cf1a83ef2e4d
>> # Parent  ff5caa8dfd993680d9602ca6ebb14da9de10d5f4
>> subrepos: support adding files in git subrepos
>>
>> This support includes correct matching, so includes,
>> excludes and patterns are all supported.
>> <snip>
>>
> Good catches on the status messages and handling dry runs.  I missed that
> the first time.
>
> Here is a patch to fix up the globs in the test, and another to make
> bypassing .hgignore work in more (all?) cases, with a more robust test.
> Feel free to fold in and modify as you see fit, or I can follow up with the
> .hgignore patch later- whatever is easiest.
>
Oh, good point. I totally forgot about Windows, oops :-) I'll fold in the
glob changes, since those are needed to make the tests pass on Windows.
I prefer if you follow up with the .hgignore patch, if that's ok.

>
> Always returning [] means 'hg add' won't return 1 if adds in the subrepo
> fails, but since _gitcommand() seems to throw away the exit code, I don't
> see how to fix that.  This is still a nice step forward even without that.
>
I guess I could make a patch for that in the future, not sure if it's worth
the effort.

Greetings and thanks,
Mathias

Patch

diff --git a/tests/test-subrepo-git.t b/tests/test-subrepo-git.t
--- a/tests/test-subrepo-git.t
+++ b/tests/test-subrepo-git.t
@@ -871,7 +871,7 @@ 
    reverting subrepo ../gitroot

    $ hg add --subrepos "glob:**.python"
-  adding s/snake.python
+  adding s/snake.python (glob)
    $ hg st --subrepos s
    A s/snake.python
    ? s/barfoo
@@ -882,11 +882,11 @@ 
    reverting subrepo ../gitroot

    $ hg add --subrepos s
-  adding s/barfoo
-  adding s/c.c
-  adding s/cpp.cpp
-  adding s/foobar.orig
-  adding s/snake.python
+  adding s/barfoo (glob)
+  adding s/c.c (glob)
+  adding s/cpp.cpp (glob)
+  adding s/foobar.orig (glob)
+  adding s/snake.python (glob)
    $ hg st --subrepos s
    A s/barfoo
    A s/c.c
@@ -904,10 +904,10 @@ 
    ? s/snake.python

    $ hg add --subrepos --exclude "path:s/c.c"
-  adding s/barfoo
-  adding s/cpp.cpp
-  adding s/foobar.orig
-  adding s/snake.python
+  adding s/barfoo (glob)
+  adding s/cpp.cpp (glob)
+  adding s/foobar.orig (glob)
+  adding s/snake.python (glob)
    $ hg st --subrepos s
    A s/barfoo
    A s/cpp.cpp
@@ -923,7 +923,7 @@ 
    > EOF
    $ hg add .hgignore
    $ hg add --subrepos "glob:**.python"
-  adding s/snake.python
+  adding s/snake.python (glob)
    $ hg st --subrepos s
    A s/snake.python
    ? s/barfoo
@@ -962,10 +962,10 @@ 

  correctly do a dry run
    $ hg add --subrepos s --dry-run
-  adding s/barfoo
-  adding s/c.c
-  adding s/cpp.cpp
-  adding s/foobar.orig
+  adding s/barfoo (glob)
+  adding s/c.c (glob)
+  adding s/cpp.cpp (glob)
+  adding s/foobar.orig (glob)
    $ hg st --subrepos s
    A s/.gitignore
    A s/snake.python


# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1424984034 18000
#      Thu Feb 26 15:53:54 2015 -0500
# Node ID 127fa2cd0c50e6c3c0ee172c2060f64ccc5445a7
# Parent  dbbf78fc43e049af6e2d7a9edc23c4ae5553686a
subrepo: don't exclude files in .hgignore when adding to git

The previous test gave a false success because only an hg-ignored pattern  
was
specified.  Therefore match.files() was empty, and it fell back to the  
files
unknown to git.  The simplest fix is to always consider what is unknown to  
git,
as well as anything specified explicitly.  Files that are ignored by git  
can
only be introduced by an explicit mention in match.files().

diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -1528,14 +1528,15 @@ 
          if self._gitmissing():
              return []
          rev = self._state[1]
-        if match.files():
-            files = match.files()
-        else:
-            (modified, added, removed,
-             deleted, unknown, ignored, clean) = self.status(None)
-            files = unknown

-        files = [f for f in files if match(f)]
+        (modified, added, removed,
+         deleted, unknown, ignored, clean) = self.status(None)
+
+        # Unknown files not of interest will be rejected by the matcher
+        files = unknown
+        files.extend(match.files())
+
+        files = [f for f in sorted(set(files)) if match(f)]
          for f in files:
              exact = match.exact(f)
              command = ["add"]
diff --git a/tests/test-subrepo-git.t b/tests/test-subrepo-git.t
--- a/tests/test-subrepo-git.t
+++ b/tests/test-subrepo-git.t
@@ -922,11 +922,11 @@ 
    > *.python
    > EOF
    $ hg add .hgignore
-  $ hg add --subrepos "glob:**.python"
+  $ hg add --subrepos "glob:**.python" s/barfoo
    adding s/snake.python (glob)
    $ hg st --subrepos s
+  A s/barfoo
    A s/snake.python
-  ? s/barfoo
    ? s/c.c
    ? s/cpp.cpp
    ? s/foobar.orig