Skip to content

Commit 769f60a

Browse files
committed
Merge branch 'fl/send-email-outside'
* fl/send-email-outside: send-email: Don't require to be called in a repository Git.pm: Don't require repository instance for ident Git.pm: Don't require a repository instance for config var: Don't require to be in a git repository.
2 parents 3ee5538 + ad79c02 commit 769f60a

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

git-send-email.perl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ sub format_2822_time {
168168
# Example reply to:
169169
#$initial_reply_to = ''; #<[email protected]>';
170170

171-
my $repo = Git->repository();
171+
my $repo = eval { Git->repository() };
172+
my @repo = $repo ? ($repo) : ();
172173
my $term = eval {
173174
$ENV{"GIT_SEND_EMAIL_NOTTY"}
174175
? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT
@@ -271,25 +272,25 @@ sub read_config {
271272

272273
foreach my $setting (keys %config_bool_settings) {
273274
my $target = $config_bool_settings{$setting}->[0];
274-
$$target = $repo->config_bool("$prefix.$setting") unless (defined $$target);
275+
$$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target);
275276
}
276277

277278
foreach my $setting (keys %config_settings) {
278279
my $target = $config_settings{$setting};
279280
if (ref($target) eq "ARRAY") {
280281
unless (@$target) {
281-
my @values = $repo->config("$prefix.$setting");
282+
my @values = Git::config(@repo, "$prefix.$setting");
282283
@$target = @values if (@values && defined $values[0]);
283284
}
284285
}
285286
else {
286-
$$target = $repo->config("$prefix.$setting") unless (defined $$target);
287+
$$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target);
287288
}
288289
}
289290
}
290291

291292
# read configuration from [sendemail "$identity"], fall back on [sendemail]
292-
$identity = $repo->config("sendemail.identity") unless (defined $identity);
293+
$identity = Git::config(@repo, "sendemail.identity") unless (defined $identity);
293294
read_config("sendemail.$identity") if (defined $identity);
294295
read_config("sendemail");
295296

@@ -327,8 +328,9 @@ sub read_config {
327328
}
328329
}
329330

330-
my ($repoauthor) = $repo->ident_person('author');
331-
my ($repocommitter) = $repo->ident_person('committer');
331+
my ($repoauthor, $repocommitter);
332+
($repoauthor) = Git::ident_person(@repo, 'author');
333+
($repocommitter) = Git::ident_person(@repo, 'committer');
332334

333335
# Verify the user input
334336

@@ -415,7 +417,7 @@ sub read_config {
415417

416418
my $prompting = 0;
417419
if (!defined $sender) {
418-
$sender = $repoauthor || $repocommitter;
420+
$sender = $repoauthor || $repocommitter || '';
419421

420422
while (1) {
421423
$_ = $term->readline("Who should the emails appear to be from? [$sender] ");
@@ -509,7 +511,7 @@ sub expand_aliases {
509511
EOT
510512
close(C);
511513

512-
my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
514+
my $editor = $ENV{GIT_EDITOR} || Git::config(@repo, "core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
513515
system('sh', '-c', '$0 $@', $editor, $compose_filename);
514516

515517
open(C2,">",$compose_filename . ".final")

perl/Git.pm

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -487,22 +487,20 @@ does. In scalar context requires the variable to be set only one time
487487
(exception is thrown otherwise), in array context returns allows the
488488
variable to be set multiple times and returns all the values.
489489
490-
Must be called on a repository instance.
491-
492490
This currently wraps command('config') so it is not so fast.
493491
494492
=cut
495493

496494
sub config {
497-
my ($self, $var) = @_;
498-
$self->repo_path()
499-
or throw Error::Simple("not a repository");
495+
my ($self, $var) = _maybe_self(@_);
500496

501497
try {
498+
my @cmd = ('config');
499+
unshift @cmd, $self if $self;
502500
if (wantarray) {
503-
return $self->command('config', '--get-all', $var);
501+
return command(@cmd, '--get-all', $var);
504502
} else {
505-
return $self->command_oneline('config', '--get', $var);
503+
return command_oneline(@cmd, '--get', $var);
506504
}
507505
} catch Git::Error::Command with {
508506
my $E = shift;
@@ -522,20 +520,17 @@ Retrieve the bool configuration C<VARIABLE>. The return value
522520
is usable as a boolean in perl (and C<undef> if it's not defined,
523521
of course).
524522
525-
Must be called on a repository instance.
526-
527523
This currently wraps command('config') so it is not so fast.
528524
529525
=cut
530526

531527
sub config_bool {
532-
my ($self, $var) = @_;
533-
$self->repo_path()
534-
or throw Error::Simple("not a repository");
528+
my ($self, $var) = _maybe_self(@_);
535529

536530
try {
537-
my $val = $self->command_oneline('config', '--bool', '--get',
538-
$var);
531+
my @cmd = ('config', '--bool', '--get', $var);
532+
unshift @cmd, $self if $self;
533+
my $val = command_oneline(@cmd);
539534
return undef unless defined $val;
540535
return $val eq 'true';
541536
} catch Git::Error::Command with {
@@ -557,19 +552,17 @@ or 'g' in the config file will cause the value to be multiplied
557552
by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
558553
It would return C<undef> if configuration variable is not defined,
559554
560-
Must be called on a repository instance.
561-
562555
This currently wraps command('config') so it is not so fast.
563556
564557
=cut
565558

566559
sub config_int {
567-
my ($self, $var) = @_;
568-
$self->repo_path()
569-
or throw Error::Simple("not a repository");
560+
my ($self, $var) = _maybe_self(@_);
570561

571562
try {
572-
return $self->command_oneline('config', '--int', '--get', $var);
563+
my @cmd = ('config', '--int', '--get', $var);
564+
unshift @cmd, $self if $self;
565+
return command_oneline(@cmd);
573566
} catch Git::Error::Command with {
574567
my $E = shift;
575568
if ($E->value() == 1) {
@@ -639,15 +632,15 @@ The synopsis is like:
639632
"$name <$email>" eq ident_person($name);
640633
$time_tz =~ /^\d+ [+-]\d{4}$/;
641634
642-
Both methods must be called on a repository instance.
643-
644635
=cut
645636

646637
sub ident {
647-
my ($self, $type) = @_;
638+
my ($self, $type) = _maybe_self(@_);
648639
my $identstr;
649640
if (lc $type eq lc 'committer' or lc $type eq lc 'author') {
650-
$identstr = $self->command_oneline('var', 'GIT_'.uc($type).'_IDENT');
641+
my @cmd = ('var', 'GIT_'.uc($type).'_IDENT');
642+
unshift @cmd, $self if $self;
643+
$identstr = command_oneline(@cmd);
651644
} else {
652645
$identstr = $type;
653646
}
@@ -659,8 +652,8 @@ sub ident {
659652
}
660653

661654
sub ident_person {
662-
my ($self, @ident) = @_;
663-
$#ident == 0 and @ident = $self->ident($ident[0]);
655+
my ($self, @ident) = _maybe_self(@_);
656+
$#ident == 0 and @ident = $self ? $self->ident($ident[0]) : ident($ident[0]);
664657
return "$ident[0] <$ident[1]>";
665658
}
666659

var.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ static int show_config(const char *var, const char *value)
5151
int main(int argc, char **argv)
5252
{
5353
const char *val;
54+
int nongit;
5455
if (argc != 2) {
5556
usage(var_usage);
5657
}
5758

58-
setup_git_directory();
59+
setup_git_directory_gently(&nongit);
5960
val = NULL;
6061

6162
if (strcmp(argv[1], "-l") == 0) {

0 commit comments

Comments
 (0)