Created
March 26, 2012 22:49
-
-
Save adumont/2210381 to your computer and use it in GitHub Desktop.
gsync
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/bash | |
# Copyright (C) 2011 SuperTeam. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# Author: Adlx (@adumont) | |
usage() { | |
echo "Usage: $( basename $0 ) cm | pull | push | reset" | |
exit 1 | |
} | |
gettop () | |
{ | |
local TOPFILE=build/core/envsetup.mk; | |
if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ]; then | |
echo $TOP; | |
else | |
if [ -f $TOPFILE ]; then | |
PWD= /bin/pwd; | |
else | |
local HERE=$PWD; | |
T=; | |
while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do | |
cd .. > /dev/null; | |
T=`PWD= /bin/pwd`; | |
done; | |
cd $HERE > /dev/null; | |
if [ -f "$T/$TOPFILE" ]; then | |
echo $T; | |
fi; | |
fi; | |
fi | |
} | |
if [ $# -ne 1 ] | |
then | |
usage | |
fi | |
ACTION=$1 | |
REPO_ROOT=$( gettop ) | |
PROJ_PATH=$( pwd | sed -e "s#^$REPO_ROOT/##" ) | |
# Guess branch | |
BRANCH=$( grep "default revision=" $REPO_ROOT/.repo/manifest.xml | cut -d\" -f2 | | |
head -1 | awk -F/ '{ print $NF }' ) | |
SOURCE=$( git config -l | grep \.projectname | tr "=" "." | cut -d. -f 2 ) | |
set -- $( git config remote.$SOURCE.projectname | tr "/" " " ) | |
USR=$1 | |
PROJ=$2 | |
echo -e "GSYNC: $PWD $SOURCE:$USR/$PROJ ($(git branch | grep "^*" | cut -c3-))" | |
[ "_$PROJ" = "_android_packages_apps_Parts" ] && PROJ=android_packages_apps_CMParts | |
function switch_branch { | |
# Place ourself in branch "gingerbread", create if it doesn't exist | |
if ! git branch | grep -q "* ${1}" | |
then | |
if git branch | grep -q "${1}" | |
then | |
git checkout ${1} | |
else | |
if [ $1 = $BRANCH ] | |
then # gb / ics | |
git checkout --track ${SOURCE}/${1} | |
git config branch.${BRANCH}.remote ${SOURCE} | |
git config branch.${BRANCH}.merge refs/heads/${BRANCH} | |
git config branch.${BRANCH}.rebase true | |
else # cm | |
if ! git config -l | grep -q remote.cm.url | |
then | |
git remote add -t ${BRANCH} cm [email protected]:CyanogenMod/${PROJ}.git | |
fi | |
if ! git branch | grep -q -w "cm" | |
then | |
git fetch cm | |
git branch --track cm cm/${BRANCH} | |
git config branch.cm.rebase true | |
fi | |
git checkout cm | |
fi | |
fi | |
fi | |
} | |
case $ACTION in | |
reset) | |
git co ${SOURCE}/${BRANCH}; git branch -d ${BRANCH} | |
;; | |
pull) | |
switch_branch $BRANCH | |
git pull --rebase | |
;; | |
push) | |
switch_branch $BRANCH | |
git push [email protected]:${USR}/${PROJ}.git $BRANCH | |
;; | |
cm) | |
switch_branch cm | |
git pull | |
;; | |
*) | |
usage | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment