Skip to content

Commit 67e7305

Browse files
committed
Merge branch 'cw/strbuf-cleanup'
Move functions that are not about pure string manipulation out of strbuf.[ch] * cw/strbuf-cleanup: strbuf: remove global variable path: move related function to path object-name: move related functions to object-name credential-store: move related functions to credential-store file abspath: move related functions to abspath strbuf: clarify dependency strbuf: clarify API boundary
2 parents da269af + 787cb8a commit 67e7305

27 files changed

+231
-194
lines changed

abspath.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
289289
return xstrdup(arg);
290290
return prefix_filename(pfx, arg);
291291
}
292+
293+
void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
294+
{
295+
if (!*path)
296+
die("The empty string is not a valid path");
297+
if (!is_absolute_path(path)) {
298+
struct stat cwd_stat, pwd_stat;
299+
size_t orig_len = sb->len;
300+
char *cwd = xgetcwd();
301+
char *pwd = getenv("PWD");
302+
if (pwd && strcmp(pwd, cwd) &&
303+
!stat(cwd, &cwd_stat) &&
304+
(cwd_stat.st_dev || cwd_stat.st_ino) &&
305+
!stat(pwd, &pwd_stat) &&
306+
pwd_stat.st_dev == cwd_stat.st_dev &&
307+
pwd_stat.st_ino == cwd_stat.st_ino)
308+
strbuf_addstr(sb, pwd);
309+
else
310+
strbuf_addstr(sb, cwd);
311+
if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
312+
strbuf_addch(sb, '/');
313+
free(cwd);
314+
}
315+
strbuf_addstr(sb, path);
316+
}
317+
318+
void strbuf_add_real_path(struct strbuf *sb, const char *path)
319+
{
320+
if (sb->len) {
321+
struct strbuf resolved = STRBUF_INIT;
322+
strbuf_realpath(&resolved, path, 1);
323+
strbuf_addbuf(sb, &resolved);
324+
strbuf_release(&resolved);
325+
} else
326+
strbuf_realpath(sb, path, 1);
327+
}

abspath.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
3030
return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
3131
}
3232

33+
/**
34+
* Add a path to a buffer, converting a relative path to an
35+
* absolute one in the process. Symbolic links are not
36+
* resolved.
37+
*/
38+
void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
39+
40+
/**
41+
* Canonize `path` (make it absolute, resolve symlinks, remove extra
42+
* slashes) and append it to `sb`. Die with an informative error
43+
* message if there is a problem.
44+
*
45+
* The directory part of `path` (i.e., everything up to the last
46+
* dir_sep) must denote a valid, existing directory, but the last
47+
* component need not exist.
48+
*
49+
* Callers that don't mind links should use the more lightweight
50+
* strbuf_add_absolute_path() instead.
51+
*/
52+
void strbuf_add_real_path(struct strbuf *sb, const char *path);
53+
3354
#endif /* ABSPATH_H */

add-patch.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11071107
size_t i;
11081108

