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 rev_info rev;
17 struct commit *commit;
18 unsigned char sha1[20], old_sha1[20];
19 char rev_range[2 * 40 + 3];
20 char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range };
21 char *patchname;
22
23 if (!hex)
24 hex = ctx.qry.head;
25
26 if (get_sha1(hex, sha1)) {
27 cgit_print_error("Bad object id: %s", hex);
28 return;
29 }
30 commit = lookup_commit_reference(sha1);
31 if (!commit) {
32 cgit_print_error("Bad commit reference: %s", hex);
33 return;
34 }
35
36 if (commit->parents && commit->parents->item) {
37 hashcpy(old_sha1, commit->parents->item->object.sha1);
38 sprintf(rev_range, "%s..%s", sha1_to_hex(old_sha1),
39 sha1_to_hex(sha1));
40 } else {
41 hashclr(old_sha1);
42 memcpy(rev_range, sha1_to_hex(sha1), 41);
43 }
44
45 patchname = fmt("%s.patch", sha1_to_hex(sha1));
46 ctx.page.mimetype = "text/plain";
47 ctx.page.filename = patchname;
48 cgit_print_http_headers(&ctx);
49
50 if (ctx.cfg.noplainemail) {
51 rev_argv[2] = "--format=format:From %H Mon Sep 17 00:00:00 "
52 "2001%nFrom: %an%nDate: %aD%n%w(78,0,1)Subject: "
53 "%s%n%n%w(0)%b";
54 }
55
56 init_revisions(&rev, NULL);
57 rev.abbrev = DEFAULT_ABBREV;
58 rev.verbose_header = 1;
59 rev.diff = 1;
60 rev.show_root_diff = 1;
61 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
62 setup_revisions(ARRAY_SIZE(rev_argv), (const char **)rev_argv, &rev,
63 NULL);
64 prepare_revision_walk(&rev);
65
66 while ((commit = get_revision(&rev)) != NULL) {
67 log_tree_commit(&rev, commit);
68 printf("--\ncgit %s\n", cgit_version);
69 }
70}