tl; dr
git describe --contains `git log --pretty=format:%H -n 1 myfile.txt` | sed 's/\(.*\)[~^].*/\1/'
Документация
Соответствующие описания флагов из документации git-description :
--always
Show uniquely abbreviated commit object as fallback.
Это нене то, что вы хотите.Это позволяет печатать хеш коммита вместо тега.
--tags
Instead of using only the annotated tags, use any tag found in refs/tags
namespace. This option enables matching a lightweight (non-annotated) tag.
Это ближе, поскольку позволяет использовать все теги, аннотированные или нет.Тем не менее, он по-прежнему подчиняется стандартному поведению поиска тега перед последним коммитом.
--contains
Instead of finding the tag that predates the commit, find the tag that comes
after the commit, and thus contains it. Automatically implies --tags.
Бинго.
Пример использования
Учитываяхранилище со следующей историей коммитов:
$ git log --decorate=short -p | grep -v Author
commit d79ae00046a3ce456316fb431af5c4473a9868c8 (HEAD -> master, tag: v0.0.3)
Date: Mon May 28 22:54:33 2018 -0700
Commit #5
diff --git a/foo.txt b/foo.txt
index 257cc56..3bd1f0e 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
foo
+bar
commit 7921bbcd4bb0712e4b819231829bed5a857f99a5
Date: Mon May 28 22:54:11 2018 -0700
Commit #4
diff --git a/test.txt b/test.txt
index 7698346..fadbf1d 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
test1
test2
test3
+test4
commit fbe5a73bc2b5edcd3cb7afa26b80f8ecb12f982d (tag: v0.0.2)
Date: Mon May 28 22:53:28 2018 -0700
Commit #3
diff --git a/test.txt b/test.txt
index bae42c5..7698346 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
test1
test2
+test3
commit 794519596d9e2de93ec71686a1708e5f81fbba21
Date: Mon May 28 22:52:51 2018 -0700
Commit #2
diff --git a/test.txt b/test.txt
index a5bce3f..bae42c5 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
test1
+test2
commit 10f854c9c09ac6c4de10311ffb5809f09a1edd1a (tag: v0.0.1)
Date: Mon May 28 22:52:00 2018 -0700
Commit #1
diff --git a/foo.txt b/foo.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/foo.txt
@@ -0,0 +1 @@
+foo
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..a5bce3f
--- /dev/null
+++ b/test.txt
@@ -0,0 +1 @@
+test1
Обратите внимание, что файл test.txt
редактируется в коммитах # 1-4, но не в # 5.Коммит № 5 помечен v0.0.3
, что мы и хотим выводить.
Запуск только команд git
приводит к следующему выводу:
$ git describe --contains `git log --pretty=format:%H -n 1 test.txt`
v0.0.3~1
~1
означает, чтопоследнее изменение файла - 1 коммит за предоставленным тегом.Трубопровод к sed
получает тег сам по себе, ради полноты.
$ git describe --contains `git log --pretty=format:%H -n 1 test.txt` | sed 's/\(.*\)[~^].*/\1/'
v0.0.3