Skip to content

Commit 2975c77

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f52a35f commit 2975c77

File tree

6 files changed

+12
-19
lines changed

6 files changed

+12
-19
lines changed

builtin/index-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16031603
die(_("--fix-thin cannot be used without --stdin"));
16041604
if (!index_name && pack_name) {
16051605
int len = strlen(pack_name);
1606-
if (!has_extension(pack_name, ".pack"))
1606+
if (!ends_with(pack_name, ".pack"))
16071607
die(_("packfile name '%s' does not end with '.pack'"),
16081608
pack_name);
16091609
index_name_buf = xmalloc(len);
@@ -1613,7 +1613,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
16131613
}
16141614
if (keep_msg && !keep_name && pack_name) {
16151615
int len = strlen(pack_name);
1616-
if (!has_extension(pack_name, ".pack"))
1616+
if (!ends_with(pack_name, ".pack"))
16171617
die(_("packfile name '%s' does not end with '.pack'"),
16181618
pack_name);
16191619
keep_name_buf = xmalloc(len);

builtin/verify-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ static int verify_one_pack(const char *path, unsigned int flags)
2727
* normalize these forms to "foo.pack" for "index-pack --verify".
2828
*/
2929
strbuf_addstr(&arg, path);
30-
if (has_extension(arg.buf, ".idx"))
30+
if (ends_with(arg.buf, ".idx"))
3131
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"))
3333
strbuf_add(&arg, ".pack", 5);
3434
argv[2] = arg.buf;
3535

git-compat-util.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,6 @@ static inline size_t xsize_t(off_t len)
578578
return (size_t)len;
579579
}
580580

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-
588581
/* in ctype.c, for kwset users */
589582
extern const char tolower_trans_tbl[256];
590583

help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static void list_commands_in_dir(struct cmdnames *cmds,
156156
continue;
157157

158158
entlen = strlen(de->d_name) - prefix_len;
159-
if (has_extension(de->d_name, ".exe"))
159+
if (ends_with(de->d_name, ".exe"))
160160
entlen -= 4;
161161

162162
add_cmdname(cmds, de->d_name + prefix_len, entlen);

refs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
11511151

11521152
if (de->d_name[0] == '.')
11531153
continue;
1154-
if (has_extension(de->d_name, ".lock"))
1154+
if (ends_with(de->d_name, ".lock"))
11551155
continue;
11561156
strbuf_addstr(&refname, de->d_name);
11571157
refdir = *refs->name
@@ -3215,7 +3215,7 @@ static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data
32153215

32163216
if (de->d_name[0] == '.')
32173217
continue;
3218-
if (has_extension(de->d_name, ".lock"))
3218+
if (ends_with(de->d_name, ".lock"))
32193219
continue;
32203220
strbuf_addstr(name, de->d_name);
32213221
if (stat(git_path("logs/%s", name->buf), &st) < 0) {

sha1_file.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ static void prepare_packed_git_one(char *objdir, int local)
12041204
strbuf_setlen(&path, dirnamelen);
12051205
strbuf_addstr(&path, de->d_name);
12061206

1207-
if (has_extension(de->d_name, ".idx")) {
1207+
if (ends_with(de->d_name, ".idx")) {
12081208
/* Don't reopen a pack we already have. */
12091209
for (p = packed_git; p; p = p->next) {
12101210
if (!memcmp(path.buf, p->pack_name, path.len - 4))
@@ -1222,10 +1222,10 @@ static void prepare_packed_git_one(char *objdir, int local)
12221222
if (!report_garbage)
12231223
continue;
12241224

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"))
12291229
string_list_append(&garbage, path.buf);
12301230
else
12311231
report_garbage("garbage found", path.buf);

0 commit comments

Comments
 (0)