1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-06 02:56:11 +02:00

Merge branch 'cw/strbuf-cleanup' into next

Move functions that are not about pure string manipulation out of
strbuf.[ch]

* cw/strbuf-cleanup:
  strbuf: remove global variable
  strbuf: clarify dependency
  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 API boundary
This commit is contained in:
Junio C Hamano 2023-05-15 14:51:06 -07:00
commit 31d08296f2
27 changed files with 227 additions and 190 deletions

View File

@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
return xstrdup(arg); return xstrdup(arg);
return prefix_filename(pfx, arg); return prefix_filename(pfx, arg);
} }
void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
{
if (!*path)
die("The empty string is not a valid path");
if (!is_absolute_path(path)) {
struct stat cwd_stat, pwd_stat;
size_t orig_len = sb->len;
char *cwd = xgetcwd();
char *pwd = getenv("PWD");
if (pwd && strcmp(pwd, cwd) &&
!stat(cwd, &cwd_stat) &&
(cwd_stat.st_dev || cwd_stat.st_ino) &&
!stat(pwd, &pwd_stat) &&
pwd_stat.st_dev == cwd_stat.st_dev &&
pwd_stat.st_ino == cwd_stat.st_ino)
strbuf_addstr(sb, pwd);
else
strbuf_addstr(sb, cwd);
if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
strbuf_addch(sb, '/');
free(cwd);
}
strbuf_addstr(sb, path);
}
void strbuf_add_real_path(struct strbuf *sb, const char *path)
{
if (sb->len) {
struct strbuf resolved = STRBUF_INIT;
strbuf_realpath(&resolved, path, 1);
strbuf_addbuf(sb, &resolved);
strbuf_release(&resolved);
} else
strbuf_realpath(sb, path, 1);
}

View File

@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
return is_dir_sep(path[0]) || has_dos_drive_prefix(path); return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
} }
/**
* Add a path to a buffer, converting a relative path to an
* absolute one in the process. Symbolic links are not
* resolved.
*/
void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
/**
* Canonize `path` (make it absolute, resolve symlinks, remove extra
* slashes) and append it to `sb`. Die with an informative error
* message if there is a problem.
*
* The directory part of `path` (i.e., everything up to the last
* dir_sep) must denote a valid, existing directory, but the last
* component need not exist.
*
* Callers that don't mind links should use the more lightweight
* strbuf_add_absolute_path() instead.
*/
void strbuf_add_real_path(struct strbuf *sb, const char *path);
#endif /* ABSPATH_H */ #endif /* ABSPATH_H */

View File

