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 static void print_line(char *line, int len)
23 static void header(unsigned char *sha1, char *path1, int mode1,
24 unsigned char *sha2, char *path2, int mode2)
26 char *abbrev1, *abbrev2;
29 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
30 htmlf("diff --git a/%s b/%s\n", path1, path2);
33 htmlf("new file mode %.6o\n", mode2);
36 htmlf("deleted file mode %.6o\n", mode1);
39 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
40 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
41 htmlf("index %s..%s", abbrev1, abbrev2);
44 if (mode1 != 0 && mode2 != 0) {
45 htmlf(" %.6o", mode1);
47 htmlf("..%.6o", mode2);
50 if (is_null_sha1(sha1)) {
52 htmlf("\n--- /%s\n", path1);
54 htmlf("\n--- a/%s\n", path1);
56 if (is_null_sha1(sha2)) {
58 htmlf("+++ /%s\n", path2);
60 htmlf("+++ b/%s\n", path2);
64 static void filepair_cb(struct diff_filepair *pair)
66 unsigned long old_size = 0;
67 unsigned long new_size = 0;
70 header(pair->one->sha1, pair->one->path, pair->one->mode,
71 pair->two->sha1, pair->two->path, pair->two->mode);
72 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
73 if (S_ISGITLINK(pair->one->mode))
74 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
75 if (S_ISGITLINK(pair->two->mode))
76 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
79 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
80 &new_size, &binary, 0, 0, print_line))
81 html("Error running diff");
83 html("Binary files differ\n");
86 void cgit_print_patch(char *hex, const char *prefix)
88 struct commit *commit;
89 struct commitinfo *info;
90 unsigned char sha1[20], old_sha1[20];
96 if (get_sha1(hex, sha1)) {
97 cgit_print_error(fmt("Bad object id: %s", hex));
100 commit = lookup_commit_reference(sha1);
102 cgit_print_error(fmt("Bad commit reference: %s", hex));
105 info = cgit_parse_commit(commit);
107 if (commit->parents && commit->parents->item)
108 hashcpy(old_sha1, commit->parents->item->object.sha1);
112 patchname = fmt("%s.patch", sha1_to_hex(sha1));
113 ctx.page.mimetype = "text/plain";
114 ctx.page.filename = patchname;
115 cgit_print_http_headers(&ctx);
116 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1));
117 htmlf("From: %s", info->author);
118 if (!ctx.cfg.noplainemail) {
119 htmlf(" %s", info->author_email);
123 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n", ctx.cfg.local_time);
124 htmlf("Subject: %s\n\n", info->subject);
125 if (info->msg && *info->msg) {
126 htmlf("%s", info->msg);
127 if (info->msg[strlen(info->msg) - 1] != '\n')
132 htmlf("(limited to '%s')\n\n", prefix);
133 cgit_diff_tree(old_sha1, sha1, filepair_cb, prefix, 0);
135 htmlf("cgit %s\n", cgit_version);
136 cgit_free_commitinfo(info);