Which information I have: the name of the file (myfile.txt) on the master branch. The file has been modified the last time with commit "3" resulting into version 2.
What I want to retrieve: the tag name "0.0.2" which includes the commits "3" and "4" since the last tag "0.0.1".
(A) How do I get the changed files between 2 tags (see here):
git diff --name-only 0.0.1 0.0.2
This prints 'myfile.txt' among others.
(B) Normally that should work exactly for what I need (see here):
git describe --always `git log --pretty=format:%H -n 1 myfile.txt`
But then I don't get the tag name '0.0.2' or the commit related to this tag. Instead, I get the commit SHA-1 of commit 3, which is includes the newest changes of myfile.txt.
(C) A tag is annotated, if the following command prints the respective tag name:
git for-each-ref --format='%(refname) %(objecttype)' refs/tags
which prints:
refs/tags/0.0.1 tag
refs/tags/0.0.2 tag
refs/tags/0.0.3 tag
refs/tags/0.0.4 tag
Question
So my questions are: is the way (B) the right one for my purpose? If yes, how do I change it do get the desired tag name? Or is there another way than (B) to get what I need?
git describe --contains `git log --pretty=format:%H -n 1 myfile.txt` | sed 's/\(.*\)[~^].*/\1/'
The relevant flag descriptions from the git-describe documentation:
--always
Show uniquely abbreviated commit object as fallback.
This isn't what you want. This is allowing a commit hash to be printed instead of tag.
--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.
This is closer, as it allows utilization of all tags, annotated or not. However, it's still subject to the default behavior of finding the tag before the last commit.
--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.
Bingo.
Given a repository with the following commit history:
$ 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
Note that the file test.txt
is edited in commits #1-4, but not #5. Commit #5 is tagged with v0.0.3
, which is what we want as output.
Running just the git
commands produces this output:
$ git describe --contains `git log --pretty=format:%H -n 1 test.txt`
v0.0.3~1
The ~1
indicates that the last change to the file is 1 commit behind the tag provided. Piping to sed
gets the tag all by itself, for completeness sake.
$ git describe --contains `git log --pretty=format:%H -n 1 test.txt` | sed 's/\(.*\)[~^].*/\1/'
v0.0.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With