Skip to content

Commit

Permalink
Add support of toggling commit and author (name and date)
Browse files Browse the repository at this point in the history
1. author name and committer name, toggle option=m
2. author-date and commit-date, toggle option=M
This is effective in views: main, log, refs, blame and tree.
  • Loading branch information
PaulChanHK committed Oct 7, 2021
1 parent f92286a commit 85a2e73
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 46 deletions.
1 change: 1 addition & 0 deletions include/tig/argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ bool argv_appendn(const char ***argv, const char *arg, size_t arglen);
bool argv_append_array(const char ***dst_argv, const char *src_argv[]);
bool argv_copy(const char ***dst, const char *src[]);
bool argv_contains(const char **argv, const char *arg);
bool argv_containsn(const char **argv, const char *arg, size_t arglen);

typedef char argv_string[SIZEOF_STR];
typedef unsigned long argv_number;
Expand Down
5 changes: 5 additions & 0 deletions include/tig/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ struct ref;
_(PP_REFS, "Refs: "), \
_(PP_REFLOG, "Reflog: "), \
_(PP_REFLOGMSG, "Reflog message: "), \
_(PP_AUTHOR, "Author: " ), \
_(PP_AUTHORDATE, "Date: "), \
_(PP_AUTHORDATE2, "AuthorDate: "), \
_(PP_COMMITTER, "Commit: " ), \
_(PP_COMMITDATE, "CommitDate: "), \
_(COMMIT, "commit "), \
_(PARENT, "parent "), \
_(TREE, "tree "), \
Expand Down
2 changes: 2 additions & 0 deletions include/tig/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ typedef struct view_column *view_settings;
_(vertical_split, enum vertical_split, VIEW_RESET_DISPLAY | VIEW_DIFF_LIKE) \
_(wrap_lines, bool, VIEW_NO_FLAGS) \
_(wrap_search, bool, VIEW_NO_FLAGS) \
_(committer, bool, VIEW_LOG_LIKE | VIEW_BLAME_LIKE) \
_(commit_date, bool, VIEW_LOG_LIKE | VIEW_BLAME_LIKE) \

#define DEFINE_OPTION_EXTERNS(name, type, flags) extern type opt_##name;
OPTION_INFO(DEFINE_OPTION_EXTERNS)
Expand Down
1 change: 1 addition & 0 deletions include/tig/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ enum view_flag {
VIEW_SORTABLE = 1 << 15,
VIEW_FLEX_WIDTH = 1 << 16,
VIEW_RESET_DISPLAY = 1 << 17,
VIEW_COMMIT_NAMEDATE = 1 << 18,
};

#define view_has_flags(view, flag) ((view)->ops->flags & (flag))
Expand Down
12 changes: 12 additions & 0 deletions src/argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ argv_contains(const char **argv, const char *arg)
return false;
}

bool
argv_containsn(const char **argv, const char *arg, size_t arglen)
{
int i;

if (arglen == 0) arglen = strlen(arg);
for (i = 0; argv && argv[i]; i++)
if (!strncmp(argv[i], arg, arglen))
return true;
return false;
}

DEFINE_ALLOCATOR(argv_realloc, const char *, SIZEOF_ARG)

