Extreme git flow: improved.

Signed-off-by: Solomon Hykes <sh.github.6811@hykes.org>
This commit is contained in:
Solomon Hykes 2021-05-04 19:22:52 +00:00
parent 3cbdd1fc2a
commit 31c4cee255

132
cmd/dgr
View File

@ -13,88 +13,89 @@ function main() {
cat <<-EOF cat <<-EOF
repo: $(repo) repo: $(repo)
branch: $(branch) branch: $(branch)
env: $(current_env)
EOF EOF
;; ;;
list) list)
{ dashA="${1:-}"
ls "$(envdir)" 2>/dev/null || true if [ "$dashA" = "-a" ]; then
} | sort list_all_envs
;; else
up) list_all_envs | {
env="$1"; shift while read path; do
dagger "$env" up "$@" if store_stat "dagger/$(branch)" "$path" ; then
;; echo "$path"
query) fi
env="$1"; shift done
dagger "$env" query "$@" }
;; fi
set)
env="$1"; shift
dagger "$env" input json "$@"
;;
dir)
env="$1"; shift
dagger "$env" input dir "$@"
;; ;;
*) *)
env="$1"; shift dagger "$cmd" "$@"
dagger "$env" "$cmd" "$@"
;; ;;
esac esac
} }
function envdir() { function current_env() {
echo "$(repo)/.dagger/env/${1:-}" if [ "$(ls -A | grep '^.*\.cue$')" ]; then
git rev-parse --show-prefix
fi
} }
function repo() { function repo() {
git rev-parse --show-toplevel git rev-parse --show-toplevel
} }
function list_all_envs() {
(
cd $(repo)
find . \
-type f -name '*.cue' -exec dirname {} \; \
-o \
-type d -name 'cue.mod' -prune \
| sed 's/^\.\///' | sort -u
)
}
function branch() { function branch() {
git branch --show-current git branch --show-current
} }
function dagger() { function dagger() {
local env="$1"; shift local env="$(current_env)"
if [ -z "$env" ]; then
# Check that env exists fatal "current directory is not a valid environment"
if [ ! -d "$(envdir $env)" ]; then
echo >&2 "no such environment: $env"
return 1
fi fi
tmpHome="$(mktemp -d)" tmpHome="$(mktemp -d)"
tmpState="${tmpHome}/.dagger/store/$env/deployment.json" tmpState="${tmpHome}/.dagger/store/my/deployment.json"
mkdir -p "$(dirname $tmpState)" mkdir -p "$(dirname $tmpState)"
loadState "$env" > "$tmpState" loadState "$env" > "$tmpState"
HOME="$tmpHome" "$(which dagger)" -e "$env" "$@" HOME="$tmpHome" "$(which dagger)" -e "my" "$@"
cat "$tmpState" | saveState "$env" cat "$tmpState" | saveState "$env"
} }
function cleanPath() {
echo "$1" | sed -E 's/\/+/\//g'
}
function loadState() { function loadState() {
local env="$1"; shift local env="$1"
local stateFile="$(cleanPath $env/state.json)"
# echo >&2 "Loading state for $(branch)::$env" # echo >&2 "Loading state for $(branch)::$env"
loadStore | jq ".env[\"$env\"]" | store_read "dagger/$(branch)" "$stateFile" |
jq ".name=\"$env\"" | jq ".name=\"my\"" |
jq ".plan.type=\"dir\"" | jq ".plan.type=\"dir\"" |
jq ".plan.dir.path=\"$(envdir $env)\"" | jq ".plan.dir.path=\"$(repo)/$env\"" |
jq ".id=\"$env\"" jq ".id=\"my\""
} }
function saveState() { function saveState() {
local env="$1"; shift local env="$1"
local stateFile="$(cleanPath $env/state.json)"
# echo >&2 "Saving state for $(branch)::$env" # echo >&2 "Saving state for $(branch)::$env"
newState="$(cat)" newState="$(cat)"
loadStore | jq ".env[\"$env\"]=$newState" | saveStore echo "$newState" | store_write "dagger/$(branch)" "$stateFile" "Write state for env $env"
}
function saveStore() {
git config "branch.$(branch).dagger" "$(cat)"
}
function loadStore() {
git config "branch.$(branch).dagger" || echo '{}'
} }
function fatal() { function fatal() {
@ -102,4 +103,43 @@ function fatal() {
exit 1 exit 1
} }
function store_read() {
local \
branch="$1" \
path="$2"
git cat-file -p "${branch}^{tree}:$path" 2>/dev/null || echo '{}'
}
function store_stat() {
local \
branch="$1" \
path="$2"
git cat-file -p "${branch}^{tree}:$path" >/dev/null 2>&1
}
function store_write() {
local \
branch="$1" \
path="$2" \
msg="${3:-write $path}"
blob="$(git hash-object -w --stdin)" \
idx="$(mktemp -d)/idx" \
oldcommit="$(git show-ref "refs/$branch" -s || true)"
# Load current state into index (will fail if state branch doesn't exist)
GIT_INDEX_FILE="$idx" git read-tree "refs/$branch" 2>&1 || true
GIT_INDEX_FILE="$idx" git \
update-index \
--add \
--cacheinfo 100644 "$blob" "$path"
# Create new tree object
local newtree=$(GIT_INDEX_FILE="$idx" git write-tree)
# Create new commit object
if [ -z "$oldcommit" ]; then
local newcommit="$(echo $msg | git commit-tree $newtree)"
else
local newcommit="$(echo $msg | git commit-tree $newtree -p $oldcommit)"
fi
git update-ref "refs/$branch" "$newcommit"
}
main "$@" main "$@"