List for cgit developers and users
 help / color / mirror / Atom feed
* [PATCH 0/3] Download patch between arbitrary revisions
@ 2013-08-14  8:50 cgit
  2013-08-14  8:50 ` [PATCH 1/3] Extract filepair_cb from ui-patch.c cgit
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: cgit @ 2013-08-14  8:50 UTC (permalink / raw)


This is the first small patch series that allows for generating raw
patches between arbitrary revisions.

Note that the first patch only does some refactoring that is required in
preparation for the second one. I wanted to do the same refactoring for
the corresponding functions in ui-diff.c but this turned out to be much
harder due to the use of global variables. It would be nice to do the
same refactoring with filepair_cb() from ui-diff.c and eventually merge
both filepair_cb(), header() and print_line() functions since they share
quite some code. This is out of the scope of this patch series, though.

Lukas Fleischer (3):
  Extract filepair_cb from ui-patch.c
  Allow for creating raw diffs with cgit_print_diff()
  cmd.c: Add a "rawdiff" command

 cmd.c       |  8 ++++++-
 ui-commit.c |  2 +-
 ui-diff.c   | 10 ++++++++-
 ui-diff.h   |  2 +-
 ui-patch.c  | 74 +------------------------------------------------------------
 ui-shared.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ui-shared.h |  1 +
 7 files changed, 92 insertions(+), 77 deletions(-)

-- 
1.8.4.rc2.477.g1da3ebd



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] Extract filepair_cb from ui-patch.c
  2013-08-14  8:50 [PATCH 0/3] Download patch between arbitrary revisions cgit
@ 2013-08-14  8:50 ` cgit
  2013-08-14  8:50 ` [PATCH 2/3] Allow for creating raw diffs with cgit_print_diff() cgit
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: cgit @ 2013-08-14  8:50 UTC (permalink / raw)


Move filepair_cb() from ui-patch.c to ui-shared.c and rename it to
filepair_cb_raw(). This callback will be used in ui-diff.c in a
follow-up patch.

Note that it is not straightforward to extract filepair_cb() from
ui-diff.c which is why it is not done here as well.

Signed-off-by: Lukas Fleischer <cgit at cryptocrack.de>
---
 ui-patch.c  | 74 +------------------------------------------------------------
 ui-shared.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ui-shared.h |  1 +
 3 files changed, 74 insertions(+), 73 deletions(-)

diff --git a/ui-patch.c b/ui-patch.c
index fbb92cc..3922a44 100644
--- a/ui-patch.c
+++ b/ui-patch.c
@@ -11,78 +11,6 @@
 #include "html.h"
 #include "ui-shared.h"
 
-static void print_line(char *line, int len)
-{
-	char c = line[len-1];
-
-	line[len-1] = '\0';
-	htmlf("%s\n", line);
-	line[len-1] = c;
-}
-
-static void header(unsigned char *sha1, char *path1, int mode1,
-		   unsigned char *sha2, char *path2, int mode2)
-{
-	char *abbrev1, *abbrev2;
-	int subproject;
-
-	subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
-	htmlf("diff --git a/%s b/%s\n", path1, path2);
-
-	if (mode1 == 0)
-		htmlf("new file mode %.6o\n", mode2);
-
-	if (mode2 == 0)
-		htmlf("deleted file mode %.6o\n", mode1);
-
-	if (!subproject) {
-		abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
-		abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
-		htmlf("index %s..%s", abbrev1, abbrev2);
-		free(abbrev1);
-		free(abbrev2);
-		if (mode1 != 0 && mode2 != 0) {
-			htmlf(" %.6o", mode1);
-			if (mode2 != mode1)
-				htmlf("..%.6o", mode2);
-		}
-
-		if (is_null_sha1(sha1)) {
-			path1 = "dev/null";
-			htmlf("\n--- /%s\n", path1);
-		} else
-			htmlf("\n--- a/%s\n", path1);
-
-		if (is_null_sha1(sha2)) {
-			path2 = "dev/null";
-			htmlf("+++ /%s\n", path2);
-		} else
-			htmlf("+++ b/%s\n", path2);
-	}
-}
-
-static void filepair_cb(struct diff_filepair *pair)
-{
-	unsigned long old_size = 0;
-	unsigned long new_size = 0;
-	int binary = 0;
-
-	header(pair->one->sha1, pair->one->path, pair->one->mode,
-	       pair->two->sha1, pair->two->path, pair->two->mode);
-	if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
-		if (S_ISGITLINK(pair->one->mode))
-			print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
-		if (S_ISGITLINK(pair->two->mode))
-			print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
-		return;
-	}
-	if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
-			    &new_size, &binary, 0, 0, print_line))
-		html("Error running diff");
-	if (binary)
-		html("Binary files differ\n");
-}
-
 void cgit_print_patch(char *hex, const char *prefix)
 {
 	struct commit *commit;
@@ -130,7 +58,7 @@ void cgit_print_patch(char *hex, const char *prefix)
 	html("---\n");
 	if (prefix)
 		htmlf("(limited to '%s')\n\n", prefix);
-	cgit_diff_tree(old_sha1, sha1, filepair_cb, prefix, 0);
+	cgit_diff_tree(old_sha1, sha1, filepair_cb_raw, prefix, 0);
 	html("--\n");
 	htmlf("cgit %s\n", cgit_version);
 	cgit_free_commitinfo(info);
diff --git a/ui-shared.c b/ui-shared.c
index 7ab2ab1..1e19421 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -950,3 +950,75 @@ void cgit_print_snapshot_links(const char *repo, const char *head,
 	}
 	strbuf_release(&filename);
 }
+
+static void print_line_raw(char *line, int len)
+{
+	char c = line[len-1];
+
+	line[len-1] = '\0';
+	htmlf("%s\n", line);
+	line[len-1] = c;
+}
+
+static void header_raw(unsigned char *sha1, char *path1, int mode1,
+		       unsigned char *sha2, char *path2, int mode2)
+{
+	char *abbrev1, *abbrev2;
+	int subproject;
+
+	subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
+	htmlf("diff --git a/%s b/%s\n", path1, path2);
+
+	if (mode1 == 0)
+		htmlf("new file mode %.6o\n", mode2);
+
+	if (mode2 == 0)
+		htmlf("deleted file mode %.6o\n", mode1);
+
+	if (!subproject) {
+		abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
+		abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
+		htmlf("index %s..%s", abbrev1, abbrev2);
+		free(abbrev1);
+		free(abbrev2);
+		if (mode1 != 0 && mode2 != 0) {
+			htmlf(" %.6o", mode1);
+			if (mode2 != mode1)
+				htmlf("..%.6o", mode2);
+		}
+
+		if (is_null_sha1(sha1)) {
+			path1 = "dev/null";
+			htmlf("\n--- /%s\n", path1);
+		} else
+			htmlf("\n--- a/%s\n", path1);
+
+		if (is_null_sha1(sha2)) {
+			path2 = "dev/null";
+			htmlf("+++ /%s\n", path2);
+		} else
+			htmlf("+++ b/%s\n", path2);
+	}
+}
+
+void filepair_cb_raw(struct diff_filepair *pair)
+{
+	unsigned long old_size = 0;
+	unsigned long new_size = 0;
+	int binary = 0;
+
+	header_raw(pair->one->sha1, pair->one->path, pair->one->mode,
+		   pair->two->sha1, pair->two->path, pair->two->mode);
+	if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
+		if (S_ISGITLINK(pair->one->mode))
+			print_line_raw(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
+		if (S_ISGITLINK(pair->two->mode))
+			print_line_raw(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
+		return;
+	}
+	if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
+			    &new_size, &binary, 0, 0, print_line_raw))
+		html("Error running diff");
+	if (binary)
+		html("Binary files differ\n");
+}
diff --git a/ui-shared.h b/ui-shared.h
index 5987e77..a337dce 100644
--- a/ui-shared.h
+++ b/ui-shared.h
@@ -67,4 +67,5 @@ extern void cgit_print_snapshot_links(const char *repo, const char *head,
 				      const char *hex, int snapshots);
 extern void cgit_add_hidden_formfields(int incl_head, int incl_search,
 				       const char *page);
+extern void filepair_cb_raw(struct diff_filepair *pair);
 #endif /* UI_SHARED_H */
-- 
1.8.4.rc2.477.g1da3ebd



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/3] Allow for creating raw diffs with cgit_print_diff()
  2013-08-14  8:50 [PATCH 0/3] Download patch between arbitrary revisions cgit
  2013-08-14  8:50 ` [PATCH 1/3] Extract filepair_cb from ui-patch.c cgit
