Created
February 2, 2012 20:26
-
-
Save justinrainbow/1725563 to your computer and use it in GitHub Desktop.
Add this to your path to add the 'git homepage' command to git. This will open the URL for the current git repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
// This is incredibly ugly... but a quick script for fun | |
exec("git remote -v", $data); | |
$remotes = array(); | |
foreach ($data as $line) { | |
if (preg_match('~^(\w+)\t(.+?(?:\.git)+)~', $line, $match)) { | |
$url = strtr($match[2], array( | |
'[email protected]:' => 'https://github.com/', | |
'git@' => 'git://' | |
)); | |
if ($url) { | |
if ('.git' === substr($url, -4)) { | |
$url = substr($url, 0, -4); | |
} | |
} | |
$url = parse_url($url); | |
$link = sprintf('%s://%s%s', $url['scheme'], $url['host'], $url['path']); | |
$remotes[$match[1]] = $link; | |
} | |
} | |
// determine the current branch | |
$cwd = realpath("."); | |
while (!is_dir("$cwd/.git") && '.' !== $cwd) { | |
$cwd = dirname($cwd); | |
} | |
$head = trim(file_get_contents($cwd.'/.git/HEAD')); | |
$branch = preg_replace('~^ref: refs/heads/~', '', $head); | |
if (isset($remotes['origin'])) { | |
printf("The homepage is at:\n\n %s\n\n", $remotes['origin']); | |
$url = $remotes['origin']; | |
if ($branch) { | |
$url .= '/tree/' . $branch; | |
} | |
exec(sprintf('open "%s" &', $url)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment