blob: 3922a4411772dcf3934e1b67d9b8aff0de839383 [file] [log] [blame]
Lars Hjemli620bb3e2007-12-10 21:47:29 +01001/* 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"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-patch.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemli620bb3e2007-12-10 21:47:29 +010013
Johan Herlandeac1b672010-06-10 01:09:33 +020014void cgit_print_patch(char *hex, const char *prefix)
Lars Hjemli620bb3e2007-12-10 21:47:29 +010015{
16 struct commit *commit;
17 struct commitinfo *info;
18 unsigned char sha1[20], old_sha1[20];
19 char *patchname;
20
21 if (!hex)
Lars Hjemlid14d77f2008-02-16 11:53:40 +010022 hex = ctx.qry.head;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010023
24 if (get_sha1(hex, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +010025 cgit_print_error("Bad object id: %s", hex);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010026 return;
27 }
28 commit = lookup_commit_reference(sha1);
29 if (!commit) {
John Keepinged5bd302013-04-06 11:23:52 +010030 cgit_print_error("Bad commit reference: %s", hex);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010031 return;
32 }
33 info = cgit_parse_commit(commit);
Lars Hjemlie01f1402008-03-17 23:13:16 +010034
35 if (commit->parents && commit->parents->item)
36 hashcpy(old_sha1, commit->parents->item->object.sha1);
37 else
38 hashclr(old_sha1);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010039
40 patchname = fmt("%s.patch", sha1_to_hex(sha1));
Lars Hjemlif3c1a182008-03-24 00:51:19 +010041 ctx.page.mimetype = "text/plain";
42 ctx.page.filename = patchname;
43 cgit_print_http_headers(&ctx);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010044 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1));
Martin Szulecki2f56e392009-08-07 14:05:17 +020045 htmlf("From: %s", info->author);
46 if (!ctx.cfg.noplainemail) {
47 htmlf(" %s", info->author_email);
48 }
49 html("\n");
Lars Hjemli620bb3e2007-12-10 21:47:29 +010050 html("Date: ");
Tomas Carnecky03afc5f2008-12-30 11:14:52 +010051 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n", ctx.cfg.local_time);
Lars Hjemlif39c3c92007-12-18 08:26:50 +000052 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 }
Lars Hjemli620bb3e2007-12-10 21:47:29 +010058 html("---\n");
Johan Herlandeac1b672010-06-10 01:09:33 +020059 if (prefix)
60 htmlf("(limited to '%s')\n\n", prefix);
Lukas Fleischer747b0352013-08-14 10:50:31 +020061 cgit_diff_tree(old_sha1, sha1, filepair_cb_raw, prefix, 0);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010062 html("--\n");
John Keeping1a6feaf2013-03-06 21:22:07 +000063 htmlf("cgit %s\n", cgit_version);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010064 cgit_free_commitinfo(info);
65}