@ -1105,10 +1105,11 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
size_t i; size_t i;
strbuf_reset(&s->buf); strbuf_reset(&s->buf);
strbuf_commented_addf(&s->buf, _("Manual hunk edit mode -- see bottom for " strbuf_commented_addf(&s->buf, comment_line_char,
"a quick guide.\n")); _("Manual hunk edit mode -- see bottom for "
"a quick guide.\n"));
render_hunk(s, hunk, 0, 0, &s->buf); render_hunk(s, hunk, 0, 0, &s->buf);
strbuf_commented_addf(&s->buf, strbuf_commented_addf(&s->buf, comment_line_char,
_("---\n" _("---\n"
"To remove '%c' lines, make them ' ' lines " "To remove '%c' lines, make them ' ' lines "
"(context).\n" "(context).\n"
@ -1117,12 +1118,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
s->mode->is_reverse ? '+' : '-', s->mode->is_reverse ? '+' : '-',
s->mode->is_reverse ? '-' : '+', s->mode->is_reverse ? '-' : '+',
comment_line_char); comment_line_char);
strbuf_commented_addf(&s->buf, "%s", _(s->mode->edit_hunk_hint)); strbuf_commented_addf(&s->buf, comment_line_char, "%s",
_(s->mode->edit_hunk_hint));
/* /*
* TRANSLATORS: 'it' refers to the patch mentioned in the previous * TRANSLATORS: 'it' refers to the patch mentioned in the previous
* messages. * messages.
*/ */
strbuf_commented_addf(&s->buf, strbuf_commented_addf(&s->buf, comment_line_char,
_("If it does not apply cleanly, you will be " _("If it does not apply cleanly, you will be "
"given an opportunity to\n" "given an opportunity to\n"
"edit again. If all lines of the hunk are " "edit again. If all lines of the hunk are "

View File

@ -1283,7 +1283,7 @@ static int parse_mail(struct am_state *state, const char *mail)
strbuf_addstr(&msg, "\n\n"); strbuf_addstr(&msg, "\n\n");
strbuf_addbuf(&msg, &mi.log_message); strbuf_addbuf(&msg, &mi.log_message);
strbuf_stripspace(&msg, 0); strbuf_stripspace(&msg, '\0');
assert(!state->author_name); assert(!state->author_name);
state->author_name = strbuf_detach(&author_name, NULL); state->author_name = strbuf_detach(&author_name, NULL);

View File

@ -674,7 +674,7 @@ static int edit_branch_description(const char *branch_name)
exists = !read_branch_desc(&buf, branch_name); exists = !read_branch_desc(&buf, branch_name);
if (!buf.len || buf.buf[buf.len-1] != '\n') if (!buf.len || buf.buf[buf.len-1] != '\n')
strbuf_addch(&buf, '\n'); strbuf_addch(&buf, '\n');
strbuf_commented_addf(&buf, strbuf_commented_addf(&buf, comment_line_char,
_("Please edit the description for the branch\n" _("Please edit the description for the branch\n"
" %s\n" " %s\n"
"Lines starting with '%c' will be stripped.\n"), "Lines starting with '%c' will be stripped.\n"),
@ -685,7 +685,7 @@ static int edit_branch_description(const char *branch_name)
strbuf_release(&buf); strbuf_release(&buf);
return -1; return -1;
} }
strbuf_stripspace(&buf, 1); strbuf_stripspace(&buf, comment_line_char);
strbuf_addf(&name, "branch.%s.description", branch_name); strbuf_addf(&name, "branch.%s.description", branch_name);
if (buf.len || exists) if (buf.len || exists)

View File

@ -893,7 +893,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
s->hints = 0; s->hints = 0;
if (clean_message_contents) if (clean_message_contents)
strbuf_stripspace(&sb, 0); strbuf_stripspace(&sb, '\0');
if (signoff) if (signoff)
append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0); append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);

View File

@ -74,6 +74,25 @@ static void rewrite_credential_file(const char *fn, struct credential *c,
die_errno("unable to write credential store"); die_errno("unable to write credential store");
} }
static int is_rfc3986_unreserved(char ch)
{
return isalnum(ch) ||
ch == '-' || ch == '_' || ch == '.' || ch == '~';
}
static int is_rfc3986_reserved_or_unreserved(char ch)
{
if (is_rfc3986_unreserved(ch))
return 1;
switch (ch) {
case '!': case '*': case '\'': case '(': case ')': case ';':
case ':': case '@': case '&': case '=': case '+': case '$':
case ',': case '/': case '?': case '#': case '[': case ']':
return 1;
}
return 0;
}
static void store_credential_file(const char *fn, struct credential *c) static void store_credential_file(const char *fn, struct credential *c)
{ {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;

View File

@ -879,13 +879,15 @@ static void prepare_to_commit(struct commit_list *remoteheads)
strbuf_addch(&msg, '\n'); strbuf_addch(&msg, '\n');
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
wt_status_append_cut_line(&msg); wt_status_append_cut_line(&msg);
strbuf_commented_addf(&msg, "\n"); strbuf_commented_addf(&msg, comment_line_char, "\n");
} }
strbuf_commented_addf(&msg, _(merge_editor_comment)); strbuf_commented_addf(&msg, comment_line_char,
_(merge_editor_comment));
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
strbuf_commented_addf(&msg, _(scissors_editor_comment)); strbuf_commented_addf(&msg, comment_line_char,
_(scissors_editor_comment));
else else
strbuf_commented_addf(&msg, strbuf_commented_addf(&msg, comment_line_char,
_(no_scissors_editor_comment), comment_line_char); _(no_scissors_editor_comment), comment_line_char);
} }
if (signoff) if (signoff)

View File

@ -12,6 +12,7 @@
#include "config.h" #include "config.h"
#include "builtin.h" #include "builtin.h"
#include "editor.h" #include "editor.h"
#include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"
#include "notes.h" #include "notes.h"
@ -180,7 +181,7 @@ static void write_commented_object(int fd, const struct object_id *object)
if (strbuf_read(&buf, show.out, 0) < 0) if (strbuf_read(&buf, show.out, 0) < 0)
die_errno(_("could not read 'show' output")); die_errno(_("could not read 'show' output"));
strbuf_add_commented_lines(&cbuf, buf.buf, buf.len); strbuf_add_commented_lines(&cbuf, buf.buf, buf.len, comment_line_char);
write_or_die(fd, cbuf.buf, cbuf.len); write_or_die(fd, cbuf.buf, cbuf.len);
strbuf_release(&cbuf); strbuf_release(&cbuf);
@ -208,9 +209,10 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
copy_obj_to_fd(fd, old_note); copy_obj_to_fd(fd, old_note);
strbuf_addch(&buf, '\n'); strbuf_addch(&buf, '\n');
strbuf_add_commented_lines(&buf, "\n", strlen("\n")); strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template))); strbuf_add_commented_lines(&buf, _(note_template), strlen(_(note_template)),
strbuf_add_commented_lines(&buf, "\n", strlen("\n")); comment_line_char);
strbuf_add_commented_lines(&buf, "\n", strlen("\n"), comment_line_char);
write_or_die(fd, buf.buf, buf.len); write_or_die(fd, buf.buf, buf.len);
write_commented_object(fd, object); write_commented_object(fd, object);
@ -223,7 +225,7 @@ static void prepare_note_data(const struct object_id *object, struct note_data *
die(_("please supply the note contents using either -m or -F option")); die(_("please supply the note contents using either -m or -F option"));
} }
if (d->stripspace) if (d->stripspace)
strbuf_stripspace(&d->buf, 1); strbuf_stripspace(&d->buf, comment_line_char);
} }
} }

