All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Add in-place editing support to git interpret-trailers
@ 2016-01-11 13:33 Tobias Klauser
  2016-01-11 13:33 ` [PATCH 1/2] trailer: use fprintf instead of printf Tobias Klauser
  2016-01-11 13:33 ` [PATCH 2/2] interpret-trailers: add option for in-place editing Tobias Klauser
  0 siblings, 2 replies; 6+ messages in thread
From: Tobias Klauser @ 2016-01-11 13:33 UTC (permalink / raw)
  To: Junio C Hamano, Christian Couder; +Cc: Matthieu Moy, Eric Sunshine, git

This patch series adds support for in-place editing to git interpret-trailers
akin to sed -i, perl -i.

v1->v2:
 - Split patch to make review easier, as suggested by Matthieu Moy.
 - Rename FILE * function parameters to a more readable name, as suggested by
   Matthieu Moy.
 - Write output to temporary file and rename after successfully written in full
   to avoid losing the original file in case of an error/interrupt. Pointed out
   by Eric Sunshine.

Tobias Klauser (2):
  trailer: use fprintf instead of printf
  interpret-trailers: add option for in-place editing

 Documentation/git-interpret-trailers.txt | 24 +++++++++++-
 builtin/interpret-trailers.c             | 13 +++++--
 t/t7513-interpret-trailers.sh            | 32 ++++++++++++++++
 trailer.c                                | 63 +++++++++++++++++++++++++-------
 trailer.h                                |  3 +-
 5 files changed, 115 insertions(+), 20 deletions(-)

-- 
2.7.0.1.g5e091f5

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

* [PATCH 1/2] trailer: use fprintf instead of printf
  2016-01-11 13:33 [PATCH 0/2] Add in-place editing support to git interpret-trailers Tobias Klauser
@ 2016-01-11 13:33 ` Tobias Klauser
  2016-01-11 13:33 ` [PATCH 2/2] interpret-trailers: add option for in-place editing Tobias Klauser
  1 sibling, 0 replies; 6+ messages in thread
From: Tobias Klauser @ 2016-01-11 13:33 UTC (permalink / raw)
  To: Junio C Hamano, Christian Couder; +Cc: Matthieu Moy, Eric Sunshine, git

Use fprintf instead of printf in trailer.c in order to allow printing
to a file other than stdout. This will be needed to support in-place
editing in git interpret-trailers.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
---
 trailer.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/trailer.c b/trailer.c
index 6f3416febaba..176fac213450 100644
--- a/trailer.c
+++ b/trailer.c
@@ -108,23 +108,23 @@ static char last_non_space_char(const char *s)
 	return '\0';
 }
 
-static void print_tok_val(const char *tok, const char *val)
+static void print_tok_val(FILE *outfile, const char *tok, const char *val)
 {
 	char c = last_non_space_char(tok);
 	if (!c)
 		return;
 	if (strchr(separators, c))
-		printf("%s%s\n", tok, val);
+		fprintf(outfile, "%s%s\n", tok, val);
 	else
-		printf("%s%c %s\n", tok, separators[0], val);
+		fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
 }
 
-static void print_all(struct trailer_item *first, int trim_empty)
+static void print_all(FILE *outfile, struct trailer_item *first, int trim_empty)
 {
 	struct trailer_item *item;
 	for (item = first; item; item = item->next) {
 		if (!trim_empty || strlen(item->value) > 0)
-			print_tok_val(item->token, item->value);
+			print_tok_val(outfile, item->token, item->value);
 	}
 }
 
@@ -795,14 +795,15 @@ static int has_blank_line_before(struct strbuf **lines, int start)
 	return 0;
 }
 
-static void print_lines(struct strbuf **lines, int start, int end)
+static void print_lines(FILE *outfile, struct strbuf **lines, int start, int end)
 {
 	int i;
 	for (i = start; lines[i] && i < end; i++)
-		printf("%s", lines[i]->buf);
+		fprintf(outfile, "%s", lines[i]->buf);
 }
 
