]>
git.cameronkatri.com Git - cgit.git/blob - ui-commit.c
1 /* ui-commit.c: generate commit view
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include "ui-shared.h"
13 static int files
, slots
;
14 static int total_adds
, total_rems
, max_changes
;
15 static int lines_added
, lines_removed
;
16 static char *curr_rev
;
18 static struct fileinfo
{
20 unsigned char old_sha1
[20];
21 unsigned char new_sha1
[20];
22 unsigned short old_mode
;
23 unsigned short new_mode
;
31 void print_fileinfo(struct fileinfo
*info
)
35 switch (info
->status
) {
36 case DIFF_STATUS_ADDED
:
39 case DIFF_STATUS_COPIED
:
42 case DIFF_STATUS_DELETED
:
45 case DIFF_STATUS_MODIFIED
:
48 case DIFF_STATUS_RENAMED
:
51 case DIFF_STATUS_TYPE_CHANGED
:
54 case DIFF_STATUS_UNKNOWN
:
57 case DIFF_STATUS_UNMERGED
:
61 die("bug: unhandled diff status %c", info
->status
);
65 htmlf("<td class='mode'>");
66 if (is_null_sha1(info
->new_sha1
)) {
67 cgit_print_filemode(info
->old_mode
);
69 cgit_print_filemode(info
->new_mode
);
72 if (info
->old_mode
!= info
->new_mode
&&
73 !is_null_sha1(info
->old_sha1
) &&
74 !is_null_sha1(info
->new_sha1
)) {
75 html("<span class='modechange'>[");
76 cgit_print_filemode(info
->old_mode
);
79 htmlf("</td><td class='%s'>", class);
80 cgit_diff_link(info
->new_path
, NULL
, NULL
, ctx
.qry
.head
, curr_rev
,
81 NULL
, info
->new_path
);
82 if (info
->status
== DIFF_STATUS_COPIED
|| info
->status
== DIFF_STATUS_RENAMED
)
83 htmlf(" (%s from %s)",
84 info
->status
== DIFF_STATUS_COPIED
? "copied" : "renamed",
86 html("</td><td class='right'>");
87 htmlf("%d", info
->added
+ info
->removed
);
88 html("</td><td class='graph'>");
89 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes
> 100 ? 100 : max_changes
));
90 htmlf("<td class='add' style='width: %.1f%%;'/>",
91 info
->added
* 100.0 / max_changes
);
92 htmlf("<td class='rem' style='width: %.1f%%;'/>",
93 info
->removed
* 100.0 / max_changes
);
94 htmlf("<td class='none' style='width: %.1f%%;'/>",
95 (max_changes
- info
->removed
- info
->added
) * 100.0 / max_changes
);
96 html("</tr></table></td></tr>\n");
99 void cgit_count_diff_lines(char *line
, int len
)
101 if (line
&& (len
> 0)) {
104 else if (line
[0] == '-')
109 void inspect_filepair(struct diff_filepair
*pair
)
114 cgit_diff_files(pair
->one
->sha1
, pair
->two
->sha1
, cgit_count_diff_lines
);
115 if (files
>= slots
) {
120 items
= xrealloc(items
, slots
* sizeof(struct fileinfo
));
122 items
[files
-1].status
= pair
->status
;
123 hashcpy(items
[files
-1].old_sha1
, pair
->one
->sha1
);
124 hashcpy(items
[files
-1].new_sha1
, pair
->two
->sha1
);
125 items
[files
-1].old_mode
= pair
->one
->mode
;
126 items
[files
-1].new_mode
= pair
->two
->mode
;
127 items
[files
-1].old_path
= xstrdup(pair
->one
->path
);
128 items
[files
-1].new_path
= xstrdup(pair
->two
->path
);
129 items
[files
-1].added
= lines_added
;
130 items
[files
-1].removed
= lines_removed
;
131 if (lines_added
+ lines_removed
> max_changes
)
132 max_changes
= lines_added
+ lines_removed
;
133 total_adds
+= lines_added
;
134 total_rems
+= lines_removed
;
138 void cgit_print_commit(char *hex
)
140 struct commit
*commit
, *parent
;
141 struct commitinfo
*info
;
142 struct commit_list
*p
;
143 unsigned char sha1
[20];
151 if (get_sha1(hex
, sha1
)) {
152 cgit_print_error(fmt("Bad object id: %s", hex
));
155 commit
= lookup_commit_reference(sha1
);
157 cgit_print_error(fmt("Bad commit reference: %s", hex
));
160 info
= cgit_parse_commit(commit
);
162 html("<table summary='commit info' class='commit-info'>\n");
163 html("<tr><th>author</th><td>");
164 html_txt(info
->author
);
166 html_txt(info
->author_email
);
167 html("</td><td class='right'>");
168 cgit_print_date(info
->author_date
, FMT_LONGDATE
);
169 html("</td></tr>\n");
170 html("<tr><th>committer</th><td>");
171 html_txt(info
->committer
);
173 html_txt(info
->committer_email
);
174 html("</td><td class='right'>");
175 cgit_print_date(info
->committer_date
, FMT_LONGDATE
);
176 html("</td></tr>\n");
177 html("<tr><th>tree</th><td colspan='2' class='sha1'>");
179 cgit_tree_link(sha1_to_hex(commit
->tree
->object
.sha1
), NULL
, NULL
,
180 ctx
.qry
.head
, tmp
, NULL
);
181 html("</td></tr>\n");
182 for (p
= commit
->parents
; p
; p
= p
->next
) {
183 parent
= lookup_commit_reference(p
->item
->object
.sha1
);
185 html("<tr><td colspan='3'>");
186 cgit_print_error("Error reading parent commit");
190 html("<tr><th>parent</th>"
191 "<td colspan='2' class='sha1'>");
192 cgit_commit_link(sha1_to_hex(p
->item
->object
.sha1
), NULL
, NULL
,
193 ctx
.qry
.head
, sha1_to_hex(p
->item
->object
.sha1
));
195 cgit_diff_link("diff", NULL
, NULL
, ctx
.qry
.head
, hex
,
196 sha1_to_hex(p
->item
->object
.sha1
), NULL
);
199 if (ctx
.repo
->snapshots
) {
200 html("<tr><th>download</th><td colspan='2' class='sha1'>");
201 cgit_print_snapshot_links(ctx
.qry
.repo
, ctx
.qry
.head
,
202 hex
, ctx
.repo
->snapshots
);
206 html("<div class='commit-subject'>");
207 html_txt(info
->subject
);
209 html("<div class='commit-msg'>");
212 if (!(commit
->parents
&& commit
->parents
->next
&& commit
->parents
->next
->next
)) {
213 html("<div class='diffstat-header'>Diffstat</div>");
214 html("<table summary='diffstat' class='diffstat'>");
216 cgit_diff_commit(commit
, inspect_filepair
);
217 for(i
= 0; i
<files
; i
++)
218 print_fileinfo(&items
[i
]);
220 html("<div class='diffstat-summary'>");
221 htmlf("%d files changed, %d insertions, %d deletions (",
222 files
, total_adds
, total_rems
);
223 cgit_diff_link("show diff", NULL
, NULL
, ctx
.qry
.head
, hex
,
227 cgit_free_commitinfo(info
);