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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#!/usr/bin/env sh
# POSIX shell implementation of Gitrect
#
# Operate on a tempfile and generate a listing of:
#
# prefix-trim-directory,remote=URL,...
#
# For all directories found in $WORKDIR
# Reset all variables that might be set
DEFAULT_STATE="$HOME/.local/share/gitlist"
WORKDIR="$HOME/.code"
verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand.
if [ ! $(command -v git) ]; then
echo "git command not found. git must be installed and available in \$PATH to continue"
exit 1
fi
usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE]
Set remotes and pull new changes where:
-w set the working directory (default: ${WORKDIR})
-f set the git list file (default: ${DEFAULT_STATE})
-h show this help text
-v increase output verbosity
"
while :; do
case $1 in
-h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit.
echo "$usage"
exit
;;
-f) # Takes an option argument, ensuring it has been specified.
if [ -n "$2" ]; then
DEFAULT_STATE=$2
shift
fi
;;
-f=?*)
DEFAULT_STATE=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-f=) # Handle the case of an empty --file=
printf 'ERROR: "-f" requires a non-empty option argument.\n' >&2
exit 1
;;
-w) # Takes an option argument, ensuring it has been specified.
if [ -n "$2" ]; then
WORKDIR=$2
shift
fi
;;
-w=?*)
WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-w=) # Handle the case of an empty --file=
printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2
exit 1
;;
-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
# If there are input files (for example) that follow the options, they
# will remain in the "$@" positional parameters.
# No posix-compliant way of getting processor count, so leverage OS-specifics
FS_SEPARATOR="/"
if [ "$(uname -s)" = "Darwin" ]; then
NPROC=$(eval "sysctl -n hw.ncpu")
elif [ "$(uname -s)" = "Linux" ]; then
NPROC=$(eval "nproc")
else
# Windows uses backslash
FS_SEPARATOR="\\"
if [ $verbose -gt 0 ]; then
echo "cannot detect CPU total"
fi
fi
while read -r line; do
dir=$(echo $line | cut -d, -f 1)
remlist=$(echo $line | cut -d, -f 2-)
clone=0 #False
if [ $verbose -gt 0 ]; then
echo "Operating on ${WORKDIR}${FS_SEPARATOR}${dir}"
fi
if [ ! -d "${WORKDIR}${FS_SEPARATOR}${dir}" ]; then
#clone repo
clone=$((clone + 1)) # Set true
mkdir -p "${WORKDIR}${FS_SEPARATOR}${dir}"
fi
cd "${WORKDIR}${FS_SEPARATOR}${dir}" || exit
if [ $clone -gt 0 ]; then
git init
fi
if [ $verbose -gt 1 ]; then
echo "Remote list: ${remlist}"
fi
echo "${remlist}" | awk -F ',' '{for (i = 1; i <= NF; i++) {printf "%s \n", $i}; printf "\n"}' | while read -r r || [ -n "$r" ]; do
remoteName=$(echo $r | awk -F'=' '{print $1}') # Just get the remote name
# Allow for blank / newlines during processing
if [ "${remoteName}" = "" ]; then continue; fi
if [ -d .git/refs/remotes/$remoteName ]; then
if [ $verbose -gt 0 ]; then
echo "Setting remote ${r}"
fi
echo $r | awk -F'=' '{ print $1, $2; }' | xargs -n 2 git remote set-url
else
if [ $verbose -gt 0 ]; then
echo "Adding remote ${r}"
fi
echo $r | awk -F'=' '{ print $1, $2; }' | xargs -n 2 git remote add
fi
done
if [ $clone -gt 0 ]; then
git fetch --all
rev=$(git ls-remote origin | grep HEAD | cut -f 1)
br=$(git ls-remote origin | grep "${rev}" | grep -v HEAD | cut -f 2 | cut -d / -f 3)
git switch $br
fi
done <$DEFAULT_STATE
# Update all remotes if we have the fetchgit command
if [ $(command -v fetchgit) ]; then
if [ $(command -v fd) ]; then
fd -H -t d -E mod -E vendor "\.git$" $WORKDIR -x fetchgit
else
find $WORKDIR -name ".git" -exec fetchgit {} \;
fi
fi
|