View File

@ -209,7 +209,7 @@ static int edit_todo_file(unsigned flags)
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file); return error_errno(_("could not read '%s'."), todo_file);
strbuf_stripspace(&todo_list.buf, 1); strbuf_stripspace(&todo_list.buf, comment_line_char);
res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags); res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file, if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS))) NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))

View File

@ -1,6 +1,7 @@
#include "builtin.h" #include "builtin.h"
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "parse-options.h" #include "parse-options.h"
#include "setup.h" #include "setup.h"
@ -13,7 +14,7 @@ static void comment_lines(struct strbuf *buf)
size_t len; size_t len;
msg = strbuf_detach(buf, &len); msg = strbuf_detach(buf, &len);
strbuf_add_commented_lines(buf, msg, len); strbuf_add_commented_lines(buf, msg, len, comment_line_char);
free(msg); free(msg);
} }
@ -58,7 +59,8 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
die_errno("could not read the input"); die_errno("could not read the input");
if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS) if (mode == STRIP_DEFAULT || mode == STRIP_COMMENTS)
strbuf_stripspace(&buf, mode == STRIP_COMMENTS); strbuf_stripspace(&buf,
mode == STRIP_COMMENTS ? comment_line_char : '\0');
else else
comment_lines(&buf); comment_lines(&buf);

View File

@ -311,9 +311,11 @@ static void create_tag(const struct object_id *object, const char *object_ref,
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
strbuf_addch(&buf, '\n'); strbuf_addch(&buf, '\n');
if (opt->cleanup_mode == CLEANUP_ALL) if (opt->cleanup_mode == CLEANUP_ALL)
strbuf_commented_addf(&buf, _(tag_template), tag, comment_line_char); strbuf_commented_addf(&buf, comment_line_char,
_(tag_template), tag, comment_line_char);
else else
strbuf_commented_addf(&buf, _(tag_template_nocleanup), tag, comment_line_char); strbuf_commented_addf(&buf, comment_line_char,
_(tag_template_nocleanup), tag, comment_line_char);
write_or_die(fd, buf.buf, buf.len); write_or_die(fd, buf.buf, buf.len);
strbuf_release(&buf); strbuf_release(&buf);
} }
@ -327,7 +329,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
} }
if (opt->cleanup_mode != CLEANUP_NONE) if (opt->cleanup_mode != CLEANUP_NONE)
strbuf_stripspace(buf, opt->cleanup_mode == CLEANUP_ALL); strbuf_stripspace(buf,
opt->cleanup_mode == CLEANUP_ALL ? comment_line_char : '\0');
if (!opt->message_given && !buf->len) if (!opt->message_given && !buf->len)
die(_("no tag message?")); die(_("no tag message?"));

2
dir.h
View File

