privex.helpers.extras.git

Helper functions / classes for using git within a python application.

Quickstart with the aliases get_current_commit, get_current_tag and get_current_branch

To save you time, we pre-instantiate _AsyncGit at _cwd_git within the module, using the current working directory as the repo, allowing you to quickly call some of the most common Git functions without having to instantiate a Git() instance:

>>> from privex.helpers import get_current_tag, get_current_branch, get_current_commit
>>> get_current_commit()
'8418c964b35d76bcad984f5102ac605be0ae7b58'
>>> get_current_branch()
'master'
>>> get_current_tag()
'2.14.0'

Using the Git class ( _AsyncGit )

Basic usage of Git ( _AsyncGit )

For full functionality, you’ll want to import Git and create an instance.

Despite the fact that the Git class methods are all async methods, they can be called from within a synchronous app or context, and they’ll generally work as if they were synchronous functions, thanks to the awaitable_class() decorator.

>>> from privex.helpers import Git
>>> g = Git(repo='/home/user/projects/some_app')
>>> g.get_current_commit()
'e962f66650729e2f66395b45e1600f61d8461378'
>>> g.add("docs", "README.md")
>>> g.commit("1.2.3 - Added documentation and README.md")
>>> g.tag('1.2.3')
>>> g.get_current_tag()
'1.2.3'

Using Git commands which don’t have a method implemented

The magic method _AsyncGit.__getattr__() allows you to call Git sub-commands which aren’t yet implemented as a method, simply by calling the command’s name as a method on the class instance.

If the sub-command contains - ‘s (dashes) in it’s name, simply replace the dashes with underlines - __getattr__ will automatically rewrite the underlines (_) back into dashes (-).

For example, we can run git count-objects by calling the method count_objects, even though count_objects isn’t implemented as a method. Just like normal methods, we can call it synchronously from non-async functions/methods, and use async await when using it within asynchronous code:

>>> await g.count_objects()
'2061 objects, 8880 kilobytes'
>>> g.count_objects('-v')
'count: 2061

size: 8880 in-pack: 0 packs: 0 size-pack: 0 prune-packable: 0 garbage: 0 size-garbage: 0’

Functions / Classes / Attributes

Attributes

get_current_commit(*args, **kwargs)

get_current_branch(*args, **kwargs)

get_current_tag(*args, **kwargs)

_cwd_git

_cwd_git is a pre-instantiated _AsyncGit instance which uses the current working directory as the current git repo.

Classes

_AsyncGit(repo, default_version, **kwargs)

Git CLI wrapper class - methods can be used both synchronously and async via AsyncGit / Git (aliases, both are the same)