emacs.g/bin/borg-bootstrap
2016-12-01 16:14:53 +01:00

102 lines
3.1 KiB
Bash
Executable File

#!/bin/sh
# Copyright (C) 2016 Jonas Bernoulli
#
# Author: Jonas Bernoulli <jonas@bernoul.li>
# License: GPL v3 <https://www.gnu.org/licenses/gpl-3.0.txt>
# Commentary:
# This script is a replacement for `git submodule update'. The
# main differences are that this script resets the current branch
# if necessary instead of detaching HEAD and that it is capable of
# adding and configuring additional remotes.
# The following variables can be set in ".gitmodules":
#
# * submodule.DRONE.remote = NAME URL
#
# This variable specifies an additional remote named DRONE that is
# fetched from URL.
#
# * borg.collective = REMOTE
#
# This variable specifies the name used for remotes that reference
# a repository that has been patched by the collective. If a NAME
# matches REMOTE, then it is configured as the upstream of the
# current branch of the respective DRONE.
#
# * borg.pushDefault = DEFAULT
#
# This variable specifies a name used for push-remotes. If a NAME
# matches DEFAULT, then it is configured as the push-remote of the
# current branch of the respective DRONE.
# If an error occurs while `make bootstrap' runs this script, then
# fix the error(s) and run `bin/borg-bootstrap' directly, followed
# by `make build'.
hive_remote=$(git config -f .gitmodules borg.collective)
push_remote=$(git config -f .gitmodules borg.pushDefault)
git submodule--helper list |
while read mode sha1 stage path
do
if test -e "$path" && ! test -e "$path"/.git
then
name=$(git submodule--helper name "$path")
echo "\n--- [$name] ---\n"
git submodule--helper clone \
--name "$name" \
--path "$path" \
--url $(git config -f .gitmodules submodule."$name".url)
git config -f .gitmodules --get-all submodule."$name".remote |
while read remote remote_url
do
if ! test -e "$path"/.git
then
git submodule--helper clone \
--name "$name" \
--path "$path" \
--url "$remote_url" &&
git remote rename origin "$remote"
else
(
cd "$path"
git remote add "$remote" "$remote_url"
git fetch "$remote"
)
fi
if test -e "$path"/.git
then
(
cd "$path"
if test "$remote" = "$hive_remote"
then
git config branch.master.remote "$remote"
elif test "$remote" = "$push_remote"
then
git config remote.pushDefault "$remote"
fi
)
fi
done
if test -e "$path"/.git
then
(
cd "$path"
git reset --hard "$sha1" ||
echo >&2 "futile: checkout of '$sha1' into submodule path '$path' failed"
)
else
echo >&2 "futile: clone of any remote into submodule path '$path' failed"
fi
fi
done