diff options
author | Junio C Hamano <gitster@pobox.com> | 2019-04-22 11:14:43 +0900 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2019-04-22 11:14:43 +0900 |
commit | e36adf712251a19bd2ada0016764d340dfcf2ba0 (patch) | |
tree | 0b58e0a8e315bb737fdcdb16cf8f30d262025f76 /strbuf.c | |
parent | ffac537e6cbbf934b08745a378932722df287a53 (diff) | |
parent | 7906af0cb84c8e65656347909abd4e22b04d1c1e (diff) | |
download | git-e36adf712251a19bd2ada0016764d340dfcf2ba0.tar.gz |
Merge branch 'ps/stash-in-c'
"git stash" rewritten in C.
* ps/stash-in-c: (28 commits)
tests: add a special setup where stash.useBuiltin is off
stash: optionally use the scripted version again
stash: add back the original, scripted `git stash`
stash: convert `stash--helper.c` into `stash.c`
stash: replace all `write-tree` child processes with API calls
stash: optimize `get_untracked_files()` and `check_changes()`
stash: convert save to builtin
stash: make push -q quiet
stash: convert push to builtin
stash: convert create to builtin
stash: convert store to builtin
stash: convert show to builtin
stash: convert list to builtin
stash: convert pop to builtin
stash: convert branch to builtin
stash: convert drop and clear to builtin
stash: convert apply to builtin
stash: mention options in `show` synopsis
stash: add tests for `git stash show` config
stash: rename test cases to be more descriptive
...
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -249,6 +249,42 @@ void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) strbuf_splice(sb, pos, 0, data, len); } +void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap) +{ + int len, len2; + char save; + va_list cp; + + if (pos > sb->len) + die("`pos' is too far after the end of the buffer"); + va_copy(cp, ap); + len = vsnprintf(sb->buf + sb->len, 0, fmt, cp); + va_end(cp); + if (len < 0) + BUG("your vsnprintf is broken (returned %d)", len); + if (!len) + return; /* nothing to do */ + if (unsigned_add_overflows(sb->len, len)) + die("you want to use way too much memory"); + strbuf_grow(sb, len); + memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos); + /* vsnprintf() will append a NUL, overwriting one of our characters */ + save = sb->buf[pos + len]; + len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap); + sb->buf[pos + len] = save; + if (len2 != len) + BUG("your vsnprintf is broken (returns inconsistent lengths)"); + strbuf_setlen(sb, sb->len + len); +} + +void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + strbuf_vinsertf(sb, pos, fmt, ap); + va_end(ap); +} + void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) { strbuf_splice(sb, pos, len, "", 0); @@ -268,6 +304,21 @@ void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) strbuf_setlen(sb, sb->len + sb2->len); } +const char *strbuf_join_argv(struct strbuf *buf, + int argc, const char **argv, char delim) +{ + if (!argc) + return buf->buf; + + strbuf_addstr(buf, *argv); + while (--argc) { + strbuf_addch(buf, delim); + strbuf_addstr(buf, *(++argv)); + } + + return buf->buf; +} + void strbuf_addchars(struct strbuf *sb, int c, size_t n) { strbuf_grow(sb, n); |