builtin-add.c: restructure the code for maintainability
The implementation of "git add" has four major codepaths that are mutually
- if "--interactive" or "--patch" is given, spawn "git add--interactive"
and exit without doing anything else. Otherwise things are handled
internally in this C code;
- if "--update" is given, update the modified files and exit without
doing anything else;
- if "--refresh" is given, do refresh and exit without doing anything
- otherwise, find the paths that match pathspecs and stage their
It led to an unholy mess in the code structure; each of the latter three
codepaths has a separate call to read_cache(), even though they are all
about "read the current index, update it and write it back", and logically
they should read the index once _anyway_.
This cleans up the latter three cases by introducing a pair of helper
- "add_new_files" is set if we need to scan the working tree for paths
that match the pathspec. This variable is false for "--update" and
"--refresh", because they only work on already tracked files.
- "require_pathspec" is set if the user must give at least one pathspec.
"--update" does not need it but all the other cases do.
This is in preparation for introducing a new option "--all", that does the
equivalent of "git add -u && git add ." (aka "addremove").
Signed-off-by: Junio C Hamano <email@example.com>