@ 2013-08-14  8:50 ` cgit
  2013-08-14  8:50 ` [PATCH 3/3] cmd.c: Add a "rawdiff" command cgit
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: cgit @ 2013-08-14  8:50 UTC (permalink / raw)


This adds a parameter to cgit_print_diff() to create raw diffs, using
the same format as `git diff <commit>`.

Signed-off-by: Lukas Fleischer <cgit at cryptocrack.de>
---
 cmd.c       |  2 +-
 ui-commit.c |  2 +-
 ui-diff.c   | 10 +++++++++-
 ui-diff.h   |  2 +-
 4 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/cmd.c b/cmd.c
index abe8e46..10761ce 100644
--- a/cmd.c
+++ b/cmd.c
@@ -57,7 +57,7 @@ static void commit_fn(struct cgit_context *ctx)
 
 static void diff_fn(struct cgit_context *ctx)
 {
-	cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path, 1);
+	cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path, 1, 0);
 }
 
 static void info_fn(struct cgit_context *ctx)
diff --git a/ui-commit.c b/ui-commit.c
index a5a6ea8..ef85a49 100644
--- a/ui-commit.c
+++ b/ui-commit.c
@@ -137,7 +137,7 @@ void cgit_print_commit(char *hex, const char *prefix)
 			tmp = sha1_to_hex(commit->parents->item->object.sha1);
 		else
 			tmp = NULL;
