blob: 5a964108e51e5f553536899cb6b79922e25e1288 [file] [log] [blame]
Lars Hjemli620bb3e2007-12-10 21:47:29 +01001/* ui-patch.c: generate patch view
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli620bb3e2007-12-10 21:47:29 +01004 *
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
Christian Hesseedb34032018-08-28 18:18:37 +020014/* two commit hashes with two dots in between and termination */
15#define REV_RANGE_LEN 2 * GIT_MAX_HEXSZ + 3
16
Lukas Fleischer4e00d332013-08-20 18:56:15 +020017void cgit_print_patch(const char *new_rev, const char *old_rev,
18 const char *prefix)
Lars Hjemli620bb3e2007-12-10 21:47:29 +010019{
Lukas Fleischer455b5982013-08-20 18:56:13 +020020 struct rev_info rev;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010021 struct commit *commit;
Christian Hesse85793b82016-09-29 21:51:41 +020022 struct object_id new_rev_oid, old_rev_oid;
Christian Hesseedb34032018-08-28 18:18:37 +020023 char rev_range[REV_RANGE_LEN];
Lukas Fleischerbe39d222016-11-24 20:14:54 +010024 const char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range, "--", prefix, NULL };
25 int rev_argc = ARRAY_SIZE(rev_argv) - 1;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010026 char *patchname;
27
John Keeping5fe88a92016-03-14 22:41:14 +000028 if (!prefix)
29 rev_argc--;
30
Lukas Fleischer4e00d332013-08-20 18:56:15 +020031 if (!new_rev)
32 new_rev = ctx.qry.head;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010033
Christian Hesse85793b82016-09-29 21:51:41 +020034 if (get_oid(new_rev, &new_rev_oid)) {
John Keepinge3e41e52015-08-14 12:47:07 +010035 cgit_print_error_page(404, "Not found",
36 "Bad object id: %s", new_rev);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010037 return;
38 }
Christian Hesse2c9f56f2018-08-28 18:27:00 +020039 commit = lookup_commit_reference(the_repository, &new_rev_oid);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010040 if (!commit) {
John Keepinge3e41e52015-08-14 12:47:07 +010041 cgit_print_error_page(404, "Not found",
42 "Bad commit reference: %s", new_rev);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010043 return;
44 }
Lars Hjemlie01f1402008-03-17 23:13:16 +010045
Lukas Fleischer750f6462013-08-20 18:56:14 +020046 if (old_rev) {
Christian Hesse85793b82016-09-29 21:51:41 +020047 if (get_oid(old_rev, &old_rev_oid)) {
John Keepinge3e41e52015-08-14 12:47:07 +010048 cgit_print_error_page(404, "Not found",
49 "Bad object id: %s", old_rev);
Lukas Fleischer750f6462013-08-20 18:56:14 +020050 return;
51 }
Christian Hesse2c9f56f2018-08-28 18:27:00 +020052 if (!lookup_commit_reference(the_repository, &old_rev_oid)) {
John Keepinge3e41e52015-08-14 12:47:07 +010053 cgit_print_error_page(404, "Not found",
54 "Bad commit reference: %s", old_rev);
Lukas Fleischer750f6462013-08-20 18:56:14 +020055 return;
56 }
57 } else if (commit->parents && commit->parents->item) {
Christian Hesse85793b82016-09-29 21:51:41 +020058 oidcpy(&old_rev_oid, &commit->parents->item->object.oid);
Lukas Fleischer455b5982013-08-20 18:56:13 +020059 } else {
Christian Hesse85793b82016-09-29 21:51:41 +020060 oidclr(&old_rev_oid);
Lukas Fleischer455b5982013-08-20 18:56:13 +020061 }
Lars Hjemli620bb3e2007-12-10 21:47:29 +010062
Christian Hesse85793b82016-09-29 21:51:41 +020063 if (is_null_oid(&old_rev_oid)) {
64 memcpy(rev_range, oid_to_hex(&new_rev_oid), GIT_SHA1_HEXSZ + 1);
Lukas Fleischer750f6462013-08-20 18:56:14 +020065 } else {
Christian Hesseedb34032018-08-28 18:18:37 +020066 xsnprintf(rev_range, REV_RANGE_LEN, "%s..%s", oid_to_hex(&old_rev_oid),
Christian Hesse85793b82016-09-29 21:51:41 +020067 oid_to_hex(&new_rev_oid));
Lukas Fleischer750f6462013-08-20 18:56:14 +020068 }
69
70 patchname = fmt("%s.patch", rev_range);
Lars Hjemlif3c1a182008-03-24 00:51:19 +010071 ctx.page.mimetype = "text/plain";
72 ctx.page.filename = patchname;
Lukas Fleischerf60ffa12014-01-15 21:53:15 +010073 cgit_print_http_headers();
Lukas Fleischer455b5982013-08-20 18:56:13 +020074
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";
Martin Szulecki2f56e392009-08-07 14:05:17 +020079 }
Lukas Fleischer455b5982013-08-20 18:56:13 +020080
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;
Lukas Fleischer735c7f02013-08-22 14:48:47 +020086 rev.max_parents = 1;
John Keeping75522662014-12-28 13:10:33 +000087 rev.diffopt.output_format |= DIFF_FORMAT_DIFFSTAT |
88 DIFF_FORMAT_PATCH | DIFF_FORMAT_SUMMARY;
John Keeping5fe88a92016-03-14 22:41:14 +000089 if (prefix)
90 rev.diffopt.stat_sep = fmt("(limited to '%s')\n\n", prefix);
Lukas Fleischerbe39d222016-11-24 20:14:54 +010091 setup_revisions(rev_argc, rev_argv, &rev, NULL);
Lukas Fleischer455b5982013-08-20 18:56:13 +020092 prepare_revision_walk(&rev);
93
94 while ((commit = get_revision(&rev)) != NULL) {
95 log_tree_commit(&rev, commit);
Lukas Fleischer84085852013-08-26 20:38:33 +020096 printf("-- \ncgit %s\n\n", cgit_version);
Lars Hjemlif39c3c92007-12-18 08:26:50 +000097 }
Lars Hjemli620bb3e2007-12-10 21:47:29 +010098}