From: Junio C Hamano Date: Wed, 7 Jan 2015 20:49:19 +0000 (-0800) Subject: Merge branch 'jc/strbuf-add-lines-avoid-sp-ht-sequence' X-Git-Tag: v2.3.0-rc0~28 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/58e0362eddba1678a71c8990862053ff989527b6?hp=-c Merge branch 'jc/strbuf-add-lines-avoid-sp-ht-sequence' The commented output used to blindly add a SP before the payload line, resulting in "# \t\n" when the payload began with a HT. Instead, produce "#\t\n". * jc/strbuf-add-lines-avoid-sp-ht-sequence: strbuf_add_commented_lines(): avoid SP-HT sequence in commented lines --- 58e0362eddba1678a71c8990862053ff989527b6 diff --combined strbuf.c index 0346e74a47,a4486a9e49..88cafd4a70 --- a/strbuf.c +++ b/strbuf.c @@@ -204,13 -204,6 +204,13 @@@ void strbuf_adddup(struct strbuf *sb, s strbuf_setlen(sb, sb->len + len); } +void strbuf_addchars(struct strbuf *sb, int c, size_t n) +{ + strbuf_grow(sb, n); + memset(sb->buf + sb->len, c, n); + strbuf_setlen(sb, sb->len + n); +} + void strbuf_addf(struct strbuf *sb, const char *fmt, ...) { va_list ap; @@@ -229,7 -222,8 +229,8 @@@ static void add_lines(struct strbuf *ou const char *next = memchr(buf, '\n', size); next = next ? (next + 1) : (buf + size); - prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1; + prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t')) + ? prefix2 : prefix1); strbuf_addstr(out, prefix); strbuf_add(out, buf, next - buf); size -= next - buf; @@@ -413,27 -407,6 +414,27 @@@ int strbuf_readlink(struct strbuf *sb, return -1; } +int strbuf_getcwd(struct strbuf *sb) +{ + size_t oldalloc = sb->alloc; + size_t guessed_len = 128; + + for (;; guessed_len *= 2) { + strbuf_grow(sb, guessed_len); + if (getcwd(sb->buf, sb->alloc)) { + strbuf_setlen(sb, strlen(sb->buf)); + return 0; + } + if (errno != ERANGE) + break; + } + if (oldalloc == 0) + strbuf_release(sb); + else + strbuf_reset(sb); + return -1; +} + int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) { int ch; @@@ -583,31 -556,6 +584,31 @@@ void strbuf_humanise_bytes(struct strbu } } +void strbuf_add_absolute_path(struct strbuf *sb, const char *path) +{ + if (!*path) + die("The empty string is not a valid path"); + if (!is_absolute_path(path)) { + struct stat cwd_stat, pwd_stat; + size_t orig_len = sb->len; + char *cwd = xgetcwd(); + char *pwd = getenv("PWD"); + if (pwd && strcmp(pwd, cwd) && + !stat(cwd, &cwd_stat) && + (cwd_stat.st_dev || cwd_stat.st_ino) && + !stat(pwd, &pwd_stat) && + pwd_stat.st_dev == cwd_stat.st_dev && + pwd_stat.st_ino == cwd_stat.st_ino) + strbuf_addstr(sb, pwd); + else + strbuf_addstr(sb, cwd); + if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1])) + strbuf_addch(sb, '/'); + free(cwd); + } + strbuf_addstr(sb, path); +} + int printf_ln(const char *fmt, ...) { int ret;