Merge branch 'bw/submodule-branch-dot-doc'
[gitweb.git] / builtin / blame.c
index 0e10e302fbad93ec9d49af8f14f02e99d3da7a3e..4ddfadb71f7ef93958e2f236e166212e8b6bda2b 100644 (file)
@@ -1941,7 +1941,7 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
        cp = nth_line(sb, ent->lno);
        for (cnt = 0; cnt < ent->num_lines; cnt++) {
                char ch;
-               int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? 40 : abbrev;
+               int length = (opt & OUTPUT_LONG_OBJECT_NAME) ? GIT_SHA1_HEXSZ : abbrev;
 
                if (suspect->commit->object.flags & UNINTERESTING) {
                        if (blank_boundary)
@@ -2111,7 +2111,7 @@ static void find_alignment(struct scoreboard *sb, int *option)
        unsigned largest_score = 0;
        struct blame_entry *e;
        int compute_auto_abbrev = (abbrev < 0);
-       int auto_abbrev = default_abbrev;
+       int auto_abbrev = DEFAULT_ABBREV;
 
        for (e = sb->ent; e; e = e->next) {
                struct origin *suspect = e->suspect;
@@ -2220,6 +2220,8 @@ static int git_blame_config(const char *var, const char *value, void *cb)
                return 0;
        }
 
+       if (git_diff_heuristic_config(var, value, cb) < 0)
+               return -1;
        if (userdiff_config(var, value) < 0)
                return -1;
 
@@ -2232,12 +2234,12 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
        int pos;
 
        for (parents = work_tree->parents; parents; parents = parents->next) {
-               const unsigned char *commit_sha1 = parents->item->object.oid.hash;
-               unsigned char blob_sha1[20];
+               const struct object_id *commit_oid = &parents->item->object.oid;
+               struct object_id blob_oid;
                unsigned mode;
 
-               if (!get_tree_entry(commit_sha1, path, blob_sha1, &mode) &&
-                   sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
+               if (!get_tree_entry(commit_oid->hash, path, blob_oid.hash, &mode) &&
+                   sha1_object_info(blob_oid.hash, NULL) == OBJ_BLOB)
                        return;
        }
 
@@ -2251,13 +2253,13 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
                die("no such path '%s' in HEAD", path);
 }
 
-static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)
+static struct commit_list **append_parent(struct commit_list **tail, const struct object_id *oid)
 {
        struct commit *parent;
 
-       parent = lookup_commit_reference(sha1);
+       parent = lookup_commit_reference(oid->hash);
        if (!parent)
-               die("no such commit %s", sha1_to_hex(sha1));
+               die("no such commit %s", oid_to_hex(oid));
        return &commit_list_insert(parent, tail)->next;
 }
 
@@ -2274,10 +2276,10 @@ static void append_merge_parents(struct commit_list **tail)
        }
 
        while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
-               unsigned char sha1[20];
-               if (line.len < 40 || get_sha1_hex(line.buf, sha1))
+               struct object_id oid;
+               if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
                        die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
-               tail = append_parent(tail, sha1);
+               tail = append_parent(tail, &oid);
        }
        close(merge_head);
        strbuf_release(&line);
@@ -2306,7 +2308,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        struct commit *commit;
        struct origin *origin;
        struct commit_list **parent_tail, *parent;
-       unsigned char head_sha1[20];
+       struct object_id head_oid;
        struct strbuf buf = STRBUF_INIT;
        const char *ident;
        time_t now;
@@ -2322,10 +2324,10 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
        commit->date = now;
        parent_tail = &commit->parents;
 
-       if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
+       if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL))
                die("no such ref: HEAD");
 
-       parent_tail = append_parent(parent_tail, head_sha1);
+       parent_tail = append_parent(parent_tail, &head_oid);
        append_merge_parents(parent_tail);
        verify_working_tree_path(commit, path);
 
@@ -2454,6 +2456,41 @@ static char *prepare_final(struct scoreboard *sb)
        return xstrdup_or_null(name);
 }
 
+static const char *dwim_reverse_initial(struct scoreboard *sb)
+{
+       /*
+        * DWIM "git blame --reverse ONE -- PATH" as
+        * "git blame --reverse ONE..HEAD -- PATH" but only do so
+        * when it makes sense.
+        */
+       struct object *obj;
+       struct commit *head_commit;
+       unsigned char head_sha1[20];
+
+       if (sb->revs->pending.nr != 1)
+               return NULL;
+
+       /* Is that sole rev a committish? */
+       obj = sb->revs->pending.objects[0].item;
+       obj = deref_tag(obj, NULL, 0);
+       if (obj->type != OBJ_COMMIT)
+               return NULL;
+
+       /* Do we have HEAD? */
+       if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL))
+               return NULL;
+       head_commit = lookup_commit_reference_gently(head_sha1, 1);
+       if (!head_commit)
+               return NULL;
+
+       /* Turn "ONE" into "ONE..HEAD" then */
+       obj->flags |= UNINTERESTING;
+       add_pending_object(sb->revs, &head_commit->object, "HEAD");
+
+       sb->final = (struct commit *)obj;
+       return sb->revs->pending.objects[0].name;
+}
+
 static char *prepare_initial(struct scoreboard *sb)
 {
        int i;
@@ -2472,14 +2509,17 @@ static char *prepare_initial(struct scoreboard *sb)
                if (obj->type != OBJ_COMMIT)
                        die("Non commit %s?", revs->pending.objects[i].name);
                if (sb->final)
-                       die("More than one commit to dig down to %s and %s?",
+                       die("More than one commit to dig up from, %s and %s?",
                            revs->pending.objects[i].name,
                            final_commit_name);
                sb->final = (struct commit *) obj;
                final_commit_name = revs->pending.objects[i].name;
        }
+
+       if (!final_commit_name)
+               final_commit_name = dwim_reverse_initial(sb);
        if (!final_commit_name)
-               die("No commit to dig down to?");
+               die("No commit to dig up from?");
        return xstrdup(final_commit_name);
 }
 
