add new Perl API: Git::Repo, Git::Commit, Git::Tag, and Git::RepoRoot
[git/gitweb-caching.git] / perl / Git / RepoRoot.pm
blob6c8e7495f4b42623d9c6b5bc786117048ef4eee4
1 =head1 NAME
3 Git::RepoRoot - A factory class representing a root directory
4 containing Git repositories.
6 =head1 DESCRIPTION
8 Git::RepoRoot is a factory class to create L<Git::Repo> instances that
9 are located under a common root directory. It also allows for
10 specifying options that all Git::Repo instances will be created with.
12 Using Git::RepoRoot to create Git::Repo instances is entirely
13 optional, but can be more convenient than instantiating them directly.
15 =cut
18 use strict;
19 use warnings;
22 package Git::RepoRoot;
24 use File::Spec;
26 use Git::Repo;
28 use base qw(Exporter);
30 our @EXPORT = qw();
31 our @EXPORT_OK = qw();
33 =head1 METHODS
35 =over
37 =item $repo_root = Git::RepoRoot->new(%opts)
39 Return a new Git::RepoRoot object. The following options are
40 supported:
42 =over
44 =item 'root_dir'
46 The directory holding all repositories.
48 =back
50 All other options will be passed through to Git::Repo->new.
52 Example:
54 $repo_root = Git::RepoRoot->new(root_dir => '/pub/git',
55 git_binary => '/usr/bin/git');
57 =cut
59 sub new {
60 my $class = shift;
61 Git::Repo::_assert_opts(@_);
62 my $self = {@_};
63 bless $self, $class;
64 die 'no root_dir given' unless $self->{root_dir};
65 return $self;
68 =item $repo_root->repo(%opts)
70 Return a new L<Git::Repo> object. The following options are
71 supported:
73 =over
75 =item 'root_dir'
77 The path of the repository relative to the repository root.
79 =item 'repo_class'
81 The Repo class to instantiate (default: 'Git::Repo').
83 =back
85 All other options are passed through to Git::Repo.
87 =cut
89 sub repo {
90 my $self = shift;
91 Git::Repo::_assert_opts(@_);
92 my %opts = (%$self, @_);
93 die 'no repo_dir given' unless $opts{repo_dir};
94 # not technically necessary, but to guard against errors in the caller:
95 die "you passed an absolute path ($opts{repo_dir})"
96 if $opts{repo_dir} =~ m!^/!;
97 my $repo_class = delete $opts{repo_class} || 'Git::Repo';
98 $opts{repo_dir} = File::Spec->catfile($self->{root_dir}, $opts{repo_dir});
99 return $repo_class->new(%opts);