Make it possible to set up libgit directly (instead of from the environment)
[gitweb.git] / perl / Git.pm
index 24fd7ce25c20f85cea10fb8ffec1cebf9c38a6af..9da15e9c8c208ffd6a51b524edc828c85dce5355 100644 (file)
@@ -98,6 +98,8 @@ =head1 DESCRIPTION
 
 }
 
+my $instance_id = 0;
+
 
 =head1 CONSTRUCTORS
 
@@ -215,7 +217,7 @@ sub repository {
                delete $opts{Directory};
        }
 
-       $self = { opts => \%opts };
+       $self = { opts => \%opts, id => $instance_id++ };
        bless $self, $class;
 }
 
@@ -521,6 +523,55 @@ sub config {
 }
 
 
+=item ident ( TYPE | IDENTSTR )
+
+=item ident_person ( TYPE | IDENTSTR | IDENTARRAY )
+
+This suite of functions retrieves and parses ident information, as stored
+in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus
+C<TYPE> can be either I<author> or I<committer>; case is insignificant).
+
+The C<ident> method retrieves the ident information from C<git-var>
+and either returns it as a scalar string or as an array with the fields parsed.
+Alternatively, it can take a prepared ident string (e.g. from the commit
+object) and just parse it.
+
+C<ident_person> returns the person part of the ident - name and email;
+it can take the same arguments as C<ident> or the array returned by C<ident>.
+
+The synopsis is like:
+
+       my ($name, $email, $time_tz) = ident('author');
+       "$name <$email>" eq ident_person('author');
+       "$name <$email>" eq ident_person($name);
+       $time_tz =~ /^\d+ [+-]\d{4}$/;
+
+Both methods must be called on a repository instance.
+
+=cut
+
+sub ident {
+       my ($self, $type) = @_;
+       my $identstr;
+       if (lc $type eq lc 'committer' or lc $type eq lc 'author') {
+               $identstr = $self->command_oneline('var', 'GIT_'.uc($type).'_IDENT');
+       } else {
+               $identstr = $type;
+       }
+       if (wantarray) {
+               return $identstr =~ /^(.*) <(.*)> (\d+ [+-]\d{4})$/;
+       } else {
+               return $identstr;
+       }
+}
+
+sub ident_person {
+       my ($self, @ident) = @_;
+       $#ident == 0 and @ident = $self->ident($ident[0]);
+       return "$ident[0] <$ident[1]>";
+}
+
+
 =item hash_object ( TYPE, FILENAME )
 
 =item hash_object ( TYPE, FILEHANDLE )
@@ -784,11 +835,10 @@ sub _call_gate {
        if (defined $self) {
                # XXX: We ignore the WorkingCopy! To properly support
                # that will require heavy changes in libgit.
+               # For now, when we will need to do it we could temporarily
+               # chdir() there and then chdir() back after the call is done.
 
-               # XXX: And we ignore everything else as well. libgit
-               # at least needs to be extended to let us specify
-               # the $GIT_DIR instead of looking it up in environment.
-               #xs_call_gate($self->{opts}->{Repository});
+               xs__call_gate($self->{id}, $self->repo_path());
        }
 
        # Having to call throw from the C code is a sure path to insanity.