blob: 72c9749831bf167f1ad38330b8f04058dcc141bd [file] [log] [blame]
Lars Hjemli620bb3e2007-12-10 21:47:29 +01001/* 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"
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
Johan Herlandeac1b672010-06-10 01:09:33 +020014void cgit_print_patch(char *hex, const char *prefix)
Lars Hjemli620bb3e2007-12-10 21:47:29 +010015{
Lukas Fleischer455b5982013-08-20 18:56:13 +020016 struct rev_info rev;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010017 struct commit *commit;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010018 unsigned char sha1[20], old_sha1[20];
Lukas Fleischer455b5982013-08-20 18:56:13 +020019 char rev_range[2 * 40 + 3];
20 char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range };
Lars Hjemli620bb3e2007-12-10 21:47:29 +010021 char *patchname;
22
23 if (!hex)
Lars Hjemlid14d77f2008-02-16 11:53:40 +010024 hex = ctx.qry.head;
Lars Hjemli620bb3e2007-12-10 21:47:29 +010025
26 if (get_sha1(hex, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +010027 cgit_print_error("Bad object id: %s", hex);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010028 return;
29 }
30 commit = lookup_commit_reference(sha1);
31 if (!commit) {
John Keepinged5bd302013-04-06 11:23:52 +010032 cgit_print_error("Bad commit reference: %s", hex);
Lars Hjemli620bb3e2007-12-10 21:47:29 +010033 return;
34 }
Lars Hjemlie01f1402008-03-17 23:13:16 +010035
Lukas Fleischer455b5982013-08-20 18:56:13 +020036 if (commit->parents && commit->parents->item) {
Lars Hjemlie01f1402008-03-17 23:13:16 +010037 hashcpy(old_sha1, commit->parents->item->object.sha1);
Lukas Fleischer455b5982013-08-20 18:56:13 +020038 sprintf(rev_range, "%s..%s", sha1_to_hex(old_sha1),
39 sha1_to_hex(sha1));
40 } else {
Lars Hjemlie01f1402008-03-17 23:13:16 +010041 hashclr(old_sha1);
Lukas Fleischer455b5982013-08-20 18:56:13 +020042 memcpy(rev_range, sha1_to_hex(sha1), 41);
43 }
Lars Hjemli620bb3e2007-12-10 21:47:29 +010044
45 patchname = fmt("%s.patch", sha1_to_hex(sha1));
Lars Hjemlif3c1a182008-03-24 00:51:19 +010046 ctx.page.mimetype = "text/plain";
47 ctx.page.filename = patchname;
48 cgit_print_http_headers(&ctx);
Lukas Fleischer455b5982013-08-20 18:56:13 +020049
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";
Martin Szulecki2f56e392009-08-07 14:05:17 +020054 }
Lukas Fleischer455b5982013-08-20 18:56:13 +020055
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);
Lars Hjemlif39c3c92007-12-18 08:26:50 +000069 }
Lars Hjemli620bb3e2007-12-10 21:47:29 +010070}