Me@0: #! /bin/bash Me@0: Me@0: usage=$(cat << EOF Me@0: Me@0: Usage: $0 [-h] [-a] [-r] [-k] [-w] [-b Commit/Tag/Branch] Me@0: Me@0: -a Apply all the changes to all repositories in the current tree. Me@0: Me@0: -b Update to a commit/tag/branch. This is useful if you work on a single Me@0: branch/tag in all repositories. If the requested repository is not Me@0: found it falls back to the recent version. The -k and -i options are Me@0: useful here. Me@0: Me@0: -h Prints this usage information Me@0: Me@0: -i Update to a commit/tag/branch with the -b flag in interactive mode. Me@0: This asks if you want to fall back to the recent version if the Me@0: requested commit/tag/branch is not found. Me@0: This is only useful with the -b flag. Me@0: Me@0: -k This does not pull from the remote repository, just updates the Me@0: repositories. Me@0: Me@0: -r Revert to the last version. This is not deleting the pull commit, Me@0: just updating back to your last state. Me@0: Me@0: -w Do the pull over https instead of ssh Me@0: EOF Me@0: ) Me@0: Me@0: SAVED_STATE_FILE=~/.hg.saved Me@0: Me@0: #set selected repository to current folder Me@0: SELECTED_REPOS=$(pwd) Me@0: Me@0: source repo_update_helper.sh Me@0: Me@0: ################################# Me@0: #Parse arguments Me@0: ################################# Me@0: while getopts "arb:hikw" flag Me@0: do Me@0: case $flag in Me@0: a) Me@0: #update all repositories in the directory tree Me@0: SELECTED_REPOS=$(find_repos) Me@0: ;; Me@0: r) Me@0: #revert to commits after the last pull from the server Me@0: REVERT="1" Me@0: ;; Me@0: b) Me@0: #update to a tag/branch/commit Me@0: VERSION=$OPTARG Me@0: ;; Me@0: k) Me@0: NO_UPDATE="1" Me@0: ;; Me@0: i) Me@0: INTERACTIVE_UPDATE="1" Me@0: ;; Me@0: w) Me@0: REMOTE="http" Me@0: ;; Me@0: h) Me@0: echo "$usage" Me@0: exit 0 Me@0: ;; Me@0: *) Me@0: echo "$usage" Me@0: exit 0 Me@0: ;; Me@0: esac Me@0: done Me@0: Me@0: if [ ! -e $SAVED_STATE_FILE ] Me@0: then Me@0: touch $SAVED_STATE_FILE Me@0: fi Me@0: SAVED_STATE=$(cat $SAVED_STATE_FILE | sed "s/^$//g") Me@0: BASE_FOLDER=$(pwd) Me@0: Me@0: ################################# Me@0: # Revert changes and exit. Me@0: ################################# Me@0: if [ -n "$REVERT" ] Me@0: then Me@0: print_info "Reverting changes" Me@0: for REPO in $SELECTED_REPOS Me@0: do Me@0: cd $REPO Me@0: pwd Me@0: Me@0: REVERT_COMMIT=$(echo "$SAVED_STATE" | grep "$PWD " | grep -o -E "[a-z0-9]{12}") Me@0: if [ -z "$REVERT_COMMIT" ] Me@0: then Me@0: echo "No saved commit." Me@0: cd $BASE_FOLDER Me@0: continue Me@0: fi Me@0: hg update $REVERT_COMMIT Me@0: Me@0: cd $BASE_FOLDER Me@0: done Me@0: exit 0 Me@0: fi Me@0: Me@0: ################################# Me@0: # Save the current current state of the repository Me@0: ################################# Me@0: print_info "Saving current commit." Me@0: for REPO in $SELECTED_REPOS Me@0: do Me@0: cd $REPO Me@0: pwd Me@0: if [ 0 -ne $? ] Me@0: then Me@0: echo "Cannot change to repository $REPO" Me@0: exit 1 Me@0: fi Me@0: Me@0: STATE=$(hg summary) Me@0: if [ 0 -ne $? ] Me@0: then Me@0: echo "Not a repository: $REPO" Me@0: exit 1 Me@0: fi Me@0: CURRENT_COMMIT=$(echo "$STATE" | grep -o -E "[0-9]:[a-z0-9]{12}" | grep -o -E "[a-z0-9]{12}") Me@0: Me@0: #remove current repository from saved state Me@0: SAVED_STATE=$(echo "$SAVED_STATE" | grep -v "$PWD ") Me@0: SAVED_STATE=$(echo -e "$SAVED_STATE" && echo "$PWD $CURRENT_COMMIT") Me@0: Me@0: cd $BASE_FOLDER Me@0: done Me@0: echo "$SAVED_STATE" > $SAVED_STATE_FILE Me@0: Me@0: ################################# Me@0: # Pull changes from remote server. Me@0: ################################# Me@0: if [ -z "$NO_UPDATE" ] Me@0: then Me@0: print_info "Pull changes from remote." Me@0: for REPO in $SELECTED_REPOS Me@0: do Me@0: cd $REPO Me@0: pwd Me@0: hg pull $REMOTE Me@0: if [ 0 -ne $? ] Me@0: then Me@0: echo "An issue occured while pulling changes from remote server." Me@0: exit 1 Me@0: fi Me@0: Me@0: cd $BASE_FOLDER Me@0: done Me@0: fi Me@0: Me@0: ################################# Me@0: # Update repository to most recent version Me@0: ################################# Me@0: print_info "Updating repositories." Me@0: for REPO in $SELECTED_REPOS Me@0: do Me@0: cd $REPO Me@0: pwd Me@0: Me@0: if [ -n "$VERSION" ] Me@0: then Me@0: hg update $VERSION Me@0: if [ 0 -ne $? ] Me@0: then Me@0: #update not possible. falling back to recent version or interactive mode Me@0: if [ -n "$INTERACTIVE_UPDATE" ] Me@0: then Me@0: while true Me@0: do Me@0: read -p "Fallback to recent version? [y/n]: " answer Me@0: if [ "$answer" = "y" ] Me@0: then Me@0: hg update Me@0: break Me@0: fi Me@0: if [ "$answer" = "n" ] Me@0: then Me@0: echo "Not updated." Me@0: break Me@0: fi Me@0: done Me@0: else Me@0: hg update Me@0: fi Me@0: fi Me@0: else Me@0: hg update Me@0: fi Me@0: if [ 0 -ne $? ] Me@0: then Me@0: echo "Error updating the repository." Me@0: exit 1 Me@0: fi Me@0: Me@0: cd $BASE_FOLDER Me@0: done