Skip to content

Instantly share code, notes, and snippets.

@mspreij
Created July 15, 2016 09:44
Show Gist options
  • Save mspreij/821cf4aa41ff0c3871704af4089063a3 to your computer and use it in GitHub Desktop.
Save mspreij/821cf4aa41ff0c3871704af4089063a3 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
ob_start();
require 'inc/func.inc.php';
if ($argv[0] == 'php') array_shift($argv);
$script = array_shift($argv); // CLI input array, first item = scriptname, discard
// usage:
if (count($argv) == 0) {
echo " Usage: tm <file1> <file2> .., where file can be a substring too.\n";
die();
}
/*
combine
git status -s -uno | cut -d\ -f3 | xargs ls -t | grep php | head -n1
and
git log --name-status | grep -E "^[AM]\s" | cut -f2 | grep -i php | head -n1
for partial file patterns, list by .. date.. or something. somehow.
*/
$pwd = trim(`pwd`);
$gitToplevel = trim(`git rev-parse --show-toplevel`);
$files = array();
foreach ($argv as $item) {
if (file_exists($item)) {
$files[] = $pwd.'/'.$item; // <3
}else{
$escaped = escapeshellcmd($item);
// it's not a file right *here*, check git status for recently modified ones
// both working dir & staged files are returned.
$file = `echo git status -s -uno | cut -d\ -f3 | xargs ls -t | grep -i $escaped | head -n1`;
if (trim($file)) {
// git status returns relative filepaths
if (file_exists($file)) {
$file = realpath($pwd.'/'.$file);
$files[] = $file;
}else{
echo styled("file ".var_export($file, 1)." matched (git status) but no longer exists..\n");
}
}else{
// bugger! check moar. this doesn't /guarantee/ to return the last edited matching file,
// but it's close enough..
$file = `echo git log --name-status | grep -E "^[AM]\s" | cut -f2 | grep -i $escaped | head -n1`;
// no matches here pretty much mean the file never existed
if (trim($file)) {
// these paths are relative to the git project root
$gitFile = $file;
$file = $gitToplevel.'/'.$file;
if (file_exists($file)) {
$files[] = $file;
}else{
echo styled("file ".var_export($gitFile, 1)." matched (git log) but no longer exists..\n");
}
}
}
}
}
if (count($files)) {
$fullPaths = '/Volumes'. join(' /Volumes', $files); // ugly? your mom
shell_exec("ssh maarten@{$_SERVER['MACMINI_IP']} /usr/local/bin/mate $fullPaths");
}else{
echo " No matches.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment