stop using the_repository global variable.
cover stop using the_repository global variable.
Remove the_repository global variable in favor of the repository argument that gets passed in builtin commands.
These sets of commands are commands that use only RUN_SETUP macro in "git.c". Basically, When -h is passed to any of this command outside a Git repository, the run_builtin() will call the cmd_x() function (where x is any of the command from the sets of builtin commands that the_repository is removed from) with repo set to NULL and then early in the function, parse_options() or show_usage_with_options_if_asked() call will give the options help and exit, without having to consult much of the configuration file.
As there exist some builtin commands where the repository variable is accessed before options is given exit and fail, we should check if the repository variable is not NULL in such scenerio.
Some, functions also uses the_repository global internally, so, let's let's refactor them and pass struct repo as one of the argument.
I picked some of this files based on the above explanation, how easy they are to resolve and how easy easy to review.
[1]: https://public-inbox.org/git/[email protected]/
*** BLURB HERE ***
Usman Akinyemi (7):
builtin/verify-tag: stop using the_repository builtin/verify-commit.c: stop using the_repository builtin/send-pack.c: stop using the_repository builtin/pack-refs: stop using the_repository builtin/ls-files: stop using the_repository builtin/for-each-ref: stop using the_repository builtin/checkout-index.c: stop using the_repository
builtin/checkout-index.c | 43 ++++++++++++++++++++-------------------- builtin/for-each-ref.c | 6 +++--- builtin/ls-files.c | 32 +++++++++++++++--------------- builtin/pack-refs.c | 9 ++++----- builtin/send-pack.c | 8 ++++---- builtin/verify-commit.c | 14 ++++++------- builtin/verify-tag.c | 8 ++++---- 7 files changed, 59 insertions(+), 61 deletions(-)
Signature · 1 line
2.48.1
PATCH 1/7 builtin/verify-tag: stop using `the_repository`
Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_tag() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file. So it is safe to omit reading the config when repo argument the caller gave us is NULL.
--- builtin/verify-tag.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
builtin/verify-tag.c +4 -4
@@ -5,7 +5,6 @@* * Based on git-verify-tag.sh */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -23,7 +22,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -35,7 +34,8 @@ int cmd_verify_tag(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_tag_options, verify_tag_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -56,7 +56,7 @@ int cmd_verify_tag(int argc,struct object_id oid;const char *name = argv[i++]; -if (repo_get_oid (the_repository, name, &oid)) { +if (repo_get_oid (repo, name, &oid)) { had_error = !!error ("tag '%s' not found." , name);continue ; }
Signature · 1 line
2.48.1
Re: [PATCH 1/7] builtin/verify-tag: stop using `the_repository`
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 39
Quoted 8 lines · Usman Akinyemi
builtin/verify-tag.c +2
-1@@ -35,7 +34,8 @@ int cmd_verify_tag(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );
I recently noticed that we have usage_with_options_if_asked(). Should we use that function rather than making the call to git_config() conditional? Otherwise it's not obvious why we have the conditional in the first place.
The same comment also applies to subsequent commits.
Patrick
Re: [PATCH 1/7] builtin/verify-tag: stop using `the_repository`
Quoted 6 lines · Patrick Steinhardt
Quoted 4 lines · Usman Akinyemi
builtin/verify-tag.c +0
-0@@ -35,7 +34,8 @@ int cmd_verify_tag(int argc, OPT_END() };
Quoted 9 lines
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 39
Quoted 4 lines
builtin/verify-tag.c +2
-1-git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );I recently noticed that we have
usage_with_options_if_asked(). Should we use that function rather than making the call togit_config()conditional? Otherwise it's not obvious why we have the conditional in the first place.
Hi Patrick,
I think the function is show_usage_with_options_if_asked(). The function is quite different from git_config() or the repo_config(). The config function
consults the configuration file for setting up config values and it uses the repo
variable during this. While show_usage_with_options_if_asked() is used when the "-h" option is passed to the builtin functions to display the help string.
In a case when "-h" is passed to the builtin functions which use the RUN_SETUP macro,
the repo config will be NULL.
There are some builtin commands functions that which has thegit_config() function
comes before show_usage_with_options_if_asked() or it's variant and some, git_config()
comes after.
For those that have git_config() comes after show_usage_with_options_if_asked() , no need for the check, since the
show_usage_with_options_if_asked()call will exit without reachinggit_config(). For scenario where the git_config()
comes earlier, we have to check the repo to see if it is NULL, if it is NULL, we are sure this happens when the "-h" is
passed to the function and we do not need to setup and configuration since show_usage_with_options_if_asked()
will exit.
So, the condition is necessary else, NULL value will be passed to the git_config() which will lead to accessing NULL
value.
Thank you.
Quoted 4 lines
The same comment also applies to subsequent commits.
Patrick
Re: [PATCH 1/7] builtin/verify-tag: stop using `the_repository`
Quoted 33 lines · Usman Akinyemi
Quoted 6 lines · Patrick Steinhardt
Quoted 4 lines · Usman Akinyemi
builtin/verify-tag.c +0
-0@@ -35,7 +34,8 @@ int cmd_verify_tag(int argc, OPT_END() };Quoted 9 lines
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 39
Quoted 4 lines
builtin/verify-tag.c +2
-1-git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );I recently noticed that we have
usage_with_options_if_asked(). Should we use that function rather than making the call togit_config()conditional? Otherwise it's not obvious why we have the conditional in the first place.Hi Patrick,
I think the function is
show_usage_with_options_if_asked(). The function is quite different fromgit_config()or therepo_config(). The config function consults the configuration file for setting up config values and it uses therepovariable during this. Whileshow_usage_with_options_if_asked()is used when the "-h" option is passed to the builtin functions to display the help string.In a case when "-h" is passed to the builtin functions which use the
RUN_SETUPmacro, therepoconfig will be NULL.There are some builtin commands functions that which has the
git_config()function comes beforeshow_usage_with_options_if_asked()or it's variant and some,git_config()comes after.
Quoted 8 lines
For those that have
git_config()comes aftershow_usage_with_options_if_asked(), no need for the check, since theshow_usage_with_options_if_asked()call will exit without reachinggit_config(). For scenario where thegit_config()comes earlier, we have to check therepoto see if it is NULL, if it is NULL, we are sure this happens when the "-h" is passed to the function and we do not need to setup and configuration sinceshow_usage_with_options_if_asked()will exit.
Exactly, this is what my suggestion is. If we introduced new calls to show_usage_with_options_if_asked() before git_config() we wouldn't have to check for a NULL repository in the first place because we know that we'd have already exited if there was a "-h" parameter.
Patrick
Re: [PATCH 1/7] builtin/verify-tag: stop using `the_repository`
Quoted 44 lines · Patrick Steinhardt
Quoted 33 lines · Usman Akinyemi
Quoted 6 lines · Patrick Steinhardt
Quoted 4 lines · Usman Akinyemi
builtin/verify-tag.c +0
-0@@ -35,7 +34,8 @@ int cmd_verify_tag(int argc, OPT_END() };Quoted 9 lines
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 39
Quoted 4 lines
builtin/verify-tag.c +2
-1-git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );I recently noticed that we have
usage_with_options_if_asked(). Should we use that function rather than making the call togit_config()conditional? Otherwise it's not obvious why we have the conditional in the first place.Hi Patrick,
I think the function is
show_usage_with_options_if_asked(). The function is quite different fromgit_config()or therepo_config(). The config function consults the configuration file for setting up config values and it uses therepovariable during this. Whileshow_usage_with_options_if_asked()is used when the "-h" option is passed to the builtin functions to display the help string.In a case when "-h" is passed to the builtin functions which use the
RUN_SETUPmacro, therepoconfig will be NULL.There are some builtin commands functions that which has the
git_config()function comes beforeshow_usage_with_options_if_asked()or it's variant and some,git_config()comes after.Quoted 8 lines
For those that have
git_config()comes aftershow_usage_with_options_if_asked(), no need for the check, since theshow_usage_with_options_if_asked()call will exit without reachinggit_config(). For scenario where thegit_config()comes earlier, we have to check therepoto see if it is NULL, if it is NULL, we are sure this happens when the "-h" is passed to the function and we do not need to setup and configuration sinceshow_usage_with_options_if_asked()will exit.
Quoted 4 lines
Exactly, this is what my suggestion is. If we introduced new calls to
show_usage_with_options_if_asked()beforegit_config()we wouldn't have to check for aNULLrepository in the first place because we know that we'd have already exited if there was a "-h" parameter.
Yeah, that is true. Maybe having this as a preparatory patch could be better.
There was a previous similar patch also which has been accepted. Maybe this can be done after this patch series got accepted, so, I could do it together
with the already accepted patch.
What do you think ?
Thank you.
Quoted 2 lines
Patrick
Re: [PATCH 1/7] builtin/verify-tag: stop using `the_repository`
Quoted 49 lines · Usman Akinyemi
Quoted 44 lines · Patrick Steinhardt
Quoted 33 lines · Usman Akinyemi
Quoted 6 lines · Patrick Steinhardt
Quoted 4 lines · Usman Akinyemi
builtin/verify-tag.c +0
-0@@ -35,7 +34,8 @@ int cmd_verify_tag(int argc, OPT_END() };Quoted 9 lines
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 39
Quoted 4 lines
builtin/verify-tag.c +2
-1-git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );I recently noticed that we have
usage_with_options_if_asked(). Should we use that function rather than making the call togit_config()conditional? Otherwise it's not obvious why we have the conditional in the first place.Hi Patrick,
I think the function is
show_usage_with_options_if_asked(). The function is quite different fromgit_config()or therepo_config(). The config function consults the configuration file for setting up config values and it uses therepovariable during this. Whileshow_usage_with_options_if_asked()is used when the "-h" option is passed to the builtin functions to display the help string.In a case when "-h" is passed to the builtin functions which use the
RUN_SETUPmacro, therepoconfig will be NULL.There are some builtin commands functions that which has the
git_config()function comes beforeshow_usage_with_options_if_asked()or it's variant and some,git_config()comes after.Quoted 8 lines
For those that have
git_config()comes aftershow_usage_with_options_if_asked(), no need for the check, since theshow_usage_with_options_if_asked()call will exit without reachinggit_config(). For scenario where thegit_config()comes earlier, we have to check therepoto see if it is NULL, if it is NULL, we are sure this happens when the "-h" is passed to the function and we do not need to setup and configuration sinceshow_usage_with_options_if_asked()will exit.Quoted 4 lines
Exactly, this is what my suggestion is. If we introduced new calls to
show_usage_with_options_if_asked()beforegit_config()we wouldn't have to check for aNULLrepository in the first place because we know that we'd have already exited if there was a "-h" parameter.
Quoted 6 lines
Yeah, that is true. Maybe having this as a preparatory patch could be better.
There was a previous similar patch also which has been accepted. Maybe this can be done after this patch series got accepted, so, I could do it together
with the already accepted patch.
Yup, that'd be great indeed. Thanks!
Patrick
PATCH 2/7 builtin/verify-commit.c: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_commit() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file. So it is safe to omit reading the config when repo argument the caller gave us is NULL.
Let's pass repository argument to verify_commit() function to remove it's dependency on the global the_repository variable.
--- builtin/verify-commit.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
builtin/verify-commit.c +7 -7
@@ -5,7 +5,6 @@* * Based on git-verify-tag */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)return ret; } -static int verify_commit (const char *name,unsigned flags) +static int verify_commit (struct repository *repo,const char *name,unsigned flags) {struct object_id oid;struct object *obj; -if (repo_get_oid (the_repository, name, &oid)) +if (repo_get_oid (repo, name, &oid))return error ("commit '%s' not found." , name); - obj =parse_object (the_repository, &oid); + obj =parse_object (repo, &oid);if (!obj)return error ("%s: unable to read file." , name);if (obj->type !=OBJ_COMMIT )
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)int cmd_verify_commit (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -64,7 +63,8 @@ int cmd_verify_commit(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_commit_options, verify_commit_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -78,7 +78,7 @@ int cmd_verify_commit(int argc,* was received in the process of writing the gpg input: */ signal (SIGPIPE ,SIG_IGN );while (i < argc) -if (verify_commit (argv[i++], flags)) +if (verify_commit (repo, argv[i++], flags)) had_error =1 ;return had_error; }
Signature · 1 line
2.48.1
Re: [PATCH 2/7] builtin/verify-commit.c: stop using `the_repository`
Quoted 3 lines · Usman Akinyemi
Remove
the_repositoryglobal variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".
I think this is a typo. "builtin/verify-tag.c" should be "builtin/verify-commit.c".
Re: [PATCH 2/7] builtin/verify-commit.c: stop using `the_repository`
Quoted 8 lines · shejialuo
Quoted 3 lines · Usman Akinyemi
Remove
the_repositoryglobal variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".I think this is a typo. "
builtin/verify-tag.c" should be "builtin/verify-commit.c".
Ohh, yeah, I will fix it in the next version. Thanks.
PATCH 3/7 builtin/send-pack.c: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/send-pack.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_send_pack() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file. So it is safe to omit reading the config when repo argument the caller gave us is NULL.
--- builtin/send-pack.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
builtin/send-pack.c +4 -4
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "hex.h"
@@ -151,7 +150,7 @@ static int send_pack_config(const char *k, const char *v,int cmd_send_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct refspec rs =REFSPEC_INIT_PUSH ;const char *remote_name =NULL ;
@@ -212,7 +211,8 @@ int cmd_send_pack(int argc, OPT_END() }; -git_config (send_pack_config,NULL ); +if (repo) +repo_config (repo, send_pack_config,NULL ); argc =parse_options (argc, argv, prefix, options, send_pack_usage,0 );if (argc >0 ) { dest = argv[0 ];
@@ -317,7 +317,7 @@ int cmd_send_pack(int argc,set_ref_status_for_push (remote_refs, args.send_mirror, args.force_update); - ret =send_pack (the_repository, &args, fd, conn, remote_refs, &extra_have); + ret =send_pack (repo, &args, fd, conn, remote_refs, &extra_have);if (helper_status)print_helper_status (remote_refs);
Signature · 1 line
2.48.1
PATCH 4/7 builtin/pack-refs: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/pack-refs.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_pack_refs() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file. So it is safe to omit reading the config when repo argument the caller gave us is NULL.
--- builtin/pack-refs.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
builtin/pack-refs.c +4 -5
@@ -1,5 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE -#include "builtin.h" #include "config.h" #include "gettext.h"
@@ -15,7 +13,7 @@ static char const * const pack_refs_usage[] = {int cmd_pack_refs (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_exclusions excludes =REF_EXCLUSIONS_INIT ;struct string_list included_refs =STRING_LIST_INIT_NODUP ;
@@ -39,7 +37,8 @@ int cmd_pack_refs(int argc, N_("references to exclude")), OPT_END(), }; -git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );if (parse_options (argc, argv, prefix, opts, pack_refs_usage,0 ))usage_with_options (pack_refs_usage, opts);
@@ -52,7 +51,7 @@ int cmd_pack_refs(int argc,if (!pack_refs_opts.includes->nr)string_list_append (pack_refs_opts.includes,"refs/tags/*" ); - ret =refs_pack_refs (get_main_ref_store (the_repository), &pack_refs_opts); + ret =refs_pack_refs (get_main_ref_store (repo), &pack_refs_opts);clear_ref_exclusions (&excludes);string_list_clear (&included_refs,0 );
Signature · 1 line
2.48.1
PATCH 5/7 builtin/ls-files: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/ls-files.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_ls_files() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit, without having to consult much of the configuration file.
Let's pass repository argument to expand_objectsize(), show_ru_info() functions to remove their dependency on the global the_repository variable.
--- builtin/ls-files.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
builtin/ls-files.c +16 -16
@@ -6,7 +6,6 @@* Copyright (C) Linus Torvalds, 2005 */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,repo_clear (&subrepo); } -static void expand_objectsize (struct strbuf *line,const struct object_id *oid, +static void expand_objectsize (struct repository *repo,struct strbuf *line, +const struct object_id *oid,const enum object_type type,unsigned int padded) {if (type ==OBJ_BLOB ) {unsigned long size; -if (oid_object_info (the_repository, oid, &size) <0 ) +if (oid_object_info (repo, oid, &size) <0 )die (_ ("could not get object info about '%s'" ),oid_to_hex (oid));if (padded)
@@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,else if (skip_prefix (format,"(objecttype)" , &format))strbuf_addstr (&sb,type_name (object_type (ce->ce_mode)));else if (skip_prefix (format,"(objectsize:padded)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),1 );else if (skip_prefix (format,"(objectsize)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),0 );else if (skip_prefix (format,"(stage)" , &format))strbuf_addf (&sb,"%d" ,ce_stage (ce));
@@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir, } } -static void show_ru_info (struct index_state *istate) +static void show_ru_info (struct repository *repo,struct index_state *istate) {struct string_list_item *item;
@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)if (!ui->mode[i])continue ;printf ("%s%06o %s %d\t" , tag_resolve_undo, ui->mode[i], -repo_find_unique_abbrev (the_repository, &ui->oid[i], abbrev), +repo_find_unique_abbrev (repo, &ui->oid[i], abbrev), i +1 );write_name (path); }
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,int cmd_ls_files (int argc,const char **argv,const char *cmd_prefix, - struct repository *repo UNUSED) +struct repository *repo) {int require_work_tree =0 , show_tag =0 , i;char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,show_usage_with_options_if_asked (argc, argv, ls_files_usage, builtin_ls_files_options); -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; prefix = cmd_prefix;if (prefix) prefix_len =strlen (prefix); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); -if (repo_read_index (the_repository) <0 ) +if (repo_read_index (repo) <0 )die ("index file corrupt" ); argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -724,7 +724,7 @@ int cmd_ls_files(int argc, max_prefix =common_prefix (&pathspec); max_prefix_len =get_common_prefix_len (max_prefix); -prune_index (the_repository->index, max_prefix, max_prefix_len); +prune_index (repo->index, max_prefix, max_prefix_len);/* Treat unmatching pathspec elements as errors */ if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,*/ if (show_stage || show_unmerged)die (_ ("options '%s' and '%s' cannot be used together" ),"ls-files --with-tree" ,"-s/-u" ); -overlay_tree_on_index (the_repository->index, with_tree, max_prefix); +overlay_tree_on_index (repo->index, with_tree, max_prefix); } -show_files (the_repository, &dir); +show_files (repo, &dir);if (show_resolve_undo) -show_ru_info (the_repository->index); +show_ru_info (repo, repo->index);if (ps_matched &&report_path_error (ps_matched, &pathspec)) {fprintf (stderr,"Did you forget to 'git add'?\n" );
Signature · 1 line
2.48.1
Re: [PATCH 5/7] builtin/ls-files: stop using `the_repository`
Quoted 9 lines · Usman Akinyemi
Remove
the_repositoryglobal variable in favor of the repository argument that gets passed in "builtin/ls-files.c".When
-his passed to the command outside a Git repository, therun_builtin()will call thecmd_ls_files()function withreposet to NULL and then early in the function,show_usage_with_options_if_asked()call will give the options help and exit, without having to consult much of the configuration file.
Quoted 3 lines
Let's pass
repositoryargument toexpand_objectsize(),show_ru_info()functions to remove their dependency on the globalthe_repositoryvariable.
This paragraph made my reading hickup a bit. How about:
Pass the repository available in the calling context to both expand_objectsize() and show_ru_info() to remove their dependency on the global the_repository variable.
Patrick
Re: [PATCH 5/7] builtin/ls-files: stop using `the_repository`
Quoted 15 lines · Patrick Steinhardt
Quoted 9 lines · Usman Akinyemi
Remove
the_repositoryglobal variable in favor of the repository argument that gets passed in "builtin/ls-files.c".When
-his passed to the command outside a Git repository, therun_builtin()will call thecmd_ls_files()function withreposet to NULL and then early in the function,show_usage_with_options_if_asked()call will give the options help and exit, without having to consult much of the configuration file.Quoted 3 lines
Let's pass
repositoryargument toexpand_objectsize(),show_ru_info()functions to remove their dependency on the globalthe_repositoryvariable.
Quoted 5 lines
This paragraph made my reading hickup a bit. How about:
Pass the repository available in the calling context to both
expand_objectsize()andshow_ru_info()to remove their dependency on the globalthe_repositoryvariable.
Thanks for the suggestion, I will use it in the next version.
Quoted 2 lines
Patrick
PATCH 6/7 builtin/for-each-ref: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/for-each-ref.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_for_each_ref() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file. So it is safe to omit reading the config when repo argument the caller gave us is NULL.
--- builtin/for-each-ref.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
builtin/for-each-ref.c +3 -3
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "commit.h" #include "config.h"
@@ -20,7 +19,7 @@ static char const * const for_each_ref_usage[] = {int cmd_for_each_ref (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_sorting *sorting;struct string_list sorting_options =STRING_LIST_INIT_DUP ;
@@ -63,7 +62,8 @@ int cmd_for_each_ref(int argc, format.format ="%(objectname) %(objecttype)\t%(refname)" ; -git_config (git_default_config,NULL ); +if (repo) +repo_config (repo, git_default_config,NULL );/* Set default (refname) sorting */ string_list_append (&sorting_options,"refname" );
Signature · 1 line
2.48.1
PATCH 7/7 builtin/checkout-index.c: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_checkout_index() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit, without having to consult much of the configuration file.
Let's pass repository argument to checkout_all() and checkout_file() functions to remove their dependency on the global the_repository variable.
--- builtin/checkout-index.c | 43 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 22 deletions(-)
builtin/checkout-index.c +21 -22
@@ -5,7 +5,6 @@* */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -68,10 +67,10 @@ static void write_tempfile_record(const char *name, const char *prefix) } } -static int checkout_file (const char *name,const char *prefix) +static int checkout_file (struct repository *repo,const char *name,const char *prefix) {int namelen =strlen (name); -int pos =index_name_pos (the_repository->index, name, namelen); +int pos =index_name_pos (repo->index, name, namelen);int has_same_name =0 ;int is_file =0 ;int is_skipped =1 ;
@@ -81,8 +80,8 @@ static int checkout_file(const char *name, const char *prefix)if (pos <0 ) pos = -pos -1 ; -while (pos <the_repository->index->cache_nr) { -struct cache_entry *ce =the_repository->index->cache[pos]; +while (pos < repo->index->cache_nr) { +struct cache_entry *ce =repo->index->cache[pos];if (ce_namelen (ce) != namelen ||memcmp (ce->name, name, namelen))break ;
@@ -137,13 +136,13 @@ static int checkout_file(const char *name, const char *prefix)return -1 ; } -static int checkout_all (const char *prefix,int prefix_length) +static int checkout_all (struct repository *repo,const char *prefix,int prefix_length) {int i, errs =0 ;struct cache_entry *last_ce =NULL ; -for (i =0 ; i < the_repository->index->cache_nr ; i++) { -struct cache_entry *ce = the_repository->index->cache[i]; +for (i =0 ; i < repo->index->cache_nr ; i++) { +struct cache_entry *ce = repo->index->cache[i];if (S_ISSPARSEDIR (ce->ce_mode)) {if (!ce_skip_worktree (ce))
@@ -156,8 +155,8 @@ static int checkout_all(const char *prefix, int prefix_length)* first entry inside the expanded sparse directory). */ if (ignore_skip_worktree) { -ensure_full_index (the_repository->index); - ce = the_repository->index->cache[i]; +ensure_full_index (repo->index); + ce = repo->index->cache[i]; } }
@@ -213,7 +212,7 @@ static int option_parse_stage(const struct option *opt,int cmd_checkout_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i;struct lock_file lock_file =LOCK_INIT ;
@@ -253,19 +252,19 @@ int cmd_checkout_index(int argc,show_usage_with_options_if_asked (argc, argv, builtin_checkout_index_usage, builtin_checkout_index_options); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); prefix_length = prefix ?strlen (prefix) :0 ; -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; -if (repo_read_index (the_repository) <0 ) { +if (repo_read_index (repo) <0 ) {die ("invalid cache" ); } argc =parse_options (argc, argv, prefix, builtin_checkout_index_options, builtin_checkout_index_usage,0 ); - state.istate = the_repository->index; + state.istate = repo->index; state.force = force; state.quiet = quiet; state.not_new = not_new;
@@ -285,8 +284,8 @@ int cmd_checkout_index(int argc,*/ if (index_opt && !state.base_dir_len && !to_tempfile) { state.refresh_cache =1 ; - state.istate = the_repository->index; -repo_hold_locked_index (the_repository, &lock_file, + state.istate = repo->index; +repo_hold_locked_index (repo, &lock_file,LOCK_DIE_ON_ERROR ); }
@@ -304,7 +303,7 @@ int cmd_checkout_index(int argc,if (read_from_stdin)die ("git checkout-index: don't mix '--stdin' and explicit filenames" ); p =prefix_path (prefix, prefix_length, arg); - err |=checkout_file (p, prefix); + err |=checkout_file (repo, p, prefix);free (p); }
@@ -326,7 +325,7 @@ int cmd_checkout_index(int argc,strbuf_swap (&buf, &unquoted); } p =prefix_path (prefix, prefix_length, buf.buf); - err |=checkout_file (p, prefix); + err |=checkout_file (repo, p, prefix);free (p); }strbuf_release (&unquoted);
@@ -334,7 +333,7 @@ int cmd_checkout_index(int argc, }if (all) - err |=checkout_all (prefix, prefix_length); + err |=checkout_all (repo, prefix, prefix_length);if (pc_workers >1 ) err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +343,7 @@ int cmd_checkout_index(int argc,return 1 ;if (is_lock_file_locked (&lock_file) && -write_locked_index (the_repository->index, &lock_file,COMMIT_LOCK )) +write_locked_index (repo->index, &lock_file,COMMIT_LOCK ))die ("Unable to write new index file" );return 0 ; }
Signature · 1 line
2.48.1
On Sat, Feb 15, 2025 at 04:27:16AM +0530, Usman Akinyemi wrote:
[snip]
Quoted 9 lines
Usman Akinyemi (7):
builtin/verify-tag: stop usingthe_repositorybuiltin/verify-commit.c: stop usingthe_repositorybuiltin/send-pack.c: stop usingthe_repositorybuiltin/pack-refs: stop usingthe_repositorybuiltin/ls-files: stop usingthe_repositorybuiltin/for-each-ref: stop usingthe_repositorybuiltin/checkout-index.c: stop usingthe_repository
The commit message is not consistent. We should remove ".c".
Quoted 15 lines · shejialuo
On Sat, Feb 15, 2025 at 04:27:16AM +0530, Usman Akinyemi wrote:
[snip]
Quoted 9 lines
Usman Akinyemi (7):
builtin/verify-tag: stop usingthe_repositorybuiltin/verify-commit.c: stop usingthe_repositorybuiltin/send-pack.c: stop usingthe_repositorybuiltin/pack-refs: stop usingthe_repositorybuiltin/ls-files: stop usingthe_repositorybuiltin/for-each-ref: stop usingthe_repositorybuiltin/checkout-index.c: stop usingthe_repository
Quoted 1 line
The commit message is not consistent. We should remove ".c".
Yeah, I will fix it in the next version.
v2 cover stop using the_repository global variable.
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in builtin commands.
These sets of commands are commands that use only RUN_SETUP macro in "git.c". Basically, When -h is passed to any of this command outside a Git repository, the run_builtin() will call the cmd_x() function (where x is any of the command from the sets of builtin commands that the_repository is removed from) with repo set to NULL and then early in the function, parse_options() or show_usage_with_options_if_asked() call will give the options help and exit, without having to consult much of the configuration file.
As there exist some builtin commands where the repository variable is accessed before options is given exit and fail, we will move the functions which accessed the repository below the usage_with_options() and it variants which will exit before getting to the function accessing the repository variable. We will do this in a preparatory patches.
Some, functions also uses the_repository global internally, so, let's let's refactor them and pass struct repo as one of the argument.
I picked some of this files based on the above explanation, how easy they are to resolve and how easy easy to review.
[1]: https://public-inbox.org/git/[email protected]/
Changes since v1
================
- Add some new preparatory patches to move the git_config() below the usage_with_options().
- Fix some errors in the commit messages.
Usman Akinyemi (12):
builtin/verify-tag: refactor cmd_verify_tag() builtin/verify-tag: stop using the_repository builtin/verify-commit: refactor cmd_verify_commit() builtin/verify-commit: stop using the_repository builtin/send-pack: refactor cmd_send_pack() builtin/send-pack: stop using the_repository builtin/pack-refs: refactor cmd_pack_refs() builtin/pack-refs: stop using the_repository builtin/ls-files: stop using the_repository builtin/for-each-ref: refactor cmd_for_each_ref() builtin/for-each-ref: stop using the_repository builtin/checkout-index: stop using the_repository
builtin/checkout-index.c | 43 ++++++++++++++++++++-------------------- builtin/for-each-ref.c | 6 ++---- builtin/ls-files.c | 32 +++++++++++++++--------------- builtin/pack-refs.c | 9 ++++----- builtin/send-pack.c | 8 ++++---- builtin/verify-commit.c | 15 +++++++------- builtin/verify-tag.c | 9 ++++----- 7 files changed, 58 insertions(+), 64 deletions(-)
Range-diff versus v1:
-: ---------- > 1: ceb03199e1 builtin/verify-tag: refactor `cmd_verify_tag()` 1: 3e8d11ccfe ! 2: ecfb834600 builtin/verify-tag: stop using `the_repository`
@@ Commit message
run_builtin() will call the cmd_verify_tag() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the
- configuration file. So it is safe to omit reading the config when repo
- argument the caller gave us is NULL.
+ configuration file.
@@ builtin/verify-tag.c: static const char * const verify_tag_usage[] = {
int i = 1, verbose = 0, had_error = 0;
unsigned flags = 0;
@@ builtin/verify-tag.c: int cmd_verify_tag(int argc,
- OPT_END()
- };
+ flags |= GPG_VERIFY_OMIT_STATUS;
+ }
- git_config(git_default_config, NULL);
-+ if (repo)
-+ repo_config(repo, git_default_config, NULL);
++ repo_config(repo, git_default_config, NULL);
- argc = parse_options(argc, argv, prefix, verify_tag_options,
- verify_tag_usage, PARSE_OPT_KEEP_ARGV0);
-@@ builtin/verify-tag.c: int cmd_verify_tag(int argc,
+ while (i < argc) {
struct object_id oid;
const char *name = argv[i++];
-: ---------- > 3: 2f770824b1 builtin/verify-commit: refactor `cmd_verify_commit()` 2: 88af56e220 ! 4: 546588a6ae builtin/verify-commit.c: stop using `the_repository`
@@ Metadata
Author: Usman Akinyemi <[email protected]>
## Commit message ##
- builtin/verify-commit.c: stop using the_repository
+ builtin/verify-commit: stop using the_repository
Remove the_repository global variable in favor of the repository
- argument that gets passed in "builtin/verify-tag.c".
+ argument that gets passed in "builtin/verify-commit.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_commit() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the
- configuration file. So it is safe to omit reading the config when repo
- argument the caller gave us is NULL.
+ configuration file.
- Let's pass repository argument to verify_commit() function to remove
- it's dependency on the global the_repository variable.
+ Pass the repository available in the calling context to verify_commit()
+ to remove it's dependency on the global the_repository variable.
@@ builtin/verify-commit.c: static int verify_commit(const char *name, unsigned fla
int i = 1, verbose = 0, had_error = 0;
unsigned flags = 0;
@@ builtin/verify-commit.c: int cmd_verify_commit(int argc,
- OPT_END()
- };
+ if (verbose)
+ flags |= GPG_VERIFY_VERBOSE;
- git_config(git_default_config, NULL);
-+ if (repo)
-+ repo_config(repo, git_default_config, NULL);
++ repo_config(repo, git_default_config, NULL);
- argc = parse_options(argc, argv, prefix, verify_commit_options,
- verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
-@@ builtin/verify-commit.c: int cmd_verify_commit(int argc,
+ /* sometimes the program was terminated because this signal
* was received in the process of writing the gpg input: */
signal(SIGPIPE, SIG_IGN);
while (i < argc)
-: ---------- > 5: 99f10469bd builtin/send-pack: refactor `cmd_send_pack()` 3: 39409ea113 ! 6: cb3886bc46 builtin/send-pack.c: stop using `the_repository`
@@ Metadata
Author: Usman Akinyemi <[email protected]>
## Commit message ##
- builtin/send-pack.c: stop using the_repository
+ builtin/send-pack: stop using the_repository
Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/send-pack.c".
@@ Commit message
run_builtin() will call the cmd_send_pack() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the
- configuration file. So it is safe to omit reading the config when repo
- argument the caller gave us is NULL.
+ configuration file.
@@ builtin/send-pack.c: static int send_pack_config(const char *k, const char *v,
struct refspec rs = REFSPEC_INIT_PUSH;
const char *remote_name = NULL;
@@ builtin/send-pack.c: int cmd_send_pack(int argc,
- OPT_END()
- };
+ if (!dest)
+ usage_with_options(send_pack_usage, options);
- git_config(send_pack_config, NULL);
-+ if (repo)
-+ repo_config(repo, send_pack_config, NULL);
- argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
- if (argc > 0) {
- dest = argv[0];
++ repo_config(repo, send_pack_config, NULL);
+
+ args.verbose = verbose;
+ args.dry_run = dry_run;
@@ builtin/send-pack.c: int cmd_send_pack(int argc,
set_ref_status_for_push(remote_refs, args.send_mirror,
args.force_update);
-: ---------- > 7: d104522e30 builtin/pack-refs: refactor `cmd_pack_refs()` 4: 6fd5f4727c ! 8: 7f02e48663 builtin/pack-refs: stop using `the_repository`
@@ Commit message
run_builtin() will call the cmd_pack_refs() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the
- configuration file. So it is safe to omit reading the config when repo
- argument the caller gave us is NULL.
+ configuration file.
@@ builtin/pack-refs.c: static char const * const pack_refs_usage[] = {
struct ref_exclusions excludes = REF_EXCLUSIONS_INIT; struct string_list included_refs = STRING_LIST_INIT_NODUP;
@@ builtin/pack-refs.c: int cmd_pack_refs(int argc,
- N_("references to exclude")),
- OPT_END(),
- };
-- git_config(git_default_config, NULL);
-+ if (repo)
-+ repo_config(repo, git_default_config, NULL);
if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
usage_with_options(pack_refs_usage, opts);
+- git_config(git_default_config, NULL);
++ repo_config(repo, git_default_config, NULL);
+ for_each_string_list_item(item, &option_excluded_refs)
+ add_ref_exclusion(pack_refs_opts.exclusions, item->string);
+
@@ builtin/pack-refs.c: int cmd_pack_refs(int argc,
if (!pack_refs_opts.includes->nr)
string_list_append(pack_refs_opts.includes, "refs/tags/*");
5: c58f27988b ! 9: b706fab321 builtin/ls-files: stop using the_repository
@@ Commit message
call will give the options help and exit, without having to consult much of the configuration file.
- Let's pass repository argument to expand_objectsize(),
- show_ru_info() functions to remove their dependency on the global
- the_repository variable.
+ Pass the repository available in the calling context to both
+ expand_objectsize() and show_ru_info() to remove their
+ dependency on the global the_repository variable.
-: ---------- > 10: d2fc527d51 builtin/for-each-ref: refactor `cmd_for_each_ref()` 6: 4bbca37330 ! 11: c3a3a6cff7 builtin/for-each-ref: stop using `the_repository`
@@ Commit message
run_builtin() will call the cmd_for_each_ref() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the
- configuration file. So it is safe to omit reading the config when repo
- argument the caller gave us is NULL.
+ configuration file.
@@ builtin/for-each-ref.c: static char const * const for_each_ref_usage[] = {
struct ref_sorting *sorting;
struct string_list sorting_options = STRING_LIST_INIT_DUP;
@@ builtin/for-each-ref.c: int cmd_for_each_ref(int argc,
-
- format.format = "%(objectname) %(objecttype)\t%(refname)";
+ if (verify_ref_format(&format))
+ usage_with_options(for_each_ref_usage, opts);
- git_config(git_default_config, NULL);
-+ if (repo)
-+ repo_config(repo, git_default_config, NULL);
-
- /* Set default (refname) sorting */
- string_list_append(&sorting_options, "refname");
++ repo_config(repo, git_default_config, NULL);
+ sorting = ref_sorting_options(&sorting_options);
+ ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
+ filter.ignore_case = icase;
7: 369008d554 ! 12: d3a18f857e builtin/checkout-index.c: stop using the_repository
@@ Metadata
Author: Usman Akinyemi <[email protected]>
## Commit message ##
- builtin/checkout-index.c: stop using the_repository
+ builtin/checkout-index: stop using the_repository
Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".
@@ Commit message
call will give the options help and exit, without having to consult much of the configuration file.
- Let's pass repository argument to checkout_all() and checkout_file()
- functions to remove their dependency on the global the_repository variable.
+ Pass the repository available in the calling context to both checkout_all()
+ and checkout_file() to remove their dependency on the global
+ the_repository variable.
Signature · 1 line
2.48.1
PATCH v2 1/12 builtin/verify-tag: refactor `cmd_verify_tag()`
Move git_config() call after usage_with_options() to avoid NULL repo check.
When "-h" is passed to builtins using the RUN_SETUP macro, repo passed by run_builtin() will be NULL. If we use the repo instead of the global the_repository variable. We will have to switch from git_config() to repo_config() which takes in repo. We must check for NULL repo if repo_config() comes before usage_with_options(). Moving git_config() after usage_with_options() eliminates this need, as usage_with_options() exit before calling repo_config().
This will be useful in the following patch which remove the_repository global variable in favor of the repo passed by run_builtin().
--- builtin/verify-tag.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
builtin/verify-tag.c +2 -2
@@ -35,8 +35,6 @@ int cmd_verify_tag(int argc, OPT_END() }; -git_config (git_default_config,NULL ); - argc =parse_options (argc, argv, prefix, verify_tag_options, verify_tag_usage,PARSE_OPT_KEEP_ARGV0 );if (argc <= i)
@@ -52,6 +50,8 @@ int cmd_verify_tag(int argc, flags |=GPG_VERIFY_OMIT_STATUS ; } +git_config (git_default_config,NULL ); +while (i < argc) {struct object_id oid;const char *name = argv[i++];
Signature · 1 line
2.48.1
Re: [PATCH v2 01/12] builtin/verify-tag: refactor `cmd_verify_tag()`
Quoted 2 lines · Usman Akinyemi
Move
git_config()call afterusage_with_options()to avoid NULLrepocheck.
Two things and a half.
- This move of a single call is not something I see usually called "refactor".
- The new call to git_config() should be chosen a bit more carefully to be future-proof. It is harder to see with the reduced context, but verbose and format.format has already been referenced before the new place where you read the configuration file, which would mean that you are making it impossible for future developers to add configuration variables to give default values to these two settings.
The same comment applies to potential new configuration variables that may control how parse_options() would work. With this line of conversion, we are closing the door for such configuration variables (e.g., OPTION_FILENAME may want to understand "~/path" and if we had user.homedirectory configuration variable, the value of the variable should affect what file the string "~/path" given to the option refers to).
Quoted 5 lines
When "-h" is passed to builtins using the
RUN_SETUPmacro,repopassed byrun_builtin()will be NULL. If we use therepoinstead of the globalthe_repositoryvariable. We will have to switch fromgit_config()torepo_config()which takes inrepo.
I do not quite agree with this line of reasoning behind "will have to switch". You need to realize that this is cmd_verify_tag() that is designed to be ONLY called as the top-level command implementation and not called from random places as a library function. It is perfectly fine for it to work with the_repository with git_config().
The helper functions that this functions call may be different matter, though.
PATCH v2 2/12 builtin/verify-tag: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_tag() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file.
--- builtin/verify-tag.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
builtin/verify-tag.c +3 -4
@@ -5,7 +5,6 @@* * Based on git-verify-tag.sh */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -23,7 +22,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -50,13 +49,13 @@ int cmd_verify_tag(int argc, flags |=GPG_VERIFY_OMIT_STATUS ; } -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );while (i < argc) {struct object_id oid;const char *name = argv[i++]; -if (repo_get_oid (the_repository, name, &oid)) { +if (repo_get_oid (repo, name, &oid)) { had_error = !!error ("tag '%s' not found." , name);continue ; }
Signature · 1 line
2.48.1
Re: [PATCH v2 02/12] builtin/verify-tag: stop using `the_repository`
Quoted 10 lines · Usman Akinyemi
builtin/verify-tag.c +1
-1@@ -23,7 +22,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 52
Quoted 5 lines
builtin/verify-tag.c +1
-1@@ -50,13 +49,13 @@ int cmd_verify_tag(int argc, flags |=GPG_VERIFY_OMIT_STATUS ; } -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );
I seriously think that it is a horrible idea (but the previous step of this series is hardly the first one that commits the same sin) to move git_config() down only to deal with "repo might be NULL if run outside a repository". We should stop making such changes, and we should revert the changes we already made along that line, to solve it differently.
Wouldn't it work much better if we teach repo_config() to allow repo to be NULL to signal that we are outside any repository, and behave the same way the current git_config() works when called outside a repository? Even though the function is called repo_config(), it is *NOT* limited to read from $GIT_DIR/config but does read from the usual "repository configuration trumps per-user configuration which trumps system-side configuration" cascade, so it is natural to skip the repository configuration when called outside any repository but read the other configuration sources, which should be what happens when git_config() is called from outside the repository, no?
Re: [PATCH v2 02/12] builtin/verify-tag: stop using `the_repository`
Quoted 3 lines · Junio C Hamano
Usman Akinyemi <[email protected]> writes:
Hi Junio.
Quoted 13 lines
Quoted 13 lines
builtin/verify-tag.c +1
-1@@ -23,7 +22,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;@@ -50,13 +49,13 @@ int cmd_verify_tag(int argc, flags |=GPG_VERIFY_OMIT_STATUS ; }
Quoted 10 lines
builtin/verify-tag.c int cmd_verify_tag(int argc, · line 52
Quoted 2 lines
builtin/verify-tag.c +1
-1-git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );I seriously think that it is a horrible idea (but the previous step of this series is hardly the first one that commits the same sin) to move
git_config()down only to deal with "repo might be NULL if run outside a repository". We should stop making such changes, and we should revert the changes we already made along that line, to solve it differently.
Yeah, I agree with this after going through your comment on the other patch. We should look for a better solution.
Quoted 10 lines
Wouldn't it work much better if we teach
repo_config()to allow repo to be NULL to signal that we are outside any repository, and behave the same way the currentgit_config()works when called outside a repository? Even though the function is calledrepo_config(), it is *NOT* limited to read from $GIT_DIR/config but does read from the usual "repository configuration trumps per-user configuration which trumps system-side configuration" cascade, so it is natural to skip the repository configuration when called outside any repository but read the other configuration sources, which should be what happens whengit_config()is called from outside the repository, no?
Yeah, I was studying the config.c and config.h files to understand better how all these functions work.
The git_config() when called outside the repository, uses the global the_repository
variable basically called the repo_config(). It does not necessarily handle any situation
when the repo was NULL. It always uses the global the_repository variable. I do not think we want to handle the repo_config() the same as the point of all these are to reduce/remove the use of the_repository global variable.
While going through the config.c I saw the read_very_early_config() which read the config
from the system and global settings and does not require any repo variable. I think, to teach
the repo_config() to allow NULL value, we could call the read_very_early_config() whenever
the repo is NULL as we know, this happens outside the repository. I sent a rfc patch for this
and it can viewed here :-
https://public-inbox.org/git/[email protected]/T/#u
Another approach which I was thinking about is having a local repository variable inside
the repo_config() whenever the repo variable passed to it is NULL.
What do you think ?
Thanks.
Quoted 1 line
Re: [PATCH v2 02/12] builtin/verify-tag: stop using `the_repository`
Quoted 6 lines · Usman Akinyemi
While going through the
config.cI saw theread_very_early_config()which read the config from the system and global settings and does not require any repo variable. I think, to teach therepo_config()to allow NULL value, we could call theread_very_early_config()whenever the repo is NULL as we know, this happens outside the repository.
Yeah, when I wrote the message you were responding to, I noticed that do_git_config_sequence() is prepared to take NULL as repo (even though it is a bit clunky interface; you need to futz with members of the opts structure like opts->ignore_repo to have the code ignore repo that is NULL). So a caller that calls config_with_options() with no config_source and no repo should be a good candidate to reuse.
As we positively know that we are not in a repository in our case, using read_very_early_config() when repo is NULL sounds like a sensible thing to do, I would think.
Thanks.
PATCH v2 3/12 builtin/verify-commit: refactor `cmd_verify_commit()`
In reply to Usman AkinyemiMove git_config() call after usage_with_options() to avoid NULL repo check.
When "-h" is passed to builtins using the RUN_SETUP macro, repo passed by run_builtin() will be NULL. If we use the repo instead of the global the_repository variable. We will have to switch from git_config() to repo_config() which takes in repo. We must check for NULL repo if repo_config() comes before usage_with_options(). Moving git_config() after usage_with_options() eliminates this need, as usage_with_options() exit before calling repo_config().
This will be useful in the following patch which remove the_repository global variable in favor of the repo passed by run_builtin().
--- builtin/verify-commit.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
builtin/verify-commit.c +2 -2
@@ -64,8 +64,6 @@ int cmd_verify_commit(int argc, OPT_END() }; -git_config (git_default_config,NULL ); - argc =parse_options (argc, argv, prefix, verify_commit_options, verify_commit_usage,PARSE_OPT_KEEP_ARGV0 );if (argc <= i)
@@ -74,6 +72,8 @@ int cmd_verify_commit(int argc,if (verbose) flags |=GPG_VERIFY_VERBOSE ; +git_config (git_default_config,NULL ); +/* sometimes the program was terminated because this signal * was received in the process of writing the gpg input: */ signal (SIGPIPE ,SIG_IGN );
Signature · 1 line
2.48.1
PATCH v2 4/12 builtin/verify-commit: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-commit.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_commit() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file.
Pass the repository available in the calling context to verify_commit() to remove it's dependency on the global the_repository variable.
--- builtin/verify-commit.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
builtin/verify-commit.c +6 -7
@@ -5,7 +5,6 @@* * Based on git-verify-tag */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)return ret; } -static int verify_commit (const char *name,unsigned flags) +static int verify_commit (struct repository *repo,const char *name,unsigned flags) {struct object_id oid;struct object *obj; -if (repo_get_oid (the_repository, name, &oid)) +if (repo_get_oid (repo, name, &oid))return error ("commit '%s' not found." , name); - obj =parse_object (the_repository, &oid); + obj =parse_object (repo, &oid);if (!obj)return error ("%s: unable to read file." , name);if (obj->type !=OBJ_COMMIT )
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)int cmd_verify_commit (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -72,13 +71,13 @@ int cmd_verify_commit(int argc,if (verbose) flags |=GPG_VERIFY_VERBOSE ; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );/* sometimes the program was terminated because this signal * was received in the process of writing the gpg input: */ signal (SIGPIPE ,SIG_IGN );while (i < argc) -if (verify_commit (argv[i++], flags)) +if (verify_commit (repo, argv[i++], flags)) had_error =1 ;return had_error; }
Signature · 1 line
2.48.1
PATCH v2 5/12 builtin/send-pack: refactor `cmd_send_pack()`
In reply to Usman AkinyemiMove git_config() call after usage_with_options() to avoid NULL repo check.
When "-h" is passed to builtins using the RUN_SETUP macro, repo passed by run_builtin() will be NULL. If we use the repo instead of the global the_repository variable. We will have to switch from git_config() to repo_config() which takes in repo. We must check for NULL repo if repo_config() comes before usage_with_options(). Moving git_config() after usage_with_options() eliminates this need, as usage_with_options() exit before calling repo_config().
This will be useful in the following patch which remove the_repository global variable in favor of the repo passed by run_builtin().
--- builtin/send-pack.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
builtin/send-pack.c +2 -1
@@ -212,7 +212,6 @@ int cmd_send_pack(int argc, OPT_END() }; -git_config (send_pack_config,NULL ); argc =parse_options (argc, argv, prefix, options, send_pack_usage,0 );if (argc >0 ) { dest = argv[0 ];
@@ -222,6 +221,8 @@ int cmd_send_pack(int argc,if (!dest)usage_with_options (send_pack_usage, options); +git_config (send_pack_config,NULL ); + args.verbose = verbose; args.dry_run = dry_run; args.send_mirror = send_mirror;
Signature · 1 line
2.48.1
PATCH v2 6/12 builtin/send-pack: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/send-pack.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_send_pack() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file.
--- builtin/send-pack.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
builtin/send-pack.c +3 -4
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "hex.h"
@@ -151,7 +150,7 @@ static int send_pack_config(const char *k, const char *v,int cmd_send_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct refspec rs =REFSPEC_INIT_PUSH ;const char *remote_name =NULL ;
@@ -221,7 +220,7 @@ int cmd_send_pack(int argc,if (!dest)usage_with_options (send_pack_usage, options); -git_config (send_pack_config,NULL ); +repo_config (repo, send_pack_config,NULL ); args.verbose = verbose; args.dry_run = dry_run;
@@ -318,7 +317,7 @@ int cmd_send_pack(int argc,set_ref_status_for_push (remote_refs, args.send_mirror, args.force_update); - ret =send_pack (the_repository, &args, fd, conn, remote_refs, &extra_have); + ret =send_pack (repo, &args, fd, conn, remote_refs, &extra_have);if (helper_status)print_helper_status (remote_refs);
Signature · 1 line
2.48.1
PATCH v2 7/12 builtin/pack-refs: refactor `cmd_pack_refs()`
In reply to Usman AkinyemiMove git_config() call after usage_with_options() to avoid NULL repo check.
When "-h" is passed to builtins using the RUN_SETUP macro, repo passed by run_builtin() will be NULL. If we use the repo instead of the global the_repository variable. We will have to switch from git_config() to repo_config() which takes in repo. We must check for NULL repo if repo_config() comes before usage_with_options(). Moving git_config() after usage_with_options() eliminates this need, as usage_with_options() exit before calling repo_config().
This will be useful in the following patch which remove the_repository global variable in favor of the repo passed by run_builtin().
--- builtin/pack-refs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
builtin/pack-refs.c +2 -1
@@ -39,10 +39,11 @@ int cmd_pack_refs(int argc, N_("references to exclude")), OPT_END(), }; -git_config (git_default_config,NULL ); +if (parse_options (argc, argv, prefix, opts, pack_refs_usage,0 ))usage_with_options (pack_refs_usage, opts); +git_config (git_default_config,NULL );for_each_string_list_item (item, &option_excluded_refs)add_ref_exclusion (pack_refs_opts.exclusions, item->string);
Signature · 1 line
2.48.1
PATCH v2 8/12 builtin/pack-refs: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/pack-refs.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_pack_refs() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file.
--- builtin/pack-refs.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
builtin/pack-refs.c +3 -5
@@ -1,5 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE -#include "builtin.h" #include "config.h" #include "gettext.h"
@@ -15,7 +13,7 @@ static char const * const pack_refs_usage[] = {int cmd_pack_refs (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_exclusions excludes =REF_EXCLUSIONS_INIT ;struct string_list included_refs =STRING_LIST_INIT_NODUP ;
@@ -43,7 +41,7 @@ int cmd_pack_refs(int argc,if (parse_options (argc, argv, prefix, opts, pack_refs_usage,0 ))usage_with_options (pack_refs_usage, opts); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );for_each_string_list_item (item, &option_excluded_refs)add_ref_exclusion (pack_refs_opts.exclusions, item->string);
@@ -53,7 +51,7 @@ int cmd_pack_refs(int argc,if (!pack_refs_opts.includes->nr)string_list_append (pack_refs_opts.includes,"refs/tags/*" ); - ret =refs_pack_refs (get_main_ref_store (the_repository), &pack_refs_opts); + ret =refs_pack_refs (get_main_ref_store (repo), &pack_refs_opts);clear_ref_exclusions (&excludes);string_list_clear (&included_refs,0 );
Signature · 1 line
2.48.1
PATCH v2 9/12 builtin/ls-files: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/ls-files.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_ls_files() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit, without having to consult much of the configuration file.
Pass the repository available in the calling context to both expand_objectsize() and show_ru_info() to remove their dependency on the global the_repository variable.
--- builtin/ls-files.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
builtin/ls-files.c +16 -16
@@ -6,7 +6,6 @@* Copyright (C) Linus Torvalds, 2005 */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,repo_clear (&subrepo); } -static void expand_objectsize (struct strbuf *line,const struct object_id *oid, +static void expand_objectsize (struct repository *repo,struct strbuf *line, +const struct object_id *oid,const enum object_type type,unsigned int padded) {if (type ==OBJ_BLOB ) {unsigned long size; -if (oid_object_info (the_repository, oid, &size) <0 ) +if (oid_object_info (repo, oid, &size) <0 )die (_ ("could not get object info about '%s'" ),oid_to_hex (oid));if (padded)
@@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,else if (skip_prefix (format,"(objecttype)" , &format))strbuf_addstr (&sb,type_name (object_type (ce->ce_mode)));else if (skip_prefix (format,"(objectsize:padded)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),1 );else if (skip_prefix (format,"(objectsize)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),0 );else if (skip_prefix (format,"(stage)" , &format))strbuf_addf (&sb,"%d" ,ce_stage (ce));
@@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir, } } -static void show_ru_info (struct index_state *istate) +static void show_ru_info (struct repository *repo,struct index_state *istate) {struct string_list_item *item;
@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)if (!ui->mode[i])continue ;printf ("%s%06o %s %d\t" , tag_resolve_undo, ui->mode[i], -repo_find_unique_abbrev (the_repository, &ui->oid[i], abbrev), +repo_find_unique_abbrev (repo, &ui->oid[i], abbrev), i +1 );write_name (path); }
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,int cmd_ls_files (int argc,const char **argv,const char *cmd_prefix, - struct repository *repo UNUSED) +struct repository *repo) {int require_work_tree =0 , show_tag =0 , i;char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,show_usage_with_options_if_asked (argc, argv, ls_files_usage, builtin_ls_files_options); -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; prefix = cmd_prefix;if (prefix) prefix_len =strlen (prefix); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); -if (repo_read_index (the_repository) <0 ) +if (repo_read_index (repo) <0 )die ("index file corrupt" ); argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -724,7 +724,7 @@ int cmd_ls_files(int argc, max_prefix =common_prefix (&pathspec); max_prefix_len =get_common_prefix_len (max_prefix); -prune_index (the_repository->index, max_prefix, max_prefix_len); +prune_index (repo->index, max_prefix, max_prefix_len);/* Treat unmatching pathspec elements as errors */ if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,*/ if (show_stage || show_unmerged)die (_ ("options '%s' and '%s' cannot be used together" ),"ls-files --with-tree" ,"-s/-u" ); -overlay_tree_on_index (the_repository->index, with_tree, max_prefix); +overlay_tree_on_index (repo->index, with_tree, max_prefix); } -show_files (the_repository, &dir); +show_files (repo, &dir);if (show_resolve_undo) -show_ru_info (the_repository->index); +show_ru_info (repo, repo->index);if (ps_matched &&report_path_error (ps_matched, &pathspec)) {fprintf (stderr,"Did you forget to 'git add'?\n" );
Signature · 1 line
2.48.1
PATCH v2 10/12 builtin/for-each-ref: refactor `cmd_for_each_ref()`
In reply to Usman AkinyemiMove git_config() call after usage_with_options() to avoid NULL repo check.
When "-h" is passed to builtins using the RUN_SETUP macro, repo passed by run_builtin() will be NULL. If we use the repo instead of the global the_repository variable. We will have to switch from git_config() to repo_config() which takes in repo. We must check for NULL repo if repo_config() comes before usage_with_options(). Moving git_config() after usage_with_options() eliminates this need, as usage_with_options() exit before calling repo_config().
This will be useful in the following patch which remove the_repository global variable in favor of the repo passed by run_builtin().
--- builtin/for-each-ref.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
builtin/for-each-ref.c +1 -2
@@ -63,8 +63,6 @@ int cmd_for_each_ref(int argc, format.format ="%(objectname) %(objecttype)\t%(refname)" ; -git_config (git_default_config,NULL ); -/* Set default (refname) sorting */ string_list_append (&sorting_options,"refname" );
@@ -80,6 +78,7 @@ int cmd_for_each_ref(int argc,if (verify_ref_format (&format))usage_with_options (for_each_ref_usage, opts); +git_config (git_default_config,NULL ); sorting =ref_sorting_options (&sorting_options);ref_sorting_set_sort_flags_all (sorting,REF_SORTING_ICASE , icase); filter.ignore_case = icase;
Signature · 1 line
2.48.1
PATCH v2 11/12 builtin/for-each-ref: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/for-each-ref.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_for_each_ref() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit, without having to consult much of the configuration file.
--- builtin/for-each-ref.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
builtin/for-each-ref.c +2 -3
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "commit.h" #include "config.h"
@@ -20,7 +19,7 @@ static char const * const for_each_ref_usage[] = {int cmd_for_each_ref (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_sorting *sorting;struct string_list sorting_options =STRING_LIST_INIT_DUP ;
@@ -78,7 +77,7 @@ int cmd_for_each_ref(int argc,if (verify_ref_format (&format))usage_with_options (for_each_ref_usage, opts); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); sorting =ref_sorting_options (&sorting_options);ref_sorting_set_sort_flags_all (sorting,REF_SORTING_ICASE , icase); filter.ignore_case = icase;
Signature · 1 line
2.48.1
PATCH v2 12/12 builtin/checkout-index: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_checkout_index() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit, without having to consult much of the configuration file.
Pass the repository available in the calling context to both checkout_all() and checkout_file() to remove their dependency on the global the_repository variable.
--- builtin/checkout-index.c | 43 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 22 deletions(-)
builtin/checkout-index.c +21 -22
@@ -5,7 +5,6 @@* */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -68,10 +67,10 @@ static void write_tempfile_record(const char *name, const char *prefix) } } -static int checkout_file (const char *name,const char *prefix) +static int checkout_file (struct repository *repo,const char *name,const char *prefix) {int namelen =strlen (name); -int pos =index_name_pos (the_repository->index, name, namelen); +int pos =index_name_pos (repo->index, name, namelen);int has_same_name =0 ;int is_file =0 ;int is_skipped =1 ;
@@ -81,8 +80,8 @@ static int checkout_file(const char *name, const char *prefix)if (pos <0 ) pos = -pos -1 ; -while (pos <the_repository->index->cache_nr) { -struct cache_entry *ce =the_repository->index->cache[pos]; +while (pos < repo->index->cache_nr) { +struct cache_entry *ce =repo->index->cache[pos];if (ce_namelen (ce) != namelen ||memcmp (ce->name, name, namelen))break ;
@@ -137,13 +136,13 @@ static int checkout_file(const char *name, const char *prefix)return -1 ; } -static int checkout_all (const char *prefix,int prefix_length) +static int checkout_all (struct repository *repo,const char *prefix,int prefix_length) {int i, errs =0 ;struct cache_entry *last_ce =NULL ; -for (i =0 ; i < the_repository->index->cache_nr ; i++) { -struct cache_entry *ce = the_repository->index->cache[i]; +for (i =0 ; i < repo->index->cache_nr ; i++) { +struct cache_entry *ce = repo->index->cache[i];if (S_ISSPARSEDIR (ce->ce_mode)) {if (!ce_skip_worktree (ce))
@@ -156,8 +155,8 @@ static int checkout_all(const char *prefix, int prefix_length)* first entry inside the expanded sparse directory). */ if (ignore_skip_worktree) { -ensure_full_index (the_repository->index); - ce = the_repository->index->cache[i]; +ensure_full_index (repo->index); + ce = repo->index->cache[i]; } }
@@ -213,7 +212,7 @@ static int option_parse_stage(const struct option *opt,int cmd_checkout_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i;struct lock_file lock_file =LOCK_INIT ;
@@ -253,19 +252,19 @@ int cmd_checkout_index(int argc,show_usage_with_options_if_asked (argc, argv, builtin_checkout_index_usage, builtin_checkout_index_options); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); prefix_length = prefix ?strlen (prefix) :0 ; -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; -if (repo_read_index (the_repository) <0 ) { +if (repo_read_index (repo) <0 ) {die ("invalid cache" ); } argc =parse_options (argc, argv, prefix, builtin_checkout_index_options, builtin_checkout_index_usage,0 ); - state.istate = the_repository->index; + state.istate = repo->index; state.force = force; state.quiet = quiet; state.not_new = not_new;
@@ -285,8 +284,8 @@ int cmd_checkout_index(int argc,*/ if (index_opt && !state.base_dir_len && !to_tempfile) { state.refresh_cache =1 ; - state.istate = the_repository->index; -repo_hold_locked_index (the_repository, &lock_file, + state.istate = repo->index; +repo_hold_locked_index (repo, &lock_file,LOCK_DIE_ON_ERROR ); }
@@ -304,7 +303,7 @@ int cmd_checkout_index(int argc,if (read_from_stdin)die ("git checkout-index: don't mix '--stdin' and explicit filenames" ); p =prefix_path (prefix, prefix_length, arg); - err |=checkout_file (p, prefix); + err |=checkout_file (repo, p, prefix);free (p); }
@@ -326,7 +325,7 @@ int cmd_checkout_index(int argc,strbuf_swap (&buf, &unquoted); } p =prefix_path (prefix, prefix_length, buf.buf); - err |=checkout_file (p, prefix); + err |=checkout_file (repo, p, prefix);free (p); }strbuf_release (&unquoted);
@@ -334,7 +333,7 @@ int cmd_checkout_index(int argc, }if (all) - err |=checkout_all (prefix, prefix_length); + err |=checkout_all (repo, prefix, prefix_length);if (pc_workers >1 ) err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +343,7 @@ int cmd_checkout_index(int argc,return 1 ;if (is_lock_file_locked (&lock_file) && -write_locked_index (the_repository->index, &lock_file,COMMIT_LOCK )) +write_locked_index (repo->index, &lock_file,COMMIT_LOCK ))die ("Unable to write new index file" );return 0 ; }
Signature · 1 line
2.48.1
v3 cover stop using the_repository global variable.
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in builtin commands.
These sets of commands are commands that use only RUN_SETUP macro in "git.c". Basically, When -h is passed to any of this command outside a Git repository, the run_builtin() will call the cmd_x() function (where x is any of the command from the sets of builtin commands that the_repository is removed from) with repo set to NULL and then early in the function, parse_options() or show_usage_with_options_if_asked() call will give the options help and exit.
Some, functions also uses the_repository global internally, so, let's let's refactor them and pass struct repo as one of the argument.
As the repo value can be NULL if a builtin command is run outside any repository. The current implementation of repo_config() will fail if repo is NULL.
If the repo is NULL, the repo_config() can ignore the repository configuration but it should read the other configuration sources like the system-side configuration instead of failing.
Teach the repo_config() to allow repo to be NULL by calling the read_very_early_config() which read config but only enumerate system and global settings. This make it possible for us to savely replace git_config() with repo_config().
Changes since v2
================
- Remove preparatory patches that move the git_config() below the usage_with_options().
- Add a new preparatory patch that teach the repo_config() to accept NULL value for repo variable.
- Add tests to each of the builtin function in this commit to ensure repo_config() works as expected.
- Make changes to the commit messages.
Usman Akinyemi (8):
config: teach repo_config to allow repo to be NULL builtin/verify-tag: stop using the_repository builtin/verify-commit: stop using the_repository builtin/send-pack: stop using the_repository builtin/pack-refs: stop using the_repository builtin/ls-files: stop using the_repository builtin/for-each-ref: stop using the_repository builtin/checkout-index: stop using the_repository
builtin/checkout-index.c | 43 ++++++++++++++++----------------- builtin/for-each-ref.c | 5 ++-- builtin/ls-files.c | 32 ++++++++++++------------ builtin/pack-refs.c | 8 +++--- builtin/send-pack.c | 7 +++--- builtin/verify-commit.c | 13 +++++----- builtin/verify-tag.c | 7 +++--- config.c | 4 +++ config.h | 3 +++ t/t0610-reftable-basics.sh | 7 ++++++ t/t2006-checkout-index-basic.sh | 7 ++++++ t/t3004-ls-files-basic.sh | 7 ++++++ t/t5400-send-pack.sh | 7 ++++++ t/t6300-for-each-ref.sh | 7 ++++++ t/t7030-verify-tag.sh | 7 ++++++ t/t7510-signed-commit.sh | 7 ++++++ 16 files changed, 110 insertions(+), 61 deletions(-)
Range-diff versus v2:
1: a313f9afb7 < -: ---------- builtin/verify-tag: refactor `cmd_verify_tag()` -: ---------- > 1: a5c69f3753 config: teach repo_config to allow `repo` to be NULL 2: f993160d90 ! 2: dfa0da4061 builtin/verify-tag: stop using `the_repository`
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_tag() function with repo set to NULL and then early in the function, parse_options() call will give
- the options help and exit, without having to consult much of the
- configuration file.
+ the options help and exit.
@@ builtin/verify-tag.c: static const char * const verify_tag_usage[] = {
int i = 1, verbose = 0, had_error = 0;
unsigned flags = 0;
@@ builtin/verify-tag.c: int cmd_verify_tag(int argc,
- flags |= GPG_VERIFY_OMIT_STATUS;
- }
+ OPT_END()
+ };
- git_config(git_default_config, NULL);
+ repo_config(repo, git_default_config, NULL);
- while (i < argc) {
+ argc = parse_options(argc, argv, prefix, verify_tag_options,
+ verify_tag_usage, PARSE_OPT_KEEP_ARGV0);
+@@ builtin/verify-tag.c: int cmd_verify_tag(int argc,
struct object_id oid;
const char *name = argv[i++];
@@ builtin/verify-tag.c: int cmd_verify_tag(int argc,
had_error = !!error("tag '%s' not found.", name); continue;
}
+
+ ## t/t7030-verify-tag.sh ##
+@@ t/t7030-verify-tag.sh: export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+ . ./test-lib.sh
+ . "$TEST_DIRECTORY/lib-gpg.sh"
+
++test_expect_success GPG 'verify-tag does not crash with -h' '
++ test_expect_code 129 git verify-tag -h >usage &&
++ test_grep "[Uu]sage: git verify-tag " usage &&
++ test_expect_code 129 nongit git verify-tag -h >usage &&
++ test_grep "[Uu]sage: git verify-tag " usage
++'
++
+ test_expect_success GPG 'create signed tags' '
+ echo 1 >file && git add file &&
+ test_tick && git commit -m initial &&
3: f2f785190d < -: ---------- builtin/verify-commit: refactor `cmd_verify_commit()` 4: 5f8dc0c0ec ! 3: ade2d026cb builtin/verify-commit: stop using `the_repository`
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_commit() function with repo set to NULL and then early in the function, parse_options() call will
- give the options help and exit, without having to consult much of the
- configuration file.
+ give the options help and exit.
Pass the repository available in the calling context to verify_commit() to remove it's dependency on the global the_repository variable.
@@ builtin/verify-commit.c: static int verify_commit(const char *name, unsigned fla
int i = 1, verbose = 0, had_error = 0;
unsigned flags = 0;
@@ builtin/verify-commit.c: int cmd_verify_commit(int argc,
- if (verbose)
- flags |= GPG_VERIFY_VERBOSE;
+ OPT_END()
+ };
- git_config(git_default_config, NULL);
+ repo_config(repo, git_default_config, NULL);
- /* sometimes the program was terminated because this signal
+ argc = parse_options(argc, argv, prefix, verify_commit_options,
+ verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
+@@ builtin/verify-commit.c: int cmd_verify_commit(int argc,
* was received in the process of writing the gpg input: */
signal(SIGPIPE, SIG_IGN);
while (i < argc)
@@ builtin/verify-commit.c: int cmd_verify_commit(int argc,
had_error = 1;
return had_error;
}
+
+ ## t/t7510-signed-commit.sh ##
+@@ t/t7510-signed-commit.sh: export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+ GNUPGHOME_NOT_USED=$GNUPGHOME
+ . "$TEST_DIRECTORY/lib-gpg.sh"
+
++test_expect_success GPG 'verify-commit does not crash with -h' '
++ test_expect_code 129 git verify-commit -h >usage &&
++ test_grep "[Uu]sage: git verify-commit " usage &&
++ test_expect_code 129 nongit git verify-commit -h >usage &&
++ test_grep "[Uu]sage: git verify-commit " usage
++'
++
+ test_expect_success GPG 'create signed commits' '
+ test_oid_cache <<-\EOF &&
+ header sha1:gpgsig
5: d79c3fc1ee < -: ---------- builtin/send-pack: refactor `cmd_send_pack()` 6: f280a9c387 ! 4: e3b58bc6cf builtin/send-pack: stop using `the_repository`
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_send_pack() function with repo set to NULL and then early in the function, parse_options() call will give
- the options help and exit, without having to consult much of the
- configuration file.
+ the options help and exit.
@@ builtin/send-pack.c: static int send_pack_config(const char *k, const char *v,
struct refspec rs = REFSPEC_INIT_PUSH;
const char *remote_name = NULL;
@@ builtin/send-pack.c: int cmd_send_pack(int argc,
- if (!dest)
- usage_with_options(send_pack_usage, options);
+ OPT_END()
+ };
- git_config(send_pack_config, NULL);
+ repo_config(repo, send_pack_config, NULL);
-
- args.verbose = verbose;
- args.dry_run = dry_run;
+ argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
+ if (argc > 0) {
+ dest = argv[0];
@@ builtin/send-pack.c: int cmd_send_pack(int argc,
set_ref_status_for_push(remote_refs, args.send_mirror,
args.force_update);
@@ builtin/send-pack.c: int cmd_send_pack(int argc,
if (helper_status)
print_helper_status(remote_refs);
+
+ ## t/t5400-send-pack.sh ##
+@@ t/t5400-send-pack.sh: test_expect_success setup '
+ echo Rebase &&
+ git log'
+
++test_expect_success 'send-pack does not crash with -h' '
++ test_expect_code 129 git send-pack -h >usage &&
++ test_grep "[Uu]sage: git send-pack " usage &&
++ test_expect_code 129 nongit git send-pack -h >usage &&
++ test_grep "[Uu]sage: git send-pack " usage
++'
++
+ test_expect_success 'pack the source repository' '
+ git repack -a -d &&
+ git prune
7: df192a1b2c < -: ---------- builtin/pack-refs: refactor `cmd_pack_refs()` 8: 845cd1ea7c ! 5: b11e99627c builtin/pack-refs: stop using `the_repository`
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_pack_refs() function with repo set to NULL and then early in the function, parse_options() call will give
- the options help and exit, without having to consult much of the
- configuration file.
+ the options help and exit.
@@ builtin/pack-refs.c: static char const * const pack_refs_usage[] = {
struct ref_exclusions excludes = REF_EXCLUSIONS_INIT; struct string_list included_refs = STRING_LIST_INIT_NODUP;
@@ builtin/pack-refs.c: int cmd_pack_refs(int argc,
- if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
- usage_with_options(pack_refs_usage, opts);
-
+ N_("references to exclude")),
+ OPT_END(),
+ };
- git_config(git_default_config, NULL);
+ repo_config(repo, git_default_config, NULL);
- for_each_string_list_item(item, &option_excluded_refs)
- add_ref_exclusion(pack_refs_opts.exclusions, item->string);
+ if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
+ usage_with_options(pack_refs_usage, opts);
@@ builtin/pack-refs.c: int cmd_pack_refs(int argc,
if (!pack_refs_opts.includes->nr)
@@ builtin/pack-refs.c: int cmd_pack_refs(int argc,
clear_ref_exclusions(&excludes); string_list_clear(&included_refs, 0);
+
+ ## t/t0610-reftable-basics.sh ##
+@@ t/t0610-reftable-basics.sh: export GIT_TEST_DEFAULT_REF_FORMAT +
+ INVALID_OID=$(test_oid 001)
+
++test_expect_success 'pack-refs does not crash with -h' '
++ test_expect_code 129 git pack-refs -h >usage &&
++ test_grep "[Uu]sage: git pack-refs " usage &&
++ test_expect_code 129 nongit git pack-refs -h >usage &&
++ test_grep "[Uu]sage: git pack-refs " usage
++'
++
+ test_expect_success 'init: creates basic reftable structures' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
9: 2aff17b09c ! 6: 51c80f9273 builtin/ls-files: stop using the_repository
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_ls_files() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked()
- call will give the options help and exit, without having to consult much
- of the configuration file.
+ call will give the options help and exit.
Pass the repository available in the calling context to both expand_objectsize() and show_ru_info() to remove their
@@ builtin/ls-files.c: int cmd_ls_files(int argc,
if (ps_matched && report_path_error(ps_matched, &pathspec)) {
fprintf(stderr, "Did you forget to 'git add'?\n");
+
+ ## t/t3004-ls-files-basic.sh ##
+@@ t/t3004-ls-files-basic.sh: test_expect_success 'ls-files -h in corrupt repository' '
+ test_grep "[Uu]sage: git ls-files " broken/usage
+ '
+
++test_expect_success 'ls-files does not crash with -h' '
++ test_expect_code 129 git ls-files -h >usage &&
++ test_grep "[Uu]sage: git ls-files " usage &&
++ test_expect_code 129 nongit git ls-files -h >usage &&
++ test_grep "[Uu]sage: git ls-files " usage
++'
++
+ test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' '
+ mkdir subs &&
+ ln -s nosuch link &&
10: 4cc9a4935a < -: ---------- builtin/for-each-ref: refactor `cmd_for_each_ref()` 11: 42c1a7ae74 ! 7: 63bb89291f builtin/for-each-ref: stop using `the_repository`
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_for_each_ref() function with repo set to NULL and then early in the function, parse_options() call will
- give the options help and exit, without having to consult much of the
- configuration file.
+ give the options help and exit.
@@ builtin/for-each-ref.c: static char const * const for_each_ref_usage[] = {
struct ref_sorting *sorting;
struct string_list sorting_options = STRING_LIST_INIT_DUP;
@@ builtin/for-each-ref.c: int cmd_for_each_ref(int argc,
- if (verify_ref_format(&format))
- usage_with_options(for_each_ref_usage, opts);
+
+ format.format = "%(objectname) %(objecttype)\t%(refname)";
- git_config(git_default_config, NULL);
+ repo_config(repo, git_default_config, NULL);
- sorting = ref_sorting_options(&sorting_options);
- ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
- filter.ignore_case = icase;
+
+ /* Set default (refname) sorting */
+ string_list_append(&sorting_options, "refname");
+
+ ## t/t6300-for-each-ref.sh ##
+@@ t/t6300-for-each-ref.sh: test_expect_success 'Check invalid atoms names are errors' '
+ test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
+ '
+
++test_expect_success 'for-each-ref does not crash with -h' '
++ test_expect_code 129 git for-each-ref -h >usage &&
++ test_grep "[Uu]sage: git for-each-ref " usage &&
++ test_expect_code 129 nongit git for-each-ref -h >usage &&
++ test_grep "[Uu]sage: git for-each-ref " usage
++'
++
+ test_expect_success 'Check format specifiers are ignored in naming date atoms' '
+ git for-each-ref --format="%(authordate)" refs/heads &&
+ git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
12: 232cbef160 ! 8: 8dfe6b40c8 builtin/checkout-index: stop using the_repository
@@ Commit message
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_checkout_index() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked()
- call will give the options help and exit, without having to consult much
- of the configuration file.
+ call will give the options help and exit.
Pass the repository available in the calling context to both checkout_all() and checkout_file() to remove their dependency on the global
@@ builtin/checkout-index.c: int cmd_checkout_index(int argc,
die("Unable to write new index file");
return 0;
}
+
+ ## t/t2006-checkout-index-basic.sh ##
+@@ t/t2006-checkout-index-basic.sh: test_expect_success 'checkout-index -h in broken repository' '
+ test_grep "[Uu]sage" broken/usage
+ '
+
++test_expect_success 'checkout-index does not crash with -h' '
++ test_expect_code 129 git checkout-index -h >usage &&
++ test_grep "[Uu]sage: git checkout-index " usage &&
++ test_expect_code 129 nongit git checkout-index -h >usage &&
++ test_grep "[Uu]sage: git checkout-index " usage
++'
++
+ test_expect_success 'checkout-index reports errors (cmdline)' '
+ test_must_fail git checkout-index -- does-not-exist 2>stderr &&
+ test_grep not.in.the.cache stderr
Signature · 1 line
2.48.1
PATCH v3 1/8 config: teach repo_config to allow `repo` to be NULL
The repo value can be NULL if a builtin command is run outside any repository. The current implementation of repo_config() will fail if repo is NULL.
If the repo is NULL the repo_config() can ignore the repository configuration but it should read the other configuration sources like the system-side configuration instead of failing.
Teach the repo_config() to allow repo to be NULL by calling the read_very_early_config() which read config but only enumerate system and global settings.
This will be useful in the following commits.
--- config.c | 4 ++++ config.h | 3 +++ 2 files changed, 7 insertions(+)
config.c +4 -0
@@ -2526,6 +2526,10 @@ void repo_config_clear(struct repository *repo)void repo_config (struct repository *repo,config_fn_t fn,void *data) { +if (!repo) { +read_very_early_config (fn, data); +return ; + }git_config_check_init (repo);configset_iter (repo->config, fn, data); }
config.h +3 -0
@@ -219,6 +219,9 @@ void read_very_early_config(config_fn_t cb, void *data);* repo-specific one; by overwriting, the higher-priority repo-specific * value is left at the end). * +* In cases where the repository variable is NULL, repo_config() will +* call read_early_config(). +* * Unlike git_config_from_file(), this function respects includes. */ void repo_config (struct repository *r,config_fn_t fn,void *);
Signature · 1 line
2.48.1
Re: [PATCH v3 1/8] config: teach repo_config to allow `repo` to be NULL
Quoted 14 lines · Usman Akinyemi
void
repo_config(struct repository *repo, config_fn_t fn, void *data){config.c +4
-0+if (!repo) { +read_very_early_config (fn, data); +return ; + }git_config_check_init (repo);configset_iter (repo->config, fn, data); }config.h +0
-0
config.h void read_very_early_config(config_fn_t cb, void *data); · line 224
Quoted 6 lines
config.h +3
-0@@ -219,6 +219,9 @@ void read_very_early_config(config_fn_t cb, void *data);* repo-specific one; by overwriting, the higher-priority repo-specific * value is left at the end). * +* In cases where the repository variable is NULL, repo_config() will +* call read_early_config(). +*
early or very early?
I am wondering if we should describe the effect we want out of the design more prominently than the way we try to obtain the effect here. In other words, instead of (rather, in addition to) saying that we call helper X, wouldn't it be more helpful to future developers why we call X, to convey the intent, so that they know how to adjust when for example what X does change or X even disappears? E.g.,
When repo==NULL, skip reading the per-repository configuration file but still use the system- and globa- configuration, by calling X. Note that this ignores one-time configuration override "git -c var=val" given from the command line. The only use case the feature to allow passing repo==NULL was designed for is to support handling "git foo -h" (which lets git.c:run_builtin() to pass NULL and have the cmd_foo() call repo_config() before calling parse_options() to notice "-h", give help and exit) for a command that ordinarily require a repository, so this limitation may be OK (but if needed you are welcome to fix it).
That way, folks who are planning to update read_veriy_early_config() so that it pays attention to the "git -c var=val" in the future will be rest assured that they won't be breaking this caller with their planned change.
Of course I didn't spend enough brainpower to make the above comment more concise and to the point, which the final version should be, but hopefully you got the idea.
Thanks.
Re: [PATCH v3 1/8] config: teach repo_config to allow `repo` to be NULL
Quoted 47 lines · Junio C Hamano
Quoted 14 lines · Usman Akinyemi
void
repo_config(struct repository *repo, config_fn_t fn, void *data){config.c +4
-0+if (!repo) { +read_very_early_config (fn, data); +return ; + }git_config_check_init (repo);configset_iter (repo->config, fn, data); }config.h +0
-0config.h void read_very_early_config(config_fn_t cb, void *data); · line 224
Quoted 6 lines
config.h +3
-0@@ -219,6 +219,9 @@ void read_very_early_config(config_fn_t cb, void *data);* repo-specific one; by overwriting, the higher-priority repo-specific * value is left at the end). * +* In cases where the repository variable is NULL, repo_config() will +* call read_early_config(). +* early or very early?
I am wondering if we should describe the effect we want out of the design more prominently than the way we try to obtain the effect here. In other words, instead of (rather, in addition to) saying that we call helper X, wouldn't it be more helpful to future developers why we call X, to convey the intent, so that they know how to adjust when for example what X does change or X even disappears? E.g.,
When repo==NULL, skip reading the per-repository configuration file but still use the system- and globa- configuration, by calling X. Note that this ignores one-time configuration override "git -c var=val" given from the command line. The only use case the feature to allow passing repo==NULL was designed for is to support handling "git foo -h" (which lets git.c:run_builtin() to pass NULL and have the
cmd_foo()callrepo_config()before callingparse_options()to notice "-h", give help and exit) for a command that ordinarily require a repository, so this limitation may be OK (but if needed you are welcome to fix it).
Quoted 8 lines
That way, folks who are planning to update
read_veriy_early_config()so that it pays attention to the "git -c var=val" in the future will be rest assured that they won't be breaking this caller with their planned change.Of course I didn't spend enough brainpower to make the above comment more concise and to the point, which the final version should be, but hopefully you got the idea.
Yeah, thanks Junio, I understand clearly. Your comment is well explained also. I will refine it and add it in an updated patch.
Thank you.
Quoted 3 lines
Thanks.
Re: [PATCH v3 1/8] config: teach repo_config to allow `repo` to be NULL
In reply to Junio C HamanoQuoted 10 lines · Junio C Hamano
Usman Akinyemi <[email protected]> writes:
I am wondering if we should describe the effect we want out of the design more prominently than the way we try to obtain the effect here. In other words, instead of (rather, in addition to) saying that we call helper X, wouldn't it be more helpful to future developers why we call X, to convey the intent, so that they know how to adjust when for example what X does change or X even disappears? E.g.,
Quoted 12 lines
When repo==NULL, skip reading the per-repository configuration file but still use the system- and globa- configuration, by calling X. Note that this ignores one-time configuration override "git -c var=val" given from the command line. The only use case the feature to allow passing repo==NULL was designed for is to support handling "git foo -h" (which lets git.c:run_builtin() to pass NULL and have the
cmd_foo()callrepo_config()before callingparse_options()to notice "-h", give help and exit) for a command that ordinarily require a repository, so this limitation may be OK (but if needed you are welcome to fix it).
I think that would be very helpful as it makes it clear why we don't care about config on the commandline for this series.
Best Wishes
Phillip
Quoted 12 lines
That way, folks who are planning to update
read_veriy_early_config()so that it pays attention to the "git -c var=val" in the future will be rest assured that they won't be breaking this caller with their planned change.Of course I didn't spend enough brainpower to make the above comment more concise and to the point, which the final version should be, but hopefully you got the idea.
Thanks.
PATCH v3 2/8 builtin/verify-tag: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_tag() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/verify-tag.c | 7 +++---- t/t7030-verify-tag.sh | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-)
builtin/verify-tag.c +3 -4
@@ -5,7 +5,6 @@* * Based on git-verify-tag.sh */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -23,7 +22,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -35,7 +34,7 @@ int cmd_verify_tag(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_tag_options, verify_tag_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -56,7 +55,7 @@ int cmd_verify_tag(int argc,struct object_id oid;const char *name = argv[i++]; -if (repo_get_oid (the_repository, name, &oid)) { +if (repo_get_oid (repo, name, &oid)) { had_error = !!error ("tag '%s' not found." , name);continue ; }
t/t7030-verify-tag.sh +7 -0
@@ -7,6 +7,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh . "$TEST_DIRECTORY/lib-gpg.sh" +test_expect_success GPG 'verify-tag does not crash with -h' ' + test_expect_code 129 git verify-tag -h >usage && + test_grep "[Uu]sage: git verify-tag " usage && + test_expect_code 129 nongit git verify-tag -h >usage && + test_grep "[Uu]sage: git verify-tag " usage +' + test_expect_success GPG 'create signed tags' ' echo 1 >file && git add file && test_tick && git commit -m initial &&
Signature · 1 line
2.48.1
Re: [PATCH v3 2/8] builtin/verify-tag: stop using `the_repository`
Quoted 4 lines · Usman Akinyemi
When
-his passed to the command outside a Git repository, therun_builtin()will call thecmd_verify_tag()function withreposet to NULL and then early in the function,parse_options()call will give the options help and exit.
Makes sense.
Quoted 5 lines
t/t7030-verify-tag.sh +0
-0
t/t7030-verify-tag.sh export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME · line 15
Quoted 9 lines
t/t7030-verify-tag.sh +6
-0@@ -7,6 +7,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh . "$TEST_DIRECTORY/lib-gpg.sh" +test_expect_success GPG 'verify-tag does not crash with -h' ' + test_expect_code 129 git verify-tag -h >usage && + test_grep "[Uu]sage: git verify-tag " usage && + test_expect_code 129 nongit git verify-tag -h >usage && + test_grep "[Uu]sage: git verify-tag " usage +'
OK. I am not sure if we want to insist that the "-h" invocation exits with status 129, but changing it would be totaly outside the topic.
Will queue.
PATCH v3 3/8 builtin/verify-commit: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-commit.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_commit() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
Pass the repository available in the calling context to verify_commit() to remove it's dependency on the global the_repository variable.
--- builtin/verify-commit.c | 13 ++++++------- t/t7510-signed-commit.sh | 7 +++++++ 2 files changed, 13 insertions(+), 7 deletions(-)
builtin/verify-commit.c +6 -7
@@ -5,7 +5,6 @@* * Based on git-verify-tag */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)return ret; } -static int verify_commit (const char *name,unsigned flags) +static int verify_commit (struct repository *repo,const char *name,unsigned flags) {struct object_id oid;struct object *obj; -if (repo_get_oid (the_repository, name, &oid)) +if (repo_get_oid (repo, name, &oid))return error ("commit '%s' not found." , name); - obj =parse_object (the_repository, &oid); + obj =parse_object (repo, &oid);if (!obj)return error ("%s: unable to read file." , name);if (obj->type !=OBJ_COMMIT )
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)int cmd_verify_commit (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -64,7 +63,7 @@ int cmd_verify_commit(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_commit_options, verify_commit_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -78,7 +77,7 @@ int cmd_verify_commit(int argc,* was received in the process of writing the gpg input: */ signal (SIGPIPE ,SIG_IGN );while (i < argc) -if (verify_commit (argv[i++], flags)) +if (verify_commit (repo, argv[i++], flags)) had_error =1 ;return had_error; }
t/t7510-signed-commit.sh +7 -0
@@ -8,6 +8,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME GNUPGHOME_NOT_USED=$GNUPGHOME . "$TEST_DIRECTORY/lib-gpg.sh" +test_expect_success GPG 'verify-commit does not crash with -h' ' + test_expect_code 129 git verify-commit -h >usage && + test_grep "[Uu]sage: git verify-commit " usage && + test_expect_code 129 nongit git verify-commit -h >usage && + test_grep "[Uu]sage: git verify-commit " usage +' + test_expect_success GPG 'create signed commits' ' test_oid_cache <<-\EOF && header sha1:gpgsig
Signature · 1 line
2.48.1
PATCH v3 4/8 builtin/send-pack: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/send-pack.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_send_pack() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/send-pack.c | 7 +++---- t/t5400-send-pack.sh | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-)
builtin/send-pack.c +3 -4
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "hex.h"
@@ -151,7 +150,7 @@ static int send_pack_config(const char *k, const char *v,int cmd_send_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct refspec rs =REFSPEC_INIT_PUSH ;const char *remote_name =NULL ;
@@ -212,7 +211,7 @@ int cmd_send_pack(int argc, OPT_END() }; -git_config (send_pack_config,NULL ); +repo_config (repo, send_pack_config,NULL ); argc =parse_options (argc, argv, prefix, options, send_pack_usage,0 );if (argc >0 ) { dest = argv[0 ];
@@ -317,7 +316,7 @@ int cmd_send_pack(int argc,set_ref_status_for_push (remote_refs, args.send_mirror, args.force_update); - ret =send_pack (the_repository, &args, fd, conn, remote_refs, &extra_have); + ret =send_pack (repo, &args, fd, conn, remote_refs, &extra_have);if (helper_status)print_helper_status (remote_refs);
t/t5400-send-pack.sh +7 -0
@@ -55,6 +55,13 @@ test_expect_success setup ' echo Rebase && git log' +test_expect_success 'send-pack does not crash with -h' ' + test_expect_code 129 git send-pack -h >usage && + test_grep "[Uu]sage: git send-pack " usage && + test_expect_code 129 nongit git send-pack -h >usage && + test_grep "[Uu]sage: git send-pack " usage +' + test_expect_success 'pack the source repository' ' git repack -a -d && git prune
Signature · 1 line
2.48.1
PATCH v3 5/8 builtin/pack-refs: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/pack-refs.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_pack_refs() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/pack-refs.c | 8 +++----- t/t0610-reftable-basics.sh | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-)
builtin/pack-refs.c +3 -5
@@ -1,5 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE -#include "builtin.h" #include "config.h" #include "gettext.h"
@@ -15,7 +13,7 @@ static char const * const pack_refs_usage[] = {int cmd_pack_refs (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_exclusions excludes =REF_EXCLUSIONS_INIT ;struct string_list included_refs =STRING_LIST_INIT_NODUP ;
@@ -39,7 +37,7 @@ int cmd_pack_refs(int argc, N_("references to exclude")),OPT_END (), }; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );if (parse_options (argc, argv, prefix, opts, pack_refs_usage,0 ))usage_with_options (pack_refs_usage, opts);
@@ -52,7 +50,7 @@ int cmd_pack_refs(int argc,if (!pack_refs_opts.includes->nr)string_list_append (pack_refs_opts.includes,"refs/tags/*" ); - ret =refs_pack_refs (get_main_ref_store (the_repository), &pack_refs_opts); + ret =refs_pack_refs (get_main_ref_store (repo), &pack_refs_opts);clear_ref_exclusions (&excludes);string_list_clear (&included_refs,0 );
t/t0610-reftable-basics.sh +7 -0
@@ -14,6 +14,13 @@ export GIT_TEST_DEFAULT_REF_FORMAT INVALID_OID=$(test_oid 001) +test_expect_success 'pack-refs does not crash with -h' ' + test_expect_code 129 git pack-refs -h >usage && + test_grep "[Uu]sage: git pack-refs " usage && + test_expect_code 129 nongit git pack-refs -h >usage && + test_grep "[Uu]sage: git pack-refs " usage +' + test_expect_success 'init: creates basic reftable structures' ' test_when_finished "rm -rf repo" && git init repo &&
Signature · 1 line
2.48.1
PATCH v3 6/8 builtin/ls-files: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/ls-files.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_ls_files() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit.
Pass the repository available in the calling context to both expand_objectsize() and show_ru_info() to remove their dependency on the global the_repository variable.
--- builtin/ls-files.c | 32 ++++++++++++++++---------------- t/t3004-ls-files-basic.sh | 7 +++++++ 2 files changed, 23 insertions(+), 16 deletions(-)
builtin/ls-files.c +16 -16
@@ -6,7 +6,6 @@* Copyright (C) Linus Torvalds, 2005 */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,repo_clear (&subrepo); } -static void expand_objectsize (struct strbuf *line,const struct object_id *oid, +static void expand_objectsize (struct repository *repo,struct strbuf *line, +const struct object_id *oid,const enum object_type type,unsigned int padded) {if (type ==OBJ_BLOB ) {unsigned long size; -if (oid_object_info (the_repository, oid, &size) <0 ) +if (oid_object_info (repo, oid, &size) <0 )die (_ ("could not get object info about '%s'" ),oid_to_hex (oid));if (padded)
@@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,else if (skip_prefix (format,"(objecttype)" , &format))strbuf_addstr (&sb,type_name (object_type (ce->ce_mode)));else if (skip_prefix (format,"(objectsize:padded)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),1 );else if (skip_prefix (format,"(objectsize)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),0 );else if (skip_prefix (format,"(stage)" , &format))strbuf_addf (&sb,"%d" ,ce_stage (ce));
@@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir, } } -static void show_ru_info (struct index_state *istate) +static void show_ru_info (struct repository *repo,struct index_state *istate) {struct string_list_item *item;
@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)if (!ui->mode[i])continue ;printf ("%s%06o %s %d\t" , tag_resolve_undo, ui->mode[i], -repo_find_unique_abbrev (the_repository, &ui->oid[i], abbrev), +repo_find_unique_abbrev (repo, &ui->oid[i], abbrev), i +1 );write_name (path); }
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,int cmd_ls_files (int argc,const char **argv,const char *cmd_prefix, - struct repository *repo UNUSED) +struct repository *repo) {int require_work_tree =0 , show_tag =0 , i;char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,show_usage_with_options_if_asked (argc, argv, ls_files_usage, builtin_ls_files_options); -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; prefix = cmd_prefix;if (prefix) prefix_len =strlen (prefix); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); -if (repo_read_index (the_repository) <0 ) +if (repo_read_index (repo) <0 )die ("index file corrupt" ); argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -724,7 +724,7 @@ int cmd_ls_files(int argc, max_prefix =common_prefix (&pathspec); max_prefix_len =get_common_prefix_len (max_prefix); -prune_index (the_repository->index, max_prefix, max_prefix_len); +prune_index (repo->index, max_prefix, max_prefix_len);/* Treat unmatching pathspec elements as errors */ if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,*/ if (show_stage || show_unmerged)die (_ ("options '%s' and '%s' cannot be used together" ),"ls-files --with-tree" ,"-s/-u" ); -overlay_tree_on_index (the_repository->index, with_tree, max_prefix); +overlay_tree_on_index (repo->index, with_tree, max_prefix); } -show_files (the_repository, &dir); +show_files (repo, &dir);if (show_resolve_undo) -show_ru_info (the_repository->index); +show_ru_info (repo, repo->index);if (ps_matched &&report_path_error (ps_matched, &pathspec)) {fprintf (stderr,"Did you forget to 'git add'?\n" );
t/t3004-ls-files-basic.sh +7 -0
@@ -34,6 +34,13 @@ test_expect_success 'ls-files -h in corrupt repository' ' test_grep "[Uu]sage: git ls-files " broken/usage ' +test_expect_success 'ls-files does not crash with -h' ' + test_expect_code 129 git ls-files -h >usage && + test_grep "[Uu]sage: git ls-files " usage && + test_expect_code 129 nongit git ls-files -h >usage && + test_grep "[Uu]sage: git ls-files " usage +' + test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' ' mkdir subs && ln -s nosuch link &&
Signature · 1 line
2.48.1
Re: [PATCH v3 6/8] builtin/ls-files: stop using `the_repository`
Quoted 3 lines · Usman Akinyemi
Pass the repository available in the calling context to both
expand_objectsize()andshow_ru_info()to remove their dependency on the globalthe_repositoryvariable.
Makes sense, and the changes look reasonable.
PATCH v3 7/8 builtin/for-each-ref: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/for-each-ref.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_for_each_ref() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/for-each-ref.c | 5 ++--- t/t6300-for-each-ref.sh | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-)
builtin/for-each-ref.c +2 -3
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "commit.h" #include "config.h"
@@ -20,7 +19,7 @@ static char const * const for_each_ref_usage[] = {int cmd_for_each_ref (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_sorting *sorting;struct string_list sorting_options =STRING_LIST_INIT_DUP ;
@@ -63,7 +62,7 @@ int cmd_for_each_ref(int argc, format.format ="%(objectname) %(objecttype)\t%(refname)" ; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );/* Set default (refname) sorting */ string_list_append (&sorting_options,"refname" );
t/t6300-for-each-ref.sh +7 -0
@@ -292,6 +292,13 @@ test_expect_success 'Check invalid atoms names are errors' ' test_must_fail git for-each-ref --format="%(INVALID)" refs/heads ' +test_expect_success 'for-each-ref does not crash with -h' ' + test_expect_code 129 git for-each-ref -h >usage && + test_grep "[Uu]sage: git for-each-ref " usage && + test_expect_code 129 nongit git for-each-ref -h >usage && + test_grep "[Uu]sage: git for-each-ref " usage +' + test_expect_success 'Check format specifiers are ignored in naming date atoms' ' git for-each-ref --format="%(authordate)" refs/heads && git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
Signature · 1 line
2.48.1
PATCH v3 8/8 builtin/checkout-index: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_checkout_index() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit.
Pass the repository available in the calling context to both checkout_all() and checkout_file() to remove their dependency on the global the_repository variable.
--- builtin/checkout-index.c | 43 ++++++++++++++++----------------- t/t2006-checkout-index-basic.sh | 7 ++++++ 2 files changed, 28 insertions(+), 22 deletions(-)
builtin/checkout-index.c +21 -22
@@ -5,7 +5,6 @@* */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -68,10 +67,10 @@ static void write_tempfile_record(const char *name, const char *prefix) } } -static int checkout_file (const char *name,const char *prefix) +static int checkout_file (struct repository *repo,const char *name,const char *prefix) {int namelen =strlen (name); -int pos =index_name_pos (the_repository->index, name, namelen); +int pos =index_name_pos (repo->index, name, namelen);int has_same_name =0 ;int is_file =0 ;int is_skipped =1 ;
@@ -81,8 +80,8 @@ static int checkout_file(const char *name, const char *prefix)if (pos <0 ) pos = -pos -1 ; -while (pos <the_repository->index->cache_nr) { -struct cache_entry *ce =the_repository->index->cache[pos]; +while (pos < repo->index->cache_nr) { +struct cache_entry *ce =repo->index->cache[pos];if (ce_namelen (ce) != namelen ||memcmp (ce->name, name, namelen))break ;
@@ -137,13 +136,13 @@ static int checkout_file(const char *name, const char *prefix)return -1 ; } -static int checkout_all (const char *prefix,int prefix_length) +static int checkout_all (struct repository *repo,const char *prefix,int prefix_length) {int i, errs =0 ;struct cache_entry *last_ce =NULL ; -for (i =0 ; i < the_repository->index->cache_nr ; i++) { -struct cache_entry *ce = the_repository->index->cache[i]; +for (i =0 ; i < repo->index->cache_nr ; i++) { +struct cache_entry *ce = repo->index->cache[i];if (S_ISSPARSEDIR (ce->ce_mode)) {if (!ce_skip_worktree (ce))
@@ -156,8 +155,8 @@ static int checkout_all(const char *prefix, int prefix_length)* first entry inside the expanded sparse directory). */ if (ignore_skip_worktree) { -ensure_full_index (the_repository->index); - ce = the_repository->index->cache[i]; +ensure_full_index (repo->index); + ce = repo->index->cache[i]; } }
@@ -213,7 +212,7 @@ static int option_parse_stage(const struct option *opt,int cmd_checkout_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i;struct lock_file lock_file =LOCK_INIT ;
@@ -253,19 +252,19 @@ int cmd_checkout_index(int argc,show_usage_with_options_if_asked (argc, argv, builtin_checkout_index_usage, builtin_checkout_index_options); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); prefix_length = prefix ?strlen (prefix) :0 ; -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; -if (repo_read_index (the_repository) <0 ) { +if (repo_read_index (repo) <0 ) {die ("invalid cache" ); } argc =parse_options (argc, argv, prefix, builtin_checkout_index_options, builtin_checkout_index_usage,0 ); - state.istate = the_repository->index; + state.istate = repo->index; state.force = force; state.quiet = quiet; state.not_new = not_new;
@@ -285,8 +284,8 @@ int cmd_checkout_index(int argc,*/ if (index_opt && !state.base_dir_len && !to_tempfile) { state.refresh_cache =1 ; - state.istate = the_repository->index; -repo_hold_locked_index (the_repository, &lock_file, + state.istate = repo->index; +repo_hold_locked_index (repo, &lock_file,LOCK_DIE_ON_ERROR ); }
@@ -304,7 +303,7 @@ int cmd_checkout_index(int argc,if (read_from_stdin)die ("git checkout-index: don't mix '--stdin' and explicit filenames" ); p =prefix_path (prefix, prefix_length, arg); - err |=checkout_file (p, prefix); + err |=checkout_file (repo, p, prefix);free (p); }
@@ -326,7 +325,7 @@ int cmd_checkout_index(int argc,strbuf_swap (&buf, &unquoted); } p =prefix_path (prefix, prefix_length, buf.buf); - err |=checkout_file (p, prefix); + err |=checkout_file (repo, p, prefix);free (p); }strbuf_release (&unquoted);
@@ -334,7 +333,7 @@ int cmd_checkout_index(int argc, }if (all) - err |=checkout_all (prefix, prefix_length); + err |=checkout_all (repo, prefix, prefix_length);if (pc_workers >1 ) err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +343,7 @@ int cmd_checkout_index(int argc,return 1 ;if (is_lock_file_locked (&lock_file) && -write_locked_index (the_repository->index, &lock_file,COMMIT_LOCK )) +write_locked_index (repo->index, &lock_file,COMMIT_LOCK ))die ("Unable to write new index file" );return 0 ; }
t/t2006-checkout-index-basic.sh +7 -0
@@ -21,6 +21,13 @@ test_expect_success 'checkout-index -h in broken repository' ' test_grep "[Uu]sage" broken/usage ' +test_expect_success 'checkout-index does not crash with -h' ' + test_expect_code 129 git checkout-index -h >usage && + test_grep "[Uu]sage: git checkout-index " usage && + test_expect_code 129 nongit git checkout-index -h >usage && + test_grep "[Uu]sage: git checkout-index " usage +' + test_expect_success 'checkout-index reports errors (cmdline)' ' test_must_fail git checkout-index -- does-not-exist 2>stderr && test_grep not.in.the.cache stderr
Signature · 1 line
2.48.1
Re: [PATCH v3 8/8] builtin/checkout-index: stop using `the_repository`
Quoted 11 lines · Usman Akinyemi
Remove
the_repositoryglobal variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".When
-his passed to the command outside a Git repository, therun_builtin()will call thecmd_checkout_index()function withreposet to NULL and then early in the function,show_usage_with_options_if_asked()call will give the options help and exit.Pass the repository available in the calling context to both
checkout_all()andcheckout_file()to remove their dependency on the globalthe_repositoryvariable.
Hmph, if we are passing anything down to these code paths, I would have expected that it would be an instance of "struct index_state".
Do these two helper functions need anything other than that from the repository instance?
Other than that, I think this step does look great.
Will queue.
Thanks.
Re: [PATCH v3 8/8] builtin/checkout-index: stop using `the_repository`
Quoted 15 lines · Junio C Hamano
Quoted 11 lines · Usman Akinyemi
Remove
the_repositoryglobal variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".When
-his passed to the command outside a Git repository, therun_builtin()will call thecmd_checkout_index()function withreposet to NULL and then early in the function,show_usage_with_options_if_asked()call will give the options help and exit.Pass the repository available in the calling context to both
checkout_all()andcheckout_file()to remove their dependency on the globalthe_repositoryvariable.
Quoted 5 lines
Hmph, if we are passing anything down to these code paths, I would have expected that it would be an instance of "struct
index_state".Do these two helper functions need anything other than that from the repository instance?
No, they do not. They could possibly do in the future and is there any reason why we might want to pass the "struct index_state" instead of the whole "struct repository" ?
Quoted 6 lines
Other than that, I think this step does look great.
Will queue.
Thanks.
Re: [PATCH v3 8/8] builtin/checkout-index: stop using `the_repository`
Quoted 8 lines · Usman Akinyemi
Quoted 5 lines
Hmph, if we are passing anything down to these code paths, I would have expected that it would be an instance of "struct
index_state".Do these two helper functions need anything other than that from the repository instance?
No, they do not. They could possibly do in the future and is there any reason why we might want to pass the "struct
index_state" instead of the whole "struct repository" ?
You are asking a wrong question.
When there are two things, A and B, where B is a piece of data with a smaller scope that is contained within A, unless your function needs to access parts of A that is impossible to access if you fed it only B, limiting the interface to the smallest piece necessary (in this case, B) is always the better design. A potential caller that only has B and not A can still call your function that takes B if you designed the API that way. If you insist that the caller pass A (and infer which B to use as part of A), a potential caller that only has B cannot call your function.
So, with that understanding of a general principle in mind, you need a stronger justification to pass the larger object A that goes against the best practice, saying "Even though we do not have to pass A because only B is necessary, we pass A because ...". And you just said there is no such reason why you need a repository and for these functions an index_state is sufficient.
There are many functions that only need "struct index_state" instance and obtains one from their callers. They do not have a repository object, other than the one that is referred to from the index_state (to keep track of which repository it originated from).
But this pointer is not designed to round-trip. There can be, and there indeed are, code paths that use multiple index_state instances associated with one repository. But a repository instance has only one ".index" member. It means if you pass a repository, you are making it impossible for your potential callers that has secondary index_state instances.
Re: [PATCH v3 8/8] builtin/checkout-index: stop using `the_repository`
Quoted 36 lines · Junio C Hamano
Quoted 8 lines · Usman Akinyemi
Quoted 5 lines
Hmph, if we are passing anything down to these code paths, I would have expected that it would be an instance of "struct
index_state".Do these two helper functions need anything other than that from the repository instance?
No, they do not. They could possibly do in the future and is there any reason why we might want to pass the "struct
index_state" instead of the whole "struct repository" ?You are asking a wrong question.
When there are two things, A and B, where B is a piece of data with a smaller scope that is contained within A, unless your function needs to access parts of A that is impossible to access if you fed it only B, limiting the interface to the smallest piece necessary (in this case, B) is always the better design. A potential caller that only has B and not A can still call your function that takes B if you designed the API that way. If you insist that the caller pass A (and infer which B to use as part of A), a potential caller that only has B cannot call your function.
So, with that understanding of a general principle in mind, you need a stronger justification to pass the larger object A that goes against the best practice, saying "Even though we do not have to pass A because only B is necessary, we pass A because ...". And you just said there is no such reason why you need a repository and for these functions an
index_stateis sufficient.There are many functions that only need "struct
index_state" instance and obtains one from their callers. They do not have a repository object, other than the one that is referred to from theindex_state(to keep track of which repository it originated from).
Quoted 6 lines
But this pointer is not designed to round-trip. There can be, and there indeed are, code paths that use multiple
index_stateinstances associated with one repository. But a repository instance has only one ".index" member. It means if you pass a repository, you are making it impossible for your potential callers that has secondaryindex_stateinstances.
Thanks for the explanation.
I did not know or think about this before.
I will make changes to this in the next iteration.
Thank you.
Quoted 1 line
v4 cover stop using the_repository global variable.
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in builtin commands.
These sets of commands are commands that use only RUN_SETUP macro in "git.c". Basically, When -h is passed to any of this command outside a Git repository, the run_builtin() will call the cmd_x() function (where x is any of the command from the sets of builtin commands that the_repository is removed from) with repo set to NULL and then early in the function, parse_options() or show_usage_with_options_if_asked() call will give the options help and exit.
Some functions also uses the_repository global internally, so, let's let's refactor them and pass struct repo as one of the argument. Some functions only need an instance of "struct index_state", so, let's pass it to such functions.
As the repo value can be NULL if a builtin command is run outside any repository. The current implementation of repo_config() will fail if repo is NULL.
If the repo is NULL, the repo_config() can ignore the repository configuration but it should read the other configuration sources like the system-side configuration instead of failing.
Teach the repo_config() to allow repo to be NULL by calling the read_very_early_config() which read config but only enumerate system and global settings. This make it possible for us to savely replace git_config() with repo_config().
Changes since v3
================
- Add a comment to describe why we teach repo_config() to take NULL repo value and what we intend to achieve.
- Pass "struct index_state" instead of repo in functions inside "builtin/checkout-index.c"
- Fix some typo.
Usman Akinyemi (8):
config: teach repo_config to allow repo to be NULL builtin/verify-tag: stop using the_repository builtin/verify-commit: stop using the_repository builtin/send-pack: stop using the_repository builtin/pack-refs: stop using the_repository builtin/ls-files: stop using the_repository builtin/for-each-ref: stop using the_repository builtin/checkout-index: stop using the_repository
builtin/checkout-index.c | 43 ++++++++++++++++----------------- builtin/for-each-ref.c | 5 ++-- builtin/ls-files.c | 32 ++++++++++++------------ builtin/pack-refs.c | 8 +++--- builtin/send-pack.c | 7 +++--- builtin/verify-commit.c | 13 +++++----- builtin/verify-tag.c | 7 +++--- config.c | 4 +++ config.h | 9 +++++++ t/t0610-reftable-basics.sh | 7 ++++++ t/t2006-checkout-index-basic.sh | 7 ++++++ t/t3004-ls-files-basic.sh | 7 ++++++ t/t5400-send-pack.sh | 7 ++++++ t/t6300-for-each-ref.sh | 7 ++++++ t/t7030-verify-tag.sh | 7 ++++++ t/t7510-signed-commit.sh | 7 ++++++ 16 files changed, 116 insertions(+), 61 deletions(-)
Range-diff versus v3:
1: a5c69f3753 ! 1: f53677cbd6 config: teach repo_config to allow repo to be NULL
@@ config.h: void read_very_early_config(config_fn_t cb, void *data);
* value is left at the end).
*
+ * In cases where the repository variable is NULL, repo_config() will
-+ * call read_early_config().
++ * skip the per-repository config but retain system and global configs ++ * by calling read_very_early_config() which also ignores one-time ++ * overrides like "git -c var=val". This is to support handling "git foo -h" ++ * (which lets git.c:run_builtin() to pass NULL and have the cmd_foo() ++ * call repo_config() before calling parse_options() to notice "-h", give ++ * help and exit) for a command that ordinarily require a repository ++ * so this limitation may be OK (but if needed you are welcome to fix it).
+ *
* Unlike git_config_from_file(), this function respects includes. */
2: dfa0da4061 = 2: 6560218b7a builtin/verify-tag: stop using `the_repository`
3: ade2d026cb = 3: 22681bad00 builtin/verify-commit: stop using `the_repository`
4: e3b58bc6cf = 4: a2a97b10a6 builtin/send-pack: stop using `the_repository`
5: b11e99627c = 5: b88e45e795 builtin/pack-refs: stop using `the_repository`
6: 51c80f9273 = 6: d976fab012 builtin/ls-files: stop using `the_repository`
7: 63bb89291f = 7: 6a44666310 builtin/for-each-ref: stop using `the_repository`
8: 8dfe6b40c8 ! 8: 677e088e55 builtin/checkout-index: stop using `the_repository`
@@ Commit message
set to NULL and then early in the function, `show_usage_with_options_if_asked()`
call will give the options help and exit.
- Pass the repository available in the calling context to both `checkout_all()`
- and `checkout_file()` to remove their dependency on the global
- `the_repository` variable.
+ Pass an instance of "struct index_state" available in the calling
+ context to both `checkout_all()` and `checkout_file()` to remove their
+ dependency on the global `the_repository` variable.
Mentored-by: Christian Couder <[email protected]>
Signed-off-by: Usman Akinyemi <[email protected]>
@@ builtin/checkout-index.c: static void write_tempfile_record(const char *name, co
}
-static int checkout_file(const char *name, const char *prefix)
-+static int checkout_file(struct repository *repo, const char *name, const char *prefix)
++static int checkout_file(struct index_state *index, const char *name, const char *prefix)
{
int namelen = strlen(name);
- int pos = index_name_pos(the_repository->index, name, namelen);
-+ int pos = index_name_pos(repo->index, name, namelen);
++ int pos = index_name_pos(index, name, namelen);
int has_same_name = 0;
int is_file = 0;
int is_skipped = 1;
@@ builtin/checkout-index.c: static int checkout_file(const char *name, const char
- while (pos <the_repository->index->cache_nr) {
- struct cache_entry *ce =the_repository->index->cache[pos];
-+ while (pos < repo->index->cache_nr) {
-+ struct cache_entry *ce =repo->index->cache[pos];
++ while (pos < index->cache_nr) {
++ struct cache_entry *ce = index->cache[pos];
if (ce_namelen(ce) != namelen ||
memcmp(ce->name, name, namelen))
break;
@@ builtin/checkout-index.c: static int checkout_file(const char *name, const char
}
-static int checkout_all(const char *prefix, int prefix_length)
-+static int checkout_all(struct repository *repo, const char *prefix, int prefix_length)
++static int checkout_all(struct index_state *index, const char *prefix, int prefix_length)
{
int i, errs = 0;
struct cache_entry *last_ce = NULL;
- for (i = 0; i < the_repository->index->cache_nr ; i++) {
- struct cache_entry *ce = the_repository->index->cache[i];
-+ for (i = 0; i < repo->index->cache_nr ; i++) {
-+ struct cache_entry *ce = repo->index->cache[i];
++ for (i = 0; i < index->cache_nr ; i++) {
++ struct cache_entry *ce = index->cache[i];
if (S_ISSPARSEDIR(ce->ce_mode)) {
if (!ce_skip_worktree(ce))
@@ builtin/checkout-index.c: static int checkout_all(const char *prefix, int prefix
if (ignore_skip_worktree) {
- ensure_full_index(the_repository->index);
- ce = the_repository->index->cache[i];
-+ ensure_full_index(repo->index);
-+ ce = repo->index->cache[i];
++ ensure_full_index(index);
++ ce = index->cache[i];
}
}
@@ builtin/checkout-index.c: int cmd_checkout_index(int argc,
die("git checkout-index: don't mix '--stdin' and explicit filenames");
p = prefix_path(prefix, prefix_length, arg);
- err |= checkout_file(p, prefix);
-+ err |= checkout_file(repo, p, prefix);
++ err |= checkout_file(repo->index, p, prefix);
free(p);
}
@@ builtin/checkout-index.c: int cmd_checkout_index(int argc,
}
p = prefix_path(prefix, prefix_length, buf.buf);
- err |= checkout_file(p, prefix);
-+ err |= checkout_file(repo, p, prefix);
++ err |= checkout_file(repo->index, p, prefix);
free(p);
}
strbuf_release(&unquoted);
@@ builtin/checkout-index.c: int cmd_checkout_index(int argc,
if (all)
- err |= checkout_all(prefix, prefix_length);
-+ err |= checkout_all(repo, prefix, prefix_length);
++ err |= checkout_all(repo->index, prefix, prefix_length);
if (pc_workers > 1)
err |= run_parallel_checkout(&state, pc_workers, pc_threshold,Signature · 1 line
2.48.1
PATCH v4 1/8 config: teach repo_config to allow `repo` to be NULL
The repo value can be NULL if a builtin command is run outside any repository. The current implementation of repo_config() will fail if repo is NULL.
If the repo is NULL the repo_config() can ignore the repository configuration but it should read the other configuration sources like the system-side configuration instead of failing.
Teach the repo_config() to allow repo to be NULL by calling the read_very_early_config() which read config but only enumerate system and global settings.
This will be useful in the following commits.
--- config.c | 4 ++++ config.h | 9 +++++++++ 2 files changed, 13 insertions(+)
config.c +4 -0
@@ -2526,6 +2526,10 @@ void repo_config_clear(struct repository *repo)void repo_config (struct repository *repo,config_fn_t fn,void *data) { +if (!repo) { +read_very_early_config (fn, data); +return ; + }git_config_check_init (repo);configset_iter (repo->config, fn, data); }
config.h +9 -0
@@ -219,6 +219,15 @@ void read_very_early_config(config_fn_t cb, void *data);* repo-specific one; by overwriting, the higher-priority repo-specific * value is left at the end). * +* In cases where the repository variable is NULL, repo_config() will +* skip the per-repository config but retain system and global configs +* by calling read_very_early_config() which also ignores one-time +* overrides like "git -c var=val". This is to support handling "git foo -h" +* (which lets git.c:run_builtin() to pass NULL and have the cmd_foo() +* call repo_config() before calling parse_options() to notice "-h", give +* help and exit) for a command that ordinarily require a repository +* so this limitation may be OK (but if needed you are welcome to fix it). +* * Unlike git_config_from_file(), this function respects includes. */ void repo_config (struct repository *r,config_fn_t fn,void *);
Signature · 1 line
2.48.1
PATCH v4 2/8 builtin/verify-tag: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-tag.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_tag() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/verify-tag.c | 7 +++---- t/t7030-verify-tag.sh | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-)
builtin/verify-tag.c +3 -4
@@ -5,7 +5,6 @@* * Based on git-verify-tag.sh */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -23,7 +22,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -35,7 +34,7 @@ int cmd_verify_tag(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_tag_options, verify_tag_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -56,7 +55,7 @@ int cmd_verify_tag(int argc,struct object_id oid;const char *name = argv[i++]; -if (repo_get_oid (the_repository, name, &oid)) { +if (repo_get_oid (repo, name, &oid)) { had_error = !!error ("tag '%s' not found." , name);continue ; }
t/t7030-verify-tag.sh +7 -0
@@ -7,6 +7,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME . ./test-lib.sh . "$TEST_DIRECTORY/lib-gpg.sh" +test_expect_success GPG 'verify-tag does not crash with -h' ' + test_expect_code 129 git verify-tag -h >usage && + test_grep "[Uu]sage: git verify-tag " usage && + test_expect_code 129 nongit git verify-tag -h >usage && + test_grep "[Uu]sage: git verify-tag " usage +' + test_expect_success GPG 'create signed tags' ' echo 1 >file && git add file && test_tick && git commit -m initial &&
Signature · 1 line
2.48.1
PATCH v4 3/8 builtin/verify-commit: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/verify-commit.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_verify_commit() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
Pass the repository available in the calling context to verify_commit() to remove it's dependency on the global the_repository variable.
--- builtin/verify-commit.c | 13 ++++++------- t/t7510-signed-commit.sh | 7 +++++++ 2 files changed, 13 insertions(+), 7 deletions(-)
builtin/verify-commit.c +6 -7
@@ -5,7 +5,6 @@* * Based on git-verify-tag */ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)return ret; } -static int verify_commit (const char *name,unsigned flags) +static int verify_commit (struct repository *repo,const char *name,unsigned flags) {struct object_id oid;struct object *obj; -if (repo_get_oid (the_repository, name, &oid)) +if (repo_get_oid (repo, name, &oid))return error ("commit '%s' not found." , name); - obj =parse_object (the_repository, &oid); + obj =parse_object (repo, &oid);if (!obj)return error ("%s: unable to read file." , name);if (obj->type !=OBJ_COMMIT )
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)int cmd_verify_commit (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -64,7 +63,7 @@ int cmd_verify_commit(int argc, OPT_END() }; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_commit_options, verify_commit_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -78,7 +77,7 @@ int cmd_verify_commit(int argc,* was received in the process of writing the gpg input: */ signal (SIGPIPE ,SIG_IGN );while (i < argc) -if (verify_commit (argv[i++], flags)) +if (verify_commit (repo, argv[i++], flags)) had_error =1 ;return had_error; }
t/t7510-signed-commit.sh +7 -0
@@ -8,6 +8,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME GNUPGHOME_NOT_USED=$GNUPGHOME . "$TEST_DIRECTORY/lib-gpg.sh" +test_expect_success GPG 'verify-commit does not crash with -h' ' + test_expect_code 129 git verify-commit -h >usage && + test_grep "[Uu]sage: git verify-commit " usage && + test_expect_code 129 nongit git verify-commit -h >usage && + test_grep "[Uu]sage: git verify-commit " usage +' + test_expect_success GPG 'create signed commits' ' test_oid_cache <<-\EOF && header sha1:gpgsig
Signature · 1 line
2.48.1
PATCH v4 4/8 builtin/send-pack: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/send-pack.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_send_pack() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/send-pack.c | 7 +++---- t/t5400-send-pack.sh | 7 +++++++ 2 files changed, 10 insertions(+), 4 deletions(-)
builtin/send-pack.c +3 -4
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "hex.h"
@@ -151,7 +150,7 @@ static int send_pack_config(const char *k, const char *v,int cmd_send_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct refspec rs =REFSPEC_INIT_PUSH ;const char *remote_name =NULL ;
@@ -212,7 +211,7 @@ int cmd_send_pack(int argc, OPT_END() }; -git_config (send_pack_config,NULL ); +repo_config (repo, send_pack_config,NULL ); argc =parse_options (argc, argv, prefix, options, send_pack_usage,0 );if (argc >0 ) { dest = argv[0 ];
@@ -317,7 +316,7 @@ int cmd_send_pack(int argc,set_ref_status_for_push (remote_refs, args.send_mirror, args.force_update); - ret =send_pack (the_repository, &args, fd, conn, remote_refs, &extra_have); + ret =send_pack (repo, &args, fd, conn, remote_refs, &extra_have);if (helper_status)print_helper_status (remote_refs);
t/t5400-send-pack.sh +7 -0
@@ -55,6 +55,13 @@ test_expect_success setup ' echo Rebase && git log' +test_expect_success 'send-pack does not crash with -h' ' + test_expect_code 129 git send-pack -h >usage && + test_grep "[Uu]sage: git send-pack " usage && + test_expect_code 129 nongit git send-pack -h >usage && + test_grep "[Uu]sage: git send-pack " usage +' + test_expect_success 'pack the source repository' ' git repack -a -d && git prune
Signature · 1 line
2.48.1
PATCH v4 5/8 builtin/pack-refs: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/pack-refs.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_pack_refs() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/pack-refs.c | 8 +++----- t/t0610-reftable-basics.sh | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-)
builtin/pack-refs.c +3 -5
@@ -1,5 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE -#include "builtin.h" #include "config.h" #include "gettext.h"
@@ -15,7 +13,7 @@ static char const * const pack_refs_usage[] = {int cmd_pack_refs (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_exclusions excludes =REF_EXCLUSIONS_INIT ;struct string_list included_refs =STRING_LIST_INIT_NODUP ;
@@ -39,7 +37,7 @@ int cmd_pack_refs(int argc, N_("references to exclude")),OPT_END (), }; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );if (parse_options (argc, argv, prefix, opts, pack_refs_usage,0 ))usage_with_options (pack_refs_usage, opts);
@@ -52,7 +50,7 @@ int cmd_pack_refs(int argc,if (!pack_refs_opts.includes->nr)string_list_append (pack_refs_opts.includes,"refs/tags/*" ); - ret =refs_pack_refs (get_main_ref_store (the_repository), &pack_refs_opts); + ret =refs_pack_refs (get_main_ref_store (repo), &pack_refs_opts);clear_ref_exclusions (&excludes);string_list_clear (&included_refs,0 );
t/t0610-reftable-basics.sh +7 -0
@@ -14,6 +14,13 @@ export GIT_TEST_DEFAULT_REF_FORMAT INVALID_OID=$(test_oid 001) +test_expect_success 'pack-refs does not crash with -h' ' + test_expect_code 129 git pack-refs -h >usage && + test_grep "[Uu]sage: git pack-refs " usage && + test_expect_code 129 nongit git pack-refs -h >usage && + test_grep "[Uu]sage: git pack-refs " usage +' + test_expect_success 'init: creates basic reftable structures' ' test_when_finished "rm -rf repo" && git init repo &&
Signature · 1 line
2.48.1
PATCH v4 6/8 builtin/ls-files: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/ls-files.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_ls_files() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit.
Pass the repository available in the calling context to both expand_objectsize() and show_ru_info() to remove their dependency on the global the_repository variable.
--- builtin/ls-files.c | 32 ++++++++++++++++---------------- t/t3004-ls-files-basic.sh | 7 +++++++ 2 files changed, 23 insertions(+), 16 deletions(-)
builtin/ls-files.c +16 -16
@@ -6,7 +6,6 @@* Copyright (C) Linus Torvalds, 2005 */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -245,12 +244,13 @@ static void show_submodule(struct repository *superproject,repo_clear (&subrepo); } -static void expand_objectsize (struct strbuf *line,const struct object_id *oid, +static void expand_objectsize (struct repository *repo,struct strbuf *line, +const struct object_id *oid,const enum object_type type,unsigned int padded) {if (type ==OBJ_BLOB ) {unsigned long size; -if (oid_object_info (the_repository, oid, &size) <0 ) +if (oid_object_info (repo, oid, &size) <0 )die (_ ("could not get object info about '%s'" ),oid_to_hex (oid));if (padded)
@@ -283,10 +283,10 @@ static void show_ce_fmt(struct repository *repo, const struct cache_entry *ce,else if (skip_prefix (format,"(objecttype)" , &format))strbuf_addstr (&sb,type_name (object_type (ce->ce_mode)));else if (skip_prefix (format,"(objectsize:padded)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),1 );else if (skip_prefix (format,"(objectsize)" , &format)) -expand_objectsize (&sb, &ce->oid, +expand_objectsize (repo, &sb, &ce->oid,object_type (ce->ce_mode),0 );else if (skip_prefix (format,"(stage)" , &format))strbuf_addf (&sb,"%d" ,ce_stage (ce));
@@ -348,7 +348,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir, } } -static void show_ru_info (struct index_state *istate) +static void show_ru_info (struct repository *repo,struct index_state *istate) {struct string_list_item *item;
@@ -370,7 +370,7 @@ static void show_ru_info(struct index_state *istate)if (!ui->mode[i])continue ;printf ("%s%06o %s %d\t" , tag_resolve_undo, ui->mode[i], -repo_find_unique_abbrev (the_repository, &ui->oid[i], abbrev), +repo_find_unique_abbrev (repo, &ui->oid[i], abbrev), i +1 );write_name (path); }
@@ -567,7 +567,7 @@ static int option_parse_exclude_standard(const struct option *opt,int cmd_ls_files (int argc,const char **argv,const char *cmd_prefix, - struct repository *repo UNUSED) +struct repository *repo) {int require_work_tree =0 , show_tag =0 , i;char *max_prefix;
@@ -647,15 +647,15 @@ int cmd_ls_files(int argc,show_usage_with_options_if_asked (argc, argv, ls_files_usage, builtin_ls_files_options); -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; prefix = cmd_prefix;if (prefix) prefix_len =strlen (prefix); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); -if (repo_read_index (the_repository) <0 ) +if (repo_read_index (repo) <0 )die ("index file corrupt" ); argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -724,7 +724,7 @@ int cmd_ls_files(int argc, max_prefix =common_prefix (&pathspec); max_prefix_len =get_common_prefix_len (max_prefix); -prune_index (the_repository->index, max_prefix, max_prefix_len); +prune_index (repo->index, max_prefix, max_prefix_len);/* Treat unmatching pathspec elements as errors */ if (pathspec.nr && error_unmatch)
@@ -748,13 +748,13 @@ int cmd_ls_files(int argc,*/ if (show_stage || show_unmerged)die (_ ("options '%s' and '%s' cannot be used together" ),"ls-files --with-tree" ,"-s/-u" ); -overlay_tree_on_index (the_repository->index, with_tree, max_prefix); +overlay_tree_on_index (repo->index, with_tree, max_prefix); } -show_files (the_repository, &dir); +show_files (repo, &dir);if (show_resolve_undo) -show_ru_info (the_repository->index); +show_ru_info (repo, repo->index);if (ps_matched &&report_path_error (ps_matched, &pathspec)) {fprintf (stderr,"Did you forget to 'git add'?\n" );
t/t3004-ls-files-basic.sh +7 -0
@@ -34,6 +34,13 @@ test_expect_success 'ls-files -h in corrupt repository' ' test_grep "[Uu]sage: git ls-files " broken/usage ' +test_expect_success 'ls-files does not crash with -h' ' + test_expect_code 129 git ls-files -h >usage && + test_grep "[Uu]sage: git ls-files " usage && + test_expect_code 129 nongit git ls-files -h >usage && + test_grep "[Uu]sage: git ls-files " usage +' + test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' ' mkdir subs && ln -s nosuch link &&
Signature · 1 line
2.48.1
PATCH v4 7/8 builtin/for-each-ref: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/for-each-ref.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_for_each_ref() function with repo set to NULL and then early in the function, parse_options() call will give the options help and exit.
--- builtin/for-each-ref.c | 5 ++--- t/t6300-for-each-ref.sh | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-)
builtin/for-each-ref.c +2 -3
@@ -1,4 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "commit.h" #include "config.h"
@@ -20,7 +19,7 @@ static char const * const for_each_ref_usage[] = {int cmd_for_each_ref (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct ref_sorting *sorting;struct string_list sorting_options =STRING_LIST_INIT_DUP ;
@@ -63,7 +62,7 @@ int cmd_for_each_ref(int argc, format.format ="%(objectname) %(objecttype)\t%(refname)" ; -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL );/* Set default (refname) sorting */ string_list_append (&sorting_options,"refname" );
t/t6300-for-each-ref.sh +7 -0
@@ -292,6 +292,13 @@ test_expect_success 'Check invalid atoms names are errors' ' test_must_fail git for-each-ref --format="%(INVALID)" refs/heads ' +test_expect_success 'for-each-ref does not crash with -h' ' + test_expect_code 129 git for-each-ref -h >usage && + test_grep "[Uu]sage: git for-each-ref " usage && + test_expect_code 129 nongit git for-each-ref -h >usage && + test_grep "[Uu]sage: git for-each-ref " usage +' + test_expect_success 'Check format specifiers are ignored in naming date atoms' ' git for-each-ref --format="%(authordate)" refs/heads && git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
Signature · 1 line
2.48.1
PATCH v4 8/8 builtin/checkout-index: stop using `the_repository`
In reply to Usman AkinyemiRemove the_repository global variable in favor of the repository argument that gets passed in "builtin/checkout-index.c".
When -h is passed to the command outside a Git repository, the run_builtin() will call the cmd_checkout_index() function with repo set to NULL and then early in the function, show_usage_with_options_if_asked() call will give the options help and exit.
Pass an instance of "struct index_state" available in the calling context to both checkout_all() and checkout_file() to remove their dependency on the global the_repository variable.
--- builtin/checkout-index.c | 43 ++++++++++++++++----------------- t/t2006-checkout-index-basic.sh | 7 ++++++ 2 files changed, 28 insertions(+), 22 deletions(-)
builtin/checkout-index.c +21 -22
@@ -5,7 +5,6 @@* */ -#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -68,10 +67,10 @@ static void write_tempfile_record(const char *name, const char *prefix) } } -static int checkout_file (const char *name,const char *prefix) +static int checkout_file (struct index_state *index,const char *name,const char *prefix) {int namelen =strlen (name); -int pos =index_name_pos (the_repository->index, name, namelen); +int pos =index_name_pos (index, name, namelen);int has_same_name =0 ;int is_file =0 ;int is_skipped =1 ;
@@ -81,8 +80,8 @@ static int checkout_file(const char *name, const char *prefix)if (pos <0 ) pos = -pos -1 ; -while (pos <the_repository->index->cache_nr) { -struct cache_entry *ce =the_repository->index->cache[pos]; +while (pos < index->cache_nr) { +struct cache_entry *ce = index->cache[pos];if (ce_namelen (ce) != namelen ||memcmp (ce->name, name, namelen))break ;
@@ -137,13 +136,13 @@ static int checkout_file(const char *name, const char *prefix)return -1 ; } -static int checkout_all (const char *prefix,int prefix_length) +static int checkout_all (struct index_state *index,const char *prefix,int prefix_length) {int i, errs =0 ;struct cache_entry *last_ce =NULL ; -for (i =0 ; i < the_repository->index->cache_nr ; i++) { -struct cache_entry *ce = the_repository->index->cache[i]; +for (i =0 ; i < index->cache_nr ; i++) { +struct cache_entry *ce = index->cache[i];if (S_ISSPARSEDIR (ce->ce_mode)) {if (!ce_skip_worktree (ce))
@@ -156,8 +155,8 @@ static int checkout_all(const char *prefix, int prefix_length)* first entry inside the expanded sparse directory). */ if (ignore_skip_worktree) { -ensure_full_index (the_repository->index); - ce = the_repository->index->cache[i]; +ensure_full_index (index); + ce = index->cache[i]; } }
@@ -213,7 +212,7 @@ static int option_parse_stage(const struct option *opt,int cmd_checkout_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int i;struct lock_file lock_file =LOCK_INIT ;
@@ -253,19 +252,19 @@ int cmd_checkout_index(int argc,show_usage_with_options_if_asked (argc, argv, builtin_checkout_index_usage, builtin_checkout_index_options); -git_config (git_default_config,NULL ); +repo_config (repo, git_default_config,NULL ); prefix_length = prefix ?strlen (prefix) :0 ; -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; +prepare_repo_settings (repo); + repo->settings.command_requires_full_index =0 ; -if (repo_read_index (the_repository) <0 ) { +if (repo_read_index (repo) <0 ) {die ("invalid cache" ); } argc =parse_options (argc, argv, prefix, builtin_checkout_index_options, builtin_checkout_index_usage,0 ); - state.istate = the_repository->index; + state.istate = repo->index; state.force = force; state.quiet = quiet; state.not_new = not_new;
@@ -285,8 +284,8 @@ int cmd_checkout_index(int argc,*/ if (index_opt && !state.base_dir_len && !to_tempfile) { state.refresh_cache =1 ; - state.istate = the_repository->index; -repo_hold_locked_index (the_repository, &lock_file, + state.istate = repo->index; +repo_hold_locked_index (repo, &lock_file,LOCK_DIE_ON_ERROR ); }
@@ -304,7 +303,7 @@ int cmd_checkout_index(int argc,if (read_from_stdin)die ("git checkout-index: don't mix '--stdin' and explicit filenames" ); p =prefix_path (prefix, prefix_length, arg); - err |=checkout_file (p, prefix); + err |=checkout_file (repo->index, p, prefix);free (p); }
@@ -326,7 +325,7 @@ int cmd_checkout_index(int argc,strbuf_swap (&buf, &unquoted); } p =prefix_path (prefix, prefix_length, buf.buf); - err |=checkout_file (p, prefix); + err |=checkout_file (repo->index, p, prefix);free (p); }strbuf_release (&unquoted);
@@ -334,7 +333,7 @@ int cmd_checkout_index(int argc, }if (all) - err |=checkout_all (prefix, prefix_length); + err |=checkout_all (repo->index, prefix, prefix_length);if (pc_workers >1 ) err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +343,7 @@ int cmd_checkout_index(int argc,return 1 ;if (is_lock_file_locked (&lock_file) && -write_locked_index (the_repository->index, &lock_file,COMMIT_LOCK )) +write_locked_index (repo->index, &lock_file,COMMIT_LOCK ))die ("Unable to write new index file" );return 0 ; }
t/t2006-checkout-index-basic.sh +7 -0
@@ -21,6 +21,13 @@ test_expect_success 'checkout-index -h in broken repository' ' test_grep "[Uu]sage" broken/usage ' +test_expect_success 'checkout-index does not crash with -h' ' + test_expect_code 129 git checkout-index -h >usage && + test_grep "[Uu]sage: git checkout-index " usage && + test_expect_code 129 nongit git checkout-index -h >usage && + test_grep "[Uu]sage: git checkout-index " usage +' + test_expect_success 'checkout-index reports errors (cmdline)' ' test_must_fail git checkout-index -- does-not-exist 2>stderr && test_grep not.in.the.cache stderr
Signature · 1 line
2.48.1