Getting git informations

To easily log an information on code used for simulations, the git module allow to get informations on the current state of a git repository.

A small tool to get information from git. Useful to reuse it in python scripts (and get a trace of the version of the code used when doing a simulation).

Can also be used as a script passing the path as an argument. It will print the main informations.

Note

This class does nothing when run with python 2

Authors:

Léo Viallon-Galinier

utils.git.COMMAND_OUTPUT = ('commit', 'short_commit', 'author', 'author_email', 'date', 'committer', 'committer_email', 'committer_date', 'branch_raw', 'message', 'message_full')

The keys that git_infos get from git log

utils.git.current_git_repo(path=None)[source]

Get the git repo path if we are in a git repo else return None

Parameters:

path (str or path-like) – Path where to search for git repo (default is curent directory)

Returns:

Path of git repository, or None if an error occurs or not in a git repository

Return type:

str

utils.git.get_summary_git(path=None)[source]

Return a short string describing the state of git repository.

Try to use git command. If this fails, search for a .git_info file and return its content.

Parameters:

path (str or path-like) – Path where to search for git repo (if None, search in curent directory)

class utils.git.git_infos(path=None)[source]

Bases: object

Get git informations and put it in a dictionary self.dict.

Parameters:

path (str or path-like) – Path where to search for git repo (if None, search in curent directory)

To get access to all information in a dictionnary, look at the dict attribute. Information can also be get by subscripting the object directly.

All data is given as a string. A uncomplete list of available keys is available here. Parsed data includes:

  • path: it is the only one to be guaranteed to be present. If None, the git repository have not been found.

  • commit: the full commit hash

  • short_commit: the short commit hash

  • author, author_email

  • committer, committer_email, committer_date

  • date : the date, as a string but on iso format

  • branch : the branch name, if the branch label is on the current commit only

  • message : the commit header message

  • message_full : the full commit message

  • last_tag : the last tag on the history

  • clean : a boolean, True if the followed files have not been modified since last commit

Note that some of the keys may not be defined if the git repository was not found.

Example :

import os
from snowtools.utils.git import git_infos

gi = git_infos(os.environ.get('EXESURFEX'))

you can then access information :

>>> gi['commit']
98730c7c7f486dc85462b0427ff2ff9275bd5537

print all what was collected:

>>> print(gi)
path: /home/viallonl/bin/snowtools
commit: 98730c7c7f486dc85462b0427ff2ff9275bd5537
short_commit: 98730c7
author: Matthieu Lafaysse
author_email: matthieu.lafaysse@meteo.fr
date: 2021-07-08 16:53:15 +0200
committer: Matthieu Lafaysse
committer_email: matthieu.lafaysse@meteo.fr
committer_date: 2021-07-08 16:53:15 +0200
branch_raw: HEAD -> master, origin/master, origin/HEAD
message: bdpe for post-processing
message_full: bdpe for post-processing
branch: master
last_tag: s2m_reanalysis_2020.2
clean: True

or get a dictionnary for further use:

>>> gi.dict
{'path': '/home/viallonl/bin/snowtools',
 'commit': '98730c7c7f486dc85462b0427ff2ff9275bd5537',
 'short_commit': '98730c7',
 'author': 'Matthieu Lafaysse',
 'author_email': 'matthieu.lafaysse@meteo.fr',
 'date': '2021-07-08 16:53:15 +0200',
 'committer': 'Matthieu Lafaysse',
 'committer_email': 'matthieu.lafaysse@meteo.fr',
 'committer_date': '2021-07-08 16:53:15 +0200',
 'branch_raw': 'HEAD -> master, origin/master, origin/HEAD',
 'message': 'bdpe for post-processing',
 'message_full': 'bdpe for post-processing',
 'branch': 'master',
 'last_tag': 's2m_reanalysis_2020.2',
 'clean': False}

Some very often used strings are also computed by pre-coded functions with management of lacking keys:

>>> gi.pretty_str_commit()
c46bb7a088fc182950621f6849e64d2654987325 (master) by Matthieu Lafaysse on 2021-07-20 (from s2m_reanalysis_2020.2)
>>> gi.get_commit()
c46bb7a088fc182950621f6849e64d2654987325
get_commit(short=False, default='')[source]

Return the commit number if present, and a zero-length string else (or default if specified).

Parameters:
  • short (bool) – If True, return the short commit format

  • default (str) – Default string to be returned if commit could not be read

Returns:

commit string

Return type:

str

is_clean()[source]

Check if there is modified files in the repo.

Returns:

True if the repository is clean (no modified file), else False. Could also return None in case of error in command execution or git repo not found.

Return type:

bool

last_tag()[source]

Get the last available tag from current commit

Returns:

The last available tag

Return type:

str

parse_commit_infos()[source]

Parse commit information, executing git log -1 and fill the dict containing all git informations

pretty_str_commit(short=False, default='')[source]

Return a pretty one-line string to describe the commit

Example: c46bb7a088fc182950621f6849e64d2654987325 (master) by Matthieu Lafaysse on 2021-07-20 (from s2m_reanalysis_2020.2)

Parameters:
  • short (bool) – If True, return the short commit format

  • default (str) – Default string to be returned if commit could not be read

Returns:

A pretty string describing the commit

Return type:

str