Created
November 7, 2012 21:47
-
-
Save abackstrom/4034721 to your computer and use it in GitHub Desktop.
git-url: display github urls on the command line
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
#!/bin/env php | |
<?php | |
// | |
// Save to your path as git-url. | |
// | |
// Display GitHub URLs for your current branch and last commit: | |
// | |
// git url | |
// | |
// Display URLs and shortened forms (you must define doShorten()): | |
// | |
// git url -s | |
// | |
exec('git remote -v', $output); | |
foreach ($output as $url) { | |
if (!preg_match('/^origin\s+(\S+)\s*\(fetch\)$/', $url, $matches)) { | |
continue; | |
} | |
$parts = parse_url($matches[1]); | |
break; | |
} | |
if (!isset($parts)) { | |
exit(1); | |
} | |
$opts = getopt('s'); | |
$shorten = isset($opts['s']); | |
$github_hostname = $parts['host']; | |
$github_repo = substr($parts['path'], 1, -4); // /Foo/Bar.git to Foo/Bar | |
$github_baseurl = "https://{$github_hostname}/$github_repo"; | |
$branch = trim(`git rev-parse --symbolic-full-name --abbrev-ref HEAD`); | |
$commit = trim(`git rev-parse HEAD`); | |
$compare_url = sprintf("%s/compare/%s", $github_baseurl, $branch); | |
$commit_url = sprintf("%s/commit/%s", $github_baseurl, $commit); | |
// function doShorten() { | |
// } | |
if (defined('doShorten') && $shorten) { | |
$compare_url_short = doShorten($compare_url); | |
$commit_url_short = doShorten($commit_url); | |
$urllen = max(strlen($compare_url), strlen($commit_url)); | |
printf("%-{$urllen}s -> %s\n%-{$urllen}s -> %s\n", $compare_url, $compare_url_short, $commit_url, $commit_url_short); | |
} else { | |
printf("%s\n%s\n", $compare_url, $commit_url); | |
} | |
// vim:filetype=php: |
Here's a minimal solution with a single line in .gitconfig
which also works with git's tab completion:
[alias]
hub-commit-url = "!f() { sha=$(git show --pretty=%H ${1:-HEAD}) ; git remote get-url origin | sed -e 's%[email protected]:%https://github.com/%' -e \"s%.git\\$%/commit/$sha%\" ; }; f"
hub-file-url = "!f() { sha=$(git rev-parse $1) ; git remote get-url origin | sed -e 's%[email protected]:%https://github.com/%' -e \"s%.git\\$%/blob/$sha/$2%\" ; }; f"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sooo sexy seeing people right PHP for the CLI. :)