aboutsummaryrefslogtreecommitdiff
path: root/reposync-functions.sh
diff options
context:
space:
mode:
authorTim Keller <tjk@tjkeller.xyz>2025-12-27 22:05:00 -0600
committerTim Keller <tjk@tjkeller.xyz>2025-12-27 22:05:00 -0600
commitf813117ee176205721f48dd5e8d7b21f1c411702 (patch)
tree41e901344c7a51c8e5dc7a4410e5638958797ee8 /reposync-functions.sh
downloadhm-reposync-f813117ee176205721f48dd5e8d7b21f1c411702.tar.xz
hm-reposync-f813117ee176205721f48dd5e8d7b21f1c411702.zip
initial commit
Diffstat (limited to 'reposync-functions.sh')
-rwxr-xr-xreposync-functions.sh83
1 files changed, 83 insertions, 0 deletions
diff --git a/reposync-functions.sh b/reposync-functions.sh
new file mode 100755
index 0000000..efb4e9b
--- /dev/null
+++ b/reposync-functions.sh
@@ -0,0 +1,83 @@
+set -e
+
+# handle base args
+for arg in "$@"; do
+ case "$arg" in
+ -v|--verbose) VERBOSE=1 ;;
+ -d|--dry-run) DRY_RUN=1 ;;
+ esac
+done
+
+[ -n "$VERBOSE" ] && VERBOSE_ARG=--verbose
+run() { [ -n "$DRY_RUN" ] && echo "DRY RUN:" $@ || $@; }
+
+# define base functions
+clonemissing() {
+ url="$1"
+ target="$2"
+ flags="$3"
+ #branch="$3"
+
+ # return if already existing
+ if [ -d "${target}/.git" ]; then
+ return
+ fi
+
+ # clone url to target
+ echo "cloning $url to $target"
+ run mkdir -p $VERBOSE_ARG "$target"
+ run git clone $VERBOSE_ARG $flags "$url" "$target"
+}
+
+pull() {
+ target="$1"
+ flags="$2"
+
+ cd "$target"
+
+ # check if local changes exist before running pull
+ if ! (git diff --quiet && git diff --cached --quiet); then
+ echo "$target: local changes exist. won't attempt to pull"
+ return
+ fi
+
+ # pull changes
+ echo "$target: pulling latest changes"
+ run git pull $VERBOSE_ARG $flags
+}
+
+remoteadd() {
+ target="$1"
+ remote="$2"
+ url="$3"
+
+ cd "$target"
+
+ # check if remote exists
+ remoteurl="$(git $VERBOSE_ARG remote get-url "$remote" 2>/dev/null)"
+ if [ $? = 0 ]; then
+ # verify remote url
+ if [ "$remoteurl" = "$url" ]; then
+ return
+ fi
+ echo "$target: the url of remote $remote ($remoteurl) does not match the expected value ($url)"
+ return
+ fi
+
+ # add remote
+ echo "$target: adding remote $remote ($remoteurl)"
+ run git remote $VERBOSE_ARG add "$remote" "$url"
+}
+
+stowrepo() {
+ dir="$1"
+ target="$2"
+ packages="$3"
+
+ cd "$dir"
+
+ echo "$target: stowing packages ($packages) to $target"
+ run stow $VERBOSE_ARG --restow --target="$target" $packages
+}
+
+# define repo specific sync funcs