summaryrefslogtreecommitdiffstats
path: root/.local/bin/fetchgit
blob: f33a408d82cdb4c3288d42233f5c02b3997dc41c (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env sh

verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand.
while :; do
        case $1 in
        -v)
                verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity.
                ;;
        --) # End of all options.
                shift
                break
                ;;
        -?*)
                printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
                ;;
        *) # Default case: If no more options then break out of the loop.
                break ;;
        esac
        shift
done

CWD=$PWD
if [ "$#" -gt 0 ]; then
        for d in "$@"; do
                case $d in
                        "."*) cd ${CWD}/${d} ;;
                        *) cd ${d} ;;
                esac

		if [ $verbose -gt 0 ]; then
			echo $PWD
		fi
                if [ ! -d .git ]; then # If $PWD/.git doesn't exist
                        # then are we in a ".git" dir? jump up one dir
                        if [ ${PWD##*/} = ".git" ]; then
                                cd ..
                        else #Finally, are we at least in a work tree?
                                if [ ! $(git rev-parse --is-inside-work-tree) ]; then
                                        echo "Not in a git directory"
                                        exit
                                fi
                        fi
                fi

                rems=$(eval "git remote") # list all remotes
                len_rems=${#rems}
                if [ $len_rems -lt 1 ]; then
                        # If the list of remotes is zero, we don't do any remote ops
                        exit 0
                fi

                CURRENT_BRANCH=$(git symbolic-ref --short HEAD)
                # There should always be an origin remote if there is at least one remote
                DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')

                git fetch --all --prune --quiet
                FETCH_FAILED=$?

                git switch --quiet $DEFAULT_BRANCH

                if [ $FETCH_FAILED -ne 0 ]; then
                        # We should almost always be able to connect to origin
                        git pull -q --ff-only --autostash origin $DEFAULT_BRANCH
                else
                        if git config remote.upstream.url >/dev/null; then
                                git pull -q --ff-only --autostash upstream $DEFAULT_BRANCH
                                git push -q origin $DEFAULT_BRANCH
                        elif git config remote.upstream.tydavis >/dev/null; then
                                git pull -q --ff-only --autostash tydavis $DEFAULT_BRANCH
                                git push -q origin $DEFAULT_BRANCH
                        else
                                git pull -q --ff-only --autostash origin $DEFAULT_BRANCH
                        fi
                fi

                git switch --quiet $CURRENT_BRANCH
                # Update current branch if out of date
                git pull -q --ff-only --autostash origin $CURRENT_BRANCH

        done
else
        # Current directory invocation
        if [ $verbose -gt 0 ]; then
                echo $PWD
        fi
        if [ ! -d .git ]; then # If $PWD/.git doesn't exist
                # then are we in a ".git" dir? jump up one dir
                if [ ${PWD##*/} = ".git" ]; then
                        cd ..
                else #Finally, are we at least in a work tree?
                        if [ ! $(git rev-parse --is-inside-work-tree) ]; then
                                echo "Not in a git directory"
                                exit
                        fi
                fi
        fi

        rems=$(eval "git remote") # list all remotes
        len_rems=${#rems}
        if [ $len_rems -lt 1 ]; then
                # If the list of remotes is zero, we don't do any remote ops
                exit 0
        fi

        CURRENT_BRANCH=$(git symbolic-ref --short HEAD)
        # There should always be an origin remote if there is at least one remote
        DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')

        git fetch --all --prune --quiet
        FETCH_FAILED=$?

        git switch --quiet $DEFAULT_BRANCH

        if [ $FETCH_FAILED -ne 0 ]; then
                # We should almost always be able to connect to origin
                git pull -q --ff-only --autostash origin $DEFAULT_BRANCH
        else
                if git config remote.upstream.url >/dev/null; then
                        git pull -q --ff-only --autostash upstream $DEFAULT_BRANCH
                        git push -q origin $DEFAULT_BRANCH
                elif git config remote.upstream.tydavis >/dev/null; then
                        git pull -q --ff-only --autostash tydavis $DEFAULT_BRANCH
                        git push -q origin $DEFAULT_BRANCH
                else
                        git pull -q --ff-only --autostash origin $DEFAULT_BRANCH
                fi
        fi

        git switch --quiet $CURRENT_BRANCH
        # Update current branch if out of date
        git pull -q --ff-only --autostash origin $CURRENT_BRANCH
fi