@ -6,6 +6,8 @@
#include "statinfo.h" #include "statinfo.h"
#include "strbuf.h" #include "strbuf.h"
struct repository;
/** /**
* The directory listing API is used to enumerate paths in the work tree, * The directory listing API is used to enumerate paths in the work tree,
* optionally taking `.git/info/exclude` and `.gitignore` files per directory * optionally taking `.git/info/exclude` and `.gitignore` files per directory

View File

@ -508,7 +508,8 @@ static void fmt_tag_signature(struct strbuf *tagbuf,
strbuf_complete_line(tagbuf); strbuf_complete_line(tagbuf);
if (sig->len) { if (sig->len) {
strbuf_addch(tagbuf, '\n'); strbuf_addch(tagbuf, '\n');
strbuf_add_commented_lines(tagbuf, sig->buf, sig->len); strbuf_add_commented_lines(tagbuf, sig->buf, sig->len,
comment_line_char);
} }
} }
@ -554,7 +555,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
strbuf_addch(&tagline, '\n'); strbuf_addch(&tagline, '\n');
strbuf_add_commented_lines(&tagline, strbuf_add_commented_lines(&tagline,
origins.items[first_tag].string, origins.items[first_tag].string,
strlen(origins.items[first_tag].string)); strlen(origins.items[first_tag].string),
comment_line_char);
strbuf_insert(&tagbuf, 0, tagline.buf, strbuf_insert(&tagbuf, 0, tagline.buf,
tagline.len); tagline.len);
strbuf_release(&tagline); strbuf_release(&tagline);
@ -562,7 +564,8 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
strbuf_addch(&tagbuf, '\n'); strbuf_addch(&tagbuf, '\n');
strbuf_add_commented_lines(&tagbuf, strbuf_add_commented_lines(&tagbuf,
origins.items[i].string, origins.items[i].string,
strlen(origins.items[i].string)); strlen(origins.items[i].string),
comment_line_char);
fmt_tag_signature(&tagbuf, &sig, buf, len); fmt_tag_signature(&tagbuf, &sig, buf, len);
} }
strbuf_release(&payload); strbuf_release(&payload);

View File

@ -13,6 +13,7 @@
#include "tempfile.h" #include "tempfile.h"
#include "alias.h" #include "alias.h"
#include "wrapper.h" #include "wrapper.h"
#include "environment.h"
static int git_gpg_config(const char *, const char *, void *); static int git_gpg_config(const char *, const char *, void *);
@ -586,8 +587,8 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
} }
} }
strbuf_stripspace(&ssh_keygen_out, 0); strbuf_stripspace(&ssh_keygen_out, '\0');
strbuf_stripspace(&ssh_keygen_err, 0); strbuf_stripspace(&ssh_keygen_err, '\0');
/* Add stderr outputs to show the user actual ssh-keygen errors */ /* Add stderr outputs to show the user actual ssh-keygen errors */
strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len); strbuf_add(&ssh_keygen_out, ssh_principals_err.buf, ssh_principals_err.len);
strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len); strbuf_add(&ssh_keygen_out, ssh_keygen_err.buf, ssh_keygen_err.len);

1
hook.c
View File

@ -1,4 +1,5 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h"
#include "advice.h" #include "advice.h"
#include "gettext.h" #include "gettext.h"
#include "hook.h" #include "hook.h"

View File

@ -768,6 +768,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
find_abbrev_len_for_pack(p, mad); find_abbrev_len_for_pack(p, mad);
} }
void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
const struct object_id *oid, int abbrev_len)
{
int r;
strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
strbuf_setlen(sb, sb->len + r);
}
void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len)
{
strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
}
int repo_find_unique_abbrev_r(struct repository *r, char *hex, int repo_find_unique_abbrev_r(struct repository *r, char *hex,
const struct object_id *oid, int len) const struct object_id *oid, int len)
{ {

View File

@ -40,6 +40,15 @@ struct object_context {
const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len); const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len); int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
/**
* Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
* the strbuf `sb`.
*/
void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
const struct object_id *oid, int abbrev_len);
void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len);
int repo_get_oid(struct repository *r, const char *str, struct object_id *oid); int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
__attribute__((format (printf, 2, 3))) __attribute__((format (printf, 2, 3)))
int get_oidf(struct object_id *oid, const char *fmt, ...); int get_oidf(struct object_id *oid, const char *fmt, ...);

20
path.c
View File

@ -1213,6 +1213,26 @@ int normalize_path_copy(char *dst, const char *src)
return normalize_path_copy_len(dst, src, NULL); return normalize_path_copy_len(dst, src, NULL);
} }
int strbuf_normalize_path(struct strbuf *src)
{
struct strbuf dst = STRBUF_INIT;
strbuf_grow(&dst, src->len);
if (normalize_path_copy(dst.buf, src->buf) < 0) {
strbuf_release(&dst);
return -1;
}
/*
* normalize_path does not tell us the new length, so we have to
* compute it by looking for the new NUL it placed
*/
strbuf_setlen(&dst, strlen(dst.buf));
strbuf_swap(src, &dst);
strbuf_release(&dst);
return 0;
}
/* /*
* path = Canonical absolute path * path = Canonical absolute path
* prefixes = string_list containing normalized, absolute paths without * prefixes = string_list containing normalized, absolute paths without

5
path.h
View File

@ -191,6 +191,11 @@ const char *remove_leading_path(const char *in, const char *prefix);
const char *relative_path(const char *in, const char *prefix, struct strbuf *sb); const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
int normalize_path_copy_len(char *dst, const char *src, int *prefix_len); int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);
int normalize_path_copy(char *dst, const char *src); int normalize_path_copy(char *dst, const char *src);
/**
* Normalize in-place the path contained in the strbuf. If an error occurs,
* the contents of "sb" are left untouched, and -1 is returned.
*/
int strbuf_normalize_path(struct strbuf *src);
int longest_ancestor_length(const char *path, struct string_list *prefixes); int longest_ancestor_length(const char *path, struct string_list *prefixes);
char *strip_path_suffix(const char *path, const char *suffix); char *strip_path_suffix(const char *path, const char *suffix);
int daemon_avoid_alias(const char *path); int daemon_avoid_alias(const char *path);

