split tog's show_log_view into open/show/close view functions
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
diff --git a/tog/tog.c b/tog/tog.c
index eddecc7..aeac875 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -89,6 +89,17 @@ enum tog_view_type {
TOG_VIEW_BLAME
};
+struct commit_queue_entry {
+ TAILQ_ENTRY(commit_queue_entry) entry;
+ struct got_object_id *id;
+ struct got_commit_object *commit;
+};
+TAILQ_HEAD(commit_queue_head, commit_queue_entry);
+struct commit_queue {
+ int ncommits;
+ struct commit_queue_head head;
+};
+
struct tog_view {
WINDOW *window;
PANEL *panel;
@@ -104,17 +115,27 @@ struct tog_view {
int first_displayed_line;
int last_displayed_line;
} diff;
+ struct {
+ struct got_commit_graph *graph;
+ struct commit_queue commits;
+ struct commit_queue_entry *first_displayed_entry;
+ struct commit_queue_entry *last_displayed_entry;
+ struct commit_queue_entry *selected_entry;
+ int selected;
+ char *in_repo_path;
+ struct got_repository *repo;
+ } log;
} state;
};
-static const struct got_error *
-open_diff_view(struct tog_view *, struct got_object *, struct got_object *,
- struct got_repository *);
+static const struct got_error *open_diff_view(struct tog_view *,
+ struct got_object *, struct got_object *, struct got_repository *);
static const struct got_error *show_diff_view(struct tog_view *);
static void close_diff_view(struct tog_view *);
-static const struct got_error *
-show_log_view(struct tog_view *, struct got_object_id *,
- struct got_repository *, const char *);
+static const struct got_error *open_log_view(struct tog_view *,
+ struct got_object_id *, struct got_repository *, const char *);
+static const struct got_error * show_log_view(struct tog_view *);
+static void close_log_view(struct tog_view *);
static const struct got_error *
show_blame_view(struct tog_view *, const char *, struct got_object_id *,
struct got_repository *);
@@ -395,17 +416,6 @@ done:
return err;
}
-struct commit_queue_entry {
- TAILQ_ENTRY(commit_queue_entry) entry;
- struct got_object_id *id;
- struct got_commit_object *commit;
-};
-TAILQ_HEAD(commit_queue_head, commit_queue_entry);
-struct commit_queue {
- int ncommits;
- struct commit_queue_head head;
-};
-
static struct commit_queue_entry *
alloc_commit_queue_entry(struct got_commit_object *commit,
struct got_object_id *id)
@@ -818,20 +828,14 @@ done:
}
static const struct got_error *
-show_log_view(struct tog_view *view, struct got_object_id *start_id,
+open_log_view(struct tog_view *view, struct got_object_id *start_id,
struct got_repository *repo, const char *path)
{
const struct got_error *err = NULL;
struct got_object_id *head_id = NULL;
- int ch, done = 0, selected = 0, nfetched;
- struct got_commit_graph *graph = NULL;
- struct commit_queue commits;
- struct commit_queue_entry *first_displayed_entry = NULL;
- struct commit_queue_entry *last_displayed_entry = NULL;
- struct commit_queue_entry *selected_entry = NULL;
- char *in_repo_path = NULL;
-
- err = got_repo_map_path(&in_repo_path, repo, path);
+ int nfetched;
+
+ err = got_repo_map_path(&view->state.log.in_repo_path, repo, path);
if (err != NULL)
goto done;
@@ -840,16 +844,16 @@ show_log_view(struct tog_view *view, struct got_object_id *start_id,
return err;
/* The graph contains all commits. */
- err = got_commit_graph_open(&graph, head_id, 0, repo);
+ err = got_commit_graph_open(&view->state.log.graph, head_id, 0, repo);
if (err)
goto done;
/* The commit queue contains a subset of commits filtered by path. */
- TAILQ_INIT(&commits.head);
- commits.ncommits = 0;
+ TAILQ_INIT(&view->state.log.commits.head);
+ view->state.log.commits.ncommits = 0;
/* Populate commit graph with a sufficient number of commits. */
- err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
- repo);
+ err = got_commit_graph_fetch_commits_up_to(&nfetched,
+ view->state.log.graph, start_id, repo);
if (err)
goto done;
@@ -859,22 +863,46 @@ show_log_view(struct tog_view *view, struct got_object_id *start_id,
* in order to avoid having to re-fetch commits from disk while
* updating the display.
*/
- err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
- in_repo_path);
+ err = queue_commits(view->state.log.graph, &view->state.log.commits, start_id,
+ view->nlines, 1, repo, view->state.log.in_repo_path);
if (err) {
if (err->code != GOT_ERR_ITER_COMPLETED)
goto done;
err = NULL;
}
+ view->state.log.repo = repo;
+done:
+ free(head_id);
+ return err;
+}
+
+static void close_log_view(struct tog_view *view)
+{
+ if (view->state.log.graph)
+ got_commit_graph_close(view->state.log.graph);
+ free_commits(&view->state.log.commits);
+ free(view->state.log.in_repo_path);
+}
+
+static const struct got_error *
+show_log_view(struct tog_view *view)
+{
+ const struct got_error *err = NULL;
+ int ch, done = 0;
+
view_show(view);
- first_displayed_entry = TAILQ_FIRST(&commits.head);
- selected_entry = first_displayed_entry;
+ view->state.log.first_displayed_entry =
+ TAILQ_FIRST(&view->state.log.commits.head);
+ view->state.log.selected_entry = view->state.log.first_displayed_entry;
while (!done) {
- err = draw_commits(view, &last_displayed_entry, &selected_entry,
- first_displayed_entry, &commits, selected, view->nlines,
- graph, repo, in_repo_path);
+ err = draw_commits(view, &view->state.log.last_displayed_entry,
+ &view->state.log.selected_entry,
+ view->state.log.first_displayed_entry,
+ &view->state.log.commits, view->state.log.selected,
+ view->nlines, view->state.log.graph, view->state.log.repo,
+ view->state.log.in_repo_path);
if (err)
goto done;
@@ -889,31 +917,34 @@ show_log_view(struct tog_view *view, struct got_object_id *start_id,
break;
case 'k':
case KEY_UP:
- if (selected > 0)
- selected--;
- if (selected > 0)
+ if (view->state.log.selected > 0)
+ view->state.log.selected--;
+ if (view->state.log.selected > 0)
break;
- scroll_up(&first_displayed_entry, 1, &commits);
+ scroll_up(
+ &view->state.log.first_displayed_entry,
+ 1, &view->state.log.commits);
break;
case KEY_PPAGE:
- if (TAILQ_FIRST(&commits.head) ==
- first_displayed_entry) {
- selected = 0;
+ if (TAILQ_FIRST(&view->state.log.commits.head) ==
+ view->state.log.first_displayed_entry) {
+ view->state.log.selected = 0;
break;
}
- scroll_up(&first_displayed_entry, view->nlines,
- &commits);
+ scroll_up(&view->state.log.first_displayed_entry,
+ view->nlines, &view->state.log.commits);
break;
case 'j':
case KEY_DOWN:
- if (selected < MIN(view->nlines - 2,
- commits.ncommits - 1)) {
- selected++;
+ if (view->state.log.selected < MIN(view->nlines - 2,
+ view->state.log.commits.ncommits - 1)) {
+ view->state.log.selected++;
break;
}
- err = scroll_down(&first_displayed_entry, 1,
- last_displayed_entry, &commits, graph,
- repo, in_repo_path);
+ err = scroll_down(&view->state.log.first_displayed_entry, 1,
+ view->state.log.last_displayed_entry, &view->state.log.commits,
+ view->state.log.graph,
+ view->state.log.repo, view->state.log.in_repo_path);
if (err) {
if (err->code != GOT_ERR_ITER_COMPLETED)
goto done;
@@ -922,19 +953,19 @@ show_log_view(struct tog_view *view, struct got_object_id *start_id,
break;
case KEY_NPAGE: {
struct commit_queue_entry *first;
- first = first_displayed_entry;
- err = scroll_down(&first_displayed_entry,
- view->nlines, last_displayed_entry,
- &commits, graph, repo, in_repo_path);
+ first = view->state.log.first_displayed_entry;
+ err = scroll_down(&view->state.log.first_displayed_entry,
+ view->nlines, view->state.log.last_displayed_entry,
+ &view->state.log.commits, view->state.log.graph, view->state.log.repo, view->state.log.in_repo_path);
if (err) {
if (err->code != GOT_ERR_ITER_COMPLETED)
goto done;
- if (first == first_displayed_entry &&
- selected < MIN(view->nlines - 2,
- commits.ncommits - 1)) {
+ if (first == view->state.log.first_displayed_entry &&
+ view->state.log.selected < MIN(view->nlines - 2,
+ view->state.log.commits.ncommits - 1)) {
/* can't scroll further down */
- selected = MIN(view->nlines - 2,
- commits.ncommits - 1);
+ view->state.log.selected = MIN(view->nlines - 2,
+ view->state.log.commits.ncommits - 1);
}
err = NULL;
}
@@ -944,20 +975,20 @@ show_log_view(struct tog_view *view, struct got_object_id *start_id,
err = view_resize(view);
if (err)
goto done;
- if (selected > view->nlines - 2)
- selected = view->nlines - 2;
- if (selected > commits.ncommits - 1)
- selected = commits.ncommits - 1;
+ if (view->state.log.selected > view->nlines - 2)
+ view->state.log.selected = view->nlines - 2;
+ if (view->state.log.selected > view->state.log.commits.ncommits - 1)
+ view->state.log.selected = view->state.log.commits.ncommits - 1;
break;
case KEY_ENTER:
case '\r':
- err = show_commit(view, selected_entry, repo);
+ err = show_commit(view, view->state.log.selected_entry, view->state.log.repo);
if (err)
goto done;
view_show(view);
break;
case 't':
- err = browse_commit(view, selected_entry, repo);
+ err = browse_commit(view, view->state.log.selected_entry, view->state.log.repo);
if (err)
goto done;
view_show(view);
@@ -967,11 +998,6 @@ show_log_view(struct tog_view *view, struct got_object_id *start_id,
}
}
done:
- free(head_id);
- if (graph)
- got_commit_graph_close(graph);
- free_commits(&commits);
- free(in_repo_path);
return err;
}
@@ -1058,7 +1084,11 @@ cmd_log(int argc, char *argv[])
error = got_error_from_errno();
goto done;
}
- error = show_log_view(view, start_id, repo, path);
+ error = open_log_view(view, start_id, repo, path);
+ if (error)
+ goto done;
+ error = show_log_view(view);
+ close_log_view(view);
view_close(view);
done:
free(repo_path);
@@ -2332,7 +2362,12 @@ log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
if (err)
return err;
- err = show_log_view(view, commit_id, repo, path);
+ err = open_log_view(view, commit_id, repo, path);
+ if (err)
+ goto done;
+ err = show_log_view(view);
+ close_log_view(view);
+done:
free(path);
return err;
}