1 #include "git-compat-util.h"
4 #include "string-list.h"
8 int starts_with(const char *str
, const char *prefix
)
10 for (; ; str
++, prefix
++)
13 else if (*str
!= *prefix
)
17 int istarts_with(const char *str
, const char *prefix
)
19 for (; ; str
++, prefix
++)
22 else if (tolower(*str
) != tolower(*prefix
))
26 int skip_to_optional_arg_default(const char *str
, const char *prefix
,
27 const char **arg
, const char *def
)
31 if (!skip_prefix(str
, prefix
, &p
))
49 * Used as the default ->buf value, so that people can always assume
50 * buf is non NULL and ->buf is NUL terminated even for a freshly
53 char strbuf_slopbuf
[1];
55 void strbuf_init(struct strbuf
*sb
, size_t hint
)
57 struct strbuf blank
= STRBUF_INIT
;
58 memcpy(sb
, &blank
, sizeof(*sb
));
60 strbuf_grow(sb
, hint
);
63 void strbuf_release(struct strbuf
*sb
)
71 char *strbuf_detach(struct strbuf
*sb
, size_t *sz
)
82 void strbuf_attach(struct strbuf
*sb
, void *buf
, size_t len
, size_t alloc
)
89 sb
->buf
[sb
->len
] = '\0';
92 void strbuf_grow(struct strbuf
*sb
, size_t extra
)
94 int new_buf
= !sb
->alloc
;
95 if (unsigned_add_overflows(extra
, 1) ||
96 unsigned_add_overflows(sb
->len
, extra
+ 1))
97 die("you want to use way too much memory");
100 ALLOC_GROW(sb
->buf
, sb
->len
+ extra
+ 1, sb
->alloc
);
105 void strbuf_trim(struct strbuf
*sb
)
111 void strbuf_rtrim(struct strbuf
*sb
)
113 while (sb
->len
> 0 && isspace((unsigned char)sb
->buf
[sb
->len
- 1]))
115 sb
->buf
[sb
->len
] = '\0';
118 void strbuf_trim_trailing_dir_sep(struct strbuf
*sb
)
120 while (sb
->len
> 0 && is_dir_sep((unsigned char)sb
->buf
[sb
->len
- 1]))
122 sb
->buf
[sb
->len
] = '\0';
125 void strbuf_trim_trailing_newline(struct strbuf
*sb
)
127 if (sb
->len
> 0 && sb
->buf
[sb
->len
- 1] == '\n') {
128 if (--sb
->len
> 0 && sb
->buf
[sb
->len
- 1] == '\r')
130 sb
->buf
[sb
->len
] = '\0';
134 void strbuf_ltrim(struct strbuf
*sb
)
137 while (sb
->len
> 0 && isspace(*b
)) {
141 memmove(sb
->buf
, b
, sb
->len
);
142 sb
->buf
[sb
->len
] = '\0';
145 int strbuf_reencode(struct strbuf
*sb
, const char *from
, const char *to
)
150 if (same_encoding(from
, to
))
153 out
= reencode_string_len(sb
->buf
, sb
->len
, to
, from
, &len
);
157 strbuf_attach(sb
, out
, len
, len
);
161 void strbuf_tolower(struct strbuf
*sb
)
163 char *p
= sb
->buf
, *end
= sb
->buf
+ sb
->len
;
168 struct strbuf
**strbuf_split_buf(const char *str
, size_t slen
,
169 int terminator
, int max
)
171 struct strbuf
**ret
= NULL
;
172 size_t nr
= 0, alloc
= 0;
177 if (max
<= 0 || nr
+ 1 < max
) {
178 const char *end
= memchr(str
, terminator
, slen
);
182 t
= xmalloc(sizeof(struct strbuf
));
184 strbuf_add(t
, str
, len
);
185 ALLOC_GROW(ret
, nr
+ 2, alloc
);
190 ALLOC_GROW(ret
, nr
+ 1, alloc
); /* In case string was empty */
195 void strbuf_add_separated_string_list(struct strbuf
*str
,
197 struct string_list
*slist
)
199 struct string_list_item
*item
;
202 for_each_string_list_item(item
, slist
) {
204 strbuf_addstr(str
, sep
);
205 strbuf_addstr(str
, item
->string
);
210 void strbuf_list_free(struct strbuf
**sbs
)
212 struct strbuf
**s
= sbs
;
223 int strbuf_cmp(const struct strbuf
*a
, const struct strbuf
*b
)
225 size_t len
= a
->len
< b
->len
? a
->len
: b
->len
;
226 int cmp
= memcmp(a
->buf
, b
->buf
, len
);
229 return a
->len
< b
->len
? -1: a
->len
!= b
->len
;
232 void strbuf_splice(struct strbuf
*sb
, size_t pos
, size_t len
,
233 const void *data
, size_t dlen
)
235 if (unsigned_add_overflows(pos
, len
))
236 die("you want to use way too much memory");
238 die("`pos' is too far after the end of the buffer");
239 if (pos
+ len
> sb
->len
)
240 die("`pos + len' is too far after the end of the buffer");
243 strbuf_grow(sb
, dlen
- len
);
244 memmove(sb
->buf
+ pos
+ dlen
,
246 sb
->len
- pos
- len
);
247 memcpy(sb
->buf
+ pos
, data
, dlen
);
248 strbuf_setlen(sb
, sb
->len
+ dlen
- len
);
251 void strbuf_insert(struct strbuf
*sb
, size_t pos
, const void *data
, size_t len
)
253 strbuf_splice(sb
, pos
, 0, data
, len
);
256 void strbuf_vinsertf(struct strbuf
*sb
, size_t pos
, const char *fmt
, va_list ap
)
263 die("`pos' is too far after the end of the buffer");
265 len
= vsnprintf(sb
->buf
+ sb
->len
, 0, fmt
, cp
);
268 BUG("your vsnprintf is broken (returned %d)", len
);
270 return; /* nothing to do */
271 if (unsigned_add_overflows(sb
->len
, len
))
272 die("you want to use way too much memory");
273 strbuf_grow(sb
, len
);
274 memmove(sb
->buf
+ pos
+ len
, sb
->buf
+ pos
, sb
->len
- pos
);
275 /* vsnprintf() will append a NUL, overwriting one of our characters */
276 save
= sb
->buf
[pos
+ len
];
277 len2
= vsnprintf(sb
->buf
+ pos
, len
+ 1, fmt
, ap
);
278 sb
->buf
[pos
+ len
] = save
;
280 BUG("your vsnprintf is broken (returns inconsistent lengths)");
281 strbuf_setlen(sb
, sb
->len
+ len
);
284 void strbuf_insertf(struct strbuf
*sb
, size_t pos
, const char *fmt
, ...)
288 strbuf_vinsertf(sb
, pos
, fmt
, ap
);
292 void strbuf_remove(struct strbuf
*sb
, size_t pos
, size_t len
)
294 strbuf_splice(sb
, pos
, len
, "", 0);
297 void strbuf_add(struct strbuf
*sb
, const void *data
, size_t len
)
299 strbuf_grow(sb
, len
);
300 memcpy(sb
->buf
+ sb
->len
, data
, len
);
301 strbuf_setlen(sb
, sb
->len
+ len
);
304 void strbuf_addbuf(struct strbuf
*sb
, const struct strbuf
*sb2
)
306 strbuf_grow(sb
, sb2
->len
);
307 memcpy(sb
->buf
+ sb
->len
, sb2
->buf
, sb2
->len
);
308 strbuf_setlen(sb
, sb
->len
+ sb2
->len
);
311 const char *strbuf_join_argv(struct strbuf
*buf
,
312 int argc
, const char **argv
, char delim
)
317 strbuf_addstr(buf
, *argv
);
319 strbuf_addch(buf
, delim
);
320 strbuf_addstr(buf
, *(++argv
));
326 void strbuf_addchars(struct strbuf
*sb
, int c
, size_t n
)
329 memset(sb
->buf
+ sb
->len
, c
, n
);
330 strbuf_setlen(sb
, sb
->len
+ n
);
333 void strbuf_addf(struct strbuf
*sb
, const char *fmt
, ...)
337 strbuf_vaddf(sb
, fmt
, ap
);
341 static void add_lines(struct strbuf
*out
,
344 const char *buf
, size_t size
)
348 const char *next
= memchr(buf
, '\n', size
);
349 next
= next
? (next
+ 1) : (buf
+ size
);
351 prefix
= ((prefix2
&& (buf
[0] == '\n' || buf
[0] == '\t'))
352 ? prefix2
: prefix1
);
353 strbuf_addstr(out
, prefix
);
354 strbuf_add(out
, buf
, next
- buf
);
358 strbuf_complete_line(out
);
361 void strbuf_add_commented_lines(struct strbuf
*out
, const char *buf
, size_t size
)
363 static char prefix1
[3];
364 static char prefix2
[2];
366 if (prefix1
[0] != comment_line_char
) {
367 xsnprintf(prefix1
, sizeof(prefix1
), "%c ", comment_line_char
);
368 xsnprintf(prefix2
, sizeof(prefix2
), "%c", comment_line_char
);
370 add_lines(out
, prefix1
, prefix2
, buf
, size
);
373 void strbuf_commented_addf(struct strbuf
*sb
, const char *fmt
, ...)
376 struct strbuf buf
= STRBUF_INIT
;
377 int incomplete_line
= sb
->len
&& sb
->buf
[sb
->len
- 1] != '\n';
379 va_start(params
, fmt
);
380 strbuf_vaddf(&buf
, fmt
, params
);
383 strbuf_add_commented_lines(sb
, buf
.buf
, buf
.len
);
385 sb
->buf
[--sb
->len
] = '\0';
387 strbuf_release(&buf
);
390 void strbuf_vaddf(struct strbuf
*sb
, const char *fmt
, va_list ap
)
395 if (!strbuf_avail(sb
))
398 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, cp
);
401 BUG("your vsnprintf is broken (returned %d)", len
);
402 if (len
> strbuf_avail(sb
)) {
403 strbuf_grow(sb
, len
);
404 len
= vsnprintf(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, ap
);
405 if (len
> strbuf_avail(sb
))
406 BUG("your vsnprintf is broken (insatiable)");
408 strbuf_setlen(sb
, sb
->len
+ len
);
411 void strbuf_expand(struct strbuf
*sb
, const char *format
, expand_fn_t fn
,
418 percent
= strchrnul(format
, '%');
419 strbuf_add(sb
, format
, percent
- format
);
422 format
= percent
+ 1;
424 if (*format
== '%') {
425 strbuf_addch(sb
, '%');
430 consumed
= fn(sb
, format
, context
);
434 strbuf_addch(sb
, '%');
438 size_t strbuf_expand_literal_cb(struct strbuf
*sb
,
439 const char *placeholder
,
440 void *context UNUSED
)
444 switch (placeholder
[0]) {
445 case 'n': /* newline */
446 strbuf_addch(sb
, '\n');
449 /* %x00 == NUL, %x0a == LF, etc. */
450 ch
= hex2chr(placeholder
+ 1);
453 strbuf_addch(sb
, ch
);
459 size_t strbuf_expand_dict_cb(struct strbuf
*sb
, const char *placeholder
,
462 struct strbuf_expand_dict_entry
*e
= context
;
465 for (; e
->placeholder
&& (len
= strlen(e
->placeholder
)); e
++) {
466 if (!strncmp(placeholder
, e
->placeholder
, len
)) {
468 strbuf_addstr(sb
, e
->value
);
475 void strbuf_addbuf_percentquote(struct strbuf
*dst
, const struct strbuf
*src
)
477 size_t i
, len
= src
->len
;
479 for (i
= 0; i
< len
; i
++) {
480 if (src
->buf
[i
] == '%')
481 strbuf_addch(dst
, '%');
482 strbuf_addch(dst
, src
->buf
[i
]);
486 #define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;="
488 void strbuf_add_percentencode(struct strbuf
*dst
, const char *src
, int flags
)
490 size_t i
, len
= strlen(src
);
492 for (i
= 0; i
< len
; i
++) {
493 unsigned char ch
= src
[i
];
494 if (ch
<= 0x1F || ch
>= 0x7F ||
495 (ch
== '/' && (flags
& STRBUF_ENCODE_SLASH
)) ||
496 strchr(URL_UNSAFE_CHARS
, ch
))
497 strbuf_addf(dst
, "%%%02X", (unsigned char)ch
);
499 strbuf_addch(dst
, ch
);
503 size_t strbuf_fread(struct strbuf
*sb
, size_t size
, FILE *f
)
506 size_t oldalloc
= sb
->alloc
;
508 strbuf_grow(sb
, size
);
509 res
= fread(sb
->buf
+ sb
->len
, 1, size
, f
);
511 strbuf_setlen(sb
, sb
->len
+ res
);
512 else if (oldalloc
== 0)
517 ssize_t
strbuf_read(struct strbuf
*sb
, int fd
, size_t hint
)
519 size_t oldlen
= sb
->len
;
520 size_t oldalloc
= sb
->alloc
;
522 strbuf_grow(sb
, hint
? hint
: 8192);
524 ssize_t want
= sb
->alloc
- sb
->len
- 1;
525 ssize_t got
= read_in_full(fd
, sb
->buf
+ sb
->len
, want
);
531 strbuf_setlen(sb
, oldlen
);
537 strbuf_grow(sb
, 8192);
540 sb
->buf
[sb
->len
] = '\0';
541 return sb
->len
- oldlen
;
544 ssize_t
strbuf_read_once(struct strbuf
*sb
, int fd
, size_t hint
)
546 size_t oldalloc
= sb
->alloc
;
549 strbuf_grow(sb
, hint
? hint
: 8192);
550 cnt
= xread(fd
, sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
- 1);
552 strbuf_setlen(sb
, sb
->len
+ cnt
);
553 else if (oldalloc
== 0)
558 ssize_t
strbuf_write(struct strbuf
*sb
, FILE *f
)
560 return sb
->len
? fwrite(sb
->buf
, 1, sb
->len
, f
) : 0;
563 #define STRBUF_MAXLINK (2*PATH_MAX)
565 int strbuf_readlink(struct strbuf
*sb
, const char *path
, size_t hint
)
567 size_t oldalloc
= sb
->alloc
;
572 while (hint
< STRBUF_MAXLINK
) {
575 strbuf_grow(sb
, hint
);
576 len
= readlink(path
, sb
->buf
, hint
);
580 } else if (len
< hint
) {
581 strbuf_setlen(sb
, len
);
585 /* .. the buffer was too small - try again */
593 int strbuf_getcwd(struct strbuf
*sb
)
595 size_t oldalloc
= sb
->alloc
;
596 size_t guessed_len
= 128;
598 for (;; guessed_len
*= 2) {
599 strbuf_grow(sb
, guessed_len
);
600 if (getcwd(sb
->buf
, sb
->alloc
)) {
601 strbuf_setlen(sb
, strlen(sb
->buf
));
606 * If getcwd(3) is implemented as a syscall that falls
607 * back to a regular lookup using readdir(3) etc. then
608 * we may be able to avoid EACCES by providing enough
609 * space to the syscall as it's not necessarily bound
610 * to the same restrictions as the fallback.
612 if (errno
== EACCES
&& guessed_len
< PATH_MAX
)
626 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
635 /* Translate slopbuf to NULL, as we cannot call realloc on it */
639 r
= getdelim(&sb
->buf
, &sb
->alloc
, term
, fp
);
648 * Normally we would have called xrealloc, which will try to free
649 * memory and recover. But we have no way to tell getdelim() to do so.
650 * Worse, we cannot try to recover ENOMEM ourselves, because we have
651 * no idea how many bytes were read by getdelim.
653 * Dying here is reasonable. It mirrors what xrealloc would do on
654 * catastrophic memory failure. We skip the opportunity to free pack
655 * memory and retry, but that's unlikely to help for a malloc small
656 * enough to hold a single line of input, anyway.
659 die("Out of memory, getdelim failed");
662 * Restore strbuf invariants; if getdelim left us with a NULL pointer,
663 * we can just re-init, but otherwise we should make sure that our
664 * length is empty, and that the result is NUL-terminated.
673 int strbuf_getwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
682 while ((ch
= getc_unlocked(fp
)) != EOF
) {
683 if (!strbuf_avail(sb
))
685 sb
->buf
[sb
->len
++] = ch
;
690 if (ch
== EOF
&& sb
->len
== 0)
693 sb
->buf
[sb
->len
] = '\0';
698 int strbuf_appendwholeline(struct strbuf
*sb
, FILE *fp
, int term
)
700 struct strbuf line
= STRBUF_INIT
;
701 if (strbuf_getwholeline(&line
, fp
, term
))
703 strbuf_addbuf(sb
, &line
);
704 strbuf_release(&line
);
708 static int strbuf_getdelim(struct strbuf
*sb
, FILE *fp
, int term
)
710 if (strbuf_getwholeline(sb
, fp
, term
))
712 if (sb
->buf
[sb
->len
- 1] == term
)
713 strbuf_setlen(sb
, sb
->len
- 1);
717 int strbuf_getline(struct strbuf
*sb
, FILE *fp
)
719 if (strbuf_getwholeline(sb
, fp
, '\n'))
721 if (sb
->buf
[sb
->len
- 1] == '\n') {
722 strbuf_setlen(sb
, sb
->len
- 1);
723 if (sb
->len
&& sb
->buf
[sb
->len
- 1] == '\r')
724 strbuf_setlen(sb
, sb
->len
- 1);
729 int strbuf_getline_lf(struct strbuf
*sb
, FILE *fp
)
731 return strbuf_getdelim(sb
, fp
, '\n');
734 int strbuf_getline_nul(struct strbuf
*sb
, FILE *fp
)
736 return strbuf_getdelim(sb
, fp
, '\0');
739 int strbuf_getwholeline_fd(struct strbuf
*sb
, int fd
, int term
)
745 ssize_t len
= xread(fd
, &ch
, 1);
748 strbuf_addch(sb
, ch
);
755 ssize_t
strbuf_read_file(struct strbuf
*sb
, const char *path
, size_t hint
)
761 fd
= open(path
, O_RDONLY
);
764 len
= strbuf_read(sb
, fd
, hint
);
775 void strbuf_add_lines(struct strbuf
*out
, const char *prefix
,
776 const char *buf
, size_t size
)
778 add_lines(out
, prefix
, NULL
, buf
, size
);
781 void strbuf_addstr_xml_quoted(struct strbuf
*buf
, const char *s
)
784 size_t len
= strcspn(s
, "\"<>&");
785 strbuf_add(buf
, s
, len
);
789 strbuf_addstr(buf
, """);
792 strbuf_addstr(buf
, "<");
795 strbuf_addstr(buf
, ">");
798 strbuf_addstr(buf
, "&");
807 int is_rfc3986_reserved_or_unreserved(char ch
)
809 if (is_rfc3986_unreserved(ch
))
812 case '!': case '*': case '\'': case '(': case ')': case ';':
813 case ':': case '@': case '&': case '=': case '+': case '$':
814 case ',': case '/': case '?': case '#': case '[': case ']':
820 int is_rfc3986_unreserved(char ch
)
822 return isalnum(ch
) ||
823 ch
== '-' || ch
== '_' || ch
== '.' || ch
== '~';
826 static void strbuf_add_urlencode(struct strbuf
*sb
, const char *s
, size_t len
,
827 char_predicate allow_unencoded_fn
)
829 strbuf_grow(sb
, len
);
832 if (allow_unencoded_fn(ch
))
833 strbuf_addch(sb
, ch
);
835 strbuf_addf(sb
, "%%%02x", (unsigned char)ch
);
839 void strbuf_addstr_urlencode(struct strbuf
*sb
, const char *s
,
840 char_predicate allow_unencoded_fn
)
842 strbuf_add_urlencode(sb
, s
, strlen(s
), allow_unencoded_fn
);
845 static void strbuf_humanise(struct strbuf
*buf
, off_t bytes
,
848 if (bytes
> 1 << 30) {
851 /* TRANSLATORS: IEC 80000-13:2008 gibibyte */
853 /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
855 (unsigned)(bytes
>> 30),
856 (unsigned)(bytes
& ((1 << 30) - 1)) / 10737419);
857 } else if (bytes
> 1 << 20) {
858 unsigned x
= bytes
+ 5243; /* for rounding */
861 /* TRANSLATORS: IEC 80000-13:2008 mebibyte */
863 /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
865 x
>> 20, ((x
& ((1 << 20) - 1)) * 100) >> 20);
866 } else if (bytes
> 1 << 10) {
867 unsigned x
= bytes
+ 5; /* for rounding */
870 /* TRANSLATORS: IEC 80000-13:2008 kibibyte */
872 /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
874 x
>> 10, ((x
& ((1 << 10) - 1)) * 100) >> 10);
878 /* TRANSLATORS: IEC 80000-13:2008 byte */
879 Q_("%u byte", "%u bytes", bytes
) :
880 /* TRANSLATORS: IEC 80000-13:2008 byte/second */
881 Q_("%u byte/s", "%u bytes/s", bytes
),
886 void strbuf_humanise_bytes(struct strbuf
*buf
, off_t bytes
)
888 strbuf_humanise(buf
, bytes
, 0);
891 void strbuf_humanise_rate(struct strbuf
*buf
, off_t bytes
)
893 strbuf_humanise(buf
, bytes
, 1);
896 void strbuf_add_absolute_path(struct strbuf
*sb
, const char *path
)
899 die("The empty string is not a valid path");
900 if (!is_absolute_path(path
)) {
901 struct stat cwd_stat
, pwd_stat
;
902 size_t orig_len
= sb
->len
;
903 char *cwd
= xgetcwd();
904 char *pwd
= getenv("PWD");
905 if (pwd
&& strcmp(pwd
, cwd
) &&
906 !stat(cwd
, &cwd_stat
) &&
907 (cwd_stat
.st_dev
|| cwd_stat
.st_ino
) &&
908 !stat(pwd
, &pwd_stat
) &&
909 pwd_stat
.st_dev
== cwd_stat
.st_dev
&&
910 pwd_stat
.st_ino
== cwd_stat
.st_ino
)
911 strbuf_addstr(sb
, pwd
);
913 strbuf_addstr(sb
, cwd
);
914 if (sb
->len
> orig_len
&& !is_dir_sep(sb
->buf
[sb
->len
- 1]))
915 strbuf_addch(sb
, '/');
918 strbuf_addstr(sb
, path
);
921 void strbuf_add_real_path(struct strbuf
*sb
, const char *path
)
924 struct strbuf resolved
= STRBUF_INIT
;
925 strbuf_realpath(&resolved
, path
, 1);
926 strbuf_addbuf(sb
, &resolved
);
927 strbuf_release(&resolved
);
929 strbuf_realpath(sb
, path
, 1);
932 int printf_ln(const char *fmt
, ...)
937 ret
= vprintf(fmt
, ap
);
939 if (ret
< 0 || putchar('\n') == EOF
)
944 int fprintf_ln(FILE *fp
, const char *fmt
, ...)
949 ret
= vfprintf(fp
, fmt
, ap
);
951 if (ret
< 0 || putc('\n', fp
) == EOF
)
956 char *xstrdup_tolower(const char *string
)
961 len
= strlen(string
);
962 result
= xmallocz(len
);
963 for (i
= 0; i
< len
; i
++)
964 result
[i
] = tolower(string
[i
]);
968 char *xstrdup_toupper(const char *string
)
973 len
= strlen(string
);
974 result
= xmallocz(len
);
975 for (i
= 0; i
< len
; i
++)
976 result
[i
] = toupper(string
[i
]);
980 char *xstrvfmt(const char *fmt
, va_list ap
)
982 struct strbuf buf
= STRBUF_INIT
;
983 strbuf_vaddf(&buf
, fmt
, ap
);
984 return strbuf_detach(&buf
, NULL
);
987 char *xstrfmt(const char *fmt
, ...)
993 ret
= xstrvfmt(fmt
, ap
);
999 void strbuf_addftime(struct strbuf
*sb
, const char *fmt
, const struct tm
*tm
,
1000 int tz_offset
, int suppress_tz_name
)
1002 struct strbuf munged_fmt
= STRBUF_INIT
;
1010 * There is no portable way to pass timezone information to
1011 * strftime, so we handle %z and %Z here. Likewise '%s', because
1012 * going back to an epoch time requires knowing the zone.
1014 * Note that tz_offset is in the "[-+]HHMM" decimal form; this is what
1015 * we want for %z, but the computation for %s has to convert to number
1019 const char *percent
= strchrnul(fmt
, '%');
1020 strbuf_add(&munged_fmt
, fmt
, percent
- fmt
);
1026 strbuf_addstr(&munged_fmt
, "%%");
1030 strbuf_addf(&munged_fmt
, "%"PRItime
,
1031 (timestamp_t
)tm_to_time_t(tm
) -
1032 3600 * (tz_offset
/ 100) -
1033 60 * (tz_offset
% 100));
1037 strbuf_addf(&munged_fmt
, "%+05d", tz_offset
);
1041 if (suppress_tz_name
) {
1047 strbuf_addch(&munged_fmt
, '%');
1050 fmt
= munged_fmt
.buf
;
1052 strbuf_grow(sb
, hint
);
1053 len
= strftime(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
, fmt
, tm
);
1057 * strftime reports "0" if it could not fit the result in the buffer.
1058 * Unfortunately, it also reports "0" if the requested time string
1059 * takes 0 bytes. So our strategy is to munge the format so that the
1060 * output contains at least one character, and then drop the extra
1061 * character before returning.
1063 strbuf_addch(&munged_fmt
, ' ');
1066 strbuf_grow(sb
, hint
);
1067 len
= strftime(sb
->buf
+ sb
->len
, sb
->alloc
- sb
->len
,
1068 munged_fmt
.buf
, tm
);
1070 len
--; /* drop munged space */
1072 strbuf_release(&munged_fmt
);
1073 strbuf_setlen(sb
, sb
->len
+ len
);
1076 void strbuf_repo_add_unique_abbrev(struct strbuf
*sb
, struct repository
*repo
,
1077 const struct object_id
*oid
, int abbrev_len
)
1080 strbuf_grow(sb
, GIT_MAX_HEXSZ
+ 1);
1081 r
= repo_find_unique_abbrev_r(repo
, sb
->buf
+ sb
->len
, oid
, abbrev_len
);
1082 strbuf_setlen(sb
, sb
->len
+ r
);
1085 void strbuf_add_unique_abbrev(struct strbuf
*sb
, const struct object_id
*oid
,
1088 strbuf_repo_add_unique_abbrev(sb
, the_repository
, oid
, abbrev_len
);
1092 * Returns the length of a line, without trailing spaces.
1094 * If the line ends with newline, it will be removed too.
1096 static size_t cleanup(char *line
, size_t len
)
1099 unsigned char c
= line
[len
- 1];
1109 * Remove empty lines from the beginning and end
1110 * and also trailing spaces from every line.
1112 * Turn multiple consecutive empty lines between paragraphs
1113 * into just one empty line.
1115 * If the input has only empty lines and spaces,
1116 * no output will be produced.
1118 * If last line does not have a newline at the end, one is added.
1120 * Enable skip_comments to skip every line starting with comment
1123 void strbuf_stripspace(struct strbuf
*sb
, int skip_comments
)
1126 size_t i
, j
, len
, newlen
;
1129 /* We may have to add a newline. */
1132 for (i
= j
= 0; i
< sb
->len
; i
+= len
, j
+= newlen
) {
1133 eol
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
1134 len
= eol
? eol
- (sb
->buf
+ i
) + 1 : sb
->len
- i
;
1136 if (skip_comments
&& len
&& sb
->buf
[i
] == comment_line_char
) {
1140 newlen
= cleanup(sb
->buf
+ i
, len
);
1142 /* Not just an empty line? */
1144 if (empties
> 0 && j
> 0)
1145 sb
->buf
[j
++] = '\n';
1147 memmove(sb
->buf
+ j
, sb
->buf
+ i
, newlen
);
1148 sb
->buf
[newlen
+ j
++] = '\n';
1154 strbuf_setlen(sb
, j
);
1157 int strbuf_normalize_path(struct strbuf
*src
)
1159 struct strbuf dst
= STRBUF_INIT
;
1161 strbuf_grow(&dst
, src
->len
);
1162 if (normalize_path_copy(dst
.buf
, src
->buf
) < 0) {
1163 strbuf_release(&dst
);
1168 * normalize_path does not tell us the new length, so we have to
1169 * compute it by looking for the new NUL it placed
1171 strbuf_setlen(&dst
, strlen(dst
.buf
));
1172 strbuf_swap(src
, &dst
);
1173 strbuf_release(&dst
);
1177 int strbuf_edit_interactively(struct strbuf
*buffer
, const char *path
,
1178 const char *const *env
)
1183 if (!is_absolute_path(path
))
1184 path
= path2
= xstrdup(git_path("%s", path
));
1186 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
1188 res
= error_errno(_("could not open '%s' for writing"), path
);
1189 else if (write_in_full(fd
, buffer
->buf
, buffer
->len
) < 0) {
1190 res
= error_errno(_("could not write to '%s'"), path
);
1192 } else if (close(fd
) < 0)
1193 res
= error_errno(_("could not close '%s'"), path
);
1195 strbuf_reset(buffer
);
1196 if (launch_editor(path
, buffer
, env
) < 0)
1197 res
= error_errno(_("could not edit '%s'"), path
);
1205 void strbuf_strip_file_from_path(struct strbuf
*sb
)
1207 char *path_sep
= find_last_dir_sep(sb
->buf
);
1208 strbuf_setlen(sb
, path_sep
? path_sep
- sb
->buf
+ 1 : 0);