config: make git_config_set die on failure
PATCH v3 1/1 config: make git_config_set die on failure
Failure to write the configuration file may be caused by multiple conditions, the most common one being the case where the configuration is locked because of a leftover lock file or because another process is currently writing to it. We used to ignore those errors in many cases, possibly leading to inconsistently configured repositories. More often than not git even pretended that everything was fine and that the settings have been applied when indeed they were not.
Handle these failures by dying by default whenever an error is occured by the git_config_set family of functions. Introduce a new set of functions git_config_set_gently that instead returns an error code for the cases where we are required to explicitly handle configuration errors.
---
In contrast to the old version ([1]) this version of the patch introduces a set of functions git_config_set_gently instead of git_config_set_or_die, thus going the other route of dying by default.
It first seemed that going the default-die route instead of or_die-wrappers might prove to be more contained and easier to grasp. Seeing the result, though, I'm not convinced of this anymore. This is a minimal compiling example without even caring about test failures, which do occur now.
Previously in v2 we had a diffstat of +127/-55 lines where we now have +88/-82, which amounts to roughly the same amount of lines changed. The previous approach, though, was much easier to grasp in my opinion as nearly all changes could be split up instead of requiring one big change to make it compile. As said before, this does not yet include the changes required for handling test failures, thus the patch is expected to become bigger rather than smaller.
Furthermore, I do think it's more explicit what the functions are doing when there is a 'or_die' suffix. Without this suffix it may be unexpected that the functions simply abort the program whenever an error occurs.
I'd thus prefer to use the old style used in version 2. I could try to make the second patch ([2]) a little bit smaller by avoiding all the fuss about passing up the error code only to die a little later.
Opinions?
Patrick
[1]: http://article.gmane.org/gmane.comp.version-control.git/285000
[2]: http://thread.gmane.org/gmane.comp.version-control.git/285002
builtin/branch.c | 2 +- builtin/clone.c | 2 +- builtin/config.c | 28 +++++++++++------------ builtin/remote.c | 70 ++++++++++++++++++-------------------------------------- cache.h | 12 ++++++---- config.c | 46 ++++++++++++++++++++++++++++++------- submodule.c | 10 ++++---- 7 files changed, 88 insertions(+), 82 deletions(-)
builtin/branch.c +1 -1
@@ -595,7 +595,7 @@ static int edit_branch_description(const char *branch_name)strbuf_stripspace (&buf,1 );strbuf_addf (&name,"branch.%s.description" , branch_name); - status =git_config_set (name.buf, buf.len ? buf.buf :NULL ); +git_config_set (name.buf, buf.len ? buf.buf :NULL );strbuf_release (&name);strbuf_release (&buf);
builtin/clone.c +1 -1
@@ -735,7 +735,7 @@ static int checkout(void)static int write_one_config (const char *key,const char *value,void *data) { -return git_config_set_multivar (key, value ? value :"true" ,"^$" ,0 ); +return git_config_set_multivar_gently (key, value ? value :"true" ,"^$" ,0 ); }static void write_config (struct string_list *config)
builtin/config.c +14 -14
@@ -582,7 +582,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)check_write ();check_argc (argc,2 ,2 ); value =normalize_value (argv[0 ], argv[1 ]); - ret =git_config_set_in_file (given_config_source.file, argv[0 ], value); + ret =git_config_set_in_file_gently (given_config_source.file, argv[0 ], value);if (ret ==CONFIG_NOTHING_SET )error ("cannot overwrite multiple values with a single value\n" " Use a regexp, --add or --replace-all to change %s." , argv[0 ]);
@@ -592,23 +592,23 @@ int cmd_config(int argc, const char **argv, const char *prefix)check_write ();check_argc (argc,2 ,3 ); value =normalize_value (argv[0 ], argv[1 ]); -return git_config_set_multivar_in_file (given_config_source.file, - argv[0 ], value, argv[2 ],0 ); +return git_config_set_multivar_in_file_gently (given_config_source.file, + argv[0 ], value, argv[2 ],0 ); }else if (actions ==ACTION_ADD ) {check_write ();check_argc (argc,2 ,2 ); value =normalize_value (argv[0 ], argv[1 ]); -return git_config_set_multivar_in_file (given_config_source.file, - argv[0 ], value, -CONFIG_REGEX_NONE ,0 ); +return git_config_set_multivar_in_file_gently (given_config_source.file, + argv[0 ], value, +CONFIG_REGEX_NONE ,0 ); }else if (actions ==ACTION_REPLACE_ALL ) {check_write ();check_argc (argc,2 ,3 ); value =normalize_value (argv[0 ], argv[1 ]); -return git_config_set_multivar_in_file (given_config_source.file, - argv[0 ], value, argv[2 ],1 ); +return git_config_set_multivar_in_file_gently (given_config_source.file, + argv[0 ], value, argv[2 ],1 ); }else if (actions ==ACTION_GET ) {check_argc (argc,1 ,2 );
@@ -634,17 +634,17 @@ int cmd_config(int argc, const char **argv, const char *prefix)check_write ();check_argc (argc,1 ,2 );if (argc ==2 ) -return git_config_set_multivar_in_file (given_config_source.file, - argv[0 ],NULL , argv[1 ],0 ); +return git_config_set_multivar_in_file_gently (given_config_source.file, + argv[0 ],NULL , argv[1 ],0 );else -return git_config_set_in_file (given_config_source.file, - argv[0 ],NULL ); +return git_config_set_in_file_gently (given_config_source.file, + argv[0 ],NULL ); }else if (actions ==ACTION_UNSET_ALL ) {check_write ();check_argc (argc,1 ,2 ); -return git_config_set_multivar_in_file (given_config_source.file, - argv[0 ],NULL , argv[1 ],1 ); +return git_config_set_multivar_in_file_gently (given_config_source.file, + argv[0 ],NULL , argv[1 ],1 ); }else if (actions ==ACTION_RENAME_SECTION ) {int ret;
builtin/remote.c +22 -48
164 lines not on this page · raw message
cache.h +8 -4
@@ -1507,12 +1507,16 @@ extern int git_config_bool(const char *, const char *);extern int git_config_maybe_bool (const char *,const char *);extern int git_config_string (const char **,const char *,const char *);extern int git_config_pathname (const char **,const char *,const char *); -extern int git_config_set_in_file (const char *,const char *,const char *); -extern int git_config_set (const char *,const char *); +extern void git_config_set_in_file (const char *,const char *,const char *); +extern int git_config_set_in_file_gently (const char *,const char *,const char *); +extern void git_config_set (const char *,const char *); +extern int git_config_set_gently (const char *,const char *);extern int git_config_parse_key (const char *,char **,int *);extern int git_config_key_is_valid (const char *key); -extern int git_config_set_multivar (const char *,const char *,const char *,int ); -extern int git_config_set_multivar_in_file (const char *,const char *,const char *,const char *,int ); +extern void git_config_set_multivar (const char *,const char *,const char *,int ); +extern int git_config_set_multivar_gently (const char *,const char *,const char *,int ); +extern void git_config_set_multivar_in_file (const char *,const char *,const char *,const char *,int ); +extern int git_config_set_multivar_in_file_gently (const char *,const char *,const char *,const char *,int );extern int git_config_rename_section (const char *,const char *);extern int git_config_rename_section_in_file (const char *,const char *,const char *);extern const char *git_etc_gitconfig (void );
config.c +38 -8
77 lines not on this page · raw message
submodule.c +4 -6
@@ -69,7 +69,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)strbuf_addstr (&entry,"submodule." );strbuf_addstr (&entry, submodule->name);strbuf_addstr (&entry,".path" ); -if (git_config_set_in_file (".gitmodules" , entry.buf, newpath) <0 ) { +if (git_config_set_in_file_gently (".gitmodules" , entry.buf, newpath) <0 ) {/* Maybe the user already did that, don't error out here */ warning (_ ("Could not update .gitmodules entry %s" ), entry.buf);strbuf_release (&entry);
@@ -1087,11 +1087,9 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)/* Update core.worktree setting */ strbuf_reset (&file_name);strbuf_addf (&file_name,"%s/config" , git_dir); -if (git_config_set_in_file (file_name.buf,"core.worktree" , -relative_path (real_work_tree, git_dir, - &rel_path))) -die (_ ("Could not set core.worktree in %s" ), - file_name.buf); +git_config_set_in_file (file_name.buf,"core.worktree" , +relative_path (real_work_tree, git_dir, + &rel_path));strbuf_release (&file_name);strbuf_release (&rel_path);
Signature · 1 line
2.7.0
Quoted 4 lines · Patrick Steinhardt
Furthermore, I do think it's more explicit what the functions are doing when there is a '
or_die' suffix. Without this suffix it may be unexpected that the functions simply abort the program whenever an error occurs.
That largely depends on what you are used to see. Many internal API functions do "if we cannot do this, die" without or_die suffix.
As the endgame state, I'd find it far more preferrable to see a function that dies without _or_die(), while allowing outlier callers to call the _gently() variant to do their own clean-up [*1*].
How we get to that endgame is a different matter. It is a viable approach like you did in the original series to first temporarily introduce _or_die() and convert the current callers in small chunks. The second step would be to rename git_config_set() and friends that do not die to hae _gently() suffix, and update the remaining callers to call them. At that point, nobody calls git_config_set(), so we can drop the _or_die() suffix, which would lead us to the endgame state.
But because we are talking about only a very small number of callers, I think either is OK.
Thanks.
[Footnote]
*1* Another consideration while talking about a transition like this
is what would happen in topics in flight (either in my tree above 'next' or people privately are working on). New callsites they added expecting git_config_set() to return error would have to be adjusted to call _gently() variants, and we need to catch such sites somehow. In this partcular transision, it can easily be done and you've done so already by making the functions return no value, so anybody who checked their return values would be flagged by the compiler.