merge-recursive: use fspathcmp() in path_hashmap_cmp()
PATCH 1/1 merge-recursive: use fspathcmp() in path_hashmap_cmp()
Call fspathcmp() instead of open-coding it. This shortens the code and makes it less repetitive.
--- merge-recursive.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
merge-recursive.c +1 -4
@@ -55,10 +55,7 @@ static int path_hashmap_cmp(const void *cmp_data, a = container_of(eptr, const struct path_hashmap_entry, e); b = container_of(entry_or_key, const struct path_hashmap_entry, e); -if (ignore_case) -return strcasecmp (a->path, key ? key : b->path); -else -return strcmp (a->path, key ? key : b->path); +return fspathcmp (a->path, key ? key : b->path); }/*
--
2.33.0
Hi René,
Quoted 13 lines · René Scharfe
Call
fspathcmp()instead of open-coding it. This shortens the code and makes it less repetitive.Signed-off-by René Scharfe <[email protected]>--- merge-recursive.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)merge-recursive.c +0
-0
merge-recursive.c static int path_hashmap_cmp(const void *cmp_data, · line 59
Quoted 9 lines
merge-recursive.c +1
-4@@ -55,10 +55,7 @@ static int path_hashmap_cmp(const void *cmp_data, a = container_of(eptr, const struct path_hashmap_entry, e); b = container_of(entry_or_key, const struct path_hashmap_entry, e); -if (ignore_case) -return strcasecmp (a->path, key ? key : b->path); -else -return strcmp (a->path, key ? key : b->path); +return fspathcmp (a->path, key ? key : b->path); }
Looks obviously right to me. I found another spot in t/helper/test-hashmap.c:test_entry_cmp() that could be cleaned up in the same way. But this looks fine with or without the following diff:
t/helper/test-hashmap.c +1 -4
@@ -28,10 +28,7 @@ static int test_entry_cmp(const void *cmp_data, e1 = container_of(eptr, const struct test_entry, ent); e2 = container_of(entry_or_key, const struct test_entry, ent); -if (ignore_case) -return strcasecmp (e1->key, key ? key : e2->key); -else -return strcmp (e1->key, key ? key : e2->key); +return fspathcmp (e1->key, key ? key : e2->key); } static struct test_entry *alloc_test_entry(unsigned int hash,
Quoted 14 lines · Taylor Blau
Review static int path_hashmap_cmp(const void *cmp_data,
Quoted 2 lines
+++
b/merge-recursive.c(quoted diff) +0
-0@@ -55,10 +55,7 @@ static int path_hashmap_cmp(const void *cmp_data,[...]
Looks obviously right to me. I found another spot in t/helper/test-hashmap.c:test_entry_cmp() that could be cleaned up in the same way. But this looks fine with or without the following diff:
t/helper/test-hashmap.c +0
-0@@ -28,10 +28,7 @@ static int test_entry_cmp(const void *cmp_data, e1 = container_of(eptr, const struct test_entry, ent);
t/helper/test-hashmap.c static int test_entry_cmp(const void *cmp_data, · line 34
Quoted 10 lines
t/helper/test-hashmap.c +1
-4e2 = container_of(entry_or_key, const struct test_entry, ent); -if (ignore_case) -return strcasecmp (e1->key, key ? key : e2->key); -else -return strcmp (e1->key, key ? key : e2->key); +return fspathcmp (e1->key, key ? key : e2->key); } static struct test_entry *alloc_test_entry(unsigned int hash,
Maybe also:
dir.c +1 -3
@@ -669,9 +669,7 @@ int pl_hashmap_cmp(const void *unused_cmp_data, ? ee1->patternlen : ee2->patternlen; -if (ignore_case) -return strncasecmp (ee1->pattern, ee2->pattern, min_len); -return strncmp (ee1->pattern, ee2->pattern, min_len); +return fspathncmp (ee1->pattern, ee2->pattern, min_len); }static char *dup_and_filter_pattern (const char *pattern)
This is fun. :)
-Peff
Quoted 27 lines · Jeff King
Review
Quoted 8 lines
(quoted diff) +1
-4- if (ignore_case) - return strcasecmp(e1->key, key ? key : e2->key); - else - return strcmp(e1->key, key ? key : e2->key); + return fspathcmp(e1->key, key ? key : e2->key); } static struct test_entry *alloc_test_entry(unsigned int hash,Maybe also:
dir.c +1
-3@@ -669,9 +669,7 @@ int pl_hashmap_cmp(const void *unused_cmp_data, ? ee1->patternlen : ee2->patternlen; -if (ignore_case) -return strncasecmp (ee1->pattern, ee2->pattern, min_len); -return strncmp (ee1->pattern, ee2->pattern, min_len); +return fspathncmp (ee1->pattern, ee2->pattern, min_len); }static char *dup_and_filter_pattern (const char *pattern)
Quoted 1 line
This is fun. :)
Yes.
So we found three of them in the existing code. A Coccinelle rule may be an overkill, I guess.
Am 29.08.21 um 22:21 schrieb Taylor Blau:
Quoted 37 lines
Hi René,
Quoted 13 lines · René Scharfe
Call
fspathcmp()instead of open-coding it. This shortens the code and makes it less repetitive.Signed-off-by René Scharfe <[email protected]>--- merge-recursive.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)merge-recursive.c +0
-0merge-recursive.c static int path_hashmap_cmp(const void *cmp_data, · line 59
Quoted 9 lines
merge-recursive.c +1
-4@@ -55,10 +55,7 @@ static int path_hashmap_cmp(const void *cmp_data, a = container_of(eptr, const struct path_hashmap_entry, e); b = container_of(entry_or_key, const struct path_hashmap_entry, e); -if (ignore_case) -return strcasecmp (a->path, key ? key : b->path); -else -return strcmp (a->path, key ? key : b->path); +return fspathcmp (a->path, key ? key : b->path); }Looks obviously right to me. I found another spot in t/helper/test-hashmap.c:test_entry_cmp() that could be cleaned up in the same way. But this looks fine with or without the following diff:
t/helper/test-hashmap.c +0
-0@@ -28,10 +28,7 @@ static int test_entry_cmp(const void *cmp_data, e1 = container_of(eptr, const struct test_entry, ent);
t/helper/test-hashmap.c static int test_entry_cmp(const void *cmp_data, · line 34
Quoted 10 lines
t/helper/test-hashmap.c +1
-4e2 = container_of(entry_or_key, const struct test_entry, ent); -if (ignore_case) -return strcasecmp (e1->key, key ? key : e2->key); -else -return strcmp (e1->key, key ? key : e2->key); +return fspathcmp (e1->key, key ? key : e2->key); } static struct test_entry *alloc_test_entry(unsigned int hash,
That's a local variable named "ignore_case", not the one declared in environment.c that fspathcmp() uses, so this would change the behavior. The helper code does not include cache.h, so this is not even a case of variable shadowing, just two different variables for similar purposes in different places having the same name.
René
Quoted 15 lines · René Scharfe
Quoted 15 lines
t/helper/test-hashmap.c +1
-4@@ -28,10 +28,7 @@ static int test_entry_cmp(const void *cmp_data, e1 = container_of(eptr, const struct test_entry, ent); e2 = container_of(entry_or_key, const struct test_entry, ent); -if (ignore_case) -return strcasecmp (e1->key, key ? key : e2->key); -else -return strcmp (e1->key, key ? key : e2->key); +return fspathcmp (e1->key, key ? key : e2->key); }
Quoted 8 lines
t/helper/test-hashmap.c static int test_entry_cmp(const void *cmp_data, · line 34
Quoted 2 lines
t/helper/test-hashmap.c +0
-0static struct test_entry *alloc_test_entry(unsigned int hash,That's a local variable named "
ignore_case", not the one declared inenvironment.cthatfspathcmp()uses, so this would change the behavior. The helper code does not includecache.h, so this is not even a case of variable shadowing, just two different variables for similar purposes in different places having the same name.
Yikes, good catch. Perhaps it's overkill, but I wonder if a comment like:
/*
* Do not use fspathcmp() here; our behavior depends on the local
* ignore_case variable, not the usual Git-wide global. */
would help.
I double-checked the spot I suggested. I think it is actually using the global (though I got it right through sheer luck).
-Peff
Quoted 9 lines · Taylor Blau
Looks obviously right to me. I found another spot in t/helper/test-hashmap.c:test_entry_cmp() that could be cleaned up in the same way. But this looks fine with or without the following diff:
t/helper/test-hashmap.c +0
-0
t/helper/test-hashmap.c static int test_entry_cmp(const void *cmp_data, · line 31
Quoted 8 lines
t/helper/test-hashmap.c +1
-4@@ -28,10 +28,7 @@ static int test_entry_cmp(const void *cmp_data, e1 = container_of(eptr, const struct test_entry, ent); e2 = container_of(entry_or_key, const struct test_entry, ent); -if (ignore_case) -return strcasecmp (e1->key, key ? key : e2->key); -else -return strcmp (e1->key, key ? key : e2->key); +return fspathcmp (e1->key, key ? key : e2->key);
Sorry but I think this patch is wrong. Before the precontext of the patch, there is a local variable decl for ignore_case---the existing code looks at ignore_case that is different from the global ignore_case fspathcmp() looks at.
Admittedly, it was probably not an excellent idea to give a name so bland and unremarkable, 'ignore_case', to a global that affects so many code paths in the system. But the variable is already very established that renaming it would not contribute to improving the code at all.
It however may not be a bad idea to catch these code paths where a local variable masks 'ignore_case' (and possibly other globals) and rename these local ones to avoid a mistake like this.
Thanks.
Am 30.08.21 um 18:55 schrieb Junio C Hamano:
Quoted 31 lines
Quoted 9 lines · Taylor Blau
Looks obviously right to me. I found another spot in t/helper/test-hashmap.c:test_entry_cmp() that could be cleaned up in the same way. But this looks fine with or without the following diff:
t/helper/test-hashmap.c +0
-0t/helper/test-hashmap.c static int test_entry_cmp(const void *cmp_data, · line 31
Quoted 8 lines
t/helper/test-hashmap.c +1
-4@@ -28,10 +28,7 @@ static int test_entry_cmp(const void *cmp_data, e1 = container_of(eptr, const struct test_entry, ent); e2 = container_of(entry_or_key, const struct test_entry, ent); -if (ignore_case) -return strcasecmp (e1->key, key ? key : e2->key); -else -return strcmp (e1->key, key ? key : e2->key); +return fspathcmp (e1->key, key ? key : e2->key);Sorry but I think this patch is wrong. Before the precontext of the patch, there is a local variable decl for ignore_case---the existing code looks at
ignore_casethat is different from the globalignore_casefspathcmp()looks at.Admittedly, it was probably not an excellent idea to give a name so bland and unremarkable, '
ignore_case', to a global that affects so many code paths in the system. But the variable is already very established that renaming it would not contribute to improving the code at all.
Quoted 3 lines
It however may not be a bad idea to catch these code paths where a local variable masks '
ignore_case' (and possibly other globals) and rename these local ones to avoid a mistake like this.
The name itself is OK, I think, but using it at global scope is confusing. -Wshadow can help find such cases, but not this one, as test-hashmap.c doesn't include the global declaration. Moving the global into a struct to provide a poor man's namespace would fix this for all namesakes, assisted by the compiler. We'd then access it as the_config.ignore_case or even the_config.core.ignore_case.
Moving all config-related variables would be quite noisy, I guess, and probably conflict with lots of in-flight patches, but might be worth it.
René
Quoted 11 lines · René Scharfe
Quoted 3 lines
It however may not be a bad idea to catch these code paths where a local variable masks '
ignore_case' (and possibly other globals) and rename these local ones to avoid a mistake like this.The name itself is OK, I think, but using it at global scope is confusing. -Wshadow can help find such cases, but not this one, as
test-hashmap.cdoesn't include the global declaration. Moving the global into a struct to provide a poor man's namespace would fix this for all namesakes, assisted by the compiler. We'd then access it as the_config.ignore_case or even the_config.core.ignore_case.
Quoted 3 lines
Moving all config-related variables would be quite noisy, I guess, and probably conflict with lots of in-flight patches, but might be worth it.
Really most of these ought to be in the repository struct anyway, I would think. The value of ignore_case comes from core.ignorecase, which is going to be repository-specific. We are probably doing the wrong thing already by looking at the parent core.ignorecase value when operating in an in-process submodule, but nobody noticed because it's quite unlikely for a submodule to have a different setting than the parent.
-Peff
Am 30.08.21 um 22:49 schrieb Jeff King:
Quoted 17 lines · René Scharfe
Quoted 12 lines
Quoted 4 lines
It however may not be a bad idea to catch these code paths where a local variable masks '
ignore_case' (and possibly other globals) and rename these local ones to avoid a mistake like this.The name itself is OK, I think, but using it at global scope is confusing. -Wshadow can help find such cases, but not this one, as
test-hashmap.cdoesn't include the global declaration. Moving the global into a struct to provide a poor man's namespace would fix this for all namesakes, assisted by the compiler. We'd then access it as the_config.ignore_case or even the_config.core.ignore_case.Quoted 3 lines
Moving all config-related variables would be quite noisy, I guess, and probably conflict with lots of in-flight patches, but might be worth it.
Quoted 7 lines
Really most of these ought to be in the repository struct anyway, I would think. The value of
ignore_casecomes from core.ignorecase, which is going to be repository-specific. We are probably doing the wrong thing already by looking at the parent core.ignorecase value when operating in an in-process submodule, but nobody noticed because it's quite unlikely for a submodule to have a different setting than the parent.
Good point. So fspathcmp() and friends would need a repo parameter. :-|
René
Hi René,
Quoted 26 lines · René Scharfe
Am 30.08.21 um 22:49 schrieb Jeff King:
Quoted 17 lines · René Scharfe
Quoted 12 lines
Quoted 4 lines
It however may not be a bad idea to catch these code paths where a local variable masks '
ignore_case' (and possibly other globals) and rename these local ones to avoid a mistake like this.The name itself is OK, I think, but using it at global scope is confusing. -Wshadow can help find such cases, but not this one, as
test-hashmap.cdoesn't include the global declaration. Moving the global into a struct to provide a poor man's namespace would fix this for all namesakes, assisted by the compiler. We'd then access it as the_config.ignore_case or even the_config.core.ignore_case.Quoted 3 lines
Moving all config-related variables would be quite noisy, I guess, and probably conflict with lots of in-flight patches, but might be worth it.
Quoted 7 lines
Really most of these ought to be in the repository struct anyway, I would think. The value of
ignore_casecomes from core.ignorecase, which is going to be repository-specific. We are probably doing the wrong thing already by looking at the parent core.ignorecase value when operating in an in-process submodule, but nobody noticed because it's quite unlikely for a submodule to have a different setting than the parent.
Quoted 1 line
Good point. So
fspathcmp()and friends would need a repo parameter. :-|
Yes, we will eventually have to pass struct repository *r into a _lot_ of call chains. It'll be a disruptive change, yet if the submodule folks truly want to aim for in-process recursive treatment of submodules, there is no alternative.
FWIW on Windows there are other potentially repository-specific settings that are relevant in similar situations. For example, there is core.symlinks.
Ciao,
Dscho
Quoted 10 lines · Johannes Schindelin
Quoted 1 line
Good point. So
fspathcmp()and friends would need a repo parameter. :-|Yes, we will eventually have to pass
struct repository *rinto a _lot_ of call chains. It'll be a disruptive change, yet if the submodule folks truly want to aim for in-process recursive treatment of submodules, there is no alternative.FWIW on Windows there are other potentially repository-specific settings that are relevant in similar situations. For example, there is
core.symlinks.
Another approach is to stuff the appropriate globals into the repository struct, and then "push" onto the global the_repository pointer, treating it like a stack. And then low-level code is free to use that global context, even if it wasn't passed in.
That helps the primary use case of "now I need to do something in a sub-module, but I'd like to do it in-process". But it's not without challenges:
- code which acts at the boundary of a submodule and a superproject may be more awkward (since only one of them can be "the current repository" at a time).
- it's a challenge with threading (an obvious problem would be a multi-threaded grep which wanted to descend into a submodule). Using a thread-local global for the_repository might solve that.
It's possible that this is a terrible direction to go, so I'm not necessarily endorsing it, but just offering it as a possibility to think about. The trickiest thing is that any devil would likely be in the details, and we wouldn't know until proceeding for a while along that path. Whereas passing around a context struct, while verbose and annoying, is a well-understood construct.
-Peff
Quoted 9 lines · Jeff King
Another approach is to stuff the appropriate globals into the repository struct, and then "push" onto the global
the_repositorypointer, treating it like a stack. And then low-level code is free to use that global context, even if it wasn't passed in.
...
- it's a challenge with threading (an obvious problem would be a multi-threaded grep which wanted to descend into a submodule). Using a thread-local global forthe_repositorymight solve that.
Quoted 6 lines
It's possible that this is a terrible direction to go, so I'm not necessarily endorsing it, but just offering it as a possibility to think about. The trickiest thing is that any devil would likely be in the details, and we wouldn't know until proceeding for a while along that path. Whereas passing around a context struct, while verbose and annoying, is a well-understood construct.
I agree that it is cute, thread-unsafe, and tricky. Let's leave it at an interesting thought experiment for now.
Thanks.
Hi Peff,
Quoted 30 lines · Jeff King
Quoted 10 lines · Johannes Schindelin
Quoted 1 line
Good point. So
fspathcmp()and friends would need a repo parameter. :-|Yes, we will eventually have to pass
struct repository *rinto a _lot_ of call chains. It'll be a disruptive change, yet if the submodule folks truly want to aim for in-process recursive treatment of submodules, there is no alternative.FWIW on Windows there are other potentially repository-specific settings that are relevant in similar situations. For example, there is
core.symlinks.Another approach is to stuff the appropriate globals into the repository struct, and then "push" onto the global
the_repositorypointer, treating it like a stack. And then low-level code is free to use that global context, even if it wasn't passed in.That helps the primary use case of "now I need to do something in a sub-module, but I'd like to do it in-process". But it's not without challenges:
- code which acts at the boundary of a submodule and a superproject may be more awkward (since only one of them can be "the current repository" at a time).
- it's a challenge with threading (an obvious problem would be a multi-threaded grep which wanted to descend into a submodule). Using a thread-local global for
the_repositorymight solve that.
Quoted 6 lines
It's possible that this is a terrible direction to go, so I'm not necessarily endorsing it, but just offering it as a possibility to think about. The trickiest thing is that any devil would likely be in the details, and we wouldn't know until proceeding for a while along that path. Whereas passing around a context struct, while verbose and annoying, is a well-understood construct.
I would not so far as to call it a terrible direction. It is definitely worth a thought or two.
At the end of the day, I fear that it is too tricky in practice, though.
Seeing as there seems to be some appetite for refactoring Git's code on this list, I am thinking that the struct repository *r direction might be the one to go for. And I mean like "move the globals into that struct" as opposed to introducing that stack you talked about. It would even be a refactoring where I would understand the motivation, and agree with it, too.
Ciao,
Dscho
Quoted 6 lines · Johannes Schindelin
Seeing as there seems to be some appetite for refactoring Git's code on this list, I am thinking that the
struct repository *rdirection might be the one to go for. And I mean like "move the globals into that struct" as opposed to introducing that stack you talked about. It would even be a refactoring where I would understand the motivation, and agree with it, too.
Oh, definitely. Regardless of whether step 2 is "pass around the repository struct" or "treat the global repository struct as a stack", step 1 must be putting repository-related globals into the struct. I don't think there can be any solution that doesn't start with that. :)
And I think it can even be done incrementally with very little impact. Just s/ignore_case/the_repository->ignore_case/ in the use-sites is an improvement over the status quo. Even though it doesn't _fix_ anything, now we can easily see where the dependencies on repo-variables are. And of course follow-on steps to make sure we are passing around and accessing the right repository struct are then welcome.
-Peff