summaryrefslogtreecommitdiffstats
path: root/src/subgit-init
diff options
context:
space:
mode:
authorLouis Burda <dev@sinitax.com>2025-10-28 21:35:16 +0100
committerLouis Burda <dev@sinitax.com>2025-10-28 21:35:16 +0100
commita6d91d21ec6f5dce4ba6ebac1cf86e5338b32ae6 (patch)
treed20b56dbd285bb9ef58f3dc6aeab61a8a91e02a9 /src/subgit-init
parent0246674aa837e3083ef3cb14be8aaa87492a3dd2 (diff)
downloadsubgit-a6d91d21ec6f5dce4ba6ebac1cf86e5338b32ae6.tar.gz
subgit-a6d91d21ec6f5dce4ba6ebac1cf86e5338b32ae6.zip
Add untested rewrite
Diffstat (limited to 'src/subgit-init')
-rwxr-xr-xsrc/subgit-init88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/subgit-init b/src/subgit-init
new file mode 100755
index 0000000..3299f76
--- /dev/null
+++ b/src/subgit-init
@@ -0,0 +1,88 @@
+#!/bin/bash
+
+# Initializes or updates the bare repository in the subgit container.
+
+usage() { die "Usage: subgit [init|pull] [-r] [PATH..]"; }
+
+recursive=
+force=
+update=
+do_source=1
+while [ "$1" ]; do
+ case $1 in
+ -h) usage;;
+ -r) recursive=-r; shift;;
+ -u) update=-u; shift;;
+ -f) force=-f; shift;;
+ -S) do_source=0; shift;;
+ --) shift; break;;
+ -*) die "Invalid option $1";;
+ *) break;;
+ esac
+done
+
+if [ $do_source -eq 1 ]; then
+ root=$(subgit-source root .)
+ source "$root/.subgitrc"
+fi
+
+readarray -t subrepos < <(subgit-sub subrepo "$@")
+
+for subrepo in "${subrepos[@]}"; do
+ echo "$subrepo"
+
+ root=$(subgit-sub parent "$subrepo")
+ relpath=$(realpath -m --relative-to="$root" "$subrepo")
+
+ bare="$root/.subgit/$relpath"
+ workdir="$root/$relpath"
+ test "$workdir" = "$subrepo"
+
+ [ ! -d "$workdir" ] && mkdir -p "$workdir"
+
+ clone=0
+ commit=${subgitinfo[$relpath/commit]:-HEAD}
+ branch=${subgitinfo[$relpath/branch]}
+ if [ ! -d "$bare" ]; then
+ clone=1
+ mkdir -p "$(dirname "$bare")"
+ clone_args=()
+ [ ! -z "$branch" ] && clone_args+=(-b "$branch")
+ subgit-sub clone "${clone_args[@]}" \
+ "${subgitinfo[$relpath/remote]}" "$bare"
+ fi
+
+ link_relpath=$(realpath --relative-to="$workdir" "$bare")
+ ln -sf "$link_relpath" -T "$workdir/.git"
+
+ if [ ! -z "$branch" ]; then
+ fetch_spec=(origin "$branch")
+ else
+ fetch_spec=(-a)
+ fi
+
+ if ! git -C "$workdir" cat-file -e "$commit" 2>/dev/null; then
+ git -C "$workdir" fetch "${fetch_spec[@]}"
+ fi
+
+ if [ $clone -eq 1 -o ! -z "$force" ]; then
+ git -C "$workdir" reset --hard
+ fi
+
+ if [ ! -z "$branch" ]; then
+ git -C "$workdir" switch "$branch"
+ fi
+
+ git -C "$workdir" reset --soft "$commit"
+done
+
+if [ ! -z "$update" ]; then
+ subgitinfo[$relpath/commit]=$(git -C "$workdir" rev-parse HEAD)
+ subgit-source write
+fi
+
+for subrepo in "${subrepos[@]}"; do
+ if [ ! -z "$recursive" -a -e "$subrepo/.subgitrc" ]; then
+ subgit "${sg_flags[@]}" -C "$subrepo" init $force $recursive $update
+ fi
+done