diff mbox

Keep patch file permissions in mklog

Message ID 53E888A3.60502@mentor.com
State New
Headers show

Commit Message

Tom de Vries Aug. 11, 2014, 9:10 a.m. UTC
On 11-08-14 10:18, Yury Gribov wrote:
> On 08/11/2014 11:22 AM, Tom de Vries wrote:
>> I'm now using mktemp from File::Temp.
>
> The script now relies on external Perl modules. This is probably fine because
> AFAIK File::Temp and File::Copy are available included in all distributions.
>
> +use File::Copy "cp";
> +use File::Copy "mv";
>
> You could trade one line with qw(cp mv)...
>
> +    $tmp = mktemp("tmp.XXXXXXXX") or die "cannot create temp file: $!";
>
> Perhaps "Could not" like in other error messages?
>
> +    cp "$diff", "$tmp" or die "Could not copy patch file to temp file: $!";
>
> Script don't quote stuff in other places so quoting here is useless.
>

Updated according comments and retested.

Thanks again,
- Tom

Comments

Segher Boessenkool Aug. 11, 2014, 5:29 p.m. UTC | #1
On Mon, Aug 11, 2014 at 11:10:59AM +0200, Tom de Vries wrote:
> +use File::Temp;
> +use File::Copy qw(cp mv);

> +	# Copy the permissions to the temp file (in perl 2.15 and later).
> +	cp $diff, $tmp or die "Could not copy patch file to temp file: $!";

That's File::Copy 2.15, not Perl 2.15 (which doesn't exist).  File::Copy 2.15
isn't so terribly old, so you might want to do a version check, e.g. as

use File::Copy 2.15 qw/cp mv/;

(but it's in Perl 5.10 already, so you might not want to bother).

> -if ($#ARGV != 0) {
> +$inline = 0;
> +if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
> +	shift;
> +	$inline = 1;
> +} elsif ($#ARGV != 0) {

If you want any more command-line options, have a look at Getopt::Long.


Segher
diff mbox

Patch

2014-08-11  Tom de Vries  <tom@codesourcery.com>

	* mklog: Add --inline option.

diff --git a/contrib/mklog b/contrib/mklog
index 3d17dc5..b575b6e 100755
--- a/contrib/mklog
+++ b/contrib/mklog
@@ -26,6 +26,9 @@ 
 # Author: Diego Novillo <dnovillo@google.com> and
 #         Cary Coutant <ccoutant@google.com>
 
+use File::Temp;
+use File::Copy qw(cp mv);
+
 # Change these settings to reflect your profile.
 $username = $ENV{'USER'};
 $name = `finger $username | grep -o 'Name: .*'`;
@@ -56,14 +59,22 @@  if (-d "$gcc_root/.git") {
 # Program starts here. You should not need to edit anything below this
 # line.
 #-----------------------------------------------------------------------------
-if ($#ARGV != 0) {
+$inline = 0;
+if ($#ARGV == 1 && ("$ARGV[0]" eq "-i" || "$ARGV[0]" eq "--inline")) {
+	shift;
+	$inline = 1;
+} elsif ($#ARGV != 0) {
     $prog = `basename $0`; chop ($prog);
     print <<EOF;
-usage: $prog file.diff
+usage: $prog [ -i | --inline ] file.diff
 
 Generate ChangeLog template for file.diff.
 It assumes that patch has been created with -up or -cp.
+When -i is used, the ChangeLog template is followed by the contents of
+file.diff.
 When file.diff is -, read standard input.
+When -i is used and file.diff is not -, it writes to file.diff, otherwise it
+writes to stdout.
 EOF
     exit 1;
 }
@@ -273,8 +284,38 @@  foreach (@diff_lines) {
 # functions.
 $cl_entries{$clname} .= $change_msg ? "$change_msg\n" : ":\n";
 
+if ($inline && $diff ne "-") {
+	# Get a temp filename, rather than an open filehandle, because we use
+	# the open to truncate.
+	$tmp = mktemp("tmp.XXXXXXXX") or die "Could not create temp file: $!";
+
+	# Copy the permissions to the temp file (in perl 2.15 and later).
+	cp $diff, $tmp or die "Could not copy patch file to temp file: $!";
+
+	# Open the temp file, clearing contents.
+	open (OUTPUTFILE, '>', $tmp) or die "Could not open temp file: $!";
+} else {
+	*OUTPUTFILE = STDOUT;
+}
+
+# Print the log
 foreach my $clname (keys %cl_entries) {
-	print "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
+	print OUTPUTFILE "$clname:\n\n$hdrline\n\n$cl_entries{$clname}\n";
+}
+
+if ($inline) {
+	# Append the patch to the log
+	foreach (@diff_lines) {
+		print OUTPUTFILE "$_\n";
+	}
+}
+
+if ($inline && $diff ne "-") {
+	# Close $tmp
+	close(OUTPUTFILE);
+
+	# Write new contents to $diff atomically
+	mv $tmp, $diff or die "Could not move temp file to patch file: $!";
 }
 
 exit 0;
-- 
1.9.1