@@ -2550,6 +2590,15 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                OPT_BIT('s', NULL, &output_option, N_("Suppress author name and timestamp (Default: off)"), OUTPUT_NO_AUTHOR),
                OPT_BIT('e', "show-email", &output_option, N_("Show author email instead of name (Default: off)"), OUTPUT_SHOW_EMAIL),
                OPT_BIT('w', NULL, &xdl_opts, N_("Ignore whitespace differences"), XDF_IGNORE_WHITESPACE),
+
+               /*
+                * The following two options are parsed by parse_revision_opt()
+                * and are only included here to get included in the "-h"
+                * output:
+                */
+               { OPTION_LOWLEVEL_CALLBACK, 0, "indent-heuristic", NULL, NULL, N_("Use an experimental indent-based heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
+               { OPTION_LOWLEVEL_CALLBACK, 0, "compaction-heuristic", NULL, NULL, N_("Use an experimental blank-line-based heuristic to improve diffs"), PARSE_OPT_NOARG, parse_opt_unknown_cb },
+
                OPT_BIT(0, "minimal", &xdl_opts, N_("Spend extra cycles to find better match"), XDF_NEED_MINIMAL),
                OPT_STRING('S', NULL, &revs_file, N_("file"), N_("Use revisions from <file> instead of calling git-rev-list")),
                OPT_STRING(0, "contents", &contents_from, N_("file"), N_("Use <file>'s contents as the final image")),
@@ -2596,12 +2645,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        }
 parse_done:
        no_whole_file_rename = !DIFF_OPT_TST(&revs.diffopt, FOLLOW_RENAMES);
+       xdl_opts |= revs.diffopt.xdl_opts & (XDF_COMPACTION_HEURISTIC | XDF_INDENT_HEURISTIC);
        DIFF_OPT_CLR(&revs.diffopt, FOLLOW_RENAMES);
        argc = parse_options_end(&ctx);
 
        if (incremental || (output_option & OUTPUT_PORCELAIN)) {
                if (show_progress > 0)
-                       die("--progress can't be used with --incremental or porcelain formats");
+                       die(_("--progress can't be used with --incremental or porcelain formats"));
                show_progress = 0;
        } else if (show_progress < 0)
                show_progress = isatty(2);
@@ -2727,7 +2777,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                sb.commits.compare = compare_commits_by_commit_date;
        }
        else if (contents_from)
-               die("--contents and --reverse do not blend well.");
+               die(_("--contents and --reverse do not blend well."));
        else {
                final_commit_name = prepare_initial(&sb);
                sb.commits.compare = compare_commits_by_reverse_commit_date;
@@ -2747,12 +2797,12 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                add_pending_object(&revs, &(sb.final->object), ":");
        }
        else if (contents_from)
-               die("Cannot use --contents with final commit object name");
+               die(_("cannot use --contents with final commit object name"));
 
        if (reverse && revs.first_parent_only) {
                final_commit = find_single_final(sb.revs, NULL);
                if (!final_commit)
-                       die("--reverse and --first-parent together require specified latest commit");
+                       die(_("--reverse and --first-parent together require specified latest commit"));
        }
 
        /*
@@ -2779,7 +2829,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                }
 
                if (oidcmp(&c->object.oid, &sb.final->object.oid))
-                       die("--reverse --first-parent together require range along first-parent chain");
+                       die(_("--reverse --first-parent together require range along first-parent chain"));
        }
 
        if (is_null_oid(&sb.final->object.oid)) {
@@ -2790,7 +2840,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
        else {
                o = get_origin(&sb, sb.final, path);
                if (fill_blob_sha1_and_mode(o))
-                       die("no such path %s in %s", path, final_commit_name);
+                       die(_("no such path %s in %s"), path, final_commit_name);
 
                if (DIFF_OPT_TST(&sb.revs->diffopt, ALLOW_TEXTCONV) &&
                    textconv_object(path, o->mode, &o->blob_oid, 1, (char **) &sb.final_buf,
@@ -2801,7 +2851,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                                                      &sb.final_buf_size);
 
                if (!sb.final_buf)
-                       die("Cannot read blob %s for path %s",
+                       die(_("cannot read blob %s for path %s"),
                            oid_to_hex(&o->blob_oid),
                            path);
        }
@@ -2820,7 +2870,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
                                    &bottom, &top, sb.path))
                        usage(blame_usage);
                if (lno < top || ((lno || bottom) && lno < bottom))
-                       die("file %s has only %lu lines", path, lno);
+                       die(Q_("file %s has only %lu line",
+                              "file %s has only %lu lines",
+                              lno), path, lno);
                if (bottom < 1)
                        bottom = 1;
                if (top < 1)