Southwark
Extensions to the Dulwich Git library.
(Dulwich is located in the London Borough of Southwark)
Spun out from repo_helper. Needs more tests.
Docs |
|
---|---|
Tests |
|
PyPI |
|
Anaconda |
|
Activity |
|
QA |
|
Other |
Installation
python3 -m pip install Southwark --user
First add the required channels
conda config --add channels https://conda.anaconda.org/conda-forge
conda config --add channels https://conda.anaconda.org/domdfcoding
Then install
conda install Southwark
python3 -m pip install git+https://github.com/repo-helper/southwark@master --user
Contents
southwark
Extensions to the Dulwich Git library.
Classes:
|
Represents the output of |
The values are lists of filenames, relative to the repository root. |
Data:
Invariant |
Functions:
|
Returns |
|
Check the |
|
Clone a local or remote git repository. |
|
Returns a mapping of commit SHAs to tags. |
|
Return add/delete/modify changes to tree by comparing the index to HEAD. |
|
Returns a list of untracked files. |
|
Returns a context manager which will return |
|
Returns staged, unstaged, and untracked changes relative to the HEAD. |
Contextmanager to aid cloning on Windows during tests. |
-
namedtuple
GitStatus
(staged, unstaged, untracked)[source] Bases:
NamedTuple
Represents the output of
status()
.New in version 0.6.1.
- Fields
staged (
StagedDict
) – Dict with lists of staged paths.untracked (
List
[PathPlus
]) – List of untracked, un-ignored & non-.git paths.
-
__repr__
() Return a nicely formatted representation string
-
typeddict
StagedDict
[source] Bases:
dict
The values are lists of filenames, relative to the repository root.
New in version 0.6.1.
-
_DR
= TypeVar(_DR, bound=Repo) Type:
TypeVar
Invariant
TypeVar
bound todulwich.repo.Repo
.
-
assert_clean
(repo, allow_config=())[source] Returns
True
if the working directory is clean.If not, returns
False
and prints a helpful error message to stderr.
-
clone
(source, target=None, bare=False, checkout=None, errstream=<_io.BufferedWriter name='<stderr>'>, origin='origin', depth=None, **kwargs)[source] Clone a local or remote git repository.
- Parameters
source (
Union
[str
,bytes
]) – Path or URL for source repository.target (
Union
[str
,Path
,PathLike
,bytes
,None
]) – Path to target repository. DefaultNone
.bare (
bool
) – Whether to create a bare repository. DefaultFalse
.checkout (
Optional
[bool
]) – Whether to check-out HEAD after cloning. DefaultNone
.errstream (
IO
) – Optional stream to write progress to. Default<_io.BufferedWriter name='<stderr>'>
.origin (
Union
[str
,bytes
]) – Name of remote from the repository used to clone. Default'origin'
.
- Return type
- Returns
The cloned repository.
New in version 0.6.1.
Changed in version 0.7.2:target
now acceptsdomdf_python_tools.typing.PathLike
objects.origin
now acceptsstr
objects.
Returns a mapping of commit SHAs to tags.
-
get_tree_changes
(repo)[source] Return add/delete/modify changes to tree by comparing the index to HEAD.
- Parameters
repo (
Union
[str
,Path
,PathLike
,Repo
]) – repo path or object.- Return type
- Returns
Dictionary containing changes for each type of change.
New in version 0.6.1.
-
open_repo_closing
(path_or_repo)[source] Returns a context manager which will return
dulwich.repo.Repo
objects unchanged, but will create a newdulwich.repo.Repo
when a filesystem path is given.New in version 0.7.0.
- Parameters
path_or_repo – Either a
dulwich.repo.Repo
object or the path of a repository.- Return type
AbstractContextManager
[+T_co
]- Overloads
open_repo_closing
(path_or_repo:~_DR
) ->AbstractContextManager
[~_DR
]open_repo_closing
(path_or_repo:Union
[str
,PathLike
] ) ->AbstractContextManager
[Repo
]
southwark.click
Extensions to click.
New in version 0.5.0.
Functions:
|
Decorator to add the |
|
Decorator to add the |
southwark.config
Utilities for repository configuration.
New in version 0.5.0.
Functions:
|
Returns a dictionary mapping remote names to URLs. |
|
Set the remote url for the repository, using HTTP. |
|
Set the remote url for the repository, using SSH. |
-
get_remotes
(config)[source] Returns a dictionary mapping remote names to URLs.
- Parameters
config (
ConfigFile
)- Return type
southwark.log
Python implementation of git log
.
Classes:
|
Python implementation of |
-
class
Log
(repo='.')[source] Bases:
object
Python implementation of
git log
.Attributes:
The name of the current branch
Mapping of local branches to the SHA of the latest commit in that branch.
Mapping of git refs to commit SHAs.
Mapping of remote branches to the SHA of the latest commit in that branch.
The git repository.
Mapping of commit SHAs to tags.
Methods:
format_commit
(commit)Return a human-readable commit log entry.
log
([max_entries, reverse, from_date, …])Return the formatted commit log.
-
format_commit
(commit)[source] Return a human-readable commit log entry.
- Parameters
commit (
Commit
) – A Commit object- Return type
-
local_branches
-
Mapping of local branches to the SHA of the latest commit in that branch.
-
log
(max_entries=None, reverse=False, from_date=None, from_tag=None, colour=True)[source] Return the formatted commit log.
- Parameters
max_entries (
Optional
[int
]) – Maximum number of entries to display. Default all entries.reverse (
bool
) – Print entries in reverse order. DefaultFalse
.from_date (
Optional
[datetime
]) – Show commits after the given date. DefaultNone
.from_tag (
Optional
[str
]) – Show commits after the given tag. DefaultNone
.
- Return type
-
remote_branches
-
Mapping of remote branches to the SHA of the latest commit in that branch.
Mapping of commit SHAs to tags.
-
southwark.repo
Modified Dulwich repository object.
New in version 0.3.0.
Classes:
|
Modified Dulwich repository object. |
Data:
Invariant |
Functions:
|
Determine the identity to use for new commits. |
-
class
Repo
(root, object_store=None, bare=None)[source] Bases:
Repo
Modified Dulwich repository object.
A git repository backed by local disk.
To open an existing repository, call the constructor with the path of the repository.
To create a new repository, use the Repo.init class method.
- Parameters
root (
str
)
Methods:
do_commit
([message, committer, author, …])Create a new commit.
init
(path[, mkdir])Create a new repository.
init_bare
(path[, mkdir])Create a new bare repository.
Returns a mapping of remote names to remote URLs, for the repo’s current remotes.
reset_to
(sha)Reset the state of the repository to the given commit sha.
-
do_commit
(message=None, committer=None, author=None, commit_timestamp=None, commit_timezone=None, author_timestamp=None, author_timezone=None, tree=None, encoding=None, ref=b'HEAD', merge_heads=None)[source] Create a new commit.
If not specified, committer and author default to
get_user_identity(..., 'COMMITTER')
andget_user_identity(..., 'AUTHOR')
respectively.- Parameters
message (
Union
[str
,bytes
,None
]) – Commit message. DefaultNone
.committer (
Union
[str
,bytes
,None
]) – Committer fullname. DefaultNone
.author (
Union
[str
,bytes
,None
]) – Author fullname. DefaultNone
.commit_timestamp (
Optional
[float
]) – Commit timestamp (defaults to now). DefaultNone
.commit_timezone (
Optional
[float
]) – Commit timestamp timezone (defaults to GMT). DefaultNone
.author_timestamp (
Optional
[float
]) – Author timestamp (defaults to commit timestamp). DefaultNone
.author_timezone (
Optional
[float
]) – Author timestamp timezone (defaults to commit timestamp timezone). DefaultNone
.tree (
Optional
[Any
]) – SHA1 of the tree root to use (if not specified the current index will be committed). DefaultNone
.encoding (
Union
[str
,bytes
,None
]) – Encoding. DefaultNone
.ref (
bytes
) – Optional ref to commit to (defaults to current branch). Defaultb'HEAD'
.merge_heads (
Optional
[Any
]) – Merge heads (defaults to .git/MERGE_HEADS). DefaultNone
.
- Return type
- Returns
New commit SHA1
-
_R
= TypeVar(_R, bound=Repo) Type:
TypeVar
Invariant
TypeVar
bound tosouthwark.repo.Repo
.
-
get_user_identity
(config, kind=None)[source] Determine the identity to use for new commits.
If kind is set, this first checks
GIT_${KIND}_NAME
andGIT_${KIND}_EMAIL
.If those variables are not set, then it will fall back to reading the
user.name
anduser.email
settings from the specified configuration.If that also fails, then it will fall back to using the current users’ identity as obtained from the host system (e.g. the gecos field,
$EMAIL
,$USER@$(hostname -f)
.- Parameters
config (
StackedConfig
)kind (
Optional
[str
]) – Optional kind to return identity for, usually either'AUTHOR'
or'COMMITTER'
. DefaultNone
.
- Return type
- Returns
A user identity
southwark.targit
Archive where the changes to the contents are recorded using git.
Exceptions:
Exception to indicate an archive contains files utilising path traversal. |
Data:
Valid modes for opening |
Classes:
alias of |
|
|
A “TarGit” (pronounced “target”, /tɑːɡɪt/) is a |
|
Represents a save event in a |
Functions:
|
Checks the contents of an archive to ensure it does not contain any filenames with absolute paths or path traversal. |
-
exception
BadArchiveError
[source] Bases:
OSError
Exception to indicate an archive contains files utilising path traversal.
-
Modes
Valid modes for opening
TarGit
archives in'r'
– Read only access. The archive must exist.'w'
– Read and write access. The archive must not exist.'a'
– Read and write access to an existing archive.
Alias of
Literal
['r'
,'w'
,'a'
]
-
Status
alias of
southwark.StagedDict
-
class
TarGit
(filename, mode='r')[source] Bases:
PathLike
A “TarGit” (pronounced “target”, /tɑːɡɪt/) is a
tar.gz
archive where the changes to the contents are recorded using git.- Parameters
- Raises
FileNotFoundError – If the file is opened in read or append mode, but it does not exist.
FileExistsError – If the file is opened in write mode, but it already exists.
ValueError – If an unknown value for
mode
is given.
Methods:
save
()Saves the contents of the archive.
status
()Returns the status of the TarGit archive.
exists
()Returns whether the
TarGit
archive exists.close
()Closes the
TarGit
archive.__truediv__
(filename)Returns a
PathPlus
object representing the given filename relative to the archive root.__repr__
()Returns a string representation of the
TarGit
.Returns the filename of the
TarGit
archive.__str__
()Returns the filename of the
TarGit
archive.Attributes:
Returns whether the
TarGit
archive is closed.Returns the mode the
TarGit
archive was opened in.Returns an iterable over the historic save states of the
TarGit
.-
save
()[source] Saves the contents of the archive.
Does nothing if there are no changes to be saved.
-
status
()[source] Returns the status of the TarGit archive.
The values in the dictionary are lists of filenames, relative to the TarGit root.
- Raises
IOError – If the file is closed.
- Return type
-
property
mode
Returns the mode the
TarGit
archive was opened in.This defaults to
'r'
. After the archive is closed this will show the last mode until the archive is opened again.- Return type
Literal
['r'
,'w'
,'a'
]
-
check_archive_paths
(archive)[source] Checks the contents of an archive to ensure it does not contain any filenames with absolute paths or path traversal.
For example, the following paths would raise an
BadArchiveError
:/usr/bin/malware.sh
– this is an absolute path.~/.local/bin/malware.sh
– this tries to put the file in the user’s home directory.../.local/bin/malware.sh
– this uses path traversal to try to get to a parent directory.
See also
The warning for
tarfile.TarFile.extractall()
in the Python documentation.
-
namedtuple
SaveState
(id, user, device, time, timezone)[source] Bases:
NamedTuple
Represents a save event in a
TarGit
archive’s history.- Fields
id (
str
) – The SHA id of the underlying commit.user (
str
) – The name of the user who made the changes.device (
str
) – The hostname of the device the changes were made on.time (
float
) – The time the changes were saved, in seconds from epoch.timezone (
int
) – The timezone the changes were made in, as a GMT offset in seconds.
Downloading source code
The Southwark
source code is available on GitHub,
and can be accessed from the following URL: https://github.com/repo-helper/southwark
If you have git
installed, you can clone the repository with the following command:
git clone https://github.com/repo-helper/southwark
Cloning into 'southwark'...
remote: Enumerating objects: 47, done.
remote: Counting objects: 100% (47/47), done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 173 (delta 16), reused 17 (delta 6), pack-reused 126
Receiving objects: 100% (173/173), 126.56 KiB | 678.00 KiB/s, done.
Resolving deltas: 100% (66/66), done.

Downloading a ‘zip’ file of the source code
Building from source
The recommended way to build Southwark
is to use tox:
tox -e build
The source and wheel distributions will be in the directory dist
.
If you wish, you may also use pep517.build or another PEP 517-compatible build tool.
License
Southwark
is licensed under the MIT License
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.
Permissions | Conditions | Limitations |
---|---|---|
|
|
Copyright (c) 2020 Dominic Davis-Foster
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
View the Function Index or browse the Source Code.