changeset 600:33b86caf4b1a

Find nearest tag to selected revision.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Sat, 12 Dec 2015 17:10:21 +0200
parents b0859d9290ce
children ea6a5dc5893b
files Makefile dvcs/Makefile dvcs/mygit-nearest-tag dvcs/myhg-nearest-tag
diffstat 4 files changed, 156 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Dec 12 17:09:01 2015 +0200
+++ b/Makefile	Sat Dec 12 17:10:21 2015 +0200
@@ -20,7 +20,7 @@
   host_os = cygwin
 endif
 
-PROJS := emacs misc printarg hg pathsearch mc
+PROJS := emacs misc printarg dvcs hg pathsearch mc
 ifeq '$(host_os)' 'cygwin'
   PROJS += win cygwin
 endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dvcs/Makefile	Sat Dec 12 17:10:21 2015 +0200
@@ -0,0 +1,58 @@
+SHELL = /bin/sh
+
+# Disable built in pattern rules.
+MAKEFLAGS += -r
+# Disable built in variables.
+MAKEFLAGS += -R
+# Disable built in suffix rules.
+.SUFFIXES:
+# Delete target file if command fails.
+.DELETE_ON_ERROR:
+# Default target.
+.DEFAULT_GOAL = all
+
+ifeq '' '$(prefix)'
+  ifneq '' '$(HOME)'
+    prefix = $(HOME)/usr
+  else
+    $(error HOME env var and prefix var are not set!)
+  endif
+endif
+
+host_os = unix
+ifneq '' '$(COMSPEC)'
+  host_os = windows
+endif
+
+bindir = $(prefix)/bin
+
+.PHONY: all
+all:
+
+.PHONY: check
+check:
+
+BIN_FILES := mygit-nearest-tag myhg-nearest-tag
+
+.PHONY: install
+install:
+	[ -d $(bindir) ] || mkdir -p $(bindir)
+	\
+for f in $(BIN_FILES); do \
+  install -m 755 $$f $(bindir); \
+done
+
+INSTALLED_FILES := $(patsubst %,$(bindir)/%,$(BIN_FILES))
+
+.PHONY: uninstall
+uninstall:
+	for file in $(INSTALLED_FILES); do \
+		[ -e $$file ] && if rm $$file; then :; else echo Unable to delete $$file; exit 1; fi || :; \
+	done
+
+.PHONY: distclean
+distclean: clean
+
+.PHONY: clean
+clean:
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dvcs/mygit-nearest-tag	Sat Dec 12 17:10:21 2015 +0200
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+nearest_tag_by_date() {
+  git tag | while read t; do
+    b=`git merge-base $1 $t`
+    echo `git log -n 1 $b --format=%ai` "  "$t
+  done | sort
+}
+
+nearest_tag_by_rev() {
+  git tag | while read t; do
+    echo `git rev-list --count $t..$1` $t;
+  done | sort -n -r | while read n t; do printf "%8s  %s\n" $n $t; done
+}
+
+usage() {
+cat <<EOF
+  mygit-nearest-tag [--by-date | --by-rev] [REV]
+
+  --by-date     sort by dates to marge base
+  --by-date     sort by rev counts to marge base
+
+  REV           from which revision (default HEAD)
+EOF
+}
+
+distance=nearest_tag_by_date
+case "$1" in
+  --by-date)
+    shift
+    distance=nearest_tag_by_date
+    ;;
+  --by-rev)
+    shift
+    distance=nearest_tag_by_rev
+    ;;
+  --help)
+    usage
+    exit 0
+    ;;
+esac
+
+if [ -z "$1" ]; then
+  set HEAD
+fi
+
+$distance $1
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dvcs/myhg-nearest-tag	Sat Dec 12 17:10:21 2015 +0200
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+nearest_tag_by_date() {
+  hg log -r 'tag()' --template '{node} {tags}\n' | while read r t; do \
+    hg log -r "ancestor($r,$1)" --template "{date|isodate}  $t\n"; \
+  done | sort
+}
+
+nearest_tag_by_rev() {
+  hg log -r 'tag()' --template '{node} {tags}\n' | while read r t; do
+    n=`hg log -r "ancestor($r,$1)::$1" --template "X\n" | wc -l`;
+    echo $n $t;
+  done | sort -n -r | while read n t; do printf "%8s  %s\n" $n "$t"; done
+}
+
+usage() {
+cat <<EOF
+  myhg-nearest-tag [--by-date | --by-rev] [REV]
+
+  --by-date     sort by dates to marge base
+  --by-date     sort by rev counts to marge base
+
+  REV           from which revision (default is ".")
+EOF
+}
+
+distance=nearest_tag_by_date
+case "$1" in
+  --by-date)
+    shift
+    distance=nearest_tag_by_date
+    ;;
+  --by-rev)
+    shift
+    distance=nearest_tag_by_rev
+    ;;
+  --help)
+    usage
+    exit 0
+    ;;
+esac
+
+if [ -z "$1" ]; then
+  set .
+fi
+
+echo $distance $1
+$distance $1
+