# [PATCH v3] import-tars: Allow per-tar author and commit message.

5 messages from 2009-08-26 to 2009-08-28. Participants: Peter Krefting, Junio C Hamano.
Thread: https://gitlist.dev/t/20744

## Peter Krefting, 2009-08-26 19:26

Subject: [PATCH v3] import-tars: Allow per-tar author and commit message.
Message-ID: <20090826193015.962A7189B12@perkele>
URL: https://gitlist.dev/e/20090826193015.962A7189B12%40perkele

```
If the "--metainfo=<ext>" option is given on the command line, a file
called "<filename.tar>.<ext>" will be used to create the commit message
for "<filename.tar>", instead of using "Imported from filename.tar".

The author and committer of the tar ball can also be overridden by
embedding an "Author:" or "Committer:" header in the metainfo file.

Signed-off-by: Peter Krefting <peter@softwolves.pp.se>
---
This version adds a command line option --metainfo that is used to
enable the new behaviour, as suggested by Junio C Hamano.

 contrib/fast-import/import-tars.perl |   54 +++++++++++++++++++++++++++++++--
 1 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/contrib/fast-import/import-tars.perl b/contrib/fast-import/import-tars.perl
index 78e40d2..15835cb 100755
--- a/contrib/fast-import/import-tars.perl
+++ b/contrib/fast-import/import-tars.perl
@@ -8,9 +8,20 @@
 ##  perl import-tars.perl *.tar.bz2
 ##  git whatchanged import-tars
 ##
+## Use --metainfo to specify the extension for a meta data file, where
+## import-tars can read the commit message and optionally author and
+## committer information.
+##
+##  echo 'This is the commit message' > myfile.tar.bz2.msg
+##  perl import-tars.perl --metainfo=msg myfile.tar.bz2
 
 use strict;
-die "usage: import-tars *.tar.{gz,bz2,Z}\n" unless @ARGV;
+use Getopt::Long;
+
+my $metaext = '';
+
+die "usage: import-tars [--metainfo=extension] *.tar.{gz,bz2,Z}\n"
+	unless GetOptions('metainfo=s' => \$metaext) && @ARGV;
 
 my $branch_name = 'import-tars';
 my $branch_ref = "refs/heads/$branch_name";
@@ -109,12 +120,47 @@ foreach my $tar_file (@ARGV)
 		$have_top_dir = 0 if $top_dir ne $1;
 	}
 
+	my $commit_msg = "Imported from $tar_file.";
+	my $this_committer_name = $committer_name;
+	my $this_committer_email = $committer_email;
+	my $this_author_name = $author_name;
+	my $this_author_email = $author_email;
+	if ($metaext ne '')
+	{
+		# Optionally read a commit message from <filename.tar>.msg
+		# Add a line on the form "Committer: name <e-mail>" to override
+		# the committer and "Author: name <e-mail>" to override the
+		# author for this tar ball.
+		if (open MSG, '<', "${tar_file}.${metaext}")
+		{
+			$commit_msg = '';
+			while (<MSG>)
+			{
+				if (/^Committer:\s+([^<>]*)\s+<(.*)>\s*$/i)
+				{
+					$this_committer_name = $1;
+					$this_committer_email = $2;
+				}
+				elsif (/^Author:\s+([^<>]*)\s+<(.*)>\s*$/i)
+				{
+					$this_author_name = $1;
+					$this_author_email = $2;
+				}
+				else
+				{
+					$commit_msg .= $_;
+				}
+			}
+			close MSG;
+		}
+	}
+
 	print FI <<EOF;
 commit $branch_ref
-author $author_name <$author_email> $author_time +0000
-committer $committer_name <$committer_email> $commit_time +0000
+author $this_author_name <$this_author_email> $author_time +0000
+committer $this_committer_name <$this_committer_email> $commit_time +0000
 data <<END_OF_COMMIT_MESSAGE
-Imported from $tar_file.
+$commit_msg
 END_OF_COMMIT_MESSAGE
 
 deleteall
-- 
1.6.3.3

```

## Junio C Hamano, 2009-08-27 04:57

Subject: Re: [PATCH v3] import-tars: Allow per-tar author and commit message.
Message-ID: <7viqg96ehf.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7viqg96ehf.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20090826193015.962A7189B12@perkele>

