all repos — cgit @ 6c5d4fefac1364a38686133862d5a128ec0fb944

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
14/* two commit hashes with two dots in between and termination */
15#define REV_RANGE_LEN 2 * GIT_MAX_HEXSZ + 3
16
17void cgit_print_patch(const char *new_rev, const char *old_rev,
18		      const char *prefix)
19{
20	struct rev_info rev;
21	struct commit *commit;
22	struct object_id new_rev_oid, old_rev_oid;
23	char rev_range[REV_RANGE_LEN];
24	const char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range, "--", prefix, NULL };
25	int rev_argc = ARRAY_SIZE(rev_argv) - 1;
26	char *patchname;
27
28	if (!prefix)
29		rev_argc--;
30
31	if (!new_rev)
32		new_rev = ctx.qry.head;
33
34	if (get_oid(new_rev, &new_rev_oid)) {
35		cgit_print_error_page(404, "Not found",
36				"Bad object id: %s", new_rev);
37		return;
38	}
39	commit = lookup_commit_reference(the_repository, &new_rev_oid);
40	if (!commit) {
41		cgit_print_error_page(404, "Not found",
42				"Bad commit reference: %s", new_rev);
43		return;
44	}
45
46	if (old_rev) {
47		if (get_oid(old_rev, &old_rev_oid)) {
48			cgit_print_error_page(404, "Not found",
49					"Bad object id: %s", old_rev);
50			return;
51		}
52		if (!lookup_commit_reference(the_repository, &old_rev_oid)) {
53			cgit_print_error_page(404, "Not found",
54					"Bad commit reference: %s", old_rev);
55			return;
56		}
57	} else if (commit->parents && commit->parents->item) {
58		oidcpy(&old_rev_oid, &commit->parents->item->object.oid);
59	} else {
60		oidclr(&old_rev_oid);
61	}
62
63	if (is_null_oid(&old_rev_oid)) {
64		memcpy(rev_range, oid_to_hex(&new_rev_oid), the_hash_algo->hexsz + 1);
65	} else {
66		xsnprintf(rev_range, REV_RANGE_LEN, "%s..%s", oid_to_hex(&old_rev_oid),
67			oid_to_hex(&new_rev_oid));
68	}
69
70	patchname = fmt("%s.patch", rev_range);
71	ctx.page.mimetype = "text/plain";
72	ctx.page.filename = patchname;
73	cgit_print_http_headers();
74
75	if (ctx.cfg.noplainemail) {
76		rev_argv[2] = "--format=format:From %H Mon Sep 17 00:00:00 "
77			      "2001%nFrom: %an%nDate: %aD%n%w(78,0,1)Subject: "
78			      "%s%n%n%w(0)%b";
79	}
80
81	init_revisions(&rev, NULL);
82	rev.abbrev = DEFAULT_ABBREV;
83	rev.verbose_header = 1;
84	rev.diff = 1;
85	rev.show_root_diff = 1;
86	rev.max_parents = 1;
87	rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT |
88			DIFF_FORMAT_PATCH | DIFF_FORMAT_SUMMARY;
89	if (prefix)
90		rev.diffopt.stat_sep = fmt("(limited to '%s')\n\n", prefix);
91	setup_revisions(rev_argc, rev_argv, &rev, NULL);
92	prepare_revision_walk(&rev);
93
94	while ((commit = get_revision(&rev)) != NULL) {
95		log_tree_commit(&rev, commit);
96		printf("-- \ncgit %s\n\n", cgit_version);
97	}
98}