View File

@ -18,6 +18,7 @@
#include "gpg-interface.h" #include "gpg-interface.h"
#include "trailer.h" #include "trailer.h"
#include "run-command.h" #include "run-command.h"
#include "object-name.h"
/* /*
* The limit for formatting directives, which enable the caller to append * The limit for formatting directives, which enable the caller to append

View File

@ -72,13 +72,14 @@ void append_todo_help(int command_count,
if (!edit_todo) { if (!edit_todo) {
strbuf_addch(buf, '\n'); strbuf_addch(buf, '\n');
strbuf_commented_addf(buf, Q_("Rebase %s onto %s (%d command)", strbuf_commented_addf(buf, comment_line_char,
"Rebase %s onto %s (%d commands)", Q_("Rebase %s onto %s (%d command)",
command_count), "Rebase %s onto %s (%d commands)",
command_count),
shortrevisions, shortonto, command_count); shortrevisions, shortonto, command_count);
} }
strbuf_add_commented_lines(buf, msg, strlen(msg)); strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR) if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
msg = _("\nDo not remove any line. Use 'drop' " msg = _("\nDo not remove any line. Use 'drop' "
@ -87,7 +88,7 @@ void append_todo_help(int command_count,
msg = _("\nIf you remove a line here " msg = _("\nIf you remove a line here "
"THAT COMMIT WILL BE LOST.\n"); "THAT COMMIT WILL BE LOST.\n");
strbuf_add_commented_lines(buf, msg, strlen(msg)); strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
if (edit_todo) if (edit_todo)
msg = _("\nYou are editing the todo file " msg = _("\nYou are editing the todo file "
@ -98,7 +99,7 @@ void append_todo_help(int command_count,
msg = _("\nHowever, if you remove everything, " msg = _("\nHowever, if you remove everything, "
"the rebase will be aborted.\n\n"); "the rebase will be aborted.\n\n");
strbuf_add_commented_lines(buf, msg, strlen(msg)); strbuf_add_commented_lines(buf, msg, strlen(msg), comment_line_char);
} }
int edit_todo_list(struct repository *r, struct todo_list *todo_list, int edit_todo_list(struct repository *r, struct todo_list *todo_list,
@ -130,7 +131,7 @@ int edit_todo_list(struct repository *r, struct todo_list *todo_list,
if (launch_sequence_editor(todo_file, &new_todo->buf, NULL)) if (launch_sequence_editor(todo_file, &new_todo->buf, NULL))
return -2; return -2;
strbuf_stripspace(&new_todo->buf, 1); strbuf_stripspace(&new_todo->buf, comment_line_char);
if (initial && new_todo->buf.len == 0) if (initial && new_todo->buf.len == 0)
return -3; return -3;

View File

@ -660,11 +660,12 @@ void append_conflicts_hint(struct index_state *istate,
} }
strbuf_addch(msgbuf, '\n'); strbuf_addch(msgbuf, '\n');
strbuf_commented_addf(msgbuf, "Conflicts:\n"); strbuf_commented_addf(msgbuf, comment_line_char, "Conflicts:\n");
for (i = 0; i < istate->cache_nr;) { for (i = 0; i < istate->cache_nr;) {
const struct cache_entry *ce = istate->cache[i++]; const struct cache_entry *ce = istate->cache[i++];
if (ce_stage(ce)) { if (ce_stage(ce)) {
strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); strbuf_commented_addf(msgbuf, comment_line_char,
"\t%s\n", ce->name);
while (i < istate->cache_nr && while (i < istate->cache_nr &&
!strcmp(ce->name, istate->cache[i]->name)) !strcmp(ce->name, istate->cache[i]->name))
i++; i++;
@ -1143,7 +1144,8 @@ void cleanup_message(struct strbuf *msgbuf,
cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len)); strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE) if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); strbuf_stripspace(msgbuf,
cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
} }
/* /*
@ -1174,7 +1176,8 @@ int template_untouched(const struct strbuf *sb, const char *template_file,
if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
return 0; return 0;
strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); strbuf_stripspace(&tmpl,
cleanup_mode == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
if (!skip_prefix(sb->buf, tmpl.buf, &start)) if (!skip_prefix(sb->buf, tmpl.buf, &start))
start = sb->buf; start = sb->buf;
strbuf_release(&tmpl); strbuf_release(&tmpl);
@ -1546,7 +1549,8 @@ static int try_to_commit(struct repository *r,
cleanup = opts->default_msg_cleanup; cleanup = opts->default_msg_cleanup;
if (cleanup != COMMIT_MSG_CLEANUP_NONE) if (cleanup != COMMIT_MSG_CLEANUP_NONE)
strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL); strbuf_stripspace(msg,
cleanup == COMMIT_MSG_CLEANUP_ALL ? comment_line_char : '\0');
if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) { if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
res = 1; /* run 'git commit' to display error message */ res = 1; /* run 'git commit' to display error message */
goto out; goto out;
@ -1840,7 +1844,7 @@ static void add_commented_lines(struct strbuf *buf, const void *str, size_t len)
s += count; s += count;
len -= count; len -= count;
} }
strbuf_add_commented_lines(buf, s, len); strbuf_add_commented_lines(buf, s, len, comment_line_char);
} }
/* Does the current fixup chain contain a squash command? */ /* Does the current fixup chain contain a squash command? */
@ -1939,7 +1943,7 @@ static int append_squash_message(struct strbuf *buf, const char *body,
strbuf_addf(buf, _(nth_commit_msg_fmt), strbuf_addf(buf, _(nth_commit_msg_fmt),
++opts->current_fixup_count + 1); ++opts->current_fixup_count + 1);
strbuf_addstr(buf, "\n\n"); strbuf_addstr(buf, "\n\n");
strbuf_add_commented_lines(buf, body, commented_len); strbuf_add_commented_lines(buf, body, commented_len, comment_line_char);
/* buf->buf may be reallocated so store an offset into the buffer */ /* buf->buf may be reallocated so store an offset into the buffer */
fixup_off = buf->len; fixup_off = buf->len;
strbuf_addstr(buf, body + commented_len); strbuf_addstr(buf, body + commented_len);
@ -2029,7 +2033,8 @@ static int update_squash_messages(struct repository *r,
_(first_commit_msg_str)); _(first_commit_msg_str));
strbuf_addstr(&buf, "\n\n"); strbuf_addstr(&buf, "\n\n");
if (is_fixup_flag(command, flag)) if (is_fixup_flag(command, flag))
strbuf_add_commented_lines(&buf, body, strlen(body)); strbuf_add_commented_lines(&buf, body, strlen(body),
comment_line_char);
else else
strbuf_addstr(&buf, body); strbuf_addstr(&buf, body);
@ -2048,7 +2053,8 @@ static int update_squash_messages(struct repository *r,
strbuf_addf(&buf, _(skip_nth_commit_msg_fmt), strbuf_addf(&buf, _(skip_nth_commit_msg_fmt),
++opts->current_fixup_count + 1); ++opts->current_fixup_count + 1);
strbuf_addstr(&buf, "\n\n"); strbuf_addstr(&buf, "\n\n");
strbuf_add_commented_lines(&buf, body, strlen(body)); strbuf_add_commented_lines(&buf, body, strlen(body),
comment_line_char);
} else } else
return error(_("unknown command: %d"), command); return error(_("unknown command: %d"), command);
repo_unuse_commit_buffer(r, commit, message); repo_unuse_commit_buffer(r, commit, message);

