From: Pushkar Singh <pushkarkumarsingh1970@gmail•com>
To: git@vger•kernel.org
Cc: peff@peff•net, gitster@pobox•com,
Pushkar Singh <pushkarkumarsingh1970@gmail•com>
Subject: [PATCH] path: refactor normalize_path_copy_len()
Date: Thu, 29 Jan 2026 14:54:35 +0000 [thread overview]
Message-ID: <20260129145434.29123-2-pushkarkumarsingh1970@gmail.com> (raw)
Refactor normalize_path_copy_len() by extracting helpers for skipping
slashes, handling dot components, and stripping the previous path
component, making the control flow easier to follow.
This is a mechanical refactor only; there are no functional changes.
Behavior is unchanged, as verified by t0060-path-utils.sh.
Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail•com>
---
path.c | 105 ++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 67 insertions(+), 38 deletions(-)
diff --git a/path.c b/path.c
index d726537622..00845cc03f 100644
--- a/path.c
+++ b/path.c
@@ -1112,6 +1112,63 @@ const char *remove_leading_path(const char *in, const char *prefix)
* end with a '/', then the callers need to be fixed up accordingly.
*
*/
+
+static const char *skip_slashes(const char *p)
+{
+ while (is_dir_sep(*p))
+ p++;
+ return p;
+}
+
+static int handle_dot_component(const char **src)
+{
+ const char *s = *src;
+
+ if (*s != '.')
+ return 0;
+
+ if (!s[1]) {
+ *src = s + 1;
+ return 1;
+ }
+
+ if (is_dir_sep(s[1])) {
+ *src = skip_slashes(s + 2);
+ return 1;
+ }
+
+ if (s[1] == '.') {
+ if (!s[2]) {
+ *src = s + 2;
+ return 2;
+ }
+ if (is_dir_sep(s[2])) {
+ *src = skip_slashes(s + 3);
+ return 2;
+ }
+ }
+
+ return 0;
+}
+
+static int strip_last_component(char **dst, char *dst0, int *prefix_len)
+{
+ char *d = *dst;
+
+ d--;
+ if (d <= dst0)
+ return -1;
+
+ while (dst0 < d && d[-1] != '/')
+ d--;
+
+ if (prefix_len && *prefix_len > d - dst0)
+ *prefix_len = d - dst0;
+
+ *dst = d;
+ return 0;
+}
+
int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
{
char *dst0;
@@ -1129,8 +1186,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
}
dst0 = dst;
- while (is_dir_sep(*src))
- src++;
+ src = skip_slashes(src);
for (;;) {
char c = *src;
@@ -1143,29 +1199,14 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
* (3) ".." and ends -- strip one and terminate.
* (4) "../" -- strip one, eat slash and continue.
*/
- if (c == '.') {
- if (!src[1]) {
- /* (1) */
- src++;
- } else if (is_dir_sep(src[1])) {
- /* (2) */
- src += 2;
- while (is_dir_sep(*src))
- src++;
- continue;
- } else if (src[1] == '.') {
- if (!src[2]) {
- /* (3) */
- src += 2;
- goto up_one;
- } else if (is_dir_sep(src[2])) {
- /* (4) */
- src += 3;
- while (is_dir_sep(*src))
- src++;
- goto up_one;
- }
- }
+ int dot = handle_dot_component(&src);
+
+ if (dot == 1)
+ continue;
+ if (dot == 2) {
+ if (strip_last_component(&dst, dst0, prefix_len))
+ return -1;
+ continue;
}
/* copy up to the next '/', and eat all '/' */
@@ -1180,20 +1221,8 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
break;
continue;
- up_one:
- /*
- * dst0..dst is prefix portion, and dst[-1] is '/';
- * go up one level.
- */
- dst--; /* go to trailing '/' */
- if (dst <= dst0)
- return -1;
- /* Windows: dst[-1] cannot be backslash anymore */
- while (dst0 < dst && dst[-1] != '/')
- dst--;
- if (prefix_len && *prefix_len > dst - dst0)
- *prefix_len = dst - dst0;
}
+
*dst = '\0';
return 0;
}
--
2.43.0
next reply other threads:[~2026-01-29 15:15 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-29 14:54 Pushkar Singh [this message]
2026-01-29 18:54 ` [PATCH] path: refactor normalize_path_copy_len() Junio C Hamano
2026-01-30 14:01 ` [PATCH v2] path: factor out skip_slashes() in normalize_path_copy_len() Pushkar Singh
2026-02-14 9:13 ` Pushkar Singh
2026-02-17 18:27 ` Junio C Hamano
2026-02-21 11:05 ` [PATCH v3] " Pushkar Singh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260129145434.29123-2-pushkarkumarsingh1970@gmail.com \
--to=pushkarkumarsingh1970@gmail$(echo .)com \
--cc=git@vger$(echo .)kernel.org \
--cc=gitster@pobox$(echo .)com \
--cc=peff@peff$(echo .)net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox