]> git.ipfire.org Git - thirdparty/git.git/commitdiff
path: add a function to check for path suffix
authorbrian m. carlson <sandals@crustytoothpaste.net>
Sun, 25 Aug 2019 23:33:39 +0000 (23:33 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Aug 2019 17:36:13 +0000 (10:36 -0700)
We have a function to strip the path suffix from a commit, but we don't
have one to check for a path suffix. For a plain filename, we can use
basename, but that requires an allocation, since POSIX allows it to
modify its argument. Refactor strip_path_suffix into a helper function
and a new function, ends_with_path_components, to meet this need.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
path.c
path.h

diff --git a/path.c b/path.c
index 25e97b8c3f76ce9246d8d985adba9777acd5f43c..e3da1f3c4e2c7ed077c1ed3a98103b178045a45a 100644 (file)
--- a/path.c
+++ b/path.c
@@ -1221,31 +1221,52 @@ static inline int chomp_trailing_dir_sep(const char *path, int len)
 }
 
 /*
- * If path ends with suffix (complete path components), returns the
- * part before suffix (sans trailing directory separators).
- * Otherwise returns NULL.
+ * If path ends with suffix (complete path components), returns the offset of
+ * the last character in the path before the suffix (sans trailing directory
+ * separators), and -1 otherwise.
  */
-char *strip_path_suffix(const char *path, const char *suffix)
+static ssize_t stripped_path_suffix_offset(const char *path, const char *suffix)
 {
        int path_len = strlen(path), suffix_len = strlen(suffix);
 
        while (suffix_len) {
                if (!path_len)
-                       return NULL;
+                       return -1;
 
                if (is_dir_sep(path[path_len - 1])) {
                        if (!is_dir_sep(suffix[suffix_len - 1]))
-                               return NULL;
+                               return -1;
                        path_len = chomp_trailing_dir_sep(path, path_len);
                        suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
                }
                else if (path[--path_len] != suffix[--suffix_len])
-                       return NULL;
+                       return -1;
        }
 
        if (path_len && !is_dir_sep(path[path_len - 1]))
-               return NULL;
-       return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
+               return -1;
+       return chomp_trailing_dir_sep(path, path_len);
+}
+
+/*
+ * Returns true if the path ends with components, considering only complete path
+ * components, and false otherwise.
+ */
+int ends_with_path_components(const char *path, const char *components)
+{
+       return stripped_path_suffix_offset(path, components) != -1;
+}
+
+/*
+ * If path ends with suffix (complete path components), returns the
+ * part before suffix (sans trailing directory separators).
+ * Otherwise returns NULL.
+ */
+char *strip_path_suffix(const char *path, const char *suffix)
+{
+       ssize_t offset = stripped_path_suffix_offset(path, suffix);
+
+       return offset == -1 ? NULL : xstrndup(path, offset);
 }
 
 int daemon_avoid_alias(const char *p)
diff --git a/path.h b/path.h
index 2ba6ca58c83487b5e02a71ce7c2f0d556c59aebe..14d6dcad161e3720629ed60d1ec7e4a4b3e34fdd 100644 (file)
--- a/path.h
+++ b/path.h
@@ -193,4 +193,7 @@ const char *git_path_merge_head(struct repository *r);
 const char *git_path_fetch_head(struct repository *r);
 const char *git_path_shallow(struct repository *r);
 
+
+int ends_with_path_components(const char *path, const char *components);
+
 #endif /* PATH_H */