1 /* ui-patch.c: generate patch view
3 * Copyright (C) 2007 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 void cgit_print_patch(char *hex, const char *old_rev, const char *prefix)
17 struct commit *commit;
18 unsigned char sha1[20], old_sha1[20];
19 char rev_range[2 * 40 + 3];
20 char *rev_argv[] = { NULL, "--reverse", "--format=email", rev_range };
26 if (get_sha1(hex, sha1)) {
27 cgit_print_error("Bad object id: %s", hex);
30 commit = lookup_commit_reference(sha1);
32 cgit_print_error("Bad commit reference: %s", hex);
37 if (get_sha1(old_rev, old_sha1)) {
38 cgit_print_error("Bad object id: %s", old_rev);
41 if (!lookup_commit_reference(old_sha1)) {
42 cgit_print_error("Bad commit reference: %s", old_rev);
45 } else if (commit->parents && commit->parents->item) {
46 hashcpy(old_sha1, commit->parents->item->object.sha1);
51 if (is_null_sha1(old_sha1)) {
52 memcpy(rev_range, sha1_to_hex(sha1), 41);
54 sprintf(rev_range, "%s..%s", sha1_to_hex(old_sha1),
58 patchname = fmt("%s.patch", rev_range);
59 ctx.page.mimetype = "text/plain";
60 ctx.page.filename = patchname;
61 cgit_print_http_headers(&ctx);
63 if (ctx.cfg.noplainemail) {
64 rev_argv[2] = "--format=format:From %H Mon Sep 17 00:00:00 "
65 "2001%nFrom: %an%nDate: %aD%n%w(78,0,1)Subject: "
69 init_revisions(&rev, NULL);
70 rev.abbrev = DEFAULT_ABBREV;
71 rev.verbose_header = 1;
73 rev.show_root_diff = 1;
74 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
75 setup_revisions(ARRAY_SIZE(rev_argv), (const char **)rev_argv, &rev,
77 prepare_revision_walk(&rev);
79 while ((commit = get_revision(&rev)) != NULL) {
80 log_tree_commit(&rev, commit);
81 printf("--\ncgit %s\n", cgit_version);