From 5cd7ac66c55e01d79e9704467ef9f88b7759367d Mon Sep 17 00:00:00 2001 From: David Souther Date: Fri, 5 Apr 2013 17:05:29 -0400 Subject: [PATCH 01/24] Added hub-clone commands. --- bin/git-hub-clone | 39 +++++++++++++++++++++++++++++++++++++++ bin/git-hub-helpers | 24 ++++++++++++++++-------- 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100755 bin/git-hub-clone mode change 100644 => 100755 bin/git-hub-helpers diff --git a/bin/git-hub-clone b/bin/git-hub-clone new file mode 100755 index 0000000..5ce2d9b --- /dev/null +++ b/bin/git-hub-clone @@ -0,0 +1,39 @@ +#!/bin/bash + +set -x + +source git-hub-helpers + +clone(){ + FULL_REPO=$1 + REPO=$(echo $FULL_REPO | awk -F/ '{print $2}') + USER=$(github_user) + + github /repos/$FULL_REPO/forks .tmp_fork + + git clone git@github.com:$USER/$REPO.git + cd $REPO + git remote add upstream git@github.com/$FULL_REPO.git + + git config --local --add github-flow.origin origin + git config --local --add github-flow.upstream upstream + git config --local --add github-flow.master master + + echo "Repo cloned into $(pwd)" +} + +usage(){ + echo "$0 : Initialize a new working repository, and set git-hubflow configurations." +} + +help(){ + usage + echo "" + echo " : The organization and repository name to clone from." +} + +if [ "x$1" == "-h"] ; then + help +else + clone $@ +fi diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers old mode 100644 new mode 100755 index d206eb6..f2c9949 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -6,7 +6,7 @@ github(){ DATA=$2 AUTH="Authorization: token $(cat ~/.github_token)" - CURL="curl" + CURL="curl -vi" if [ ! -z "$DATA" ] ; then CURL="$CURL -d @$DATA" fi @@ -23,9 +23,17 @@ github_put(){ $CURL -X PUT -d "$2" -H "$AUTH" "$API$URL" 2>/dev/null } +github_user(){ + USERNAME=$(git config --get github-flow.username) + if [ $? -eq 1 ] ; then + read -p "Github username: " USERNAME + git config --global --add github-flow.username $USERNAME + fi + echo $USERNAME +} + github_login(){ - read -p "Github username: " USERNAME - curl -u $USERNAME -d '{"scopes":["repo"],"note":"git-hub-flow"}' $API/authorizations | json token >| ~/.github_token + curl -u $(github_user) -d '{"scopes":["repo"],"note":"git-hub-flow"}' $API/authorizations | json token >| ~/.github_token } github_pr_number(){ @@ -36,11 +44,11 @@ EOF echo $NUMBER } -ORIGIN=origin -UPSTREAM=origin -MASTER=master +ORIGIN=$(git config --get github-flow.origin) +UPSTREAM=$(git config --get github-flow.upstream) +MASTER=$(git config --get github-flow.master) -GH_ORIGIN=$(git remote -v | grep $ORIGIN | head -1 | awk -F: '{print $2}' | awk -F. '{print $1'}) +GH_ORIGIN=$(git remote -v 2>/dev/null | grep $ORIGIN | head -1 | awk -F: '{print $2}' | awk -F. '{print $1'}) GH_ORIGIN_USER=$(echo $GH_ORIGIN | awk -F/ '{print $1}') -GH_UPSTREAM=$(git remote -v | grep $UPSTREAM | head -1 | awk -F: '{print $2}' | awk -F. '{print $1'}) +GH_UPSTREAM=$(git remote -v 2>/dev/null | grep $UPSTREAM | head -1 | awk -F: '{print $2}' | awk -F. '{print $1'}) GH_UPSTREAM_USER=$(echo $GH_UPSTREAM | awk -F/ '{print $1}') From c37bf466bf5df17a9d8ddeeb6ef59eef242e7678 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 08:38:52 -0400 Subject: [PATCH 02/24] Fixed URL for new pull link. --- bin/git-hub-feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index e6794d6..9aeeb49 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -49,7 +49,7 @@ review(){ github /repos/$GH_UPSTREAM/pulls .tmp_pr_req >| .tmp_pr_res PULL_NUMBER=$(github_pr_number $BRANCH) - PULL_URL="http://github.com/$GH_UPSTREAM/pulls/$PULL_NUMBER" + PULL_URL="http://github.com/$GH_UPSTREAM/pull/$PULL_NUMBER" echo "Pull request is at $PULL_URL" From 3afab434dd77239596ba77a78a5f48613dbc6d8b Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 08:46:05 -0400 Subject: [PATCH 03/24] Added stash helpers. --- bin/git-hub-feature | 6 ++++-- bin/git-hub-helpers | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 9aeeb49..9bd1c67 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -19,7 +19,7 @@ publish(){ update(){ CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) - git stash + _stash BRANCH=${1:-$CUR_BRANCH} git checkout $MASTER @@ -28,7 +28,7 @@ update(){ git rebase $MASTER git checkout $CUR_BRANCH - git stash pop + _stash_pop } review(){ @@ -68,10 +68,12 @@ finish(){ if [ "x$(cat .tmp_merge | json merged)" == "xtrue" ] ; then echo "Pull request $PULL_NUMBER successfully merged." + _stash git checkout $MASTER git pull --rebase $UPSTREAM $MASTER git push $ORIGIN :$BRANCH git branch -D $BRANCH + _stash_pop else cat .tmp_merge | json message fi diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index f2c9949..8384e7e 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -44,6 +44,18 @@ EOF echo $NUMBER } + +TIMES_STASHED=0 +_stash(){ + git stash + TIMES_STASHED=$((TIMES_STASHED + 1)) +} + +_stash_pop(){ + TIMES_STASHED=$((TIMES_STASHED - 1)) + [ $TIMES_STASHED -le 0 ] && git stash pop +} + ORIGIN=$(git config --get github-flow.origin) UPSTREAM=$(git config --get github-flow.upstream) MASTER=$(git config --get github-flow.master) From 8bcb753c50806e0b17035f5ba845b22a7e967f51 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 08:48:13 -0400 Subject: [PATCH 04/24] Check for current clean wd, and do not overpop the stash. --- bin/git-hub-helpers | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 8384e7e..8989d9b 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -47,7 +47,7 @@ EOF TIMES_STASHED=0 _stash(){ - git stash + git stash | fgrep "working directory clean" && TIMES_STASHED=1 TIMES_STASHED=$((TIMES_STASHED + 1)) } From e50303a7a0643ebc2929cfcff397134447e40895 Mon Sep 17 00:00:00 2001 From: David Souther Date: Fri, 5 Apr 2013 17:51:53 -0400 Subject: [PATCH 05/24] Fixed bug in upstream URI --- bin/git-hub-clone | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-hub-clone b/bin/git-hub-clone index 5ce2d9b..610c068 100755 --- a/bin/git-hub-clone +++ b/bin/git-hub-clone @@ -13,7 +13,7 @@ clone(){ git clone git@github.com:$USER/$REPO.git cd $REPO - git remote add upstream git@github.com/$FULL_REPO.git + git remote add upstream git@github.com:$FULL_REPO.git git config --local --add github-flow.origin origin git config --local --add github-flow.upstream upstream From fa1c8fead2f073b12eeb1eb7cce33106b366ec41 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 08:24:21 -0400 Subject: [PATCH 06/24] Finished and debugged clone tools. --- bin/git-hub-clone | 26 +++++++++++++++++++++----- bin/git-hub-feature | 4 +++- bin/git-hub-helpers | 10 ++++++++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/bin/git-hub-clone b/bin/git-hub-clone index 610c068..78c67fe 100755 --- a/bin/git-hub-clone +++ b/bin/git-hub-clone @@ -4,12 +4,28 @@ set -x source git-hub-helpers +_fork(){ + local FULL_REPO=$1 + local REPO=$2 + local USER=$3 + + github /repos/$FULL_REPO/forks >| .tmp_forks + + if cat .tmp_forks | json -a owner.login | grep DavidSouther ; then + echo "Fork exists" >/dev/null + else + github /repos/$FULL_REPO/forks .tmp_fork + echo "Sleeping for a moment, to let github register the fork." && sleep 1 + fi + rm .tmp_fork* +} + clone(){ - FULL_REPO=$1 - REPO=$(echo $FULL_REPO | awk -F/ '{print $2}') - USER=$(github_user) + local FULL_REPO=$1 + local REPO=$(echo $FULL_REPO | awk -F/ '{print $2}') + local USER=$(github_user) - github /repos/$FULL_REPO/forks .tmp_fork + _fork $FULL_REPO $REPO $USER git clone git@github.com:$USER/$REPO.git cd $REPO @@ -19,7 +35,7 @@ clone(){ git config --local --add github-flow.upstream upstream git config --local --add github-flow.master master - echo "Repo cloned into $(pwd)" + echo "Project cloned into $(pwd)" } usage(){ diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 9bd1c67..9b87b2a 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -27,7 +27,9 @@ update(){ git checkout $BRANCH git rebase $MASTER - git checkout $CUR_BRANCH + if [ $CUR_BRANCH != $BRANCH ] ; then + git checkout $CUR_BRANCH + fi _stash_pop } diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 8989d9b..3537690 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -60,7 +60,13 @@ ORIGIN=$(git config --get github-flow.origin) UPSTREAM=$(git config --get github-flow.upstream) MASTER=$(git config --get github-flow.master) -GH_ORIGIN=$(git remote -v 2>/dev/null | grep $ORIGIN | head -1 | awk -F: '{print $2}' | awk -F. '{print $1'}) +find_repo(){ + while read line ; do + echo $line | sed -e 's!^.*git@github.com[/:]!!' -e 's!\.git.*$!!' + done +} + +GH_ORIGIN=$(git remote -v 2>/dev/null | grep $ORIGIN | head -1 | find_repo) GH_ORIGIN_USER=$(echo $GH_ORIGIN | awk -F/ '{print $1}') -GH_UPSTREAM=$(git remote -v 2>/dev/null | grep $UPSTREAM | head -1 | awk -F: '{print $2}' | awk -F. '{print $1'}) +GH_UPSTREAM=$(git remote -v 2>/dev/null | grep $UPSTREAM | head -1 | find_repo) GH_UPSTREAM_USER=$(echo $GH_UPSTREAM | awk -F/ '{print $1}') From a74521bc27b5508e073a8596c119a54f677dfd84 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 08:58:52 -0400 Subject: [PATCH 07/24] Force update on origin, in case there has been a rebase since last update. --- bin/git-hub-feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 9b87b2a..c159b5b 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -14,7 +14,7 @@ publish(){ git hub-feature update $BRANCH - git push $ORIGIN $BRANCH + git push --force $ORIGIN $BRANCH } update(){ From 8433efba3a4d4d320121eacc371c8e3cb705fd79 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 08:33:14 -0400 Subject: [PATCH 08/24] Stash changes before branching. --- bin/git-hub-feature | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index c159b5b..eae8cd8 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -3,9 +3,10 @@ source git-hub-helpers start(){ - BRANCH=$1 - git branch $BRANCH $MASTER - git checkout $BRANCH + local BRANCH=$1 + git stash + git checkout -b $BRANCH $MASTER + git stash pop } publish(){ From 1f30fb0cf2b9bc8465ea73d128dec301d8c54b11 Mon Sep 17 00:00:00 2001 From: David Souther Date: Fri, 5 Apr 2013 17:14:46 -0400 Subject: [PATCH 09/24] Added commentary throughout the code. --- bin/git-hub-feature | 11 +++++++++-- bin/git-hub-helpers | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index eae8cd8..49cb549 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -2,13 +2,15 @@ source git-hub-helpers +# Begin working on a new feature start(){ - local BRANCH=$1 git stash + # Just a semantic `git checkout -b $BRANCH $MASTER` git checkout -b $BRANCH $MASTER git stash pop } +# Make the feature branch available on the user's fork. publish(){ CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=${1:-$CUR_BRANCH} @@ -18,6 +20,8 @@ publish(){ git push --force $ORIGIN $BRANCH } +# Get all updates from upstream into the current branch. +# Can cause merge conflicts, when rebasing and when poping the stash. update(){ CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) _stash @@ -34,6 +38,7 @@ update(){ _stash_pop } +# Create a pull request to review the feature branch. review(){ CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=${1:-$CUR_BRANCH} @@ -59,6 +64,7 @@ review(){ rm .tmp_pr* } +# Close the feature branch, if it is mergeable. finish(){ CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=${1:-$CUR_BRANCH} @@ -73,10 +79,11 @@ finish(){ echo "Pull request $PULL_NUMBER successfully merged." _stash git checkout $MASTER + _stash_pop + git pull --rebase $UPSTREAM $MASTER git push $ORIGIN :$BRANCH git branch -D $BRANCH - _stash_pop else cat .tmp_merge | json message fi diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 3537690..aeaa2c0 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -1,6 +1,10 @@ #!/bin/bash API="https://api.github.com" +# Run a GET or POST request against the Github API +# First param is the API endpoint to hit +# Second param is an optional filename to use as the data. +# Handles auth. github(){ URL=$1 DATA=$2 @@ -14,6 +18,7 @@ github(){ $CURL -H "$AUTH" "$API$URL" 2>/dev/null } +# Run a PUT request against Github, with no data in the body. github_put(){ URL=$1 @@ -36,6 +41,7 @@ github_login(){ curl -u $(github_user) -d '{"scopes":["repo"],"note":"git-hub-flow"}' $API/authorizations | json token >| ~/.github_token } +# Find the pr number from the current branch. github_pr_number(){ BRANCH=$1 read NUMBER LABEL < Date: Fri, 5 Apr 2013 17:51:19 -0400 Subject: [PATCH 10/24] Improved helper handling unusual situations. --- bin/git-hub-feature | 6 ++++++ bin/git-hub-helpers | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 49cb549..b9261c0 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -1,5 +1,7 @@ #!/bin/bash +set -x + source git-hub-helpers # Begin working on a new feature @@ -35,6 +37,7 @@ update(){ if [ $CUR_BRANCH != $BRANCH ] ; then git checkout $CUR_BRANCH fi + _stash_pop } @@ -56,9 +59,12 @@ review(){ github /repos/$GH_UPSTREAM/pulls .tmp_pr_req >| .tmp_pr_res + cat .tmp_pr_res + PULL_NUMBER=$(github_pr_number $BRANCH) PULL_URL="http://github.com/$GH_UPSTREAM/pull/$PULL_NUMBER" + echo "Pull request is at $PULL_URL" rm .tmp_pr* diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index aeaa2c0..32e9232 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -25,7 +25,7 @@ github_put(){ AUTH="Authorization: token $(cat ~/.github_token)" CURL="curl" - $CURL -X PUT -d "$2" -H "$AUTH" "$API$URL" 2>/dev/null + $CURL -X PUT -d "$2" -H "$AUTH" "$API$URL" } github_user(){ @@ -50,7 +50,6 @@ EOF echo $NUMBER } - TIMES_STASHED=0 _stash(){ git stash | fgrep "working directory clean" && TIMES_STASHED=1 From 7812b1aa9fdd30f4de8e1bc4e40f92b9ac9eda61 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 09:25:19 -0400 Subject: [PATCH 11/24] Suppress output, add logging and comments. --- bin/git-hub-clone | 13 +++++++------ bin/git-hub-feature | 44 +++++++++++++++++++++++--------------------- bin/git-hub-helpers | 15 ++++++++++++--- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/bin/git-hub-clone b/bin/git-hub-clone index 78c67fe..31284b1 100755 --- a/bin/git-hub-clone +++ b/bin/git-hub-clone @@ -1,9 +1,8 @@ #!/bin/bash -set -x - source git-hub-helpers +# Safely _fork(){ local FULL_REPO=$1 local REPO=$2 @@ -11,15 +10,17 @@ _fork(){ github /repos/$FULL_REPO/forks >| .tmp_forks - if cat .tmp_forks | json -a owner.login | grep DavidSouther ; then - echo "Fork exists" >/dev/null + if [ ! $(cat .tmp_forks | json -a owner.login | grep DavidSouther) ; then else github /repos/$FULL_REPO/forks .tmp_fork - echo "Sleeping for a moment, to let github register the fork." && sleep 1 + # Sleep for a moment, to let github register the fork. + sleep 1 fi + echo "Fork at https://github.com/$USER/$REPO" rm .tmp_fork* } +# Clone the repo and set a variety of --local config options for github-flow. clone(){ local FULL_REPO=$1 local REPO=$(echo $FULL_REPO | awk -F/ '{print $2}') @@ -27,7 +28,7 @@ clone(){ _fork $FULL_REPO $REPO $USER - git clone git@github.com:$USER/$REPO.git + git clone git@github.com:$USER/$REPO.git 2>&1 >/dev/null cd $REPO git remote add upstream git@github.com:$FULL_REPO.git diff --git a/bin/git-hub-feature b/bin/git-hub-feature index b9261c0..8367e76 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -1,7 +1,5 @@ #!/bin/bash -set -x - source git-hub-helpers # Begin working on a new feature @@ -14,24 +12,25 @@ start(){ # Make the feature branch available on the user's fork. publish(){ - CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=${1:-$CUR_BRANCH} git hub-feature update $BRANCH - git push --force $ORIGIN $BRANCH + echo "Forcing updates to https://github.com/$GH_ORIGIN/tree/$BRANCH" + git push --force $ORIGIN $BRANCH >/dev/null } # Get all updates from upstream into the current branch. # Can cause merge conflicts, when rebasing and when poping the stash. update(){ - CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) _stash BRANCH=${1:-$CUR_BRANCH} git checkout $MASTER - git pull --rebase $UPSTREAM $MASTER - git checkout $BRANCH + echo "Rebasing upstream updates into master..." + git pull --rebase $UPSTREAM $MASTER >/dev/null + echo "Rebasing master into $BRANCH..." + git checkout $BRANCH >/dev/null git rebase $MASTER if [ $CUR_BRANCH != $BRANCH ] ; then @@ -43,27 +42,31 @@ update(){ # Create a pull request to review the feature branch. review(){ - CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=${1:-$CUR_BRANCH} git hub-feature publish $BRANCH - TITLE='"title":"'"$BRANCH: $(git log -1 --pretty=format:'%s')"'"' - BODY='"body":"'"$(git log --pretty=format:'%h - %s <%an>' $MASTER.. | sed ':a;N;s/\n/\\n/g;b a')"'"' - BASE='"base":"'"$GH_UPSTREAM_USER:$MASTER"'"' - BASE_SHA='"base_sha":"'"$(git rev-parse $MASTER)"'"' - HEAD='"head":"'"$GH_ORIGIN_USER:$BRANCH"'"' - HEAD_SHA='"head_sha":"'"$(git rev-parse $BRANCH)"'"' + PULL_NUMBER=$(github_pr_number $BRANCH) - echo "{$TITLE,$BODY,$BASE,$HEAD,$BASE_SHA,$HEAD_SHA}" >| .tmp_pr_req + # Check if the PR already exists + if [ -z "$PULL_NUMBER" ] ; then + # Prepare the JSON for the pull request. + # The title is the most recent commit message at time of starting the review. + TITLE='"title":"'"$BRANCH: $(git log -1 --pretty=format:'%s')"'"' + BODY='"body":"'"$(git log --pretty=format:'%h - %s <%an>' $MASTER.. | sed ':a;N;s/\n/\\n/g;b a')"'"' + BASE='"base":"'"$GH_UPSTREAM_USER:$MASTER"'"' + BASE_SHA='"base_sha":"'"$(git rev-parse $MASTER)"'"' + HEAD='"head":"'"$GH_ORIGIN_USER:$BRANCH"'"' + HEAD_SHA='"head_sha":"'"$(git rev-parse $BRANCH)"'"' - github /repos/$GH_UPSTREAM/pulls .tmp_pr_req >| .tmp_pr_res + echo "{$TITLE,$BODY,$BASE,$HEAD,$BASE_SHA,$HEAD_SHA}" >| .tmp_pr_req - cat .tmp_pr_res + github /repos/$GH_UPSTREAM/pulls .tmp_pr_req >| .tmp_pr_res - PULL_NUMBER=$(github_pr_number $BRANCH) - PULL_URL="http://github.com/$GH_UPSTREAM/pull/$PULL_NUMBER" + PULL_NUMBER=$(github_pr_number $BRANCH) + fi + PULL_URL="http://github.com/$GH_UPSTREAM/pull/$PULL_NUMBER" echo "Pull request is at $PULL_URL" @@ -72,7 +75,6 @@ review(){ # Close the feature branch, if it is mergeable. finish(){ - CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD) BRANCH=${1:-$CUR_BRANCH} git hub-feature publish $BRANCH @@ -84,7 +86,7 @@ finish(){ if [ "x$(cat .tmp_merge | json merged)" == "xtrue" ] ; then echo "Pull request $PULL_NUMBER successfully merged." _stash - git checkout $MASTER + git checkout $MASTER >/dev/null _stash_pop git pull --rebase $UPSTREAM $MASTER diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 32e9232..6c704da 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -10,7 +10,7 @@ github(){ DATA=$2 AUTH="Authorization: token $(cat ~/.github_token)" - CURL="curl -vi" + CURL="curl" if [ ! -z "$DATA" ] ; then CURL="$CURL -d @$DATA" fi @@ -28,6 +28,7 @@ github_put(){ $CURL -X PUT -d "$2" -H "$AUTH" "$API$URL" } +# Try and get the username. Read and set it if it is unset. github_user(){ USERNAME=$(git config --get github-flow.username) if [ $? -eq 1 ] ; then @@ -37,6 +38,7 @@ github_user(){ echo $USERNAME } +# Get an access token. github_login(){ curl -u $(github_user) -d '{"scopes":["repo"],"note":"git-hub-flow"}' $API/authorizations | json token >| ~/.github_token } @@ -50,28 +52,35 @@ EOF echo $NUMBER } +# 1-Semaphore for stash stack. TIMES_STASHED=0 _stash(){ - git stash | fgrep "working directory clean" && TIMES_STASHED=1 + git stash | fgrep "working directory clean" >/dev/null && TIMES_STASHED=1 TIMES_STASHED=$((TIMES_STASHED + 1)) } _stash_pop(){ TIMES_STASHED=$((TIMES_STASHED - 1)) - [ $TIMES_STASHED -le 0 ] && git stash pop + [ $TIMES_STASHED -le 0 ] && git stash pop 2>&1 >/dev/null } +# Get some config info. ORIGIN=$(git config --get github-flow.origin) UPSTREAM=$(git config --get github-flow.upstream) MASTER=$(git config --get github-flow.master) +# Helper to get an "OWNER/REPO" string. find_repo(){ while read line ; do echo $line | sed -e 's!^.*git@github.com[/:]!!' -e 's!\.git.*$!!' done } +# Get the info about the repos. GH_ORIGIN=$(git remote -v 2>/dev/null | grep $ORIGIN | head -1 | find_repo) GH_ORIGIN_USER=$(echo $GH_ORIGIN | awk -F/ '{print $1}') GH_UPSTREAM=$(git remote -v 2>/dev/null | grep $UPSTREAM | head -1 | find_repo) GH_UPSTREAM_USER=$(echo $GH_UPSTREAM | awk -F/ '{print $1}') + +# Always starting on some branch. +CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) From cdaa303ba7c79857615aa9f5b074d1da029e52d5 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 09:28:35 -0400 Subject: [PATCH 12/24] Make pull and push less chatty. --- bin/git-hub-feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 8367e76..71ef6c3 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -17,7 +17,7 @@ publish(){ git hub-feature update $BRANCH echo "Forcing updates to https://github.com/$GH_ORIGIN/tree/$BRANCH" - git push --force $ORIGIN $BRANCH >/dev/null + git push --force $ORIGIN $BRANCH 2>&1 >/dev/null } # Get all updates from upstream into the current branch. @@ -28,7 +28,7 @@ update(){ git checkout $MASTER echo "Rebasing upstream updates into master..." - git pull --rebase $UPSTREAM $MASTER >/dev/null + git pull --rebase $UPSTREAM $MASTER 2>&1 >/dev/null echo "Rebasing master into $BRANCH..." git checkout $BRANCH >/dev/null git rebase $MASTER From 8e08d57c8fc5e1f8c896424b5be1c3106d3c1503 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 09:33:42 -0400 Subject: [PATCH 13/24] Added $Q="--quiet" --- bin/git-hub-feature | 18 +++++++++--------- bin/git-hub-helpers | 2 ++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 71ef6c3..0e89116 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -17,7 +17,7 @@ publish(){ git hub-feature update $BRANCH echo "Forcing updates to https://github.com/$GH_ORIGIN/tree/$BRANCH" - git push --force $ORIGIN $BRANCH 2>&1 >/dev/null + git push $Q --force $ORIGIN $BRANCH } # Get all updates from upstream into the current branch. @@ -28,13 +28,13 @@ update(){ git checkout $MASTER echo "Rebasing upstream updates into master..." - git pull --rebase $UPSTREAM $MASTER 2>&1 >/dev/null + git pull $Q --rebase $UPSTREAM $MASTER echo "Rebasing master into $BRANCH..." - git checkout $BRANCH >/dev/null - git rebase $MASTER + git checkout $Q $BRANCH + git rebase $Q $MASTER if [ $CUR_BRANCH != $BRANCH ] ; then - git checkout $CUR_BRANCH + git checkout $Q $CUR_BRANCH fi _stash_pop @@ -86,12 +86,12 @@ finish(){ if [ "x$(cat .tmp_merge | json merged)" == "xtrue" ] ; then echo "Pull request $PULL_NUMBER successfully merged." _stash - git checkout $MASTER >/dev/null + git checkout $Q $MASTER >/dev/null _stash_pop - git pull --rebase $UPSTREAM $MASTER - git push $ORIGIN :$BRANCH - git branch -D $BRANCH + git pull $Q --rebase $UPSTREAM $MASTER + git push $Q $ORIGIN :$BRANCH + git branch $Q -D $BRANCH else cat .tmp_merge | json message fi diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 6c704da..1e9ad45 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -1,5 +1,7 @@ #!/bin/bash +Q="--quiet" + API="https://api.github.com" # Run a GET or POST request against the Github API # First param is the API endpoint to hit From 4e2eab75b4d5f8ae192d9065eeae2040986192fd Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 09:48:12 -0400 Subject: [PATCH 14/24] Correctly check for remotes when not in git repo. --- bin/git-hub-clone | 7 +++---- bin/git-hub-feature | 1 + bin/git-hub-helpers | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/git-hub-clone b/bin/git-hub-clone index 31284b1..d5f2963 100755 --- a/bin/git-hub-clone +++ b/bin/git-hub-clone @@ -10,8 +10,7 @@ _fork(){ github /repos/$FULL_REPO/forks >| .tmp_forks - if [ ! $(cat .tmp_forks | json -a owner.login | grep DavidSouther) ; then - else + if [ ! $(cat .tmp_forks | json -a owner.login | fgrep "$(github_user)") ] ; then github /repos/$FULL_REPO/forks .tmp_fork # Sleep for a moment, to let github register the fork. sleep 1 @@ -28,7 +27,7 @@ clone(){ _fork $FULL_REPO $REPO $USER - git clone git@github.com:$USER/$REPO.git 2>&1 >/dev/null + git clone $Q git@github.com:$USER/$REPO.git 2>&1 >/dev/null cd $REPO git remote add upstream git@github.com:$FULL_REPO.git @@ -49,7 +48,7 @@ help(){ echo " : The organization and repository name to clone from." } -if [ "x$1" == "-h"] ; then +if [ "x$1" == "-h" ] ; then help else clone $@ diff --git a/bin/git-hub-feature b/bin/git-hub-feature index 0e89116..0583e62 100755 --- a/bin/git-hub-feature +++ b/bin/git-hub-feature @@ -4,6 +4,7 @@ source git-hub-helpers # Begin working on a new feature start(){ + BRANCH=${1:?"Please specify a branch name."} git stash # Just a semantic `git checkout -b $BRANCH $MASTER` git checkout -b $BRANCH $MASTER diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 1e9ad45..3f7b2e1 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -79,9 +79,9 @@ find_repo(){ } # Get the info about the repos. -GH_ORIGIN=$(git remote -v 2>/dev/null | grep $ORIGIN | head -1 | find_repo) +GH_ORIGIN=$(git remote -v 2>/dev/null | fgrep "$ORIGIN" | head -1 | find_repo) GH_ORIGIN_USER=$(echo $GH_ORIGIN | awk -F/ '{print $1}') -GH_UPSTREAM=$(git remote -v 2>/dev/null | grep $UPSTREAM | head -1 | find_repo) +GH_UPSTREAM=$(git remote -v 2>/dev/null | fgrep "$UPSTREAM" | head -1 | find_repo) GH_UPSTREAM_USER=$(echo $GH_UPSTREAM | awk -F/ '{print $1}') # Always starting on some branch. From 60f64a60043ade9d1ff1cb8fa5a501a4cbb0a9e6 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 09:58:54 -0400 Subject: [PATCH 15/24] Added install script. --- install.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100755 install.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..f4ee811 --- /dev/null +++ b/install.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +INSTALL_DIR=${1:-"~/"} + +echo "Installing into $INSTALL_DIR/.ghf" + +# Create the install directory if it doesn't exist. +[ -d $INSTALL_DIR ] || mkdir -p $INSTALL_DIR + +cd $INSTALL_DIR + +# If it's already installed, update from origin. +if [ -d .ghf ] ; then + cd .ghf + echo "github-flow already installed, updating from origin." + git pull origin master +else + git clone git://github.com/github-flow/github-flow.git .ghf +fi + +# Only modify the path if the newly installed ghf doesn't exist. +if [ ! $(echo $PATH | fgrep "$INSTALL_DIR/.ghf/bin") ] ; then + echo "Adding .ghf to PATH." + + echo "PATH was '$PATH'" + + echo "PATH=$INSTALL_DIR/.ghf/bin:$PATH" >| ~/.bashrc + + source ~/.bashrc + + echo "PATH is '$PATH'" +fi From 241f59aca48ba4e7888eba5c545a0e2c93ccca95 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 8 Apr 2013 11:56:00 -0400 Subject: [PATCH 16/24] README includes install and usage instructions. --- README.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 436783c..5ac7d9d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,41 @@ github-flow =========== -git flow esque helpers, designed with github in mind. \ No newline at end of file +git flow esque helpers, designed with github in mind. + +Installation +------------ + +To install, run the installation script: + +``` +curl https://raw.github.com/github-flow/github-flow/v1.0/install.sh | $(which bash) +``` + +To update, run the install script again. + +To install or update outside the default directory, pass a folder as the first parameter to bash. + +Usage +----- + +Usage is very similar to git-flow + +`git hub-clone ` Fork and clone a repository to use with git hub-flow. + +`git hub-feature start ` Begin working on a new feature. + +`git hub-feature update [feature]` Bring master branch up to date, and rebase `feature` off master. + +`git hub-feature publish [feature]` Push changes on `feature` to `origin` repository. + +`git hub-feature review [feature]` Open pull request on `upstream`/`feature` against `origin`/`feature`. + +`git hub-feature finish [feature]` Merge pull request for this feature, and prune branches. + +All commands with `[feature]` either take a parameter to specify the branch, or work off the current (non-`master`) branch. + +TODO, Bugs +---------- + +See the [bug tracker](https://github.com/github-flow/github-flow/issues). \ No newline at end of file From 49f18f91bab18ca8e43e95ad368fe79c56801799 Mon Sep 17 00:00:00 2001 From: David Souther Date: Wed, 10 Apr 2013 15:53:21 -0400 Subject: [PATCH 17/24] Fixed default install dir and github url. --- install.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index f4ee811..03802af 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,6 @@ #!/bin/bash -INSTALL_DIR=${1:-"~/"} +INSTALL_DIR=${1:-$HOME} echo "Installing into $INSTALL_DIR/.ghf" @@ -15,7 +15,7 @@ if [ -d .ghf ] ; then echo "github-flow already installed, updating from origin." git pull origin master else - git clone git://github.com/github-flow/github-flow.git .ghf + git clone https://github.com/github-flow/github-flow.git .ghf fi # Only modify the path if the newly installed ghf doesn't exist. @@ -29,4 +29,6 @@ if [ ! $(echo $PATH | fgrep "$INSTALL_DIR/.ghf/bin") ] ; then source ~/.bashrc echo "PATH is '$PATH'" + + echo "source ~/.bashrc to update path in the current shell." fi From 333e16bc30f03dbb3b4e595723c3a49562dfcb3f Mon Sep 17 00:00:00 2001 From: David Souther Date: Wed, 10 Apr 2013 15:55:59 -0400 Subject: [PATCH 18/24] Append, not clobber, bashrc. --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 03802af..b9f052c 100755 --- a/install.sh +++ b/install.sh @@ -24,7 +24,7 @@ if [ ! $(echo $PATH | fgrep "$INSTALL_DIR/.ghf/bin") ] ; then echo "PATH was '$PATH'" - echo "PATH=$INSTALL_DIR/.ghf/bin:$PATH" >| ~/.bashrc + echo "PATH=$INSTALL_DIR/.ghf/bin:$PATH" >> ~/.bashrc source ~/.bashrc From 672f14c2ebe95e9172f0223ea05b1778e766fb75 Mon Sep 17 00:00:00 2001 From: David Souther Date: Wed, 10 Apr 2013 15:58:18 -0400 Subject: [PATCH 19/24] Update path using path reference. --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index b9f052c..8d063c5 100755 --- a/install.sh +++ b/install.sh @@ -24,7 +24,7 @@ if [ ! $(echo $PATH | fgrep "$INSTALL_DIR/.ghf/bin") ] ; then echo "PATH was '$PATH'" - echo "PATH=$INSTALL_DIR/.ghf/bin:$PATH" >> ~/.bashrc + echo "PATH=$INSTALL_DIR/.ghf/bin:\$PATH" >> ~/.bashrc source ~/.bashrc From 604d8205155088b6996d5d20bcf99de88bac86ce Mon Sep 17 00:00:00 2001 From: David Souther Date: Wed, 10 Apr 2013 16:07:15 -0400 Subject: [PATCH 20/24] Added calls to github_login at appropriate points in workflow. --- bin/git-hub-helpers | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/git-hub-helpers b/bin/git-hub-helpers index 3f7b2e1..fc0c073 100755 --- a/bin/git-hub-helpers +++ b/bin/git-hub-helpers @@ -11,6 +11,8 @@ github(){ URL=$1 DATA=$2 + github_login + AUTH="Authorization: token $(cat ~/.github_token)" CURL="curl" if [ ! -z "$DATA" ] ; then @@ -42,7 +44,7 @@ github_user(){ # Get an access token. github_login(){ - curl -u $(github_user) -d '{"scopes":["repo"],"note":"git-hub-flow"}' $API/authorizations | json token >| ~/.github_token + [ -f ~/.github_token ] || curl -u $(github_user) -d '{"scopes":["repo"],"note":"git-hub-flow"}' $API/authorizations | json token >| ~/.github_token } # Find the pr number from the current branch. From 3912cb0d4d7c7e72bd930d5d25c3cbb24d7d3b41 Mon Sep 17 00:00:00 2001 From: David Souther Date: Thu, 9 May 2013 10:45:01 -0400 Subject: [PATCH 21/24] Check for and install jsontool from npm. --- install.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/install.sh b/install.sh index 8d063c5..943477a 100755 --- a/install.sh +++ b/install.sh @@ -2,6 +2,19 @@ INSTALL_DIR=${1:-$HOME} +echo 'Checking for $(which json)' +which json > /dev/null 2>&1 +if [ $? -ne 0 ] ; then + echo '$(which json) not found, installing from npm. May require global permissions.' + npm install -g jsontool + if [ $? -ne 0 ] ; then + echo "Couldn't install jsontool from npm. Please run 'sudo npm install -g json', then rerun this installer." + exit 1 + fi +else + echo "jsontool found." +fi + echo "Installing into $INSTALL_DIR/.ghf" # Create the install directory if it doesn't exist. From a737524200e2dda24d77d55ee7151b946a4a4af3 Mon Sep 17 00:00:00 2001 From: David Souther Date: Thu, 9 May 2013 10:49:48 -0400 Subject: [PATCH 22/24] Added config documentation. --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ac7d9d..cbf994a 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,13 @@ Usage is very similar to git-flow All commands with `[feature]` either take a parameter to specify the branch, or work off the current (non-`master`) branch. + +Configuration +------------- + +Repositories are configured using three properties under `github-flow`: `origin`, `upstream`, and `master`. Origin and upstream are the logical remotes. `upstream` is the canonical upstream project, usually owned by an organization. `origin` is the users' fork, generally owned by the user's account. 'master' is the logical primary development branch, usually either "master" or "develop". By default, the values are the same as their keys. To use the traditional git-flow model of branching and working off develop, in a local repository run `git config --local --set github-flow.master develop` + TODO, Bugs ---------- -See the [bug tracker](https://github.com/github-flow/github-flow/issues). \ No newline at end of file +See the [bug tracker](https://github.com/github-flow/github-flow/issues). From f3236bd2e4a0cba71893b55a3242bed3c534cfd0 Mon Sep 17 00:00:00 2001 From: David Souther Date: Thu, 9 May 2013 10:55:31 -0400 Subject: [PATCH 23/24] Install using v1.1 (includes jsontool dependency). --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cbf994a..3e6ca9d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Installation To install, run the installation script: ``` -curl https://raw.github.com/github-flow/github-flow/v1.0/install.sh | $(which bash) +curl https://raw.github.com/github-flow/github-flow/v1.1/install.sh | $(which bash) ``` To update, run the install script again. From 65c93240be2a860345775ce45ee661e947047758 Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 24 Jun 2013 12:27:08 -0300 Subject: [PATCH 24/24] Fixed README.md config instructions. --add, not --set. But --unset. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3e6ca9d..4bd07a9 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ All commands with `[feature]` either take a parameter to specify the branch, or Configuration ------------- -Repositories are configured using three properties under `github-flow`: `origin`, `upstream`, and `master`. Origin and upstream are the logical remotes. `upstream` is the canonical upstream project, usually owned by an organization. `origin` is the users' fork, generally owned by the user's account. 'master' is the logical primary development branch, usually either "master" or "develop". By default, the values are the same as their keys. To use the traditional git-flow model of branching and working off develop, in a local repository run `git config --local --set github-flow.master develop` +Repositories are configured using three properties under `github-flow`: `origin`, `upstream`, and `master`. Origin and upstream are the logical remotes. `upstream` is the canonical upstream project, usually owned by an organization. `origin` is the users' fork, generally owned by the user's account. 'master' is the logical primary development branch, usually either "master" or "develop". By default, the values are the same as their keys. To use the traditional git-flow model of branching and working off develop, in a local repository run `git config --local --add github-flow.master develop` TODO, Bugs ----------