-		cgit_print_diff(ctx.qry.sha1, tmp, prefix, 0);
+		cgit_print_diff(ctx.qry.sha1, tmp, prefix, 0, 0);
 	}
 	strbuf_release(&notes);
 	cgit_free_commitinfo(info);
diff --git a/ui-diff.c b/ui-diff.c
index 8b38209..838db8c 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -358,7 +358,7 @@ void cgit_print_diff_ctrls()
 }
 
 void cgit_print_diff(const char *new_rev, const char *old_rev,
-		     const char *prefix, int show_ctrls)
+		     const char *prefix, int show_ctrls, int raw)
 {
 	enum object_type type;
 	unsigned long size;
@@ -398,6 +398,14 @@ void cgit_print_diff(const char *new_rev, const char *old_rev,
 		}
 	}
 
+	if (raw) {
+		ctx.page.mimetype = "text/plain";
+		cgit_print_http_headers(&ctx);
+		cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb_raw,
+			       prefix, 0);
+		return;
+	}
+
 	use_ssdiff = ctx.qry.has_ssdiff ? ctx.qry.ssdiff : ctx.cfg.ssdiff;
 
 	if (show_ctrls)
diff --git a/ui-diff.h b/ui-diff.h
index 25a9296..04f9029 100644
--- a/ui-diff.h
+++ b/ui-diff.h
@@ -4,7 +4,7 @@
 extern void cgit_print_diff_ctrls();
 
 extern void cgit_print_diff(const char *new_hex, const char *old_hex,
-			    const char *prefix, int show_ctrls);
+			    const char *prefix, int show_ctrls, int raw);
 
 extern struct diff_filespec *cgit_get_current_old_file(void);
 extern struct diff_filespec *cgit_get_current_new_file(void);
-- 
1.8.4.rc2.477.g1da3ebd



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/3] cmd.c: Add a "rawdiff" command
  2013-08-14  8:50 [PATCH 0/3] Download patch between arbitrary revisions cgit
  2013-08-14  8:50 ` [PATCH 1/3] Extract filepair_cb from ui-patch.c cgit
  2013-08-14  8:50 ` [PATCH 2/3] Allow for creating raw diffs with cgit_print_diff() cgit
