.. _library_git:

``git``
=======

This library provides access to a git repo root, remotes, current
branch, and latest commit data (e.g., commit hash). Support for using
this library on Windows operating-systems is experimental and may or may
not work depending on the used Prolog backend.

API documentation
-----------------

Open the
`../../apis/library_index.html#git <../../apis/library_index.html#git>`__
link in a web browser.

Loading
-------

To load this library, load its ``loader.lgt`` file:

::

   | ?- logtalk_load(git(loader)).

Testing
-------

To test this library, load its ``tester.lgt`` file:

::

   | ?- logtalk_load(git(tester)).

Usage
-----

All predicates take as first argument a directory, which should be
either a git repository directory or a sub-directory of a git repository
directory. The main predicate is ``commit_log/3``. It provides access to
the ``git log`` command when called with the
``--oneline -n 1 --pretty=format:`` options. By passing as second
argument the desired format, it returns an atom with the formatted
output. For example:

::

   | ?- git::commit_log('/Users/pmoura/logtalk3', '%h%n%B', Output),
        write(Output), nl.

   eccaa1a2a
   Update SVG diagrams

   Output = 'eccaa1a2a\nUpdate SVG diagrams\n'
   yes

See e.g. the official documentation on ``git log`` pretty formats for
details:

https://git-scm.com/docs/pretty-formats

Convenient predicates are also provided for commonly used formats such
as the commit author and the commit hash. For example:

::

   | ?- git::commit_author('/Users/pmoura/logtalk3', Author).

   Author = 'Paulo Moura'
   yes

   | ?- git::commit_hash('/Users/pmoura/logtalk3', Hash).

   Hash = eccaa1a2a9495fef441915bbace84e0a4b0394a2
   yes

The ``commit_hash/2`` predicate returns the HEAD commit hash, regardless
of local changes. The ``working_tree_clean/1`` predicate succeeds when
the repository has no staged or unstaged changes to tracked files,
including submodule changes. It checks the whole repository even when
given a sub-directory, ignores untracked files (also within submodules),
and fails if the Git status command fails. The ``working_tree_status/2``
predicate returns the underlying porcelain version 1 status output for
tracked changes as an atom, including submodule changes.

To get the name of the current local branch:

::

   | ?- git::branch('/Users/pmoura/logtalk3', Branch).

   Branch = master
   yes

Or the ``origin`` remote URI:

::

   | ?- git::remote('/Users/pmoura/logtalk3', origin, URI).

   URI = 'git@github.com:LogtalkDotOrg/logtalk3.git'
   yes
