Last active
July 27, 2018 09:37
-
-
Save mfrischknecht/baea22181c6554e8785a1874154f64b5 to your computer and use it in GitHub Desktop.
Git aliases
This file contains 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 bash | |
find_branch () { | |
for branch in ${1+"$@"}; do | |
if [ `git branch --list "$branch" | wc -l` -gt 0 ]; then | |
echo "$branch" | |
break | |
fi | |
done | |
} | |
find_remote () { | |
for remote in ${1+"$@"}; do | |
if [ `git remote | egrep "^$remote$" | wc -l` -gt 0 ]; then | |
echo "$remote" | |
break | |
fi | |
done | |
} | |
upstream=$(find_remote upstream origin) | |
origin=$(find_remote origin) | |
active=$(git branch --list | awk '/^\*/ { print substr($0,3) }') | |
upstream_url=$(git remote get-url $upstream) | |
origin_url=$(git remote get-url $origin) | |
find_forked_branches () { | |
git log --format=%H |\ | |
while read hash; do | |
branches=$(git branch -r --contains "$hash" |\ | |
awk '{ print substr($0,3) }' |\ | |
egrep "^$upstream" |\ | |
egrep -v "/HEAD\b" |\ | |
egrep -v "/$active\b") | |
if [ `echo "$branches" | wc -l` -gt 0 ]; then | |
echo "$branches" | sed -E 's:^[^/]+/::' | |
break | |
fi | |
done | |
} | |
get_project_url () { | |
echo "$1" |\ | |
sed -E 's/\.git$//' `# Remove the .git extension from the cloning url` | |
} | |
get_project_id () { | |
get_project_url "$1" |\ | |
sed -E 's:^[^/]+//[^/]+/::' `# Remove protocol/domain`|\ | |
sed -E 's:([^/]+/[^/]+).*$:\1:' `# Remove stuff after the project id`|\ | |
xxd --plain `# To hex`|\ | |
tr -d '\n' `# Remove newlines`|\ | |
sed -E 's/(..)/%\1/g' `# Prepend % to every binome` | |
} | |
# Open a Gitlab merge request | |
if echo "$upstream_url" | egrep "gitlab" > /dev/null; then | |
forked_branches=$(find_forked_branches) | |
if [ `echo "$branches" | wc -l` -gt 1 ]; then | |
select forked_branch in "${forked_branches}"; do break; done | |
else | |
forked_branch="$forked_branches" | |
fi | |
project_url=$(get_project_url "$upstream_url") | |
upstream_id=$(get_project_id "$upstream_url") | |
origin_id=$(get_project_id "$origin_url") | |
pr_url=$(cat\ | |
<(echo "$project_url/merge_requests/new?utf8=%E2%9C%93&")\ | |
<(echo "merge_request%5Bsource_project_id%5D=$origin_id&")\ | |
<(echo "merge_request%5Bsource_branch%5D=$active&")\ | |
<(echo "merge_request%5Btarget_project_id%5D=$upstream_id&")\ | |
<(echo "merge_request%5Btarget_project_id%5D=$forked_branch")\ | |
| tr -d '\n') | |
echo "$pr_url" | xargs $(command -v start xdg-open) | |
fi |
This file contains 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 bash | |
git config --global alias.browse '!git config remote.origin.url | xargs `command -v start xdg-open`' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment