Scanning a file system path for all version control remote repository urls

Just thought I’d share a PHP shell script to scan a file system path, search for all .git/.svn directories recursively, and collect a unique list of all remote repository URLs. It uses “svn info” or “git remote” to get the repository URL path.

// define a path to scan
$scan_path = '/var/www/vhosts';

// keep track of current working dir
$original_cwd = $_SERVER['PWD'];

// get a list of all .git and .svn directories
$files = trim(`find "$scan_path" -type d | egrep -i '\.(git|svn)$'`);

// explode on "\n"
$files = explode("\n", $files);

// loop through files
$repo_list = array();
foreach($files as $key => $file) {

  // .git or .svn ?
  $repo_type = substr($file, -4);
  $repo_path = substr($file, 0, -4);

  switch ($repo_type) {
    case '.svn':

      // get svn repo root
      $repo_root = trim(`svn info "$repo_path" | grep ^Repository\ Root | sed 's/Repository Root: //'`);
      if (!in_array($repo_root, $repo_list)) {
        $repo_list[] = $repo_root;

    case '.git':

      // change dir

      // get git remote path
      $repo_root = trim(`git remote -v | grep -i fetch | awk '{print \$2}' | head -1`);
      if (!in_array($repo_root, $repo_list)) {
        $repo_list[] = $repo_root;


// sort

// go back to cwd

// output repo list into file
file_put_contents('repo_list.txt', implode("\n", $repo_list) . "\n");

I put this PHP in a file called “scan-for-version-control.php”, and run it by typing:

$ php scan-for-version-control.php

It created a file in the current working directory: repo_list.txt containing stuff like..