Is there a single Git command to get the current tag, branch and commit?

你说的曾经没有我的故事 提交于 2019-12-03 05:25:59

问题


I'm currently using a collection of three commands to get the current tag, branch and the date and SHA1 of the most recent commit.

git describe --always --tag
git log -1 --format="%H%n%aD"
git rev-parse --abbrev-ref HEAD

Which will output something like:

1.2.3-5-gdeadbeef
deadbeef3b8d90071c24f51ac8f26ce97a72727b
Wed, 19 May 2010 09:12:34 +0200
master

To be honest, I'm totally fine with this. But I'm using these commands from Maven and anyone who'd used Maven before, knows how much things like external commands bloat the POM. I just want to slim down my pom.xml and maybe reduce execution time a bit.


回答1:


  1. git log is extremely flexible, with lots and lots of options. You might not be able to reproduce the exact output of the three commands above, but you might come close enough to achieve the effect you need.

    For example:

    git log --pretty=format:'%ad %h %d' --abbrev-commit --date=short -1
    

    produces the date, SHA-1 and symbolic references (including tags) of the latest (HEAD) commit:

    2010-05-20 45bd5e7 (HEAD, origin/master)

    After which, presumably, sed and/or awk or maybe Maven-native methods can do the fine-tuning/polishing. Note that a particular tag is associated with a particular commit, so if it was three commits prior to HEAD that was tagged with, for example, "v1.0.0", you are not going to see "v1.0.0" showing up with the above.

  2. A simpler single command to provide a succint description of a commit is:

    git describe
    

    which writes out the latest applicable tag, the number of commits since the tagged commit, and the SHA1:

    v3.3.0-46-g71a77dc

  3. I am not at all familiar with Maven, and have no idea how easy/difficult it is to run external processes, so am unsure whether any of the following help in any way, but I thought I might mention it just in case.

    For the exact purpose that you describe, i.e. tagging builds, in an autoconf/automake framework, I actually use something like:

    BUILDTAG="`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`"
    

    which produces something suitable for tacking onto the end of a program path:

    master-c5282ff

    A more extended description, suitable for including as a comment or a printed identifier:

    BUILDDESC="$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)-$(git log --pretty=format:'%h, %ad' -1)"
    

    produces something like:

    master-c5282ff, Fri Mar 12 22:19:51 2010 -0600

I think playing around with git log, possibly in conjunction with text processing tools/methods will get you what you want.




回答2:


I don't use Maven, so I don't know how you are calling these commands, but adding custom commands to git is fairly trivial.

Create a script called git-tbc that looks like this:

#!/bin/bash

git describe --always --tag
git log -1 --format="%H%n%aD"
git rev-parse --abbrev-ref HEAD

Make sure git-tbc is in your PATH, you can now call "git tbc". Is this what you were looking for?




回答3:


I created a Maven plugin for exactly this purpose, which really fits my needs (in fact it exceeds them by now).

It's called Mavanagaiata, it's open-source and available from Maven Central.




回答4:


My "repo" for things like that is always bash_completion. Ok, "tab tab" is the way bash becomes a productive tool, so, where all that magic stuff come from?

there is a /etc/bash_completion.d/ directory where extensions for bash completion are left. there must be a git file executable, open it and seek for something like get_refs(). If you give it a check you will find that git describe and git for-each-ref are your friends, let's try some examples:

A common repo:

$ cd /your/git/repo; git branch -a
  master
  blaster
* brunch
  lunch
  remotes/origin/master
  remotes/origin/develop
  remotes/github/master

Which is my checked branch?

$ git describe --contains --all HEAD
brunch

What are my remotes?

$ git remote
origin
github

What are the branches on remotes?

$ git for-each-ref --format="%(refname:short)" refs/remotes
origin/master
origin/develop
github/master

What are my local branches?

$ git branch
  master
  blaster
* brunch
  lunch

...a more parseable branches output?

$ git for-each-ref --format="%(refname:short)" refs/heads
master
blaster
brunch
lunch

What about tags?

$ git for-each-ref --format="%(refname:short)" refs/heads refs/remotes refs/tags
master
blaster
brunch
lunch
origin/master
origin/develop
github/master
release-0_1
release-0_2
release-1_0

check the "man pages" for this commands, there's much more inside!.




回答5:


I have found this Maven Plugin: https://github.com/alx3apps/jgit-buildnumber, which seems a good replacement of buildnumber-maven-plugin for git projects. Furthermore it is available from Maven Central Repositories.

It works nicely in Maven 3. For Maven 2 multi-module projects, simply add the following line in the properties section of your parent pom:

<session.executionRootDirectory>${basedir}</session.executionRootDirectory>



回答6:


This displays the commit id of HEAD, as well as any branches or any tags that also happen to be exactly at HEAD.

git reflog --decorate -1

Sample output:

484c27b (HEAD, tag: deployment-2014-07-30-2359, master, origin/master) HEAD@{0}: 484c27b878ca5ab45185267f4a6b56f8f8d39892: updating HEAD


来源:https://stackoverflow.com/questions/2863756/is-there-a-single-git-command-to-get-the-current-tag-branch-and-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!