+const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
+const char minuses[]= "----------------------------------------------------------------------";
+
+static void show_stats(struct patch *patch)
+{
+ char *name = patch->old_name;
+ int len, max, add, del, total;
+
+ 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;
+ del = patch->lines_deleted;
+ total = add + del;
+
+ total = (total * max + max_change / 2) / max_change;
+ add = (add * max + max_change / 2) / max_change;
+ del = total - add;
+ printf(" %-*s |%5d %.*s%.*s\n",
+ len, name, patch->lines_added + patch->lines_deleted,
+ add, pluses, del, minuses);
+}
+
+static int check_patch(struct patch *patch)
+{
+ struct stat st;
+ const char *old_name = patch->old_name;
+ const char *new_name = patch->new_name;
+
+ if (old_name) {
+ int pos = cache_name_pos(old_name, strlen(old_name));
+ int changed;
+
+ if (pos < 0)
+ return error("%s: does not exist in index", old_name);
+ if (patch->is_new < 0)
+ patch->is_new = 0;
+ if (lstat(old_name, &st) < 0)
+ return error("%s: %s\n", strerror(errno));
+ changed = ce_match_stat(active_cache[pos], &st);
+ if (changed)
+ return error("%s: does not match index", old_name);
+ if (!patch->old_mode)
+ patch->old_mode = st.st_mode;
+ }
+
+ if (new_name && (patch->is_new | patch->is_rename | patch->is_copy)) {
+ if (cache_name_pos(new_name, strlen(new_name)) >= 0)
+ return error("%s: already exists in index", new_name);
+ if (!lstat(new_name, &st))
+ return error("%s: already exists in working directory", new_name);
+ if (errno != ENOENT)
+ return error("%s: %s", new_name, strerror(errno));
+ }
+ return 0;
+}
+
+static int check_patch_list(struct patch *patch)
+{
+ int error = 0;
+
+ for (;patch ; patch = patch->next)
+ error |= check_patch(patch);
+ return error;
+}
+
+static void show_file(int c, unsigned int mode, const char *name)
+{
+ printf("%c %o %s\n", c, mode, name);
+}
+
+static void show_file_list(struct patch *patch)
+{
+ for (;patch ; patch = patch->next) {
+ if (patch->is_rename) {
+ show_file('-', patch->old_mode, patch->old_name);
+ show_file('+', patch->new_mode, patch->new_name);
+ continue;
+ }
+ if (patch->is_copy || patch->is_new) {
+ show_file('+', patch->new_mode, patch->new_name);
+ continue;
+ }
+ if (patch->is_delete) {
+ show_file('-', patch->old_mode, patch->old_name);
+ continue;
+ }
+ if (patch->old_mode && patch->new_mode && patch->old_mode != patch->new_mode) {
+ printf("M %o:%o %s\n", patch->old_mode, patch->new_mode, patch->old_name);
+ continue;
+ }
+ printf("M %o %s\n", patch->old_mode, patch->old_name);
+ }
+}
+
+static void stat_patch_list(struct patch *patch)
+{
+ int files, adds, dels;
+
+ for (files = adds = dels = 0 ; patch ; patch = patch->next) {
+ files++;
+ adds += patch->lines_added;
+ dels += patch->lines_deleted;
+ show_stats(patch);
+ }
+
+ 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;
+ }
+}
+