X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=diff.c;h=3dc0ca0a0334b2ed5492d67d759c4bffad5cd404;hb=9048fe1c50499d562dcf73094f173b12bdb084c4;hp=4a4b62191284a2289631c01b3a2885387f59a8c0;hpb=89ab859e9452cbc8f1f15afab52257e9b789c899;p=git.git diff --git a/diff.c b/diff.c index 4a4b6219..3dc0ca0a 100644 --- a/diff.c +++ b/diff.c @@ -19,6 +19,7 @@ static const char *external_diff(void) { static const char *external_diff_cmd = NULL; static int done_preparing = 0; + const char *env_diff_opts; if (done_preparing) return external_diff_cmd; @@ -31,21 +32,23 @@ static const char *external_diff(void) * * 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) @@ -132,8 +135,8 @@ static void builtin_diff(const char *name_a, 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]; @@ -211,14 +214,10 @@ struct diff_filespec *alloc_filespec(const char *path) { 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; } @@ -377,8 +376,10 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only) 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]; @@ -419,8 +420,7 @@ static void prep_temp_blob(struct diff_tempfile *temp, { 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) @@ -528,10 +528,12 @@ static void run_external_diff(const char *pgm, 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)) { @@ -572,7 +574,7 @@ static void run_external_diff(const char *pgm, * 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); @@ -699,7 +701,7 @@ static int parse_num(const char **cp_p) scale *= 10; num = num * 10 + ch - '0'; } - *cp++; + cp++; } *cp_p = cp; @@ -760,7 +762,8 @@ struct diff_filepair *diff_queue(struct diff_queue_struct *queue, dp->status = 0; dp->source_stays = 0; dp->broken_pair = 0; - diff_q(queue, dp); + if (queue) + diff_q(queue, dp); return dp; } @@ -768,6 +771,8 @@ void diff_free_filepair(struct diff_filepair *p) { diff_free_filespec_data(p->one); diff_free_filespec_data(p->two); + free(p->one); + free(p->two); free(p); } @@ -779,7 +784,8 @@ static void diff_flush_raw(struct diff_filepair *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); @@ -886,13 +892,13 @@ int diff_queue_is_empty(void) 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); } @@ -901,7 +907,7 @@ void diff_debug_filepair(const struct diff_filepair *p, int i) 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); } @@ -949,7 +955,9 @@ static void diff_resolve_rename_copy(void) } /* 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]; @@ -1007,9 +1015,8 @@ void diff_flush(int diff_output_style, int line_termination) 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; @@ -1145,29 +1152,6 @@ void diff_addremove(int addremove, unsigned mode, diff_queue(&diff_queued_diff, one, two); } -void diff_helper_input(unsigned old_mode, - unsigned new_mode, - const unsigned char *old_sha1, - const unsigned char *new_sha1, - const char *old_path, - int status, - int score, - const char *new_path) -{ - struct diff_filespec *one, *two; - struct diff_filepair *dp; - - one = alloc_filespec(old_path); - two = alloc_filespec(new_path); - if (old_mode) - fill_filespec(one, old_sha1, old_mode); - if (new_mode) - fill_filespec(two, new_sha1, new_mode); - dp = diff_queue(&diff_queued_diff, one, two); - dp->score = score * MAX_SCORE / 100; - dp->status = status; -} - void diff_change(unsigned old_mode, unsigned new_mode, const unsigned char *old_sha1, const unsigned char *new_sha1,