Skip to content

Commit 4aa04a8

Browse files
committed
Merge branch 'nd/sq-quote-buf'
Code simplification as a preparatory step to something larger. * nd/sq-quote-buf: quote: remove sq_quote_print() tar-tree: remove dependency on sq_quote_print() for-each-ref, quote: convert *_quote_print -> *_quote_buf
2 parents d9fc248 + 82aae5c commit 4aa04a8

File tree

4 files changed

+39
-54
lines changed

4 files changed

+39
-54
lines changed

builtin/for-each-ref.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -867,24 +867,29 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
867867
static void print_value(struct refinfo *ref, int atom, int quote_style)
868868
{
869869
struct atom_value *v;
870+
struct strbuf sb = STRBUF_INIT;
870871
get_value(ref, atom, &v);
871872
switch (quote_style) {
872873
case QUOTE_NONE:
873874
fputs(v->s, stdout);
874875
break;
875876
case QUOTE_SHELL:
876-
sq_quote_print(stdout, v->s);
877+
sq_quote_buf(&sb, v->s);
877878
break;
878879
case QUOTE_PERL:
879-
perl_quote_print(stdout, v->s);
880+
perl_quote_buf(&sb, v->s);
880881
break;
881882
case QUOTE_PYTHON:
882-
python_quote_print(stdout, v->s);
883+
python_quote_buf(&sb, v->s);
883884
break;
884885
case QUOTE_TCL:
885-
tcl_quote_print(stdout, v->s);
886+
tcl_quote_buf(&sb, v->s);
886887
break;
887888
}
889+
if (quote_style != QUOTE_NONE) {
890+
fputs(sb.buf, stdout);
891+
strbuf_release(&sb);
892+
}
888893
}
889894

890895
static int hex1(char ch)

builtin/tar-tree.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
2626
* $0 tree-ish basedir ==>
2727
* git archive --format-tar --prefix=basedir tree-ish
2828
*/
29-
int i;
3029
const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
30+
struct strbuf sb = STRBUF_INIT;
3131
char *basedir_arg;
3232
int nargc = 0;
3333

@@ -65,11 +65,10 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)
6565
fprintf(stderr,
6666
"*** \"git tar-tree\" is now deprecated.\n"
6767
"*** Running \"git archive\" instead.\n***");
68-
for (i = 0; i < nargc; i++) {
69-
fputc(' ', stderr);
70-
sq_quote_print(stderr, nargv[i]);
71-
}
72-
fputc('\n', stderr);
68+
sq_quote_argv(&sb, nargv, 0);
69+
strbuf_addch(&sb, '\n');
70+
fputs(sb.buf, stderr);
71+
strbuf_release(&sb);
7372
return cmd_archive(nargc, nargv, prefix);
7473
}
7574

quote.c

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
4242
free(to_free);
4343
}
4444

45-
void sq_quote_print(FILE *stream, const char *src)
46-
{
47-
char c;
48-
49-
fputc('\'', stream);
50-
while ((c = *src++)) {
51-
if (need_bs_quote(c)) {
52-
fputs("'\\", stream);
53-
fputc(c, stream);
54-
fputc('\'', stream);
55-
} else {
56-
fputc(c, stream);
57-
}
58-
}
59-
fputc('\'', stream);
60-
}
61-
6245
void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
6346
{
6447
int i;
@@ -408,72 +391,72 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
408391

409392
/* quoting as a string literal for other languages */
410393

411-
void perl_quote_print(FILE *stream, const char *src)
394+
void perl_quote_buf(struct strbuf *sb, const char *src)
412395
{
413396
const char sq = '\'';
414397
const char bq = '\\';
415398
char c;
416399

417-
fputc(sq, stream);
400+
strbuf_addch(sb, sq);
418401
while ((c = *src++)) {
419402
if (c == sq || c == bq)
420-
fputc(bq, stream);
421-
fputc(c, stream);
403+
strbuf_addch(sb, bq);
404+
strbuf_addch(sb, c);
422405
}
423-
fputc(sq, stream);
406+
strbuf_addch(sb, sq);
424407
}
425408

426-
void python_quote_print(FILE *stream, const char *src)
409+
void python_quote_buf(struct strbuf *sb, const char *src)
427410
{
428411
const char sq = '\'';
429412
const char bq = '\\';
430413
const char nl = '\n';
431414
char c;
432415

433-
fputc(sq, stream);
416+
strbuf_addch(sb, sq);
434417
while ((c = *src++)) {
435418
if (c == nl) {
436-
fputc(bq, stream);
437-
fputc('n', stream);
419+
strbuf_addch(sb, bq);
420+
strbuf_addch(sb, 'n');
438421
continue;
439422
}
440423
if (c == sq || c == bq)
441-
fputc(bq, stream);
442-
fputc(c, stream);
424+
strbuf_addch(sb, bq);
425+
strbuf_addch(sb, c);
443426
}
444-
fputc(sq, stream);
427+
strbuf_addch(sb, sq);
445428
}
446429

447-
void tcl_quote_print(FILE *stream, const char *src)
430+
void tcl_quote_buf(struct strbuf *sb, const char *src)
448431
{
449432
char c;
450433

451-
fputc('"', stream);
434+
strbuf_addch(sb, '"');
452435
while ((c = *src++)) {
453436
switch (c) {
454437
case '[': case ']':
455438
case '{': case '}':
456439
case '$': case '\\': case '"':
457-
fputc('\\', stream);
440+
strbuf_addch(sb, '\\');
458441
default:
459-
fputc(c, stream);
442+
strbuf_addch(sb, c);
460443
break;
461444
case '\f':
462-
fputs("\\f", stream);
445+
strbuf_addstr(sb, "\\f");
463446
break;
464447
case '\r':
465-
fputs("\\r", stream);
448+
strbuf_addstr(sb, "\\r");
466449
break;
467450
case '\n':
468-
fputs("\\n", stream);
451+
strbuf_addstr(sb, "\\n");
469452
break;
470453
case '\t':
471-
fputs("\\t", stream);
454+
strbuf_addstr(sb, "\\t");
472455
break;
473456
case '\v':
474-
fputs("\\v", stream);
457+
strbuf_addstr(sb, "\\v");
475458
break;
476459
}
477460
}
478-
fputc('"', stream);
461+
strbuf_addch(sb, '"');
479462
}

quote.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ struct strbuf;
2727
* excluding the final null regardless of the buffer size.
2828
*/
2929

30-
extern void sq_quote_print(FILE *stream, const char *src);
31-
3230
extern void sq_quote_buf(struct strbuf *, const char *src);
3331
extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
3432

@@ -68,8 +66,8 @@ extern char *quote_path_relative(const char *in, const char *prefix,
6866
struct strbuf *out);
6967

7068
/* quoting as a string literal for other languages */
71-
extern void perl_quote_print(FILE *stream, const char *src);
72-
extern void python_quote_print(FILE *stream, const char *src);
73-
extern void tcl_quote_print(FILE *stream, const char *src);
69+
extern void perl_quote_buf(struct strbuf *sb, const char *src);
70+
extern void python_quote_buf(struct strbuf *sb, const char *src);
71+
extern void tcl_quote_buf(struct strbuf *sb, const char *src);
7472

7573
#endif

0 commit comments

Comments
 (0)