11091109
strbuf_reset(&s->buf);
1110-
strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for "
1111-
"a quick guide.\n"));
1110+
strbuf_commented_addf(&s->buf, comment_line_char,
1111+
_("Manual hunk edit mode -- see bottom for "
1112+
"a quick guide.\n"));
11121113
render_hunk(s, hunk, 0, 0, &s->buf);
1113-
strbuf_commented_addf(&s->buf,
1114+
strbuf_commented_addf(&s->buf, comment_line_char,
11141115
_("---\n"
11151116
"To remove '%c' lines, make them ' ' lines "
11161117
"(context).\n"
@@ -1119,12 +1120,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
11191120
s->mode->is_reverse ? '+' : '-',
11201121
s->mode->is_reverse ? '-' : '+',
11211122
comment_line_char);
1122-
strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint));
1123+
strbuf_commented_addf(&s->buf, comment_line_char, "%s",
1124+
_(s->mode->edit_hunk_hint));
11231125
/*
11241126
* TRANSLATORS: 'it' refers to the patch mentioned in the previous
11251127
* messages.
11261128
*/
1127-
strbuf_commented_addf(&s->buf,
1129+
strbuf_commented_addf(&s->buf, comment_line_char,
11281130
_("If it does not apply cleanly, you will be "
11291131
"given an opportunity to\n"
11301132
"edit again. If all lines of the hunk are "

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ static int parse_mail(struct am_state *state, const char *mail)
12841284

12851285
strbuf_addstr(&msg, "\n\n");
12861286
strbuf_addbuf(&msg, &mi.log_message);
1287-
strbuf_stripspace(&msg, 0);
1287+
strbuf_stripspace(&msg, '\0');
12881288

12891289
assert(!state->author_name);
12901290
state->author_name = strbuf_detach(&author_name, NULL);

builtin/branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ static int edit_branch_description(const char *branch_name)
665665
exists = !read_branch_desc(&buf, branch_name);
666666
if (!buf.len || buf.buf[buf.len-1] != '\n')
667667
strbuf_addch(&buf, '\n');
668-
strbuf_commented_addf(&buf,
668+
strbuf_commented_addf(&buf, comment_line_char,
669669
_("Please edit the description for the branch\n"
670670
" %s\n"
671671
"Lines starting with '%c' will be stripped.\n"),
@@ -676,7 +676,7 @@ static int edit_branch_description(const char *branch_name)
676676
strbuf_release(&buf);
677677
return -1;
678678
}
679-
strbuf_stripspace(&buf, 1);
679+
strbuf_stripspace(&buf, comment_line_char);
680680

681681
strbuf_addf(&name, "branch.%s.description", branch_name);
682682
if (buf.len || exists)

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
897897
s->hints = 0;
898898

899899
if (clean_message_contents)
900-
strbuf_stripspace(&sb, 0);
900+
strbuf_stripspace(&sb, '\0');
901901

902902
if (signoff)
903903
append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);

builtin/credential-store.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
7575
die_errno("unable to write credential store");
7676
}
7777

78+
static int is_rfc3986_unreserved(char ch)
79+
{
80+
return isalnum(ch) ||
81+
ch == '-' || ch == '_' || ch == '.' || ch == '~';
82+
}
83+
84+
static int is_rfc3986_reserved_or_unreserved(char ch)
85+
{
86+
if (is_rfc3986_unreserved(ch))
87+
return 1;
88+
switch (ch) {
89+
case '!': case '*': case '\'': case '(': case ')': case ';':
90+
case ':': case '@': case '&': case '=': case '+': case '$':
91+
case ',': case '/': case '?': case '#': case '[': case ']':
92+
return 1;
93+
}
94+
return 0;
95+
}
96+
7897
static void store_credential_file(const char *fn, struct credential *c)
7998
{
8099
struct strbuf buf = STRBUF_INIT;

builtin/merge.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,13 +880,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
880880
strbuf_addch(&msg, '\n');
881881
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
882882
wt_status_append_cut_line(&msg);
883-
strbuf_commented_addf(&msg, "\n");
883+
strbuf_commented_addf(&msg, comment_line_char, "\n");
884884
}
885-
strbuf_commented_addf(&msg, _(merge_editor_comment));
885+
strbuf_commented_addf(&msg, comment_line_char,
886+
_(merge_editor_comment));
886887
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
887-
strbuf_commented_addf(&msg, _(scissors_editor_comment));
888+
strbuf_commented_addf(&msg, comment_line_char,
889+
_(scissors_editor_comment));
888890
else
889-
strbuf_commented_addf(&msg,
891+
strbuf_commented_addf(&msg, comment_line_char,
890892
_(no_scissors_editor_comment), comment_line_char);
891893
}
892894
if (signoff)

builtin/notes.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "builtin.h"
1111
#include "config.h"
1212
#include "editor.h"
13+
#include "environment.h"
1314
#include "gettext.h"
1415
#include "hex.h"
1516
#include "notes.h"
@@ -157,7 +158,7 @@ static void write_commented_object(int fd, const struct object_id *object)
157158

158159
if (strbuf_read(&buf, show.out, 0) < 0)
159160
die_errno(_("could not read 'show' output"));
160-
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len);
161+
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
161162
write_or_die(fd, cbuf.buf, cbuf.len);
162163

163164
strbuf_release(&cbuf);
@@ -185,9 +186,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
185186
copy_obj_to_fd(fd, old_note);
186187

187188
strbuf_addch(&buf, '\n');
188-
strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
189-
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)));
190-
strbuf_add_commented_lines(&buf, "\n", strlen("\n"));
189+
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
190+
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
191+
comment_line_char);
192+
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
191193
write_or_die(fd, buf.buf, buf.len);
192194

193195
write_commented_object(fd, object);
@@ -199,7 +201,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
199201
if (launch_editor(d->edit_path, &d->buf, NULL)) {
200202
die(_("please supply the note contents using either -m or -F option"));
201203
}
202-
strbuf_stripspace(&d->buf, 1);
204+
strbuf_stripspace(&d->buf, comment_line_char);
203205
}
204206
}
205207

@@ -225,7 +227,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
225227
if (d->buf.len)
226228
strbuf_addch(&d->buf, '\n');
227229
strbuf_addstr(&d->buf, arg);
228-
strbuf_stripspace(&d->buf, 0);
230+
strbuf_stripspace(&d->buf, '\0');
229231

230232
d->given = 1;
231233
return 0;
@@ -244,7 +246,7 @@ static int parse_file_arg(const struct option *opt, const char *arg, int unset)
244246
die_errno(_("cannot read '%s'"), arg);
245247
} else if (strbuf_read_file(&d->buf, arg, 1024) < 0)
246248
die_errno(_("could not open or read '%s'"), arg);
247-
strbuf_stripspace(&d->buf, 0);
249+
strbuf_stripspace(&d->buf, '\0');
248250

249251
d->given = 1;
250252
return 0;

builtin/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int edit_todo_file(unsigned flags)
210210
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
211211
return error_errno(_("could not read '%s'."), todo_file);
212212

213-
strbuf_stripspace(&todo_list.buf, 1);
213+
strbuf_stripspace(&todo_list.buf, comment_line_char);
214214
res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
215215
if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
216216
NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))

0 commit comments

Comments
 (0)