{
static const char *external_diff_cmd = NULL;
static int done_preparing = 0;
+ const char *env_diff_opts;
if (done_preparing)
return external_diff_cmd;
*
* GIT_DIFF_OPTS="-c";
*/
- if (gitenv("GIT_EXTERNAL_DIFF"))
- external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
+ external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
/* In case external diff fails... */
- diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
+ env_diff_opts = getenv("GIT_DIFF_OPTS");
+ if (env_diff_opts) diff_opts = env_diff_opts;
done_preparing = 1;
return external_diff_cmd;
}
+#define TEMPFILE_PATH_LEN 50
+
static struct diff_tempfile {
const char *name; /* filename external diff should read from */
char hex[41];
char mode[10];
- char tmp_path[50];
+ char tmp_path[TEMPFILE_PATH_LEN];
} diff_temp[2];
static int count_lines(const char *filename)
int complete_rewrite)
{
int i, next_at, cmd_size;
- const char *diff_cmd = "diff -L%s%s -L%s%s";
- const char *diff_arg = "%s %s||:"; /* "||:" is to return 0 */
+ const char *const diff_cmd = "diff -L%s%s -L%s%s";
+ const char *const diff_arg = "%s %s||:"; /* "||:" is to return 0 */
const char *input_name_sq[2];
const char *path0[2];
const char *path1[2];
{
int namelen = strlen(path);
struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
+
+ memset(spec, 0, sizeof(*spec));
spec->path = (char *)(spec + 1);
- strcpy(spec->path, path);
- spec->should_free = spec->should_munmap = 0;
- spec->xfrm_flags = 0;
- spec->size = 0;
- spec->data = NULL;
- spec->mode = 0;
- memset(spec->sha1, 0, 20);
+ memcpy(spec->path, path, namelen+1);
return spec;
}
if (fd < 0)
goto err_empty;
s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
- s->should_munmap = 1;
close(fd);
+ if (s->data == MAP_FAILED)
+ goto err_empty;
+ s->should_munmap = 1;
}
else {
char type[20];
{
int fd;
- strcpy(temp->tmp_path, ".diff_XXXXXX");
- fd = mkstemp(temp->tmp_path);
+ fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
if (fd < 0)
die("unable to create temp-file");
if (write(fd, blob, size) != size)
pid_t pid;
int status;
static int atexit_asked = 0;
+ const char *othername;
+ othername = (other? other : name);
if (one && two) {
prepare_temp_file(name, &temp[0], one);
- prepare_temp_file(other ? : name, &temp[1], two);
+ prepare_temp_file(othername, &temp[1], two);
if (! atexit_asked &&
(temp[0].name == temp[0].tmp_path ||
temp[1].name == temp[1].tmp_path)) {
* otherwise we use the built-in one.
*/
if (one && two)
- builtin_diff(name, other ? : name, temp, xfrm_msg,
+ builtin_diff(name, othername, temp, xfrm_msg,
complete_rewrite);
else
printf("* Unmerged path %s\n", name);
other = (strcmp(name, p->two->path) ? p->two->path : NULL);
one = p->one; two = p->two;
switch (p->status) {
- case 'C':
+ case DIFF_STATUS_COPIED:
sprintf(msg_,
"similarity index %d%%\n"
"copy from %s\n"
name, other);
xfrm_msg = msg_;
break;
- case 'R':
+ case DIFF_STATUS_RENAMED:
sprintf(msg_,
"similarity index %d%%\n"
"rename from %s\n"
name, other);
xfrm_msg = msg_;
break;
- case 'M':
+ case DIFF_STATUS_MODIFIED:
if (p->score) {
sprintf(msg_,
"dissimilarity index %d%%",
scale *= 10;
num = num * 10 + ch - '0';
}
- *cp++;
+ cp++;
}
*cp_p = cp;
dp->status = 0;
dp->source_stays = 0;
dp->broken_pair = 0;
- diff_q(queue, dp);
+ if (queue)
+ diff_q(queue, dp);
return dp;
}
{
diff_free_filespec_data(p->one);
diff_free_filespec_data(p->two);
+ free(p->one);
+ free(p->two);
free(p);
}
char status[10];
if (line_termination) {
- const char *err = "path %s cannot be expressed without -z";
+ const char *const err =
+ "path %s cannot be expressed without -z";
if (strchr(p->one->path, line_termination) ||
strchr(p->one->path, inter_name_termination))
die(err, p->one->path);
status[1] = 0;
}
switch (p->status) {
- case 'C': case 'R':
+ case DIFF_STATUS_COPIED:
+ case DIFF_STATUS_RENAMED:
two_paths = 1;
break;
- case 'N': case 'D':
+ case DIFF_STATUS_ADDED:
+ case DIFF_STATUS_DELETED:
two_paths = 0;
break;
default:
void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
{
fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
- x, one ? : "",
+ x, one ? one : "",
s->path,
DIFF_FILE_VALID(s) ? "valid" : "invalid",
s->mode,
s->sha1_valid ? sha1_to_hex(s->sha1) : "");
fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
- x, one ? : "",
+ x, one ? one : "",
s->size, s->xfrm_flags);
}
diff_debug_filespec(p->one, i, "one");
diff_debug_filespec(p->two, i, "two");
fprintf(stderr, "score %d, status %c stays %d broken %d\n",
- p->score, p->status ? : '?',
+ p->score, p->status ? p->status : '?',
p->source_stays, p->broken_pair);
}
p = q->queue[i];
p->status = 0; /* undecided */
if (DIFF_PAIR_UNMERGED(p))
- p->status = 'U';
+ p->status = DIFF_STATUS_UNMERGED;
else if (!DIFF_FILE_VALID(p->one))
- p->status = 'N';
+ p->status = DIFF_STATUS_ADDED;
else if (!DIFF_FILE_VALID(p->two))
- p->status = 'D';
+ p->status = DIFF_STATUS_DELETED;
else if (DIFF_PAIR_TYPE_CHANGED(p))
- p->status = 'T';
+ p->status = DIFF_STATUS_TYPE_CHANGED;
/* from this point on, we are dealing with a pair
* whose both sides are valid and of the same type, i.e.
*/
else if (DIFF_PAIR_RENAME(p)) {
if (p->source_stays) {
- p->status = 'C';
+ p->status = DIFF_STATUS_COPIED;
continue;
}
/* See if there is some other filepair that
* copies from the same source as us. If so
- * we are a copy. Otherwise we are a rename.
+ * we are a copy. Otherwise we are either a
+ * copy if the path stays, or a rename if it
+ * does not, but we already handled "stays" case.
*/
for (j = i + 1; j < q->nr; j++) {
pp = q->queue[j];
if (!DIFF_PAIR_RENAME(pp))
continue; /* not a rename/copy */
/* pp is a rename/copy from the same source */
- p->status = 'C';
+ p->status = DIFF_STATUS_COPIED;
break;
}
if (!p->status)
- p->status = 'R';
+ p->status = DIFF_STATUS_RENAMED;
}
else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
p->one->mode != p->two->mode)
- p->status = 'M';
+ p->status = DIFF_STATUS_MODIFIED;
else {
/* This is a "no-change" entry and should not
* happen anymore, but prepare for broken callers.
*/
error("feeding unmodified %s to diffcore",
p->one->path);
- p->status = 'X';
+ p->status = DIFF_STATUS_UNKNOWN;
}
}
diff_debug_queue("resolve-rename-copy done", q);
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
- (p->status == 'X'))
+ (p->status == DIFF_STATUS_UNKNOWN))
continue;
if (p->status == 0)
die("internal error in diff-resolve-rename-copy");
diff_flush_name(p, line_termination);
break;
}
- }
- for (i = 0; i < q->nr; i++)
diff_free_filepair(q->queue[i]);
+ }
free(q->queue);
q->queue = NULL;
q->nr = q->alloc = 0;
if (!filter)
return;
- if (strchr(filter, 'A')) {
- /* All-or-none */
+ if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
int found;
for (i = found = 0; !found && i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
- if (((p->status == 'M') &&
- ((p->score && strchr(filter, 'B')) ||
- (!p->score && strchr(filter, 'M')))) ||
- ((p->status != 'M') && strchr(filter, p->status)))
+ if (((p->status == DIFF_STATUS_MODIFIED) &&
+ ((p->score &&
+ strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
+ (!p->score &&
+ strchr(filter, DIFF_STATUS_MODIFIED)))) ||
+ ((p->status != DIFF_STATUS_MODIFIED) &&
+ strchr(filter, p->status)))
found++;
}
if (found)
/* Only the matching ones */
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
- if (((p->status == 'M') &&
- ((p->score && strchr(filter, 'B')) ||
- (!p->score && strchr(filter, 'M')))) ||
- ((p->status != 'M') && strchr(filter, p->status)))
+
+ if (((p->status == DIFF_STATUS_MODIFIED) &&
+ ((p->score &&
+ strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
+ (!p->score &&
+ strchr(filter, DIFF_STATUS_MODIFIED)))) ||
+ ((p->status != DIFF_STATUS_MODIFIED) &&
+ strchr(filter, p->status)))
diff_q(&outq, p);
else
diff_free_filepair(p);