]> gitweb.ps.run Git - ps-cgit/blob - ui-log.c
Merge branch 'snapshot-fixes'
[ps-cgit] / ui-log.c
1 /* ui-log.c: functions for log output
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "html.h"
11 #include "ui-shared.h"
12
13 int files, add_lines, rem_lines;
14
15 void count_lines(char *line, int size)
16 {
17         if (size <= 0)
18                 return;
19
20         if (line[0] == '+')
21                 add_lines++;
22
23         else if (line[0] == '-')
24                 rem_lines++;
25 }
26
27 void inspect_files(struct diff_filepair *pair)
28 {
29         files++;
30         if (ctx.repo->enable_log_linecount)
31                 cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines);
32 }
33
34 void print_commit(struct commit *commit)
35 {
36         struct commitinfo *info;
37         char *tmp;
38
39         info = cgit_parse_commit(commit);
40         html("<tr><td>");
41         tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
42         tmp = cgit_pageurl(ctx.repo->url, "commit", tmp);
43         html_link_open(tmp, NULL, NULL);
44         cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
45         html_link_close();
46         html("</td><td>");
47         cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
48                          sha1_to_hex(commit->object.sha1));
49         html("</td><td>");
50         html_txt(info->author);
51         if (ctx.repo->enable_log_filecount) {
52                 files = 0;
53                 add_lines = 0;
54                 rem_lines = 0;
55                 cgit_diff_commit(commit, inspect_files);
56                 html("</td><td>");
57                 htmlf("%d", files);
58                 if (ctx.repo->enable_log_linecount) {
59                         html("</td><td>");
60                         htmlf("-%d/+%d", rem_lines, add_lines);
61                 }
62         }
63         html("</td></tr>\n");
64         cgit_free_commitinfo(info);
65 }
66
67 static const char *disambiguate_ref(const char *ref)
68 {
69         unsigned char sha1[20];
70         const char *longref;
71
72         longref = fmt("refs/heads/%s", ref);
73         if (get_sha1(longref, sha1) == 0)
74                 return longref;
75
76         return ref;
77 }
78
79 void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
80                     char *path, int pager)
81 {
82         struct rev_info rev;
83         struct commit *commit;
84         const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
85         int argc = 2;
86         int i, columns = 3;
87
88         if (!tip)
89                 tip = ctx.qry.head;
90
91         argv[1] = disambiguate_ref(tip);
92
93         if (grep && pattern && (!strcmp(grep, "grep") ||
94                                 !strcmp(grep, "author") ||
95                                 !strcmp(grep, "committer")))
96                 argv[argc++] = fmt("--%s=%s", grep, pattern);
97
98         if (path) {
99                 argv[argc++] = "--";
100                 argv[argc++] = path;
101         }
102         init_revisions(&rev, NULL);
103         rev.abbrev = DEFAULT_ABBREV;
104         rev.commit_format = CMIT_FMT_DEFAULT;
105         rev.verbose_header = 1;
106         rev.show_root_diff = 0;
107         setup_revisions(argc, argv, &rev, NULL);
108         rev.grep_filter.regflags |= REG_ICASE;
109         compile_grep_patterns(&rev.grep_filter);
110         prepare_revision_walk(&rev);
111
112         if (pager)
113                 html("<table class='list nowrap'>");
114
115         html("<tr class='nohover'><th class='left'>Age</th>"
116              "<th class='left'>Commit message</th>"
117              "<th class='left'>Author</th>");
118         if (ctx.repo->enable_log_filecount) {
119                 html("<th class='left'>Files</th>");
120                 columns++;
121                 if (ctx.repo->enable_log_linecount) {
122                         html("<th class='left'>Lines</th>");
123                         columns++;
124                 }
125         }
126         html("</tr>\n");
127
128         if (ofs<0)
129                 ofs = 0;
130
131         for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
132                 free(commit->buffer);
133                 commit->buffer = NULL;
134                 free_commit_list(commit->parents);
135                 commit->parents = NULL;
136         }
137
138         for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
139                 print_commit(commit);
140                 free(commit->buffer);
141                 commit->buffer = NULL;
142                 free_commit_list(commit->parents);
143                 commit->parents = NULL;
144         }
145         if (pager) {
146                 htmlf("</table><div class='pager'>",
147                      columns);
148                 if (ofs > 0) {
149                         cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
150                                       ctx.qry.sha1, ctx.qry.path,
151                                       ofs - cnt, ctx.qry.grep,
152                                       ctx.qry.search);
153                         html("&nbsp;");
154                 }
155                 if ((commit = get_revision(&rev)) != NULL) {
156                         cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
157                                       ctx.qry.sha1, ctx.qry.path,
158                                       ofs + cnt, ctx.qry.grep,
159                                       ctx.qry.search);
160                 }
161                 html("</div>");
162         } else if ((commit = get_revision(&rev)) != NULL) {
163                 html("<tr class='nohover'><td colspan='3'>");
164                 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL, NULL, 0,
165                               NULL, NULL);
166                 html("</td></tr>\n");
167         }
168 }