Stefan Saasen avatar Stefan Saasen committed a7a134c

Simplify git-outgoing. Use git plumbing where appropriate instead of parsing the output of porcelain

Comments (0)

Files changed (1)

 # Copied from: https://raw.github.com/juretta/bin/master/git-outgoing
 
 # Return the name of the current branch
-function current_branch {
-    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; 
+CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
+
+function tracking_branch {
+  branch_name=$1
+  git for-each-ref --format='%(refname:short) %(upstream:short)' refs/heads | grep -e "${branch_name}\$" | awk '{print $2}'
 }
 
-CURRENT_BRANCH=`current_branch`
 
-# What branch are we tracking?
-TRACKING=$(git config branch.${CURRENT_BRANCH}.merge | ruby -ne 'print $_.gsub(/refs\/heads\//, "").strip')
+# Are we tracking a remote branch?
+TRACKING=$(tracking_branch ${CURRENT_BRANCH})
 
-# What's the name of the remote?
-REMOTE=$(git config branch.${CURRENT_BRANCH}.remote)
+if [ -z "${TRACKING//[[:space:]]/}" ]; then
+    echo "The current branch '${CURRENT_BRANCH}' does not track an upstream branch"
+    exit -1
+fi
 
 # Log options
 OPTS="--pretty=oneline --abbrev-commit --decorate --color"
 
+# What's the name of the remote?
+REMOTE=$(git config branch.${CURRENT_BRANCH}.remote)
+
 # URL of the remote we are tracking
-REMOTE_URL=$(git remote -v | ruby -e "puts STDIN.each_line.grep(/^${REMOTE}/).first.split[1]")
+REMOTE_URL=$(git config --get remote.${REMOTE}.url)
 
 # E.g. for 'master':
 #  git log master..origin/master
-CHANGES=$(env PAGER="" git log --exit-code $OPTS ${REMOTE}/${TRACKING}..${CURRENT_BRANCH})
+CHANGES=$(env PAGER="" git log --exit-code $OPTS ${TRACKING}..${CURRENT_BRANCH})
 if [ $? -eq 0 ]; then
     echo "No changes need to be pushed to ${REMOTE}"
 else
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.