From d1df5743809614241883ecad51876607cf432034 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 25 Apr 2005 18:26:45 -0700 Subject: [PATCH] Introduce diff-tree-helper. This patch introduces a new program, diff-tree-helper. It reads output from diff-cache and diff-tree, and produces a patch file. The diff format customization can be done the same way the show-diff uses; the same external diff interface introduced by the previous patch to drive diff from show-diff is used so this is not surprising. It is used like the following examples: $ diff-cache --cached -z | diff-tree-helper -z -R paths... $ diff-tree -r -z | diff-tree-helper -z paths... - As usual, the use of the -z flag is recommended in the script to pass NUL-terminated filenames through the pipe between commands. - The -R flag is used to generate reverse diff. It does not matter for diff-tree case, but it is sometimes useful to get a patch in the desired direction out of diff-cache. - The paths parameters are used to restrict the paths that appears in the output. Again this is useful to use with diff-cache, which, unlike diff-tree, does not take such paths restriction parameters. Signed-off-by: Junio C Hamano Signed-off-by: Linus Torvalds --- strbuf.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 strbuf.c (limited to 'strbuf.c') diff --git a/strbuf.c b/strbuf.c new file mode 100644 index 0000000000..dac945c7c8 --- /dev/null +++ b/strbuf.c @@ -0,0 +1,43 @@ +#include +#include +#include "strbuf.h" + +void strbuf_init(struct strbuf *sb) { + sb->buf = 0; + sb->eof = sb->alloc = sb->len = 0; +} + +static void strbuf_begin(struct strbuf *sb) { + free(sb->buf); + strbuf_init(sb); +} + +static void inline strbuf_add(struct strbuf *sb, int ch) { + if (sb->alloc <= sb->len) { + sb->alloc = sb->alloc * 3 / 2 + 16; + sb->buf = realloc(sb->buf, sb->alloc); + } + sb->buf[sb->len++] = ch; +} + +static void strbuf_end(struct strbuf *sb) { + strbuf_add(sb, 0); +} + +void read_line(struct strbuf *sb, FILE *fp, int term) { + int ch; + strbuf_begin(sb); + if (feof(fp)) { + sb->eof = 1; + return; + } + while ((ch = fgetc(fp)) != EOF) { + if (ch == term) + break; + strbuf_add(sb, ch); + } + if (sb->len == 0) + sb->eof = 1; + strbuf_end(sb); +} + -- cgit v1.2.1