git/builtin
Jeff King f93c1d86cc revision: add wrapper to setup_revisions() from a strvec
The setup_revisions() function was designed to take the argc/argv pair
from the operating system. But we sometimes construct our own argv using
a strvec and pass that in. There are a few gotchas that callers need to
deal with here:

  1. You should always pass the free_removed_argv_elements option via
     setup_revision_opt. Otherwise, entries may be leaked if
     setup_revisions() re-shuffles options.

  2. After setup_revisions() returns, the strvec state is odd. We get a
     reduced argc from setup_revisions() telling us how many unknown
     options were left in place. Entries after that in argv may be
     retained, or may be NULL (depending on how the reshuffling
     happened). But the strvec's "nr" field still represents the
     original value, and some of the entries it thinks it is still
     storing may be NULL. Callers must be careful with how they access
     it.

Some callers deal with (1), but not all. In practice they are OK because
they do not pass any options that would cause setup_revisions() to
re-shuffle (namely unknown options which may be relayed from the user,
and the use of the "--" separator). But it's probably a good idea to
consistently pass this option anyway to future-proof ourselves against
the details of setup_revisions() changing.

No callers address (2), though I don't think there any visible bugs.
Most of them simply call strvec_clear() and never otherwise look at the
result. And in fact, if they naively set foo.nr to the argc returned by
setup_revisions(), that would cause leaks!  Because setup_revisions()
does not free consumed options[1], we have to leave the "nr" field of
the strvec at its original value to find and free them during
strvec_clear().

So I don't think there are any bugs to fix here, but we can make things
safer and simpler for callers. Let's introduce a helper function that
sets the free_removed_argv_elements automatically and shrinks the strvec
to represent the retained options afterwards (taking care to free the
now-obsolete entries).

We'll start by converting all of the call-sites which use the
free_removed_argv_elements option. There should be no behavior change
for them, except that their "shrunken" entries are cleaned up
immediately, rather than waiting for a strvec_clear() call.

[1] Arguably setup_revisions() should be doing this step for us if we
    told it to free removed options, but there are many existing callers
    which will be broken if it did. Introducing this helper is a
    possible first step towards that.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-09-22 14:27:03 -07:00
