diff scripts/repo_update @ 0:21573f5b2e84

initial add -- all data from Sean's machine
author Me@portablequad
date Mon, 28 Nov 2011 15:37:08 -0800
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scripts/repo_update	Mon Nov 28 15:37:08 2011 -0800
     1.3 @@ -0,0 +1,204 @@
     1.4 +#! /bin/bash
     1.5 +
     1.6 +usage=$(cat << EOF
     1.7 +
     1.8 +Usage: $0 [-h] [-a] [-r] [-k] [-w] [-b Commit/Tag/Branch]
     1.9 +
    1.10 +    -a Apply all the changes to all repositories in the current tree.
    1.11 +    
    1.12 +    -b Update to a commit/tag/branch. This is useful if you work on a single
    1.13 +       branch/tag in all repositories. If the requested repository is not 
    1.14 +       found it falls back to the recent version. The -k and -i options are
    1.15 +       useful here.
    1.16 +       
    1.17 +    -h Prints this usage information
    1.18 +    
    1.19 +    -i Update to a commit/tag/branch with the -b flag in interactive mode.
    1.20 +       This asks if you want to fall back to the recent version if the
    1.21 +       requested commit/tag/branch is not found.
    1.22 +       This is only useful with the -b flag.
    1.23 +       
    1.24 +    -k This does not pull from the remote repository, just updates the
    1.25 +       repositories.
    1.26 +    
    1.27 +    -r Revert to the last version. This is not deleting the pull commit,
    1.28 +       just updating back to your last state.
    1.29 +    
    1.30 +    -w Do the pull over https instead of ssh
    1.31 +EOF
    1.32 +)
    1.33 +
    1.34 +SAVED_STATE_FILE=~/.hg.saved
    1.35 +
    1.36 +#set selected repository to current folder
    1.37 +SELECTED_REPOS=$(pwd)
    1.38 +
    1.39 +source repo_update_helper.sh
    1.40 +
    1.41 +#################################
    1.42 +#Parse arguments
    1.43 +#################################
    1.44 +while getopts "arb:hikw" flag
    1.45 +do
    1.46 +    case $flag in
    1.47 +        a)
    1.48 +            #update all repositories in the directory tree
    1.49 +            SELECTED_REPOS=$(find_repos)
    1.50 +            ;;
    1.51 +        r)
    1.52 +            #revert to commits after the last pull from the server
    1.53 +            REVERT="1"
    1.54 +            ;;
    1.55 +        b)
    1.56 +            #update to a tag/branch/commit
    1.57 +            VERSION=$OPTARG
    1.58 +            ;;
    1.59 +        k)
    1.60 +            NO_UPDATE="1"
    1.61 +            ;;
    1.62 +        i)
    1.63 +            INTERACTIVE_UPDATE="1"
    1.64 +            ;;
    1.65 +        w)
    1.66 +            REMOTE="http"
    1.67 +            ;;
    1.68 +        h)
    1.69 +            echo "$usage"
    1.70 +            exit 0
    1.71 +            ;;
    1.72 +        *)
    1.73 +            echo "$usage"
    1.74 +            exit 0
    1.75 +            ;;
    1.76 +    esac
    1.77 +done
    1.78 +
    1.79 +if [ ! -e $SAVED_STATE_FILE ]
    1.80 +then
    1.81 +    touch $SAVED_STATE_FILE
    1.82 +fi
    1.83 +SAVED_STATE=$(cat $SAVED_STATE_FILE | sed "s/^$//g")
    1.84 +BASE_FOLDER=$(pwd)
    1.85 +
    1.86 +#################################
    1.87 +# Revert changes and exit.
    1.88 +#################################
    1.89 +if [ -n "$REVERT" ]
    1.90 +then
    1.91 +    print_info "Reverting changes"
    1.92 +    for REPO in $SELECTED_REPOS
    1.93 +    do
    1.94 +        cd $REPO
    1.95 +        pwd
    1.96 +        
    1.97 +        REVERT_COMMIT=$(echo "$SAVED_STATE" | grep "$PWD " | grep -o -E "[a-z0-9]{12}")
    1.98 +        if [ -z "$REVERT_COMMIT" ]
    1.99 +        then
   1.100 +            echo "No saved commit."
   1.101 +            cd $BASE_FOLDER
   1.102 +            continue
   1.103 +        fi
   1.104 +        hg update $REVERT_COMMIT
   1.105 +
   1.106 +        cd $BASE_FOLDER
   1.107 +    done
   1.108 +    exit 0
   1.109 +fi
   1.110 +
   1.111 +#################################
   1.112 +# Save the current current state of the repository
   1.113 +#################################
   1.114 +print_info "Saving current commit."
   1.115 +for REPO in $SELECTED_REPOS
   1.116 +do
   1.117 +    cd $REPO
   1.118 +    pwd
   1.119 +    if [ 0 -ne $? ]
   1.120 +    then 
   1.121 +        echo "Cannot change to repository $REPO"
   1.122 +        exit 1
   1.123 +    fi
   1.124 +    
   1.125 +    STATE=$(hg summary)
   1.126 +    if [ 0 -ne $? ]
   1.127 +    then 
   1.128 +        echo "Not a repository: $REPO"
   1.129 +        exit 1
   1.130 +    fi
   1.131 +    CURRENT_COMMIT=$(echo "$STATE" | grep -o -E "[0-9]:[a-z0-9]{12}" | grep -o -E "[a-z0-9]{12}")
   1.132 +    
   1.133 +    #remove current repository from saved state
   1.134 +    SAVED_STATE=$(echo "$SAVED_STATE" | grep -v "$PWD ")
   1.135 +    SAVED_STATE=$(echo -e "$SAVED_STATE" && echo "$PWD $CURRENT_COMMIT")
   1.136 +    
   1.137 +    cd $BASE_FOLDER 
   1.138 +done
   1.139 +echo "$SAVED_STATE" > $SAVED_STATE_FILE
   1.140 +
   1.141 +#################################
   1.142 +# Pull changes from remote server.
   1.143 +#################################
   1.144 +if [ -z "$NO_UPDATE" ]
   1.145 +then
   1.146 +    print_info "Pull changes from remote."
   1.147 +    for REPO in $SELECTED_REPOS
   1.148 +    do
   1.149 +        cd $REPO
   1.150 +        pwd
   1.151 +        hg pull $REMOTE
   1.152 +        if [ 0 -ne $? ]
   1.153 +        then
   1.154 +            echo "An issue occured while pulling changes from remote server."
   1.155 +            exit 1
   1.156 +        fi
   1.157 +        
   1.158 +        cd $BASE_FOLDER
   1.159 +    done
   1.160 +fi
   1.161 +
   1.162 +#################################
   1.163 +# Update repository to most recent version
   1.164 +#################################
   1.165 +print_info "Updating repositories."
   1.166 +for REPO in $SELECTED_REPOS
   1.167 +do
   1.168 +    cd $REPO
   1.169 +    pwd
   1.170 +    
   1.171 +    if [ -n "$VERSION" ]
   1.172 +    then
   1.173 +        hg update $VERSION
   1.174 +        if [ 0 -ne $? ]
   1.175 +        then
   1.176 +            #update not possible. falling back to recent version or interactive mode
   1.177 +            if [ -n "$INTERACTIVE_UPDATE" ]
   1.178 +            then
   1.179 +                while true
   1.180 +                do
   1.181 +                    read -p "Fallback to recent version? [y/n]: " answer
   1.182 +                    if [ "$answer" = "y" ]
   1.183 +                    then
   1.184 +                        hg update
   1.185 +                        break
   1.186 +                    fi
   1.187 +                    if [ "$answer" = "n" ]
   1.188 +                    then
   1.189 +                        echo "Not updated."
   1.190 +                        break
   1.191 +                    fi
   1.192 +                done
   1.193 +            else
   1.194 +                hg update
   1.195 +            fi
   1.196 +        fi
   1.197 +    else
   1.198 +        hg update
   1.199 +    fi
   1.200 +    if [ 0 -ne $? ]
   1.201 +    then
   1.202 +        echo "Error updating the repository."
   1.203 +        exit 1
   1.204 +    fi
   1.205 +    
   1.206 +    cd $BASE_FOLDER
   1.207 +done