Skip to content

Commit c9b77f2

Browse files
sunshinecogitster
authored andcommitted
worktree: factor out repeated string literal
For each worktree removed by "git worktree prune", it reports the reason for the removal. All reasons share the common prefix "Removing worktrees/%s:". As new removal reasons are added, this prefix needs to be duplicated, which is error-prone and potentially cumbersome. Therefore, factor out the common prefix. Although this change seems to increase the "sentence lego quotient", it should be reasonably safe, as the reason for removal is a distinct clause, not strictly related to the prefix. Moreover, the "worktrees" in "Removing worktrees/%s:" is a path literal which ought not be localized, so by factoring it out, we can more easily avoid exposing that path fragment to translators. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af6b65d commit c9b77f2

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

builtin/worktree.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,28 @@ static int prune_worktree(const char *id, struct strbuf *reason)
7676
ssize_t read_result;
7777

7878
if (!is_directory(git_path("worktrees/%s", id))) {
79-
strbuf_addf(reason, _("Removing worktrees/%s: not a valid directory"), id);
79+
strbuf_addstr(reason, _("not a valid directory"));
8080
return 1;
8181
}
8282
if (file_exists(git_path("worktrees/%s/locked", id)))
8383
return 0;
8484
if (stat(git_path("worktrees/%s/gitdir", id), &st)) {
85-
strbuf_addf(reason, _("Removing worktrees/%s: gitdir file does not exist"), id);
85+
strbuf_addstr(reason, _("gitdir file does not exist"));
8686
return 1;
8787
}
8888
fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
8989
if (fd < 0) {
90-
strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
91-
id, strerror(errno));
90+
strbuf_addf(reason, _("unable to read gitdir file (%s)"),
91+
strerror(errno));
9292
return 1;
9393
}
9494
len = xsize_t(st.st_size);
9595
path = xmallocz(len);
9696

9797
read_result = read_in_full(fd, path, len);
9898
if (read_result < 0) {
99-
strbuf_addf(reason, _("Removing worktrees/%s: unable to read gitdir file (%s)"),
100-
id, strerror(errno));
99+
strbuf_addf(reason, _("unable to read gitdir file (%s)"),
100+
strerror(errno));
101101
close(fd);
102102
free(path);
103103
return 1;
@@ -106,15 +106,15 @@ static int prune_worktree(const char *id, struct strbuf *reason)
106106

107107
if (read_result != len) {
108108
strbuf_addf(reason,
109-
_("Removing worktrees/%s: short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
110-
id, (uintmax_t)len, (uintmax_t)read_result);
109+
_("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"),
110+
(uintmax_t)len, (uintmax_t)read_result);
111111
free(path);
112112
return 1;
113113
}
114114
while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
115115
len--;
116116
if (!len) {
117-
strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
117+
strbuf_addstr(reason, _("invalid gitdir file"));
118118
free(path);
119119
return 1;
120120
}
@@ -123,7 +123,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)
123123
free(path);
124124
if (stat(git_path("worktrees/%s/index", id), &st) ||
125125
st.st_mtime <= expire) {
126-
strbuf_addf(reason, _("Removing worktrees/%s: gitdir file points to non-existent location"), id);
126+
strbuf_addstr(reason, _("gitdir file points to non-existent location"));
127127
return 1;
128128
} else {
129129
return 0;
@@ -147,7 +147,8 @@ static void prune_worktrees(void)
147147
if (!prune_worktree(d->d_name, &reason))
148148
continue;
149149
if (show_only || verbose)
150-
printf("%s\n", reason.buf);
150+
printf_ln(_("Removing %s/%s: %s"),
151+
"worktrees", d->d_name, reason.buf);
151152
if (show_only)
152153
continue;
153154
delete_git_dir(d->d_name);

0 commit comments

Comments
 (0)