Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Config / GitLike / Git.pm
1 package Config::GitLike::Git;
2 use Any::Moose;
3 use strict;
4 use warnings;
5
6 extends 'Config::GitLike';
7
8 has 'confname' => (
9     default => 'gitconfig',
10 );
11
12 has 'compatible' => (
13     default => 1,
14 );
15
16 sub is_git_dir {
17     my $self = shift;
18     my $path = File::Spec->rel2abs( shift );
19     $path =~ s{/+$}{};
20
21     ($path) = grep {-d} map {"$path$_"} (".git/.git", "/.git", ".git", "");
22     return unless $path;
23
24     # Has to have objects/ and refs/ directories
25     return unless -d "$path/objects" and -d "$path/refs";
26
27     # Has to have a HEAD file
28     return unless -f "$path/HEAD";
29
30     if (-l "$path/HEAD" ) {
31         # Symbolic link into refs/
32         return unless readlink("$path/HEAD") =~ m{^refs/};
33     } else {
34         open(HEAD, "$path/HEAD") or return;
35         my ($line) = <HEAD>;
36         close HEAD;
37         # Is either 'ref: refs/whatever' or a sha1
38         return unless $line =~ m{^(ref:\s*refs/|[0-9a-fA-F]{20})};
39     }
40     return $path;
41 }
42
43 sub load_dirs {
44     my $self = shift;
45     my $path = shift;
46     my $dir = $self->is_git_dir($path) or return;
47     $self->load_file( File::Spec->catfile( $dir, "config" ) );
48 }
49
50 __PACKAGE__->meta->make_immutable;
51 no Any::Moose;
52
53 1;
54
55 __END__
56
57 =head1 NAME
58
59 Config::GitLike::Git - load Git configuration files
60
61 =head1 SYNOPSIS
62
63     use Config::GitLike::Git;
64     my $config = Config::GitLike::Git->new;
65     $config->load("/path/to/repo");
66
67 =head1 DESCRIPTION
68
69 This is a modification of L<Config::GitLike> to look at the same
70 locations that Git writes to. Unlike with L<Config::GitLike>, you do
71 not need to pass a confname to its constructor. This module also
72 enables the L<Config::GitLike> option to maintain git compatibility
73 when reading and writing variables.
74
75 L<Config::GitLike/load> should be passed path to the top level of a
76 git repository -- this defaults to the current directory.  It will
77 append C<.git> as necessary.  It supports both bare and non-bare
78 repositories.
79
80 =head1 METHODS
81
82 This module overrides these methods from C<Config::GitLike>:
83
84 =head2 dir_file
85
86 The per-directory configuration file is F<.git/config>
87
88 =head2 user_file
89
90 The per-user configuration file is F<~/.gitconfig>
91
92 =head2 global_file
93
94 The per-host configuration file is F</etc/gitconfig>
95
96 =head1 SEE ALSO
97
98 L<Config::GitLike|Config::GitLike>
99
100 =head1 LICENSE
101
102 You may modify and/or redistribute this software under the same terms
103 as Perl 5.8.8.
104
105 =head1 COPYRIGHT
106
107 Copyright 2010 Best Practical Solutions, LLC
108
109 =head1 AUTHORS
110
111 Alex Vandiver <alexmv@bestpractical.com>,
112 Christine Spang <spang@bestpractical.com>