```
Peter Krefting <peter@softwolves.pp.se> writes:

> +			while (<MSG>)
> +			{
> +				if (/^Committer:\s+([^<>]*)\s+<(.*)>\s*$/i)
> +				{
> +					$this_committer_name = $1;
> +					$this_committer_email = $2;
> +				}
> +				elsif (/^Author:\s+([^<>]*)\s+<(.*)>\s*$/i)
> +				{
> +					$this_author_name = $1;
> +					$this_author_email = $2;
> +				}
> +				else
> +				{
> +					$commit_msg .= $_;
> +				}

Do you really want to slurp Committer:/Author: lines from _anywhere_ in
the file?  Wouldn't it make more sense to vaguely emulate e-mail message
format with headers, empty-line and then body that is free form?

```

## Peter Krefting, 2009-08-27 06:42

Subject: Re: [PATCH v3] import-tars: Allow per-tar author and commit message.
Message-ID: <alpine.DEB.2.00.0908270741170.6459@perkele.intern.softwolves.pp.se>
URL: https://gitlist.dev/e/alpine.DEB.2.00.0908270741170.6459%40perkele.intern.softwolves.pp.se
In-Reply-To: <7viqg96ehf.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano:

> Do you really want to slurp Committer:/Author: lines from _anywhere_ in 
> the file?  Wouldn't it make more sense to vaguely emulate e-mail message 
> format with headers, empty-line and then body that is free form?

I just tried not to overdo it, and keep the parsing code as simple as 
possible. I wasn't trying to implement an RFC 5322 compliant parser...

-- 
\\// Peter - http://www.softwolves.pp.se/

```

## Junio C Hamano, 2009-08-28 01:38

Subject: Re: [PATCH v3] import-tars: Allow per-tar author and commit message.
Message-ID: <7vskfcd8ep.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vskfcd8ep.fsf%40alter.siamese.dyndns.org
In-Reply-To: <alpine.DEB.2.00.0908270741170.6459@perkele.intern.softwolves.pp.se>

```
Peter Krefting <peter@softwolves.pp.se> writes:

> Junio C Hamano:
>
>> Do you really want to slurp Committer:/Author: lines from _anywhere_
>> in the file?  Wouldn't it make more sense to vaguely emulate e-mail
>> message format with headers, empty-line and then body that is free
>> form?
>
> I just tried not to overdo it, and keep the parsing code as simple as
> possible. I wasn't trying to implement an RFC 5322 compliant parser...

It is not about overdoing, but about not glossly underdoing.

Don't you at least want to avoid misparsing a msg file that looks like
this?

	Author: Peter Krefting <peter@softwolves.pp.se>

	import-tars: Allow per-tar author and commit message

        This version updates the import-tars program so that another
	file next to the archive can be read for the log message and
        other meta information.  A line that begins with Committer: or
        Author: is used as long as it consists of name and <email>
	to override the corresponding metainformation.  Remaining lines
	are used as the commit log message.

And I do not think you need a complex parser.  Stop paying attention to a
line that begins with Author:/Committer:, once you see a line that does
not match the pattern; and you would be Ok.

IOW, something like...

	my $reading_metainfo = 1;
        my $squashing_empty = 0;
	while (<>) {
        	if ($reading_metainfo) {
			if (/^Committer:.../) {
                        	...
				next;
			} elsif (/^Author:.../) {
                        	...
				next;
			} else {
                        	$reading_metainfo = 0;
			}
                }
                if (/^$/) {
			$squashing_empty = 1;
                        next;
		}
                if ($squashing_empty && $commit_msg ne '') {
                	$commit_msg .= "\n";
		}
                $commit_msg .= $_;
                $squashing_empty = 0;
	}

```

## Peter Krefting, 2009-08-28 18:56

Subject: Re: [PATCH v3] import-tars: Allow per-tar author and commit message.
Message-ID: <alpine.DEB.2.00.0908281955160.24273@ds9.cixit.se>
URL: https://gitlist.dev/e/alpine.DEB.2.00.0908281955160.24273%40ds9.cixit.se
In-Reply-To: <7vskfcd8ep.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano:

> Don't you at least want to avoid misparsing a msg file that looks like
> this?

Good point... Will post a fixed version.

-- 
\\// Peter - http://www.softwolves.pp.se/

```
