Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 76 lines (68 sloc) 2.37 KB
#!/usr/local/bin/php
<?php
/*
* Copyright 2017 Shaun Cummiskey, <shaun@shaunc.com> <http://shaunc.com>
* <https://github.com/parseword/util-misc/>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Find all top-level git or svn repositories on a system. This assumes
*
* 1. Your mlocate database is current and your "locate" command works;
* 2. You don't have nested repos (one repo checked out inside another)
*
* Usage: find-repos [git|svn]
*
* Output: A list of repository file paths suitable for piping to xargs
* e.g. find-repos svn | xargs svn status
*/
//Running as a regular user won't necessarily find all repositories
if (posix_getuid() !== 0) {
echo "You must become root to run this utility.\n";
exit(-1);
}
//Either "git" or "svn" must be passed as the only command line argument
if (count($argv) != 2 || !in_array($argv[1], ['git', 'svn'])) {
echo "Usage: {$argv['0']} [git|svn]\n";
exit(-1);
}
$return = $output = null;
exec('/usr/bin/locate ' . $argv[1] . ' | /bin/grep -E "\/\.' . $argv[1] . '$"',
$output, $return);
//grep returns 0 if match, 1 if no match
if ($return === 0) {
$repos = array();
foreach ($output as $path) {
$temp = $path = pathinfo(trim($path))['dirname'];
//See if we already identified a parent directory as a repository
while ($parent = pathinfo($temp)) {
if (in_array($parent['dirname'], $repos)) {
//This directory is part of a higher level repo, skip it
break;
}
$temp = $parent['dirname'];
if ($temp == '/') {
//No parent repo found, this is a top level repo
$repos[] = $path;
break;
}
}
}
//Display results if any
if (count($repos) > 0) {
foreach ($repos as $repo) {
echo "$repo\n";
}
}
}