Skip to content

Commit c7a30e5

Browse files
Petr BaudisJunio C Hamano
authored andcommitted
Git.pm: Introduce ident() and ident_person() methods
These methods can retrieve/parse the author/committer ident. Signed-off-by: Petr Baudis <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3cb8caf commit c7a30e5

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

git-send-email.perl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,8 @@
8484

8585
# Now, let's fill any that aren't set in with defaults:
8686

87-
sub gitvar_ident {
88-
my ($name) = @_;
89-
my $val = $repo->command('var', $name);
90-
my @field = split(/\s+/, $val);
91-
return join(' ', @field[0...(@field-3)]);
92-
}
93-
94-
my ($author) = gitvar_ident('GIT_AUTHOR_IDENT');
95-
my ($committer) = gitvar_ident('GIT_COMMITTER_IDENT');
87+
my ($author) = $repo->ident_person('author');
88+
my ($committer) = $repo->ident_person('committer');
9689

9790
my %aliases;
9891
my @alias_files = $repo->config('sendemail.aliasesfile');

perl/Git.pm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,55 @@ sub config {
521521
}
522522

523523

524+
=item ident ( TYPE | IDENTSTR )
525+
526+
=item ident_person ( TYPE | IDENTSTR | IDENTARRAY )
527+
528+
This suite of functions retrieves and parses ident information, as stored
529+
in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus
530+
C<TYPE> can be either I<author> or I<committer>; case is insignificant).
531+
532+
The C<ident> method retrieves the ident information from C<git-var>
533+
and either returns it as a scalar string or as an array with the fields parsed.
534+
Alternatively, it can take a prepared ident string (e.g. from the commit
535+
object) and just parse it.
536+
537+
C<ident_person> returns the person part of the ident - name and email;
538+
it can take the same arguments as C<ident> or the array returned by C<ident>.
539+
540+
The synopsis is like:
541+
542+
my ($name, $email, $time_tz) = ident('author');
543+
"$name <$email>" eq ident_person('author');
544+
"$name <$email>" eq ident_person($name);
545+
$time_tz =~ /^\d+ [+-]\d{4}$/;
546+
547+
Both methods must be called on a repository instance.
548+
549+
=cut
550+
551+
sub ident {
552+
my ($self, $type) = @_;
553+
my $identstr;
554+
if (lc $type eq lc 'committer' or lc $type eq lc 'author') {
555+
$identstr = $self->command_oneline('var', 'GIT_'.uc($type).'_IDENT');
556+
} else {
557+
$identstr = $type;
558+
}
559+
if (wantarray) {
560+
return $identstr =~ /^(.*) <(.*)> (\d+ [+-]\d{4})$/;
561+
} else {
562+
return $identstr;
563+
}
564+
}
565+
566+
sub ident_person {
567+
my ($self, @ident) = @_;
568+
$#ident == 0 and @ident = $self->ident($ident[0]);
569+
return "$ident[0] <$ident[1]>";
570+
}
571+
572+
524573
=item hash_object ( TYPE, FILENAME )
525574
526575
=item hash_object ( TYPE, FILEHANDLE )

0 commit comments

Comments
 (0)