Re: [PATCH 2/4] sequencer: add "make_patch" function to save a patch
- From
Christian Couder <chriscool@tuxfamily.org>
- Date
- Jun 28, 2009, 12:13 UTC
- Message-ID
- <200906281413.21454.chriscool@tuxfamily.org>
- In-Reply-To
- <20090626210847.3885.18347.chriscool@tuxfamily.org>
Ooops, there should be a:
From: Stephan Beyer <s-beyer@gmx.net>
header in this patch.
Regards, Christian.
On Friday 26 June 2009, Christian Couder wrote:
Show 118 quoted lines
> This function generates an informational patch file.
>
> The "make_patch" and the "get_commit" functions are copied from the
> GSoC sequencer project:
>
> git://repo.or.cz/git/sbeyer.git
>
> (commit e7b8dab0c2a73ade92017a52bb1405ea1534ef20)
>
> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> Mentored-by: Daniel Barkalow <barkalow@iabervon.org>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> builtin-sequencer--helper.c | 75
> ++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 74
> insertions(+), 1 deletions(-)
>
> diff --git a/builtin-sequencer--helper.c b/builtin-sequencer--helper.c
> index 721c0d8..8d32480 100644
> --- a/builtin-sequencer--helper.c
> +++ b/builtin-sequencer--helper.c
> @@ -1,15 +1,83 @@
> #include "builtin.h"
> #include "cache.h"
> #include "parse-options.h"
> +#include "run-command.h"
> +
> +#define SEQ_DIR "rebase-merge"
> +
> +#define PATCH_FILE git_path(SEQ_DIR "/patch")
>
> static const char * const git_sequencer_helper_usage[] = {
> "git sequencer--helper --make-patch <commit>",
> NULL
> };
>
> +/* Generate purely informational patch file */
> +static void make_patch(struct commit *commit)
> +{
> + struct commit_list *parents = commit->parents;
> + const char **args;
> + struct child_process chld;
> + int fd = open(PATCH_FILE, O_WRONLY | O_CREAT, 0666);
> + if (fd < 0)
> + return;
> +
> + memset(&chld, 0, sizeof(chld));
> + if (!parents) {
> + write(fd, "Root commit\n", 12);
> + close(fd);
> + return;
> + } else if (!parents->next) {
> + args = xcalloc(5, sizeof(char *));
> + args[0] = "diff-tree";
> + args[1] = "-p";
> + args[2] = xstrdup(sha1_to_hex(parents->item->object.sha1));
> + args[3] = xstrdup(sha1_to_hex(((struct object *)commit)->sha1));
> + } else {
> + int i = 0;
> + int count = 1;
> +
> + for (; parents; parents = parents->next)
> + ++count;
> + args = xcalloc(count + 3, sizeof(char *));
> + args[i++] = "diff";
> + args[i++] = "--cc";
> + args[i++] = xstrdup(sha1_to_hex(commit->object.sha1));
> +
> + for (parents = commit->parents; parents;
> + parents = parents->next)
> + args[i++] = xstrdup(sha1_to_hex(
> + parents->item->object.sha1));
> + }
> +
> + chld.argv = args;
> + chld.git_cmd = 1;
> + chld.out = fd;
> +
> + /* Run, ignore errors. */
> + if (start_command(&chld))
> + return;
> + finish_command(&chld);
> +
> + /* TODO: free dup'ed SHAs in argument list */
> +}
> +
> +/* Return a commit object of "arg" */
> +static struct commit *get_commit(const char *arg)
> +{
> + unsigned char sha1[20];
> +
> + if (get_sha1(arg, sha1)) {
> + error("Could not find '%s'", arg);
> + return NULL;
> + }
> + return lookup_commit_reference(sha1);
> +}
> +
> int cmd_sequencer__helper(int argc, const char **argv, const char
> *prefix) {
> char *commit = NULL;
> + struct commit *c;
> struct option options[] = {
> OPT_STRING(0, "make-patch", &commit, "commit",
> "create a patch from commit"),
> @@ -22,6 +90,11 @@ int cmd_sequencer__helper(int argc, const char **argv,
> const char *prefix) if (!commit)
> usage_with_options(git_sequencer_helper_usage, options);
>
> - /* Nothing to do yet */
> + c = get_commit(commit);
> + if (!c)
> + return 1;
> +
> + make_patch(c);
> +
> return 0;
> }