Skip to content

Commit

Permalink
Merge branch 'jc/strbuf-getline'
Browse files Browse the repository at this point in the history
The preliminary clean-up for jc/peace-with-crlf topic.

* jc/strbuf-getline:
  strbuf: give strbuf_getline() to the "most text friendly" variant
  checkout-index: there are only two possible line terminations
  update-index: there are only two possible line terminations
  check-ignore: there are only two possible line terminations
  check-attr: there are only two possible line terminations
  mktree: there are only two possible line terminations
  strbuf: introduce strbuf_getline_{lf,nul}()
  strbuf: make strbuf_getline_crlf() global
  strbuf: miniscule style fix
  • Loading branch information
Junio C Hamano committed Jan 29, 2016
2 parents 116a866 + 1a0c8df commit b62624b
Show file tree
Hide file tree
Showing 41 changed files with 150 additions and 116 deletions.
8 changes: 4 additions & 4 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ static void read_bisect_paths(struct argv_array *array)
if (!fp)
die_errno("Could not open file '%s'", filename);

while (strbuf_getline(&str, fp, '\n') != EOF) {
while (strbuf_getline_lf(&str, fp) != EOF) {
strbuf_trim(&str);
if (sq_dequote_to_argv_array(str.buf, array))
die("Badly quoted content in file '%s': %s",
Expand Down Expand Up @@ -668,7 +668,7 @@ static int is_expected_rev(const struct object_id *oid)
if (!fp)
return 0;

if (strbuf_getline(&str, fp, '\n') != EOF)
if (strbuf_getline_lf(&str, fp) != EOF)
res = !strcmp(str.buf, oid_to_hex(oid));

strbuf_release(&str);
Expand Down Expand Up @@ -914,9 +914,9 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
strerror(errno));
}
} else {
strbuf_getline(&str, fp, '\n');
strbuf_getline_lf(&str, fp);
*read_bad = strbuf_detach(&str, NULL);
strbuf_getline(&str, fp, '\n');
strbuf_getline_lf(&str, fp);
*read_good = strbuf_detach(&str, NULL);
}
strbuf_release(&str);
Expand Down
37 changes: 11 additions & 26 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@ static int is_empty_file(const char *filename)
return !st.st_size;
}

/**
* Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
*/
static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
{
if (strbuf_getwholeline(sb, fp, '\n'))
return EOF;
if (sb->buf[sb->len - 1] == '\n') {
strbuf_setlen(sb, sb->len - 1);
if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
strbuf_setlen(sb, sb->len - 1);
}
return 0;
}

/**
* Returns the length of the first line of msg.
*/
Expand Down Expand Up @@ -284,7 +269,7 @@ static char *read_shell_var(FILE *fp, const char *key)
struct strbuf sb = STRBUF_INIT;
const char *str;

if (strbuf_getline(&sb, fp, '\n'))
if (strbuf_getline_lf(&sb, fp))
goto fail;

if (!skip_prefix(sb.buf, key, &str))
Expand Down Expand Up @@ -573,7 +558,7 @@ static int copy_notes_for_rebase(const struct am_state *state)

fp = xfopen(am_path(state, "rewritten"), "r");

