summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1c15afb)
raw | patch | inline | side by side (parent: 1c15afb)
author | Junio C Hamano <junkio@cox.net> | |
Wed, 14 Dec 2005 01:21:41 +0000 (17:21 -0800) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Tue, 20 Dec 2005 02:32:44 +0000 (18:32 -0800) |
When I show transcripts to explain how something works, I often
find myself hand-editing the diff-raw output to shorten various
object names in the output.
This adds --abbrev option to the diff family, which shortens
diff-raw output and diff-tree commit id headers.
Signed-off-by: Junio C Hamano <junkio@cox.net>
find myself hand-editing the diff-raw output to shorten various
object names in the output.
This adds --abbrev option to the diff family, which shortens
diff-raw output and diff-tree commit id headers.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Documentation/diff-options.txt | patch | blob | history | |
diff-tree.c | patch | blob | history | |
diff.c | patch | blob | history | |
diff.h | patch | blob | history | |
sha1_name.c | patch | blob | history |
index 6b496ede2550cf573a3a7ce37db908e6b3ab631e..3d1175e8646474ca35244a3d7e57cd02f18dc310 100644 (file)
object name of pre- and post-image blob on the "index"
line when generating a patch format output.
+--abbrev::
+ Instead of showing the full 40-byte hexadecimal object
+ name in diff-raw format output and diff-tree header
+ lines, show only handful prefix. This is independent of
+ --full-index option above, which controls the diff-patch
+ output format.
+
-B::
Break complete rewrite changes into pairs of delete and create.
diff --git a/diff-tree.c b/diff-tree.c
index d56d921585d5cd48b88bfbb36bcde016e10f8fd5..efa2b9476eae679b7f98a4605b07b6637e49de5a 100644 (file)
--- a/diff-tree.c
+++ b/diff-tree.c
static struct diff_options diff_options;
-static void call_diff_setup_done(void)
-{
- diff_setup_done(&diff_options);
-}
-
static int call_diff_flush(void)
{
diffcore_std(&diff_options);
{
int ret;
- call_diff_setup_done();
ret = diff_tree_sha1(old, new, base, &diff_options);
call_diff_flush();
return ret;
void *tree;
struct tree_desc empty, real;
- call_diff_setup_done();
tree = read_object_with_reference(new, "tree", &real.size, NULL);
if (!tree)
die("unable to read root tree (%s)", sha1_to_hex(new));
return retval;
}
-static const char *generate_header(const char *commit, const char *parent, const char *msg)
+static const char *generate_header(const unsigned char *commit_sha1,
+ const unsigned char *parent_sha1,
+ const char *msg)
{
static char this_header[16384];
int offset;
unsigned long len;
+ int abbrev = diff_options.abbrev;
if (!verbose_header)
- return commit;
+ return sha1_to_hex(commit_sha1);
len = strlen(msg);
- offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
- offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
+
+ offset = sprintf(this_header, "%s%s ",
+ header_prefix,
+ diff_unique_abbrev(commit_sha1, abbrev));
+ offset += sprintf(this_header + offset, "(from %s)\n",
+ parent_sha1 ?
+ diff_unique_abbrev(parent_sha1, abbrev) : "root");
+ offset += pretty_print_commit(commit_format, msg, len,
+ this_header + offset,
+ sizeof(this_header) - offset);
return this_header;
}
/* Root commit? */
if (show_root_diff && !commit->parents) {
- header = generate_header(name, "root", commit->buffer);
+ header = generate_header(sha1, NULL, commit->buffer);
diff_root_tree(commit_sha1, "");
}
/* More than one parent? */
if (ignore_merges && commit->parents && commit->parents->next)
- return 0;
+ return 0;
for (parents = commit->parents; parents; parents = parents->next) {
struct commit *parent = parents->item;
- header = generate_header(name,
- sha1_to_hex(parent->object.sha1),
+ header = generate_header(sha1,
+ parent->object.sha1,
commit->buffer);
diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
if (!header && verbose_header) {
int len = strlen(line);
unsigned char commit[20], parent[20];
static char this_header[1000];
+ int abbrev = diff_options.abbrev;
if (!len || line[len-1] != '\n')
return -1;
if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
line[40] = 0;
line[81] = 0;
- sprintf(this_header, "%s (from %s)\n", line, line+41);
+ sprintf(this_header, "%s (from %s)\n",
+ diff_unique_abbrev(commit, abbrev),
+ diff_unique_abbrev(parent, abbrev));
header = this_header;
return diff_tree_sha1_top(parent, commit, "");
}
diff_options.recursive = 1;
diff_tree_setup_paths(get_pathspec(prefix, argv));
+ diff_setup_done(&diff_options);
switch (nr_sha1) {
case 0:
index 2e0797bf3ee651a644be50aee10ee125fe96368f..c8159183dac830bf7cba20edf480712f039d7135 100644 (file)
--- a/diff.c
+++ b/diff.c
if (memcmp(one->sha1, two->sha1, 20)) {
char one_sha1[41];
- const char *index_fmt = o->full_index ? "index %s..%s" : "index %.7s..%.7s";
+ int abbrev = o->full_index ? 40 : DIFF_DEFAULT_INDEX_ABBREV;
memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
len += snprintf(msg + len, sizeof(msg) - len,
- index_fmt, one_sha1, sha1_to_hex(two->sha1));
+ "index %.*s..%.*s",
+ abbrev, one_sha1, abbrev,
+ sha1_to_hex(two->sha1));
if (one->mode == two->mode)
len += snprintf(msg + len, sizeof(msg) - len,
" %06o", one->mode);
}
if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
use_size_cache = 1;
+ if (options->abbrev <= 0 || 40 < options->abbrev)
+ options->abbrev = 40; /* full */
return 0;
}
}
else if (!strcmp(arg, "--find-copies-harder"))
options->find_copies_harder = 1;
+ else if (!strcmp(arg, "--abbrev"))
+ options->abbrev = DIFF_DEFAULT_ABBREV;
+ else if (!strncmp(arg, "--abbrev=", 9))
+ options->abbrev = strtoul(arg + 9, NULL, 10);
else
return 0;
return 1;
free(p);
}
+/* This is different from find_unique_abbrev() in that
+ * it needs to deal with 0{40} SHA1.
+ */
+const char *diff_unique_abbrev(const unsigned char *sha1, int len)
+{
+ int abblen;
+ const char *abbrev;
+ if (len == 40)
+ return sha1_to_hex(sha1);
+
+ abbrev = find_unique_abbrev(sha1, len);
+ if (!abbrev) {
+ if (!memcmp(sha1, null_sha1, 20)) {
+ char *buf = sha1_to_hex(null_sha1);
+ if (len < 37)
+ strcpy(buf + len, "...");
+ return buf;
+ }
+ else
+ return sha1_to_hex(sha1);
+ }
+ abblen = strlen(abbrev);
+ if (abblen < 37) {
+ static char hex[41];
+ if (len < abblen && abblen <= len + 2)
+ sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
+ else
+ sprintf(hex, "%s...", abbrev);
+ return hex;
+ }
+ return sha1_to_hex(sha1);
+}
+
static void diff_flush_raw(struct diff_filepair *p,
int line_termination,
int inter_name_termination,
- int output_format)
+ struct diff_options *options)
{
int two_paths;
char status[10];
+ int abbrev = options->abbrev;
const char *path_one, *path_two;
+ int output_format = options->output_format;
path_one = p->one->path;
path_two = p->two->path;
}
if (output_format != DIFF_FORMAT_NAME_STATUS) {
printf(":%06o %06o %s ",
- p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
- printf("%s ", sha1_to_hex(p->two->sha1));
+ p->one->mode, p->two->mode,
+ diff_unique_abbrev(p->one->sha1, abbrev));
+ printf("%s ",
+ diff_unique_abbrev(p->two->sha1, abbrev));
}
printf("%s%c%s", status, inter_name_termination, path_one);
if (two_paths)
case DIFF_FORMAT_NAME_STATUS:
diff_flush_raw(p, line_termination,
inter_name_termination,
- diff_output_format);
+ options);
break;
case DIFF_FORMAT_NAME:
diff_flush_name(p,
index 32b4780173d78bfd0e77a37f82df4a59aa502ec1..c3486ffa8618c1b2478c7c7197399bc47af0cbf0 100644 (file)
--- a/diff.h
+++ b/diff.h
int reverse_diff;
int rename_limit;
int setup;
+ int abbrev;
change_fn_t change;
add_remove_fn_t add_remove;
#define DIFF_PICKAXE_ALL 1
+#define DIFF_DEFAULT_INDEX_ABBREV 7 /* hex digits */
+#define DIFF_DEFAULT_ABBREV 7 /* hex digits */
+
extern void diffcore_std(struct diff_options *);
extern void diffcore_std_no_resolve(struct diff_options *);
" -u synonym for -p.\n" \
" --name-only show only names of changed files.\n" \
" --name-status show names and status of changed files.\n" \
-" --full-index show full object name on index ines.\n" \
+" --full-index show full object name on index lines.\n" \
+" --abbrev abbreviate object names in diff-tree header and diff-raw.\n" \
" -R swap input file pairs.\n" \
" -B detect complete rewrites.\n" \
" -M detect renames.\n" \
#define DIFF_STATUS_FILTER_AON '*'
#define DIFF_STATUS_FILTER_BROKEN 'B'
+extern const char *diff_unique_abbrev(const unsigned char *, int);
+
#endif /* DIFF_H */
diff --git a/sha1_name.c b/sha1_name.c
index 67b69a54fb730548f668d274ec58ff4ffc544f37..b13ed78cee0ee7f6d5ed3b45acee7b308a02c3f6 100644 (file)
--- a/sha1_name.c
+++ b/sha1_name.c
{
int status;
static char hex[41];
+
memcpy(hex, sha1_to_hex(sha1), 40);
+ if (len == 40)
+ return hex;
while (len < 40) {
unsigned char sha1_ret[20];
status = get_short_sha1(hex, len, sha1_ret, 1);