Recursively add files by pattern

前端 未结 11 1365
予麋鹿
予麋鹿 2020-11-28 18:18

How do I recursively add files by a pattern (or glob) located in different directories?

For example, I\'d like to add A/B/C/foo.java and D/E/F/bar

11条回答
  •  执念已碎
    2020-11-28 18:53

    As mentioned in "git: How do I recursively add all files in a directory subtree that match a glob pattern?", if you properly escape or quote your pathspec globbing (like '*.java'), then yes, git add '*.java'

    Git 2.13 (Q2 2017) improves that for interactive add:

    See commit 7288e12 (14 Mar 2017) by Jeff King (peff).
    (Merged by Junio C Hamano -- gitster -- in commit 153e0d7, 17 Mar 2017)

    add --interactive: do not expand pathspecs with ls-files

    When we want to get the list of modified files, we first expand any user-provided pathspecs with "ls-files", and then feed the resulting list of paths as arguments to "diff-index" and "diff-files".
    If your pathspec expands into a large number of paths, you may run into one of two problems:

    1. The OS may complain about the size of the argument list, and refuse to run. For example:

      $ (ulimit -s 128 && git add -p drivers)
      Can't exec "git": Argument list too long at .../git-add--interactive line 177.
      Died at .../git-add--interactive line 177.
      

    That's on the linux.git repository, which has about 20K files in the "drivers" directory (none of them modified in this case). The "ulimit -s" trick is necessary to show the problem on Linux even for such a gigantic set of paths.
    Other operating systems have much smaller limits (e.g., a real-world case was seen with only 5K files on OS X).

    1. Even when it does work, it's really slow. The pathspec code is not optimized for huge numbers of paths. Here's the same case without the ulimit:

      $ time git add -p drivers
        No changes.
      
      real  0m16.559s
      user    0m53.140s
      sys 0m0.220s
      

    We can improve this by skipping "ls-files" completely, and just feeding the original pathspecs to the diff commands.

    Historically the pathspec language supported by "diff-index" was weaker, but that is no longer the case.

提交回复
热议问题