all repos — cgit @ a0d22c391e6e2bbe0d10a888df1ba77a468d5a18

a hyperfast web frontend for git written in c

ui-patch.c (view raw)

 1/* ui-patch.c: generate patch view
 2 *
 3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
 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(const char *new_rev, const char *old_rev,
15		      const char *prefix)
16{
17	struct rev_info rev;
18	struct commit *commit;
19	unsigned char new_rev_sha1[20], old_rev_sha1[20];
20	char rev_range[2 * 40 + 3];
21	char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range };
22	char *patchname;
23
24	if (!new_rev)
25		new_rev = ctx.qry.head;
26
27	if (get_sha1(new_rev, new_rev_sha1)) {
28		cgit_print_error_page(404, "Not found",
29				"Bad object id: %s", new_rev);
30		return;
31	}
32	commit = lookup_commit_reference(new_rev_sha1);
33	if (!commit) {
34		cgit_print_error_page(404, "Not found",
35				"Bad commit reference: %s", new_rev);
36		return;
37	}
38
39	if (old_rev) {
40		if (get_sha1(old_rev, old_rev_sha1)) {
41			cgit_print_error_page(404, "Not found",
42					"Bad object id: %s", old_rev);
43			return;
44		}
45		if (!lookup_commit_reference(old_rev_sha1)) {
46			cgit_print_error_page(404, "Not found",
47					"Bad commit reference: %s", old_rev);
48			return;
49		}
50	} else if (commit->parents && commit->parents->item) {
51		hashcpy(old_rev_sha1, commit->parents->item->object.oid.hash);
52	} else {
53		hashclr(old_rev_sha1);
54	}
55
56	if (is_null_sha1(old_rev_sha1)) {
57		memcpy(rev_range, sha1_to_hex(new_rev_sha1), 41);
58	} else {
59		sprintf(rev_range, "%s..%s", sha1_to_hex(old_rev_sha1),
60			sha1_to_hex(new_rev_sha1));
61	}
62
63	patchname = fmt("%s.patch", rev_range);
64	ctx.page.mimetype = "text/plain";
65	ctx.page.filename = patchname;
66	cgit_print_http_headers();
67
68	if (ctx.cfg.noplainemail) {
69		rev_argv[2] = "--format=format:From %H Mon Sep 17 00:00:00 "
70			      "2001%nFrom: %an%nDate: %aD%n%w(78,0,1)Subject: "
71			      "%s%n%n%w(0)%b";
72	}
73
74	init_revisions(&rev, NULL);
75	rev.abbrev = DEFAULT_ABBREV;
76	rev.verbose_header = 1;
77	rev.diff = 1;
78	rev.show_root_diff = 1;
79	rev.max_parents = 1;
80	rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT |
81			DIFF_FORMAT_PATCH | DIFF_FORMAT_SUMMARY;
82	setup_revisions(ARRAY_SIZE(rev_argv), (const char **)rev_argv, &rev,
83			NULL);
84	prepare_revision_walk(&rev);
85
86	while ((commit = get_revision(&rev)) != NULL) {
87		log_tree_commit(&rev, commit);
88		printf("-- \ncgit %s\n\n", cgit_version);
89	}
90
91	fflush(stdout);
92}