..
add.c Merge branch 'lm/add-p-context' 2025-08-04 08:10:33 -07:00
am.c config: drop `git_config_get_bool()` wrapper 2025-07-23 08:15:20 -07:00
annotate.c Merge branch 'jc/a-commands-without-the-repo' 2024-10-25 14:02:36 -04:00
apply.c builtin: use default hash when outside a repository 2025-07-01 14:58:24 -07:00
archive.c archive: remove the_repository global variable 2024-10-11 09:37:18 -07:00
backfill.c odb: rename `has_object()` 2025-07-01 14:46:38 -07:00
bisect.c global: mark code units that generate warnings with `-Wsign-compare` 2024-12-06 20:20:02 +09:00
blame.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
branch.c config: drop `git_config_set_multivar()` wrapper 2025-07-23 08:15:21 -07:00
bugreport.c object-file: move `safe_create_leading_directories()` into "path.c" 2025-04-15 08:24:35 -07:00
bundle.c Merge branch 'jt/bundle-fsck' 2024-12-13 07:33:36 -08:00
cat-file.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
check-attr.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
check-ignore.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
check-mailmap.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
check-ref-format.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
checkout--worker.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
checkout-index.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
checkout.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
clean.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
clone.c config: drop `git_config_set_multivar()` wrapper 2025-07-23 08:15:21 -07:00
column.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
commit-graph.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
commit-tree.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
commit.c Merge branch 'lm/add-p-context' 2025-08-04 08:10:33 -07:00
config.c config: drop `git_config_set_multivar_in_file_gently()` wrapper 2025-07-23 08:15:21 -07:00
count-objects.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
credential-cache--daemon.c config: drop `git_config_get_bool()` wrapper 2025-07-23 08:15:20 -07:00
credential-cache.c Merge branch 'rj/cygwin-exit' 2024-11-01 12:53:19 -04:00
credential-store.c config: drop `git_config_get_int()` wrapper 2025-07-23 08:15:20 -07:00
credential.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
describe.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
diagnose.c object-file: move `safe_create_leading_directories()` into "path.c" 2025-04-15 08:24:35 -07:00
diff-files.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
diff-index.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
diff-pairs.c builtin/diff-pairs: allow explicit diff queue flush 2025-03-03 08:17:47 -08:00
diff-tree.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
diff.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
difftool.c odb: rename `repo_read_object_file()` 2025-07-01 14:46:38 -07:00
fast-export.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
fast-import.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
fetch-pack.c builtin/fetch-pack: cleanup before return error 2025-06-04 08:52:25 -07:00
fetch.c config: drop `git_config_get_int()` wrapper 2025-07-23 08:15:20 -07:00
fmt-merge-msg.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
for-each-ref.c Merge branch 'rs/for-each-ref-start-after-marker-fix' 2025-08-11 21:30:15 -07:00
for-each-repo.c global: trivial conversions to fix `-Wsign-compare` warnings 2024-12-06 20:20:04 +09:00
fsck.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
fsmonitor--daemon.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
gc.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
get-tar-commit-id.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
grep.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
hash-object.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
help.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
hook.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
index-pack.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
init-db.c Merge branch 'ps/parse-options-integers' 2025-04-24 17:25:34 -07:00
interpret-trailers.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
log.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
ls-files.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
ls-remote.c builtin: use default hash when outside a repository 2025-07-01 14:58:24 -07:00
ls-tree.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
mailinfo.c mailinfo: stop using `the_repository` 2024-12-18 10:44:31 -08:00
mailsplit.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
merge-base.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
merge-file.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
merge-index.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
merge-ours.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
merge-recursive.c builtin/merge-recursive: switch to using merge_ort_generic() 2025-04-08 13:59:11 -07:00
merge-tree.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
merge.c Merge branch 'rs/merge-compact-summary' 2025-08-11 21:30:16 -07:00
mktag.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
mktree.c odb: introduce `odb_write_object()` 2025-07-16 22:16:15 -07:00
multi-pack-index.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
mv.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
name-rev.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
notes.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
pack-objects.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
pack-redundant.c Merge branch 'ps/object-store' 2025-07-15 15:18:18 -07:00
pack-refs.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
patch-id.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
prune-packed.c
prune.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
pull.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
push.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
range-diff.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
read-tree.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
rebase.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
receive-pack.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
reflog.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
refs.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
remote-ext.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
remote-fd.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
remote.c Merge branch 'dl/squelch-maybe-uninitialized' 2025-08-07 08:14:38 -07:00
repack.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
replace.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
replay.c repo_logmsg_reencode: fix memory leak when use repo_logmsg_reencode () 2025-06-05 08:35:22 -07:00
rerere.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
reset.c Merge branch 'lm/add-p-context' 2025-08-04 08:10:33 -07:00
rev-list.c Merge branch 'ps/config-wo-the-repository' 2025-08-04 08:10:33 -07:00
rev-parse.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
revert.c Merge branch 'jk/revert-squelch-compiler-warning' 2025-08-07 08:14:37 -07:00
rm.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
send-pack.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
shortlog.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
show-branch.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
show-index.c builtin: use default hash when outside a repository 2025-07-01 14:58:24 -07:00
show-ref.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
sparse-checkout.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
stash.c revision: add wrapper to setup_revisions() from a strvec 2025-09-22 14:27:03 -07:00
stripspace.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
submodule--helper.c revision: add wrapper to setup_revisions() from a strvec 2025-09-22 14:27:03 -07:00
symbolic-ref.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
tag.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
unpack-file.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
unpack-objects.c Merge branch 'ps/object-file-wo-the-repository' 2025-08-05 11:53:55 -07:00
update-index.c config: drop `git_config()` wrapper 2025-07-23 08:15:18 -07:00
update-ref.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
update-server-info.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
upload-archive.c builtin: send usage() help text to standard output 2025-01-17 13:30:03 -08:00
upload-pack.c serve: stop using `the_repository` 2024-12-18 10:44:30 -08:00
var.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
verify-commit.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
verify-pack.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
verify-tag.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00
worktree.c config: drop `git_config_set_multivar_in_file_gently()` wrapper 2025-07-23 08:15:21 -07:00
write-tree.c config: move Git config parsing into "environment.c" 2025-07-23 08:15:22 -07:00