path: move related function to path

Move path-related function from strbuf.[ch] to path.[ch] so that strbuf is focused on string manipulation routines with minimal dependencies. repository.h is no longer a necessary dependency after moving this function out. Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Calvin Wan committed Jun 6, 2023 at 19:48 UTC aba070683295a20bdf4f49146384984961c794b2
3 files changed +25 -21
path.c
+20
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
1213 return normalize_path_copy_len(dst, src, NULL);
1214 }
1215
1216 +int strbuf_normalize_path(struct strbuf *src)
1217 +{
1218 + struct strbuf dst = STRBUF_INIT;
1219 +
1220 + strbuf_grow(&dst, src->len);
1221 + if (normalize_path_copy(dst.buf, src->buf) < 0) {
1222 + strbuf_release(&dst);
1223 + return -1;
1224 + }
1225 +
1226 + /*
1227 + * normalize_path does not tell us the new length, so we have to
1228 + * compute it by looking for the new NUL it placed
1229 + */
1230 + strbuf_setlen(&dst, strlen(dst.buf));
1231 + strbuf_swap(src, &dst);
1232 + strbuf_release(&dst);
1233 + return 0;
1234 +}
1235 +
1236 /*
1237 * path = Canonical absolute path
1238 * prefixes = string_list containing normalized, absolute paths without
path.h
+5
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
191 const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
192 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
193 int normalize_path_copy(char *dst, const char *src);
194 +/**
195 + * Normalize in-place the path contained in the strbuf. If an error occurs,
196 + * the contents of "sb" are left untouched, and -1 is returned.
197 + */
198 +int strbuf_normalize_path(struct strbuf *src);
199 int longest_ancestor_length(const char *path, struct string_list *prefixes);
200 char *strip_path_suffix(const char *path, const char *suffix);
201 int daemon_avoid_alias(const char *path);
strbuf.c
-21
@@ -3,7 +3,6 @@
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 -#include "repository.h"
6 #include "strbuf.h"
7 #include "string-list.h"
8 #include "utf8.h"
@@ -1089,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
1088 strbuf_setlen(sb, j);
1089 }
1090
1092 -int strbuf_normalize_path(struct strbuf *src)
1093 -{
1094 - struct strbuf dst = STRBUF_INIT;
1095 -
1096 - strbuf_grow(&dst, src->len);
1097 - if (normalize_path_copy(dst.buf, src->buf) < 0) {
1098 - strbuf_release(&dst);
1099 - return -1;
1100 - }
1101 -
1102 - /*
1103 - * normalize_path does not tell us the new length, so we have to
1104 - * compute it by looking for the new NUL it placed
1105 - */
1106 - strbuf_setlen(&dst, strlen(dst.buf));
1107 - strbuf_swap(src, &dst);
1108 - strbuf_release(&dst);
1109 - return 0;
1110 -}
1111 -
1091 void strbuf_strip_file_from_path(struct strbuf *sb)
1092 {
1093 char *path_sep = find_last_dir_sep(sb->buf);