Maggit’s documentation¶
Contents:
Installing¶
Installing from source¶
You can directly download sources and install from them
$ git clone https://gitlab.com/maggit/maggit.git
$ cd maggit
$ python3 setup.py install .
To test that everything is ok
$ pip install pytest
$ py.test
You are now ready to use Maggit. Read the Tutorial if you don’t know how.
Tutorial¶
Importing Maggit¶
This is pretty simple
import maggit
You can use the star import if you want
from maggit import *
For the rest of the tutorial, we’ll assume that you import maggit and do not use the star import.
Get a repository¶
A Repo
is one the central objects when interactig with maggit:
# Create a repository
repo = maggit.Repo()
By default, Maggit will look for a git repository in your current directory. If you want to explore a repository elsewhere, specify it:
repo = maggit.Repo("a/git/repository")
You don’t have to specify a the root directory of the repository. If the directory you specify is not a git repository, maggit will look up in the parents directories.
References¶
Once we’ve got a repository, the first thing we may want to do is list it’s references (branches, tags):
branches = repo.branches
print(repo.branches.keys())
repo.branches is a dictionnary mapping all branches of the repository
If we want manipulate the master branch, lets get it:
master = branches['master']
In the same way repo.tags is a dictionnary mapping all tags of the repository
All the references we get, whatever they are branches, lightweigh tag or real tag objects share a common api. The most interesting here, is to get the commit object pointed to by the ref:
commit = master.commit
Print the log of the 10th last commit of the master branch¶
We just have to go through the parent of the commit and print the message each time
commit = master.commit
for i in range(10):
print(commit.message)
commit = commit.parents[0]
Print the content of a file¶
There are several way of doing it.
The low level way is the following:
current = commit.tree
for part in path.split(b'/'):
current = current.entries[part]
blob = current
print(blob.content)
But you can also use a high level api
entry = maggit.Entry(commit, path)
# the entry is a intermediate object making the link between a gitobject
# and a particular commit
blob = entry.gitObject
print(blob.content)
The commit object¶
Other important object in Maggit is naturally the git objects. A git object firstly allow us to access to the content of the object itself.
The commit object is one of them. Naturally it allows us to access the commit attributes:
commit.message
commit.first_line #The first line of the message
commit.author
commit.committer
commit.parents # The parents of the commit
commit.tree # The tree object
maggit package api¶
maggit.db package¶
This module contains all the low level classs to handle a git repository.
Class defined in this module use io functions and provide a more consistant API to access content of the git repository.
Most users will not use those object directly but better use the high level API provided by the maggit package itself.
maggit.db.io package¶
This module contains all the low level functions necessary to Maggit to read, write and parse all git files.
Thoses files includes git objects, pack, packedrefs, config, index, ...
Thoses function do not provide high level API. There are mainly intended to be use by Maggit itself.
maggit.db.io.loose module¶
-
maggit.db.io.loose.
commit_parse
(content)[source]¶ Parse a commit content.
Parameters: content (bytes) – The content to parse (without header). Returns: A tuple (tree, parents, message, author, committer) where: - tree is the sha of the tree object of the commit(unhexlified bytes).
- parents is a list of sha of the parents commit.
- message is the message of the commit.
- author is the name (b’name <email> timestamp’) of the author.
- author is the name (b’name <email> timestamp’) of the committer.
Return type: bytes, list[bytes], bytes, bytes, bytes
-
maggit.db.io.loose.
object_content
(filepath)[source]¶ Return the content of a loose object.
Parameters: filepath – The path of the loose object. Returns: A tuple (type, content) where: - type is the type of the object.
- content is the content of the object (without header).
Return type: bytes, bytes
-
maggit.db.io.loose.
object_rawsha
(type_, content)[source]¶ Generate the raw content and the sha of a object content.
Parameters: - type (bytes) – The type of the object.
- content (bytes) – The content of the object (without header).
Returns: A tuple (raw, sha) where:
- raw is the full content of the object (with header).
- sha is the sha of the object.
Return type: bytes, bytes
-
maggit.db.io.loose.
object_sha
(type_, content)[source]¶ Generate the sha of a object content.
Is the bit more performant than object_rawsha(...)[1] as the raw content is not generated.
Parameters: - type (bytes) – The type of the object.
- content (bytes) – The content of the object (without header).
Returns: The sha of the object.
-
maggit.db.io.loose.
object_sha_from_raw
(raw)[source]¶ Generate the sha of a object from its content.
Parameters: raw (bytes) – The content of the object (with header) Returns: The sha of the object.
-
maggit.db.io.loose.
object_write
(filepath, content, compress_level=1)[source]¶ Correctly create the loose object file.
Parameters: - filepath – The path of the loose object to write.
- content (bytes) – The full content (with header) to write.
- compress_level (int) – The compression level to use (default=1).
-
maggit.db.io.loose.
tag_parse
(content)[source]¶ Parse a tag content.
Parameters: content (bytes) – The content to parse (without header). Returns: A tuple (object, objecttype, tag, tagger, message) where: - object is the sha of the tagged object (unhexlified bytes).
- objecttype is the type of the tagged object.
- tag is the name of the tag.
- tagger is the name (b’name <email> timestamp’) of the tagger.
- message is the message of the tag.
Return type: bytes, bytes, bytes, bytes, bytes
maggit.db.io.pack module¶
maggit.db.io.packedref module¶
-
maggit.db.io.packedref.
packed_ref_parse
(filename)[source]¶ Parse a packed ref file.
Parameters: filename (path) – The path of the packed ref. Returns: A list of (ref, sha, peeledsha) where: - ref is name of the ref
- sha is the associated sha
- peeledsha is the peeled sha associated to the ref if present. Else None.
Return type: List[Tuple[bytes, bytes, bytes]]
maggit.db.db module¶
-
class
maggit.db.db.
Gitdb
(rootdir)[source]¶ Bases:
object
The Gitdb, unify all loose/pack io function to provide a coherent access to content of a git repository.
A Gitdb handle only the reading of git objects. Not the references, remotes, ...
Parameters: rootdir (path) – The path of the objects directory (.git/objects) -
blob_content
(sha)[source]¶ Read and parse a object assuming it is a blob.
Parameters: sha – The sha of the object. Returns: The content of the blob (bytes). Raises: ValueError
– If the object is not a blob.
-
commit_content
(sha)[source]¶ Read and parse a object assuming it is a commit.
Parameters: sha – The sha of the object. Returns: A tuple (tree, parents, message, author, committer) where: - tree is the sha of the tree object of the commit(unhexlified bytes).
- parents is a list of sha of the parents commit.
- message is the message of the commit.
- author is the name (b’name <email> timestamp’) of the author.
- author is the name (b’name <email> timestamp’) of the committer.
Return type: bytes, list[bytes], bytes, bytes, bytes Raises: ValueError
– If the object is not a commit.
-
get_full_sha
(prefix)[source]¶ Return the full Sha of the prefix
Parameters: prefix (bytes) – The beginning of a sha. Returns: The corresponding (bytes). - Exemples:
>>> repo.get_full_sha(b'bf09f0a9') <Sha b'bf09f0a9...'>
Raises: :Exception
– If number of object corresponding to prefix is not equal to one.
-
get_pack
(sha)[source]¶ Get a pack containing the sha
Parameters: sha – The sha of the object Returns: class:~maggit.io.pack.GitPack containing the sha Return type: The
-
object_type
(sha)[source]¶ Return the type of the object associated to sha.
Parameters: sha – The sha of the object. Returns: The type of the object.
-
tag_content
(sha)[source]¶ Read and parse a object assuming it is a tag.
Parameters: sha – The sha of the object. Returns: A tuple (object, objecttype, tag, tagger, message) where: - object is the sha of the tagged object (unhexlified bytes).
- objecttype is the type of the tagged object.
- tag is the name of the tag.
- tagger is the name (b’name <email> timestamp’) of the tagger.
- message is the message of the tag.
Return type: bytes, bytes, bytes, bytes, bytes Raises: ValueError
– If the object is not a tag.
-
tree_content
(sha)[source]¶ Read and parse a object assuming it is a tree.
Parameters: sha – The sha of the object. Returns: A list of (path, (mode, sha)) where : - path is the name of the entry.
- mode is the git mode.
- sha is the sha of the blob/tree object.
Return type: List[Tuple[bytes, Tuple[bytes, bytes]]] Raises: ValueError
– If the object is not a tree.
-
maggit.db.repo module¶
-
class
maggit.db.repo.
Repo
(gitdir=None, disable_directoryLooking=False, bare=False)[source]¶ This is the low level Repository class.
The repo make the link between all other low level subsystems and recreate a coherent database.
Parameters: - gitdir (path) –
The directory path of the repository,
If is None, the current working directory is assumed.
- disable_directoryLooking (bool) – If True, assume that the gitdir is a valid path so do not search for a valid git repository in parents and gitdir must not be None.
- bare (bool) – Does the repository is a bare one. Only relevant if disable_directoryLooking is True. Else, the bare attribute is detected from the git repository structure.
- gitdir (path) –
-
class
maggit.
Sha
[source]¶ Bases:
bytes
A Sha is a git sha. It means that it is the identifier of a git object.
Sha are store in Maggit as a 20 bytes len.
-
hexbytes
¶ The sha as a hexlified bytes
-
hexstr
¶ The sha as a hexlified str
-
-
class
maggit.
Repo
(gitdir=None, disable_directoryLooking=False, bare=False)[source]¶ Bases:
maggit.db.repo.Repo
This is the central piece of a git repository.
-
HEAD
¶ The current checkouted branch
-
branches
¶ A dict of branches in the repo
-
get_full_sha
(prefix)[source]¶ Return the full Sha of the prefix
Parameters: prefix (bytes) – The beginning of a sha. Returns: class:~maggit.Sha corresponding. Return type: The - Exemples:
>>> repo.get_full_sha(b'bf09f0a9') <Sha b'bf09f0a9...'>
Raises: :Exception
– If number of object corresponding to prefix is not equal to one.
-
get_object
(sha)[source]¶ Get a git object for the sha.
Parameters: sha ( Sha
) – The sha of the object.Returns: class:~maggit.gitObjects.GitObject object for this sha. Return type: A Raises: :Exception
– If sha doesn’t name a object.
A dict of tags in the repo
-
-
class
maggit.
Entry
(commit, path)[source]¶ A Entry represent a entry (file or directory) at a specific time.
We can somehow see a repository as a complex 2 dimentionnal array. Commits (and so the history) are rows. Files (and Trees) are columns.
In this situations, Entry are the cells of this array.
Parameters: - commit (
maggit.Commit
) – The commit associated to the Entry - path (bytes path) – The path of the file to look at. The path must be a bytes where directory are separated by b’/’.
- Raise:
- KeyError if the path is not existing.
-
get_first_appearance
()[source]¶ Return the commit who firstly introduce the current version of the change.
Returns: class:maggit.Commit Return type: A - Exemples:
>>> first_appearance_commit = this_entry.get_first_appearance() >>> # first_appearance_commit is the first one, so previous version differs >>> parent = first_appearance_commit.parents[0] >>> assert Entry(first_appearance_commit, this_entry.path).gitObject != Entry(parent, this_entry.path).gitObject >>> # from this_commit to first_appearance_commit, there is no change >>> current = this_entry.commit >>> while current != first_appearance_commit: ... assert Entry(current, this_entry.path).gitObject == this_entry.gitObject ... current = current.parents[0]
-
parents
¶ The previous versions of the files.
Previous versions can be equal to the current one if the current commit introduce no change on this file.
The length of the parents will most of the time be 1 but may be greater in case of merge.
- commit (
-
class
maggit.
Blob
(repo, sha)[source]¶ Bases:
maggit.gitObjects.gitObject.GitObject
A blob object.
-
content
¶ bytes
This is the content of the blob.
-
-
class
maggit.
Tree
(repo, sha)[source]¶ Bases:
maggit.gitObjects.gitObject.GitObject
A blob object.
-
entries
¶ unmutable mapping
This is the entries of the tree.
-
-
class
maggit.
Commit
(repo, sha)[source]¶ Bases:
maggit.gitObjects.gitObject.GitObject
A commit object.
-
parents
¶ tuple
The parents of the commits. Most of the time, there will only one parent. In case of branch merge, there will be more than one parent.
-
The author of the commit.
timedate
When the commit was created.
-
committer_date
¶ timedate
When the commit was committed.
-
first_line
¶ str
The first line of the commit message.
-
message
¶ str
The full commit message (including the first line).
-
-
class
maggit.
Tag
(repo, sha)[source]¶ Bases:
maggit.gitObjects.gitObject.GitObject
A tag object.
-
tag
¶ str
The name of the tag.
-
tagger_date
¶ timedate
When the tag was created.
-
first_line
¶ str
The first line of the tag message.
-
message
¶ str
The full tag message (including the first line).
-