X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=diff.c;h=4cc41f097dfe1ade230d2d6e438b0249dfbc1a76;hb=3a663fd9842aaa9e232ab43a811d11f1d7f0b7bc;hp=9905cdb0122afecc282273c0e728c3f3b75fc51d;hpb=be3cfa85f45e32722a65349d023667be906a66b6;p=git.git diff --git a/diff.c b/diff.c index 9905cdb0..4cc41f09 100644 --- a/diff.c +++ b/diff.c @@ -59,8 +59,7 @@ static char *sq_expand(const char *src) if (*cp == '\'') cnt += 3; - if (! (buf = malloc(cnt))) - return buf; + buf = xmalloc(cnt); bp = buf; while ((c = *src++)) { if (c != '\'') @@ -100,7 +99,7 @@ static void builtin_diff(const char *name, strlen(diff_arg) + strlen(name_1_sq) + strlen(name_2_sq) - 5); - char *cmd = malloc(cmd_size); + char *cmd = xmalloc(cmd_size); int next_at = 0; next_at += snprintf(cmd+next_at, cmd_size-next_at, @@ -195,13 +194,15 @@ void run_external_diff(const char *name, int pid, status; static int atexit_asked = 0; - prepare_temp_file(name, &temp[0], one); - prepare_temp_file(name, &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); + if (one && two) { + prepare_temp_file(name, &temp[0], one); + prepare_temp_file(name, &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); + } } fflush(NULL); @@ -210,16 +211,23 @@ void run_external_diff(const char *name, die("unable to fork"); if (!pid) { const char *pgm = external_diff(); - if (pgm) - execlp(pgm, pgm, - name, - temp[0].name, temp[0].hex, temp[0].mode, - temp[1].name, temp[1].hex, temp[1].mode, - NULL); + if (pgm) { + if (one && two) + execlp(pgm, pgm, + name, + temp[0].name, temp[0].hex, temp[0].mode, + temp[1].name, temp[1].hex, temp[1].mode, + NULL); + else + execlp(pgm, pgm, name, NULL); + } /* * otherwise we use the built-in one. */ - builtin_diff(name, temp); + if (one && two) + builtin_diff(name, temp); + else + printf("* Unmerged path %s\n", name); exit(0); } if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status)) @@ -228,41 +236,53 @@ void run_external_diff(const char *name, remove_tempfile(); } -void show_diff_empty(const struct cache_entry *ce, int reverse) +void diff_addremove(int addremove, unsigned mode, + const unsigned char *sha1, + const char *base, const char *path) { + char concatpath[PATH_MAX]; struct diff_spec spec[2], *one, *two; - memcpy(spec[0].u.sha1, ce->sha1, 20); - spec[0].mode = ntohl(ce->ce_mode); + memcpy(spec[0].u.sha1, sha1, 20); + spec[0].mode = mode; spec[0].sha1_valid = spec[0].file_valid = 1; spec[1].file_valid = 0; - if (reverse) { + if (addremove == '+') { one = spec + 1; two = spec; } else { one = spec; two = one + 1; } - - run_external_diff(ce->name, one, two); + + if (path) { + strcpy(concatpath, base); + strcat(concatpath, path); + } + run_external_diff(path ? concatpath : base, one, two); } -void show_differences(const struct cache_entry *ce, int reverse) -{ - struct diff_spec spec[2], *one, *two; - - memcpy(spec[0].u.sha1, ce->sha1, 20); - spec[0].mode = ntohl(ce->ce_mode); +void diff_change(unsigned old_mode, unsigned new_mode, + const unsigned char *old_sha1, + const unsigned char *new_sha1, + const char *base, const char *path) { + char concatpath[PATH_MAX]; + struct diff_spec spec[2]; + + memcpy(spec[0].u.sha1, old_sha1, 20); + spec[0].mode = old_mode; + memcpy(spec[1].u.sha1, new_sha1, 20); + spec[1].mode = new_mode; spec[0].sha1_valid = spec[0].file_valid = 1; + spec[1].sha1_valid = spec[1].file_valid = 1; - spec[1].u.name = ce->name; /* the name we stated */ - spec[1].sha1_valid = 0; - spec[1].file_valid = 1; - - if (reverse) { - one = spec + 1; two = spec; - } else { - one = spec; two = one + 1; + if (path) { + strcpy(concatpath, base); + strcat(concatpath, path); } + run_external_diff(path ? concatpath : base, &spec[0], &spec[1]); +} - run_external_diff(ce->name, one, two); +void diff_unmerge(const char *path) +{ + run_external_diff(path, NULL, NULL); }