in tog, allow moving between commit diffs with a single key press
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
diff --git a/TODO b/TODO
index 5278623..bcece92 100644
--- a/TODO
+++ b/TODO
@@ -7,5 +7,4 @@ tog:
- make diff view generate diffs in a background thread
- implement horizonal scrolling in all views
- implement horizonal split view mode
-- make it possible to move between commit diffs with a single key press
- implement search feature
diff --git a/tog/tog.1 b/tog/tog.1
index 2716958..1ab6e36 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -135,6 +135,11 @@ Scroll up.
Reduce the amount of diff context lines.
.It Cm ]
Increase the amount of diff context lines.
+.It Cm <, Comma
+If the diff view was opened via the log view, move to the previous (younger)
+commit.
+.It Cm >, Full stop
+If the diff view was opened via the log view, move to the next (older) commit.
.El
.It Cm blame [ Fl c Ar commit ] [ Fl r Ar repository-path ] Ar path
Display line-by-line history of a file at the specified path.
diff --git a/tog/tog.c b/tog/tog.c
index dfcd757..336187b 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -98,16 +98,6 @@ enum tog_view_type {
TOG_VIEW_TREE
};
-struct tog_diff_view_state {
- struct got_object_id *id1, *id2;
- FILE *f;
- int first_displayed_line;
- int last_displayed_line;
- int eof;
- int diff_context;
- struct got_repository *repo;
-};
-
struct commit_queue_entry {
TAILQ_ENTRY(commit_queue_entry) entry;
struct got_object_id *id;
@@ -120,6 +110,23 @@ struct commit_queue {
struct commit_queue_head head;
};
+struct tog_diff_view_state {
+ struct got_object_id *id1, *id2;
+ FILE *f;
+ int first_displayed_line;
+ int last_displayed_line;
+ int eof;
+ int diff_context;
+ struct got_repository *repo;
+
+ /* passed from log view; may be NULL */
+ struct commit_queue_entry *selected_entry;
+ struct commit_queue *commits;
+ int *log_complete;
+ pthread_cond_t *need_commits;
+ int *commits_needed;
+};
+
pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
struct tog_log_thread_args {
@@ -263,7 +270,9 @@ struct tog_view {
};
static const struct got_error *open_diff_view(struct tog_view *,
- struct got_object_id *, struct got_object_id *, struct got_repository *);
+ struct got_object_id *, struct got_object_id *, struct commit_queue_entry *,
+ struct commit_queue *, int *, pthread_cond_t *, int *,
+ struct got_repository *);
static const struct got_error *show_diff_view(struct tog_view *);
static const struct got_error *input_diff_view(struct tog_view **,
struct tog_view **, struct tog_view **, struct tog_view *, int);
@@ -1223,12 +1232,16 @@ scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
static const struct got_error *
open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
- struct got_object_id *commit_id, struct got_commit_object *commit,
+ struct commit_queue_entry *selected_entry,
+ struct commit_queue *commits, int *log_complete,
+ pthread_cond_t *need_commits, int *commits_needed,
struct got_repository *repo)
{
const struct got_error *err;
struct got_object_qid *parent_id;
struct tog_view *diff_view;
+ struct got_commit_object *commit = selected_entry->commit;
+ struct got_object_id *commit_id = selected_entry->id;
diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
if (diff_view == NULL)
@@ -1236,7 +1249,8 @@ open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
- commit_id, repo);
+ commit_id, selected_entry, commits, log_complete, need_commits,
+ commits_needed, repo);
if (err == NULL)
*new_view = diff_view;
return err;
@@ -1538,8 +1552,10 @@ input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
if (view_is_parent_view(view))
begin_x = view_split_begin_x(view->begin_x);
err = open_diff_view_for_commit(&diff_view, begin_x,
- s->selected_entry->id, s->selected_entry->commit,
- s->repo);
+ s->selected_entry, &s->commits,
+ &s->thread_args.log_complete,
+ &s->thread_args.need_commits,
+ &s->thread_args.commits_needed, s->repo);
if (err)
break;
if (view_is_parent_view(view)) {
@@ -1950,12 +1966,16 @@ create_diff(struct tog_diff_view_state *s)
err = got_object_open_as_commit(&commit2, s->repo, s->id2);
if (err)
break;
- /* Show commit info if we're diffing to a parent commit. */
- parent_ids = got_object_commit_get_parent_ids(commit2);
- SIMPLEQ_FOREACH(pid, parent_ids, entry) {
- if (got_object_id_cmp(s->id1, pid->id) == 0) {
- write_commit_info(s->id2, s->repo, f);
- break;
+ /* Show commit info if we're diffing to a parent/root commit. */
+ if (s->id1 == NULL)
+ write_commit_info(s->id2, s->repo, f);
+ else {
+ parent_ids = got_object_commit_get_parent_ids(commit2);
+ SIMPLEQ_FOREACH(pid, parent_ids, entry) {
+ if (got_object_id_cmp(s->id1, pid->id) == 0) {
+ write_commit_info(s->id2, s->repo, f);
+ break;
+ }
}
}
got_object_commit_close(commit2);
@@ -1976,7 +1996,10 @@ done:
static const struct got_error *
open_diff_view(struct tog_view *view, struct got_object_id *id1,
- struct got_object_id *id2, struct got_repository *repo)
+ struct got_object_id *id2, struct commit_queue_entry *selected_entry,
+ struct commit_queue *commits, int *log_complete,
+ pthread_cond_t *need_commits, int *commits_needed,
+ struct got_repository *repo)
{
const struct got_error *err;
@@ -2010,6 +2033,11 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
view->state.diff.first_displayed_line = 1;
view->state.diff.last_displayed_line = view->nlines;
view->state.diff.diff_context = 3;
+ view->state.diff.selected_entry = selected_entry;
+ view->state.diff.commits = commits;
+ view->state.diff.log_complete = log_complete;
+ view->state.diff.need_commits = need_commits;
+ view->state.diff.commits_needed = commits_needed;
view->state.diff.repo = repo;
err = create_diff(&view->state.diff);
@@ -2074,11 +2102,36 @@ show_diff_view(struct tog_view *view)
}
static const struct got_error *
+set_selected_commit(struct tog_diff_view_state *s,
+ struct commit_queue_entry *entry)
+{
+ struct commit_queue_entry *pentry;
+
+ free(s->id2);
+ s->id2 = got_object_id_dup(entry->id);
+ if (s->id2 == NULL)
+ return got_error_from_errno();
+
+ free(s->id1);
+ s->id1 = NULL;
+ pentry = TAILQ_NEXT(entry, entry);
+ if (pentry) {
+ s->id1 = got_object_id_dup(pentry->id);
+ if (s->id1 == NULL)
+ return got_error_from_errno();
+ }
+
+ s->selected_entry = entry;
+ return NULL;
+}
+
+static const struct got_error *
input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
struct tog_view **focus_view, struct tog_view *view, int ch)
{
const struct got_error *err = NULL;
struct tog_diff_view_state *s = &view->state.diff;
+ struct commit_queue_entry *entry, *pentry;
int i;
switch (ch) {
@@ -2121,6 +2174,66 @@ input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
err = create_diff(s);
}
break;
+ case '<':
+ case ',':
+ if (s->commits == NULL)
+ break;
+ entry = TAILQ_PREV(s->selected_entry,
+ commit_queue_head, entry);
+ if (entry == NULL)
+ break;
+
+ err = set_selected_commit(s, entry);
+ if (err)
+ break;
+
+ s->first_displayed_line = 1;
+ s->last_displayed_line = view->nlines;
+
+ err = create_diff(s);
+ break;
+ case '>':
+ case '.':
+ if (s->commits == NULL)
+ break;
+ entry = TAILQ_NEXT(s->selected_entry, entry);
+ if (entry)
+ pentry = TAILQ_NEXT(entry, entry);
+ if (entry == NULL || pentry == NULL) {
+ if (!*s->log_complete)
+ *s->commits_needed = 2;
+ }
+ while (entry == NULL || pentry == NULL) {
+ int errcode;
+ if (*s->log_complete)
+ break;
+ errcode = pthread_cond_signal(s->need_commits);
+ if (errcode)
+ return got_error_set_errno(errcode);
+ errcode = pthread_mutex_unlock(&tog_mutex);
+ if (errcode)
+ return got_error_set_errno(errcode);
+ pthread_yield();
+ errcode = pthread_mutex_lock(&tog_mutex);
+ if (errcode)
+ return got_error_set_errno(errcode);
+ entry = TAILQ_NEXT(s->selected_entry, entry);
+ if (entry)
+ pentry = TAILQ_NEXT(entry, entry);
+ }
+
+ if (entry == NULL)
+ break;
+
+ err = set_selected_commit(s, entry);
+ if (err)
+ break;
+
+ s->first_displayed_line = 1;
+ s->last_displayed_line = view->nlines;
+
+ err = create_diff(s);
+ break;
default:
break;
}
@@ -2197,7 +2310,8 @@ cmd_diff(int argc, char *argv[])
error = got_error_from_errno();
goto done;
}
- error = open_diff_view(view, id1, id2, repo);
+ error = open_diff_view(view, id1, id2, NULL, NULL, NULL, NULL, NULL,
+ repo);
if (error)
goto done;
error = view_loop(view);
@@ -2771,7 +2885,7 @@ input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
break;
}
err = open_diff_view(diff_view, pid ? pid->id : NULL,
- id, s->repo);
+ id, NULL, NULL, NULL, NULL, NULL, s->repo);
got_object_commit_close(commit);
if (err) {
view_close(diff_view);