Skip to content

Commit 325506c

Browse files
committed
Merge branch 'zh/ref-filter-raw-data' into seen
* zh/ref-filter-raw-data: ref-filter: add contents:raw atom ref-filter: support %(contents) for blob, tree quote: add *.quote_buf_with_size functions
2 parents 63f6038 + 6861b92 commit 325506c

File tree

5 files changed

+511
-71
lines changed

5 files changed

+511
-71
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,12 @@ and `date` to extract the named component. For email fields (`authoremail`,
235235
without angle brackets, and `:localpart` to get the part before the `@` symbol
236236
out of the trimmed email.
237237

238-
The message in a commit or a tag object is `contents`, from which
239-
`contents:<part>` can be used to extract various parts out of:
238+
The data in a object is `contents`, from which `contents:<part>` can be used
239+
to extract various parts out of:
240240

241241
contents:size::
242-
The size in bytes of the commit or tag message.
242+
The size in bytes of the commit or tag message, and the raw object size
243+
of the blob or tree.
243244

244245
contents:subject::
245246
The first paragraph of the message, which typically is a
@@ -257,7 +258,17 @@ contents:signature::
257258
The optional GPG signature of the tag.
258259

259260
contents:lines=N::
260-
The first `N` lines of the message.
261+
The first `N` lines of the message of the commit or tag message.
262+
263+
contents:raw::
264+
The raw data of the object. For blob and tree, it priont all object data,
265+
its output is equivalent to `%(contents)`; For commit and tag, it not only
266+
print objects’ messages (as `%(contents)` does), but also print objects
267+
headers, like the "tree XXX", "parent YYY", etc lines in commits, and the
268+
"object OOO", "type TTT", etc lines in tags.
269+
270+
Note: blob and tree objects only support `%(contents)`, `%(contents:raw)` and
271+
`%(contents:size)`.
261272

262273
Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
263274
are obtained as `trailers[:options]` (or by using the historical alias

quote.c

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

46+
void sq_quote_buf_with_size(struct strbuf *dst, const char *src, size_t size)
47+
{
48+
char *to_free = NULL;
49+
size_t cur_size = 0;
50+
51+
if (dst->buf == src)
52+
to_free = strbuf_detach(dst, NULL);
53+
54+
strbuf_addch(dst, '\'');
55+
while (cur_size < size) {
56+
size_t len = strcspn(src, "'!");
57+
if (!len) {
58+
strbuf_add(dst, src, 1);
59+
src++;
60+
cur_size++;
61+
} else {
62+
strbuf_add(dst, src, len);
63+
src += len;
64+
cur_size += len;
65+
}
66+
if (cur_size >= size)
67+
break;
68+
while (need_bs_quote(*src)) {
69+
strbuf_addstr(dst, "'\\");
70+
strbuf_addch(dst, *src++);
71+
cur_size++;
72+
strbuf_addch(dst, '\'');
73+
}
74+
}
75+
strbuf_addch(dst, '\'');
76+
free(to_free);
77+
}
78+
4679
void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
4780
{
4881
static const char ok_punct[] = "+,-./:=@_^";
@@ -471,6 +504,25 @@ void perl_quote_buf(struct strbuf *sb, const char *src)
471504
strbuf_addch(sb, sq);
472505
}
473506

507+
void perl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size)
508+
{
509+
const char sq = '\'';
510+
const char bq = '\\';
511+
char c;
512+
size_t cur_size = 0;
513+
514+
strbuf_addch(sb, sq);
515+
while (cur_size < size) {
516+
c = *src++;
517+
cur_size++;
518+
519+
if (c == sq || c == bq)
520+
strbuf_addch(sb, bq);
521+
strbuf_addch(sb, c);
522+
}
523+
strbuf_addch(sb, sq);
524+
}
525+
474526
void python_quote_buf(struct strbuf *sb, const char *src)
475527
{
476528
const char sq = '\'';
@@ -492,6 +544,31 @@ void python_quote_buf(struct strbuf *sb, const char *src)
492544
strbuf_addch(sb, sq);
493545
}
494546

547+
void python_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size)
548+
{
549+
const char sq = '\'';
550+
const char bq = '\\';
551+
const char nl = '\n';
552+
char c;
553+
size_t cur_size = 0;
554+
555+
strbuf_addch(sb, sq);
556+
while (cur_size < size) {
557+
c = *src++;
558+
cur_size++;
559+
560+
if (c == nl) {
561+
strbuf_addch(sb, bq);
562+
strbuf_addch(sb, 'n');
563+
continue;
564+
}
565+
if (c == sq || c == bq)
566+
strbuf_addch(sb, bq);
567+
strbuf_addch(sb, c);
568+
}
569+
strbuf_addch(sb, sq);
570+
}
571+
495572
void tcl_quote_buf(struct strbuf *sb, const char *src)
496573
{
497574
char c;
@@ -527,6 +604,45 @@ void tcl_quote_buf(struct strbuf *sb, const char *src)
527604
strbuf_addch(sb, '"');
528605
}
529606

607+
void tcl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size)
608+
{
609+
char c;
610+
size_t cur_size = 0;
611+
612+
strbuf_addch(sb, '"');
613+
while (cur_size < size) {
614+
c = *src++;
615+
cur_size++;
616+
617+
switch (c) {
618+
case '[': case ']':
619+
case '{': case '}':
620+
case '$': case '\\': case '"':
621+
strbuf_addch(sb, '\\');
622+
/* fallthrough */
623+
default:
624+
strbuf_addch(sb, c);
625+
break;
626+
case '\f':
627+
strbuf_addstr(sb, "\\f");
628+
break;
629+
case '\r':
630+
strbuf_addstr(sb, "\\r");
631+
break;
632+
case '\n':
633+
strbuf_addstr(sb, "\\n");
634+
break;
635+
case '\t':
636+
strbuf_addstr(sb, "\\t");
637+
break;
638+
case '\v':
639+
strbuf_addstr(sb, "\\v");
640+
break;
641+
}
642+
}
643+
strbuf_addch(sb, '"');
644+
}
645+
530646
void basic_regex_quote_buf(struct strbuf *sb, const char *src)
531647
{
532648
char c;

quote.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct strbuf;
3030
*/
3131

3232
void sq_quote_buf(struct strbuf *, const char *src);
33+
void sq_quote_buf_with_size(struct strbuf *, const char *src, size_t size);
3334
void sq_quote_argv(struct strbuf *, const char **argv);
3435
void sq_quotef(struct strbuf *, const char *fmt, ...);
3536

@@ -94,8 +95,11 @@ char *quote_path(const char *in, const char *prefix, struct strbuf *out, unsigne
9495

9596
/* quoting as a string literal for other languages */
9697
void perl_quote_buf(struct strbuf *sb, const char *src);
98+
void perl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size);
9799
void python_quote_buf(struct strbuf *sb, const char *src);
100+
void python_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size);
98101
void tcl_quote_buf(struct strbuf *sb, const char *src);
102+
void tcl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size);
99103
void basic_regex_quote_buf(struct strbuf *sb, const char *src);
100104

101105
#endif

0 commit comments

Comments
 (0)