Skip to content

Instantly share code, notes, and snippets.

@donatj donatj/ghopen.php
Created Jan 12, 2016

Embed
What would you like to do?
Open current project on Github. OS X only.
#!/usr/bin/env php
<?php
$cwd = getcwd();
$dirs = explode(DIRECTORY_SEPARATOR, $cwd);
$opened = [ ];
for( $i = count($dirs); $i >= 1; $i-- ) {
$set = array_slice($dirs, 0, $i);
$path = implode(DIRECTORY_SEPARATOR, $set);
$gitPath = $path . DIRECTORY_SEPARATOR . '.git';
if( is_dir($gitPath) ) {
$cfgPath = $gitPath . DIRECTORY_SEPARATOR . 'config';
if( is_readable($cfgPath) ) {
$ini = parse_ini_file($cfgPath, true);
foreach( $ini as $sect => $sectContent ) {
if( strpos($sect, "remote ") === 0 && !empty($sectContent['url']) ) {
if( preg_match('%(?:@|/)github\.com(?:/|:)(.*)/(.*)\.git%i', $sectContent['url'], $regs) ) {
if( empty($opened[$regs[1] . $regs[2]]) ) {
$opened[$regs[1] . $regs[2]] = true;
shell_exec('open ' . escapeshellarg("https://github.com/{$regs[1]}/{$regs[2]}"));
}
}
}
}
die(0);
}
}
}
fwrite(STDERR, "remote github repository not found\n");
die(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.