gitutils.gitrepo
: Interact with git repos using system interface¶
This module provides the GitRepo
class, which provides a basic
interface to a git repository (whether working or bare). Users may
perform basic git operations such as git-push
and git-pull
using
commands like GitRepo.push()
, GitRepo.pull()
.
- class lfc._vendor.gitutils.gitrepo.GitRepo(where=None)¶
Git repository interface class
- Call:
>>> repo = GitRepo(where=None)
- Inputs:
- where: {
None
} |str
Path from which to look for git repo (default is CWD)
- where: {
- Outputs:
- repo:
GitRepo
Interface to git repository
- repo:
- Attributes:
- add(*fnames)¶
Add a file, either new or modified
Has no effect if name is unchanged since last commit
- assert_bare(cmd=None)¶
Assert that current repo is working (non-bare)
- assert_working(cmd=None)¶
Assert that current repo is working (non-bare)
- bare¶
True
|False
– Whether this instance is in a bare repository
- call(cmd, cwd=None, **kw) int ¶
Run a command with generic options
- Call:
>>> ierr = repo.call(cmd, cwd=None, **kw)
- Inputs:
- repo:
GitRepo
Interface to git repository
- cmd:
list
[str
] Command to run in list form
- cwd: {
None
} |True
|str
Location in which to run subprocess;
None
is current working directory, andTrue
is repo.gitdir- stdout: {
None
} |subprocess.PIPE
|file
Option to supress or pipe STDOUT
- stderr: {
None
} |subprocess.PIPE
|file
Option to supress or pipe STDERR
- repo:
- Outputs:
- ierr:
int
Return code from subprocess
- ierr:
- Versions:
2023-10-25
@ddalle
: v1.02024-04-23
@ddalle
: v2.0; use_call()
- call_oe(cmd, codes=None, cwd=None)¶
Run a command, capturing STDOUT and checking return code
- Call:
>>> stdout, stderr, ierr = repo.call_oe(cmd, cwd=None)
- Inputs:
- Outputs:
- Versions:
2023-01-08
@ddalle
: v1.0
- check_call(cmd, codes=None, cwd=None, **kw) int ¶
Run a command and check return code
- Call:
>>> ierr = repo.check_call(cmd, codes=None, cwd=None)
- Inputs:
- Outputs:
- ierr:
int
Return code from subprocess
- ierr:
- Versions:
2023-01-08
@ddalle
: v1.0
- check_o(cmd, codes=None, cwd=None) str ¶
Run a command, capturing STDOUT and checking return code
- Call:
>>> stdout = repo.check_o(cmd, codes=None, cwd=None)
- Inputs:
- Outputs:
- stdout:
str
Captured STDOUT from command, if any
- stdout:
- Versions:
2023-01-08
@ddalle
: v1.0
- checkout_branch(branch=None)¶
Check out a branch on a working repo
- commit(m=None, **kw)¶
Commit current changes
- connect()¶
Connect a persistent subprocess at base of repo
- Call:
>>> shell = repo.connect()
- Inputs:
- repo:
GitRepo
Interface to git repository
- repo:
- Outputs:
- shell:
shellutils.SSH
Subprocess running on either
- shell:
- Versions:
2023-10-25
@ddalle
: v1.0
- create_patch(fname: str, contents, m=None, ref='HEAD')¶
Create a patch file that changes contents of a file
- Call:
>>> repo.patch_file( fname, contents, m=None, branch=None, clean=True)
- Inputs:
- get_branch_list()¶
Get list of branches
- get_configdir()¶
Get the path to the
*.git/
folder, whereconfig
is
- get_gitdir(path: str)¶
Get absolute path to git repo root, even on bare repos
- Call:
>>> repo.get_gitdir(where=None, bare=None)
- Inputs:
- Outputs:
- gitdir:
str
Full path to top-level of working repo or git-dir of bare
- gitdir:
- Versions:
2022-12-22
@ddalle
: v1.1; support older git vers2023-08-26
@ddalle
: v2.0; move to instance method
- get_ref(ref='HEAD')¶
Get the SHA-1 hash of commit pointed to by ref
- get_tmpdir() str ¶
Create random name for a working repo if a bare repo
- Call:
>>> tmpdir = repo.get_tmpdir()
- Inputs:
- Outputs:
- tmpdir:
str
Name of folder that’s a working repo
- tmpdir:
- Attributes:
- get_working_repo(branch=None)¶
Check out a branch on a working repo
- ls_tree(*fnames, r=True, ref='HEAD')¶
List files tracked by git, even in a bare repo
Calling this function with no arguments will show all files tracked in repo.
- Call:
>>> filelist = repo.ls_tree(*fnames, r=True, ref="HEAD")
- Inputs:
- Outputs:
- Versions:
2022-12-21
@ddalle
: v1.0
- patch_file(fname: str, contents, m=None, branch=None, clean=True)¶
Commit contents to a file, whether bare or working repo
- Call:
>>> repo.patch_file( fname, contents, m=None, branch=None, clean=True)
- Inputs:
- repo:
GitRepo
Interface to git repository
- fname:
str
Name of file (relative to root of git repo) to edit
- contents:
str
|bytes
Contents to save for fname
- m: {
None
} |str
Commit message for patch; default is “Patch ‘{fname}’”
- branch: {
None
} |str
Name of branch to work in; default is HEAD
- clean: {
True
} |False
Option to delete tmp working repo if repo is bare
- repo:
- pull(remote: str, ref='HEAD')¶
Pull from a remote repo
This function does not reproduce the full functionality of the original
git-pull
command
- push(remote: str, ref='HEAD')¶
Push to a remote repo
This function does not reproduce the full functionality of the original
git-push
command
- rm(fname: str, *fnames, r=False)¶
Remove files or folders and stage deletions for git
- Call:
>>> repo.rm(fname, *fnames, r=False)
- Inputs:
- Versions:
2022-12-29
@ddalle
: v1.0
- rm_working_repo() int ¶
Remove the working repo, if it exists
- Call:
>>> repo.rm_working_repo()
- Inputs:
- repo:
GitRepo
Interface to git repository
- repo:
- shell¶
None
|gitutils._vendor.shellutils.SSH
– Optional persistent subprocess to run commands with; usually not used
- show(fname, ref='HEAD')¶
Show contents of a file, even on a bare repo
- Call:
>>> fbytes = repo.show(fname, ref="HEAD")
- Inputs:
- Outputs:
- fbytes:
bytes
Contents of fname in repository, in raw bytes
- fbytes:
- Versions:
2022-12-22
@ddalle
: v1.0
- lfc._vendor.gitutils.gitrepo.get_gitdir(where=None, bare=None)¶
Get absolute path to git repo root, even on bare repos
- Call:
>>> gitdir = get_gitdir(where=None, bare=None)
- Inputs:
- where: {
None
} |str
Working directory; can be local path or SSH path
- bare: {
None
} |True
|False
Whether repo is bare (can be detected automatically)
- where: {
- Outputs:
- gitdir:
str
Full path to top-level of working repo or git-dir of bare
- gitdir:
- Versions:
2022-12-22
@ddalle
: v1.1; support older git vers
- lfc._vendor.gitutils.gitrepo.identify_host(where=None)¶
Identify possible remote host/local path breakdown
- lfc._vendor.gitutils.gitrepo.is_bare(where=None)¶
Check if a location is in a bare git repo
- Call:
>>> q = is_bare(where=None)
- Inputs:
- where: {
None
} | ;class:str Location to check
- where: {
- Outputs:
- q:
True
|False
Whether or not where is in a bare git repo
- q:
- Versions:
2023-01-08
@ddalle
: v1.0
- lfc._vendor.gitutils.gitrepo.run_gitdir(func)¶
Decorator to run a function within the parent folder
- Call:
>>> func = run_rootdir(func)
- Wrapper Signature:
>>> v = repo.func(*a, **kw)
- Inputs:
- Versions:
2018-11-20
@ddalle
: v1.02020-02-25
@ddalle
: v1.1: better exceptions