Use config file settings for http
[git.git] / diff.c
diff --git a/diff.c b/diff.c
index cafc755..cbb8632 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -10,7 +10,6 @@
 #include "diffcore.h"
 
 static const char *diff_opts = "-pu";
-static unsigned char null_sha1[20] = { 0, };
 
 static int use_size_cache;
 
@@ -414,7 +413,7 @@ void diff_free_filespec_data(struct diff_filespec *s)
 static void prep_temp_blob(struct diff_tempfile *temp,
                           void *blob,
                           unsigned long size,
-                          unsigned char *sha1,
+                          const unsigned char *sha1,
                           int mode)
 {
        int fd;
@@ -597,15 +596,31 @@ static void run_external_diff(const char *pgm,
        remove_tempfile();
 }
 
+static void diff_fill_sha1_info(struct diff_filespec *one)
+{
+       if (DIFF_FILE_VALID(one)) {
+               if (!one->sha1_valid) {
+                       struct stat st;
+                       if (stat(one->path, &st) < 0)
+                               die("stat %s", one->path);
+                       if (index_path(one->sha1, one->path, &st, 0))
+                               die("cannot hash %s\n", one->path);
+               }
+       }
+       else
+               memset(one->sha1, 0, 20);
+}
+
 static void run_diff(struct diff_filepair *p)
 {
        const char *pgm = external_diff();
-       char msg_[PATH_MAX*2+200], *xfrm_msg;
+       char msg[PATH_MAX*2+300], *xfrm_msg;
        struct diff_filespec *one;
        struct diff_filespec *two;
        const char *name;
        const char *other;
        int complete_rewrite = 0;
+       int len;
 
        if (DIFF_PAIR_UNMERGED(p)) {
                /* unmerged */
@@ -617,39 +632,60 @@ static void run_diff(struct diff_filepair *p)
        name = p->one->path;
        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
        one = p->one; two = p->two;
+
+       diff_fill_sha1_info(one);
+       diff_fill_sha1_info(two);
+
+       len = 0;
        switch (p->status) {
        case DIFF_STATUS_COPIED:
-               sprintf(msg_,
-                       "similarity index %d%%\n"
-                       "copy from %s\n"
-                       "copy to %s",
-                       (int)(0.5 + p->score * 100.0/MAX_SCORE),
-                       name, other);
-               xfrm_msg = msg_;
+               len += snprintf(msg + len, sizeof(msg) - len,
+                               "similarity index %d%%\n"
+                               "copy from %s\n"
+                               "copy to %s\n",
+                               (int)(0.5 + p->score * 100.0/MAX_SCORE),
+                               name, other);
                break;
        case DIFF_STATUS_RENAMED:
-               sprintf(msg_,
-                       "similarity index %d%%\n"
-                       "rename from %s\n"
-                       "rename to %s",
-                       (int)(0.5 + p->score * 100.0/MAX_SCORE),
-                       name, other);
-               xfrm_msg = msg_;
+               len += snprintf(msg + len, sizeof(msg) - len,
+                               "similarity index %d%%\n"
+                               "rename from %s\n"
+                               "rename to %s\n",
+                               (int)(0.5 + p->score * 100.0/MAX_SCORE),
+                               name, other);
                break;
        case DIFF_STATUS_MODIFIED:
                if (p->score) {
-                       sprintf(msg_,
-                               "dissimilarity index %d%%",
-                               (int)(0.5 + p->score * 100.0/MAX_SCORE));
-                       xfrm_msg = msg_;
+                       len += snprintf(msg + len, sizeof(msg) - len,
+                                       "dissimilarity index %d%%\n",
+                                       (int)(0.5 + p->score *
+                                             100.0/MAX_SCORE));
                        complete_rewrite = 1;
                        break;
                }
                /* fallthru */
        default:
-               xfrm_msg = NULL;
+               /* nothing */
+               ;
+       }
+
+       if (memcmp(one->sha1, two->sha1, 20)) {
+               char one_sha1[41];
+               memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
+
+               len += snprintf(msg + len, sizeof(msg) - len,
+                               "index %.7s..%.7s", one_sha1,
+                               sha1_to_hex(two->sha1));
+               if (one->mode == two->mode)
+                       len += snprintf(msg + len, sizeof(msg) - len,
+                                       " %06o", one->mode);
+               len += snprintf(msg + len, sizeof(msg) - len, "\n");
        }
 
+       if (len)
+               msg[--len] = 0;
+       xfrm_msg = len ? msg : NULL;
+
        if (!pgm &&
            DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
            (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
@@ -709,6 +745,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
                options->rename_limit = strtoul(arg+2, NULL, 10);
        else if (!strcmp(arg, "--name-only"))
                options->output_format = DIFF_FORMAT_NAME;
+       else if (!strcmp(arg, "--name-status"))
+               options->output_format = DIFF_FORMAT_NAME_STATUS;
        else if (!strcmp(arg, "-R"))
                options->reverse_diff = 1;
        else if (!strncmp(arg, "-S", 2))
@@ -835,7 +873,8 @@ void diff_free_filepair(struct diff_filepair *p)
 
 static void diff_flush_raw(struct diff_filepair *p,
                           int line_termination,
-                          int inter_name_termination)
+                          int inter_name_termination,
+                          int output_format)
 {
        int two_paths;
        char status[10];
@@ -871,13 +910,12 @@ static void diff_flush_raw(struct diff_filepair *p,
                two_paths = 0;
                break;
        }
-       printf(":%06o %06o %s ",
-              p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
-       printf("%s %s%c%s",
-              sha1_to_hex(p->two->sha1),
-              status,
-              inter_name_termination,
-              p->one->path);
+       if (output_format != DIFF_FORMAT_NAME_STATUS) {
+               printf(":%06o %06o %s ",
+                      p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
+               printf("%s ", sha1_to_hex(p->two->sha1));
+       }
+       printf("%s%c%s",status, inter_name_termination, p->one->path);
        if (two_paths)
                printf("%c%s", inter_name_termination, p->two->path);
        putchar(line_termination);
@@ -1067,8 +1105,10 @@ void diff_flush(struct diff_options *options)
                        diff_flush_patch(p);
                        break;
                case DIFF_FORMAT_RAW:
+               case DIFF_FORMAT_NAME_STATUS:
                        diff_flush_raw(p, line_termination,
-                                      inter_name_termination);
+                                      inter_name_termination,
+                                      diff_output_format);
                        break;
                case DIFF_FORMAT_NAME:
                        diff_flush_name(p, line_termination);