forked from carlacummins/carla-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo_root.pl
More file actions
executable file
·44 lines (34 loc) · 960 Bytes
/
Copy pathrepo_root.pl
File metadata and controls
executable file
·44 lines (34 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env perl
=head1 Description
This script is used in conjunction with .bashrc to cd to the root of
the current git repo. To make the dir change persistent, perl
cannot do this part (it would only change the perl process' working dir),
so an alias 'cd-repo-root' is used to change to the directory identified here
alias cd-repo-root='cd $(repo_root.pl)'
=cut
use strict;
use warnings;
use Cwd;
my $dir = getcwd;
my $root_found = 0;
while ( !$root_found && $dir ne '' ) {
if ( -e "$dir/.git" ){
print "$dir\n";
$root_found = 1;
} else {
$dir = one_level_up($dir);
}
}
# if it's run outside of a git repo structure, print a message
# and return CWD to the cd command to stay where we are
unless ( $root_found ) {
my $cwd = getcwd;
print STDERR "$cwd does not appear to be in a git repository..\n";
print "$cwd\n";
}
sub one_level_up {
my $dir = shift;
my @parts = split('/', $dir);
pop @parts;
return join('/', @parts);
}