diff mbox series

[2/2] test-lib: replace repeated code logic with an existing helper

Message ID 8a730b6ebba4111aaad4e8fc938c52f6e7e54480.1710766062.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series git-compat-util: move convert_slashes from compat/mingw.h and rename it | expand

Commit Message

Mohit Marathe March 18, 2024, 12:47 p.m. UTC
From: Mohit Marathe <mohitmarathe@proton.me>

This patch replaces the code, that changes the path separators,
with the already existing function `change_path_separators()`

Signed-off-by: Mohit Marathe <mohitmarathe@proton.me>
---
 t/unit-tests/test-lib.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

Comments

Junio C Hamano March 18, 2024, 9:58 p.m. UTC | #1
"Mohit Marathe via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Mohit Marathe <mohitmarathe@proton.me>
>
> This patch replaces the code, that changes the path separators,
> with the already existing function `change_path_separators()`

Aside from that the name change_path_separators() needs to be
updated, using it here means that the helper cannot become a NO-OP
on platforms where we do not have to change backslashes we read from
the system to forward slashes.  So if we really wanted to use it
here, and update the other existing callers in the main code to help
them lose the ugly "#if WINDOWS" conditional compilation, we would
need two helper functions, perhaps one that is used here that is
identical to the current convert_slashes(), and the other one used
to clean-up the callsites in [1/2] to also remove the conditional
compilation.

Even if we were to make it public, I am not sure if compat-util is
the right place, though.  It is not a kitchen sink.

In any case, perhaps we want to do something like this in a header,
...

	static inline void bs_to_forward_slash_in_place(char *path)
	{
		...
	}
	#ifdef GIT_WINDOWS_NATIVE
	#define normalize_slashes_in_place(path) bs_to_forward_slash_in_place(path)
	#else
	#define normalize_slashes_in_place(path) do { ; /* nothing */ } while (0)
	#endif

... and then use the "normalize" one to lose "#ifdef" in the callers
[1/2] touches, while "bs_to_forward_slash" one here.

I am not convinced if it is worth doing only for this single test,
though.

> @@ -88,9 +86,8 @@ static const char *make_relative(const char *location)
>  
>  	/* convert backslashes to forward slashes */
>  	strlcpy(buf, location + prefix_len, sizeof(buf));
> -	for (p = buf; *p; p++)
> -		if (*p == '\\')
> -			*p = '/';
> +	p = buf;
> +	change_path_separators(p);
>  	return buf;
>  }
Josh Steadmon April 11, 2024, 9:42 p.m. UTC | #2
On 2024.03.18 12:47, Mohit Marathe via GitGitGadget wrote:
> From: Mohit Marathe <mohitmarathe@proton.me>
> 
> This patch replaces the code, that changes the path separators,
> with the already existing function `change_path_separators()`
> 
> Signed-off-by: Mohit Marathe <mohitmarathe@proton.me>
> ---
>  t/unit-tests/test-lib.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
> index 66d6980ffbe..b0e26263046 100644
> --- a/t/unit-tests/test-lib.c
> +++ b/t/unit-tests/test-lib.c
> @@ -52,9 +52,7 @@ static const char *make_relative(const char *location)
>  		prefix_len = len - needle_len;
>  		if (prefix[prefix_len + 1] == '/') {
>  			/* Oh, we're not Windows */
> -			for (size_t i = 0; i < needle_len; i++)
> -				if (needle[i] == '\\')
> -					needle[i] = '/';
> +			change_path_separators(&needle[0]);

Why not just:
change_path_separators(needle);
?

>  			need_bs_to_fs = 0;
>  		} else {
>  			need_bs_to_fs = 1;
> @@ -88,9 +86,8 @@ static const char *make_relative(const char *location)
>  
>  	/* convert backslashes to forward slashes */
>  	strlcpy(buf, location + prefix_len, sizeof(buf));
> -	for (p = buf; *p; p++)
> -		if (*p == '\\')
> -			*p = '/';
> +	p = buf;
> +	change_path_separators(p);

And here, why not:
change_path_separators(buf)
?

>  	return buf;
>  }
>  
> -- 
> gitgitgadget
>
diff mbox series

Patch

diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
index 66d6980ffbe..b0e26263046 100644
--- a/t/unit-tests/test-lib.c
+++ b/t/unit-tests/test-lib.c
@@ -52,9 +52,7 @@  static const char *make_relative(const char *location)
 		prefix_len = len - needle_len;
 		if (prefix[prefix_len + 1] == '/') {
 			/* Oh, we're not Windows */
-			for (size_t i = 0; i < needle_len; i++)
-				if (needle[i] == '\\')
-					needle[i] = '/';
+			change_path_separators(&needle[0]);
 			need_bs_to_fs = 0;
 		} else {
 			need_bs_to_fs = 1;
@@ -88,9 +86,8 @@  static const char *make_relative(const char *location)
 
 	/* convert backslashes to forward slashes */
 	strlcpy(buf, location + prefix_len, sizeof(buf));
-	for (p = buf; *p; p++)
-		if (*p == '\\')
-			*p = '/';
+	p = buf;
+	change_path_separators(p);
 	return buf;
 }