replace has_extension with ends_with

These two are almost the same function, with the exception that has_extension only matches if there is content before the suffix. So ends_with(".exe", ".exe") is true, but has_extension would not be. This distinction does not matter to any of the callers, though, and we can just replace uses of has_extension with ends_with. We prefer the "ends_with" name because it is more generic, and there is nothing about the function that requires it to be used for file extensions. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 30, 2014 at 12:58 UTC 2975c770ca609ea5afc80631c4ac9087c527b6fd
6 files changed +12 -19
builtin/index-pack.c
+2 -2
@@ -1603,7 +1603,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1603 die(_("--fix-thin cannot be used without --stdin"));
1604 if (!index_name && pack_name) {
1605 int len = strlen(pack_name);
1606 - if (!has_extension(pack_name, ".pack"))
1606 + if (!ends_with(pack_name, ".pack"))
1607 die(_("packfile name '%s' does not end with '.pack'"),
1608 pack_name);
1609 index_name_buf = xmalloc(len);
@@ -1613,7 +1613,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1613 }
1614 if (keep_msg && !keep_name && pack_name) {
1615 int len = strlen(pack_name);
1616 - if (!has_extension(pack_name, ".pack"))
1616 + if (!ends_with(pack_name, ".pack"))
1617 die(_("packfile name '%s' does not end with '.pack'"),
1618 pack_name);
1619 keep_name_buf = xmalloc(len);
builtin/verify-pack.c
+2 -2
@@ -27,9 +27,9 @@ static int verify_one_pack(const char *path, unsigned int flags)
27 * normalize these forms to "foo.pack" for "index-pack --verify".
28 */
29 strbuf_addstr(&arg, path);
30 - if (has_extension(arg.buf, ".idx"))
30 + if (ends_with(arg.buf, ".idx"))
31 strbuf_splice(&arg, arg.len - 3, 3, "pack", 4);
32 - else if (!has_extension(arg.buf, ".pack"))
32 + else if (!ends_with(arg.buf, ".pack"))
33 strbuf_add(&arg, ".pack", 5);
34 argv[2] = arg.buf;
35
git-compat-util.h
-7
@@ -578,13 +578,6 @@ static inline size_t xsize_t(off_t len)
578 return (size_t)len;
579 }
580
581 -static inline int has_extension(const char *filename, const char *ext)
582 -{
583 - size_t len = strlen(filename);
584 - size_t extlen = strlen(ext);
585 - return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
586 -}
587 -
581 /* in ctype.c, for kwset users */
582 extern const char tolower_trans_tbl[256];
583
help.c
+1 -1
@@ -156,7 +156,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
156 continue;
157
158 entlen = strlen(de->d_name) - prefix_len;
159 - if (has_extension(de->d_name, ".exe"))
159 + if (ends_with(de->d_name, ".exe"))
160 entlen -= 4;
161
162 add_cmdname(cmds, de->d_name + prefix_len, entlen);
refs.c
+2 -2
@@ -1151,7 +1151,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
1151
1152 if (de->d_name[0] == '.')
1153 continue;
1154 - if (has_extension(de->d_name, ".lock"))
1154 + if (ends_with(de->d_name, ".lock"))
1155 continue;
1156 strbuf_addstr(&refname, de->d_name);
1157 refdir = *refs->name
@@ -3215,7 +3215,7 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data
3215
3216 if (de->d_name[0] == '.')
3217 continue;
3218 - if (has_extension(de->d_name, ".lock"))
3218 + if (ends_with(de->d_name, ".lock"))
3219 continue;
3220 strbuf_addstr(name, de->d_name);
3221 if (stat(git_path("logs/%s", name->buf), &st) < 0) {
sha1_file.c
+5 -5
@@ -1204,7 +1204,7 @@ static void prepare_packed_git_one(char *objdir, int local)
1204 strbuf_setlen(&path, dirnamelen);
1205 strbuf_addstr(&path, de->d_name);
1206
1207 - if (has_extension(de->d_name, ".idx")) {
1207 + if (ends_with(de->d_name, ".idx")) {
1208 /* Don't reopen a pack we already have. */
1209 for (p = packed_git; p; p = p->next) {
1210 if (!memcmp(path.buf, p->pack_name, path.len - 4))
@@ -1222,10 +1222,10 @@ static void prepare_packed_git_one(char *objdir, int local)
1222 if (!report_garbage)
1223 continue;
1224
1225 - if (has_extension(de->d_name, ".idx") ||
1226 - has_extension(de->d_name, ".pack") ||
1227 - has_extension(de->d_name, ".bitmap") ||
1228 - has_extension(de->d_name, ".keep"))
1225 + if (ends_with(de->d_name, ".idx") ||
1226 + ends_with(de->d_name, ".pack") ||
1227 + ends_with(de->d_name, ".bitmap") ||
1228 + ends_with(de->d_name, ".keep"))
1229 string_list_append(&garbage, path.buf);
1230 else
1231 report_garbage("garbage found", path.buf);