refactor tog blame diff display code; no functional change
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
diff --git a/tog/tog.c b/tog/tog.c
index 5574c90..b406ca3 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1254,6 +1254,44 @@ blame_thread(void *arg)
return (void *)err;
}
+
+static const struct got_error *
+open_blamed_commit_and_parent(struct got_object **pobj, struct got_object **obj,
+ struct tog_blame_line *lines, int first_displayed_line,
+ int selected_line, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct tog_blame_line *line;
+ struct got_commit_object *commit = NULL;
+ struct got_object_qid *pid;
+
+ *pobj = NULL;
+ *obj = NULL;
+
+ line = &lines[first_displayed_line - 1 + selected_line - 1];
+ if (!line->annotated || line->id == NULL)
+ return NULL;
+
+ err = got_object_open(obj, repo, line->id);
+ if (err)
+ goto done;
+
+ err = got_object_commit_open(&commit, repo, *obj);
+ if (err)
+ goto done;
+
+ pid = SIMPLEQ_FIRST(&commit->parent_ids);
+ if (pid) {
+ err = got_object_open(pobj, repo, pid->id);
+ if (err)
+ goto done;
+ }
+done:
+ if (commit)
+ got_object_commit_close(commit);
+ return err;
+}
+
static const struct got_error *
show_blame_view(const char *path, struct got_object_id *commit_id,
struct got_repository *repo)
@@ -1262,7 +1300,7 @@ show_blame_view(const char *path, struct got_object_id *commit_id,
int ch, done = 0, first_displayed_line = 1, last_displayed_line = LINES;
int selected_line = first_displayed_line;
int eof, i, blame_complete = 0;
- struct got_object *obj = NULL;
+ struct got_object *obj = NULL, *pobj = NULL;
struct got_blob_object *blob = NULL;
FILE *f = NULL;
size_t filesize, nlines = 0;
@@ -1271,22 +1309,16 @@ show_blame_view(const char *path, struct got_object_id *commit_id,
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct tog_blame_cb_args blame_cb_args;
struct tog_blame_thread_args blame_thread_args;
- struct got_object_id *selected_id;
- struct got_commit_object *commit = NULL;
- struct got_object *obj1 = NULL, *obj2 = NULL;
- struct got_object_qid *pid;
err = got_object_open_by_path(&obj, repo, commit_id, path);
if (err)
goto done;
if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
err = got_error(GOT_ERR_OBJ_TYPE);
- got_object_close(obj);
goto done;
}
err = got_object_blob_open(&blob, repo, obj, 8192);
- got_object_close(obj);
if (err)
goto done;
f = got_opentemp();
@@ -1396,36 +1428,20 @@ show_blame_view(const char *path, struct got_object_id *commit_id,
break;
case KEY_ENTER:
case '\r':
- if (!lines[first_displayed_line - 1 +
- selected_line - 1].annotated)
- break;
- selected_id = lines[first_displayed_line - 1 +
- selected_line - 1].id;
- if (selected_id == NULL)
- break;
- err = got_object_open(&obj2, repo, selected_id);
- if (err)
- goto done;
- err = got_object_commit_open(&commit, repo,
- obj2);
+ err = open_blamed_commit_and_parent(&pobj, &obj,
+ lines, first_displayed_line, selected_line,
+ repo);
if (err)
goto done;
- pid = SIMPLEQ_FIRST(&commit->parent_ids);
- if (pid) {
- err = got_object_open(&obj1, repo,
- pid->id);
- if (err)
- goto done;
- }
- got_object_commit_close(commit);
- commit = NULL;
- err = show_diff_view(obj1, obj2, repo);
- if (obj1) {
- got_object_close(obj1);
- obj1 = NULL;
+ if (pobj == NULL && obj == NULL)
+ break;
+ err = show_diff_view(pobj, obj, repo);
+ if (pobj) {
+ got_object_close(pobj);
+ pobj = NULL;
}
- got_object_close(obj2);
- obj2 = NULL;
+ got_object_close(obj);
+ obj = NULL;
show_panel(tog_blame_view.panel);
if (err)
goto done;
@@ -1460,12 +1476,10 @@ done:
}
if (blob)
got_object_blob_close(blob);
- if (obj1)
- got_object_close(obj1);
- if (obj2)
- got_object_close(obj2);
- if (commit)
- got_object_commit_close(commit);
+ if (pobj)
+ got_object_close(pobj);
+ if (obj)
+ got_object_close(obj);
if (f)
fclose(f);
for (i = 0; i < nlines; i++)