Go to navigation

Code lost

Hugo Wetterberg - 12 oktober

If you're anything like me you probably misplace some code every once in a while. I have a drupal module that I work on in both spare and work time, and I use it in many projects. I sat down just now and started coding on a feature when I realized that I've already done it somewhere else - but where? Somehow I had to find it, and being a programmer, doing it manually isn't an option. So here follows a quick, osx only, tip on how to sift through folders by combining spotlight searches with your own logic.

Fortunately I knew that I had deleted a certain file in the new version, so checking for candidates would be pretty easy. The following snippet could act as a sort of boilerplate code for your own search and rescue missions. It finds a certain folder through spotlight, checks for a condition that makes it a likely candidate for being what you want, and then it fires it up in gitx (if it's a git repo), or just tells you about the candidate by writing it's path to the terminal.

#!/usr/bin/env php
<?php
$modules = split("\n", shell_exec("mdfind oauth_common kind:folder"));

foreach ($modules as $p) {
  if (!empty($p)) {
    if (!file_exists($p . '/includes/OAuthCommonHooks.inc')) {
      if (file_exists($p . '/.git')) {
        chdir($p);
        shell_exec("gitx --all");
      }
      else {
        print "Candidate: $p\n";
      }
    }
  }
}

Unfortunately the code stayed lost, but this snippet lives on and I will probably use it again and maybe it will help somebody else.