send-email: allow send-email to run outside a repo
[gitweb.git] / git-send-email.perl
index 3112f769cd4b86cbb87fbbb14b5a9d8c4b5fc6bd..9dad10092b56d724b84ff703739b001edebf4a20 100755 (executable)
 use warnings;
 use Term::ReadLine;
 use Getopt::Long;
+use Text::ParseWords;
 use Data::Dumper;
 use Term::ANSIColor;
-use File::Temp qw/ tempdir /;
+use File::Temp qw/ tempdir tempfile /;
 use Error qw(:try);
 use Git;
 
@@ -155,7 +156,10 @@ sub format_2822_time {
 # Behavior modification variables
 my ($quiet, $dry_run) = (0, 0);
 my $format_patch;
-my $compose_filename = $repo->repo_path() . "/.gitsendemail.msg.$$";
+my $compose_filename = ($repo ?
+       tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
+       tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
+
 
 # Handle interactive edition of files.
 my $multiedit;
@@ -266,6 +270,9 @@ sub signal_handler {
     usage();
 }
 
+die "Cannot run git format-patch from outside a repository\n"
+       if $format_patch and not $repo;
+
 # Now, let's fill any that aren't set in with defaults:
 
 sub read_config {
@@ -359,6 +366,10 @@ sub read_config {
        die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
 }
 
+sub split_addrs {
+       return quotewords('\s*,\s*', 1, @_);
+}
+
 my %aliases;
 my %parse_alias = (
        # multiline formats can be supported in the future
@@ -367,7 +378,7 @@ sub read_config {
                        my ($alias, $addr) = ($1, $2);
                        $addr =~ s/#.*$//; # mutt allows # comments
                         # commas delimit multiple addresses
-                       $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
+                       $aliases{$alias} = [ split_addrs($addr) ];
                }}},
        mailrc => sub { my $fh = shift; while (<$fh>) {
                if (/^alias\s+(\S+)\s+(.*)$/) {
@@ -379,7 +390,7 @@ sub read_config {
                        chomp $x;
                        $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
                        $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
-                       $aliases{$1} = [ split(/\s*,\s*/, $2) ];
+                       $aliases{$1} = [ split_addrs($2) ];
                }},
        gnus => sub { my $fh = shift; while (<$fh>) {
                if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
@@ -399,6 +410,7 @@ sub read_config {
 
 # returns 1 if the conflict must be solved using it as a format-patch argument
 sub check_file_rev_conflict($) {
+       return unless $repo;
        my $f = shift;
        try {
                $repo->command('rev-parse', '--verify', '--quiet', $f);
@@ -440,6 +452,8 @@ ($)
 }
 
 if (@rev_list_opts) {
+       die "Cannot run git format-patch from outside a repository\n"
+               unless $repo;
        push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts);
 }
 
@@ -588,7 +602,7 @@ ($)
        }
 
        my $to = $_;
-       push @to, split /,\s*/, $to;
+       push @to, split_addrs($to);
        $prompting++;
 }