summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builtin-help.c2
-rw-r--r--git-compat-util.h6
-rw-r--r--http-fetch.c2
-rw-r--r--index-pack.c2
-rw-r--r--local-fetch.c4
-rw-r--r--refs.c2
-rw-r--r--sha1_file.c2
-rw-r--r--verify-pack.c2
8 files changed, 14 insertions, 8 deletions
diff --git a/builtin-help.c b/builtin-help.c
index fb731cc934..7a7f7759e5 100644
--- a/builtin-help.c
+++ b/builtin-help.c
@@ -140,7 +140,7 @@ static void list_commands(const char *exec_path, const char *pattern)
continue;
entlen = strlen(de->d_name);
- if (4 < entlen && !strcmp(de->d_name + entlen - 4, ".exe"))
+ if (has_extension(de->d_name, entlen, ".exe"))
entlen -= 4;
if (longest < entlen)
diff --git a/git-compat-util.h b/git-compat-util.h
index 3bcf5b13f2..dd92093652 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -139,6 +139,12 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
}
}
+static inline int has_extension(const char *filename, int len, const char *ext)
+{
+ int extlen = strlen(ext);
+ return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
+}
+
/* Sane ctype - no locale, and works with signed chars */
#undef isspace
#undef isdigit
diff --git a/http-fetch.c b/http-fetch.c
index 36af3e5b94..6ea39f0589 100644
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -870,7 +870,7 @@ static void process_ls_pack(struct remote_ls_ctx *ls)
if (strlen(ls->dentry_name) == 63 &&
!strncmp(ls->dentry_name, "objects/pack/pack-", 18) &&
- !strncmp(ls->dentry_name+58, ".pack", 5)) {
+ has_extension(ls->dentry_name, 63, ".pack")) {
get_sha1_hex(ls->dentry_name + 18, sha1);
setup_index(ls->repo, sha1);
}
diff --git a/index-pack.c b/index-pack.c
index b39953dc69..a91e39ecd6 100644
--- a/index-pack.c
+++ b/index-pack.c
@@ -447,7 +447,7 @@ int main(int argc, char **argv)
usage(index_pack_usage);
if (!index_name) {
int len = strlen(pack_name);
- if (len < 5 || strcmp(pack_name + len - 5, ".pack"))
+ if (!has_extension(pack_name, len, ".pack"))
die("packfile name '%s' does not end with '.pack'",
pack_name);
index_name_buf = xmalloc(len);
diff --git a/local-fetch.c b/local-fetch.c
index 4bf86fbbe2..b6ec170c01 100644
--- a/local-fetch.c
+++ b/local-fetch.c
@@ -43,8 +43,8 @@ static int setup_indices(void)
return -1;
while ((de = readdir(dir)) != NULL) {
int namelen = strlen(de->d_name);
- if (namelen != 50 ||
- strcmp(de->d_name + namelen - 5, ".pack"))
+ if (namelen != 50 ||
+ !has_extension(de->d_name, namelen, ".pack"))
continue;
get_sha1_hex(de->d_name + 5, sha1);
setup_index(sha1);
diff --git a/refs.c b/refs.c
index 02850b6908..b01835f634 100644
--- a/refs.c
+++ b/refs.c
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
namelen = strlen(de->d_name);
if (namelen > 255)
continue;
- if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
+ if (has_extension(de->d_name, namelen, ".lock"))
continue;
memcpy(path + baselen, de->d_name, namelen+1);
if (stat(git_path("%s", path), &st) < 0)
diff --git a/sha1_file.c b/sha1_file.c
index 43bc2ea0cf..a1bb01ca35 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -590,7 +590,7 @@ static void prepare_packed_git_one(char *objdir, int local)
int namelen = strlen(de->d_name);
struct packed_git *p;
- if (strcmp(de->d_name + namelen - 4, ".idx"))
+ if (!has_extension(de->d_name, namelen, ".idx"))
continue;
/* we have .idx. Is it a file we can map? */
diff --git a/verify-pack.c b/verify-pack.c
index c99db9dd79..ef00204ad4 100644
--- a/verify-pack.c
+++ b/verify-pack.c
@@ -10,7 +10,7 @@ static int verify_one_pack(char *arg, int verbose)
/* Should name foo.idx, but foo.pack may be named;
* convert it to foo.idx
*/
- if (!strcmp(arg + len - 5, ".pack")) {
+ if (has_extension(arg, len, ".pack")) {
strcpy(arg + len - 5, ".idx");
len--;
}