bool
Expand Down
2 changes: 1 addition & 1 deletion src/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ blame_select(struct view *view, struct line *line)
static struct view_ops blame_ops = {
"line",
argv_env.commit,
VIEW_SEND_CHILD_ENTER | VIEW_BLAME_LIKE,
VIEW_SEND_CHILD_ENTER | VIEW_BLAME_LIKE | VIEW_REFRESH | VIEW_COMMIT_NAMEDATE,
sizeof(struct blame_state),
blame_open,
blame_read,
Expand Down
32 changes: 30 additions & 2 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct log_state {
bool commit_title_read;
bool after_commit_header;
bool reading_diff_stat;
bool external_format;
};

static inline void
Expand Down Expand Up @@ -63,13 +64,20 @@ log_select(struct view *view, struct line *line)
static enum status_code
log_open(struct view *view, enum open_flags flags)
{
struct log_state *state = view->private;
bool external_format = (opt_log_options &&
(argv_containsn(opt_log_options, "--pretty", 0) ||
argv_containsn(opt_log_options, "--format", 0)));
const char *log_argv[] = {
"git", "log", encoding_arg, commit_order_arg(), "--cc",
"--stat", use_mailmap_arg(), "%(logargs)", "%(cmdlineargs)",
"%(revargs)", "--no-color", "--", "%(fileargs)", NULL
"%(revargs)", "--no-color",
external_format ? "" : "--pretty=fuller",
"--", "%(fileargs)", NULL
};
enum status_code code;

state->external_format = external_format;
code = begin_update(view, NULL, log_argv, flags);
if (code != SUCCESS)
return code;
Expand Down Expand Up @@ -142,6 +150,25 @@ log_read(struct view *view, struct buffer *buf, bool force_stop)
state->reading_diff_stat = false;
}

switch (type)
{
case LINE_PP_AUTHOR:
case LINE_PP_COMMITTER:
if (!state->external_format &&
(opt_committer ^ (type == LINE_PP_COMMITTER)))
return true;
break;
case LINE_PP_AUTHORDATE:
case LINE_PP_AUTHORDATE2:
case LINE_PP_COMMITDATE:
if (!state->external_format &&
(opt_commit_date ^ (type == LINE_PP_COMMITDATE)))
return true;
break;
default:
break;
}

if (!pager_common_read(view, data, type, &line))
return false;
if (line && state->graph_indent)
Expand All @@ -152,7 +179,8 @@ log_read(struct view *view, struct buffer *buf, bool force_stop)
static struct view_ops log_ops = {
"line",
argv_env.head,
VIEW_ADD_PAGER_REFS | VIEW_OPEN_DIFF | VIEW_SEND_CHILD_ENTER | VIEW_LOG_LIKE | VIEW_REFRESH | VIEW_FLEX_WIDTH,
VIEW_ADD_PAGER_REFS | VIEW_OPEN_DIFF | VIEW_SEND_CHILD_ENTER | VIEW_LOG_LIKE | VIEW_REFRESH | \
VIEW_FLEX_WIDTH | VIEW_COMMIT_NAMEDATE,
sizeof(struct log_state),
log_open,
log_read,
Expand Down
14 changes: 10 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,17 @@ main_read(struct view *view, struct buffer *buf, bool force_stop)
break;

case LINE_AUTHOR:
parse_author_line(line + STRING_SIZE("author "),
&commit->author, &commit->time);
if (state->with_graph)
case LINE_COMMITTER:
{
bool committer_line = (type == LINE_COMMITTER);
parse_author_line(line +
(committer_line ? STRING_SIZE("committer ") : STRING_SIZE("author ")),
(opt_committer ^ committer_line) ? NULL : &commit->author,
(opt_commit_date ^ committer_line) ? NULL : &commit->time);
if (committer_line && state->with_graph)
graph->render_parents(graph, &commit->graph);
break;
}

default:
/* Fill in the commit title if it has not already been set. */
Expand Down Expand Up @@ -629,7 +635,7 @@ main_select(struct view *view, struct line *line)
static struct view_ops main_ops = {
"commit",
argv_env.head,
VIEW_SEND_CHILD_ENTER | VIEW_FILE_FILTER | VIEW_LOG_LIKE | VIEW_REFRESH,
VIEW_SEND_CHILD_ENTER | VIEW_FILE_FILTER | VIEW_LOG_LIKE | VIEW_REFRESH | VIEW_COMMIT_NAMEDATE,
sizeof(struct main_state),
main_open,
main_read,
Expand Down
13 changes: 10 additions & 3 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ use_mailmap_arg()
const char *
log_custom_pretty_arg(void)
{
return opt_mailmap
? "--pretty=format:commit %m %H %P%x00%aN <%aE> %ad%x00%s%x00%N"
: "--pretty=format:commit %m %H %P%x00%an <%ae> %ad%x00%s%x00%N";
static char pretty_arg[64];
char name_c = opt_committer ? 'c' : 'a';
snprintf(pretty_arg, sizeof(pretty_arg)-1,
"--pretty=format:commit %%m %%H %%P%%x00%%%c%c <%%%c%c> %s%%x00%%s%%x00%%N",
name_c, opt_mailmap ? 'N' : 'n',
name_c, opt_mailmap ? 'E' : 'e',
opt_commit_date ? "%cd" : "%ad");
return pretty_arg;
}

#define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)
Expand Down Expand Up @@ -887,6 +892,8 @@ option_bind_command(int argc, const char *argv[])
{ "toggle-title-overflow", "commit-title-overflow" },
{ "toggle-untracked-dirs", "status-show-untracked-dirs" },
{ "toggle-vertical-split", "show-vertical-split" },
{ "toggle-committer", "committer" },
{ "toggle-commit-date", "commit-date" },
};
int alias;

Expand Down
12 changes: 7 additions & 5 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "tig/tig.h"
#include "tig/parse.h"
#include "tig/map.h"
#include "tig/options.h"

size_t
parse_size(const char *text)
Expand Down Expand Up @@ -73,7 +74,8 @@ parse_author_line(char *ident, const struct ident **author, struct time *time)
if (!*email)
email = *name ? name : unknown_ident.email;

*author = get_author(name, email);
if (author)
*author = get_author(name, email);

/* Parse epoch and timezone */
if (time && emailend && emailend[1] == ' ') {
Expand Down Expand Up @@ -141,10 +143,10 @@ match_blame_header(const char *name, char **line)
bool
parse_blame_info(struct blame_commit *commit, char author[SIZEOF_STR], char *line)
{
if (match_blame_header("author ", &line)) {
if (match_blame_header(opt_committer ? "committer " : "author ", &line)) {
string_ncopy_do(author, SIZEOF_STR, line, strlen(line));

} else if (match_blame_header("author-mail ", &line)) {
} else if (match_blame_header(opt_committer ? "committer-mail " : "author-mail ", &line)) {
char *end = strchr(line, '>');

if (end)
Expand All @@ -154,10 +156,10 @@ parse_blame_info(struct blame_commit *commit, char author[SIZEOF_STR], char *lin
commit->author = get_author(author, line);
author[0] = 0;

} else if (match_blame_header("author-time ", &line)) {
} else if (match_blame_header(opt_commit_date ? "committer-time " : "author-time ", &line)) {
parse_timesec(&commit->time, line);

} else if (match_blame_header("author-tz ", &line)) {
} else if (match_blame_header(opt_commit_date ? "committer-tz " : "author-tz ", &line)) {
parse_timezone(&commit->time, line);

} else if (match_blame_header("summary ", &line)) {
Expand Down
7 changes: 7 additions & 0 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,13 @@ prompt_toggle(struct view *view, const char *argv[], enum view_flag *flags)
}
}

if (enum_equals_static("committer", option, optionlen) ||
enum_equals_static("commit-date", option, optionlen)) {
if (!view_has_flags(view, VIEW_COMMIT_NAMEDATE)) {
return error("`:toggle %s` is not supported for the %s view", option, view->name);
}
}

toggle = find_option_info(option_toggles, ARRAY_SIZE(option_toggles), "", option);
if (toggle)
return prompt_toggle_option(view, argv, "", toggle, flags);
Expand Down
15 changes: 10 additions & 5 deletions src/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ refs_open_visitor(void *data, const struct ref *ref)
bool is_all = ref == refs_all;
struct line *line;

if (!is_all)
if (!is_all)
switch (refs_filter) {
case REFS_FILTER_TAGS:
if (ref->type != REFERENCE_TAG && ref->type != REFERENCE_LOCAL_TAG)
Expand Down Expand Up @@ -173,11 +173,11 @@ static const char **refs_argv;
static enum status_code
refs_open(struct view *view, enum open_flags flags)
{
char pretty_arg[50] = {0};
char name_c = opt_committer ? 'c' : 'a';
const char *refs_log[] = {
"git", "log", encoding_arg, "--no-color", "--date=raw",
opt_mailmap ? "--pretty=format:%H%x00%aN <%aE> %ad%x00%s"
: "--pretty=format:%H%x00%an <%ae> %ad%x00%s",
"--all", "--simplify-by-decoration", NULL
pretty_arg, "--all", "--simplify-by-decoration", NULL
};
enum status_code code;
const char *name = REFS_ALL_NAME;
Expand All @@ -201,6 +201,11 @@ refs_open(struct view *view, enum open_flags flags)
}
}

snprintf(pretty_arg, sizeof(pretty_arg)-1,
"--pretty=format:%%H%%x00%%%c%c <%%%c%c> %s%%x00%%s",
name_c, opt_mailmap ? 'N' : 'n',
name_c, opt_mailmap ? 'E' : 'e',
opt_commit_date ? "%cd" : "%ad");
if (!refs_all) {
int name_length = strlen(name);
struct ref *ref = calloc(1, sizeof(*refs_all) + name_length);
Expand Down Expand Up @@ -246,7 +251,7 @@ refs_select(struct view *view, struct line *line)
static struct view_ops refs_ops = {
"reference",
argv_env.head,
VIEW_REFRESH | VIEW_SORTABLE,
VIEW_SORTABLE | VIEW_BLAME_LIKE | VIEW_REFRESH | VIEW_COMMIT_NAMEDATE,
0,
refs_open,
refs_read,
Expand Down
4 changes: 4 additions & 0 deletions src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ view_request(struct view *view, enum request request)
_('$', "commit title overflow display", "commit-title-overflow"), \
_('d', "untracked directory info", "status-show-untracked-dirs"), \
_('|', "view split", "vertical-split"), \
_('E', "mail map", "mailmap"), \
_('L', "local date", "date-local"), \
_('M', "committer", "committer"), \
_('m', "commit date", "commit-date"), \

static void
toggle_option(struct view *view)
Expand Down
33 changes: 23 additions & 10 deletions src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ struct tree_entry {

struct tree_state {
char commit[SIZEOF_REV];
const struct ident *author;
struct time author_time;
const struct ident *author; /* Author/Committer */
struct time author_time; /* Author data/Commit date */
bool read_date;
};

Expand Down Expand Up @@ -171,13 +171,26 @@ tree_read_date(struct view *view, struct buffer *buf, struct tree_state *state)
state->read_date = true;
return false;

} else if (*text == 'c' && get_line_type(text) == LINE_COMMIT) {
string_copy_rev_from_commit_line(state->commit, text);

} else if (*text == 'a' && get_line_type(text) == LINE_AUTHOR) {
parse_author_line(text + STRING_SIZE("author "),
&state->author, &state->author_time);

} else if (*text == 'c' || *text == 'a') {
enum line_type type = get_line_type(text);
switch (type)
{
case LINE_COMMIT:
string_copy_rev_from_commit_line(state->commit, text);
break;
case LINE_AUTHOR:
case LINE_COMMITTER:
{
bool committer_line = (type == LINE_COMMITTER);
parse_author_line(text +
(committer_line ? STRING_SIZE("committer ") : STRING_SIZE("author ")),
(opt_committer ^ committer_line) ? NULL : &state->author,
(opt_commit_date ^ committer_line) ? NULL : &state->author_time);
break;
}
default:
break;
}
} else if (*text == ':') {
char *pos;
size_t annotated = 1;
Expand Down Expand Up @@ -464,7 +477,7 @@ tree_open(struct view *view, enum open_flags flags)
static struct view_ops tree_ops = {
"file",
argv_env.commit,
VIEW_SEND_CHILD_ENTER | VIEW_SORTABLE,
VIEW_SEND_CHILD_ENTER | VIEW_SORTABLE | VIEW_LOG_LIKE | VIEW_REFRESH | VIEW_COMMIT_NAMEDATE,
sizeof(struct tree_state),
tree_open,
tree_read,
Expand Down
4 changes: 2 additions & 2 deletions test/diff/diff-stat-test
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ Dimensions: height=28 width=181
Position: offset=0 column=0 lineno=0
line[ 0] type=commit selected=1
line[ 0] cells=1 text=[commit 1b4c64b595aeb4de1d317d669faacd3c1d82f0b0]
line[ 1] type= selected=0
line[ 1] type=pp-author selected=0
line[ 1] cells=1 text=[Author: A. U. Thor <[email protected]>]
line[ 2] type= selected=0
line[ 2] type=pp-authordate selected=0
line[ 2] cells=1 text=[Date: Sun Jul 2 15:01:24 2017 +0200]
line[ 3] type=default selected=0
line[ 3] cells=1 text=[]
Expand Down
4 changes: 2 additions & 2 deletions test/log/diff-stat-test
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ assert_equals 'diff-stat.screen' <<EOF
commit ee912870202200a0b9cf4fd86ba57243212d341e |commit ee912870202200a0b9cf4fd86ba57243212d341e
Refs: [master] |Refs: [master]
Author: Jonas Fonseca <[email protected]> |Author: Jonas Fonseca <[email protected]>
Date: Sat Mar 1 17:26:01 2014 -0500 |AuthorDate: Sat Mar 1 17:26:01 2014 -0500
AuthorDate: Sat Mar 1 17:26:01 2014 -0500 |AuthorDate: Sat Mar 1 17:26:01 2014 -0500
|Commit: Jonas Fonseca <[email protected]>
WIP: Upgrade to 0.4-SNAPSHOT and DCE |CommitDate: Sat Mar 1 17:26:01 2014 -0500
|
Expand Down Expand Up @@ -57,7 +57,7 @@ assert_equals 'diff-stat-after-refresh.screen' <<EOF
commit ee912870202200a0b9cf4fd86ba57243212d341e |commit ee912870202200a0b9cf4fd86ba57243212d341e
Refs: [master] |Refs: [master]
Author: Jonas Fonseca <[email protected]> |Author: Jonas Fonseca <[email protected]>
Date: Sat Mar 1 17:26:01 2014 -0500 |AuthorDate: Sat Mar 1 17:26:01 2014 -0500
AuthorDate: Sat Mar 1 17:26:01 2014 -0500 |AuthorDate: Sat Mar 1 17:26:01 2014 -0500
|Commit: Jonas Fonseca <[email protected]>
WIP: Upgrade to 0.4-SNAPSHOT and DCE |CommitDate: Sat Mar 1 17:26:01 2014 -0500
|
Expand Down
Loading

0 comments on commit 85a2e73

Please sign in to comment.