aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/updot
blob: f0179adae1c69e6126d4b009ab011e4c4a4277a1 (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
#!/bin/sh

# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.

main()
{
        DOTFILES_REMOTE="origin"
        DOTFILES_BRANCH="master"

        # Handle opts
        while getopts ":h" opt
        do
                case "$opt" in
                        b) DOTFILES_BRANCH="$OPTARG" ;;
                        h) usage && exit 0 ;;
                        u) DOTFILES_REMOTE="$OPTARG" ;;
                        *)
                                printf "Invalid option passed: %s\n" "$OPTARG" >&2
                                ;;
                esac
        done

        shift $(( OPTIND - 1 ))

        # Stash any pending changes
        git -C "$HOME" stash

        # Update the main repository
        git -C "$HOME" fetch "$DOTFILES_REMOTE" "$DOTFILES_BRANCH"
        git -C "$HOME" reset --hard "$DOTFILES_REMOTE/$DOTFILES_BRANCH"

        # Pull updates for all submodules
        git -C "$HOME" submodule update --init --recursive --remote

        # Apply the stash
        git -C "$HOME" stash apply
}

usage()
{
        cat <<EOF
Usage:
        ${0##*/} -h
        ${0##*/} [-b <branch>] [-u <remote>]

Utility application for dotfile maintenance.

Options:
        -b  The name of the branch to use, defaults to "master"
        -h  Show this help text and exit.
        -u  The name of the remote to use, defaults to "origin"
EOF
}

main "$@"