Skip to content

Commit

Permalink
Full rework of quote_c_style and write_name_quoted.
Browse files Browse the repository at this point in the history
* quote_c_style works on a strbuf instead of a wild buffer.
* quote_c_style is now clever enough to not add double quotes if not needed.

* write_name_quoted inherits those advantages, but also take a different
  set of arguments. Now instead of asking for quotes or not, you pass a
  "terminator". If it's \0 then we assume you don't want to escape, else C
  escaping is performed. In any case, the terminator is also appended to the
  stream. It also no longer takes the prefix/prefix_len arguments, as it's
  seldomly used, and makes some optimizations harder.

* write_name_quotedpfx is created to work like write_name_quoted and take
  the prefix/prefix_len arguments.

Thanks to those API changes, diff.c has somehow lost weight, thanks to the
removal of functions that were wrappers around the old write_name_quoted
trying to give it a semantics like the new one, but performing a lot of
allocations for this goal. Now we always write directly to the stream, no
intermediate allocation is performed.

As a side effect of the refactor in builtin-apply.c, the length of the bar
graphs in diffstats are not affected anymore by the fact that the path was
clipped.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
  • Loading branch information
MadCoder authored and gitster committed Sep 21, 2007
1 parent 7fb1011 commit 663af34
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 368 deletions.
77 changes: 30 additions & 47 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,14 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
fputs(pre, output);
if (patch->old_name && patch->new_name &&
strcmp(patch->old_name, patch->new_name)) {
write_name_quoted(NULL, 0, patch->old_name, 1, output);
quote_c_style(patch->old_name, NULL, output, 0);
fputs(" => ", output);
write_name_quoted(NULL, 0, patch->new_name, 1, output);
}
else {
quote_c_style(patch->new_name, NULL, output, 0);
} else {
const char *n = patch->new_name;
if (!n)
n = patch->old_name;
write_name_quoted(NULL, 0, n, 1, output);
quote_c_style(n, NULL, output, 0);
}
fputs(post, output);
}
Expand Down Expand Up @@ -1379,61 +1378,50 @@ static const char minuses[]= "--------------------------------------------------

static void show_stats(struct patch *patch)
{
const char *prefix = "";
char *name = patch->new_name;
char *qname = NULL;
int len, max, add, del, total;

if (!name)
name = patch->old_name;
struct strbuf qname;
char *cp = patch->new_name ? patch->new_name : patch->old_name;
int max, add, del;

if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
qname = xmalloc(len + 1);
quote_c_style(name, qname, NULL, 0);
name = qname;
}
strbuf_init(&qname, 0);
quote_c_style(cp, &qname, NULL, 0);

/*
* "scale" the filename
*/
len = strlen(name);
max = max_len;
if (max > 50)
max = 50;
if (len > max) {
char *slash;
prefix = "...";
max -= 3;
name += len - max;
slash = strchr(name, '/');
if (slash)
name = slash;

if (qname.len > max) {
cp = strchr(qname.buf + qname.len + 3 - max, '/');
if (!cp)
cp = qname.buf + qname.len + 3 - max;
strbuf_splice(&qname, 0, cp - qname.buf, "...", 3);
}

if (patch->is_binary) {
printf(" %-*s | Bin\n", max, qname.buf);
strbuf_release(&qname);
return;
}
len = max;

printf(" %-*s |", max, qname.buf);
strbuf_release(&qname);

/*
* scale the add/delete
*/
max = max_change;
if (max + len > 70)
max = 70 - len;

max = max + max_change > 70 ? 70 - max : max_change;
add = patch->lines_added;
del = patch->lines_deleted;
total = add + del;

if (max_change > 0) {
total = (total * max + max_change / 2) / max_change;
int total = ((add + del) * max + max_change / 2) / max_change;
add = (add * max + max_change / 2) / max_change;
del = total - add;
}
if (patch->is_binary)
printf(" %s%-*s | Bin\n", prefix, len, name);
else
printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
len, name, patch->lines_added + patch->lines_deleted,
add, pluses, del, minuses);
free(qname);
printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
add, pluses, del, minuses);
}

