Skip to content

Commit

Permalink
Merge branch 'zh/ref-filter-raw-data' into seen
Browse files Browse the repository at this point in the history
* zh/ref-filter-raw-data:
  ref-filter: add contents:raw atom
  SQUASH???
  ref-filter: support %(contents) for blob, tree
  quote: add *.quote_buf_with_size functions
  • Loading branch information
gitster committed May 25, 2021
2 parents 3fd7ba0 + 09bc4f0 commit 96f42f6
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 71 deletions.
19 changes: 15 additions & 4 deletions Documentation/git-for-each-ref.txt
Expand Up @@ -235,11 +235,12 @@ and `date` to extract the named component. For email fields (`authoremail`,
without angle brackets, and `:localpart` to get the part before the `@` symbol
out of the trimmed email.

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

contents:size::
The size in bytes of the commit or tag message.
The size in bytes of the commit or tag message, and the raw object size
of the blob or tree.

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

contents:lines=N::
The first `N` lines of the message.
The first `N` lines of the message of the commit or tag message.

contents:raw::
The raw data of the object. For blob and tree, it priont all object data,
its output is equivalent to `%(contents)`; For commit and tag, it not only
print objects’ messages (as `%(contents)` does), but also print objects
headers, like the "tree XXX", "parent YYY", etc lines in commits, and the
"object OOO", "type TTT", etc lines in tags.

Note: blob and tree objects only support `%(contents)`, `%(contents:raw)` and
`%(contents:size)`.

Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
are obtained as `trailers[:options]` (or by using the historical alias
Expand Down
116 changes: 116 additions & 0 deletions quote.c
Expand Up @@ -43,6 +43,39 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
free(to_free);
}

void sq_quote_buf_with_size(struct strbuf *dst, const char *src, size_t size)
{
char *to_free = NULL;
size_t cur_size = 0;

if (dst->buf == src)
to_free = strbuf_detach(dst, NULL);

strbuf_addch(dst, '\'');
while (cur_size < size) {
size_t len = strcspn(src, "'!");
if (!len) {
strbuf_add(dst, src, 1);
src++;
cur_size++;
} else {
strbuf_add(dst, src, len);
src += len;
cur_size += len;
}
if (cur_size >= size)
break;
while (need_bs_quote(*src)) {
strbuf_addstr(dst, "'\\");
strbuf_addch(dst, *src++);
cur_size++;
strbuf_addch(dst, '\'');
}
}
strbuf_addch(dst, '\'');
free(to_free);
}

void sq_quote_buf_pretty(struct strbuf *dst, const char *src)
{
static const char ok_punct[] = "+,-./:=@_^";
Expand Down Expand Up @@ -471,6 +504,25 @@ void perl_quote_buf(struct strbuf *sb, const char *src)
strbuf_addch(sb, sq);
}

void perl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size)
{
const char sq = '\'';
const char bq = '\\';
char c;
size_t cur_size = 0;

strbuf_addch(sb, sq);
while (cur_size < size) {
c = *src++;
cur_size++;

if (c == sq || c == bq)
strbuf_addch(sb, bq);
strbuf_addch(sb, c);
}
strbuf_addch(sb, sq);
}

void python_quote_buf(struct strbuf *sb, const char *src)
{
const char sq = '\'';
Expand All @@ -492,6 +544,31 @@ void python_quote_buf(struct strbuf *sb, const char *src)
strbuf_addch(sb, sq);
}

void python_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size)
{
const char sq = '\'';
const char bq = '\\';
const char nl = '\n';
char c;
size_t cur_size = 0;

strbuf_addch(sb, sq);
while (cur_size < size) {
c = *src++;
cur_size++;

if (c == nl) {
strbuf_addch(sb, bq);
strbuf_addch(sb, 'n');
continue;
}
if (c == sq || c == bq)
strbuf_addch(sb, bq);
strbuf_addch(sb, c);
}
strbuf_addch(sb, sq);
}

void tcl_quote_buf(struct strbuf *sb, const char *src)
{
char c;
Expand Down Expand Up @@ -527,6 +604,45 @@ void tcl_quote_buf(struct strbuf *sb, const char *src)
strbuf_addch(sb, '"');
}

void tcl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size)
{
char c;
size_t cur_size = 0;

strbuf_addch(sb, '"');
while (cur_size < size) {
c = *src++;
cur_size++;

switch (c) {
case '[': case ']':
case '{': case '}':
case '$': case '\\': case '"':
strbuf_addch(sb, '\\');
/* fallthrough */
default:
strbuf_addch(sb, c);
break;
case '\f':
strbuf_addstr(sb, "\\f");
break;
case '\r':
strbuf_addstr(sb, "\\r");
break;
case '\n':
strbuf_addstr(sb, "\\n");
break;
case '\t':
strbuf_addstr(sb, "\\t");
break;
case '\v':
strbuf_addstr(sb, "\\v");
break;
}
}
strbuf_addch(sb, '"');
}

void basic_regex_quote_buf(struct strbuf *sb, const char *src)
{
char c;
Expand Down
4 changes: 4 additions & 0 deletions quote.h
Expand Up @@ -30,6 +30,7 @@ struct strbuf;
*/

void sq_quote_buf(struct strbuf *, const char *src);
void sq_quote_buf_with_size(struct strbuf *, const char *src, size_t size);
void sq_quote_argv(struct strbuf *, const char **argv);
void sq_quotef(struct strbuf *, const char *fmt, ...);

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

/* quoting as a string literal for other languages */
void perl_quote_buf(struct strbuf *sb, const char *src);
void perl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size);
void python_quote_buf(struct strbuf *sb, const char *src);
void python_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size);
void tcl_quote_buf(struct strbuf *sb, const char *src);
void tcl_quote_buf_with_size(struct strbuf *sb, const char *src, size_t size);
void basic_regex_quote_buf(struct strbuf *sb, const char *src);

#endif

0 comments on commit 96f42f6

Please sign in to comment.