Skip to content

Commit 9ea5796

Browse files
derrickstoleegitster
authored andcommitted
strbuf: introduce strbuf_strip_file_from_path()
The strbuf_parent_directory() method was added as a static method in contrib/scalar by d0feac4 (scalar: 'register' sets recommended config and starts maintenance, 2021-12-03) and then removed in 65f6a9e (scalar: constrain enlistment search, 2022-08-18), but now there is a need for a similar method in the bundle URI feature. Re-add the method, this time in strbuf.c, but with a new name: strbuf_strip_file_from_path(). The method requirements are slightly modified to allow a trailing slash, in which case nothing is done, which makes the name change valuable. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 738dc7d commit 9ea5796

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

strbuf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,3 +1200,9 @@ int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
12001200
free(path2);
12011201
return res;
12021202
}
1203+
1204+
void strbuf_strip_file_from_path(struct strbuf *sb)
1205+
{
1206+
char *path_sep = find_last_dir_sep(sb->buf);
1207+
strbuf_setlen(sb, path_sep ? path_sep - sb->buf + 1 : 0);
1208+
}

strbuf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,17 @@ int launch_sequence_editor(const char *path, struct strbuf *buffer,
664664
int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
665665
const char *const *env);
666666

667+
/*
668+
* Remove the filename from the provided path string. If the path
669+
* contains a trailing separator, then the path is considered a directory
670+
* and nothing is modified.
671+
*
672+
* Examples:
673+
* - "/path/to/file" -> "/path/to/"
674+
* - "/path/to/dir/" -> "/path/to/dir/"
675+
*/
676+
void strbuf_strip_file_from_path(struct strbuf *sb);
677+
667678
void strbuf_add_lines(struct strbuf *sb,
668679
const char *prefix,
669680
const char *buf,

0 commit comments

Comments
 (0)