allow sorting references by timestamp in tog
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
diff --git a/lib/reference.c b/lib/reference.c
index 23650d4..3d4dadc 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -769,50 +769,76 @@ done:
return err;
}
+static const struct got_error *
+get_committer_time(struct got_reference *ref, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ int obj_type;
+ struct got_commit_object *commit = NULL;
+ struct got_tag_object *tag = NULL;
+ struct got_object_id *id = NULL;
+
+ err = got_ref_resolve(&id, repo, ref);
+ if (err)
+ return err;
+
+ err = got_object_get_type(&obj_type, repo, id);
+ if (err)
+ goto done;
+
+ switch (obj_type) {
+ case GOT_OBJ_TYPE_COMMIT:
+ err = got_object_open_as_commit(&commit, repo, id);
+ if (err)
+ goto done;
+ ref->committer_time =
+ got_object_commit_get_committer_time(commit);
+ break;
+ case GOT_OBJ_TYPE_TAG:
+ err = got_object_open_as_tag(&tag, repo, id);
+ if (err)
+ goto done;
+ ref->committer_time = got_object_tag_get_tagger_time(tag);
+ break;
+ default:
+ /* best effort for other object types */
+ ref->committer_time = got_ref_get_mtime(ref);
+ break;
+ }
+done:
+ free(id);
+ if (commit)
+ got_object_commit_close(commit);
+ if (tag)
+ got_object_tag_close(tag);
+ return err;
+}
+
const struct got_error *
got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
struct got_reference *ref1, struct got_reference *ref2)
{
const struct got_error *err;
struct got_repository *repo = arg;
- struct got_object_id *id1 = NULL, *id2 = NULL;
- struct got_commit_object *commit1 = NULL, *commit2 = NULL;
*cmp = 0;
if (ref1->committer_time == 0) {
- err = got_ref_resolve(&id1, repo, ref1);
+ err = get_committer_time(ref1, repo);
if (err)
return err;
- err = got_object_open_as_commit(&commit1, repo, id1);
- if (err)
- goto done;
- ref1->committer_time =
- got_object_commit_get_committer_time(commit1);
}
-
if (ref2->committer_time == 0) {
- err = got_ref_resolve(&id2, repo, ref2);
+ err = get_committer_time(ref2, repo);
if (err)
return err;
- err = got_object_open_as_commit(&commit2, repo, id2);
- if (err)
- goto done;
- ref2->committer_time =
- got_object_commit_get_committer_time(commit2);
}
if (ref1->committer_time < ref2->committer_time)
*cmp = 1;
else if (ref2->committer_time < ref1->committer_time)
*cmp = -1;
-done:
- free(id1);
- free(id2);
- if (commit1)
- got_object_commit_close(commit1);
- if (commit2)
- got_object_commit_close(commit2);
+
return err;
}
diff --git a/tog/tog.1 b/tog/tog.1
index 0e414ba..838d2ac 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -452,6 +452,8 @@ view showing the tree resolved via the currently selected reference.
Show object IDs for all non-symbolic references displayed in the
.Cm ref
view.
+.It Cm s
+Toggle display order of references between sort by name and sort by timestamp.
.It Cm /
Prompt for a search pattern and start searching for matching references.
The search pattern is an extended regular expression which is matched
diff --git a/tog/tog.c b/tog/tog.c
index 74c6676..deb6c22 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -129,11 +129,13 @@ static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
static struct got_reflist_object_id_map *tog_refs_idmap;
static const struct got_error *
-tog_load_refs(struct got_repository *repo)
+tog_load_refs(struct got_repository *repo, int sort_by_date)
{
const struct got_error *err;
- err = got_ref_list(&tog_refs, repo, NULL, got_ref_cmp_by_name, NULL);
+ err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
+ got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
+ repo);
if (err)
return err;
@@ -439,7 +441,7 @@ struct tog_ref_view_state {
struct tog_reflist_entry *first_displayed_entry;
struct tog_reflist_entry *last_displayed_entry;
struct tog_reflist_entry *selected_entry;
- int nrefs, ndisplayed, selected, show_ids;
+ int nrefs, ndisplayed, selected, show_ids, sort_by_date;
struct got_repository *repo;
struct tog_reflist_entry *matched_entry;
struct tog_colors colors;
@@ -2586,7 +2588,7 @@ input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
if (err)
return err;
tog_free_refs();
- err = tog_load_refs(s->repo);
+ err = tog_load_refs(s->repo, 0);
if (err)
return err;
err = got_commit_graph_open(&s->thread_args.graph,
@@ -2793,7 +2795,7 @@ cmd_log(int argc, char *argv[])
/* already loaded by tog_log_with_path()? */
if (TAILQ_EMPTY(&tog_refs)) {
- error = tog_load_refs(repo);
+ error = tog_load_refs(repo, 0);
if (error)
goto done;
}
@@ -3914,7 +3916,7 @@ cmd_diff(int argc, char *argv[])
if (error)
goto done;
- error = tog_load_refs(repo);
+ error = tog_load_refs(repo, 0);
if (error)
goto done;
@@ -4842,7 +4844,7 @@ cmd_blame(int argc, char *argv[])
if (error)
goto done;
- error = tog_load_refs(repo);
+ error = tog_load_refs(repo, 0);
if (error)
goto done;
@@ -5695,7 +5697,7 @@ cmd_tree(int argc, char *argv[])
if (error)
goto done;
- error = tog_load_refs(repo);
+ error = tog_load_refs(repo, 0);
if (error)
goto done;
@@ -6230,6 +6232,15 @@ input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
case 'i':
s->show_ids = !s->show_ids;
break;
+ case 's':
+ s->sort_by_date = !s->sort_by_date;
+ tog_free_refs();
+ err = tog_load_refs(s->repo, s->sort_by_date);
+ if (err)
+ break;
+ ref_view_free_refs(s);
+ err = ref_view_load_refs(s);
+ break;
case KEY_ENTER:
case '\r':
if (!s->selected_entry)
@@ -6326,7 +6337,7 @@ input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
break;
case CTRL('l'):
tog_free_refs();
- err = tog_load_refs(s->repo);
+ err = tog_load_refs(s->repo, s->sort_by_date);
if (err)
break;
ref_view_free_refs(s);
@@ -6410,7 +6421,7 @@ cmd_ref(int argc, char *argv[])
if (error)
goto done;
- error = tog_load_refs(repo);
+ error = tog_load_refs(repo, 0);
if (error)
goto done;
@@ -6535,7 +6546,7 @@ tog_log_with_path(int argc, char *argv[])
if (error)
goto done;
- error = tog_load_refs(repo);
+ error = tog_load_refs(repo, 0);
if (error)
goto done;
error = got_repo_match_object_id(&commit_id, NULL, worktree ?