]> gitweb.ps.run Git - ps-cgit/blob - ui-log.c
git: update to v2.46.0
[ps-cgit] / ui-log.c
1 /* ui-log.c: functions for log output
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12 #include "ui-log.h"
13 #include "html.h"
14 #include "ui-shared.h"
15 #include "strvec.h"
16
17 static int files, add_lines, rem_lines, lines_counted;
18
19 /*
20  * The list of available column colors in the commit graph.
21  */
22 static const char *column_colors_html[] = {
23         "<span class='column1'>",
24         "<span class='column2'>",
25         "<span class='column3'>",
26         "<span class='column4'>",
27         "<span class='column5'>",
28         "<span class='column6'>",
29         "</span>",
30 };
31
32 #define COLUMN_COLORS_HTML_MAX (ARRAY_SIZE(column_colors_html) - 1)
33
34 static void count_lines(char *line, int size)
35 {
36         if (size <= 0)
37                 return;
38
39         if (line[0] == '+')
40                 add_lines++;
41
42         else if (line[0] == '-')
43                 rem_lines++;
44 }
45
46 static void inspect_files(struct diff_filepair *pair)
47 {
48         unsigned long old_size = 0;
49         unsigned long new_size = 0;
50         int binary = 0;
51
52         files++;
53         if (ctx.repo->enable_log_linecount)
54                 cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size,
55                                 &new_size, &binary, 0, ctx.qry.ignorews,
56                                 count_lines);
57 }
58
59 void show_commit_decorations(struct commit *commit)
60 {
61         const struct name_decoration *deco;
62         static char buf[1024];
63
64         buf[sizeof(buf) - 1] = 0;
65         deco = get_name_decoration(&commit->object);
66         if (!deco)
67                 return;
68         html("<span class='decoration'>");
69         while (deco) {
70                 struct object_id oid_tag, peeled;
71                 int is_annotated = 0;
72
73                 strlcpy(buf, prettify_refname(deco->name), sizeof(buf));
74                 switch(deco->type) {
75                 case DECORATION_NONE:
76                         /* If the git-core doesn't recognize it,
77                          * don't display anything. */
78                         break;
79                 case DECORATION_REF_LOCAL:
80                         cgit_log_link(buf, NULL, "branch-deco", buf, NULL,
81                                 ctx.qry.vpath, 0, NULL, NULL,
82                                 ctx.qry.showmsg, 0);
83                         break;
84                 case DECORATION_REF_TAG:
85                         if (!refs_read_ref(get_main_ref_store(the_repository), deco->name, &oid_tag) &&
86                             !peel_iterated_oid(the_repository, &oid_tag, &peeled))
87                                 is_annotated = !oideq(&oid_tag, &peeled);
88                         cgit_tag_link(buf, NULL, is_annotated ? "tag-annotated-deco" : "tag-deco", buf);
89                         break;
90                 case DECORATION_REF_REMOTE:
91                         if (!ctx.repo->enable_remote_branches)
92                                 break;
93                         cgit_log_link(buf, NULL, "remote-deco", NULL,
94                                 oid_to_hex(&commit->object.oid),
95                                 ctx.qry.vpath, 0, NULL, NULL,
96                                 ctx.qry.showmsg, 0);
97                         break;
98                 default:
99                         cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
100                                         oid_to_hex(&commit->object.oid),
101                                         ctx.qry.vpath);
102                         break;
103                 }
104                 deco = deco->next;
105         }
106         html("</span>");
107 }
108
109 static void handle_rename(struct diff_filepair *pair)
110 {
111         /*
112          * After we have seen a rename, we generate links to the previous
113          * name of the file so that commit & diff views get fed the path
114          * that is correct for the commit they are showing, avoiding the
115          * need to walk the entire history leading back to every commit we
116          * show in order detect renames.
117          */
118         if (0 != strcmp(ctx.qry.vpath, pair->two->path)) {
119                 free(ctx.qry.vpath);
120                 ctx.qry.vpath = xstrdup(pair->two->path);
121         }
122         inspect_files(pair);
123 }
124
125 static int show_commit(struct commit *commit, struct rev_info *revs)
126 {
127         struct commit_list *parents = commit->parents;
128         struct commit *parent;
129         int found = 0, saved_fmt;
130         struct diff_flags saved_flags = revs->diffopt.flags;
131
132         /* Always show if we're not in "follow" mode with a single file. */
133         if (!ctx.qry.follow)
134                 return 1;
135
136         /*
137          * In "follow" mode, we don't show merges.  This is consistent with
138          * "git log --follow -- <file>".
139          */
140         if (parents && parents->next)
141                 return 0;
142
143         /*
144          * If this is the root commit, do what rev_info tells us.
145          */
146         if (!parents)
147                 return revs->show_root_diff;
148
149         /* When we get here we have precisely one parent. */
150         parent = parents->item;
151         /* If we can't parse the commit, let print_commit() report an error. */
152         if (repo_parse_commit(the_repository, parent))
153                 return 1;
154
155         files = 0;
156         add_lines = 0;
157         rem_lines = 0;
158
159         revs->diffopt.flags.recursive = 1;
160         diff_tree_oid(get_commit_tree_oid(parent),
161                       get_commit_tree_oid(commit),
162                       "", &revs->diffopt);
163         diffcore_std(&revs->diffopt);
164
165         found = !diff_queue_is_empty(&revs->diffopt);
166         saved_fmt = revs->diffopt.output_format;
167         revs->diffopt.output_format = DIFF_FORMAT_CALLBACK;
168         revs->diffopt.format_callback = cgit_diff_tree_cb;
169         revs->diffopt.format_callback_data = handle_rename;
170         diff_flush(&revs->diffopt);
171         revs->diffopt.output_format = saved_fmt;
172         revs->diffopt.flags = saved_flags;
173
174         lines_counted = 1;
175         return found;
176 }
177
178 static void print_commit(struct commit *commit, struct rev_info *revs)
179 {
180         struct commitinfo *info;
181         int columns = revs->graph ? 4 : 3;
182         struct strbuf graphbuf = STRBUF_INIT;
183         struct strbuf msgbuf = STRBUF_INIT;
184
185         if (ctx.repo->enable_log_filecount)
186                 columns++;
187         if (ctx.repo->enable_log_linecount)
188                 columns++;
189
190         if (revs->graph) {
191                 /* Advance graph until current commit */
192                 while (!graph_next_line(revs->graph, &graphbuf)) {
193                         /* Print graph segment in otherwise empty table row */
194                         html("<tr class='nohover'><td class='commitgraph'>");
195                         html(graphbuf.buf);
196                         htmlf("</td><td colspan='%d' /></tr>\n", columns);
197                         strbuf_setlen(&graphbuf, 0);
198                 }
199                 /* Current commit's graph segment is now ready in graphbuf */
200         }
201
202         info = cgit_parse_commit(commit);
203         htmlf("<tr%s>", ctx.qry.showmsg ? " class='logheader'" : "");
204
205         if (revs->graph) {
206                 /* Print graph segment for current commit */
207                 html("<td class='commitgraph'>");
208                 html(graphbuf.buf);
209                 html("</td>");
210                 strbuf_setlen(&graphbuf, 0);
211         }
212         else {
213                 html("<td>");
214                 cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2);
215                 html("</td>");
216         }
217
218         htmlf("<td%s>", ctx.qry.showmsg ? " class='logsubject'" : "");
219         if (ctx.qry.showmsg) {
220                 /* line-wrap long commit subjects instead of truncating them */
221                 size_t subject_len = strlen(info->subject);
222
223                 if (subject_len > ctx.cfg.max_msg_len &&
224                     ctx.cfg.max_msg_len >= 15) {
225                         /* symbol for signaling line-wrap (in PAGE_ENCODING) */
226                         const char wrap_symbol[] = { ' ', 0xE2, 0x86, 0xB5, 0 };
227                         int i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
228
229                         /* Rewind i to preceding space character */
230                         while (i > 0 && !isspace(info->subject[i]))
231                                 --i;
232                         if (!i) /* Oops, zero spaces. Reset i */
233                                 i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
234
235                         /* add remainder starting at i to msgbuf */
236                         strbuf_add(&msgbuf, info->subject + i, subject_len - i);
237                         strbuf_trim(&msgbuf);
238                         strbuf_add(&msgbuf, "\n\n", 2);
239
240                         /* Place wrap_symbol at position i in info->subject */
241                         strlcpy(info->subject + i, wrap_symbol, subject_len - i + 1);
242                 }
243         }
244         cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
245                          oid_to_hex(&commit->object.oid), ctx.qry.vpath);
246         show_commit_decorations(commit);
247         html("</td><td>");
248         cgit_open_filter(ctx.repo->email_filter, info->author_email, "log");
249         html_txt(info->author);
250         cgit_close_filter(ctx.repo->email_filter);
251
252         if (revs->graph) {
253                 html("</td><td>");
254                 cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2);
255         }
256
257         if (!lines_counted && (ctx.repo->enable_log_filecount ||
258                                ctx.repo->enable_log_linecount)) {
259                 files = 0;
260                 add_lines = 0;
261                 rem_lines = 0;
262                 cgit_diff_commit(commit, inspect_files, ctx.qry.vpath);
263         }
264
265         if (ctx.repo->enable_log_filecount)
266                 htmlf("</td><td>%d", files);
267         if (ctx.repo->enable_log_linecount)
268                 htmlf("</td><td><span class='deletions'>-%d</span>/"
269                         "<span class='insertions'>+%d</span>", rem_lines, add_lines);
270
271         html("</td></tr>\n");
272
273         if ((revs->graph && !graph_is_commit_finished(revs->graph))
274                         || ctx.qry.showmsg) { /* Print a second table row */
275                 html("<tr class='nohover-highlight'>");
276
277                 if (ctx.qry.showmsg) {
278                         /* Concatenate commit message + notes in msgbuf */
279                         if (info->msg && *(info->msg)) {
280                                 strbuf_addstr(&msgbuf, info->msg);
281                                 strbuf_addch(&msgbuf, '\n');
282                         }
283                         format_display_notes(&commit->object.oid,
284                                              &msgbuf, PAGE_ENCODING, 0);
285                         strbuf_addch(&msgbuf, '\n');
286                         strbuf_ltrim(&msgbuf);
287                 }
288
289                 if (revs->graph) {
290                         int lines = 0;
291
292                         /* Calculate graph padding */
293                         if (ctx.qry.showmsg) {
294                                 /* Count #lines in commit message + notes */
295                                 const char *p = msgbuf.buf;
296                                 lines = 1;
297                                 while ((p = strchr(p, '\n'))) {
298                                         p++;
299                                         lines++;
300                                 }
301                         }
302
303                         /* Print graph padding */
304                         html("<td class='commitgraph'>");
305                         while (lines > 0 || !graph_is_commit_finished(revs->graph)) {
306                                 if (graphbuf.len)
307                                         html("\n");
308                                 strbuf_setlen(&graphbuf, 0);
309                                 graph_next_line(revs->graph, &graphbuf);
310                                 html(graphbuf.buf);
311                                 lines--;
312                         }
313                         html("</td>\n");
314                 }
315                 else
316                         html("<td/>"); /* Empty 'Age' column */
317
318                 /* Print msgbuf into remainder of table row */
319                 htmlf("<td colspan='%d'%s>\n", columns - (revs->graph ? 1 : 0),
320                         ctx.qry.showmsg ? " class='logmsg'" : "");
321                 html_txt(msgbuf.buf);
322                 html("</td></tr>\n");
323         }
324
325         strbuf_release(&msgbuf);
326         strbuf_release(&graphbuf);
327         cgit_free_commitinfo(info);
328 }
329
330 static const char *disambiguate_ref(const char *ref, int *must_free_result)
331 {
332         struct object_id oid;
333         struct strbuf longref = STRBUF_INIT;
334
335         strbuf_addf(&longref, "refs/heads/%s", ref);
336         if (repo_get_oid(the_repository, longref.buf, &oid) == 0) {
337                 *must_free_result = 1;
338                 return strbuf_detach(&longref, NULL);
339         }
340
341         *must_free_result = 0;
342         strbuf_release(&longref);
343         return ref;
344 }
345
346 static char *next_token(char **src)
347 {
348         char *result;
349
350         if (!src || !*src)
351                 return NULL;
352         while (isspace(**src))
353                 (*src)++;
354         if (!**src)
355                 return NULL;
356         result = *src;
357         while (**src) {
358                 if (isspace(**src)) {
359                         **src = '\0';
360                         (*src)++;
361                         break;
362                 }
363                 (*src)++;
364         }
365         return result;
366 }
367
368 void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
369                     const char *path, int pager, int commit_graph, int commit_sort)
370 {
371         struct rev_info rev;
372         struct commit *commit;
373         struct strvec rev_argv = STRVEC_INIT;
374         int i, columns = commit_graph ? 4 : 3;
375         int must_free_tip = 0;
376
377         /* rev_argv.argv[0] will be ignored by setup_revisions */
378         strvec_push(&rev_argv, "log_rev_setup");
379
380         if (!tip)
381                 tip = ctx.qry.head;
382         tip = disambiguate_ref(tip, &must_free_tip);
383         strvec_push(&rev_argv, tip);
384
385         if (grep && pattern && *pattern) {
386                 pattern = xstrdup(pattern);
387                 if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
388                     !strcmp(grep, "committer")) {
389                         strvec_pushf(&rev_argv, "--%s=%s", grep, pattern);
390                 } else if (!strcmp(grep, "range")) {
391                         char *arg;
392                         /* Split the pattern at whitespace and add each token
393                          * as a revision expression. Do not accept other
394                          * rev-list options. Also, replace the previously
395                          * pushed tip (it's no longer relevant).
396                          */
397                         strvec_pop(&rev_argv);
398                         while ((arg = next_token(&pattern))) {
399                                 if (*arg == '-') {
400                                         fprintf(stderr, "Bad range expr: %s\n",
401                                                 arg);
402                                         break;
403                                 }
404                                 strvec_push(&rev_argv, arg);
405                         }
406                 }
407         }
408
409         if (!path || !ctx.cfg.enable_follow_links) {
410                 /*
411                  * If we don't have a path, "follow" is a no-op so make sure
412                  * the variable is set to false to avoid needing to check
413                  * both this and whether we have a path everywhere.
414                  */
415                 ctx.qry.follow = 0;
416         }
417
418         if (commit_graph && !ctx.qry.follow) {
419                 strvec_push(&rev_argv, "--graph");
420                 strvec_push(&rev_argv, "--color");
421                 graph_set_column_colors(column_colors_html,
422                                         COLUMN_COLORS_HTML_MAX);
423         }
424
425         if (commit_sort == 1)
426                 strvec_push(&rev_argv, "--date-order");
427         else if (commit_sort == 2)
428                 strvec_push(&rev_argv, "--topo-order");
429
430         if (path && ctx.qry.follow)
431                 strvec_push(&rev_argv, "--follow");
432         strvec_push(&rev_argv, "--");
433         if (path)
434                 strvec_push(&rev_argv, path);
435
436         repo_init_revisions(the_repository, &rev, NULL);
437         rev.abbrev = DEFAULT_ABBREV;
438         rev.commit_format = CMIT_FMT_DEFAULT;
439         rev.verbose_header = 1;
440         rev.show_root_diff = 0;
441         rev.ignore_missing = 1;
442         rev.simplify_history = 1;
443         setup_revisions(rev_argv.nr, rev_argv.v, &rev, NULL);
444         load_ref_decorations(NULL, DECORATE_FULL_REFS);
445         rev.show_decorations = 1;
446         rev.grep_filter.ignore_case = 1;
447
448         rev.diffopt.detect_rename = 1;
449         rev.diffopt.rename_limit = ctx.cfg.renamelimit;
450         if (ctx.qry.ignorews)
451                 DIFF_XDL_SET(&rev.diffopt, IGNORE_WHITESPACE);
452
453         compile_grep_patterns(&rev.grep_filter);
454         prepare_revision_walk(&rev);
455
456         if (pager) {
457                 cgit_print_layout_start();
458                 html("<table class='list nowrap'>");
459         }
460
461         html("<tr class='nohover'>");
462         if (commit_graph)
463                 html("<th></th>");
464         else
465                 html("<th class='left'>Age</th>");
466         html("<th class='left'>Commit message");
467         if (pager) {
468                 html(" (");
469                 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
470                               NULL, ctx.qry.head, ctx.qry.oid,
471                               ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
472                               ctx.qry.search, ctx.qry.showmsg ? 0 : 1,
473                               ctx.qry.follow);
474                 html(")");
475         }
476         html("</th><th class='left'>Author</th>");
477         if (rev.graph)
478                 html("<th class='left'>Age</th>");
479         if (ctx.repo->enable_log_filecount) {
480                 html("<th class='left'>Files</th>");
481                 columns++;
482         }
483         if (ctx.repo->enable_log_linecount) {
484                 html("<th class='left'>Lines</th>");
485                 columns++;
486         }
487         html("</tr>\n");
488
489         if (ofs<0)
490                 ofs = 0;
491
492         for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; /* nop */) {
493                 if (show_commit(commit, &rev))
494                         i++;
495                 release_commit_memory(the_repository->parsed_objects, commit);
496                 commit->parents = NULL;
497         }
498
499         for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; /* nop */) {
500                 /*
501                  * In "follow" mode, we must count the files and lines the
502                  * first time we invoke diff on a given commit, and we need
503                  * to do that to see if the commit touches the path we care
504                  * about, so we do it in show_commit.  Hence we must clear
505                  * lines_counted here.
506                  *
507                  * This has the side effect of avoiding running diff twice
508                  * when we are both following renames and showing file
509                  * and/or line counts.
510                  */
511                 lines_counted = 0;
512                 if (show_commit(commit, &rev)) {
513                         i++;
514                         print_commit(commit, &rev);
515                 }
516                 release_commit_memory(the_repository->parsed_objects, commit);
517                 commit->parents = NULL;
518         }
519         if (pager) {
520                 html("</table><ul class='pager'>");
521                 if (ofs > 0) {
522                         html("<li>");
523                         cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
524                                       ctx.qry.oid, ctx.qry.vpath,
525                                       ofs - cnt, ctx.qry.grep,
526                                       ctx.qry.search, ctx.qry.showmsg,
527                                       ctx.qry.follow);
528                         html("</li>");
529                 }
530                 if ((commit = get_revision(&rev)) != NULL) {
531                         html("<li>");
532                         cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
533                                       ctx.qry.oid, ctx.qry.vpath,
534                                       ofs + cnt, ctx.qry.grep,
535                                       ctx.qry.search, ctx.qry.showmsg,
536                                       ctx.qry.follow);
537                         html("</li>");
538                 }
539                 html("</ul>");
540                 cgit_print_layout_end();
541         } else if ((commit = get_revision(&rev)) != NULL) {
542                 htmlf("<tr class='nohover'><td colspan='%d'>", columns);
543                 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
544                               ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg,
545                               ctx.qry.follow);
546                 html("</td></tr>\n");
547         }
548
549         /* If we allocated tip then it is safe to cast away const. */
550         if (must_free_tip)
551                 free((char*) tip);
552 }