ui-patch.c (view raw)
1/* ui-patch.c: generate patch view
2 *
3 * Copyright (C) 2007 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 "ui-patch.h"
11#include "html.h"
12#include "ui-shared.h"
13
14void cgit_print_patch(char *hex, const char *prefix)
15{
16 struct commit *commit;
17 struct commitinfo *info;
18 unsigned char sha1[20], old_sha1[20];
19 char *patchname;
20
21 if (!hex)
22 hex = ctx.qry.head;
23
24 if (get_sha1(hex, sha1)) {
25 cgit_print_error("Bad object id: %s", hex);
26 return;
27 }
28 commit = lookup_commit_reference(sha1);
29 if (!commit) {
30 cgit_print_error("Bad commit reference: %s", hex);
31 return;
32 }
33 info = cgit_parse_commit(commit);
34
35 if (commit->parents && commit->parents->item)
36 hashcpy(old_sha1, commit->parents->item->object.sha1);
37 else
38 hashclr(old_sha1);
39
40 patchname = fmt("%s.patch", sha1_to_hex(sha1));
41 ctx.page.mimetype = "text/plain";
42 ctx.page.filename = patchname;
43 cgit_print_http_headers(&ctx);
44 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1));
45 htmlf("From: %s", info->author);
46 if (!ctx.cfg.noplainemail) {
47 htmlf(" %s", info->author_email);
48 }
49 html("\n");
50 html("Date: ");
51 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n", ctx.cfg.local_time);
52 htmlf("Subject: %s\n\n", info->subject);
53 if (info->msg && *info->msg) {
54 htmlf("%s", info->msg);
55 if (info->msg[strlen(info->msg) - 1] != '\n')
56 html("\n");
57 }
58 html("---\n");
59 if (prefix)
60 htmlf("(limited to '%s')\n\n", prefix);
61 cgit_diff_tree(old_sha1, sha1, filepair_cb_raw, prefix, 0);
62 html("--\n");
63 htmlf("cgit %s\n", cgit_version);
64 cgit_free_commitinfo(info);
65}