@matheusbsbs / matheus-bagliano / commits / c7a30e5684
Fork 0

Git.pm: Introduce ident() and ident_person() methods

These methods can retrieve/parse the author/committer ident. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Jul 3, 2006 at 22:48 UTC c7a30e56840b089c1d110b312475f692b5c1b6a4
2 files changed +51 -9
git-send-email.perl
+2 -9
@@ -84,15 +84,8 @@ foreach my $entry (@bcclist) {
84
85 # Now, let's fill any that aren't set in with defaults:
86
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');
89
90 my %aliases;
91 my @alias_files = $repo->config('sendemail.aliasesfile');
perl/Git.pm
+49
@@ -521,6 +521,55 @@ sub config {
521 }
522
523
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 +
573 =item hash_object ( TYPE, FILENAME )
574
575 =item hash_object ( TYPE, FILEHANDLE )