@matheusbsbs / matheus-bagliano / commits / 4bbd261bbd
Fork 0

Add "--summary" option to git diff.

Remove the need to pipe git diff through git apply to get the extended headers summary. Signed-off-by: Sean Estabrooks <seanlkml@sympatico.ca> Signed-off-by: Junio C Hamano <junkio@cox.net>

Sean committed May 14, 2006 at 08:13 UTC 4bbd261bbd941066735e41dff0ade9f50e6fe84d
3 files changed +93 -2
Documentation/diff-options.txt
+4
@@ -10,6 +10,10 @@
10 --stat::
11 Generate a diffstat instead of a patch.
12
13 +--summary::
14 + Output a condensed summary of extended header information
15 + such as creations, renames and mode changes.
16 +
17 --patch-with-stat::
18 Generate patch and prepend its diffstat.
19
diff.c
+87 -1
@@ -1233,6 +1233,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1233 }
1234 else if (!strcmp(arg, "--stat"))
1235 options->output_format = DIFF_FORMAT_DIFFSTAT;
1236 + else if (!strcmp(arg, "--summary"))
1237 + options->summary = 1;
1238 else if (!strcmp(arg, "--patch-with-stat")) {
1239 options->output_format = DIFF_FORMAT_PATCH;
1240 options->with_stat = 1;
@@ -1703,6 +1705,85 @@ static void flush_one_pair(struct diff_filepair *p,
1705 }
1706 }
1707
1708 +static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
1709 +{
1710 + if (fs->mode)
1711 + printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
1712 + else
1713 + printf(" %s %s\n", newdelete, fs->path);
1714 +}
1715 +
1716 +
1717 +static void show_mode_change(struct diff_filepair *p, int show_name)
1718 +{
1719 + if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
1720 + if (show_name)
1721 + printf(" mode change %06o => %06o %s\n",
1722 + p->one->mode, p->two->mode, p->two->path);
1723 + else
1724 + printf(" mode change %06o => %06o\n",
1725 + p->one->mode, p->two->mode);
1726 + }
1727 +}
1728 +
1729 +static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
1730 +{
1731 + const char *old, *new;
1732 +
1733 + /* Find common prefix */
1734 + old = p->one->path;
1735 + new = p->two->path;
1736 + while (1) {
1737 + const char *slash_old, *slash_new;
1738 + slash_old = strchr(old, '/');
1739 + slash_new = strchr(new, '/');
1740 + if (!slash_old ||
1741 + !slash_new ||
1742 + slash_old - old != slash_new - new ||
1743 + memcmp(old, new, slash_new - new))
1744 + break;
1745 + old = slash_old + 1;
1746 + new = slash_new + 1;
1747 + }
1748 + /* p->one->path thru old is the common prefix, and old and new
1749 + * through the end of names are renames
1750 + */
1751 + if (old != p->one->path)
1752 + printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
1753 + (int)(old - p->one->path), p->one->path,
1754 + old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
1755 + else
1756 + printf(" %s %s => %s (%d%%)\n", renamecopy,
1757 + p->one->path, p->two->path,
1758 + (int)(0.5 + p->score * 100.0/MAX_SCORE));
1759 + show_mode_change(p, 0);
1760 +}
1761 +
1762 +static void diff_summary(struct diff_filepair *p)
1763 +{
1764 + switch(p->status) {
1765 + case DIFF_STATUS_DELETED:
1766 + show_file_mode_name("delete", p->one);
1767 + break;
1768 + case DIFF_STATUS_ADDED:
1769 + show_file_mode_name("create", p->two);
1770 + break;
1771 + case DIFF_STATUS_COPIED:
1772 + show_rename_copy("copy", p);
1773 + break;
1774 + case DIFF_STATUS_RENAMED:
1775 + show_rename_copy("rename", p);
1776 + break;
1777 + default:
1778 + if (p->score) {
1779 + printf(" rewrite %s (%d%%)\n", p->two->path,
1780 + (int)(0.5 + p->score * 100.0/MAX_SCORE));
1781 + show_mode_change(p, 0);
1782 + } else show_mode_change(p, 1);
1783 + break;
1784 + }
1785 +}
1786 +
1787 void diff_flush(struct diff_options *options)
1788 {
1789 struct diff_queue_struct *q = &diff_queued_diff;
@@ -1736,7 +1817,6 @@ void diff_flush(struct diff_options *options)
1817 for (i = 0; i < q->nr; i++) {
1818 struct diff_filepair *p = q->queue[i];
1819 flush_one_pair(p, diff_output_format, options, diffstat);
1739 - diff_free_filepair(p);
1820 }
1821
1822 if (diffstat) {
@@ -1744,6 +1824,12 @@ void diff_flush(struct diff_options *options)
1824 free(diffstat);
1825 }
1826
1827 + for (i = 0; i < q->nr; i++) {
1828 + if (options->summary)
1829 + diff_summary(q->queue[i]);
1830 + diff_free_filepair(q->queue[i]);
1831 + }
1832 +
1833 free(q->queue);
1834 q->queue = NULL;
1835 q->nr = q->alloc = 0;
diff.h
+2 -1
@@ -31,7 +31,8 @@ struct diff_options {
31 binary:1,
32 full_index:1,
33 silent_on_remove:1,
34 - find_copies_harder:1;
34 + find_copies_harder:1,
35 + summary:1;
36 int break_opt;
37 int detect_rename;
38 int line_termination;