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).

  2. 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.
This solution was discussed in 2010:

  http://public-inbox.org/git/20100105041438.GB12574@coredump.intra.peff.net/

but at the time the diff code's pathspecs were more
primitive than those used by ls-files (e.g., they did not
support globs). Making the change would have caused a
user-visible regression, so we didn't.

Since then, the pathspec code has been unified, and the diff
commands natively understand pathspecs like '*.c'.

This patch implements that solution. That skips the
argument-list limits, and the result runs much faster:

  $ time git add -p drivers
  No changes.

  real	0m0.149s
  user	0m0.116s
  sys	0m0.080s

There are two new tests. The first just exercises the
globbing behavior to confirm that we are not causing a
regression there. The second checks the actual argument
behavior using GIT_TRACE. We _could_ do it with the "ulimit
-s" trick, as above. But that would mean the test could only
run where "ulimit -s" works. And tests of that sort are
expensive, because we have to come up with enough files to
actually bust the limit (we can't just shrink the "128" down
infinitely, since it is also the in-program stack size).

Finally, two caveats and possibilities for future work:

  a. This fixes one argument-list expansion, but there may
     be others. In fact, it's very likely that if you run
     "git add -i" and select a large number of modified
     files that the script would try to feed them all to a
     single git command.

     In practice this is probably fine. The real issue here
     is that the argument list was growing with the _total_
     number of files, not the number of modified or selected
     files.

  b. If the repository contains filenames with literal wildcard
     characters (e.g., "foo*"), the original code expanded
     them via "ls-files" and then fed those wildcard names
     to "diff-index", which would have treated them as
     wildcards. This was a bug, which is now fixed (though
     unless you really go through some contortions with
     ":(literal)", it's likely that your original pathspec
     would match whatever the accidentally-expanded wildcard
     would anyway).

     So this takes us one step closer to working correctly
     with files whose names contain wildcard characters, but
     it's likely that others remain (e.g., if "git add -i"
     feeds the selected paths to "git add").

