git_repo¶
Git repository management utilities.
This module provides the GitRepo class, a wrapper around common git operations (clone, pull, checkout, etc.) for managing local repository checkouts programmatically.
GitRepo
¶
Wrapper for git operations on a local repository path.
Provides methods to clone, pull, checkout, and inspect a git repository at a given filesystem path. All git commands are executed via subprocess.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
The filesystem path to the git repository. |
Example
import os
import subprocess
import tempfile
from py_misc_utils import git_repo as gr
# Build a local source repo (stand-in for the remote).
src = os.path.join(tempfile.mkdtemp(), 'src')
os.makedirs(src)
subprocess.run(['git', 'init', '-q', '-b', 'main'], cwd=src, check=True)
subprocess.run(['git', 'config', 'user.email', 't@t'], cwd=src, check=True)
subprocess.run(['git', 'config', 'user.name', 'T'], cwd=src, check=True)
readme = os.path.join(src, 'README.md')
with open(readme, 'w') as f:
f.write('hello' + chr(10))
subprocess.run(['git', 'add', 'README.md'], cwd=src, check=True)
subprocess.run(['git', 'commit', '-q', '-m', 'init'], cwd=src, check=True)
grepo = gr.GitRepo(os.path.join(os.path.dirname(src), 'dst'))
grepo.clone(src)
assert grepo.repo() == src
assert grepo.current_commit() == subprocess.run(
['git', 'rev-parse', 'HEAD'], cwd=src, check=True,
capture_output=True, text=True).stdout.strip()
assert grepo.is_shallow() is False
Source code in py_misc_utils/git_repo.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
repo()
¶
Return the remote origin URL of the repository.
Returns:
| Type | Description |
|---|---|
|
The remote.origin.url config value as a string. |
Source code in py_misc_utils/git_repo.py
117 118 119 120 121 122 123 | |
clone(repo, force=False, shallow=False)
¶
Clone a repository to self.path if not already present.
If the path already exists and is a valid repo, verifies the remote matches. If force is True or shallow setting differs, purges and re-clones. Otherwise pulls changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo
|
The repository URL to clone. |
required | |
force
|
If True, remove existing directory and re-clone. Defaults to False. |
False
|
|
shallow
|
If True, perform a shallow clone (depth 1). Defaults to False. |
False
|
Source code in py_misc_utils/git_repo.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
current_commit()
¶
Return the current HEAD commit hash.
Returns:
| Type | Description |
|---|---|
|
The SHA1 hash of the current commit as a string. |
Source code in py_misc_utils/git_repo.py
161 162 163 164 165 166 167 | |
is_shallow()
¶
Check if the repository is a shallow clone.
Returns:
| Type | Description |
|---|---|
|
True if the repository is shallow, False otherwise. |
Source code in py_misc_utils/git_repo.py
169 170 171 172 173 174 175 | |
pull()
¶
Pull changes from the remote repository.
Runs 'git pull -q' in the repository path.
Source code in py_misc_utils/git_repo.py
177 178 179 180 181 182 | |
checkout(commit)
¶
Checkout a specific commit or branch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
commit
|
The commit hash or branch name to checkout. |
required |
Source code in py_misc_utils/git_repo.py
184 185 186 187 188 189 190 | |