112
strbuf.c
View File

@ -1,12 +1,9 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h" #include "alloc.h"
#include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "hex.h" #include "hex.h"
#include "object-name.h"
#include "refs.h"
#include "repository.h" #include "repository.h"
#include "strbuf.h"
#include "string-list.h" #include "string-list.h"
#include "utf8.h" #include "utf8.h"
#include "date.h" #include "date.h"
@ -365,7 +362,8 @@ static void add_lines(struct strbuf *out,
strbuf_complete_line(out); strbuf_complete_line(out);
} }
void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size) void strbuf_add_commented_lines(struct strbuf *out, const char *buf,
size_t size, char comment_line_char)
{ {
static char prefix1[3]; static char prefix1[3];
static char prefix2[2]; static char prefix2[2];
@ -377,7 +375,8 @@ void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size
add_lines(out, prefix1, prefix2, buf, size); add_lines(out, prefix1, prefix2, buf, size);
} }
void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...) void strbuf_commented_addf(struct strbuf *sb, char comment_line_char,
const char *fmt, ...)
{ {
va_list params; va_list params;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
@ -387,7 +386,7 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
strbuf_vaddf(&buf, fmt, params); strbuf_vaddf(&buf, fmt, params);
va_end(params); va_end(params);
strbuf_add_commented_lines(sb, buf.buf, buf.len); strbuf_add_commented_lines(sb, buf.buf, buf.len, comment_line_char);
if (incomplete_line) if (incomplete_line)
sb->buf[--sb->len] = '\0'; sb->buf[--sb->len] = '\0';
@ -811,25 +810,6 @@ void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
} }
} }
int is_rfc3986_reserved_or_unreserved(char ch)
{
if (is_rfc3986_unreserved(ch))
return 1;
switch (ch) {
case '!': case '*': case '\'': case '(': case ')': case ';':
case ':': case '@': case '&': case '=': case '+': case '$':
case ',': case '/': case '?': case '#': case '[': case ']':
return 1;
}
return 0;
}
int is_rfc3986_unreserved(char ch)
{
return isalnum(ch) ||
ch == '-' || ch == '_' || ch == '.' || ch == '~';
}
static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
char_predicate allow_unencoded_fn) char_predicate allow_unencoded_fn)
{ {
@ -900,42 +880,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
strbuf_humanise(buf, bytes, 1); strbuf_humanise(buf, bytes, 1);
} }
void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
{
if (!*path)
die("The empty string is not a valid path");
if (!is_absolute_path(path)) {
struct stat cwd_stat, pwd_stat;
size_t orig_len = sb->len;
char *cwd = xgetcwd();
char *pwd = getenv("PWD");
if (pwd && strcmp(pwd, cwd) &&
!stat(cwd, &cwd_stat) &&
(cwd_stat.st_dev || cwd_stat.st_ino) &&
!stat(pwd, &pwd_stat) &&
pwd_stat.st_dev == cwd_stat.st_dev &&
pwd_stat.st_ino == cwd_stat.st_ino)
strbuf_addstr(sb, pwd);
else
strbuf_addstr(sb, cwd);
if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
strbuf_addch(sb, '/');
free(cwd);
}
strbuf_addstr(sb, path);
}
void strbuf_add_real_path(struct strbuf *sb, const char *path)
{
if (sb->len) {
struct strbuf resolved = STRBUF_INIT;
strbuf_realpath(&resolved, path, 1);
strbuf_addbuf(sb, &resolved);
strbuf_release(&resolved);
} else
strbuf_realpath(sb, path, 1);
}
int printf_ln(const char *fmt, ...) int printf_ln(const char *fmt, ...)
{ {
int ret; int ret;
@ -1080,21 +1024,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
strbuf_setlen(sb, sb->len + len); strbuf_setlen(sb, sb->len + len);
} }
void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
const struct object_id *oid, int abbrev_len)
{
int r;
strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
strbuf_setlen(sb, sb->len + r);
}
void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len)
{
strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
}
/* /*
* Returns the length of a line, without trailing spaces. * Returns the length of a line, without trailing spaces.
* *
@ -1124,10 +1053,10 @@ static size_t cleanup(char *line, size_t len)
* *
* If last line does not have a newline at the end, one is added. * If last line does not have a newline at the end, one is added.
* *
* Enable skip_comments to skip every line starting with comment * Pass a non-NUL comment_line_char to skip every line starting
* character. * with it.
*/ */
void strbuf_stripspace(struct strbuf *sb, int skip_comments) void strbuf_stripspace(struct strbuf *sb, char comment_line_char)
{ {
size_t empties = 0; size_t empties = 0;
size_t i, j, len, newlen; size_t i, j, len, newlen;
@ -1140,7 +1069,8 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
eol = memchr(sb->buf + i, '\n', sb->len - i); eol = memchr(sb->buf + i, '\n', sb->len - i);
len = eol ? eol - (sb->buf + i) + 1 : sb->len - i; len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
if (skip_comments && len && sb->buf[i] == comment_line_char) { if (comment_line_char && len &&
sb->buf[i] == comment_line_char) {
newlen = 0; newlen = 0;
continue; continue;
} }
@ -1161,26 +1091,6 @@ void strbuf_stripspace(struct strbuf *sb, int skip_comments)
strbuf_setlen(sb, j); strbuf_setlen(sb, j);
} }
int strbuf_normalize_path(struct strbuf *src)
{
struct strbuf dst = STRBUF_INIT;
strbuf_grow(&dst, src->len);
if (normalize_path_copy(dst.buf, src->buf) < 0) {
strbuf_release(&dst);
return -1;
}
/*
* normalize_path does not tell us the new length, so we have to
* compute it by looking for the new NUL it placed
*/
strbuf_setlen(&dst, strlen(dst.buf));
strbuf_swap(src, &dst);
strbuf_release(&dst);
return 0;
}
void strbuf_strip_file_from_path(struct strbuf *sb) void strbuf_strip_file_from_path(struct strbuf *sb)
{ {
char *path_sep = find_last_dir_sep(sb->buf); char *path_sep = find_last_dir_sep(sb->buf);

View File

@ -1,6 +1,14 @@
#ifndef STRBUF_H #ifndef STRBUF_H
#define STRBUF_H #define STRBUF_H
/*
* NOTE FOR STRBUF DEVELOPERS
*
* strbuf is a low-level primitive; as such it should interact only
* with other low-level primitives. Do not introduce new functions
* which interact with higher-level APIs.
*/
struct string_list; struct string_list;
/** /**
@ -283,7 +291,8 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
* by a comment character and a blank. * by a comment character and a blank.
*/ */
void strbuf_add_commented_lines(struct strbuf *out, void strbuf_add_commented_lines(struct strbuf *out,
const char *buf, size_t size); const char *buf, size_t size,
char comment_line_char);
/** /**
@ -412,8 +421,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
* Add a formatted string prepended by a comment character and a * Add a formatted string prepended by a comment character and a
* blank to the buffer. * blank to the buffer.
*/ */
__attribute__((format (printf, 2, 3))) __attribute__((format (printf, 3, 4)))
void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...); void strbuf_commented_addf(struct strbuf *sb, char comment_line_char, const char *fmt, ...);
__attribute__((format (printf,2,0))) __attribute__((format (printf,2,0)))
void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap); void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);
@ -527,28 +536,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
*/ */
int strbuf_getcwd(struct strbuf *sb); int strbuf_getcwd(struct strbuf *sb);
/**
* Add a path to a buffer, converting a relative path to an
* absolute one in the process. Symbolic links are not
* resolved.
*/
void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
/**
* Canonize `path` (make it absolute, resolve symlinks, remove extra
* slashes) and append it to `sb`. Die with an informative error
* message if there is a problem.
*
* The directory part of `path` (i.e., everything up to the last
* dir_sep) must denote a valid, existing directory, but the last
* component need not exist.
*
* Callers that don't mind links should use the more lightweight
* strbuf_add_absolute_path() instead.
*/
void strbuf_add_real_path(struct strbuf *sb, const char *path);
/** /**
* Normalize in-place the path contained in the strbuf. See * Normalize in-place the path contained in the strbuf. See
* normalize_path_copy() for details. If an error occurs, the contents of "sb" * normalize_path_copy() for details. If an error occurs, the contents of "sb"
@ -557,10 +544,11 @@ void strbuf_add_real_path(struct strbuf *sb, const char *path);
int strbuf_normalize_path(struct strbuf *sb); int strbuf_normalize_path(struct strbuf *sb);
/** /**
* Strip whitespace from a buffer. The second parameter controls if * Strip whitespace from a buffer. If comment_line_char is non-NUL,
* comments are considered contents to be removed or not. * then lines beginning with that character are considered comments,
* thus removed.
*/ */
void strbuf_stripspace(struct strbuf *buf, int skip_comments); void strbuf_stripspace(struct strbuf *buf, char comment_line_char);
static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix)
{ {
@ -630,16 +618,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
*/ */
void strbuf_list_free(struct strbuf **list); void strbuf_list_free(struct strbuf **list);
/**
* Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
* the strbuf `sb`.
*/
struct repository;
void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
const struct object_id *oid, int abbrev_len);
void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len);
/* /*
* Remove the filename from the provided path string. If the path * Remove the filename from the provided path string. If the path
* contains a trailing separator, then the path is considered a directory * contains a trailing separator, then the path is considered a directory
@ -704,9 +682,6 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
typedef int (*char_predicate)(char ch); typedef int (*char_predicate)(char ch);
int is_rfc3986_unreserved(char ch);
int is_rfc3986_reserved_or_unreserved(char ch);
void strbuf_addstr_urlencode(struct strbuf *sb, const char *name, void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,
char_predicate allow_unencoded_fn); char_predicate allow_unencoded_fn);

View File

@ -43,6 +43,7 @@
*/ */
#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h"
#include "path.h" #include "path.h"
#include "tempfile.h" #include "tempfile.h"
#include "sigchain.h" #include "sigchain.h"

View File

@ -1024,7 +1024,7 @@ static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncom
if (s->display_comment_prefix) { if (s->display_comment_prefix) {
size_t len; size_t len;
summary_content = strbuf_detach(&summary, &len); summary_content = strbuf_detach(&summary, &len);
strbuf_add_commented_lines(&summary, summary_content, len); strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
free(summary_content); free(summary_content);
} }
@ -1099,8 +1099,8 @@ void wt_status_append_cut_line(struct strbuf *buf)
{ {
const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored."); const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
strbuf_commented_addf(buf, "%s", cut_line); strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
strbuf_add_commented_lines(buf, explanation, strlen(explanation)); strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
} }
void wt_status_add_cut_line(FILE *fp) void wt_status_add_cut_line(FILE *fp)