@ 2013-08-14  8:50 ` cgit
  2013-08-14 21:47 ` [PATCH 0/3] Download patch between arbitrary revisions john
  2013-08-14 22:13 ` Jason
  4 siblings, 0 replies; 6+ messages in thread
From: cgit @ 2013-08-14  8:50 UTC (permalink / raw)


This can be used to generate raw diffs between arbitrary revisions using
something like

     /rawdiff/?id=v0.9&id2=v0.9.1

Signed-off-by: Lukas Fleischer <cgit at cryptocrack.de>
---
 cmd.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/cmd.c b/cmd.c
index 10761ce..9893710 100644
--- a/cmd.c
+++ b/cmd.c
@@ -60,6 +60,11 @@ static void diff_fn(struct cgit_context *ctx)
 	cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path, 1, 0);
 }
 
+static void rawdiff_fn(struct cgit_context *ctx)
+{
+	cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path, 1, 1);
+}
+
 static void info_fn(struct cgit_context *ctx)
 {
 	cgit_clone_info(ctx);
@@ -150,6 +155,7 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
 		def_cmd(objects, 1, 0, 0, 1),
 		def_cmd(patch, 1, 0, 1, 0),
 		def_cmd(plain, 1, 0, 0, 0),
+		def_cmd(rawdiff, 1, 0, 1, 0),
 		def_cmd(refs, 1, 1, 0, 0),
 		def_cmd(repolist, 0, 0, 0, 0),
 		def_cmd(snapshot, 1, 0, 0, 0),
-- 
1.8.4.rc2.477.g1da3ebd



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 0/3] Download patch between arbitrary revisions
  2013-08-14  8:50 [PATCH 0/3] Download patch between arbitrary revisions cgit
                   ` (2 preceding siblings ...)
  2013-08-14  8:50 ` [PATCH 3/3] cmd.c: Add a "rawdiff" command cgit
@ 2013-08-14 21:47 ` john
  2013-08-14 22:13 ` Jason
  4 siblings, 0 replies; 6+ messages in thread
From: john @ 2013-08-14 21:47 UTC (permalink / raw)


On Wed, Aug 14, 2013 at 10:50:30AM +0200, Lukas Fleischer wrote:
> This is the first small patch series that allows for generating raw
> patches between arbitrary revisions.
> 
> Note that the first patch only does some refactoring that is required in
> preparation for the second one. I wanted to do the same refactoring for
> the corresponding functions in ui-diff.c but this turned out to be much
> harder due to the use of global variables. It would be nice to do the
> same refactoring with filepair_cb() from ui-diff.c and eventually merge
> both filepair_cb(), header() and print_line() functions since they share
> quite some code. This is out of the scope of this patch series, though.
> 
> Lukas Fleischer (3):
>   Extract filepair_cb from ui-patch.c
>   Allow for creating raw diffs with cgit_print_diff()
>   cmd.c: Add a "rawdiff" command
> 
>  cmd.c       |  8 ++++++-
>  ui-commit.c |  2 +-
>  ui-diff.c   | 10 ++++++++-
>  ui-diff.h   |  2 +-
>  ui-patch.c  | 74 +------------------------------------------------------------
>  ui-shared.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  ui-shared.h |  1 +
>  7 files changed, 92 insertions(+), 77 deletions(-)

Nicely done!  FWIW, the whole series

Reviewed-by: John Keeping <john at keeping.me.uk>

However, it would be nice to see something touching tests/ since we do
currently make some effort to test the basics of each command.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 0/3] Download patch between arbitrary revisions
  2013-08-14  8:50 [PATCH 0/3] Download patch between arbitrary revisions cgit
                   ` (3 preceding siblings ...)
  2013-08-14 21:47 ` [PATCH 0/3] Download patch between arbitrary revisions john
@ 2013-08-14 22:13 ` Jason
  4 siblings, 0 replies; 6+ messages in thread
From: Jason @ 2013-08-14 22:13 UTC (permalink / raw)


This will be reviewed as soon as I'm back home from vacation (which will be
soon!), but for now, it's staging in:
http://git.zx2c4.com/cgit/log/?h=lf/diffs

If you also implement the git-format-patch improvements as discussed in the
RFE, go ahead and base it on this branch.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.zx2c4.com/pipermail/cgit/attachments/20130814/aab7b985/attachment.html>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-08-14 22:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-14  8:50 [PATCH 0/3] Download patch between arbitrary revisions cgit
2013-08-14  8:50 ` [PATCH 1/3] Extract filepair_cb from ui-patch.c cgit
2013-08-14  8:50 ` [PATCH 2/3] Allow for creating raw diffs with cgit_print_diff() cgit
2013-08-14  8:50 ` [PATCH 3/3] cmd.c: Add a "rawdiff" command cgit
2013-08-14 21:47 ` [PATCH 0/3] Download patch between arbitrary revisions john
2013-08-14 22:13 ` Jason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).