1#include "cache.h"2#include "diff.h"3#include "commit.h"4#include "log-tree.h"5#include "builtin.h"67static struct rev_info log_tree_opt;89static int diff_tree_commit_sha1(const unsigned char *sha1)10{11struct commit *commit = lookup_commit_reference(sha1);12if (!commit)13return -1;14return log_tree_commit(&log_tree_opt, commit);15}1617static int diff_tree_stdin(char *line)18{19int len = strlen(line);20unsigned char sha1[20];21struct commit *commit;2223if (!len || line[len-1] != '\n')24return -1;25line[len-1] = 0;26if (get_sha1_hex(line, sha1))27return -1;28commit = lookup_commit(sha1);29if (!commit || parse_commit(commit))30return -1;31if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {32/* Graft the fake parents locally to the commit */33int pos = 41;34struct commit_list **pptr, *parents;3536/* Free the real parent list */37for (parents = commit->parents; parents; ) {38struct commit_list *tmp = parents->next;39free(parents);40parents = tmp;41}42commit->parents = NULL;43pptr = &(commit->parents);44while (line[pos] && !get_sha1_hex(line + pos, sha1)) {45struct commit *parent = lookup_commit(sha1);46if (parent) {47pptr = &commit_list_insert(parent, pptr)->next;48}49pos += 41;50}51}52return log_tree_commit(&log_tree_opt, commit);53}5455static const char diff_tree_usage[] =56"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "57"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"58" -r diff recursively\n"59" --root include the initial commit as diff against /dev/null\n"60COMMON_DIFF_OPTIONS_HELP;6162int cmd_diff_tree(int argc, const char **argv, char **envp)63{64int nr_sha1;65char line[1000];66struct object *tree1, *tree2;67static struct rev_info *opt = &log_tree_opt;68int read_stdin = 0;6970git_config(git_diff_config);71nr_sha1 = 0;72init_revisions(opt);73opt->abbrev = 0;74opt->diff = 1;75argc = setup_revisions(argc, argv, opt, NULL);7677while (--argc > 0) {78const char *arg = *++argv;7980if (!strcmp(arg, "--stdin")) {81read_stdin = 1;82continue;83}84usage(diff_tree_usage);85}8687/*88* NOTE! We expect "a ^b" to be equal to "a..b", so we89* reverse the order of the objects if the second one90* is marked UNINTERESTING.91*/92nr_sha1 = opt->pending.nr;93switch (nr_sha1) {94case 0:95if (!read_stdin)96usage(diff_tree_usage);97break;98case 1:99tree1 = opt->pending.objects[0].item;100diff_tree_commit_sha1(tree1->sha1);101break;102case 2:103tree1 = opt->pending.objects[0].item;104tree2 = opt->pending.objects[1].item;105if (tree2->flags & UNINTERESTING) {106struct object *tmp = tree2;107tree2 = tree1;108tree1 = tmp;109}110diff_tree_sha1(tree1->sha1,111tree2->sha1,112"", &opt->diffopt);113log_tree_diff_flush(opt);114break;115}116117if (!read_stdin)118return 0;119120if (opt->diffopt.detect_rename)121opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |122DIFF_SETUP_USE_CACHE);123while (fgets(line, sizeof(line), stdin)) {124unsigned char sha1[20];125126if (get_sha1_hex(line, sha1)) {127fputs(line, stdout);128fflush(stdout);129}130else131diff_tree_stdin(line);132}133return 0;134}