diff-tree: don't print multiple headers for merges when silent.
[git.git] / diff-tree.c
index 3bf8899..6818648 100644 (file)
@@ -13,6 +13,7 @@ static int generate_patch = 0;
 static int detect_rename = 0;
 static int reverse_diff = 0;
 static int diff_score_opt = 0;
+static const char *pickaxe = NULL;
 static const char *header = NULL;
 static const char *header_prefix = "";
 
@@ -254,10 +255,10 @@ static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, co
        unsigned long size1, size2;
        int retval;
 
-       tree1 = read_object_with_reference(old, "tree", &size1, 0);
+       tree1 = read_object_with_reference(old, "tree", &size1, NULL);
        if (!tree1)
                die("unable to read source tree (%s)", sha1_to_hex(old));
-       tree2 = read_object_with_reference(new, "tree", &size2, 0);
+       tree2 = read_object_with_reference(new, "tree", &size2, NULL);
        if (!tree2)
                die("unable to read destination tree (%s)", sha1_to_hex(new));
        retval = diff_tree(tree1, size1, tree2, size2, base);
@@ -271,9 +272,9 @@ static int diff_tree_sha1_top(const unsigned char *old,
 {
        int ret;
 
-       diff_setup(detect_rename, diff_score_opt, reverse_diff,
-                  (generate_patch ? -1 : line_termination),
-                  0, 0);
+       diff_setup(detect_rename, diff_score_opt, pickaxe,
+                  reverse_diff, (generate_patch ? -1 : line_termination),
+                  NULL, 0);
        ret = diff_tree_sha1(old, new, base);
        diff_flush();
        return ret;
@@ -285,10 +286,10 @@ static int diff_root_tree(const unsigned char *new, const char *base)
        void *tree;
        unsigned long size;
 
-       diff_setup(detect_rename, diff_score_opt, reverse_diff,
-                  (generate_patch ? -1 : line_termination),
-                  0, 0);
-       tree = read_object_with_reference(new, "tree", &size, 0);
+       diff_setup(detect_rename, diff_score_opt, pickaxe,
+                  reverse_diff, (generate_patch ? -1 : line_termination),
+                  NULL, 0);
+       tree = read_object_with_reference(new, "tree", &size, NULL);
        if (!tree)
                die("unable to read root tree (%s)", sha1_to_hex(new));
        retval = diff_tree("", 0, tree, size, base);
@@ -331,7 +332,7 @@ static int add_author_info(char *buf, const char *line, int len)
 
 static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
 {
-       static char this_header[1000];
+       static char this_header[16384];
        int offset;
 
        offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
@@ -344,8 +345,16 @@ static char *generate_header(const char *commit, const char *parent, const char
 
                        if (!linelen)
                                break;
-                       if (offset + linelen + 10 > sizeof(this_header))
+
+                       /*
+                        * We want some slop for indentation and a possible
+                        * final "...". Thus the "+ 20".
+                        */
+                       if (offset + linelen + 20 > sizeof(this_header)) {
+                               memcpy(this_header + offset, "    ...\n", 8);
+                               offset += 8;
                                break;
+                       }
 
                        msg += linelen;
                        len -= linelen;
@@ -360,7 +369,12 @@ static char *generate_header(const char *commit, const char *parent, const char
                        memcpy(this_header + offset + 4, line, linelen);
                        offset += linelen + 4;
                }
-               this_header[offset++] = '\n';
+               /* Make sure there is an EOLN */
+               if (this_header[offset-1] != '\n')
+                       this_header[offset++] = '\n';
+               /* Add _another_ EOLN if we are doing diff output */
+               if (!silent)
+                       this_header[offset++] = '\n';
                this_header[offset] = 0;
        }
 
@@ -400,8 +414,15 @@ static int diff_tree_commit(const unsigned char *commit, const char *name)
                        return -1;
                header = generate_header(name, sha1_to_hex(parent), buf, size);
                diff_tree_sha1_top(parent, commit, "");
-               if (!header && verbose_header)
+               if (!header && verbose_header) {
                        header_prefix = "\ndiff-tree ";
+                       /*
+                        * Don't print multiple merge entries if we
+                        * don't print the diffs.
+                        */
+                       if (silent)
+                               break;
+               }
                offset += 48;
        }
        return 0;
@@ -430,7 +451,7 @@ static int diff_tree_stdin(char *line)
 }
 
 static char *diff_tree_usage =
-"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-R] [-m] [-s] [-v] <tree-ish> <tree-ish>";
+"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
 
 int main(int argc, char **argv)
 {
@@ -473,11 +494,21 @@ int main(int argc, char **argv)
                        recursive = generate_patch = 1;
                        continue;
                }
+               if (!strncmp(arg, "-S", 2)) {
+                       pickaxe = arg + 2;
+                       continue;
+               }
                if (!strncmp(arg, "-M", 2)) {
                        detect_rename = recursive = generate_patch = 1;
                        diff_score_opt = diff_scoreopt_parse(arg);
                        continue;
                }
+               if (!strncmp(arg, "-C", 2)) {
+                       detect_rename = 2;
+                       recursive = generate_patch = 1;
+                       diff_score_opt = diff_scoreopt_parse(arg);
+                       continue;
+               }
                if (!strcmp(arg, "-z")) {
                        line_termination = '\0';
                        continue;