summaryrefslogtreecommitdiffstats
path: root/update_all_submodules
blob: 776fff88aa768ceaca33edc1f13f5fe099196dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash

# echo in bold
echobd () {
    if [ -p /dev/stdout ]; then
        echo "$1"
    else
        echo -ne "\033[1m"
        echo -n "$1"
        echo -e "\033[0m"
    fi
}

# verify git directory and branch
if [[ ! -e .git ]] &&
   [[ -z "`git rev-parse --git-dir 2>/dev/null`" ]]; then
	echo "Current directory does not contain a .git folder.  Exiting..."
	exit 1
fi

branch=`git symbolic-ref -q HEAD | sed "s|^refs/heads/||"`
if [[ -z "$branch" ]]; then
    branch=`git branch --contains HEAD | egrep -v "no branch|detached" | head -n1 | cut -c 3-`
fi
if [[ -z "$branch" ]] ||
   [[ -z "`git rev-parse --symbolic-full-name --remotes=\"*/$branch\"`" ]]; then
	echo "There is not active upstream branch.  Exiting..."
	exit 1
fi

# get git user
GITUSER="Automated System <$(git config --get user.email)>"

# check git abilities
if [[ -n "`git status --help 2>/dev/null|grep -- '--ignore-submodules'`" ]]; then
  GIT_IGNORE_SUBMODULES="--ignore-submodules"
fi
if [[ -n "`git pull --help |grep -- '--\[no-\]recurse-submodules'`" ]]; then
  GIT_NO_RECURSE_SUBMODULES="--no-recurse-submodules"
fi

# commmit changed module
commitModule() {
    if [[ "$1" == "" ]]; then
        return
    fi
    local MODULE=$1

    cd $PARENTDIR/$MODULE/..
    cd `git rev-parse --show-toplevel`
    if [[ ! -z "`git status --porcelain $PARENTDIR/$MODULE`" ]]; then
        echo "Committing changes to $PWD"
        git add $PARENTDIR/$MODULE
        git commit $PARENTDIR/$MODULE --author "$GITUSER" -m "Reset submodule $MODULE to latest HEAD"
    fi
    if [[ "`git rev-parse HEAD`" != "`git rev-parse origin/$branch`" ]]; then
        echo "Push changes for $PWD"
        git pull --rebase $GIT_NO_RECURSE_SUBMODULES
        git push origin HEAD
    fi
}

# update module and submodules
updateModule() {
    local MODULE
    if [[ "$1" != "" ]]; then
        MODULE=$1/
    else
        MODULE=""
    fi

    cd $PARENTDIR/$MODULE
    if [[ ! -z "`git status --porcelain $GIT_IGNORE_SUBMODULES`" ]]; then
        git reset --hard HEAD
        git clean -dxff
    fi
    if [[ "$1" != "" ]]; then
        git checkout $branch
    fi
    git pull --rebase $GIT_NO_RECURSE_SUBMODULES
    if [[ "`git rev-parse HEAD`" != "`git rev-parse origin/$branch`" ]]; then
        echo "Push changes for $PWD"
        git push origin HEAD
    fi
    if [[ "$1" != "" ]]; then
        commitModule $1
    fi

    if [[ -e $PARENTDIR/$MODULE.gitmodules ]]; then
        sed -n "s|^\[submodule \"\([^\"]*\)\"\]$|\1|p" <$PARENTDIR/$MODULE.gitmodules |\
        while read submodule; do
            echobd "Attempting to reset submodule ${MODULE}${submodule}"
            cd $PARENTDIR/$MODULE
            if [[ -z "`git config --get submodule.$submodule.url`" ]]; then
                git submodule init -- $submodule
            fi
            if [[ ! -e "$submodule/.git" ]]; then
                git submodule update -- $submodule
            fi
            updateModule ${MODULE}${submodule}
        done
        if [[ "$1" != "" ]]; then
            commitModule $1
        fi
    fi
}

# Update from top module
if [[ -e /var/lock/update-tde-git-submodules-$branch ]]; then
        echo "TDE GIT submodules are currently being updated"
        echo "If this is not the case, please remove the lockfile"
        echo "/var/lock/update-tde-git-submodules-$branch"
        exit 0
fi
touch /var/lock/update-tde-git-submodules-$branch
echobd "Updating submodules in $(git rev-parse --show-toplevel | xargs -r basename) $branch branch"
cd `git rev-parse --show-toplevel`
PARENTDIR=$PWD
echobd "Working in $PARENTDIR"
updateModule
echobd "Done in $PARENTDIR"
rm /var/lock/update-tde-git-submodules-$branch