Code

Handle blaming beyond the creation of file more gracefully
authorJeff King <peff@peff.net>
Sun, 8 Feb 2009 11:00:42 +0000 (06:00 -0500)
committerJonas Fonseca <fonseca@diku.dk>
Sun, 8 Feb 2009 12:30:21 +0000 (13:30 +0100)
Currently when you ask to re-blame from the parent of a
commit that created the file, blame_read_file will complain
that it cannot get the file contents ("No blame exist").

At the time we try to read the file, it is too late to abort
the operation, as we have already changed to the new blame
view. However, we can detect this situation early by
limiting the selection of the parent revision to the
particular path of interest: if it returns a parent even
with path-limiting, then we know the path exists; if not,
then we know it doesn't.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
tig.c

diff --git a/tig.c b/tig.c
index 04a44db37ec0d11dfe33e5bda4be1b83fb3da151..13c167d3c49f772d12dcd680eda2658c8d5181bb 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -3384,11 +3384,11 @@ select_commit_parent_handler(void *data, char *buf, int c)
 }
 
 static bool
-select_commit_parent(const char *id, char rev[SIZEOF_REV])
+select_commit_parent(const char *id, char rev[SIZEOF_REV], const char *path)
 {
        char buf[SIZEOF_STR * 4];
        const char *revlist_argv[] = {
-               "git", "rev-list", "-1", "--parents", id, NULL
+               "git", "rev-list", "-1", "--parents", id, "--", path, NULL
        };
        int parents;
 
@@ -3399,7 +3399,10 @@ select_commit_parent(const char *id, char rev[SIZEOF_REV])
                return FALSE;
 
        } else if (parents == 0) {
-               report("The selected commit has no parents");
+               if (path)
+                       report("Path '%s' does not exist in the parent", path);
+               else
+                       report("The selected commit has no parents");
                return FALSE;
        }
 
@@ -4468,7 +4471,8 @@ blame_request(struct view *view, enum request request, struct line *line)
 
        case REQ_PARENT:
                if (check_blame_commit(blame) &&
-                   select_commit_parent(blame->commit->id, opt_ref)) {
+                   select_commit_parent(blame->commit->id, opt_ref,
+                                        blame->commit->filename)) {
                        string_copy(opt_file, blame->commit->filename);
                        setup_blame_parent_line(view, blame);
                        open_view(view, REQ_VIEW_BLAME, OPEN_REFRESH);