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

Git.pm: Add support for subdirectories inside of working copies

This patch adds support for subdirectories inside of working copies; you can specify them in the constructor either as the Directory option (it will just get autodetected using rev-parse) or explicitly using the WorkingSubdir option. This makes Git->repository() do the exact same path setup and repository lookup as the Git porcelain does. This patch also introduces repo_path(), wc_path() and wc_subdir() accessor methods and wc_chdir() mutator. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Jun 24, 2006 at 04:34 UTC d5c7721d586225c46c675b893b7693220e28cfd5
1 file changed +129 -28
perl/Git.pm
+129 -28
@@ -69,20 +69,18 @@ means getting an instance of the Git object using the repository() constructor.
69 called as methods of the object are then executed in the context of the
70 repository.
71
72 -TODO: In the future, we might also do
72 +Part of the "repository state" is also information about path to the attached
73 +working copy (unless you work with a bare repository). You can also navigate
74 +inside of the working copy using the C<wc_chdir()> method. (Note that
75 +the repository object is self-contained and will not change working directory
76 +of your process.)
77
74 - my $subdir = $repo->subdir('Documentation');
75 - # Gets called in the subdirectory context:
76 - $subdir->command('status');
78 +TODO: In the future, we might also do
79
80 my $remoterepo = $repo->remote_repository (Name => 'cogito', Branch => 'master');
81 $remoterepo ||= Git->remote_repository ('http://git.or.cz/cogito.git/');
82 my @refs = $remoterepo->refs();
83
82 -So far, all functions just die if anything goes wrong. If you don't want that,
83 -make appropriate provisions to catch the possible deaths. Better error recovery
84 -mechanisms will be provided in the future.
85 -
84 Currently, the module merely wraps calls to external Git tools. In the future,
85 it will provide a much faster way to interact with Git by linking directly
86 to libgit. This should be completely opaque to the user, though (performance
@@ -93,6 +91,7 @@ increate nonwithstanding).
91
92 use Carp qw(carp croak); # but croak is bad - throw instead
93 use Error qw(:try);
94 +use Cwd qw(abs_path);
95
96 require XSLoader;
97 XSLoader::load('Git', $VERSION);
@@ -119,12 +118,17 @@ B<Repository> - Path to the Git repository.
118 B<WorkingCopy> - Path to the associated working copy; not strictly required
119 as many commands will happily crunch on a bare repository.
120
122 -B<Directory> - Path to the Git working directory in its usual setup. This
123 -is just for convenient setting of both C<Repository> and C<WorkingCopy>
124 -at once: If the directory as a C<.git> subdirectory, C<Repository> is pointed
125 -to the subdirectory and the directory is assumed to be the working copy.
126 -If the directory does not have the subdirectory, C<WorkingCopy> is left
127 -undefined and C<Repository> is pointed to the directory itself.
121 +B<WorkingSubdir> - Subdirectory in the working copy to work inside.
122 +Just left undefined if you do not want to limit the scope of operations.
123 +
124 +B<Directory> - Path to the Git working directory in its usual setup.
125 +The C<.git> directory is searched in the directory and all the parent
126 +directories; if found, C<WorkingCopy> is set to the directory containing
127 +it and C<Repository> to the C<.git> directory itself. If no C<.git>
128 +directory was found, the C<Directory> is assumed to be a bare repository,
129 +C<Repository> is set to point at it and C<WorkingCopy> is left undefined.
130 +If the C<$GIT_DIR> environment variable is set, things behave as expected
131 +as well.
132
133 You should not use both C<Directory> and either of C<Repository> and
134 C<WorkingCopy> - the results of that are undefined.
@@ -134,7 +138,10 @@ to the constructor; it is equivalent to setting only the C<Directory> option
138 field.
139
140 Calling the constructor with no options whatsoever is equivalent to
137 -calling it with C<< Directory => '.' >>.
141 +calling it with C<< Directory => '.' >>. In general, if you are building
142 +a standard porcelain command, simply doing C<< Git->repository() >> should
143 +do the right thing and setup the object to reflect exactly where the user
144 +is right now.
145
146 =cut
147
@@ -152,18 +159,59 @@ sub repository {
159 } else {
160 %opts = @args;
161 }
162 + }
163 +
164 + if (not defined $opts{Repository} and not defined $opts{WorkingCopy}) {
165 + $opts{Directory} ||= '.';
166 + }
167 +
168 + if ($opts{Directory}) {
169 + -d $opts{Directory} or throw Error::Simple("Directory not found: $!");
170 +
171 + my $search = Git->repository(WorkingCopy => $opts{Directory});
172 + my $dir;
173 + try {
174 + $dir = $search->command_oneline(['rev-parse', '--git-dir'],
175 + STDERR => 0);
176 + } catch Git::Error::Command with {
177 + $dir = undef;
178 + };
179
156 - if ($opts{Directory}) {
157 - -d $opts{Directory} or throw Error::Simple("Directory not found: $!");
158 - if (-d $opts{Directory}."/.git") {
159 - # TODO: Might make this more clever
160 - $opts{WorkingCopy} = $opts{Directory};
161 - $opts{Repository} = $opts{Directory}."/.git";
162 - } else {
163 - $opts{Repository} = $opts{Directory};
180 + if ($dir) {
181 + $opts{Repository} = abs_path($dir);
182 +
183 + # If --git-dir went ok, this shouldn't die either.
184 + my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
185 + $dir = abs_path($opts{Directory}) . '/';
186 + if ($prefix) {
187 + if (substr($dir, -length($prefix)) ne $prefix) {
188 + throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix");
189 + }
190 + substr($dir, -length($prefix)) = '';
191 }
165 - delete $opts{Directory};
192 + $opts{WorkingCopy} = $dir;
193 + $opts{WorkingSubdir} = $prefix;
194 +
195 + } else {
196 + # A bare repository? Let's see...
197 + $dir = $opts{Directory};
198 +
199 + unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") {
200 + # Mimick git-rev-parse --git-dir error message:
201 + throw Error::Simple('fatal: Not a git repository');
202 + }
203 + my $search = Git->repository(Repository => $dir);
204 + try {
205 + $search->command('symbolic-ref', 'HEAD');
206 + } catch Git::Error::Command with {
207 + # Mimick git-rev-parse --git-dir error message:
208 + throw Error::Simple('fatal: Not a git repository');
209 + }
210 +
211 + $opts{Repository} = abs_path($dir);
212 }
213 +
214 + delete $opts{Directory};
215 }
216
217 $self = { opts => \%opts };
@@ -256,7 +304,7 @@ sub command_oneline {
304 my ($fh, $ctx) = command_output_pipe(@_);
305
306 my $line = <$fh>;
259 - chomp $line;
307 + defined $line and chomp $line;
308 try {
309 _cmd_close($fh, $ctx);
310 } catch Git::Error::Command with {
@@ -374,7 +422,7 @@ are involved.
422
423 =item exec_path ()
424
377 -Return path to the git sub-command executables (the same as
425 +Return path to the Git sub-command executables (the same as
426 C<git --exec-path>). Useful mostly only internally.
427
428 Implementation of this function is very fast; no external command calls
@@ -385,6 +433,58 @@ are involved.
433 # Implemented in Git.xs.
434
435
436 +=item repo_path ()
437 +
438 +Return path to the git repository. Must be called on a repository instance.
439 +
440 +=cut
441 +
442 +sub repo_path { $_[0]->{opts}->{Repository} }
443 +
444 +
445 +=item wc_path ()
446 +
447 +Return path to the working copy. Must be called on a repository instance.
448 +
449 +=cut
450 +
451 +sub wc_path { $_[0]->{opts}->{WorkingCopy} }
452 +
453 +
454 +=item wc_subdir ()
455 +
456 +Return path to the subdirectory inside of a working copy. Must be called
457 +on a repository instance.
458 +
459 +=cut
460 +
461 +sub wc_subdir { $_[0]->{opts}->{WorkingSubdir} ||= '' }
462 +
463 +
464 +=item wc_chdir ( SUBDIR )
465 +
466 +Change the working copy subdirectory to work within. The C<SUBDIR> is
467 +relative to the working copy root directory (not the current subdirectory).
468 +Must be called on a repository instance attached to a working copy
469 +and the directory must exist.
470 +
471 +=cut
472 +
473 +sub wc_chdir {
474 + my ($self, $subdir) = @_;
475 +
476 + $self->wc_path()
477 + or throw Error::Simple("bare repository");
478 +
479 + -d $self->wc_path().'/'.$subdir
480 + or throw Error::Simple("subdir not found: $!");
481 + # Of course we will not "hold" the subdirectory so anyone
482 + # can delete it now and we will never know. But at least we tried.
483 +
484 + $self->{opts}->{WorkingSubdir} = $subdir;
485 +}
486 +
487 +
488 =item hash_object ( FILENAME [, TYPE ] )
489
490 =item hash_object ( FILEHANDLE [, TYPE ] )
@@ -584,8 +684,9 @@ sub _command_common_pipe {
684 sub _cmd_exec {
685 my ($self, @args) = @_;
686 if ($self) {
587 - $self->{opts}->{Repository} and $ENV{'GIT_DIR'} = $self->{opts}->{Repository};
588 - $self->{opts}->{WorkingCopy} and chdir($self->{opts}->{WorkingCopy});
687 + $self->repo_path() and $ENV{'GIT_DIR'} = $self->repo_path();
688 + $self->wc_path() and chdir($self->wc_path());
689 + $self->wc_subdir() and chdir($self->wc_subdir());
690 }
691 _execv_git_cmd(@args);
692 die "exec failed: $!";