From 7fb468b76d0a503897f492a98e9b9f7f238c6a50 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 30 Nov 2018 17:10:20 +1300 Subject: Add Makefile and manual page --- watch-git-tags.sh | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 watch-git-tags.sh (limited to 'watch-git-tags.sh') diff --git a/watch-git-tags.sh b/watch-git-tags.sh new file mode 100644 index 0000000..b9fdce8 --- /dev/null +++ b/watch-git-tags.sh @@ -0,0 +1,84 @@ +#!/bin/sh + +# List sorted local tags +tags_local() { + for repo ; do + git -C "$repo" tag --list | LC_COLLATE=C sort + done +} + +# List sorted remote tags +tags_remote() { + for repo ; do + git -C "$repo" ls-remote --quiet --refs --tags || + printf >&2 'Failed to retrieve tags for repository %s\n' "$PWD" + done | + while read -r _ tag ; do + tag=${tag#refs/tags/} + printf '%s\n' "$tag" + done +} + +# Create a temporary directory with name in $td, and handle POSIX-ish traps to +# remove it when the script exits; requires mktemp(1) (not POSIX) +td=$(mktemp -d) || exit +cleanup() { + [ -n "$td" ] || return + rm -fr -- "$td" +} +for sig in EXIT HUP INT TERM ; do + # shellcheck disable=SC2064 + trap "cleanup $sig" "$sig" +done + +# Use current directory if no other arguments +[ "$#" -gt 0 ] || set -- . + +# Iterate through each repo in a subshell in parallel +for repo ; do ( + + # Make a temporary directory with a hash in its name for uniqueness + name=$(printf '%s' "$repo" | sed 's:/:_:g') + cksum=$(printf '%s' "$repo" | cksum | sed 's:[^0-9].*::') + sd=$td/$name.$cksum + mkdir -- "$sd" "$sd"/tags || exit + + # Step in and write repo path to file + cd -- "$sd" || exit + printf '%s\n' "$repo" > path || exit + + # Write local and remote tags to files + tags_local "$repo" > tags/local || exit + tags_remote "$repo" > tags/remote || exit + + # Write new tags to file + LC_COLLATE=C comm -13 -- tags/local tags/remote > tags/new + + # Attempt to quietly fetch new tags so that we don't notify about the same + # ones next time + [ -s tags/new ] || exit + git -C "$repo" fetch --quiet --tags + +) & done + +# Wait for all of those to finish +wait + +# Iterate through the temp dirs in order +for dir in "$td"/* ; do ( + cd -- "$dir" || exit + + # Look for non-zero "new" files (at least one new tag) + [ -s tags/new ] || exit + + # Print repository path and new tags + cat path + while read -r tag ; do + printf '* %s\n' "$tag" + done < tags/new + +) ; done + +# Haven't yet decided on exit value semantics; for the moment, if it completes, +# exit success +exit 0 -- cgit v1.2.3 From 786702e3f1fc9aee755b539cfa633fdc061866bf Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 6 Dec 2018 14:40:33 +1300 Subject: Refactor tag generation functions --- watch-git-tags.sh | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'watch-git-tags.sh') diff --git a/watch-git-tags.sh b/watch-git-tags.sh index b9fdce8..857fbd0 100644 --- a/watch-git-tags.sh +++ b/watch-git-tags.sh @@ -1,18 +1,10 @@ -#!/bin/sh - -# List sorted local tags -tags_local() { - for repo ; do - git -C "$repo" tag --list | LC_COLLATE=C sort - done -} - -# List sorted remote tags -tags_remote() { - for repo ; do - git -C "$repo" ls-remote --quiet --refs --tags || - printf >&2 'Failed to retrieve tags for repository %s\n' "$PWD" - done | +# Function to retrieve and filter tag names +tags() { + case $1 in + remote) git -C "$repo" show-ref --tags ;; + local) git -C "$repo" ls-remote --quiet --refs --tags ;; + *) return 2 ;; + esac | while read -r _ tag ; do tag=${tag#refs/tags/} printf '%s\n' "$tag" @@ -48,8 +40,8 @@ for repo ; do ( printf '%s\n' "$repo" > path || exit # Write local and remote tags to files - tags_local "$repo" > tags/local || exit - tags_remote "$repo" > tags/remote || exit + tags local "$repo" > tags/local || exit + tags remote "$repo" > tags/remote || exit # Write new tags to file LC_COLLATE=C comm -13 -- tags/local tags/remote > tags/new -- cgit v1.2.3 From ae895daf7ec5240300bd87a7157419ce586d9a72 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 6 Dec 2018 14:52:19 +1300 Subject: Correct/adjust default behaviour with no args --- watch-git-tags.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'watch-git-tags.sh') diff --git a/watch-git-tags.sh b/watch-git-tags.sh index 857fbd0..ba63f1c 100644 --- a/watch-git-tags.sh +++ b/watch-git-tags.sh @@ -24,7 +24,9 @@ for sig in EXIT HUP INT TERM ; do done # Use current directory if no other arguments -[ "$#" -gt 0 ] || set -- . +if [ "$#" -eq 0 ] ; then + set -- "$PWD" +fi # Iterate through each repo in a subshell in parallel for repo ; do ( -- cgit v1.2.3 From 51fe73814a5aa27cbeac1df6f5a7f0db493d0e88 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 6 Dec 2018 14:53:43 +1300 Subject: Remove unneeded semicolon --- watch-git-tags.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'watch-git-tags.sh') diff --git a/watch-git-tags.sh b/watch-git-tags.sh index ba63f1c..7dabb71 100644 --- a/watch-git-tags.sh +++ b/watch-git-tags.sh @@ -29,7 +29,7 @@ if [ "$#" -eq 0 ] ; then fi # Iterate through each repo in a subshell in parallel -for repo ; do ( +for repo do ( # Make a temporary directory with a hash in its name for uniqueness name=$(printf '%s' "$repo" | sed 's:/:_:g') -- cgit v1.2.3 From b4c295ebb2ea54c4187795c7a9987e2b31b33bb9 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 6 Dec 2018 14:58:18 +1300 Subject: Correct subcommands for tags generation function I got them around the wrong way... --- watch-git-tags.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'watch-git-tags.sh') diff --git a/watch-git-tags.sh b/watch-git-tags.sh index 7dabb71..f8d4265 100644 --- a/watch-git-tags.sh +++ b/watch-git-tags.sh @@ -1,8 +1,8 @@ # Function to retrieve and filter tag names tags() { case $1 in - remote) git -C "$repo" show-ref --tags ;; - local) git -C "$repo" ls-remote --quiet --refs --tags ;; + local) git -C "${2:-.}" show-ref --tags ;; + remote) git -C "${2:-.}" ls-remote --quiet --refs --tags ;; *) return 2 ;; esac | while read -r _ tag ; do -- cgit v1.2.3