while (!strbuf_getline(&sb, fp, '\n')) {
while (!strbuf_getline_lf(&sb, fp)) {
unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];

if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
Expand Down Expand Up @@ -628,7 +613,7 @@ static int is_mail(FILE *fp)
if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
die("invalid pattern: %s", header_regex);

while (!strbuf_getline_crlf(&sb, fp)) {
while (!strbuf_getline(&sb, fp)) {
if (!sb.len)
break; /* End of header */

Expand Down Expand Up @@ -675,7 +660,7 @@ static int detect_patch_format(const char **paths)

fp = xfopen(*paths, "r");

while (!strbuf_getline_crlf(&l1, fp)) {
while (!strbuf_getline(&l1, fp)) {
if (l1.len)
break;
}
Expand All @@ -696,9 +681,9 @@ static int detect_patch_format(const char **paths)
}

strbuf_reset(&l2);
strbuf_getline_crlf(&l2, fp);
strbuf_getline(&l2, fp);
strbuf_reset(&l3);
strbuf_getline_crlf(&l3, fp);
strbuf_getline(&l3, fp);

/*
* If the second line is empty and the third is a From, Author or Date
Expand Down Expand Up @@ -817,7 +802,7 @@ static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
struct strbuf sb = STRBUF_INIT;
int subject_printed = 0;

while (!strbuf_getline(&sb, in, '\n')) {
while (!strbuf_getline_lf(&sb, in)) {
const char *str;

if (str_isspace(sb.buf))
Expand Down Expand Up @@ -875,7 +860,7 @@ static int split_mail_stgit_series(struct am_state *state, const char **paths,
return error(_("could not open '%s' for reading: %s"), *paths,
strerror(errno));

while (!strbuf_getline(&sb, fp, '\n')) {
while (!strbuf_getline_lf(&sb, fp)) {
if (*sb.buf == '#')
continue; /* skip comment lines */

Expand All @@ -900,7 +885,7 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
{
struct strbuf sb = STRBUF_INIT;

while (!strbuf_getline(&sb, in, '\n')) {
while (!strbuf_getline_lf(&sb, in)) {
const char *str;

if (skip_prefix(sb.buf, "# User ", &str))
Expand Down Expand Up @@ -1317,7 +1302,7 @@ static int parse_mail(struct am_state *state, const char *mail)

/* Extract message and author information */
fp = xfopen(am_path(state, "info"), "r");
while (!strbuf_getline(&sb, fp, '\n')) {
while (!strbuf_getline_lf(&sb, fp)) {
const char *x;

if (skip_prefix(sb.buf, "Subject: ", &x)) {
Expand Down Expand Up @@ -1383,7 +1368,7 @@ static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
FILE *fp = xfopen(mail, "r");
const char *x;

if (strbuf_getline(&sb, fp, '\n'))
if (strbuf_getline_lf(&sb, fp))
return -1;

if (!skip_prefix(sb.buf, "From ", &x))
Expand Down
2 changes: 1 addition & 1 deletion builtin/cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static int batch_objects(struct batch_options *opt)
save_warning = warn_on_object_refname_ambiguity;
warn_on_object_refname_ambiguity = 0;

while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
if (data.split_on_whitespace) {
/*
* Split at first whitespace, tying off the beginning
Expand Down
7 changes: 4 additions & 3 deletions builtin/check-attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ static void check_attr_stdin_paths(const char *prefix, int cnt,
struct git_attr_check *check)
{
struct strbuf buf, nbuf;
int line_termination = nul_term_line ? 0 : '\n';
strbuf_getline_fn getline_fn;

getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
strbuf_init(&buf, 0);
strbuf_init(&nbuf, 0);
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
if (line_termination && buf.buf[0] == '"') {
while (getline_fn(&buf, stdin) != EOF) {
if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
die("line is badly quoted");
Expand Down
7 changes: 4 additions & 3 deletions builtin/check-ignore.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
{
struct strbuf buf, nbuf;
char *pathspec[2] = { NULL, NULL };
int line_termination = nul_term_line ? 0 : '\n';
strbuf_getline_fn getline_fn;
int num_ignored = 0;

getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
strbuf_init(&buf, 0);
strbuf_init(&nbuf, 0);
while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
if (line_termination && buf.buf[0] == '"') {
while (getline_fn(&buf, stdin) != EOF) {
if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
die("line is badly quoted");
Expand Down
2 changes: 1 addition & 1 deletion builtin/check-mailmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int cmd_check_mailmap(int argc, const char **argv, const char *prefix)

if (use_stdin) {
struct strbuf buf = STRBUF_INIT;
while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
check_mailmap(&mailmap, buf.buf);
maybe_flush_or_die(stdout, "stdout");
}
Expand Down
16 changes: 8 additions & 8 deletions builtin/checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "parse-options.h"

#define CHECKOUT_ALL 4
static int line_termination = '\n';
static int nul_term_line;
static int checkout_stage; /* default to checkout stage0 */
static int to_tempfile;
static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
Expand All @@ -35,7 +35,8 @@ static void write_tempfile_record(const char *name, const char *prefix)
fputs(topath[checkout_stage], stdout);

putchar('\t');
write_name_quoted_relative(name, prefix, stdout, line_termination);
write_name_quoted_relative(name, prefix, stdout,
nul_term_line ? '\0' : '\n');

for (i = 0; i < 4; i++) {
topath[i][0] = 0;
Expand Down Expand Up @@ -144,10 +145,7 @@ static int option_parse_u(const struct option *opt,
static int option_parse_z(const struct option *opt,
const char *arg, int unset)
{
if (unset)
line_termination = '\n';
else
line_termination = 0;
nul_term_line = !unset;
return 0;
}

Expand Down Expand Up @@ -254,13 +252,15 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)

if (read_from_stdin) {
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
strbuf_getline_fn getline_fn;

if (all)
die("git checkout-index: don't mix '--all' and '--stdin'");

while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
while (getline_fn(&buf, stdin) != EOF) {
char *p;
if (line_termination && buf.buf[0] == '"') {
if (!nul_term_line && buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
die("line is badly quoted");
Expand Down
6 changes: 3 additions & 3 deletions builtin/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
clean_get_color(CLEAN_COLOR_RESET));
}

if (strbuf_getline(&choice, stdin, '\n') != EOF) {
if (strbuf_getline_lf(&choice, stdin) != EOF) {
strbuf_trim(&choice);
} else {
eof = 1;
Expand Down Expand Up @@ -676,7 +676,7 @@ static int filter_by_patterns_cmd(void)
clean_print_color(CLEAN_COLOR_PROMPT);
printf(_("Input ignore patterns>> "));
clean_print_color(CLEAN_COLOR_RESET);
if (strbuf_getline(&confirm, stdin, '\n') != EOF)
if (strbuf_getline_lf(&confirm, stdin) != EOF)
strbuf_trim(&confirm);
else
putchar('\n');
Expand Down Expand Up @@ -774,7 +774,7 @@ static int ask_each_cmd(void)
qname = quote_path_relative(item->string, NULL, &buf);
/* TRANSLATORS: Make sure to keep [y/N] as is */
printf(_("Remove %s [y/N]? "), qname);
if (strbuf_getline(&confirm, stdin, '\n') != EOF) {
if (strbuf_getline_lf(&confirm, stdin) != EOF) {
strbuf_trim(&confirm);
} else {
putchar('\n');
Expand Down
2 changes: 1 addition & 1 deletion builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ static void copy_alternates(struct strbuf *src, struct strbuf *dst,
FILE *in = fopen(src->buf, "r");
struct strbuf line = STRBUF_INIT;

while (strbuf_getline(&line, in, '\n') != EOF) {
while (strbuf_getline_lf(&line, in) != EOF) {
char *abs_path;
if (!line.len || line.buf[0] == '#')
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int cmd_column(int argc, const char **argv, const char *prefix)
die(_("--command must be the first argument"));
}
finalize_colopts(&colopts, -1);
while (!strbuf_getline(&sb, stdin, '\n'))
while (!strbuf_getline_lf(&sb, stdin))
string_list_append(&list, sb.buf);

print_columns(&list, colopts, &copts);
Expand Down
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
if (fp == NULL)
die_errno(_("could not open '%s' for reading"),
git_path_merge_head());
while (strbuf_getline(&m, fp, '\n') != EOF) {
while (strbuf_getline_lf(&m, fp) != EOF) {
struct commit *parent;

parent = get_merge_parent(m.buf);
Expand Down
2 changes: 1 addition & 1 deletion builtin/fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
else {
/* read from stdin one ref per line, until EOF */
struct strbuf line = STRBUF_INIT;
while (strbuf_getline(&line, stdin, '\n') != EOF)
while (strbuf_getline_lf(&line, stdin) != EOF)
add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
strbuf_release(&line);
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
patterns = from_stdin ? stdin : fopen(arg, "r");
if (!patterns)
die_errno(_("cannot open '%s'"), arg);
while (strbuf_getline(&sb, patterns, '\n') == 0) {
while (strbuf_getline_lf(&sb, patterns) == 0) {
/* ignore empty line like grep does */
if (sb.len == 0)
continue;
Expand Down
2 changes: 1 addition & 1 deletion builtin/hash-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
{
struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;

while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
if (buf.buf[0] == '"') {
strbuf_reset(&nbuf);
if (unquote_c_style(&nbuf, buf.buf, NULL))
Expand Down
14 changes: 8 additions & 6 deletions builtin/mktree.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static const char *mktree_usage[] = {
NULL
};

static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_missing)
{
char *ptr, *ntr;
unsigned mode;
Expand Down Expand Up @@ -97,7 +97,7 @@ static void mktree_line(char *buf, size_t len, int line_termination, int allow_m
*ntr++ = 0; /* now at the beginning of SHA1 */

path = ntr + 41; /* at the beginning of name */
if (line_termination && path[0] == '"') {
if (!nul_term_line && path[0] == '"') {
struct strbuf p_uq = STRBUF_INIT;
if (unquote_c_style(&p_uq, path, NULL))
die("invalid quoting");
Expand Down Expand Up @@ -141,23 +141,25 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
{
struct strbuf sb = STRBUF_INIT;
unsigned char sha1[20];
int line_termination = '\n';
int nul_term_line = 0;
int allow_missing = 0;
int is_batch_mode = 0;
int got_eof = 0;
strbuf_getline_fn getline_fn;

const struct option option[] = {
OPT_SET_INT('z', NULL, &line_termination, N_("input is NUL terminated"), '\0'),
OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
OPT_END()
};

ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;

while (!got_eof) {
while (1) {
if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
if (getline_fn(&sb, stdin) == EOF) {
got_eof = 1;
break;
}
Expand All @@ -167,7 +169,7 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
break;
die("input format error: (blank line only valid in batch mode)");
}
mktree_line(sb.buf, sb.len, line_termination, allow_missing);
mktree_line(sb.buf, sb.len, nul_term_line, allow_missing);
}
if (is_batch_mode && got_eof && used < 1) {
/*
Expand Down
2 changes: 1 addition & 1 deletion builtin/notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
t = &default_notes_tree;
}

while (strbuf_getline(&buf, stdin, '\n') != EOF) {
while (strbuf_getline_lf(&buf, stdin) != EOF) {
unsigned char from_obj[20], to_obj[20];
struct strbuf **split;
int err;
Expand Down
Loading

0 comments on commit b62624b

Please sign in to comment.