static int builtin_diff_b_f(struct rev_info *revs, int argc, const char **argv, struct blobinfo *blob, const char *path) { /* Blob vs file in the working tree*/ struct stat st; if (argc > 1) usage(builtin_diff_usage); if (lstat(path, &st)) die_errno("failed to stat '%s'", path); if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) die("'%s': not a regular file or symlink", path); diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/"); if (blob[0].mode == S_IFINVALID) blob[0].mode = canon_mode(st.st_mode); stuff_change(&revs->diffopt, blob[0].mode, canon_mode(st.st_mode), blob[0].sha1, null_sha1, path, path); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; }
static int builtin_diff_b_f(struct rev_info *revs, int argc, const char **argv, struct blobinfo *blob) { /* Blob vs file in the working tree*/ struct stat st; const char *path; if (argc > 1) usage(builtin_diff_usage); GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL); path = revs->prune_data.items[0].match; if (lstat(path, &st)) die_errno(_("failed to stat '%s'"), path); if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) die(_("'%s': not a regular file or symlink"), path); diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/"); if (blob[0].mode == S_IFINVALID) blob[0].mode = canon_mode(st.st_mode); stuff_change(&revs->diffopt, blob[0].mode, canon_mode(st.st_mode), blob[0].sha1, null_sha1, 1, 0, path, path); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; }
static int builtin_diff_blobs(struct rev_info *revs, int argc, const char **argv, struct blobinfo *blob) { unsigned mode = canon_mode(S_IFREG | 0644); if (argc > 1) usage(builtin_diff_usage); if (blob[0].mode == S_IFINVALID) blob[0].mode = mode; if (blob[1].mode == S_IFINVALID) blob[1].mode = mode; stuff_change(&revs->diffopt, blob[0].mode, blob[1].mode, blob[0].sha1, blob[1].sha1, blob[0].name, blob[1].name); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; }
static int builtin_diff_blobs(struct rev_info *revs, int argc, const char **argv, struct object_array_entry **blob) { unsigned mode = canon_mode(S_IFREG | 0644); if (argc > 1) usage(builtin_diff_usage); if (blob[0]->mode == S_IFINVALID) blob[0]->mode = mode; if (blob[1]->mode == S_IFINVALID) blob[1]->mode = mode; stuff_change(&revs->diffopt, blob[0]->mode, blob[1]->mode, &blob[0]->item->oid, &blob[1]->item->oid, 1, 1, blob_path(blob[0]), blob_path(blob[1])); diffcore_std(&revs->diffopt); diff_flush(&revs->diffopt); return 0; }
static void show_patch_diff(struct combine_diff_path *elem, int num_parent, int dense, struct rev_info *rev) { struct diff_options *opt = &rev->diffopt; unsigned long result_size, cnt, lno; char *result, *cp; struct sline *sline; /* survived lines */ int mode_differs = 0; int i, show_hunks; int working_tree_file = is_null_sha1(elem->sha1); int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV; const char *a_prefix, *b_prefix; mmfile_t result_file; context = opt->context; a_prefix = opt->a_prefix ? opt->a_prefix : "a/"; b_prefix = opt->b_prefix ? opt->b_prefix : "b/"; /* Read the result of merge first */ if (!working_tree_file) result = grab_blob(elem->sha1, &result_size); else { /* Used by diff-tree to read from the working tree */ struct stat st; int fd = -1; if (lstat(elem->path, &st) < 0) goto deleted_file; if (S_ISLNK(st.st_mode)) { size_t len = xsize_t(st.st_size); result_size = len; result = xmalloc(len + 1); if (result_size != readlink(elem->path, result, len)) { error("readlink(%s): %s", elem->path, strerror(errno)); return; } result[len] = 0; elem->mode = canon_mode(st.st_mode); } else if (0 <= (fd = open(elem->path, O_RDONLY)) && !fstat(fd, &st)) { size_t len = xsize_t(st.st_size); ssize_t done; int is_file, i; elem->mode = canon_mode(st.st_mode); /* if symlinks don't work, assume symlink if all parents * are symlinks */ is_file = has_symlinks; for (i = 0; !is_file && i < num_parent; i++) is_file = !S_ISLNK(elem->parent[i].mode); if (!is_file) elem->mode = canon_mode(S_IFLNK); result_size = len; result = xmalloc(len + 1); done = read_in_full(fd, result, len); if (done < 0) die("read error '%s'", elem->path); else if (done < len) die("early EOF '%s'", elem->path); result[len] = 0; /* If not a fake symlink, apply filters, e.g. autocrlf */ if (is_file) { struct strbuf buf = STRBUF_INIT; if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) { free(result); result = strbuf_detach(&buf, &len); result_size = len; } } } else { deleted_file: result_size = 0; elem->mode = 0; result = xcalloc(1, 1); } if (0 <= fd) close(fd); } for (cnt = 0, cp = result; cp < result + result_size; cp++) { if (*cp == '\n') cnt++; } if (result_size && result[result_size-1] != '\n') cnt++; /* incomplete line */ sline = xcalloc(cnt+2, sizeof(*sline)); sline[0].bol = result; for (lno = 0; lno <= cnt + 1; lno++) { sline[lno].lost_tail = &sline[lno].lost_head; sline[lno].flag = 0; } for (lno = 0, cp = result; cp < result + result_size; cp++) { if (*cp == '\n') { sline[lno].len = cp - sline[lno].bol; lno++; if (lno < cnt) sline[lno].bol = cp + 1; } } if (result_size && result[result_size-1] != '\n') sline[cnt-1].len = result_size - (sline[cnt-1].bol - result); result_file.ptr = result; result_file.size = result_size; /* Even p_lno[cnt+1] is valid -- that is for the end line number * for deletion hunk at the end. */ sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long)); for (lno = 0; lno <= cnt; lno++) sline[lno+1].p_lno = sline[lno].p_lno + num_parent; for (i = 0; i < num_parent; i++) { int j; for (j = 0; j < i; j++) { if (!hashcmp(elem->parent[i].sha1, elem->parent[j].sha1)) { reuse_combine_diff(sline, cnt, i, j); break; } } if (i <= j) combine_diff(elem->parent[i].sha1, &result_file, sline, cnt, i, num_parent); if (elem->parent[i].mode != elem->mode) mode_differs = 1; } show_hunks = make_hunks(sline, cnt, num_parent, dense); if (show_hunks || mode_differs || working_tree_file) { const char *abb; int use_color = DIFF_OPT_TST(opt, COLOR_DIFF); const char *c_meta = diff_get_color(use_color, DIFF_METAINFO); const char *c_reset = diff_get_color(use_color, DIFF_RESET); int added = 0; int deleted = 0; if (rev->loginfo && !rev->no_commit_id) show_log(rev); dump_quoted_path(dense ? "diff --cc " : "diff --combined ", "", elem->path, c_meta, c_reset); printf("%sindex ", c_meta); for (i = 0; i < num_parent; i++) { abb = find_unique_abbrev(elem->parent[i].sha1, abbrev); printf("%s%s", i ? "," : "", abb); } abb = find_unique_abbrev(elem->sha1, abbrev); printf("..%s%s\n", abb, c_reset); if (mode_differs) { deleted = !elem->mode; /* We say it was added if nobody had it */ added = !deleted; for (i = 0; added && i < num_parent; i++) if (elem->parent[i].status != DIFF_STATUS_ADDED) added = 0; if (added) printf("%snew file mode %06o", c_meta, elem->mode); else { if (deleted) printf("%sdeleted file ", c_meta); printf("mode "); for (i = 0; i < num_parent; i++) { printf("%s%06o", i ? "," : "", elem->parent[i].mode); } if (elem->mode) printf("..%06o", elem->mode); } printf("%s\n", c_reset); } if (added) dump_quoted_path("--- ", "", "/dev/null", c_meta, c_reset); else dump_quoted_path("--- ", a_prefix, elem->path, c_meta, c_reset); if (deleted) dump_quoted_path("+++ ", "", "/dev/null", c_meta, c_reset); else dump_quoted_path("+++ ", b_prefix, elem->path, c_meta, c_reset); dump_sline(sline, cnt, num_parent, DIFF_OPT_TST(opt, COLOR_DIFF)); } free(result); for (lno = 0; lno < cnt; lno++) { if (sline[lno].lost_head) { struct lline *ll = sline[lno].lost_head; while (ll) { struct lline *tmp = ll; ll = ll->next; free(tmp); } } } free(sline[0].p_lno); free(sline); }
/* * Prepare a dummy commit that represents the work tree (or staged) item. * Note that annotating work tree item never works in the reverse. */ static struct commit *fake_working_tree_commit(struct diff_options *opt, const char *path, const char *contents_from) { struct commit *commit; struct blame_origin *origin; struct commit_list **parent_tail, *parent; struct object_id head_oid; struct strbuf buf = STRBUF_INIT; const char *ident; time_t now; int size, len; struct cache_entry *ce; unsigned mode; struct strbuf msg = STRBUF_INIT; read_cache(); time(&now); commit = alloc_commit_node(); commit->object.parsed = 1; commit->date = now; parent_tail = &commit->parents; if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL)) die("no such ref: HEAD"); parent_tail = append_parent(parent_tail, &head_oid); append_merge_parents(parent_tail); verify_working_tree_path(commit, path); origin = make_origin(commit, path); ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0); strbuf_addstr(&msg, "tree 0000000000000000000000000000000000000000\n"); for (parent = commit->parents; parent; parent = parent->next) strbuf_addf(&msg, "parent %s\n", oid_to_hex(&parent->item->object.oid)); strbuf_addf(&msg, "author %s\n" "committer %s\n\n" "Version of %s from %s\n", ident, ident, path, (!contents_from ? path : (!strcmp(contents_from, "-") ? "standard input" : contents_from))); set_commit_buffer_from_strbuf(commit, &msg); if (!contents_from || strcmp("-", contents_from)) { struct stat st; const char *read_from; char *buf_ptr; unsigned long buf_len; if (contents_from) { if (stat(contents_from, &st) < 0) die_errno("Cannot stat '%s'", contents_from); read_from = contents_from; } else { if (lstat(path, &st) < 0) die_errno("Cannot lstat '%s'", path); read_from = path; } mode = canon_mode(st.st_mode); switch (st.st_mode & S_IFMT) { case S_IFREG: if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) && textconv_object(read_from, mode, &null_oid, 0, &buf_ptr, &buf_len)) strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1); else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) die_errno("cannot open or read '%s'", read_from); break; case S_IFLNK: if (strbuf_readlink(&buf, read_from, st.st_size) < 0) die_errno("cannot readlink '%s'", read_from); break; default: die("unsupported file type %s", read_from); } } else { /* Reading from stdin */ mode = 0; if (strbuf_read(&buf, 0, 0) < 0) die_errno("failed to read from stdin"); } convert_to_git(path, buf.buf, buf.len, &buf, 0); origin->file.ptr = buf.buf; origin->file.size = buf.len; pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_oid.hash); /* * Read the current index, replace the path entry with * origin->blob_sha1 without mucking with its mode or type * bits; we are not going to write this index out -- we just * want to run "diff-index --cached". */ discard_cache(); read_cache(); len = strlen(path); if (!mode) { int pos = cache_name_pos(path, len); if (0 <= pos) mode = active_cache[pos]->ce_mode; else /* Let's not bother reading from HEAD tree */ mode = S_IFREG | 0644; } size = cache_entry_size(len); ce = xcalloc(1, size); oidcpy(&ce->oid, &origin->blob_oid); memcpy(ce->name, path, len); ce->ce_flags = create_ce_flags(0); ce->ce_namelen = len; ce->ce_mode = create_ce_mode(mode); add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); cache_tree_invalidate_path(&the_index, path); return commit; }