path: factor out skip_slashes() in normalize_path_copy_len()

Extract skip_slashes() to avoid repeating the same is_dir_sep() loop in multiple places inside normalize_path_copy_len(). Keep the dot-component handling inline to preserve the original control flow and readability, as suggested in review. No functional changes. Behavior verified with t0060-path-utils.sh. Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pushkar Singh committed Feb 21, 2026 at 11:05 UTC 40b30f245d57f89215f89c29b72e8b557f09b82a
1 file changed +13 -6
path.c
+13 -6
@@ -1112,6 +1112,14 @@ const char *remove_leading_path(const char *in, const char *prefix)
1112 * end with a '/', then the callers need to be fixed up accordingly.
1113 *
1114 */
1115 +
1116 +static const char *skip_slashes(const char *p)
1117 +{
1118 + while (is_dir_sep(*p))
1119 + p++;
1120 + return p;
1121 +}
1122 +
1123 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1124 {
1125 char *dst0;
@@ -1129,8 +1137,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1137 }
1138 dst0 = dst;
1139
1132 - while (is_dir_sep(*src))
1133 - src++;
1140 + src = skip_slashes(src);
1141
1142 for (;;) {
1143 char c = *src;
@@ -1150,8 +1157,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1157 } else if (is_dir_sep(src[1])) {
1158 /* (2) */
1159 src += 2;
1153 - while (is_dir_sep(*src))
1154 - src++;
1160 + src = skip_slashes(src);
1161 continue;
1162 } else if (src[1] == '.') {
1163 if (!src[2]) {
@@ -1161,8 +1167,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1167 } else if (is_dir_sep(src[2])) {
1168 /* (4) */
1169 src += 3;
1164 - while (is_dir_sep(*src))
1165 - src++;
1170 + src = skip_slashes(src);
1171 goto up_one;
1172 }
1173 }
@@ -1182,6 +1187,8 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1187
1188 up_one:
1189 /*
1190 + * strip the last component
1191 + *
1192 * dst0..dst is prefix portion, and dst[-1] is '/';
1193 * go up one level.
1194 */