Reported-by: Wincent Colaiuta <win@wincent.com>
Reported-by: Mislav Marohnić <mislav.marohnic@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2 files changed
tree: 9cc9783fd2f080e2fe9f8f171b1d120deda2e47c
  1. block-sha1/
  2. builtin/
  3. ci/
  4. compat/
  5. contrib/
  6. Documentation/
  7. ewah/
  8. git-gui/
  9. gitk-git/
  10. gitweb/
  11. mergetools/
  12. perl/
  13. po/
  14. ppc/
  15. refs/
  16. t/
  17. templates/
  18. vcs-svn/
  19. xdiff/
  20. .gitattributes
  21. .gitignore
  22. .mailmap
  23. .travis.yml
  24. abspath.c
  25. aclocal.m4
  26. advice.c
  27. advice.h
  28. alias.c
  29. alloc.c
  30. apply.c
  31. apply.h
  32. archive-tar.c
  33. archive-zip.c
  34. archive.c
  35. archive.h
  36. argv-array.c
  37. argv-array.h
  38. attr.c
  39. attr.h
  40. base85.c
  41. bisect.c
  42. bisect.h
  43. blob.c
  44. blob.h
  45. branch.c
  46. branch.h
  47. builtin.h
  48. bulk-checkin.c
  49. bulk-checkin.h
  50. bundle.c
  51. bundle.h
  52. cache-tree.c
  53. cache-tree.h
  54. cache.h
  55. check-builtins.sh
  56. check-racy.c
  57. check_bindir
  58. color.c
  59. color.h
  60. column.c
  61. column.h
  62. combine-diff.c
  63. command-list.txt
  64. commit-slab.h
  65. commit.c
  66. commit.h
  67. common-main.c
  68. config.c
  69. config.mak.in
  70. config.mak.uname
  71. configure.ac
  72. connect.c
  73. connect.h
  74. connected.c
  75. connected.h
  76. convert.c
  77. convert.h
  78. copy.c
  79. COPYING
  80. credential-cache--daemon.c
  81. credential-cache.c
  82. credential-store.c
  83. credential.c
  84. credential.h
  85. csum-file.c
  86. csum-file.h
  87. ctype.c
  88. daemon.c
  89. date.c
  90. decorate.c
  91. decorate.h
  92. delta.h
  93. diff-delta.c
  94. diff-lib.c
  95. diff-no-index.c
  96. diff.c
  97. diff.h
  98. diffcore-break.c
  99. diffcore-delta.c
  100. diffcore-order.c
  101. diffcore-pickaxe.c
  102. diffcore-rename.c
  103. diffcore.h
  104. dir-iterator.c
  105. dir-iterator.h
  106. dir.c
  107. dir.h
  108. editor.c
  109. entry.c
  110. environment.c
  111. exec_cmd.c
  112. exec_cmd.h
  113. fast-import.c
  114. fetch-pack.c
  115. fetch-pack.h
  116. fmt-merge-msg.h
  117. fsck.c
  118. fsck.h
  119. generate-cmdlist.sh
  120. gettext.c
  121. gettext.h
  122. git-add--interactive.perl
  123. git-archimport.perl
  124. git-bisect.sh
  125. git-compat-util.h
  126. git-cvsexportcommit.perl
  127. git-cvsimport.perl
  128. git-cvsserver.perl
  129. git-difftool--helper.sh
  130. git-filter-branch.sh
  131. git-instaweb.sh
  132. git-merge-octopus.sh
  133. git-merge-one-file.sh
  134. git-merge-resolve.sh
  135. git-mergetool--lib.sh
  136. git-mergetool.sh
  137. git-p4.py
  138. git-parse-remote.sh
  139. git-quiltimport.sh
  140. git-rebase--am.sh
  141. git-rebase--interactive.sh
  142. git-rebase--merge.sh
  143. git-rebase.sh
  144. git-remote-testgit.sh
  145. git-request-pull.sh
  146. git-send-email.perl
  147. git-sh-i18n.sh
  148. git-sh-setup.sh
  149. git-stash.sh
  150. git-submodule.sh
  151. git-svn.perl
  152. GIT-VERSION-GEN
  153. git-web--browse.sh
  154. git.c
  155. git.rc
  156. gpg-interface.c
  157. gpg-interface.h
  158. graph.c
  159. graph.h
  160. grep.c
  161. grep.h
  162. hashmap.c
  163. hashmap.h
  164. help.c
  165. help.h
  166. hex.c
  167. http-backend.c
  168. http-fetch.c
  169. http-push.c
  170. http-walker.c
  171. http.c
  172. http.h
  173. ident.c
  174. imap-send.c
  175. INSTALL
  176. iterator.h
  177. khash.h
  178. kwset.c
  179. kwset.h
  180. levenshtein.c
  181. levenshtein.h
  182. LGPL-2.1
  183. line-log.c
  184. line-log.h
  185. line-range.c
  186. line-range.h
  187. list-objects.c
  188. list-objects.h
  189. list.h
  190. ll-merge.c
  191. ll-merge.h
  192. lockfile.c
  193. lockfile.h
  194. log-tree.c
  195. log-tree.h
  196. mailinfo.c
  197. mailinfo.h
  198. mailmap.c
  199. mailmap.h
  200. Makefile
  201. match-trees.c
  202. merge-blobs.c
  203. merge-blobs.h
  204. merge-recursive.c
  205. merge-recursive.h
  206. merge.c
  207. mergesort.c
  208. mergesort.h
  209. mru.c
  210. mru.h
  211. name-hash.c
  212. notes-cache.c
  213. notes-cache.h
  214. notes-merge.c
  215. notes-merge.h
  216. notes-utils.c
  217. notes-utils.h
  218. notes.c
  219. notes.h
  220. object.c
  221. object.h
  222. pack-bitmap-write.c
  223. pack-bitmap.c
  224. pack-bitmap.h
  225. pack-check.c
  226. pack-objects.c
  227. pack-objects.h
  228. pack-revindex.c
  229. pack-revindex.h
  230. pack-write.c
  231. pack.h
  232. pager.c
  233. parse-options-cb.c
  234. parse-options.c
  235. parse-options.h
  236. patch-delta.c
  237. patch-ids.c
  238. patch-ids.h
  239. path.c
  240. pathspec.c
  241. pathspec.h
  242. pkt-line.c
  243. pkt-line.h
  244. preload-index.c
  245. pretty.c
  246. prio-queue.c
  247. prio-queue.h
  248. progress.c
  249. progress.h
  250. prompt.c
  251. prompt.h
  252. quote.c
  253. quote.h
  254. reachable.c
  255. reachable.h
  256. read-cache.c
  257. README.md
  258. ref-filter.c
  259. ref-filter.h
  260. reflog-walk.c
  261. reflog-walk.h
  262. refs.c
  263. refs.h
  264. remote-curl.c
  265. remote-testsvn.c
  266. remote.c
  267. remote.h
  268. replace_object.c
  269. rerere.c
  270. rerere.h
  271. resolve-undo.c
  272. resolve-undo.h
  273. revision.c
  274. revision.h
  275. run-command.c
  276. run-command.h
  277. send-pack.c
  278. send-pack.h
  279. sequencer.c
  280. sequencer.h
  281. server-info.c
  282. setup.c
  283. sh-i18n--envsubst.c
  284. sha1-array.c
  285. sha1-array.h
  286. sha1-lookup.c
  287. sha1-lookup.h
  288. sha1_file.c
  289. sha1_name.c
  290. shallow.c
  291. shell.c
  292. shortlog.h
  293. show-index.c
  294. sideband.c
  295. sideband.h
  296. sigchain.c
  297. sigchain.h
  298. split-index.c
  299. split-index.h
  300. strbuf.c
  301. strbuf.h
  302. streaming.c
  303. streaming.h
  304. string-list.c
  305. string-list.h
  306. submodule-config.c
  307. submodule-config.h
  308. submodule.c
  309. submodule.h
  310. symlinks.c
  311. tag.c
  312. tag.h
  313. tar.h
  314. tempfile.c
  315. tempfile.h
  316. thread-utils.c
  317. thread-utils.h
  318. tmp-objdir.c
  319. tmp-objdir.h
  320. trace.c
  321. trace.h
  322. trailer.c
  323. trailer.h
  324. transport-helper.c
  325. transport.c
  326. transport.h
  327. tree-diff.c
  328. tree-walk.c
  329. tree-walk.h
  330. tree.c
  331. tree.h
  332. unicode_width.h
  333. unimplemented.sh
  334. unix-socket.c
  335. unix-socket.h
  336. unpack-trees.c
  337. unpack-trees.h
  338. upload-pack.c
  339. url.c
  340. url.h
  341. urlmatch.c
  342. urlmatch.h
  343. usage.c
  344. userdiff.c
  345. userdiff.h
  346. utf8.c
  347. utf8.h
  348. varint.c
  349. varint.h
  350. version.c
  351. version.h
  352. versioncmp.c
  353. walker.c
  354. walker.h
  355. wildmatch.c
  356. wildmatch.h
  357. worktree.c
  358. worktree.h
  359. wrap-for-bin.sh
  360. wrapper.c
  361. write_or_die.c
  362. ws.c
  363. wt-status.c
  364. wt-status.h
  365. xdiff-interface.c
  366. xdiff-interface.h
  367. zlib.c
README.md

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from http://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission). To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org. The mailing list archives are available at https://public-inbox.org/git, http://marc.info/?l=git and other archival sites.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks