git-apply: implement "diffstat" output

Hey, it's almost free by now, and it's a good way to see that
we parse the patches correctly.
This commit is contained in:
Linus Torvalds 2005-05-26 11:40:43 -07:00
parent 19c58fb84d
commit 3f40315aaa

92
apply.c
View file

@ -21,6 +21,13 @@
static int merge_patch = 1; static int merge_patch = 1;
static const char apply_usage[] = "git-apply <patch>"; static const char apply_usage[] = "git-apply <patch>";
/*
* For "diff-stat" like behaviour, we keep track of the biggest change
* we've seen, and the longest filename. That allows us to do simple
* scaling.
*/
static int max_change, max_len;
/* /*
* Various "current state", notably line numbers and what * Various "current state", notably line numbers and what
* file (and how) we're patching right now.. The "is_xxxx" * file (and how) we're patching right now.. The "is_xxxx"
@ -41,6 +48,7 @@ struct patch {
char *new_name, *old_name; char *new_name, *old_name;
unsigned int old_mode, new_mode; unsigned int old_mode, new_mode;
int is_rename, is_copy, is_new, is_delete; int is_rename, is_copy, is_new, is_delete;
int lines_added, lines_deleted;
struct fragment *fragments; struct fragment *fragments;
struct patch *next; struct patch *next;
}; };
@ -503,6 +511,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
*/ */
static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment) static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
{ {
int added, deleted;
int len = linelen(line, size), offset; int len = linelen(line, size), offset;
unsigned long pos[4], oldlines, newlines; unsigned long pos[4], oldlines, newlines;
@ -521,6 +530,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
line += len; line += len;
size -= len; size -= len;
linenr++; linenr++;
added = deleted = 0;
for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) { for (offset = len; size > 0; offset += len, size -= len, line += len, linenr++) {
if (!oldlines && !newlines) if (!oldlines && !newlines)
break; break;
@ -535,13 +545,17 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
newlines--; newlines--;
break; break;
case '-': case '-':
deleted++;
oldlines--; oldlines--;
break; break;
case '+': case '+':
added++;
newlines--; newlines--;
break; break;
} }
} }
patch->lines_added += added;
patch->lines_deleted += deleted;
return offset; return offset;
} }
@ -586,14 +600,52 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
return offset + hdrsize + patchsize; return offset + hdrsize + patchsize;
} }
const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
const char minuses[]= "----------------------------------------------------------------------";
static void show_stats(struct patch *patch)
{
char *name = patch->old_name;
int len, max, add, del;
if (!name)
name = patch->new_name;
/*
* "scale" the filename
*/
len = strlen(name);
max = max_len;
if (max > 50)
max = 50;
if (len > max)
name += len - max;
len = max;
/*
* scale the add/delete
*/
max = max_change;
if (max + len > 70)
max = 70 - len;
add = (patch->lines_added * max + max_change/2) / max_change;
del = (patch->lines_deleted * max + max_change/2) / max_change;
printf(" %-*s |%5d %.*s%.*s\n",
len, name, patch->lines_added + patch->lines_deleted,
add, pluses, del, minuses);
}
static void apply_patch_list(struct patch *patch) static void apply_patch_list(struct patch *patch)
{ {
int files, adds, dels;
files = adds = dels = 0;
if (!patch) if (!patch)
die("no patch found"); die("no patch found");
do { do {
const char *old_name = patch->old_name; const char *old_name = patch->old_name;
const char *new_name = patch->new_name; const char *new_name = patch->new_name;
struct fragment *frag;
if (old_name) { if (old_name) {
if (cache_name_pos(old_name, strlen(old_name)) < 0) if (cache_name_pos(old_name, strlen(old_name)) < 0)
@ -606,21 +658,30 @@ static void apply_patch_list(struct patch *patch)
die("file %s already exists", new_name); die("file %s already exists", new_name);
} }
printf("Applying patch to %s\n", new_name); files++;
printf(" new=%d delete=%d copy=%d rename=%d\n", adds += patch->lines_added;
patch->is_new, patch->is_delete, patch->is_copy, patch->is_rename); dels += patch->lines_deleted;
if (patch->old_mode != patch->new_mode) show_stats(patch);
printf(" %o->%o\n", patch->old_mode, patch->new_mode);
frag = patch->fragments;
while (frag) {
printf("Fragment %lu,%lu -> %lu,%lu\n%.*s",
frag->oldpos, frag->oldlines,
frag->newpos, frag->newlines,
frag->size, frag->patch);
frag = frag->next;
}
} while ((patch = patch->next) != NULL); } while ((patch = patch->next) != NULL);
printf(" %d files changed, %d insertions(+), %d deletions(-)\n", files, adds, dels);
}
static void patch_stats(struct patch *patch)
{
int lines = patch->lines_added + patch->lines_deleted;
if (lines > max_change)
max_change = lines;
if (patch->old_name) {
int len = strlen(patch->old_name);
if (len > max_len)
max_len = len;
}
if (patch->new_name) {
int len = strlen(patch->new_name);
if (len > max_len)
max_len = len;
}
} }
static int apply_patch(int fd) static int apply_patch(int fd)
@ -641,6 +702,7 @@ static int apply_patch(int fd)
nr = parse_chunk(buffer + offset, size, patch); nr = parse_chunk(buffer + offset, size, patch);
if (nr < 0) if (nr < 0)
break; break;
patch_stats(patch);
*listp = patch; *listp = patch;
listp = &patch->next; listp = &patch->next;
offset += nr; offset += nr;