submodule config lookup API
Subsystems (2)
cover submodule config lookup API
I have been holding back this series during the RC phase but now I think it is ready for another round. The most important changes:
* The API is using a singleton now. No need to pass in the cache object anymore.
* Local configuration can be looked up by passing in the null_sha1
* We use the API for existing lookup of submodule values
One open question:
* Since behind the scenes there is a global cache filled with the
values: Do we need to free it explicitely? Or is it ok to let it be dealt with on exit?
The last iteration was here:
http://article.gmane.org/gmane.comp.version-control.git/243818
Heiko Voigt (5):
hashmap: add enum for hashmap free_entries option implement submodule config cache for lookup of submodule names extract functions for submodule config set and lookup use new config API for worktree configurations of submodules do not die on error of parsing fetchrecursesubmodules option
.gitignore | 1 + Documentation/technical/api-hashmap.txt | 2 +- Documentation/technical/api-submodule-config.txt | 63 ++++ Makefile | 2 + builtin/checkout.c | 1 + builtin/fetch.c | 1 + diff.c | 1 + diffcore-rename.c | 2 +- hashmap.c | 2 +- hashmap.h | 8 +- name-hash.c | 4 +- submodule-config.c | 435 +++++++++++++++++++++++ submodule-config.h | 29 ++ submodule.c | 122 ++----- submodule.h | 4 +- t/t7410-submodule-config.sh | 141 ++++++++ test-hashmap.c | 6 +- test-submodule-config.c | 74 ++++ 18 files changed, 791 insertions(+), 107 deletions(-) create mode 100644 Documentation/technical/api-submodule-config.txt create mode 100644 submodule-config.c create mode 100644 submodule-config.h create mode 100755 t/t7410-submodule-config.sh create mode 100644 test-submodule-config.c
Signature · 1 line
2.0.0
PATCH 1/5 hashmap: add enum for hashmap free_entries option
This allows a reader to immediately know which options can be used and what this parameter is about.
--- Documentation/technical/api-hashmap.txt | 2 +- diffcore-rename.c | 2 +- hashmap.c | 2 +- hashmap.h | 8 +++++++- name-hash.c | 4 ++-- test-hashmap.c | 6 +++--- 6 files changed, 15 insertions(+), 9 deletions(-)
Documentation/technical/api-hashmap.txt +1 -1
@@ -187,7 +187,7 @@ void long2double_init(void) void long2double_free(void) { - hashmap_free(&map, 1); + hashmap_free(&map, HASHMAP_FREE_ENTRIES); } static struct long2double *find_entry(long key)
diffcore-rename.c +1 -1
@@ -335,7 +335,7 @@ static int find_exact_renames(struct diff_options *options) renames +=find_identical_files (&file_table, i, options);/* Free the hash data structure and entries */ -hashmap_free (&file_table,1 ); +hashmap_free (&file_table,HASHMAP_FREE_ENTRIES );return renames; }
hashmap.c +1 -1
@@ -135,7 +135,7 @@ void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,alloc_table (map, size); } -void hashmap_free (struct hashmap *map,int free_entries) +void hashmap_free (struct hashmap *map,enum hashmap_free_options free_entries) {if (!map || !map->table)return ;
hashmap.h +7 -1
@@ -1,6 +1,11 @@#ifndef HASHMAP_H #define HASHMAP_H +enum hashmap_free_options { +HASHMAP_NO_FREE_ENTRIES =0 , +HASHMAP_FREE_ENTRIES =1 , +}; +/* * Generic implementation of hash-based key-value mappings. * See Documentation/technical/api-hashmap.txt.
@@ -39,7 +44,8 @@ struct hashmap_iter {extern void hashmap_init (struct hashmap *map,hashmap_cmp_fn equals_function,size_t initial_size); -extern void hashmap_free (struct hashmap *map,int free_entries); +extern void hashmap_free (struct hashmap *map, +enum hashmap_free_options free_entries);/* hashmap_entry functions */
name-hash.c +2 -2
@@ -233,6 +233,6 @@ void free_name_hash(struct index_state *istate)return ; istate->name_hash_initialized =0 ; -hashmap_free (&istate->name_hash,0 ); -hashmap_free (&istate->dir_hash,1 ); +hashmap_free (&istate->name_hash,HASHMAP_NO_FREE_ENTRIES ); +hashmap_free (&istate->dir_hash,HASHMAP_FREE_ENTRIES ); }
test-hashmap.c +3 -3
@@ -100,7 +100,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)hashmap_add (&map, entries[i]); } -hashmap_free (&map,0 ); +hashmap_free (&map,HASHMAP_NO_FREE_ENTRIES ); } }else {/* test map lookups */
@@ -121,7 +121,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds) } } -hashmap_free (&map,0 ); +hashmap_free (&map,HASHMAP_NO_FREE_ENTRIES ); } }
@@ -250,6 +250,6 @@ int main(int argc, char *argv[]) } } -hashmap_free (&map,1 ); +hashmap_free (&map,HASHMAP_FREE_ENTRIES );return 0 ; }
Signature · 1 line
2.0.0
PATCH 2/5 implement submodule config cache for lookup of submodule names
In reply to Heiko VoigtThis submodule configuration cache allows us to lazily read .gitmodules configurations by commit into a runtime cache which can then be used to easily lookup values from it. Currently only the values for path or name are stored but it can be extended for any value needed.
It is expected that .gitmodules files do not change often between commits. Thats why we lookup the .gitmodules sha1 from a commit and then either lookup an already parsed configuration or parse and cache an unknown one for each sha1. The cache is lazily build on demand for each requested commit.
This cache can be used for all purposes which need knowledge about submodule configurations. Example use cases are:
* Recursive submodule checkout needs lookup a submodule name from its path when a submodule first appears. This needs be done before this configuration exists in the worktree.
* The implementation of submodule support for 'git archive' needs to lookup the submodule name to generate the archive when given a revision that is not checked out.
* 'git fetch' when given the --recurse-submodules=on-demand option (or configuration) needs to lookup submodule names by path from the database rather than reading from the worktree. For new submodule it needs to lookup the name from its path to allow cloning new submodules into the .git folder so they can be checked out without any network interaction when the user does a checkout of that revision.
--- .gitignore | 1 + Documentation/technical/api-submodule-config.txt | 46 +++ Makefile | 2 + submodule-config.c | 396 +++++++++++++++++++++++ submodule-config.h | 27 ++ submodule.c | 1 + submodule.h | 1 + t/t7410-submodule-config.sh | 73 +++++ test-submodule-config.c | 64 ++++ 9 files changed, 611 insertions(+) create mode 100644 Documentation/technical/api-submodule-config.txt create mode 100644 submodule-config.c create mode 100644 submodule-config.h create mode 100755 t/t7410-submodule-config.sh create mode 100644 test-submodule-config.c
.gitignore +1 -0
@@ -198,6 +198,7 @@ /test-sha1 /test-sigchain /test-string-list +/test-submodule-config /test-subprocess /test-svn-fe /test-urlmatch-normalization
Documentation/technical/api-submodule-config.txt new file +46 -0
46 lines not on this page · raw message
Makefile +2 -0
@@ -570,6 +570,7 @@ TEST_PROGRAMS_NEED_X += test-scrap-cache-tree TEST_PROGRAMS_NEED_X += test-sha1 TEST_PROGRAMS_NEED_X += test-sigchain TEST_PROGRAMS_NEED_X += test-string-list +TEST_PROGRAMS_NEED_X += test-submodule-config TEST_PROGRAMS_NEED_X += test-subprocess TEST_PROGRAMS_NEED_X += test-svn-fe TEST_PROGRAMS_NEED_X += test-urlmatch-normalization
@@ -878,6 +879,7 @@ LIB_OBJS += strbuf.o LIB_OBJS += streaming.o LIB_OBJS += string-list.o LIB_OBJS += submodule.o +LIB_OBJS += submodule-config.o LIB_OBJS += symlinks.o LIB_OBJS += tag.o LIB_OBJS += trace.o
submodule-config.c new file +396 -0
396 lines not on this page · raw message
submodule-config.h new file +27 -0
@@ -0,0 +1,27 @@ +#ifndef SUBMODULE_CONFIG_CACHE_H +#define SUBMODULE_CONFIG_CACHE_H + +#include "hashmap.h" +#include "strbuf.h" + +/* +* Submodule entry containing the information about a certain submodule +* in a certain revision. +*/ +struct submodule { +const char *path; +const char *name; +const char *url; +int fetch_recurse; +const char *ignore; +/* the sha1 blob id of the responsible .gitmodules file */ +unsigned char gitmodules_sha1[20 ]; +}; + +const struct submodule *submodule_from_name (const unsigned char *commit_sha1, +const char *name); +const struct submodule *submodule_from_path (const unsigned char *commit_sha1, +const char *path); +void submodule_free (); + +#endif /* SUBMODULE_CONFIG_H */
submodule.c +1 -0
@@ -355,6 +355,7 @@ int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)default :if (!strcmp (arg,"on-demand" ))return RECURSE_SUBMODULES_ON_DEMAND ; +/* TODO: remove the die for history parsing here */ die ("bad %s argument: %s" , opt, arg); } }
submodule.h +1 -0
@@ -5,6 +5,7 @@ struct diff_options;struct argv_array ;enum { +RECURSE_SUBMODULES_NONE =-2 ,RECURSE_SUBMODULES_ON_DEMAND =-1 ,RECURSE_SUBMODULES_OFF =0 ,RECURSE_SUBMODULES_DEFAULT =1 ,
t/t7410-submodule-config.sh new file +73 -0
73 lines not on this page · raw message
test-submodule-config.c new file +64 -0
64 lines not on this page · raw message
Signature · 1 line
2.0.0
Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names
Quoted 2 lines · Heiko Voigt
+The caller can look up information about submodules by using the +
submodule_from_path()orsubmodule_from_name()functions.
That's for an already-known submodule. Do we need a way to list submodules (e.g. for 'submodule foreach' style operations) or is the preferred way to do that just walking the tree looking for gitlinks? The cases where .gitmodules would lead you astray (e.g. via sloppy commits after removing a submodule) are:
* Listing a submodule that wasn't in the tree anymore. Easy to check for and ignore.
* Not listing a submodule that is in the tree. You'd need to walk the tree to check for this, but it's a pretty broken situation already, so I'd be fine just ignoring the orphaned gitlink.
Cheers,
Trevor
Signature · 2 lines
This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
PATCH 3/5 extract functions for submodule config set and lookup
In reply to Heiko VoigtThis is one step towards using the new configuration API. We just extract these functions to make replacing the actual code easier.
---
This refactoring is included in the series to make following the series easier (and because it was one step I did). The extracted functions will be replaced in the next commit with the ones from the cache. I think its easier to follow the implementation this way. In case you think its unnecessary I can squash this commit into the next one.
submodule.c | 142 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 45 deletions(-)
submodule.c +97 -45
@@ -41,6 +41,76 @@ static int gitmodules_is_unmerged;*/ static int gitmodules_is_modified; +static const char *get_name_for_path (const char *path) +{ +struct string_list_item *path_option; +if (path ==NULL ) { +if (config_name_for_path.nr >0 ) +return config_name_for_path.items[0 ].util; +else +return NULL ; + } + path_option =unsorted_string_list_lookup (&config_name_for_path, path); +if (!path_option) +return NULL ; +return path_option->util; +} + +static void set_name_for_path (const char *path,const char *name,int namelen) +{ +struct string_list_item *config; + config =unsorted_string_list_lookup (&config_name_for_path, path); +if (config) +free (config->util); +else + config =string_list_append (&config_name_for_path,xstrdup (path)); + config->util =xmemdupz (name, namelen); +} + +static const char *get_ignore_for_name (const char *name) +{ +struct string_list_item *ignore_option; + ignore_option =unsorted_string_list_lookup (&config_ignore_for_name, name); +if (!ignore_option) +return NULL ; + +return ignore_option->util; +} + +static void set_ignore_for_name (const char *name,int namelen,const char *ignore) +{ +struct string_list_item *config; +char *name_cstr =xmemdupz (name, namelen); + config =unsorted_string_list_lookup (&config_ignore_for_name, name_cstr); +if (config) { +free (config->util); +free (name_cstr); + }else + config =string_list_append (&config_ignore_for_name, name_cstr); + config->util =xstrdup (ignore); +} + +static int get_fetch_recurse_for_name (const char *name) +{ +struct string_list_item *fetch_recurse; + fetch_recurse =unsorted_string_list_lookup (&config_fetch_recurse_submodules_for_name, name); +if (!fetch_recurse) +return RECURSE_SUBMODULES_NONE ; + +return (intptr_t ) fetch_recurse->util; +} + +static void set_fetch_recurse_for_name (const char *name,int namelen,int fetch_recurse) +{ +struct string_list_item *config; +char *name_cstr =xmemdupz (name, namelen); + config =unsorted_string_list_lookup (&config_fetch_recurse_submodules_for_name, name_cstr); +if (!config) + config =string_list_append (&config_fetch_recurse_submodules_for_name, name_cstr); +else +free (name_cstr); + config->util = (void *)(intptr_t ) fetch_recurse; +}int is_staging_gitmodules_ok (void ) {
@@ -55,7 +125,7 @@ int is_staging_gitmodules_ok(void)int update_path_in_gitmodules (const char *oldpath,const char *newpath) {struct strbuf entry =STRBUF_INIT ; -struct string_list_item *path_option; +const char *path;if (!file_exists (".gitmodules" ))/* Do nothing without .gitmodules */ return -1 ;
@@ -63,13 +133,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)if (gitmodules_is_unmerged)die (_ ("Cannot change unmerged .gitmodules, resolve merge conflicts first" )); - path_option =unsorted_string_list_lookup (&config_name_for_path, oldpath); -if (!path_option) { + path =get_name_for_path (oldpath); +if (!path) {warning (_ ("Could not find section in .gitmodules where path=%s" ), oldpath);return -1 ; }strbuf_addstr (&entry,"submodule." ); -strbuf_addstr (&entry, path_option->util); +strbuf_addstr (&entry, path);strbuf_addstr (&entry,".path" );if (git_config_set_in_file (".gitmodules" , entry.buf, newpath) <0 ) {/* Maybe the user already did that, don't error out here */
@@ -89,7 +159,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)int remove_path_from_gitmodules (const char *path) {struct strbuf sect =STRBUF_INIT ; -struct string_list_item *path_option; +const char *path_option;if (!file_exists (".gitmodules" ))/* Do nothing without .gitmodules */ return -1 ;
@@ -97,13 +167,13 @@ int remove_path_from_gitmodules(const char *path)if (gitmodules_is_unmerged)die (_ ("Cannot change unmerged .gitmodules, resolve merge conflicts first" )); - path_option =unsorted_string_list_lookup (&config_name_for_path, path); + path_option =get_name_for_path (path);if (!path_option) {warning (_ ("Could not find section in .gitmodules where path=%s" ), path);return -1 ; }strbuf_addstr (§,"submodule." ); -strbuf_addstr (§, path_option->util); +strbuf_addstr (§, path_option);if (git_config_rename_section_in_file (".gitmodules" , sect.buf,NULL ) <0 ) {/* Maybe the user already did that, don't error out here */ warning (_ ("Could not remove .gitmodules entry for %s" ), path);
@@ -165,12 +235,11 @@ done:void set_diffopt_flags_from_submodule_config (struct diff_options *diffopt,const char *path) { -struct string_list_item *path_option, *ignore_option; - path_option =unsorted_string_list_lookup (&config_name_for_path, path); -if (path_option) { - ignore_option =unsorted_string_list_lookup (&config_ignore_for_name, path_option->util); -if (ignore_option) -handle_ignore_submodules_arg (diffopt, ignore_option->util); +const char *name =get_name_for_path (path); +if (name) { +const char *ignore =get_ignore_for_name (name); +if (ignore) +handle_ignore_submodules_arg (diffopt, ignore);else if (gitmodules_is_unmerged)DIFF_OPT_SET (diffopt,IGNORE_SUBMODULES ); }
@@ -221,7 +290,6 @@ void gitmodules_config(void)int parse_submodule_config_option (const char *var,const char *value) { -struct string_list_item *config;const char *name, *key;int namelen;
@@ -232,22 +300,14 @@ int parse_submodule_config_option(const char *var, const char *value)if (!value)return config_error_nonbool (var); - config =unsorted_string_list_lookup (&config_name_for_path, value); -if (config) -free (config->util); -else - config =string_list_append (&config_name_for_path,xstrdup (value)); - config->util =xmemdupz (name, namelen); +set_name_for_path (value, name, namelen); + }else if (!strcmp (key,"fetchrecursesubmodules" )) { -char *name_cstr =xmemdupz (name, namelen); - config =unsorted_string_list_lookup (&config_fetch_recurse_submodules_for_name, name_cstr); -if (!config) - config =string_list_append (&config_fetch_recurse_submodules_for_name, name_cstr); -else -free (name_cstr); - config->util = (void *)(intptr_t )parse_fetch_recurse_submodules_arg (var, value); +int fetch_recurse =parse_fetch_recurse_submodules_arg (var, value); + +set_fetch_recurse_for_name (name, namelen, fetch_recurse); + }else if (!strcmp (key,"ignore" )) { -char *name_cstr;if (!value)return config_error_nonbool (var);
@@ -258,14 +318,7 @@ int parse_submodule_config_option(const char *var, const char *value)return 0 ; } - name_cstr =xmemdupz (name, namelen); - config =unsorted_string_list_lookup (&config_ignore_for_name, name_cstr); -if (config) { -free (config->util); -free (name_cstr); - }else - config =string_list_append (&config_ignore_for_name, name_cstr); - config->util =xstrdup (value); +set_ignore_for_name (name, namelen, value);return 0 ; }return 0 ;
@@ -654,7 +707,7 @@ static void calculate_changed_submodule_paths(void)struct argv_array argv =ARGV_ARRAY_INIT ;/* No need to check if there are no submodules configured */ -if (!config_name_for_path.nr) +if (!get_name_for_path (NULL ))return ;init_revisions (&rev,NULL );
@@ -701,7 +754,7 @@ int fetch_populated_submodules(const struct argv_array *options,int i, result =0 ;struct child_process cp;struct argv_array argv =ARGV_ARRAY_INIT ; -struct string_list_item *name_for_path; +const char *name_for_path;const char *work_tree =get_git_work_tree ();if (!work_tree) goto out;
@@ -733,18 +786,17 @@ int fetch_populated_submodules(const struct argv_array *options,continue ; name = ce->name; - name_for_path =unsorted_string_list_lookup (&config_name_for_path, ce->name); + name_for_path =get_name_for_path (ce->name);if (name_for_path) - name = name_for_path->util; + name = name_for_path; default_argv ="yes" ;if (command_line_option ==RECURSE_SUBMODULES_DEFAULT ) { -struct string_list_item *fetch_recurse_submodules_option; - fetch_recurse_submodules_option =unsorted_string_list_lookup (&config_fetch_recurse_submodules_for_name, name); -if (fetch_recurse_submodules_option) { -if ((intptr_t )fetch_recurse_submodules_option->util ==RECURSE_SUBMODULES_OFF ) +int fetch_recurse_option =get_fetch_recurse_for_name (name); +if (fetch_recurse_option !=RECURSE_SUBMODULES_NONE ) { +if (fetch_recurse_option ==RECURSE_SUBMODULES_OFF )continue ; -if ((intptr_t )fetch_recurse_submodules_option->util ==RECURSE_SUBMODULES_ON_DEMAND ) { +if (fetch_recurse_option ==RECURSE_SUBMODULES_ON_DEMAND ) {if (!unsorted_string_list_lookup (&changed_submodule_paths, ce->name))continue ; default_argv ="on-demand" ;
Signature · 1 line
2.0.0
PATCH 4/5 use new config API for worktree configurations of submodules
In reply to Heiko VoigtWe remove the extracted functions and directly parse into and read out of the cache. This allows us to have one unified way of accessing submodule configuration values specific to single submodules. Regardless whether we need to access a configuration from history or from the worktree.
--- Documentation/technical/api-submodule-config.txt | 19 ++- builtin/checkout.c | 1 + diff.c | 1 + submodule-config.c | 12 ++ submodule-config.h | 1 + submodule.c | 160 ++++------------------- submodule.h | 1 - t/t7410-submodule-config.sh | 37 +++++- test-submodule-config.c | 10 ++ 9 files changed, 104 insertions(+), 138 deletions(-)
Documentation/technical/api-submodule-config.txt +18 -1
@@ -10,10 +10,18 @@ submodule path or name. Usage ----- +To initialize the cache with configurations from the worktree the caller +typically first calls `gitmodules_config()` to read values from the +worktree .gitmodules and then to overlay the local git config values +`parse_submodule_config_option()` from the config parsing +infrastructure. + The caller can look up information about submodules by using the `submodule_from_path()` or `submodule_from_name()` functions. They return a `struct submodule` which contains the values. The API automatically -initializes and allocates the needed infrastructure on-demand. +initializes and allocates the needed infrastructure on-demand. If the +caller does only want to lookup values from revisions the initialization +can be skipped. If the internal cache might grow too big or when the caller is done with the API, all internally cached values can be freed with submodule_free().
@@ -34,6 +42,11 @@ Functions Use these to free the internally cached values. +`int parse_submodule_config_option(const char *var, const char *value)`:: + + Can be passed to the config parsing infrastructure to parse + local (worktree) submodule configurations. + `const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`:: Lookup values for one submodule by its commit_sha1 and path or
@@ -43,4 +56,8 @@ Functions The same as above but lookup by name. +If given the null_sha1 as commit_sha1 the local configuration of a +submodule will be returned (e.g. consolidated values from local git +configuration and the .gitmodules file in the worktree). + For an example usage see test-submodule-config.c.
builtin/checkout.c +1 -0
@@ -18,6 +18,7 @@#include "xdiff-interface.h" #include "ll-merge.h" #include "resolve-undo.h" +#include "submodule-config.h" #include "submodule.h" #include "argv-array.h"
diff.c +1 -0
@@ -13,6 +13,7 @@#include "utf8.h" #include "userdiff.h" #include "sigchain.h" +#include "submodule-config.h" #include "submodule.h" #include "ll-merge.h" #include "string-list.h"
submodule-config.c +12 -0
@@ -375,6 +375,18 @@ static void ensure_cache_init() is_cache_init =1 ; } +int parse_submodule_config_option (const char *var,const char *value) +{ +struct parse_config_parameter parameter; + parameter.cache = &cache; + parameter.commit_sha1 =NULL ; + parameter.gitmodules_sha1 = null_sha1; + parameter.overwrite =1 ; + +ensure_cache_init (); +return parse_config (var, value, ¶meter); +} +const struct submodule *submodule_from_name (const unsigned char *commit_sha1,const char *name) {
submodule-config.h +1 -0
@@ -18,6 +18,7 @@ struct submodule {unsigned char gitmodules_sha1[20 ]; }; +int parse_submodule_config_option (const char *var,const char *value);const struct submodule *submodule_from_name (const unsigned char *commit_sha1,const char *name); const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
submodule.c +26 -134
246 lines not on this page · raw message
submodule.h +0 -1
@@ -20,7 +20,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, const char *path);int submodule_config (const char *var,const char *value,void *cb);void gitmodules_config (void ); -int parse_submodule_config_option (const char *var,const char *value);void handle_ignore_submodules_arg (struct diff_options *diffopt,const char *);int parse_fetch_recurse_submodules_arg (const char *opt,const char *arg); void show_submodule_summary(FILE *f, const char *path,
t/t7410-submodule-config.sh +35 -2
@@ -5,8 +5,8 @@ test_description='Test submodules config cache infrastructure -This test verifies that parsing .gitmodules configuration directly -from the database works. +This test verifies that parsing .gitmodules configurations directly +from the database and from the worktree works. ' TEST_NO_CREATE_REPO=1
@@ -70,4 +70,37 @@ test_expect_success 'error in one submodule config lets continue' ' ) ' +cat >super/expect_url <<EOF +Submodule url: '[email protected]:a.git' for path 'b' +Submodule url: '[email protected]:submodule.git' for path 'submodule' +EOF + +cat >super/expect_local_path <<EOF +Submodule name: 'a' for path 'c' +Submodule name: 'submodule' for path 'submodule' +EOF + +test_expect_success 'reading of local configuration' ' + (cd super && + old_a=$(git config submodule.a.url) && + old_submodule=$(git config submodule.submodule.url) && + git config submodule.a.url [email protected]:a.git && + git config submodule.submodule.url [email protected]:submodule.git && + test-submodule-config --url \ + "" b \ + "" submodule \ + >actual && + test_cmp expect_url actual && + git config submodule.a.path c && + test-submodule-config \ + "" c \ + "" submodule \ + >actual && + test_cmp expect_local_path actual && + git config submodule.a.url $old_a && + git config submodule.submodule.url $old_submodule && + git config --unset submodule.a.path c + ) +' + test_done
test-submodule-config.c +10 -0
@@ -4,6 +4,7 @@#include "cache.h" #include "submodule-config.h" +#include "submodule.h" static void die_usage (int argc,char **argv,const char *msg) {
@@ -12,6 +13,11 @@ static void die_usage(int argc, char **argv, const char *msg)exit (1 ); } +static int git_test_config (const char *var,const char *value,void *cb) +{ +return parse_submodule_config_option (var, value); +} +int main (int argc,char **argv) {char **arg = argv;
@@ -30,6 +36,10 @@ int main(int argc, char **argv)if (my_argc %2 !=0 )die_usage (argc, argv,"Wrong number of arguments." ); +setup_git_directory (); +gitmodules_config (); +git_config (git_test_config,NULL ); +while (*arg) {unsigned char commit_sha1[20 ];const struct submodule *submodule;
Signature · 1 line
2.0.0
PATCH 5/5 do not die on error of parsing fetchrecursesubmodules option
In reply to Heiko VoigtWe should not die when reading the submodule config cache since the user might not be able to get out of that situation when the configuration is part of the history.
We should handle this condition later when the value is about to be used.
--- builtin/fetch.c | 1 + submodule-config.c | 29 ++++++++++++++++++++++++++++- submodule-config.h | 1 + submodule.c | 15 --------------- submodule.h | 2 +- t/t7410-submodule-config.sh | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 17 deletions(-)
builtin/fetch.c +1 -0
@@ -12,6 +12,7 @@#include "parse-options.h" #include "sigchain.h" #include "transport.h" +#include "submodule-config.h" #include "submodule.h" #include "connected.h" #include "argv-array.h"
submodule-config.c +28 -1
@@ -199,6 +199,30 @@ static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,return submodule; } +static int parse_fetch_recurse (const char *opt,const char *arg, +int die_on_error) +{ +switch (git_config_maybe_bool (opt, arg)) { +case 1 : +return RECURSE_SUBMODULES_ON ; +case 0 : +return RECURSE_SUBMODULES_OFF ; +default : +if (!strcmp (arg,"on-demand" )) +return RECURSE_SUBMODULES_ON_DEMAND ; + +if (die_on_error) +die ("bad %s argument: %s" , opt, arg); +else +return RECURSE_SUBMODULES_ERROR ; + } +} + +int parse_fetch_recurse_submodules_arg (const char *opt,const char *arg) +{ +return parse_fetch_recurse (opt, arg,1 ); +} +static void warn_multiple_config (const unsigned char *commit_sha1,const char *name,const char *option) {
@@ -250,6 +274,8 @@ static int parse_config(const char *var, const char *value, void *data) submodule->path =strbuf_detach (&path,NULL );cache_put_path (me->cache, submodule); }else if (!strcmp (item.buf,"fetchrecursesubmodules" )) { +/* when parsing worktree configurations we can die early */ +int die_on_error =is_null_sha1 (me->gitmodules_sha1);if (!me->overwrite && submodule->fetch_recurse !=RECURSE_SUBMODULES_NONE ) { warn_multiple_config(me->commit_sha1, submodule->name,
@@ -257,7 +283,8 @@ static int parse_config(const char *var, const char *value, void *data) goto release_return; } - submodule->fetch_recurse =parse_fetch_recurse_submodules_arg (var, value); + submodule->fetch_recurse =parse_fetch_recurse (var, value, + die_on_error); }else if (!strcmp (item.buf,"ignore" )) {struct strbuf ignore =STRBUF_INIT ;if (!me->overwrite && submodule->ignore !=NULL ) {
submodule-config.h +1 -0
@@ -18,6 +18,7 @@ struct submodule {unsigned char gitmodules_sha1[20 ]; }; +int parse_fetch_recurse_submodules_arg (const char *opt,const char *arg);int parse_submodule_config_option (const char *var,const char *value);const struct submodule *submodule_from_name (const unsigned char *commit_sha1,const char *name);
submodule.c +0 -15
@@ -288,21 +288,6 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,strbuf_release (&sb); } -int parse_fetch_recurse_submodules_arg (const char *opt,const char *arg) -{ -switch (git_config_maybe_bool (opt, arg)) { -case 1 : -return RECURSE_SUBMODULES_ON ; -case 0 : -return RECURSE_SUBMODULES_OFF ; -default : -if (!strcmp (arg,"on-demand" )) -return RECURSE_SUBMODULES_ON_DEMAND ; -/* TODO: remove the die for history parsing here */ -die ("bad %s argument: %s" , opt, arg); - } -} - void show_submodule_summary(FILE *f, const char *path, const char *line_prefix, unsigned char one[20], unsigned char two[20],
submodule.h +1 -1
@@ -5,6 +5,7 @@ struct diff_options;struct argv_array ;enum { +RECURSE_SUBMODULES_ERROR =-3 ,RECURSE_SUBMODULES_NONE =-2 ,RECURSE_SUBMODULES_ON_DEMAND =-1 ,RECURSE_SUBMODULES_OFF =0 ,
@@ -21,7 +22,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,int submodule_config (const char *var,const char *value,void *cb);void gitmodules_config (void );void handle_ignore_submodules_arg (struct diff_options *diffopt,const char *); -int parse_fetch_recurse_submodules_arg (const char *opt,const char *arg); void show_submodule_summary(FILE *f, const char *path, const char *line_prefix, unsigned char one[20], unsigned char two[20],
t/t7410-submodule-config.sh +35 -0
@@ -103,4 +103,39 @@ test_expect_success 'reading of local configuration' ' ) ' +cat >super/expect_fetchrecurse_die.err <<EOF +fatal: bad submodule.submodule.fetchrecursesubmodules argument: blabla +EOF + +test_expect_success 'local error in fetchrecursesubmodule dies early' ' + (cd super && + git config submodule.submodule.fetchrecursesubmodules blabla && + test_must_fail test-submodule-config \ + "" b \ + "" submodule \ + >actual.out 2>actual.err && + touch expect_fetchrecurse_die.out && + test_cmp expect_fetchrecurse_die.out actual.out && + test_cmp expect_fetchrecurse_die.err actual.err && + git config --unset submodule.submodule.fetchrecursesubmodules + ) +' + +test_expect_success 'error in history in fetchrecursesubmodule lets continue' ' + (cd super && + git config -f .gitmodules \ + submodule.submodule.fetchrecursesubmodules blabla && + git add .gitmodules && + git config --unset -f .gitmodules \ + submodule.submodule.fetchrecursesubmodules && + git commit -m "add error in fetchrecursesubmodules" && + test-submodule-config \ + HEAD b \ + HEAD submodule \ + >actual && + test_cmp expect_error actual && + git reset --hard HEAD^ + ) +' + test_done
Signature · 1 line
2.0.0
Re: Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names
In reply to W. Trevor KingQuoted 10 lines · W. Trevor King
Quoted 2 lines · Heiko Voigt
+The caller can look up information about submodules by using the +
submodule_from_path()orsubmodule_from_name()functions.That's for an already-known submodule. Do we need a way to list submodules (e.g. for 'submodule foreach' style operations) or is the preferred way to do that just walking the tree looking for gitlinks? The cases where .gitmodules would lead you astray (e.g. via sloppy commits after removing a submodule) are:
Quoted 6 lines
* Listing a submodule that wasn't in the tree anymore. Easy to check for and ignore.
* Not listing a submodule that is in the tree. You'd need to walk the tree to check for this, but it's a pretty broken situation already, so I'd be fine just ignoring the orphaned gitlink.
Currently there is no need to list the submodules in a .gitmodule file. We currently always begin from the gitlink and try to do things. If we have enough information thats fine we go ahead, if not we stop (or skip?) the submodule. So for already initialized submodules it is even ok to not have a .gitmodules entry at all and we can still go ahead with most operations. Here we should not be too picky, I think.
The only use-case I can think of is for checking whether .gitmodules contains any extra unneeded values. But on the other hand that is not so easy. Since .gitmodules are just config files they can contain user defined values. That is ok.
So in summary: Yes the preferred way to list submodules is via iterating the gitlinks and I do not think we need a way to iterate through the .gitmodules file (at least not for the use-cases we currently need this for).
Cheers Heiko
Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
In reply to Heiko VoigtAm 05.06.2014 08:06, schrieb Heiko Voigt:
Quoted 3 lines
This allows a reader to immediately know which options can be used and what this parameter is about.
[...]
Quoted 2 lines
hashmap.c +1
-1-void hashmap_free (struct hashmap *map,int free_entries) +void hashmap_free (struct hashmap *map,enum hashmap_free_options free_entries)
[...]
hashmap.h line 7
Quoted 5 lines
hashmap.h +4
-0+enum hashmap_free_options { +HASHMAP_NO_FREE_ENTRIES =0 , +HASHMAP_FREE_ENTRIES =1 , +};
This was meant as a boolean parameter. Would it make sense to have
enum boolean {
false,
true
};
or similar in some central place?
Note that an earlier version took a function pointer, and you could pass stdlib's free() in the common case, or a special free routine for nested entry structures, or NULL to do the cleanup yourself.
Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names
In reply to Heiko VoigtQuoted 23 lines · Heiko Voigt
This submodule configuration cache allows us to lazily read .gitmodules configurations by commit into a runtime cache which can then be used to easily lookup values from it. Currently only the values for path or name are stored but it can be extended for any value needed.
[...]
Signed-off-by Heiko Voigt <[email protected]>---
t/t7410-submodule-config.sh new file +8
-0@@ -0,0 +1,73 @@ +#!/bin/sh +# +# Copyright (c) 2014 Heiko Voigt +# + +test_description='Test submodules config cache infrastructure + +This test verifies that parsing .gitmodules configuration directly
t/t7410-submodule-config.sh line 18
Quoted 10 lines
t/t7410-submodule-config.sh new file +10
-0+from the database works. +' + +TEST_NO_CREATE_REPO=1 +. ./test-lib.sh + +test_expect_success 'submodule config cache setup' ' + mkdir submodule && + (cd submodule && + git init
Broken &&-chain.
Quoted 55 lines
t/t7410-submodule-config.sh new file +55
-0+ echo a >a && + git add . && + git commit -ma + ) && + mkdir super && + (cd super && + git init && + git submodule add ../submodule && + git submodule add ../submodule a && + git commit -m "add as submodule and as a" && + git mv a b && + git commit -m "move a to b" + ) +' + +cat >super/expect <<EOF +Submodule name: 'a' for path 'a' +Submodule name: 'a' for path 'b' +Submodule name: 'submodule' for path 'submodule' +Submodule name: 'submodule' for path 'submodule' +EOF + +test_expect_success 'test parsing of submodule config' ' + (cd super && + test-submodule-config \ + HEAD^ a \ + HEAD b \ + HEAD^ submodule \ + HEAD submodule \ + >actual && + test_cmp expect actual + ) +' + +cat >super/expect_error <<EOF +Submodule name: 'a' for path 'b' +Submodule name: 'submodule' for path 'submodule' +EOF + +test_expect_success 'error in one submodule config lets continue' ' + (cd super && + cp .gitmodules .gitmodules.bak && + echo " value = \"" >>.gitmodules && + git add .gitmodules && + mv .gitmodules.bak .gitmodules && + git commit -m "add error" && + test-submodule-config \ + HEAD b \ + HEAD submodule \ + >actual && + test_cmp expect_error actual + ) +' + +test_done
Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
In reply to Karsten BleesQuoted 16 lines · Karsten Blees
Am 05.06.2014 08:06, schrieb Heiko Voigt:
Quoted 3 lines
This allows a reader to immediately know which options can be used and what this parameter is about.
[...]
Quoted 2 lines
hashmap.c +1
-1-void hashmap_free (struct hashmap *map,int free_entries) +void hashmap_free (struct hashmap *map,enum hashmap_free_options free_entries)[...]
hashmap.h line 7
Quoted 5 lines
hashmap.h +4
-0+enum hashmap_free_options { +HASHMAP_NO_FREE_ENTRIES =0 , +HASHMAP_FREE_ENTRIES =1 , +};This was meant as a boolean parameter. Would it make sense to have
Quoted 6 lines
enum boolean {
false,
true
};or similar in some central place?
The intention of Jonathans critique here[1] was that you do not see what this parameter does on the callsite. I.e.:
hashmap_free(&map, 1);
compared to
hashmap_free(&map, HASHMAP_FREE_ENTRIES);
A boolean basically transfers the same information and would not help the reader here.
Cheers Heiko
[1] http://article.gmane.org/gmane.comp.version-control.git/243917
Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names
In reply to Eric SunshineQuoted 28 lines · Eric Sunshine
Quoted 27 lines · Heiko Voigt
This submodule configuration cache allows us to lazily read .gitmodules configurations by commit into a runtime cache which can then be used to easily lookup values from it. Currently only the values for path or name are stored but it can be extended for any value needed.
[...]
Signed-off-by Heiko Voigt <[email protected]>---
t/t7410-submodule-config.sh new file +12
-0@@ -0,0 +1,73 @@ +#!/bin/sh +# +# Copyright (c) 2014 Heiko Voigt +# + +test_description='Test submodules config cache infrastructure + +This test verifies that parsing .gitmodules configuration directly +from the database works. +' + +TEST_NO_CREATE_REPO=1
Quoted 8 lines
t/t7410-submodule-config.sh line 18
Quoted 6 lines
t/t7410-submodule-config.sh new file +6
-0+. ./test-lib.sh + +test_expect_success 'submodule config cache setup' ' + mkdir submodule && + (cd submodule && + git initBroken &&-chain.
Will fix. Thanks for catching.
Cheers Heiko
Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
In reply to Heiko VoigtAm 10.06.2014 12:17, schrieb Heiko Voigt:
Quoted 31 lines · Karsten Blees
Quoted 16 lines
Am 05.06.2014 08:06, schrieb Heiko Voigt:
Quoted 3 lines
This allows a reader to immediately know which options can be used and what this parameter is about.
[...]
Quoted 2 lines
hashmap.c +1
-1-void hashmap_free (struct hashmap *map,int free_entries) +void hashmap_free (struct hashmap *map,enum hashmap_free_options free_entries)[...]
hashmap.h line 7
Quoted 5 lines
hashmap.h +4
-0+enum hashmap_free_options { +HASHMAP_NO_FREE_ENTRIES =0 , +HASHMAP_FREE_ENTRIES =1 , +};This was meant as a boolean parameter. Would it make sense to have
Quoted 6 lines
enum boolean {
false,
true
};or similar in some central place?
The intention of Jonathans critique here[1] was that you do not see what this parameter does on the callsite. I.e.:
hashmap_free(&map, 1);compared to
Quoted 9 lines
hashmap_free(&map, HASHMAP_FREE_ENTRIES);A boolean basically transfers the same information and would not help the reader here.
Cheers Heiko
[1] http://article.gmane.org/gmane.comp.version-control.git/243917
There are languages where you can have e.g. 'hashmap_free(..., free_entries: true)'. In C, however, you do not see what a parameter does at the call site. This is a general language feature, reducing redundancy and keeping it short and concise. IMO there's no reason to treat boolean parameters differently.
Using an enum suggests that there is more to the parameter than a simple yes/no decision, underpinned by naming it '...options' (plural). I find this rather confusing.
Finally, enums share a global namespace, which means long identifiers, provoking additional line breaks and thus reducing readability. Not a problem with hashmap_free per se, but if you do the same for e.g. 'free_util' in string-list.[ch] or 'icase' in name-hash.c, I suspect it'll get pretty ugly.
So please lets not spoil the global namespace with a thousand different names for 0/1. Using enums for >= tristate values and bit flags is fine, but inventing enums for every simple boolean in the system is bound to end in chaos.
Just my 2c
Karsten
Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
Quoted 15 lines · Karsten Blees
Am 10.06.2014 12:17, schrieb Heiko Voigt:
Quoted 14 lines
The intention of Jonathans critique here[1] was that you do not see what this parameter does on the callsite. I.e.:
hashmap_free(&map, 1);compared to
hashmap_free(&map, HASHMAP_FREE_ENTRIES);A boolean basically transfers the same information and would not help the reader here.
Cheers Heiko
Quoted 8 lines
Quoted 2 lines
[1] http://article.gmane.org/gmane.comp.version-control.git/243917
There are languages where you can have e.g. 'hashmap_free(...,
free_entries: true)'. In C, however, you do not see what a parameter does at the call site. This is a general language feature, reducing redundancy and keeping it short and concise. IMO there's no reason to treat boolean parameters differently.
But given that you are writing in C, is any of that relevant? We do want to keep our call-sites readable and understandable, and 1 or true would not help, unless (1) you are the one who wrote the function and know that 1 means free the entries, or (2) the API is so widely used and everybody knows what 1 means free the entries.
Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names
In reply to Heiko VoigtQuoted 2 lines · Heiko Voigt
...
+static intis_cache_init= 0;
Please don't initialise variables in the .bss to zero by hand.
submodule-config.c line 277
Quoted 12 lines
+ ...
submodule-config.c new file +11
-0+warning ("%s:.gitmodules, multiple configurations found for " +"submodule.%s.%s. Skipping second one!" , + commit_string, name, option); +} + ... +if (strcmp (value,"untracked" ) &&strcmp (value,"dirty" ) && +strcmp (value,"all" ) &&strcmp (value,"none" )) { +warning ("Invalid parameter \"%s\" for config option " +"\"submodule.%s.ignore\"" , value, var); + goto release_return; + }
These two look inconsistent in different ways. I think we typically quote the names like so:
warning("I have trouble with variable '%s' somehow", var);Hmph, this seems to conflict in a meaningful (and painful) way with Jens's "jl/submodule-recursive-checkout".
Am 13.06.2014 00:04, schrieb Junio C Hamano:
Quoted 2 lines
Hmph, this seems to conflict in a meaningful (and painful) way with Jens's "jl/submodule-recursive-checkout".
Then you might wanna drop my series for now, I need to rebase it above Heiko's series myself to make new submodules work anyway.
Quoted 1 line · Heiko Voigt
t/t7410-submodule-config.sh | 141 ++++++++
We already use 7410 for something else in 'pu'; please avoid dups waiting to happen.
Quoted 8 lines
test-hashmap.c | 6 +- test-submodule-config.c | 74 ++++ 18 files changed, 791 insertions(+), 107 deletions(-) create mode 100644 Documentation/technical/api-submodule-config.txt create mode 100644 submodule-config.c create mode 100644 submodule-config.h create mode 100755 t/t7410-submodule-config.sh create mode 100644 test-submodule-config.c
Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names
In reply to Junio C HamanoQuoted 6 lines · Junio C Hamano
Quoted 2 lines · Heiko Voigt
...
+static intis_cache_init= 0;Please don't initialise variables in the .bss to zero by hand.
Ok will remove that.
Quoted 16 lines
Review
Quoted 12 lines
+ ...
(quoted diff) +11
-0+ warning("%s:.gitmodules, multiple configurations found for " + "submodule.%s.%s. Skipping second one!", + commit_string, name, option); +} + ... + if (strcmp(value, "untracked") && strcmp(value, "dirty") && + strcmp(value, "all") && strcmp(value, "none")) { + warning("Invalid parameter \"%s\" for config option " + "\"submodule.%s.ignore\"", value, var); + goto release_return; + }These two look inconsistent in different ways. I think we typically quote the names like so:
Quoted 1 line
warning("I have trouble with variable '%s' somehow", var);Ok will change the quotation for the variable and parameter names.
Here is the fixup[1] I will queue in my branch.
Cheers Heiko
[1] ---8<----
Subject: [PATCH] fixup! implement submodule config cache for lookup of
submodule names
--- submodule-config.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
submodule-config.c +4 -4
@@ -26,7 +26,7 @@ struct submodule_entry { };static struct submodule_cache cache; -static int is_cache_init =0 ; +static int is_cache_init; static int config_path_cmp(const struct submodule_entry *a, const struct submodule_entry *b,
@@ -230,7 +230,7 @@ static void warn_multiple_config(const unsigned char *commit_sha1,if (commit_sha1) commit_string =sha1_to_hex (commit_sha1);warning ("%s:.gitmodules, multiple configurations found for " -"submodule.%s.%s. Skipping second one!" , +"'submodule.%s.%s'. Skipping second one!" , commit_string, name, option); }
@@ -298,8 +298,8 @@ static int parse_config(const char *var, const char *value, void *data) }if (strcmp (value,"untracked" ) &&strcmp (value,"dirty" ) &&strcmp (value,"all" ) &&strcmp (value,"none" )) { -warning ("Invalid parameter \"%s\" for config option " -"\"submodule.%s.ignore\"" , value, var); +warning ("Invalid parameter '%s' for config option " +"'submodule.%s.ignore'" , value, var); goto release_return; }
Signature · 1 line
2.0.0
Quoted 6 lines · Jens Lehmann
Am 13.06.2014 00:04, schrieb Junio C Hamano:
Quoted 2 lines
Hmph, this seems to conflict in a meaningful (and painful) way with Jens's "jl/submodule-recursive-checkout".
Then you might wanna drop my series for now, I need to rebase it above Heiko's series myself to make new submodules work anyway.
Thanks, that makes my pile smaller by one topic ;-)
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Heiko Voigt
t/t7410-submodule-config.sh | 141 ++++++++We already use 7410 for something else in 'pu'; please avoid dups waiting to happen.
Sorry about that. Should I use 7411 even though that other series is still work in progress?
Quoted 10 lines · Heiko Voigt
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Heiko Voigt
t/t7410-submodule-config.sh | 141 ++++++++We already use 7410 for something else in 'pu'; please avoid dups waiting to happen.
Sorry about that. Should I use 7411 even though that other series is still work in progress?
Surely.
What would be an alternative? Tell the other series to rename? ;-)
Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
In reply to Junio C HamanoAm 12.06.2014 21:12, schrieb Junio C Hamano:
Quoted 20 lines
Quoted 18 lines · Karsten Blees
Am 10.06.2014 12:17, schrieb Heiko Voigt:
Quoted 16 lines
The intention of Jonathans critique here[1] was that you do not see what this parameter does on the callsite. I.e.:
hashmap_free(&map, 1);compared to
hashmap_free(&map, HASHMAP_FREE_ENTRIES);A boolean basically transfers the same information and would not help the reader here.
Cheers Heiko
[1] http://article.gmane.org/gmane.comp.version-control.git/243917
Quoted 8 lines
Quoted 5 lines
There are languages where you can have e.g. 'hashmap_free(...,
free_entries: true)'. In C, however, you do not see what a parameter does at the call site. This is a general language feature, reducing redundancy and keeping it short and concise. IMO there's no reason to treat boolean parameters differently.But given that you are writing in C, is any of that relevant? We do want to keep our call-sites readable and understandable,
But in C, readable and understandable are opposite goals. 'Understandable' entails long, redundant identifiers, automatically decreasing readability. The compiler doesn't care about either, so we could just as well keep the C part short and use plain English for understandability:
/* free maps, except file entries (owned by istate->cache) */ hashmap_free(&istate->name_hash, 0); hashmap_free(&istate->dir_hash, 1);
Note that this not only explains what we're doing, but also why.
Quoted 5 lines
and 1 or
true would not help, unless (1) you are the one who wrote the function and know that 1 means free the entries, or (2) the API is so widely used and everybody knows what 1 means free the entries.
or (3) you need to check the function declaration or documentation anyway, to understand what the non-boolean parameters do.
E.g. consider this (from remote.c:1186):
dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);
vs.
dst_value = resolve_ref_unsafe(matched_src->name, sha1,
RESOLVE_REF_UNSAFE_FOR_READING,
&flag);That's three lines vs. one, "RESOLVE_REF_UNSAFE_" is completely redundant with the function name, "FOR_READING" isn't particularly enlightening either, and you still don't know what the other three parameters do. IMO this would be much better:
/* fully resolve matched symref to resolved ref name and sha1 */ dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);
So veterans highly familiar with the code can stick to the C part without being distracted by unnecessary line breaks and SHOUTED_IDENTIFIERS, while everyone else may find the explanation helpful.
As I said, using enums for hashmap_free isn't a problem on its own. However, the accepted solution for booleans in the git code base seems to be to use just an int and 0/1.
For consistency, we could of course change string_list*, resolve_ref*, index_file_exists etc. as well.
...and in turn 'extern int ignore_case' (because it gets passed to index_file_exists)?
...and in turn all other boolean config variables?
I don't think this would be an improvement, though.
Re: Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
Quoted 82 lines · Karsten Blees
Am 12.06.2014 21:12, schrieb Junio C Hamano:
Quoted 20 lines
Quoted 18 lines · Karsten Blees
Am 10.06.2014 12:17, schrieb Heiko Voigt:
Quoted 16 lines
The intention of Jonathans critique here[1] was that you do not see what this parameter does on the callsite. I.e.:
hashmap_free(&map, 1);compared to
hashmap_free(&map, HASHMAP_FREE_ENTRIES);A boolean basically transfers the same information and would not help the reader here.
Cheers Heiko
[1] http://article.gmane.org/gmane.comp.version-control.git/243917
Quoted 8 lines
Quoted 5 lines
There are languages where you can have e.g. 'hashmap_free(...,
free_entries: true)'. In C, however, you do not see what a parameter does at the call site. This is a general language feature, reducing redundancy and keeping it short and concise. IMO there's no reason to treat boolean parameters differently.But given that you are writing in C, is any of that relevant? We do want to keep our call-sites readable and understandable,
But in C, readable and understandable are opposite goals. 'Understandable' entails long, redundant identifiers, automatically decreasing readability. The compiler doesn't care about either, so we could just as well keep the C part short and use plain English for understandability:
/* free maps, except file entries (owned by istate->cache) */ hashmap_free(&istate->name_hash, 0); hashmap_free(&istate->dir_hash, 1);Note that this not only explains what we're doing, but also why.
Quoted 5 lines
and 1 or
true would not help, unless (1) you are the one who wrote the function and know that 1 means free the entries, or (2) the API is so widely used and everybody knows what 1 means free the entries.or (3) you need to check the function declaration or documentation anyway, to understand what the non-boolean parameters do.
E.g. consider this (from remote.c:1186):
dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);vs.
dst_value= resolve_ref_unsafe(matched_src->name, sha1,RESOLVE_REF_UNSAFE_FOR_READING, &flag);That's three lines vs. one, "
RESOLVE_REF_UNSAFE_" is completely redundant with the function name, "FOR_READING" isn't particularly enlightening either, and you still don't know what the other three parameters do. IMO this would be much better:/* fully resolve matched symref to resolved ref name and sha1 */
dst_value=resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);So veterans highly familiar with the code can stick to the C part without being distracted by unnecessary line breaks and
SHOUTED_IDENTIFIERS, while everyone else may find the explanation helpful.As I said, using enums for
hashmap_freeisn't a problem on its own. However, the accepted solution for booleans in the git code base seems to be to use just an int and 0/1.For consistency, we could of course change string_list*, resolve_ref*,
index_file_existsetc. as well.
Quoted 6 lines
...and in turn 'extern int
ignore_case' (because it gets passed toindex_file_exists)?...and in turn all other boolean config variables?
I don't think this would be an improvement, though.
If this is such a controversial change for you I will drop this patch in the next round. I think it would make the callsite more readable without adding much clutter but I am fine with it either way.
Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option
Quoted 3 lines · Heiko Voigt
If this is such a controversial change for you I will drop this patch in the next round. I think it would make the callsite more readable without adding much clutter but I am fine with it either way.
OK, let's do that.
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Heiko Voigt
Quoted 6 lines · Junio C Hamano
Quoted 1 line · Heiko Voigt
t/t7410-submodule-config.sh | 141 ++++++++We already use 7410 for something else in 'pu'; please avoid dups waiting to happen.
Quoted 6 lines
Quoted 2 lines
Sorry about that. Should I use 7411 even though that other series is still work in progress?
Surely.
What would be an alternative? Tell the other series to rename? ;-)
Don't know. Of course not ;-) Will rename.