-static int process_input_file(struct strbuf **lines,
+static int process_input_file(FILE *outfile,
+			      struct strbuf **lines,
 			      struct trailer_item **in_tok_first,
 			      struct trailer_item **in_tok_last)
 {
@@ -818,10 +819,10 @@ static int process_input_file(struct strbuf **lines,
 	trailer_start = find_trailer_start(lines, trailer_end);
 
 	/* Print lines before the trailers as is */
-	print_lines(lines, 0, trailer_start);
+	print_lines(outfile, lines, 0, trailer_start);
 
 	if (!has_blank_line_before(lines, trailer_start - 1))
-		printf("\n");
+		fprintf(outfile, "\n");
 
 	/* Parse trailer lines */
 	for (i = trailer_start; i < trailer_end; i++) {
@@ -849,6 +850,7 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
 	struct trailer_item *arg_tok_first;
 	struct strbuf **lines;
 	int trailer_end;
+	FILE *outfile = stdout;
 
 	/* Default config must be setup first */
 	git_config(git_trailer_default_config, NULL);
@@ -857,18 +859,18 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
 	lines = read_input_file(file);
 
 	/* Print the lines before the trailers */
-	trailer_end = process_input_file(lines, &in_tok_first, &in_tok_last);
+	trailer_end = process_input_file(outfile, lines, &in_tok_first, &in_tok_last);
 
 	arg_tok_first = process_command_line_args(trailers);
 
 	process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
 
-	print_all(in_tok_first, trim_empty);
+	print_all(outfile, in_tok_first, trim_empty);
 
 	free_all(&in_tok_first);
 
 	/* Print the lines after the trailers as is */
-	print_lines(lines, trailer_end, INT_MAX);
+	print_lines(outfile, lines, trailer_end, INT_MAX);
 
 	strbuf_list_free(lines);
 }
-- 
2.7.0.1.g5e091f5

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

* [PATCH 2/2] interpret-trailers: add option for in-place editing
  2016-01-11 13:33 [PATCH 0/2] Add in-place editing support to git interpret-trailers Tobias Klauser
  2016-01-11 13:33 ` [PATCH 1/2] trailer: use fprintf instead of printf Tobias Klauser
@ 2016-01-11 13:33 ` Tobias Klauser
  2016-01-11 16:33   ` Matthieu Moy
  1 sibling, 1 reply; 6+ messages in thread
From: Tobias Klauser @ 2016-01-11 13:33 UTC (permalink / raw)
  To: Junio C Hamano, Christian Couder; +Cc: Matthieu Moy, Eric Sunshine, git

Add a command line option --in-place to support in-place editing akin to
sed -i.  This allows to write commands like the following:

  git interpret-trailers --trailer "X: Y" a.txt > b.txt && mv b.txt a.txt

in a more concise way:

  git interpret-trailers --trailer "X: Y" --in-place a.txt

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
---
 Documentation/git-interpret-trailers.txt | 24 +++++++++++++++++++++-
 builtin/interpret-trailers.c             | 13 ++++++++----
 t/t7513-interpret-trailers.sh            | 32 +++++++++++++++++++++++++++++
 trailer.c                                | 35 +++++++++++++++++++++++++++++++-
 trailer.h                                |  3 ++-
 5 files changed, 100 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-interpret-trailers.txt b/Documentation/git-interpret-trailers.txt
index 0ecd497c4de7..a77b901f1d7b 100644
--- a/Documentation/git-interpret-trailers.txt
+++ b/Documentation/git-interpret-trailers.txt
@@ -8,7 +8,7 @@ git-interpret-trailers - help add structured information into commit messages
 SYNOPSIS
 --------
 [verse]
-'git interpret-trailers' [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]
+'git interpret-trailers' [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]
 
 DESCRIPTION
 -----------
@@ -64,6 +64,9 @@ folding rules, the encoding rules and probably many other rules.
 
 OPTIONS
 -------
+--in-place::
+	Edit the files in place.
+
 --trim-empty::
 	If the <value> part of any trailer contains only whitespace,
 	the whole trailer will be removed from the resulting message.
@@ -216,6 +219,25 @@ Signed-off-by: Alice <alice@example.com>
 Signed-off-by: Bob <bob@example.com>
 ------------
 
+* Use the '--in-place' option to edit a message file in place:
++
+------------
+$ cat msg.txt
+subject
+
+message
+
+Signed-off-by: Bob <bob@example.com>
+$ git interpret-trailers --trailer 'Acked-by: Alice <alice@example.com>' --in-place msg.txt
+$ cat msg.txt
+subject
+
+message
+
+Signed-off-by: Bob <bob@example.com>
+Acked-by: Alice <alice@example.com>
+------------
+
 * Extract the last commit as a patch, and add a 'Cc' and a
   'Reviewed-by' trailer to it:
 +
diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c
index 46838d24a90a..b99ae4be8875 100644
--- a/builtin/interpret-trailers.c
+++ b/builtin/interpret-trailers.c
@@ -12,16 +12,18 @@
 #include "trailer.h"
 
 static const char * const git_interpret_trailers_usage[] = {
-	N_("git interpret-trailers [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"),
+	N_("git interpret-trailers [--in-place] [--trim-empty] [(--trailer <token>[(=|:)<value>])...] [<file>...]"),
 	NULL
 };
 
 int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 {
+	int in_place = 0;
 	int trim_empty = 0;
 	struct string_list trailers = STRING_LIST_INIT_DUP;
 
 	struct option options[] = {
+		OPT_BOOL(0, "in-place", &in_place, N_("edit files in place")),
 		OPT_BOOL(0, "trim-empty", &trim_empty, N_("trim empty trailers")),
 		OPT_STRING_LIST(0, "trailer", &trailers, N_("trailer"),
 				N_("trailer(s) to add")),
@@ -34,9 +36,12 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix)
 	if (argc) {
 		int i;
 		for (i = 0; i < argc; i++)
-			process_trailers(argv[i], trim_empty, &trailers);
-	} else
-		process_trailers(NULL, trim_empty, &trailers);
+			process_trailers(argv[i], in_place, trim_empty, &trailers);
+	} else {
+		if (in_place)
+			die(_("no input file given for in-place editing"));
+		process_trailers(NULL, in_place, trim_empty, &trailers);
+	}
 
 	string_list_clear(&trailers, 0);
 
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
index 322c436a494c..1103a4838b5c 100755
--- a/t/t7513-interpret-trailers.sh
+++ b/t/t7513-interpret-trailers.sh
@@ -326,6 +326,38 @@ test_expect_success 'with complex patch, args and --trim-empty' '
 	test_cmp expected actual
 '
 
+test_expect_success 'in-place editing with basic patch' '
+	cat basic_message >message &&
+	cat basic_patch >>message &&
+	cat basic_message >expected &&
+	echo >>expected &&
+	cat basic_patch >>expected &&
+	git interpret-trailers --in-place message &&
+	test_cmp expected message
+'
+
+test_expect_success 'in-place editing with additional trailer' '
+	cat basic_message >message &&
+	cat basic_patch >>message &&
+	cat basic_message >expected &&
+	echo >>expected &&
+	cat >>expected <<-\EOF &&
+		Reviewed-by: Alice
+	EOF
+	cat basic_patch >>expected &&
+	git interpret-trailers --trailer "Reviewed-by: Alice" --in-place message &&
+	test_cmp expected message
+'
+
+test_expect_success 'in-place editing on stdin' '
+	test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place < basic_message
+'
+
+test_expect_success 'in-place editing on non-existing file' '
+	test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place nonexisting &&
+	test_path_is_missing nonexisting
+'
+
 test_expect_success 'using "where = before"' '
 	git config trailer.bug.where "before" &&
 	cat complex_message_body >expected &&
diff --git a/trailer.c b/trailer.c
index 176fac213450..a52bd045be8d 100644
--- a/trailer.c
+++ b/trailer.c
@@ -2,6 +2,7 @@
 #include "string-list.h"
 #include "run-command.h"
 #include "commit.h"
+#include "tempfile.h"
 #include "trailer.h"
 /*
  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
@@ -843,7 +844,9 @@ static void free_all(struct trailer_item **first)
 	}
 }
 
-void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
+static struct tempfile trailers_tempfile;
+
+void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
 {
 	struct trailer_item *in_tok_first = NULL;
 	struct trailer_item *in_tok_last = NULL;
@@ -858,6 +861,31 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
 
 	lines = read_input_file(file);
 
+	if (in_place) {
+		struct stat st;
+		struct strbuf template = STRBUF_INIT;
+		const char *tail;
+
+		if (stat(file, &st))
+			die_errno(_("could not stat %s"), file);
+		if (!S_ISREG(st.st_mode))
+			die(_("file %s is not a regular file"), file);
+		if (!(st.st_mode & S_IWUSR))
+			die(_("file %s is not writable by user"), file);
+
+		/* Create temporary file in the same directory as the original */
+		tail = strrchr(file, '/');
+		if (tail != NULL)
+			strbuf_add(&template, file, tail - file + 1);
+		strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
+
+		xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
+		strbuf_release(&template);
+		outfile = fdopen_tempfile(&trailers_tempfile, "w");
+		if (!outfile)
+			die_errno(_("could not fdopen tempfile"));
+	}
+
 	/* Print the lines before the trailers */
 	trailer_end = process_input_file(outfile, lines, &in_tok_first, &in_tok_last);
 
@@ -872,5 +900,10 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
 	/* Print the lines after the trailers as is */
 	print_lines(outfile, lines, trailer_end, INT_MAX);
 
+	if (in_place) {
+		if (rename_tempfile(&trailers_tempfile, file))
+			die_errno(_("could not rename tempfile"));
+	}
+
 	strbuf_list_free(lines);
 }
diff --git a/trailer.h b/trailer.h
index 8eb25d565e28..36b40b81761f 100644
--- a/trailer.h
+++ b/trailer.h
@@ -1,6 +1,7 @@
 #ifndef TRAILER_H
 #define TRAILER_H
 
-void process_trailers(const char *file, int trim_empty, struct string_list *trailers);
+void process_trailers(const char *file, int in_place, int trim_empty,
+		      struct string_list *trailers);
 
 #endif /* TRAILER_H */
-- 
2.7.0.1.g5e091f5

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

* Re: [PATCH 2/2] interpret-trailers: add option for in-place editing
  2016-01-11 13:33 ` [PATCH 2/2] interpret-trailers: add option for in-place editing Tobias Klauser
@ 2016-01-11 16:33   ` Matthieu Moy
  2016-01-11 17:13     ` Tobias Klauser
  0 siblings, 1 reply; 6+ messages in thread
From: Matthieu Moy @ 2016-01-11 16:33 UTC (permalink / raw)
  To: Tobias Klauser; +Cc: Junio C Hamano, Christian Couder, Eric Sunshine, git

Tobias Klauser <tklauser@distanz.ch> writes:

> @@ -843,7 +844,9 @@ static void free_all(struct trailer_item **first)
>  	}
>  }
>  
> -void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
> +static struct tempfile trailers_tempfile;

Does this need to be a static global? I'd rather have this be a local
variable of process_trailers.

> +			die_errno(_("could not fdopen tempfile"));

I think you should spell it "could not open temporary file" to be more
user-friendly.

> @@ -872,5 +900,10 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
>  	/* Print the lines after the trailers as is */
>  	print_lines(outfile, lines, trailer_end, INT_MAX);
>  
> +	if (in_place) {
> +		if (rename_tempfile(&trailers_tempfile, file))
> +			die_errno(_("could not rename tempfile"));
> +	}

When this happens, I think you also want to try removing the temporary
file. Not sure, though: it may be nice to leave the tempfile for the
user to debug. What do we do in other places of the code?

It may help the user to get "could not rename temporary file %s to %s"
in case this happens.

On overall, the split makes the series much more pleasant to review, and
other than these details, this sounds good to me. Thanks!

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

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

* Re: [PATCH 2/2] interpret-trailers: add option for in-place editing
  2016-01-11 16:33   ` Matthieu Moy
@ 2016-01-11 17:13     ` Tobias Klauser
  2016-01-11 17:24       ` Matthieu Moy
  0 siblings, 1 reply; 6+ messages in thread
From: Tobias Klauser @ 2016-01-11 17:13 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Junio C Hamano, Christian Couder, Eric Sunshine, git

Oops, I just realized I forgot the v2 in the subject line :-( Sorry
about that.

On 2016-01-11 at 17:33:56 +0100, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:
> Tobias Klauser <tklauser@distanz.ch> writes:
> 
> > @@ -843,7 +844,9 @@ static void free_all(struct trailer_item **first)
> >  	}
> >  }
> >  
> > -void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
> > +static struct tempfile trailers_tempfile;
> 
> Does this need to be a static global? I'd rather have this be a local
> variable of process_trailers.

I'm using a static global in order to have it automatically zeroed out
and according to the documentation in tempfile.h it can be reused. Also,
all other users of struct tempfile (except for lockfile.h) are using it
this way.

> 
> > +			die_errno(_("could not fdopen tempfile"));
> 
> I think you should spell it "could not open temporary file" to be more
> user-friendly.

Ok, will adjust.

> > @@ -872,5 +900,10 @@ void process_trailers(const char *file, int trim_empty, struct string_list *trai
> >  	/* Print the lines after the trailers as is */
> >  	print_lines(outfile, lines, trailer_end, INT_MAX);
> >  
> > +	if (in_place) {
> > +		if (rename_tempfile(&trailers_tempfile, file))
> > +			die_errno(_("could not rename tempfile"));
> > +	}
> 
> When this happens, I think you also want to try removing the temporary
> file. Not sure, though: it may be nice to leave the tempfile for the
> user to debug. What do we do in other places of the code?

According to the comment in tempfile.h an atexit(3) handler is installed
by prepare_tempfile_object() (which in turn is called by
x?mks_tempfile_*) which will remove the file in this case. Or did I miss
something here?

AFAICS the two other current users of rename_tempfile() also don't
explicitely treat the tempfile on error.

> It may help the user to get "could not rename temporary file %s to %s"
> in case this happens.

I think if we keep the current semantics (where the tempfile will be
deleted by the atexit(3) handler), it doesn't make sense to mention the
filename in the error message as the file will be gone by the time the
user has any chance to react. I'd suggest somethin like "could not
rename temporary file to %s".

> On overall, the split makes the series much more pleasant to review, and
> other than these details, this sounds good to me. Thanks!

Thanks a lot for your feedback!

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

* Re: [PATCH 2/2] interpret-trailers: add option for in-place editing
  2016-01-11 17:13     ` Tobias Klauser
@ 2016-01-11 17:24       ` Matthieu Moy
  0 siblings, 0 replies; 6+ messages in thread
From: Matthieu Moy @ 2016-01-11 17:24 UTC (permalink / raw)
  To: Tobias Klauser; +Cc: Junio C Hamano, Christian Couder, Eric Sunshine, git

Tobias Klauser <tklauser@distanz.ch> writes:

> I'm using a static global in order to have it automatically zeroed out
> and according to the documentation in tempfile.h it can be reused. Also,
> all other users of struct tempfile (except for lockfile.h) are using it
> this way.

It seems I hate global variables more than other Git contributors ;-).
Anyway, OK with this.

> According to the comment in tempfile.h an atexit(3) handler is installed
> by prepare_tempfile_object() (which in turn is called by
> x?mks_tempfile_*) which will remove the file in this case. Or did I miss
> something here?

You didn't, I wasn't aware of this atexit handler.

>> It may help the user to get "could not rename temporary file %s to %s"
>> in case this happens.
>
> I think if we keep the current semantics (where the tempfile will be
> deleted by the atexit(3) handler), it doesn't make sense to mention the
> filename in the error message as the file will be gone by the time the
> user has any chance to react. I'd suggest somethin like "could not
> rename temporary file to %s".

Good. The important bit is to tell the user which file.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

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

end of thread, other threads:[~2016-01-11 17:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-11 13:33 [PATCH 0/2] Add in-place editing support to git interpret-trailers Tobias Klauser
2016-01-11 13:33 ` [PATCH 1/2] trailer: use fprintf instead of printf Tobias Klauser
2016-01-11 13:33 ` [PATCH 2/2] interpret-trailers: add option for in-place editing Tobias Klauser
2016-01-11 16:33   ` Matthieu Moy
2016-01-11 17:13     ` Tobias Klauser
2016-01-11 17:24       ` Matthieu Moy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.