From a041727d53e89022b29121c6290f89c23ab6c298 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Sun, 22 Aug 2021 03:11:09 -0500 Subject: Add tracking toggle to repo to prevent unintentional updates --- git-update-agent | 94 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 17 deletions(-) (limited to 'git-update-agent') diff --git a/git-update-agent b/git-update-agent index 0a75161..ec3916e 100644 --- a/git-update-agent +++ b/git-update-agent @@ -1,7 +1,6 @@ #!/bin/sh # GLOBAL VARIABLES: -# - AGENT: Name of the project which is receiving updates (for display purposes) # - UPSTREAM_BRANCH: The branch in the upstream repo used for updates (defaults to remote's head if set) # - REMOTE: Name of the remote used for updates # - REMOTE_URL: Name of URL used for synchronization @@ -9,7 +8,7 @@ # USAGE: # # Initialize a project: -# git-update-agent --init https://git.mydomain.com +# git-update-agent --init https://git.example.com # # Update: # git-update-agent @@ -176,6 +175,49 @@ init_if_empty() { fi } +should_track() { + echo $(git config updateagent.track) +} + +set_should_track() { + git config updateagent.track "$1" +} + +die_not_tracking() { + die <<_EOF +$PROGRAM is not tracking $(pwd). To enable tracking: +$PROGRAM --track +_EOF +} + +set_tracking() { + local dir + dir="$1" + + local track + track="$2" + + cd_or_die "$dir" + + if [[ ! -d ".git" ]]; then + die <<_EOF +$(pwd) is not the root of a git project +_EOF + fi + + set_should_track "$2" +} + +disable_tracking() { + local dir + dir="$1" + + cd_or_die "$dir" + in_git_or_die + + set_should_track false +} + update() { local dir local previous_version @@ -186,14 +228,12 @@ update() { previous_version="$(current_version)" - if [[ -z "$AGENT" ]]; then - AGENT="$(basename "$(pwd)")" - fi - - depends git - init_if_empty + if [[ "$(should_track)" != "true" ]]; then + die_not_tracking + fi + local remote local remote_branch local local_branch @@ -213,20 +253,22 @@ update() { next_version="$(current_version)" if [[ "$previous_version" == "" ]]; then - echo "Initialized $AGENT with \"$previous_version\"" + echo "Initialized $PROGRAM with \"$previous_version\"" elif [[ "$previous_version" != "$next_version" ]]; then - echo "Updated $AGENT from version \"$previous_version\" to \"$next_version\"" + echo "Updated $PROGRAM from version \"$previous_version\" to \"$next_version\"" else - echo "$AGENT is already up to date." + echo "$PROGRAM is already up to date." fi } print_usage() { cat <<_EOF Usage: ${PROGRAM} [OPTIONS]... - -h, --help output this help info - -d, --dir update in directory - --init initialize a project with the given URL + -h, --help output this help info + -d, --dir update in directory + --init initialize a project with the given URL + --track track a git repository not initalized with $PROGRAM + --disable-tracking disable tracking _EOF } @@ -248,6 +290,7 @@ init_repo() { git branch -M "$DEFAULT_BRANCH" git config remote.origin.url "$remote_url" git config remote.origin.fetch "+refs/heads/*:refs/remotes/$remote/*" + set_should_track true git fetch --force --tags "$remote" &>/dev/null git remote set-head "$remote" --auto >/dev/null @@ -274,6 +317,8 @@ git_update_agent() { local dir local option local remote_url + local track + local disable_tracking while [[ $# -gt 0 ]]; do case "$1" in @@ -302,19 +347,34 @@ git_update_agent() { shift ;; + --track) track=1; shift ;; + --disable-tracking) disable_tracking=1; shift ;; *) die "$(print_usage)" ;; esac done + depends git + if [[ -z "$dir" ]]; then dir="." fi - if [[ -z "$remote_url" ]]; then - update "$dir" - else + if [[ -n "$remote_url" ]]; then init_repo "$remote_url" + exit 0 fi + + if [[ -n "$track" ]]; then + set_tracking "$dir" "true" + exit 0 + fi + + if [[ -n "$disable_tracking" ]]; then + set_tracking "$dir" "false" + exit 0 + fi + + update "$dir" } git_update_agent "$@" -- cgit v1.2.3