RFE: download patch between arbitrary revisions

John Keeping john at keeping.me.uk
Mon Jun 3 20:49:53 CEST 2013

On Sun, Jun 02, 2013 at 09:19:11PM -0400, Konstantin Ryabitsev wrote:
> There is currently a way to render a diff between two arbitrary objects,
> e.g.:
> https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/diff/?id=v3.10-rc4&id2=v3.10-rc3
> However, there doesn't appear to be a way to download a patch in the
> same way -- it will only make patch against id's parent. E.g.:
> https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/patch/?id=v3.10-rc4&id2=v3.10-rc3
> Any way we can make the behaviour of patch match that of diff?

I had a quick look at this and changing the patch output is really easy,
but the top of the output is now completely wrong since it displays the
message from the "id" commit.  I'm not sure what to do about this;
clearly it is useful to be able to get the patch output between two
arbitrary points in a raw format (not HTML) but I don't know what to do
about the commit message and headers.  Perhaps we can do something like:

    if "id2" is specified:
        print shortlog output
        do what we currently do

but I don't know how much code would be needed for that.

Here's the patch in case anyone wants to try it (slightly bigger than
strictly necessary because I renamed hex -> new_rev for consistency with

-- >8 --
diff --git a/cmd.c b/cmd.c
index abe8e46..7e8b168 100644
--- a/cmd.c
+++ b/cmd.c
@@ -93,7 +93,7 @@ static void repolist_fn(struct cgit_context *ctx)
 static void patch_fn(struct cgit_context *ctx)
-	cgit_print_patch(ctx->qry.sha1, ctx->qry.path);
+	cgit_print_patch(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path);
 static void plain_fn(struct cgit_context *ctx)
diff --git a/ui-patch.c b/ui-patch.c
index fbb92cc..ff8ee34 100644
--- a/ui-patch.c
+++ b/ui-patch.c
@@ -83,28 +83,30 @@ static void filepair_cb(struct diff_filepair *pair)
 		html("Binary files differ\n");
-void cgit_print_patch(char *hex, const char *prefix)
+void cgit_print_patch(const char *new_rev, const char *old_rev, const char *prefix)
 	struct commit *commit;
 	struct commitinfo *info;
 	unsigned char sha1[20], old_sha1[20];
 	char *patchname;
-	if (!hex)
-		hex = ctx.qry.head;
+	if (!new_rev)
+		new_rev = ctx.qry.head;
-	if (get_sha1(hex, sha1)) {
-		cgit_print_error("Bad object id: %s", hex);
+	if (get_sha1(new_rev, sha1)) {
+		cgit_print_error("Bad object id: %s", new_rev);
 	commit = lookup_commit_reference(sha1);
 	if (!commit) {
-		cgit_print_error("Bad commit reference: %s", hex);
+		cgit_print_error("Bad commit reference: %s", new_rev);
 	info = cgit_parse_commit(commit);
-	if (commit->parents && commit->parents->item)
+	if (old_rev)
+		get_sha1(old_rev, old_sha1);
+	else if (commit->parents && commit->parents->item)
 		hashcpy(old_sha1, commit->parents->item->object.sha1);
diff --git a/ui-patch.h b/ui-patch.h
index 1641cea..e833042 100644
--- a/ui-patch.h
+++ b/ui-patch.h
@@ -1,6 +1,6 @@
 #ifndef UI_PATCH_H
 #define UI_PATCH_H
-extern void cgit_print_patch(char *hex, const char *prefix);
+extern void cgit_print_patch(const char *new_rev, const char *old_rev, const char *prefix);
 #endif /* UI_PATCH_H */

More information about the CGit mailing list