all repos — cgit @ ed3497b0de6634350cd17b320538fba918d4084c

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("Bad object id: %s", new_rev);
29		return;
30	}
31	commit = lookup_commit_reference(new_rev_sha1);
32	if (!commit) {
33		cgit_print_error("Bad commit reference: %s", new_rev);
34		return;
35	}
36
37	if (old_rev) {
38		if (get_sha1(old_rev, old_rev_sha1)) {
39			cgit_print_error("Bad object id: %s", old_rev);
40			return;
41		}
42		if (!lookup_commit_reference(old_rev_sha1)) {
43			cgit_print_error("Bad commit reference: %s", old_rev);
44			return;
45		}
46	} else if (commit->parents && commit->parents->item) {
47		hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
48	} else {
49		hashclr(old_rev_sha1);
50	}
51
52	if (is_null_sha1(old_rev_sha1)) {
53		memcpy(rev_range, sha1_to_hex(new_rev_sha1), 41);
54	} else {
55		sprintf(rev_range, "%s..%s", sha1_to_hex(old_rev_sha1),
56			sha1_to_hex(new_rev_sha1));
57	}
58
59	patchname = fmt("%s.patch", rev_range);
60	ctx.page.mimetype = "text/plain";
61	ctx.page.filename = patchname;
62	cgit_print_http_headers(&ctx);
63
64	if (ctx.cfg.noplainemail) {
65		rev_argv[2] = "--format=format:From %H Mon Sep 17 00:00:00 "
66			      "2001%nFrom: %an%nDate: %aD%n%w(78,0,1)Subject: "
67			      "%s%n%n%w(0)%b";
68	}
69
70	init_revisions(&rev, NULL);
71	rev.abbrev = DEFAULT_ABBREV;
72	rev.verbose_header = 1;
73	rev.diff = 1;
74	rev.show_root_diff = 1;
75	rev.max_parents = 1;
76	rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
77	setup_revisions(ARRAY_SIZE(rev_argv), (const char **)rev_argv, &rev,
78			NULL);
79	prepare_revision_walk(&rev);
80
81	while ((commit = get_revision(&rev)) != NULL) {
82		log_tree_commit(&rev, commit);
83		printf("-- \ncgit %s\n\n", cgit_version);
84	}
85}