neocgit

a more 'modern' version of cgit
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/neocgit

ui-log.c (17585B)


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