Skip to content

Commit aba0706

Browse files
calvin-wan-googlegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f940185 commit aba0706

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

path.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
12131213
return normalize_path_copy_len(dst, src, NULL);
12141214
}
12151215

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+
12161236
/*
12171237
* path = Canonical absolute path
12181238
* prefixes = string_list containing normalized, absolute paths without

path.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
191191
const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
192192
int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
193193
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);
194199
int longest_ancestor_length(const char *path, struct string_list *prefixes);
195200
char *strip_path_suffix(const char *path, const char *suffix);
196201
int daemon_avoid_alias(const char *path);

strbuf.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "environment.h"
44
#include "gettext.h"
55
#include "hex.h"
6-
#include "repository.h"
76
#include "strbuf.h"
87
#include "string-list.h"
98
#include "utf8.h"
@@ -1089,26 +1088,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
10891088
strbuf_setlen(sb, j);
10901089
}
10911090

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-
11121091
void strbuf_strip_file_from_path(struct strbuf *sb)
11131092
{
11141093
char *path_sep = find_last_dir_sep(sb->buf);

0 commit comments

Comments
 (0)