1

From svnversion documentation:

[adrdec@opsynxvm0081 common_cpp]$ svnversion --help
usage: svnversion [OPTIONS] [WC_PATH [TRAIL_URL]]

Produce a compact 'version number' for the working copy path
WC_PATH. For example:

$ svnversion . /repos/svn/trunk
4168

The version number will be a single number if the working copy is single revision, unmodified, not switched and with an URL that matches the TRAIL_URL argument. If the working copy is unusual the version number will be more complex:

   4123:4168     mixed revision working copy
   4168M         modified working copy
   4123S         switched working copy
   4123P         partial working copy, from a sparse checkout
   4123:4168MS   mixed revision, modified, switched working copy
4
  • And people say SVN is simple .. :( Commented Aug 24, 2012 at 1:58
  • There's no direct equivalent in Git, since Git has different concepts of "modified" (ie staged and unstaged), refers to its commits differently, doesn't have a concept of "switching", etc etc. To be able to give you something useful, can you explain what you want the command for? Commented Aug 24, 2012 at 9:08
  • I need to store information in the binaries about the version and status of the code used in the compilation process. Commented Aug 26, 2012 at 23:22
  • If you are storing the version number in the binaries, then all but a simple number from svnversion are useless. As all others are impossible to reproduce from just the repository and version number. Commented Dec 31, 2016 at 18:10

2 Answers 2

3

This solution detects changes in the working directory like svnversion does.

    def get_version(self, path):
            curdir = self.get_cur_dir()
            os.chdir(path)
            version = self.execute_command("git log --pretty=format:%H -n1")[self.OUT].strip()  #get the last revision and it's comment
            status = self.execute_command("git status")[self.OUT].strip()  #get the status of the working copy
            if "modified" in status or "added" in status or "deleted" in status:
                    version += self.modified
            os.chdir(curdir)
            return version


    def execute_command(self, cmd_list):
            proc = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, shell=True)
            (out, err) = proc.communicate()
            rc = proc.returncode
            return rc, out, err   
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not super familar with SVN, but from what I can tell, SVN identifies revisions in the form of simple numbers: 1, 2, 3... Git doesn't translate so well, since it uses SSH hashes to identify revisions (known as 'commits' in the Git world). However, getting this is still pretty simple using git log:

git log --pretty="format:%h" -n1 HEAD

This prints the currently checked-out commit in the repo (which is what HEAD is). Alternatively, you can replace HEAD in the command with master (or any other branch, for that matter) to get the last commit of that branch, rather than the one that represents your working directory. Additionally, if you need the full SHA1, replace %h above with %H. You can also read the git-log manpage for more about --pretty formats.

Additionally, you could add an alias in .gitconfig to do this anywhere. Add the following lines to ~/.gitconfig (leave off [alias] if your .gitconfig already has that section, however):

[alias]
    rev = "git log --pretty='format:%h'"

Now, any time you're in a Git repo and you want to see the current revision, just type git rev.

3 Comments

Thanks, but I am after the revision, in Git it's not very useful to get the revision count. Taking your script I'd rather do: git log --pretty=oneline|head -n 1 | sed 's/\s.*//'
Does that work? There's a bit cleaner way of just getting the current commit/revision using git log --pretty="format:%h" -n1 HEAD. I'll update my answer accordingly.
yes it does. Nevertheless your solution is better, although I would use: git log --pretty="format:%H" -n1 Since you want the current full hash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.