- if (WIFEXITED(status) && !WEXITSTATUS(status))
- return 0;
- return -1;
-}
-
-/* An external diff command takes:
- *
- * diff-cmd name infile1 infile1-sha1 infile1-mode \
- * infile2 infile2-sha1 infile2-mode [ rename-to ]
- *
- */
-static void run_external_diff(const char *pgm,
- const char *name,
- const char *other,
- struct diff_filespec *one,
- struct diff_filespec *two,
- const char *xfrm_msg,
- int complete_rewrite)
-{
- const char *spawn_arg[10];
- struct diff_tempfile *temp = diff_temp;
- int retval;
- static int atexit_asked = 0;
- const char *othername;
- const char **arg = &spawn_arg[0];
-
- othername = (other? other : name);
- if (one && two) {
- prepare_temp_file(name, &temp[0], one);
- prepare_temp_file(othername, &temp[1], two);
- if (! atexit_asked &&
- (temp[0].name == temp[0].tmp_path ||
- temp[1].name == temp[1].tmp_path)) {
- atexit_asked = 1;
- atexit(remove_tempfile);
- }
- signal(SIGINT, remove_tempfile_on_signal);
- }
-
- if (one && two) {
- *arg++ = pgm;
- *arg++ = name;
- *arg++ = temp[0].name;
- *arg++ = temp[0].hex;
- *arg++ = temp[0].mode;
- *arg++ = temp[1].name;
- *arg++ = temp[1].hex;
- *arg++ = temp[1].mode;
- if (other) {
- *arg++ = other;
- *arg++ = xfrm_msg;
- }
- } else {
- *arg++ = pgm;
- *arg++ = name;
- }
- *arg = NULL;
- retval = spawn_prog(pgm, spawn_arg);
- remove_tempfile();
- if (retval) {
- fprintf(stderr, "external diff died, stopping at %s.\n", name);
- exit(1);
- }
-}
-
-static void run_diff_cmd(const char *pgm,
- const char *name,
- const char *other,
- struct diff_filespec *one,
- struct diff_filespec *two,
- const char *xfrm_msg,
- int complete_rewrite)
-{
- if (pgm) {
- run_external_diff(pgm, name, other, one, two, xfrm_msg,
- complete_rewrite);
- return;
- }
- if (one && two)
- builtin_diff(name, other ? other : name,
- one, two, xfrm_msg, complete_rewrite);
- else
- printf("* Unmerged path %s\n", name);
-}
-
-static void diff_fill_sha1_info(struct diff_filespec *one)
-{
- if (DIFF_FILE_VALID(one)) {
- if (!one->sha1_valid) {
- struct stat st;
- if (lstat(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, struct diff_options *o)
-{
- const char *pgm = external_diff();
- char msg[PATH_MAX*2+300], *xfrm_msg;
- struct diff_filespec *one;
- struct diff_filespec *two;
- const char *name;
- const char *other;
- char *name_munged, *other_munged;
- int complete_rewrite = 0;
- int len;
-
- if (DIFF_PAIR_UNMERGED(p)) {
- /* unmerged */
- run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
- return;
- }
-
- name = p->one->path;
- other = (strcmp(name, p->two->path) ? p->two->path : NULL);
- name_munged = quote_one(name);
- other_munged = quote_one(other);
- 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:
- 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_munged, other_munged);
- break;
- case DIFF_STATUS_RENAMED:
- 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_munged, other_munged);
- break;
- case DIFF_STATUS_MODIFIED:
- if (p->score) {
- 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:
- /* nothing */
- ;
- }
-
- if (memcmp(one->sha1, two->sha1, 20)) {
- char one_sha1[41];
- int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
- memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
-
- len += snprintf(msg + len, sizeof(msg) - len,
- "index %.*s..%.*s",
- abbrev, one_sha1, abbrev,
- 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)) {
- /* a filepair that changes between file and symlink
- * needs to be split into deletion and creation.
- */
- struct diff_filespec *null = alloc_filespec(two->path);
- run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
- free(null);
- null = alloc_filespec(one->path);
- run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
- free(null);
- }
- else
- run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
- complete_rewrite);
-
- free(name_munged);
- free(other_munged);
-}
-
-static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
- struct diffstat_t *diffstat)
-{
- const char *name;
- const char *other;
-
- if (DIFF_PAIR_UNMERGED(p)) {
- /* unmerged */
- builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat);