Skip to content

Commit a37c280

Browse files
committed
Toggle name between committer and author
1. View specific option author-as-committer, similar to date-use-author 2. Work dynamically in views: main, log, refs, blame and tree. tig command : :toggle author-as-committer toggle key seq. : o, m * for log view, it is using `author-as-committer` instead of column option `log-view-author-as-committer` because log view does not have author column. 3. Give precedence to external formatter arguments if any, e.g. tig log --prettry=fuller
1 parent 5013d60 commit a37c280

25 files changed

+330
-155
lines changed

include/tig/line.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ struct ref;
4343
_(PP_REFS, "Refs: "), \
4444
_(PP_REFLOG, "Reflog: "), \
4545
_(PP_REFLOGMSG, "Reflog message: "), \
46+
_(PP_AUTHOR, "Author: " ), \
47+
_(PP_DATE, "Date: "), \
48+
_(PP_AUTHORDATE, "AuthorDate: "), \
49+
_(PP_COMMITTER, "Commit: " ), \
50+
_(PP_COMMITDATE, "CommitDate: "), \
4651
_(COMMIT, "commit "), \
4752
_(PARENT, "parent "), \
4853
_(TREE, "tree "), \

include/tig/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct commit {
2929
struct main_state {
3030
struct graph *graph;
3131
struct commit current;
32+
struct option_common optcom;
3233
char **reflog;
3334
size_t reflogs;
3435
int reflog_width;

include/tig/options.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ typedef struct view_column *view_settings;
9090
_(vertical_split, enum vertical_split, VIEW_RESET_DISPLAY | VIEW_DIFF_LIKE) \
9191
_(wrap_lines, bool, VIEW_DIFF_LIKE) \
9292
_(wrap_search, bool, VIEW_NO_FLAGS) \
93+
_(date_use_author, bool, VIEW_LOG_LIKE) \
94+
_(author_as_committer, bool, VIEW_LOG_LIKE) \
9395

9496
#define DEFINE_OPTION_EXTERNS(name, type, flags) extern type opt_##name;
9597
OPTION_INFO(DEFINE_OPTION_EXTERNS)
@@ -102,6 +104,7 @@ OPTION_INFO(DEFINE_OPTION_EXTERNS)
102104
_(display, enum author, VIEW_NO_FLAGS) \
103105
_(width, int, VIEW_NO_FLAGS) \
104106
_(maxwidth, int, VIEW_NO_FLAGS) \
107+
_(as_committer, bool, VIEW_BLAME_LIKE | VIEW_LOG_LIKE) \
105108

106109
#define COMMIT_TITLE_COLUMN_OPTIONS(_) \
107110
_(display, bool, VIEW_NO_FLAGS) \
@@ -198,7 +201,7 @@ void update_options_from_argv(const char *argv[]);
198201
const char *ignore_space_arg();
199202
const char *commit_order_arg();
200203
const char *commit_order_arg_with_graph(enum graph_display graph_display);
201-
const char *log_custom_pretty_arg(bool use_author_date);
204+
const char *log_custom_pretty_arg(struct option_common *optcom);
202205
const char *use_mailmap_arg();
203206
const char *diff_context_arg();
204207
const char *diff_prefix_arg();
@@ -222,6 +225,7 @@ struct option_info *find_option_info(struct option_info *option, size_t options,
222225
enum status_code parse_option(struct option_info *option, const char *prefix, const char *arg);
223226
struct option_info *find_column_option_info(enum view_column_type type, union view_column_options *opts,
224227
const char *option, struct option_info *column_info, const char **column_name);
228+
void read_option_common(void *view, struct option_common *optcom);
225229
enum status_code parse_int(int *opt, const char *arg, int min, int max);
226230
enum status_code parse_step(double *opt, const char *arg);
227231
enum status_code set_option(const char *opt, int argc, const char *argv[]);

include/tig/parse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct blame_header {
4848
};
4949

5050
bool parse_blame_header(struct blame_header *header, const char *text);
51-
bool parse_blame_info(struct blame_commit *commit, char author[SIZEOF_STR], char *line, bool use_author_date);
51+
bool parse_blame_info(struct blame_commit *commit, char author[SIZEOF_STR], char *line, struct option_common *optcom);
5252

5353
/* Parse author lines where the name may be empty:
5454
* author <email@address.tld> 1138474660 +0100

include/tig/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,14 @@ name(type **mem, size_t size, size_t increase) \
118118

119119
void sigsegv_handler(int sig);
120120

121+
/*
122+
* Common structures
123+
*/
124+
125+
struct option_common {
126+
bool author_as_committer;
127+
bool use_author_date;
128+
};
129+
121130
#endif
122131
/* vim: set ts=8 sw=8 noexpandtab: */

src/blame.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct blame {
4343
struct blame_state {
4444
struct blame_commit *commit;
4545
struct blame_header header;
46+
struct option_common optcom;
4647
char author[SIZEOF_STR];
4748
bool auto_filename_display;
4849
const char *filename;
@@ -77,6 +78,8 @@ blame_open(struct view *view, enum open_flags flags)
7778
enum status_code code;
7879
size_t i;
7980

81+
read_option_common(view, &state->optcom);
82+
8083
if (is_initial_view(view)) {
8184
/* Finish validating and setting up blame options */
8285
if (!opt_file_args || opt_file_args[1])
@@ -216,8 +219,6 @@ static bool
216219
blame_read(struct view *view, struct buffer *buf, bool force_stop)
217220
{
218221
struct blame_state *state = view->private;
219-
struct view_column *column = get_view_column(view, VIEW_COLUMN_DATE);
220-
bool use_author_date = column && column->opt.date.use_author;
221222

222223
if (!buf) {
223224
if (failed_to_load_initial_view(view))
@@ -255,7 +256,7 @@ blame_read(struct view *view, struct buffer *buf, bool force_stop)
255256

256257
state->commit = NULL;
257258

258-
} else if (parse_blame_info(state->commit, state->author, buf->data, use_author_date)) {
259+
} else if (parse_blame_info(state->commit, state->author, buf->data, &state->optcom)) {
259260
if (!state->commit->filename)
260261
return false;
261262

src/diff.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,10 @@ diff_blame_line(const char *ref, const char *file, unsigned long lineno,
591591
struct io io;
592592
bool ok = false;
593593
struct buffer buf;
594+
struct option_common optcom = {
595+
.author_as_committer = false,
596+
.use_author_date = false,
597+
};
594598

595599
if (!string_format(line_arg, "-L%lu,+1", lineno))
596600
return false;
@@ -604,7 +608,7 @@ diff_blame_line(const char *ref, const char *file, unsigned long lineno,
604608
break;
605609
header = NULL;
606610

607-
} else if (parse_blame_info(commit, author, buf.data, false)) {
611+
} else if (parse_blame_info(commit, author, buf.data, &optcom)) {
608612
ok = commit->filename != NULL;
609613
break;
610614
}

src/log.c

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ struct log_state {
2424
* up/down in the log view. */
2525
int last_lineno;
2626
size_t graph_indent;
27+
struct option_common optcom;
2728
enum line_type last_type;
2829
bool commit_title_read;
2930
bool after_commit_header;
3031
bool reading_diff_stat;
32+
bool external_format;
3133
};
3234

3335
static inline void
@@ -63,16 +65,39 @@ log_select(struct view *view, struct line *line)
6365
state->last_type = line->type;
6466
}
6567

68+
static bool
69+
log_check_external_formatter()
70+
{
71+
/* check if any formatter arugments in "%(logargs)", "%(cmdlineargs)" */
72+
const char ** opt_list[] = {
73+
opt_log_options,
74+
opt_cmdline_args,
75+
};
76+
for (int i=0; i<ARRAY_SIZE(opt_list); i++) {
77+
if (opt_list[i] &&
78+
(argv_containsn(opt_list[i], "--pretty", STRING_SIZE("--pretty")) ||
79+
argv_containsn(opt_list[i], "--format", STRING_SIZE("--format"))))
80+
return true;
81+
}
82+
return false;
83+
}
84+
6685
static enum status_code
6786
log_open(struct view *view, enum open_flags flags)
6887
{
88+
struct log_state *state = view->private;
89+
bool external_format = log_check_external_formatter();
6990
const char *log_argv[] = {
7091
"git", "log", encoding_arg, commit_order_arg(),
7192
use_mailmap_arg(), "%(logargs)", "%(cmdlineargs)",
72-
"%(revargs)", "--no-color", "--", "%(fileargs)", NULL
93+
"%(revargs)", "--no-color",
94+
external_format ? "" : "--pretty=fuller",
95+
"--", "%(fileargs)", NULL
7396
};
7497
enum status_code code;
7598

99+
read_option_common(view, &state->optcom);
100+
state->external_format = external_format;
76101
code = begin_update(view, NULL, log_argv, flags | OPEN_WITH_STDERR);
77102
if (code != SUCCESS)
78103
return code;
@@ -115,6 +140,7 @@ log_read(struct view *view, struct buffer *buf, bool force_stop)
115140
size_t len;
116141
char *commit;
117142
char *data;
143+
bool swap_lines = false;
118144

119145
if (!buf)
120146
return true;
@@ -148,10 +174,71 @@ log_read(struct view *view, struct buffer *buf, bool force_stop)
148174
state->reading_diff_stat = false;
149175
}
150176

177+
if (!state->external_format) {
178+
switch (type)
179+
{
180+
case LINE_PP_AUTHOR:
181+
if (state->optcom.author_as_committer)
182+
return true;
183+
break;
184+
case LINE_PP_COMMITTER:
185+
if (!state->optcom.author_as_committer)
186+
return true;
187+
swap_lines = state->optcom.use_author_date;
188+
break;
189+
case LINE_PP_AUTHORDATE:
190+
case LINE_PP_DATE:
191+
if (!state->optcom.use_author_date)
192+
return true;
193+
break;
194+
case LINE_PP_COMMITDATE:
195+
if (state->optcom.use_author_date)
196+
return true;
197+
break;
198+
default:
199+
break;
200+
}
201+
/* remove 4 spaces after Commit:/Author:, or
202+
* convert CommitDate:/AuthorDate: to Date: */
203+
switch (type)
204+
{
205+
case LINE_PP_AUTHOR:
206+
case LINE_PP_COMMITTER:
207+
{
208+
char *p = strchr(data, ':');
209+
if (p && p[5]==' ')
210+
memmove(p+1, p+5, strlen(p+5)+1);
211+
break;
212+
}
213+
case LINE_PP_AUTHORDATE:
214+
case LINE_PP_COMMITDATE:
215+
{
216+
char *p = strchr(data, ':');
217+
if (p && p[1]==' ' && (p - data) >= 10) {
218+
memcpy(p - 10, "Date: ", STRING_SIZE("Date: "));
219+
memmove(p - 10 + STRING_SIZE("Date: "), p+2, strlen(p+2)+1);
220+
}
221+
break;
222+
}
223+
default:
224+
break;
225+
}
226+
}
227+
151228
if (!pager_common_read(view, data, type, &line))
152229
return false;
153230
if (line && state->graph_indent)
154231
line->graph_indent = 1;
232+
if (swap_lines && view->lines >= 2) {
233+
size_t last_idx = view->lines - 1;
234+
struct line *line1 = &view->line[last_idx];
235+
struct line *line2 = &view->line[last_idx - 1];
236+
struct line buf = *line1;
237+
*line1 = *line2;
238+
*line2 = buf;
239+
line1->lineno--;
240+
line2->lineno++;
241+
}
155242
return true;
156243
}
157244

src/main.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,20 @@ main_with_graph(struct view *view, struct view_column *column, enum open_flags f
274274
static enum status_code
275275
main_open(struct view *view, enum open_flags flags)
276276
{
277+
struct main_state *state = view->private;
277278
struct view_column *commit_title_column = get_view_column(view, VIEW_COLUMN_COMMIT_TITLE);
278279
enum graph_display graph_display = main_with_graph(view, commit_title_column, flags);
279-
struct view_column *column = get_view_column(view, VIEW_COLUMN_DATE);
280-
bool use_author_date = column && column->opt.date.use_author;
280+
read_option_common(view, &state->optcom);
281281
const char *pretty_custom_argv[] = {
282282
GIT_MAIN_LOG(encoding_arg, commit_order_arg_with_graph(graph_display),
283283
"%(mainargs)", "%(cmdlineargs)", "%(revargs)", "%(fileargs)",
284-
show_notes_arg(), log_custom_pretty_arg(use_author_date))
284+
show_notes_arg(), log_custom_pretty_arg(&state->optcom))
285285
};
286286
const char *pretty_raw_argv[] = {
287287
GIT_MAIN_LOG_RAW(encoding_arg, commit_order_arg_with_graph(graph_display),
288288
"%(mainargs)", "%(cmdlineargs)", "%(revargs)", "%(fileargs)",
289289
show_notes_arg())
290290
};
291-
struct main_state *state = view->private;
292291
const char **main_argv = pretty_custom_argv;
293292
enum watch_trigger changes_triggers = WATCH_NONE;
294293

@@ -415,8 +414,6 @@ main_add_reflog(struct view *view, struct main_state *state, char *reflog)
415414
bool
416415
main_read(struct view *view, struct buffer *buf, bool force_stop)
417416
{
418-
struct view_column *column = get_view_column(view, VIEW_COLUMN_DATE);
419-
bool use_author_date = column && column->opt.date.use_author;
420417
struct main_state *state = view->private;
421418
struct graph *graph = state->graph;
422419
enum line_type type;
@@ -510,16 +507,17 @@ main_read(struct view *view, struct buffer *buf, bool force_stop)
510507
break;
511508

512509
case LINE_AUTHOR:
513-
parse_author_line(line + STRING_SIZE("author "),
514-
&commit->author, use_author_date ? &commit->time : NULL);
515-
if (state->with_graph)
516-
graph->render_parents(graph, &commit->graph);
517-
break;
518-
519510
case LINE_COMMITTER:
520-
parse_author_line(line + STRING_SIZE("committer "),
521-
NULL, use_author_date ? NULL : &commit->time);
511+
{
512+
bool committer_line = (type == LINE_COMMITTER);
513+
if ((state->optcom.author_as_committer ^ committer_line) == 0)
514+
parse_author_line(line +
515+
(committer_line ? STRING_SIZE("committer ") : STRING_SIZE("author ")),
516+
&commit->author, &commit->time);
517+
if (committer_line && state->with_graph)
518+
graph->render_parents(graph, &commit->graph);
522519
break;
520+
}
523521

524522
default:
525523
/* Fill in the commit title if it has not already been set. */

src/options.c

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ find_column_option_info(enum view_column_type type, union view_column_options *o
115115
return NULL;
116116
}
117117

118+
void read_option_common(void *_view, struct option_common *opt)
119+
{
120+
extern struct view log_view;
121+
struct view *view = _view;
122+
bool as_committer = false;
123+
bool use_author_date = false;
124+
125+
if (view == &log_view) {
126+
as_committer = opt_author_as_committer;
127+
use_author_date = opt_date_use_author;
128+
} else {
129+
struct view_column *column1 = get_view_column(view, VIEW_COLUMN_AUTHOR);
130+
struct view_column *column2 = get_view_column(view, VIEW_COLUMN_DATE);
131+
132+
if (column1)
133+
as_committer = column1->opt.author.as_committer;
134+
if (column2)
135+
use_author_date = column2->opt.date.use_author;
136+
}
137+
138+
opt->author_as_committer = as_committer;
139+
opt->use_author_date = use_author_date;
140+
}
141+
118142
/*
119143
* State variables.
120144
*/
@@ -159,15 +183,28 @@ use_mailmap_arg()
159183
}
160184

161185
const char *
162-
log_custom_pretty_arg(bool use_author_date)
186+
log_custom_pretty_arg(struct option_common *optcom)
163187
{
164-
return use_author_date
165-
? opt_mailmap
166-
? "--pretty=format:commit %m %H %P%x00%aN <%aE> %ad%x00%s%x00%N"
167-
: "--pretty=format:commit %m %H %P%x00%an <%ae> %ad%x00%s%x00%N"
168-
: opt_mailmap
169-
? "--pretty=format:commit %m %H %P%x00%aN <%aE> %cd%x00%s%x00%N"
170-
: "--pretty=format:commit %m %H %P%x00%an <%ae> %cd%x00%s%x00%N";
188+
switch ((optcom->author_as_committer*2) + opt_mailmap)
189+
{
190+
case 0x3:
191+
return optcom->use_author_date ?
192+
"--pretty=format:commit %m %H %P%x00%cN <%cE> %ad%x00%s%x00%N" :
193+
"--pretty=format:commit %m %H %P%x00%cN <%cE> %cd%x00%s%x00%N";
194+
case 0x2:
195+
return optcom->use_author_date ?
196+
"--pretty=format:commit %m %H %P%x00%cn <%ce> %ad%x00%s%x00%N" :
197+
"--pretty=format:commit %m %H %P%x00%cn <%ce> %cd%x00%s%x00%N";
198+
case 0x1:
199+
return optcom->use_author_date ?
200+
"--pretty=format:commit %m %H %P%x00%aN <%aE> %ad%x00%s%x00%N" :
201+
"--pretty=format:commit %m %H %P%x00%aN <%aE> %cd%x00%s%x00%N";
202+
case 0x0:
203+
default:
204+
return optcom->use_author_date ?
205+
"--pretty=format:commit %m %H %P%x00%an <%ae> %ad%x00%s%x00%N" :
206+
"--pretty=format:commit %m %H %P%x00%an <%ae> %cd%x00%s%x00%N";
207+
}
171208
}
172209

173210
#define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)

0 commit comments

Comments
 (0)