@matheusbsbs / matheus-bagliano / commits / c9b77f2cea
Fork 0

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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jun 8, 2020 at 02:23 UTC c9b77f2cea2f0b4ee34d1d7a25cd1ebed767caf4
1 file changed +12 -11
builtin/worktree.c
+12 -11
@@ -76,19 +76,19 @@ static int prune_worktree(const char *id, struct strbuf *reason)
76 ssize_t read_result;
77
78 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"));
80 return 1;
81 }
82 if (file_exists(git_path("worktrees/%s/locked", id)))
83 return 0;
84 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"));
86 return 1;
87 }
88 fd = open(git_path("worktrees/%s/gitdir", id), O_RDONLY);
89 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));
92 return 1;
93 }
94 len = xsize_t(st.st_size);
@@ -96,8 +96,8 @@ static int prune_worktree(const char *id, struct strbuf *reason)
96
97 read_result = read_in_full(fd, path, len);
98 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));
101 close(fd);
102 free(path);
103 return 1;
@@ -106,15 +106,15 @@ static int prune_worktree(const char *id, struct strbuf *reason)
106
107 if (read_result != len) {
108 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);
111 free(path);
112 return 1;
113 }
114 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
115 len--;
116 if (!len) {
117 - strbuf_addf(reason, _("Removing worktrees/%s: invalid gitdir file"), id);
117 + strbuf_addstr(reason, _("invalid gitdir file"));
118 free(path);
119 return 1;
120 }
@@ -123,7 +123,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)
123 free(path);
124 if (stat(git_path("worktrees/%s/index", id), &st) ||
125 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"));
127 return 1;
128 } else {
129 return 0;
@@ -147,7 +147,8 @@ static void prune_worktrees(void)
147 if (!prune_worktree(d->d_name, &reason))
148 continue;
149 if (show_only || verbose)
150 - printf("%s\n", reason.buf);
150 + printf_ln(_("Removing %s/%s: %s"),
151 + "worktrees", d->d_name, reason.buf);
152 if (show_only)
153 continue;
154 delete_git_dir(d->d_name);