From: Mark Jamsek Subject: Re: [rfc] tog ref 'd' keymap to toggle date To: gameoftrees@openbsd.org Date: Thu, 16 Jun 2022 15:20:11 +1000 On 22-06-14 10:02am, Stefan Sperling wrote: > On Tue, Jun 14, 2022 at 03:55:12PM +1000, Mark Jamsek wrote: > > As mentioned in the recent tog thread, a potential use of the 'd' keymap > > in the ref view is to show the last modified date of each entry. > > I figured it was quick enough to demonstrate with a patch. > > I like this, thank you! > > And I don't see any mistakes in the diff. > If nobody has any concerns over this I will commit it soon. > Here's the rebased patch. Key map has been changed to 'm'. diff 1b1b91abdb4cd380995e3542580daa8700d93f6f /home/mark/src/git/got blob - 56d92245cc73778b494f7d494ab0ad7560d3fab6 file + tog/tog.1 --- tog/tog.1 +++ tog/tog.1 @@ -472,6 +472,8 @@ view showing the tree resolved via the currently selec Show object IDs for all non-symbolic references displayed in the .Cm ref view. +.It Cm m +Show last modified date of each displayed reference. .It Cm o Toggle display order of references between sort by name and sort by timestamp. .It Cm / blob - 48c3db4951d9e65f17f32a087f88342694e36c1f file + tog/tog.c --- tog/tog.c +++ tog/tog.c @@ -470,7 +470,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, sort_by_date; + int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date; struct got_repository *repo; struct tog_reflist_entry *matched_entry; struct tog_colors colors; @@ -6313,10 +6313,45 @@ show_ref_view(struct tog_view *view) n = 0; while (re && limit > 0) { char *line = NULL; + char ymd[13]; /* YYYY-MM-DD + " " + NUL */ + if (s->show_date) { + struct got_commit_object *ci; + struct got_tag_object *tag; + struct got_object_id *id; + struct tm tm; + time_t t; + + err = got_ref_resolve(&id, s->repo, re->ref); + if (err) + return err; + err = got_object_open_as_tag(&tag, s->repo, id); + if (err) { + if (err->code != GOT_ERR_OBJ_TYPE) { + free(id); + return err; + } + err = got_object_open_as_commit(&ci, s->repo, + id); + if (err) { + free(id); + return err; + } + t = got_object_commit_get_committer_time(ci); + got_object_commit_close(ci); + } else { + t = got_object_tag_get_tagger_time(tag); + got_object_tag_close(tag); + } + free(id); + if (gmtime_r(&t, &tm) == NULL) + return got_error_from_errno("gmtime_r"); + if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0) + return got_error(GOT_ERR_NO_SPACE); + } if (got_ref_is_symbolic(re->ref)) { - if (asprintf(&line, "%s -> %s", - got_ref_get_name(re->ref), + if (asprintf(&line, "%s%s -> %s", s->show_date ? + ymd : "", got_ref_get_name(re->ref), got_ref_get_symref_target(re->ref)) == -1) return got_error_from_errno("asprintf"); } else if (s->show_ids) { @@ -6330,7 +6365,7 @@ show_ref_view(struct tog_view *view) free(id); return err; } - if (asprintf(&line, "%s: %s", + if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "", got_ref_get_name(re->ref), id_str) == -1) { err = got_error_from_errno("asprintf"); free(id); @@ -6339,11 +6374,9 @@ show_ref_view(struct tog_view *view) } free(id); free(id_str); - } else { - line = strdup(got_ref_get_name(re->ref)); - if (line == NULL) - return got_error_from_errno("strdup"); - } + } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "", + got_ref_get_name(re->ref)) == -1) + return got_error_from_errno("asprintf"); err = format_line(&wline, &width, line, view->ncols, 0); if (err) { @@ -6430,6 +6463,9 @@ input_ref_view(struct tog_view **new_view, struct tog_ case 'i': s->show_ids = !s->show_ids; break; + case 'm': + s->show_date = !s->show_date; + break; case 'o': s->sort_by_date = !s->sort_by_date; err = got_reflist_sort(&tog_refs, s->sort_by_date ? -- Mark Jamsek GPG: F2FF 13DE 6A06 C471 CA80 E6E2 2930 DC66 86EE CF68