Skip to content

Commit

Permalink
Be more flexible about finding git repos.
Browse files Browse the repository at this point in the history
- If directories are passed as arguments to `git wip` then they will be
  used as the root directories to search for git repos.
- If the environment variable `GIT_REPO_HOME` is set, then it will be
  treated as a space separated list of directories to search for repos.
- The script now works while in subdirectories of git repos, not just
  the root directory.
  • Loading branch information
conormcd committed Sep 25, 2012
1 parent c399395 commit e372c0e
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions git-wip
Expand Up @@ -33,16 +33,23 @@ use warnings;
use Cwd qw(abs_path cwd);
use File::Basename;
use File::Find;
use File::Spec;

# Find the repo dirs to scan for work-in-progress
#
# If we're in a git checkout, just show the WIP for the current repo.
# Otherwise, search for the git repos.
my @repo_dirs;
if (-d '.git') {
@repo_dirs = (abs_path('.'));
if (@ARGV) {
@repo_dirs = &repo_dirs(grep { -d $_ } @ARGV);
} elsif ($ENV{'GIT_REPO_HOME'}) {
@repo_dirs = &repo_dirs(grep { -d $_ } split /\s+/o, $ENV{'GIT_REPO_HOME'});
} else {
@repo_dirs = &repo_dirs();
if (my $repo_dir = is_git_repo('.')) {
@repo_dirs = ($repo_dir);
} else {
@repo_dirs = &repo_dirs($ENV{'HOME'});
}
}

# Calculate the WIP and output it.
Expand Down Expand Up @@ -150,10 +157,6 @@ sub repo_dirs {
my @root_dirs = @_;
my @repo_dirs;

if ($ENV{'HOME'}) {
push @root_dirs, $ENV{'HOME'};
}

find(
sub {
if (-d "$File::Find::name/.git") {
Expand All @@ -168,6 +171,29 @@ sub repo_dirs {
return @repo_dirs;
}

# Test if a directory is within a git repository.
#
# @param $dir The directory to test.
#
# @return If $dir is a directory inside a git repo, return the full path to
# the root of the repo. If $dir is not a directory inside a git repo,
# return false.
sub is_git_repo {
my $dir = shift;

unless (File::Spec->file_name_is_absolute($dir)) {
$dir = abs_path($dir);
}

my @dir_portions = File::Spec->splitdir($dir);
for (my $i = @dir_portions - 1; $i >= 0; $i--) {
if (-d File::Spec->catdir((@dir_portions[0..$i], '.git'))) {
return File::Spec->catdir(@dir_portions[0..$i]);
}
}
return undef;
}

# Run a git command.
#
# @param $repo The directory in which to run the command.
Expand Down

0 comments on commit e372c0e

Please sign in to comment.