static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
Expand Down Expand Up @@ -2228,13 +2216,8 @@ static void numstat_patch_list(struct patch *patch)
if (patch->is_binary)
printf("-\t-\t");
else
printf("%d\t%d\t",
patch->lines_added, patch->lines_deleted);
if (line_termination && quote_c_style(name, NULL, NULL, 0))
quote_c_style(name, NULL, stdout, 0);
else
fputs(name, stdout);
putchar(line_termination);
printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
write_name_quoted(name, stdout, line_termination);
}
}

Expand Down
3 changes: 1 addition & 2 deletions builtin-blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,7 @@ static void get_commit_info(struct commit *commit,
static void write_filename_info(const char *path)
{
printf("filename ");
write_name_quoted(NULL, 0, path, 1, stdout);
putchar('\n');
write_name_quoted(path, stdout, '\n');
}

/*
Expand Down
2 changes: 1 addition & 1 deletion builtin-check-attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
else if (ATTR_UNSET(value))
value = "unspecified";

write_name_quoted("", 0, argv[i], 1, stdout);
quote_c_style(argv[i], NULL, stdout, 0);
printf(": %s: %s\n", argv[j+1], value);
}
}
Expand Down
4 changes: 1 addition & 3 deletions builtin-checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ static void write_tempfile_record(const char *name, int prefix_length)
fputs(topath[checkout_stage], stdout);

putchar('\t');
write_name_quoted("", 0, name + prefix_length,
line_termination, stdout);
putchar(line_termination);
write_name_quoted(name + prefix_length, stdout, line_termination);

for (i = 0; i < 4; i++) {
topath[i][0] = 0;
Expand Down
13 changes: 3 additions & 10 deletions builtin-ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
return;

fputs(tag, stdout);
write_name_quoted("", 0, ent->name + offset, line_terminator, stdout);
putchar(line_terminator);
write_name_quoted(ent->name + offset, stdout, line_terminator);
}

static void show_other_files(struct dir_struct *dir)
Expand Down Expand Up @@ -208,21 +207,15 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)

if (!show_stage) {
fputs(tag, stdout);
write_name_quoted("", 0, ce->name + offset,
line_terminator, stdout);
putchar(line_terminator);
}
else {
} else {
printf("%s%06o %s %d\t",
tag,
ntohl(ce->ce_mode),
abbrev ? find_unique_abbrev(ce->sha1,abbrev)
: sha1_to_hex(ce->sha1),
ce_stage(ce));
write_name_quoted("", 0, ce->name + offset,
line_terminator, stdout);
putchar(line_terminator);
}
write_name_quoted(ce->name + offset, stdout, line_terminator);
}

static void show_files(struct dir_struct *dir, const char *prefix)
Expand Down
6 changes: 2 additions & 4 deletions builtin-ls-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ static int show_tree(const unsigned char *sha1, const char *base, int baselen,
abbrev ? find_unique_abbrev(sha1, abbrev)
: sha1_to_hex(sha1));
}
write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
pathname,
line_termination, stdout);
putchar(line_termination);
write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
pathname, stdout, line_termination);
return retval;
}

Expand Down
16 changes: 2 additions & 14 deletions combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,7 @@ static void dump_quoted_path(const char *prefix, const char *path,
const char *c_meta, const char *c_reset)
{
printf("%s%s", c_meta, prefix);
if (quote_c_style(path, NULL, NULL, 0))
quote_c_style(path, NULL, stdout, 0);
else
printf("%s", path);
quote_c_style(path, NULL, stdout, 0);
printf("%s\n", c_reset);
}

Expand Down Expand Up @@ -900,16 +897,7 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
putchar(inter_name_termination);
}

if (line_termination) {
if (quote_c_style(p->path, NULL, NULL, 0))
quote_c_style(p->path, NULL, stdout, 0);
else
printf("%s", p->path);
putchar(line_termination);
}
else {
printf("%s%c", p->path, line_termination);
}
write_name_quoted(p->path, stdout, line_termination);
}

void show_combined_diff(struct combine_diff_path *p,
Expand Down
Loading

0 comments on commit 663af34

Please sign in to comment.