annotate scripts/repo_update @ 17:2021f42cc672

scheduling consequence graph construction script
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Fri, 10 Feb 2012 16:15:55 +0100
parents
children
rev   line source
Me@0 1 #! /bin/bash
Me@0 2
Me@0 3 usage=$(cat << EOF
Me@0 4
Me@0 5 Usage: $0 [-h] [-a] [-r] [-k] [-w] [-b Commit/Tag/Branch]
Me@0 6
Me@0 7 -a Apply all the changes to all repositories in the current tree.
Me@0 8
Me@0 9 -b Update to a commit/tag/branch. This is useful if you work on a single
Me@0 10 branch/tag in all repositories. If the requested repository is not
Me@0 11 found it falls back to the recent version. The -k and -i options are
Me@0 12 useful here.
Me@0 13
Me@0 14 -h Prints this usage information
Me@0 15
Me@0 16 -i Update to a commit/tag/branch with the -b flag in interactive mode.
Me@0 17 This asks if you want to fall back to the recent version if the
Me@0 18 requested commit/tag/branch is not found.
Me@0 19 This is only useful with the -b flag.
Me@0 20
Me@0 21 -k This does not pull from the remote repository, just updates the
Me@0 22 repositories.
Me@0 23
Me@0 24 -r Revert to the last version. This is not deleting the pull commit,
Me@0 25 just updating back to your last state.
Me@0 26
Me@0 27 -w Do the pull over https instead of ssh
Me@0 28 EOF
Me@0 29 )
Me@0 30
Me@0 31 SAVED_STATE_FILE=~/.hg.saved
Me@0 32
Me@0 33 #set selected repository to current folder
Me@0 34 SELECTED_REPOS=$(pwd)
Me@0 35
Me@0 36 source repo_update_helper.sh
Me@0 37
Me@0 38 #################################
Me@0 39 #Parse arguments
Me@0 40 #################################
Me@0 41 while getopts "arb:hikw" flag
Me@0 42 do
Me@0 43 case $flag in
Me@0 44 a)
Me@0 45 #update all repositories in the directory tree
Me@0 46 SELECTED_REPOS=$(find_repos)
Me@0 47 ;;
Me@0 48 r)
Me@0 49 #revert to commits after the last pull from the server
Me@0 50 REVERT="1"
Me@0 51 ;;
Me@0 52 b)
Me@0 53 #update to a tag/branch/commit
Me@0 54 VERSION=$OPTARG
Me@0 55 ;;
Me@0 56 k)
Me@0 57 NO_UPDATE="1"
Me@0 58 ;;
Me@0 59 i)
Me@0 60 INTERACTIVE_UPDATE="1"
Me@0 61 ;;
Me@0 62 w)
Me@0 63 REMOTE="http"
Me@0 64 ;;
Me@0 65 h)
Me@0 66 echo "$usage"
Me@0 67 exit 0
Me@0 68 ;;
Me@0 69 *)
Me@0 70 echo "$usage"
Me@0 71 exit 0
Me@0 72 ;;
Me@0 73 esac
Me@0 74 done
Me@0 75
Me@0 76 if [ ! -e $SAVED_STATE_FILE ]
Me@0 77 then
Me@0 78 touch $SAVED_STATE_FILE
Me@0 79 fi
Me@0 80 SAVED_STATE=$(cat $SAVED_STATE_FILE | sed "s/^$//g")
Me@0 81 BASE_FOLDER=$(pwd)
Me@0 82
Me@0 83 #################################
Me@0 84 # Revert changes and exit.
Me@0 85 #################################
Me@0 86 if [ -n "$REVERT" ]
Me@0 87 then
Me@0 88 print_info "Reverting changes"
Me@0 89 for REPO in $SELECTED_REPOS
Me@0 90 do
Me@0 91 cd $REPO
Me@0 92 pwd
Me@0 93
Me@0 94 REVERT_COMMIT=$(echo "$SAVED_STATE" | grep "$PWD " | grep -o -E "[a-z0-9]{12}")
Me@0 95 if [ -z "$REVERT_COMMIT" ]
Me@0 96 then
Me@0 97 echo "No saved commit."
Me@0 98 cd $BASE_FOLDER
Me@0 99 continue
Me@0 100 fi
Me@0 101 hg update $REVERT_COMMIT
Me@0 102
Me@0 103 cd $BASE_FOLDER
Me@0 104 done
Me@0 105 exit 0
Me@0 106 fi
Me@0 107
Me@0 108 #################################
Me@0 109 # Save the current current state of the repository
Me@0 110 #################################
Me@0 111 print_info "Saving current commit."
Me@0 112 for REPO in $SELECTED_REPOS
Me@0 113 do
Me@0 114 cd $REPO
Me@0 115 pwd
Me@0 116 if [ 0 -ne $? ]
Me@0 117 then
Me@0 118 echo "Cannot change to repository $REPO"
Me@0 119 exit 1
Me@0 120 fi
Me@0 121
Me@0 122 STATE=$(hg summary)
Me@0 123 if [ 0 -ne $? ]
Me@0 124 then
Me@0 125 echo "Not a repository: $REPO"
Me@0 126 exit 1
Me@0 127 fi
Me@0 128 CURRENT_COMMIT=$(echo "$STATE" | grep -o -E "[0-9]:[a-z0-9]{12}" | grep -o -E "[a-z0-9]{12}")
Me@0 129
Me@0 130 #remove current repository from saved state
Me@0 131 SAVED_STATE=$(echo "$SAVED_STATE" | grep -v "$PWD ")
Me@0 132 SAVED_STATE=$(echo -e "$SAVED_STATE" && echo "$PWD $CURRENT_COMMIT")
Me@0 133
Me@0 134 cd $BASE_FOLDER
Me@0 135 done
Me@0 136 echo "$SAVED_STATE" > $SAVED_STATE_FILE
Me@0 137
Me@0 138 #################################
Me@0 139 # Pull changes from remote server.
Me@0 140 #################################
Me@0 141 if [ -z "$NO_UPDATE" ]
Me@0 142 then
Me@0 143 print_info "Pull changes from remote."
Me@0 144 for REPO in $SELECTED_REPOS
Me@0 145 do
Me@0 146 cd $REPO
Me@0 147 pwd
Me@0 148 hg pull $REMOTE
Me@0 149 if [ 0 -ne $? ]
Me@0 150 then
Me@0 151 echo "An issue occured while pulling changes from remote server."
Me@0 152 exit 1
Me@0 153 fi
Me@0 154
Me@0 155 cd $BASE_FOLDER
Me@0 156 done
Me@0 157 fi
Me@0 158
Me@0 159 #################################
Me@0 160 # Update repository to most recent version
Me@0 161 #################################
Me@0 162 print_info "Updating repositories."
Me@0 163 for REPO in $SELECTED_REPOS
Me@0 164 do
Me@0 165 cd $REPO
Me@0 166 pwd
Me@0 167
Me@0 168 if [ -n "$VERSION" ]
Me@0 169 then
Me@0 170 hg update $VERSION
Me@0 171 if [ 0 -ne $? ]
Me@0 172 then
Me@0 173 #update not possible. falling back to recent version or interactive mode
Me@0 174 if [ -n "$INTERACTIVE_UPDATE" ]
Me@0 175 then
Me@0 176 while true
Me@0 177 do
Me@0 178 read -p "Fallback to recent version? [y/n]: " answer
Me@0 179 if [ "$answer" = "y" ]
Me@0 180 then
Me@0 181 hg update
Me@0 182 break
Me@0 183 fi
Me@0 184 if [ "$answer" = "n" ]
Me@0 185 then
Me@0 186 echo "Not updated."
Me@0 187 break
Me@0 188 fi
Me@0 189 done
Me@0 190 else
Me@0 191 hg update
Me@0 192 fi
Me@0 193 fi
Me@0 194 else
Me@0 195 hg update
Me@0 196 fi
Me@0 197 if [ 0 -ne $? ]
Me@0 198 then
Me@0 199 echo "Error updating the repository."
Me@0 200 exit 1
Me@0 201 fi
Me@0 202
Me@0 203 cd $BASE_FOLDER
Me@0 204 done