git-well 0.2.3__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- git_well-0.3.1/MANIFEST.in +1 -0
- git_well-0.3.1/PKG-INFO +53 -0
- {git_well-0.2.3 → git_well-0.3.1}/README.rst +135 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well/__init__.py +3 -1
- {git_well-0.2.3 → git_well-0.3.1}/git_well/__main__.py +3 -0
- git_well-0.3.1/git_well/_utils.py +317 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well/demo/__init__.py +39 -10
- git_well-0.3.1/git_well/git_archive_source.py +774 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_autoconf_gpgsign.py +122 -41
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_branch_cleanup.py +37 -13
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_branch_upgrade.py +26 -10
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_discover_remote.py +92 -36
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_rebase_add_continue.py +67 -27
- git_well-0.3.1/git_well/git_remote_protocol.py +167 -0
- git_well-0.3.1/git_well/git_squash.py +145 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_squash_streaks.py +349 -239
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_stats.py +45 -9
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_sync.py +98 -43
- {git_well-0.2.3 → git_well-0.3.1}/git_well/git_track_upstream.py +38 -13
- git_well-0.3.1/git_well/git_url_components.py +83 -0
- git_well-0.3.1/git_well/ipfs.py +1283 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well/main.py +9 -2
- git_well-0.3.1/git_well/patchdir/__init__.py +1 -0
- git_well-0.3.1/git_well/patchdir/git_patchdir_apply.py +162 -0
- git_well-0.3.1/git_well/patchdir/git_patchdir_save.py +101 -0
- git_well-0.3.1/git_well/patchdir/patchdir_modal.py +16 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well/repo.py +27 -14
- git_well-0.3.1/git_well.egg-info/PKG-INFO +53 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well.egg-info/SOURCES.txt +15 -16
- {git_well-0.2.3 → git_well-0.3.1}/git_well.egg-info/entry_points.txt +2 -0
- git_well-0.3.1/git_well.egg-info/requires.txt +71 -0
- git_well-0.3.1/pyproject.toml +188 -0
- git_well-0.3.1/requirements/docs.txt +8 -0
- git_well-0.3.1/requirements/optional.txt +7 -0
- git_well-0.3.1/requirements/runtime.txt +14 -0
- git_well-0.3.1/requirements/tests.txt +14 -0
- git_well-0.3.1/tests/test_cli.py +164 -0
- git_well-0.3.1/tests/test_import.py +2 -0
- git_well-0.3.1/tests/test_ipfs.py +333 -0
- git_well-0.2.3/PKG-INFO +0 -338
- git_well-0.2.3/git_well/__main__.pyi +0 -1
- git_well-0.2.3/git_well/_utils.py +0 -115
- git_well-0.2.3/git_well/_utils.pyi +0 -18
- git_well-0.2.3/git_well/git_autoconf_gpgsign.pyi +0 -33
- git_well-0.2.3/git_well/git_branch_cleanup.pyi +0 -18
- git_well-0.2.3/git_well/git_branch_upgrade.pyi +0 -19
- git_well-0.2.3/git_well/git_discover_remote.pyi +0 -25
- git_well-0.2.3/git_well/git_rebase_add_continue.pyi +0 -20
- git_well-0.2.3/git_well/git_remote_protocol.py +0 -245
- git_well-0.2.3/git_well/git_remote_protocol.pyi +0 -38
- git_well-0.2.3/git_well/git_squash_streaks.pyi +0 -134
- git_well-0.2.3/git_well/git_stats.pyi +0 -22
- git_well-0.2.3/git_well/git_sync.pyi +0 -38
- git_well-0.2.3/git_well/git_track_upstream.pyi +0 -20
- git_well-0.2.3/git_well/main.pyi +0 -9
- git_well-0.2.3/git_well/repo.pyi +0 -31
- git_well-0.2.3/git_well.egg-info/PKG-INFO +0 -338
- git_well-0.2.3/git_well.egg-info/requires.txt +0 -386
- git_well-0.2.3/pyproject.toml +0 -44
- git_well-0.2.3/setup.py +0 -258
- git_well-0.2.3/tests/test_cli.py +0 -19
- git_well-0.2.3/tests/test_import.py +0 -2
- {git_well-0.2.3 → git_well-0.3.1}/git_well/py.typed +0 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well.egg-info/dependency_links.txt +0 -0
- {git_well-0.2.3 → git_well-0.3.1}/git_well.egg-info/top_level.txt +0 -0
- {git_well-0.2.3 → git_well-0.3.1}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include requirements/*.txt
|
git_well-0.3.1/PKG-INFO
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: git_well
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: The git_well module
|
|
5
|
+
Author-email: Jon Crall <erotemic@gmail.com>
|
|
6
|
+
License: Apache 2
|
|
7
|
+
Project-URL: Homepage, https://github.com/Erotemic/git_well
|
|
8
|
+
Classifier: Development Status :: 1 - Planning
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Classifier: Topic :: Utilities
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: ubelt>=1.3.3
|
|
19
|
+
Requires-Dist: scriptconfig>=0.9.1; python_version >= "3.6"
|
|
20
|
+
Requires-Dist: rich>=12.4.4; python_version >= "3.8"
|
|
21
|
+
Requires-Dist: rich>=11.2.0; python_version < "3.8" and python_version >= "3.6"
|
|
22
|
+
Requires-Dist: git-python>=1.0.0; python_version >= "2.7"
|
|
23
|
+
Requires-Dist: packaging>=21.3
|
|
24
|
+
Requires-Dist: pyyaml>=6.0
|
|
25
|
+
Requires-Dist: networkx>=2.8; python_version < "4.0" and python_version >= "3.11"
|
|
26
|
+
Requires-Dist: networkx>=2.7; python_version < "3.11" and python_version >= "3.8"
|
|
27
|
+
Requires-Dist: networkx>=2.6.2; python_version < "3.8" and python_version >= "3.7"
|
|
28
|
+
Requires-Dist: networkx<=2.5.1,>=2.2.0; python_version < "3.7.0" and python_version >= "3.6.0"
|
|
29
|
+
Provides-Extra: docs
|
|
30
|
+
Requires-Dist: sphinx>=5.0.1; extra == "docs"
|
|
31
|
+
Requires-Dist: sphinx-autobuild>=2021.3.14; extra == "docs"
|
|
32
|
+
Requires-Dist: sphinx_rtd_theme>=1.0.0; extra == "docs"
|
|
33
|
+
Requires-Dist: sphinxcontrib-napoleon>=0.7; extra == "docs"
|
|
34
|
+
Requires-Dist: sphinx-autoapi>=1.8.4; extra == "docs"
|
|
35
|
+
Requires-Dist: Pygments>=2.9.0; extra == "docs"
|
|
36
|
+
Requires-Dist: myst_parser>=0.18.0; extra == "docs"
|
|
37
|
+
Requires-Dist: sphinx-reredirects>=0.0.1; extra == "docs"
|
|
38
|
+
Provides-Extra: optional
|
|
39
|
+
Requires-Dist: argcomplete>=1.0; extra == "optional"
|
|
40
|
+
Requires-Dist: rich_argparse>=1.1.0; python_version >= "3.7" and extra == "optional"
|
|
41
|
+
Requires-Dist: fsspec; extra == "optional"
|
|
42
|
+
Requires-Dist: paramiko; extra == "optional"
|
|
43
|
+
Provides-Extra: tests
|
|
44
|
+
Requires-Dist: xdoctest>=1.1.5; extra == "tests"
|
|
45
|
+
Requires-Dist: pytest>=8.1.1; (python_version < "4.0" and python_version >= "3.13") and extra == "tests"
|
|
46
|
+
Requires-Dist: pytest>=8.1.1; python_version < "3.13" and extra == "tests"
|
|
47
|
+
Requires-Dist: pytest-cov>=3.0.0; extra == "tests"
|
|
48
|
+
Requires-Dist: pytest_timeout>=2.3.1; (python_version < "4.0" and python_version >= "3.12") and extra == "tests"
|
|
49
|
+
Requires-Dist: pytest_timeout>=1.4.2; python_version < "3.12" and extra == "tests"
|
|
50
|
+
Requires-Dist: coverage>=7.3.0; (python_version < "4.0" and python_version >= "3.12") and extra == "tests"
|
|
51
|
+
Requires-Dist: coverage>=6.1.1; (python_version < "3.12" and python_version >= "3.10") and extra == "tests"
|
|
52
|
+
Requires-Dist: coverage>=5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "tests"
|
|
53
|
+
Requires-Dist: coverage>=6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "tests"
|
|
@@ -95,6 +95,141 @@ The tools in this module are derived from:
|
|
|
95
95
|
|
|
96
96
|
|
|
97
97
|
|
|
98
|
+
Tracking large files with IPFS
|
|
99
|
+
------------------------------
|
|
100
|
+
|
|
101
|
+
``git-well`` also exposes an experimental ``git ipfs`` helper for keeping
|
|
102
|
+
large payloads out of Git while tracking reproducible IPFS CIDs in small
|
|
103
|
+
``*.ipfs`` sidecar files. There is intentionally no repository-local IPFS
|
|
104
|
+
store and no required ``git ipfs init`` step: Kubo still owns its normal
|
|
105
|
+
``$IPFS_PATH`` repository, while Git only tracks small sidecar metadata.
|
|
106
|
+
|
|
107
|
+
The minimum supported Kubo version is **0.37.0**. This is the first Kubo
|
|
108
|
+
release with ``ipfs add --pin-name``, which ``git ipfs add --name`` uses to
|
|
109
|
+
assign a human-readable name at import time.
|
|
110
|
+
|
|
111
|
+
The intended happy path is:
|
|
112
|
+
|
|
113
|
+
.. code:: bash
|
|
114
|
+
|
|
115
|
+
git ipfs doctor
|
|
116
|
+
git ipfs add data/ --name my-data
|
|
117
|
+
git commit -m "Track data with IPFS"
|
|
118
|
+
|
|
119
|
+
When you know the peer that is likely to provide the content, record it during
|
|
120
|
+
add:
|
|
121
|
+
|
|
122
|
+
.. code:: bash
|
|
123
|
+
|
|
124
|
+
git ipfs add data/ --name my-data --suggested-peers 12D3KooW...
|
|
125
|
+
|
|
126
|
+
A collaborator can then materialize the payloads with:
|
|
127
|
+
|
|
128
|
+
.. code:: bash
|
|
129
|
+
|
|
130
|
+
git clone <repo-url>
|
|
131
|
+
cd <repo>
|
|
132
|
+
git ipfs doctor
|
|
133
|
+
git ipfs pull
|
|
134
|
+
|
|
135
|
+
The sidecar stores the CID, relative path, object kind, byte size, and the
|
|
136
|
+
CID-affecting import settings. Volatile local details such as mtimes, command
|
|
137
|
+
elapsed time, and machine-specific cache state are intentionally left out of
|
|
138
|
+
the committed sidecar so repeated runs stay reviewable.
|
|
139
|
+
|
|
140
|
+
Sidecar schema v1
|
|
141
|
+
~~~~~~~~~~~~~~~~~
|
|
142
|
+
|
|
143
|
+
The ``schema_version: 1`` contract is intentionally small and YAML-first.
|
|
144
|
+
Unknown fields should be ignored by readers, and fields that are not needed to
|
|
145
|
+
materialize content should be treated as advisory. This keeps hand-written
|
|
146
|
+
sidecars easy to review and gives future versions room to add optional metadata
|
|
147
|
+
without breaking old repositories.
|
|
148
|
+
|
|
149
|
+
.. code:: yaml
|
|
150
|
+
|
|
151
|
+
schema_version: 1
|
|
152
|
+
type: ipfs-sidecar
|
|
153
|
+
cid: bafy...
|
|
154
|
+
rel_path: data
|
|
155
|
+
kind: directory
|
|
156
|
+
size_bytes: 123456
|
|
157
|
+
num_files: 42
|
|
158
|
+
pin_name: my-data
|
|
159
|
+
import:
|
|
160
|
+
recursive: true
|
|
161
|
+
cid_version: 1
|
|
162
|
+
raw_leaves: false
|
|
163
|
+
suggested_peers:
|
|
164
|
+
- 12D3KooW...
|
|
165
|
+
- /ip4/203.0.113.10/tcp/4001/p2p/12D3KooW...
|
|
166
|
+
|
|
167
|
+
Required fields for materialization are ``cid`` and ``rel_path``. The
|
|
168
|
+
``import`` mapping records options that affect CID reproducibility.
|
|
169
|
+
``pin_name``, sizes, counts, and ``suggested_peers`` are useful metadata, but a
|
|
170
|
+
reader should still be able to pull content without them.
|
|
171
|
+
|
|
172
|
+
Sidecars may include peer hints. Bare peer IDs are useful hints when routing
|
|
173
|
+
can discover addresses; full multiaddrs are more reliable when known.
|
|
174
|
+
``git ipfs pull`` makes a best-effort attempt to connect to ``suggested_peers``
|
|
175
|
+
before downloading. Peer hints can also be inspected or connected manually:
|
|
176
|
+
|
|
177
|
+
.. code:: bash
|
|
178
|
+
|
|
179
|
+
git ipfs peers
|
|
180
|
+
git ipfs peers --connect
|
|
181
|
+
|
|
182
|
+
To make the data retrievable by others, configure a Kubo remote pinning service
|
|
183
|
+
and push sidecar CIDs:
|
|
184
|
+
|
|
185
|
+
.. code:: bash
|
|
186
|
+
|
|
187
|
+
ipfs pin remote service add <service-name> <endpoint> <key>
|
|
188
|
+
git ipfs push --service <service-name>
|
|
189
|
+
|
|
190
|
+
Useful inspection commands:
|
|
191
|
+
|
|
192
|
+
.. code:: bash
|
|
193
|
+
|
|
194
|
+
git ipfs status
|
|
195
|
+
git ipfs status --full
|
|
196
|
+
git ipfs export --emit_bash
|
|
197
|
+
|
|
198
|
+
Dogfood checklist
|
|
199
|
+
~~~~~~~~~~~~~~~~~
|
|
200
|
+
|
|
201
|
+
When changing this workflow, test it on a scratch branch with a small real
|
|
202
|
+
payload before publishing the change:
|
|
203
|
+
|
|
204
|
+
.. code:: bash
|
|
205
|
+
|
|
206
|
+
git ipfs doctor
|
|
207
|
+
mkdir -p .git-well-ipfs-smoke
|
|
208
|
+
printf 'hello from git ipfs\n' > .git-well-ipfs-smoke/payload.txt
|
|
209
|
+
git ipfs add .git-well-ipfs-smoke --name git-well-ipfs-smoke
|
|
210
|
+
git status --short .gitignore .git-well-ipfs-smoke.ipfs
|
|
211
|
+
rm -rf .git-well-ipfs-smoke
|
|
212
|
+
git ipfs pull .git-well-ipfs-smoke.ipfs
|
|
213
|
+
git ipfs status .git-well-ipfs-smoke.ipfs
|
|
214
|
+
|
|
215
|
+
The helper script ``dev/ipfs_dogfood_smoke.sh`` runs the same smoke flow and
|
|
216
|
+
accepts an optional peer hint as its second argument.
|
|
217
|
+
|
|
218
|
+
Troubleshooting
|
|
219
|
+
~~~~~~~~~~~~~~~
|
|
220
|
+
|
|
221
|
+
``git ipfs doctor`` is the first command to run when retrieval is confusing.
|
|
222
|
+
Common failure modes are reported with actionable hints:
|
|
223
|
+
|
|
224
|
+
* ``ipfs`` is missing from ``PATH``: install Kubo >= 0.37.0.
|
|
225
|
+
* The Kubo repo is missing: run ``ipfs init`` for Kubo itself. This does not
|
|
226
|
+
create anything inside the Git repository.
|
|
227
|
+
* The daemon/API is offline: start ``ipfs daemon`` or check ``IPFS_PATH``.
|
|
228
|
+
* Retrieval cannot find a CID: check that someone is pinning the content, then
|
|
229
|
+
try ``git ipfs peers --connect`` or add more specific ``suggested_peers``.
|
|
230
|
+
* Named pins fail: upgrade to Kubo >= 0.37.0 or omit ``--name``.
|
|
231
|
+
|
|
232
|
+
|
|
98
233
|
Use Cases
|
|
99
234
|
---------
|
|
100
235
|
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
import os
|
|
5
|
+
import ubelt as ub
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def rich_print(*args: Any, **kwargs: Any) -> Any:
|
|
9
|
+
try:
|
|
10
|
+
from rich import print as printer
|
|
11
|
+
except Exception:
|
|
12
|
+
printer: Any = print
|
|
13
|
+
return printer(*args, **kwargs)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def find_merged_branches(repo: Any, main_branch: str = 'main') -> Any:
|
|
17
|
+
# git branch --merged main
|
|
18
|
+
# main_branch = 'main'
|
|
19
|
+
merged_branches = [
|
|
20
|
+
p.replace('*', '').strip()
|
|
21
|
+
for p in repo.git.branch(merged=main_branch).split('\n')
|
|
22
|
+
if p.strip()
|
|
23
|
+
]
|
|
24
|
+
merged_branches = ub.oset(merged_branches) - {main_branch}
|
|
25
|
+
return merged_branches
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def confirm(msg: str) -> bool:
|
|
29
|
+
try:
|
|
30
|
+
from rich import prompt
|
|
31
|
+
|
|
32
|
+
ret = prompt.Confirm.ask(msg)
|
|
33
|
+
except ImportError:
|
|
34
|
+
while True:
|
|
35
|
+
ans = input(msg + ' [y/n]')
|
|
36
|
+
if ans in {'y', 'yes'}:
|
|
37
|
+
ret = True
|
|
38
|
+
break
|
|
39
|
+
elif ans in {'n', 'no'}:
|
|
40
|
+
ret = False
|
|
41
|
+
break
|
|
42
|
+
else:
|
|
43
|
+
print('invalid response')
|
|
44
|
+
return ret
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def choice_prompt(msg: str, choices: list[str]) -> str:
|
|
48
|
+
"""
|
|
49
|
+
Ignore:
|
|
50
|
+
choice_prompt('which one?', choices=['a', 'b', 'c'])
|
|
51
|
+
"""
|
|
52
|
+
try:
|
|
53
|
+
from rich.prompt import Prompt, InvalidResponse
|
|
54
|
+
except ImportError:
|
|
55
|
+
print('Rich is required here')
|
|
56
|
+
raise
|
|
57
|
+
|
|
58
|
+
class ChoiceWithIntPrompt(Prompt):
|
|
59
|
+
"""
|
|
60
|
+
Assigns an integer to each choice.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def make_prompt(self, default: Any) -> Any:
|
|
64
|
+
prompt = self.prompt.copy()
|
|
65
|
+
prompt.end = ''
|
|
66
|
+
|
|
67
|
+
if self.show_choices and self.choices:
|
|
68
|
+
prompt.append('\n')
|
|
69
|
+
for idx, choice in enumerate(self.choices, start=1):
|
|
70
|
+
try:
|
|
71
|
+
int(choice)
|
|
72
|
+
except ValueError:
|
|
73
|
+
...
|
|
74
|
+
else:
|
|
75
|
+
raise AssertionError('choices cannot be integers')
|
|
76
|
+
|
|
77
|
+
prompt.append(f'{idx}. ', style='json.number')
|
|
78
|
+
prompt.append(f'{choice}\n', style='prompt')
|
|
79
|
+
if (
|
|
80
|
+
default != ...
|
|
81
|
+
and self.show_default
|
|
82
|
+
and isinstance(default, (str, self.response_type))
|
|
83
|
+
):
|
|
84
|
+
prompt.append(' ')
|
|
85
|
+
_default = self.render_default(default)
|
|
86
|
+
prompt.append(_default)
|
|
87
|
+
|
|
88
|
+
prompt.append(self.prompt_suffix)
|
|
89
|
+
return prompt
|
|
90
|
+
|
|
91
|
+
def process_response(self, value: str) -> str:
|
|
92
|
+
value = value.strip()
|
|
93
|
+
assert self.choices is not None
|
|
94
|
+
try:
|
|
95
|
+
return_value = self.response_type(value)
|
|
96
|
+
except ValueError:
|
|
97
|
+
raise InvalidResponse(self.validate_error_message)
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
idx = int(return_value) - 1
|
|
101
|
+
return_value = self.choices[idx]
|
|
102
|
+
except Exception:
|
|
103
|
+
...
|
|
104
|
+
|
|
105
|
+
if return_value not in self.choices:
|
|
106
|
+
raise InvalidResponse(self.illegal_choice_message)
|
|
107
|
+
|
|
108
|
+
return return_value
|
|
109
|
+
|
|
110
|
+
return ChoiceWithIntPrompt.ask(msg, choices=choices)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def find_git_root(dpath: str | os.PathLike[str]) -> ub.Path:
|
|
114
|
+
if 0:
|
|
115
|
+
# Old implementation
|
|
116
|
+
cwd = ub.Path(dpath).absolute()
|
|
117
|
+
parts = cwd.parts
|
|
118
|
+
found = None
|
|
119
|
+
for i in reversed(range(0, len(parts) + 1)):
|
|
120
|
+
p = ub.Path(*parts[0:i])
|
|
121
|
+
cand = p / '.git'
|
|
122
|
+
if cand.exists():
|
|
123
|
+
found = p
|
|
124
|
+
break
|
|
125
|
+
if found is None:
|
|
126
|
+
raise Exception('cannot find git root')
|
|
127
|
+
else:
|
|
128
|
+
# New implementation (should be more robust)
|
|
129
|
+
# allow running inside a subdir of a repo
|
|
130
|
+
info = ub.cmd('git rev-parse --show-toplevel', cwd=dpath, verbose=0)
|
|
131
|
+
if info['ret'] != 0:
|
|
132
|
+
raise RuntimeError(f'Not a git repo: {dpath}')
|
|
133
|
+
found = ub.Path(info['out'].strip())
|
|
134
|
+
return found
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class GitURL(str):
|
|
138
|
+
"""
|
|
139
|
+
Represents a url to a git repo and can parse info about / modify the
|
|
140
|
+
protocol
|
|
141
|
+
|
|
142
|
+
References:
|
|
143
|
+
https://git-scm.com/docs/git-clone#_git_urls
|
|
144
|
+
|
|
145
|
+
CommandLine:
|
|
146
|
+
xdoctest -m git_well.git_remote_protocol GitURL
|
|
147
|
+
|
|
148
|
+
Example:
|
|
149
|
+
>>> from git_well.git_remote_protocol import * # NOQA
|
|
150
|
+
>>> from git_well._utils import * # NOQA
|
|
151
|
+
>>> urls = [
|
|
152
|
+
>>> GitURL('https://foo.bar/user/repo.git'),
|
|
153
|
+
>>> GitURL('ssh://foo.bar/user/repo.git'),
|
|
154
|
+
>>> GitURL('ssh://git@foo.bar/user/repo.git'),
|
|
155
|
+
>>> GitURL('git@foo.bar:group/repo.git'),
|
|
156
|
+
>>> GitURL('host:path/to/my/repo/.git'),
|
|
157
|
+
>>> ]
|
|
158
|
+
>>> for url in urls:
|
|
159
|
+
>>> info = url.info
|
|
160
|
+
>>> print('---')
|
|
161
|
+
>>> print(f'url = {url}')
|
|
162
|
+
>>> print(ub.urepr(info))
|
|
163
|
+
>>> print('As git : ' + url.to_git())
|
|
164
|
+
>>> print('As ssh : ' + url.to_ssh())
|
|
165
|
+
>>> print('As https : ' + url.to_https())
|
|
166
|
+
>>> if info['protocol'] not in {'scp'}:
|
|
167
|
+
>>> # SCP recon is broken
|
|
168
|
+
>>> recon = url.to_protocol(info['protocol'])
|
|
169
|
+
>>> assert recon == url
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
def __init__(self, data: str) -> None:
|
|
173
|
+
# note: inheriting from str so data is handled in __new__
|
|
174
|
+
self._info: dict[str, Any] | None = None
|
|
175
|
+
|
|
176
|
+
def _parse(self) -> None:
|
|
177
|
+
import parse
|
|
178
|
+
|
|
179
|
+
parse.Parser('ssh://{user}')
|
|
180
|
+
|
|
181
|
+
def _fixup_endpoint(self, repo_endpoint: str) -> tuple[str, str]:
|
|
182
|
+
if repo_endpoint.endswith('.git'):
|
|
183
|
+
repo_name = repo_endpoint[:-4]
|
|
184
|
+
else:
|
|
185
|
+
repo_name = repo_endpoint
|
|
186
|
+
repo_endpoint = repo_name + '.git'
|
|
187
|
+
return repo_name, repo_endpoint
|
|
188
|
+
|
|
189
|
+
@property
|
|
190
|
+
def info(self) -> dict[str, Any]:
|
|
191
|
+
if self._info is None:
|
|
192
|
+
url = self
|
|
193
|
+
info = {}
|
|
194
|
+
if url.startswith('https://'):
|
|
195
|
+
parts = url.split('https://')[1].split('/', 3)
|
|
196
|
+
repo_endpoint = parts[2]
|
|
197
|
+
repo_name, repo_endpoint = self._fixup_endpoint(repo_endpoint)
|
|
198
|
+
info['host'] = parts[0]
|
|
199
|
+
info['group'] = parts[1]
|
|
200
|
+
info['repo_name'] = repo_name
|
|
201
|
+
info['repo_endpoint'] = repo_endpoint
|
|
202
|
+
info['user'] = None
|
|
203
|
+
info['protocol'] = 'https'
|
|
204
|
+
elif url.startswith('http://'):
|
|
205
|
+
# Coerce http to https
|
|
206
|
+
parts = url.split('http://')[1].split('/', 3)
|
|
207
|
+
repo_endpoint = parts[2]
|
|
208
|
+
repo_name, repo_endpoint = self._fixup_endpoint(repo_endpoint)
|
|
209
|
+
info['host'] = parts[0]
|
|
210
|
+
info['group'] = parts[1]
|
|
211
|
+
info['repo_name'] = repo_name
|
|
212
|
+
info['repo_endpoint'] = repo_endpoint
|
|
213
|
+
info['user'] = None
|
|
214
|
+
info['protocol'] = 'http'
|
|
215
|
+
elif url.startswith('git@'):
|
|
216
|
+
parts = url.split('git@')[1].split(':')
|
|
217
|
+
repo_endpoint = parts[1].split('/')[1]
|
|
218
|
+
repo_name, repo_endpoint = self._fixup_endpoint(repo_endpoint)
|
|
219
|
+
info['host'] = parts[0]
|
|
220
|
+
info['group'] = parts[1].split('/')[0]
|
|
221
|
+
info['repo_name'] = repo_name
|
|
222
|
+
info['repo_endpoint'] = repo_endpoint
|
|
223
|
+
info['user'] = 'git'
|
|
224
|
+
info['protocol'] = 'git'
|
|
225
|
+
elif url.startswith('ssh://'):
|
|
226
|
+
parts = url.split('ssh://')[1].split('/', 3)
|
|
227
|
+
user = None
|
|
228
|
+
if '@' in parts[0]:
|
|
229
|
+
user, host = parts[0].split('@')
|
|
230
|
+
else:
|
|
231
|
+
host = parts[0]
|
|
232
|
+
repo_name, repo_endpoint = self._fixup_endpoint(parts[2])
|
|
233
|
+
info['host'] = host
|
|
234
|
+
info['user'] = user
|
|
235
|
+
info['group'] = parts[1]
|
|
236
|
+
info['repo_name'] = repo_name
|
|
237
|
+
info['repo_endpoint'] = repo_endpoint
|
|
238
|
+
info['protocol'] = 'ssh'
|
|
239
|
+
elif url.endswith('/.git'):
|
|
240
|
+
# An ssh protocol to an explicit directory
|
|
241
|
+
host, rest = url.split(':', 1)
|
|
242
|
+
parts = rest.rsplit('/', 2)
|
|
243
|
+
info['host'] = host
|
|
244
|
+
info['group'] = parts[0]
|
|
245
|
+
info['repo_name'] = parts[1]
|
|
246
|
+
info['repo_endpoint'] = parts[1] + '/.git'
|
|
247
|
+
info['protocol'] = 'scp'
|
|
248
|
+
elif '//' not in url and '@' not in url:
|
|
249
|
+
parts = url.split(':')
|
|
250
|
+
repo_name, repo_endpoint = self._fixup_endpoint(
|
|
251
|
+
parts[1].split('/')[1]
|
|
252
|
+
)
|
|
253
|
+
info['host'] = parts[0]
|
|
254
|
+
info['group'] = parts[1].split('/')[0]
|
|
255
|
+
info['repo_name'] = repo_name
|
|
256
|
+
info['repo_endpoint'] = repo_endpoint
|
|
257
|
+
info['protocol'] = 'ssh'
|
|
258
|
+
else:
|
|
259
|
+
raise ValueError(url)
|
|
260
|
+
info['url'] = url
|
|
261
|
+
self._info = info
|
|
262
|
+
return self._info
|
|
263
|
+
|
|
264
|
+
def to_protocol(self, protocol: str) -> GitURL:
|
|
265
|
+
"""
|
|
266
|
+
Convert the URL to a different protocol
|
|
267
|
+
"""
|
|
268
|
+
if protocol == 'git':
|
|
269
|
+
return self.to_git()
|
|
270
|
+
elif protocol in {'ssh', 'scp'}:
|
|
271
|
+
return self.to_ssh()
|
|
272
|
+
elif protocol == 'https':
|
|
273
|
+
return self.to_https()
|
|
274
|
+
else:
|
|
275
|
+
raise KeyError(protocol)
|
|
276
|
+
|
|
277
|
+
def to_git(self) -> GitURL:
|
|
278
|
+
info = self.info
|
|
279
|
+
new_url = (
|
|
280
|
+
'git@'
|
|
281
|
+
+ info['host']
|
|
282
|
+
+ ':'
|
|
283
|
+
+ info['group']
|
|
284
|
+
+ '/'
|
|
285
|
+
+ info['repo_endpoint']
|
|
286
|
+
)
|
|
287
|
+
return self.__class__(new_url)
|
|
288
|
+
|
|
289
|
+
def to_ssh(self) -> GitURL:
|
|
290
|
+
info = self.info
|
|
291
|
+
user = info.get('user', None)
|
|
292
|
+
if user is None:
|
|
293
|
+
user_part = ''
|
|
294
|
+
else:
|
|
295
|
+
user_part = user + '@'
|
|
296
|
+
new_url = (
|
|
297
|
+
'ssh://'
|
|
298
|
+
+ user_part
|
|
299
|
+
+ info['host']
|
|
300
|
+
+ '/'
|
|
301
|
+
+ info['group']
|
|
302
|
+
+ '/'
|
|
303
|
+
+ info['repo_endpoint']
|
|
304
|
+
)
|
|
305
|
+
return self.__class__(new_url)
|
|
306
|
+
|
|
307
|
+
def to_https(self) -> GitURL:
|
|
308
|
+
info = self.info
|
|
309
|
+
new_url = (
|
|
310
|
+
'https://'
|
|
311
|
+
+ info['host']
|
|
312
|
+
+ '/'
|
|
313
|
+
+ info['group']
|
|
314
|
+
+ '/'
|
|
315
|
+
+ info['repo_endpoint']
|
|
316
|
+
)
|
|
317
|
+
return self.__class__(new_url)
|
|
@@ -1,50 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
|
|
2
4
|
def make_dummy_git_repo():
|
|
3
5
|
import ubelt as ub
|
|
6
|
+
|
|
4
7
|
repo_dpath = ub.Path.appdir('git_well', 'tests', 'dummy-git-repo')
|
|
8
|
+
|
|
9
|
+
# On windows we need to use a tempdir because it doesn't seem to let you
|
|
10
|
+
# clean up after yourself.
|
|
11
|
+
if ub.WIN32:
|
|
12
|
+
import tempfile
|
|
13
|
+
|
|
14
|
+
repo_dpath = ub.Path(tempfile.mkdtemp()) / 'dummy-git-repo'
|
|
15
|
+
|
|
16
|
+
# try:
|
|
5
17
|
repo_dpath.delete().ensuredir()
|
|
18
|
+
# except PermissionError:
|
|
19
|
+
# if ub.WIN32:
|
|
20
|
+
# # On windows we might run into this on the CI server. The working
|
|
21
|
+
# # hypothesis for what could be causing it is that another git
|
|
22
|
+
# # instance might have a lock on data in the .git directory.
|
|
23
|
+
# # To work around this we are going to garbage collect to hopefully
|
|
24
|
+
# # cause any lock to be released, wait, and then try again.
|
|
25
|
+
# # another git instance might have a lock on a file.
|
|
26
|
+
# import gc
|
|
27
|
+
# import time
|
|
28
|
+
# gc.collect()
|
|
29
|
+
# time.sleep(1)
|
|
30
|
+
# repo_dpath.delete().ensuredir()
|
|
31
|
+
# else:
|
|
32
|
+
# raise
|
|
6
33
|
|
|
7
34
|
import git
|
|
35
|
+
|
|
8
36
|
repo = git.Repo.init(repo_dpath, initial_branch='main')
|
|
9
37
|
|
|
10
38
|
repo.git.config('user.email', 'demo.user@zombo.com', local=True)
|
|
11
39
|
repo.git.config('user.name', 'Demo User', local=True)
|
|
12
40
|
|
|
13
|
-
fpath =
|
|
41
|
+
fpath = repo_dpath / 'data1.txt'
|
|
14
42
|
fpath.write_text('data')
|
|
15
43
|
|
|
16
44
|
repo.git.add(fpath)
|
|
17
|
-
repo.git.commit(a=True, m=
|
|
45
|
+
repo.git.commit(a=True, m='Add Data1')
|
|
18
46
|
|
|
19
47
|
for idx in range(5):
|
|
20
48
|
fpath.write_text(f'data{idx}')
|
|
21
|
-
repo.git.commit(a=True, m=
|
|
49
|
+
repo.git.commit(a=True, m='wip')
|
|
22
50
|
|
|
23
51
|
repo.git.checkout(b='branch1')
|
|
24
|
-
fpath =
|
|
52
|
+
fpath = repo_dpath / 'data2.txt'
|
|
25
53
|
for idx in range(5):
|
|
26
54
|
fpath.write_text(f'data{idx}')
|
|
27
55
|
repo.git.add(fpath)
|
|
28
|
-
repo.git.commit(a=True, m=
|
|
56
|
+
repo.git.commit(a=True, m='wip')
|
|
29
57
|
|
|
30
58
|
repo.git.checkout('main')
|
|
31
59
|
|
|
32
60
|
repo.git.checkout(b='branch2')
|
|
33
|
-
fpath =
|
|
61
|
+
fpath = repo_dpath / 'data3.txt'
|
|
34
62
|
for idx in range(5):
|
|
35
63
|
fpath.write_text(f'data{idx}')
|
|
36
64
|
repo.git.add(fpath)
|
|
37
|
-
repo.git.commit(a=True, m=
|
|
65
|
+
repo.git.commit(a=True, m='wip')
|
|
38
66
|
|
|
39
67
|
repo.git.checkout('main')
|
|
40
68
|
repo.git.merge('branch1')
|
|
41
69
|
repo.git.merge('branch2')
|
|
42
70
|
|
|
43
|
-
fpath =
|
|
71
|
+
fpath = repo_dpath / 'data3.txt'
|
|
44
72
|
for idx in range(10):
|
|
45
73
|
fpath.write_text(f'data{idx}')
|
|
46
74
|
repo.git.add(fpath)
|
|
47
|
-
repo.git.commit(a=True, m=
|
|
75
|
+
repo.git.commit(a=True, m='wip')
|
|
48
76
|
|
|
49
77
|
return repo_dpath
|
|
50
78
|
|
|
@@ -52,6 +80,7 @@ def make_dummy_git_repo():
|
|
|
52
80
|
def make_dummy_git_repo_with_orphans():
|
|
53
81
|
import ubelt as ub
|
|
54
82
|
import git
|
|
83
|
+
|
|
55
84
|
repo_dpath = ub.Path.appdir('git_well', 'tests', 'dummy-git-orphan-repo')
|
|
56
85
|
repo_dpath.delete().ensuredir()
|
|
57
86
|
|
|
@@ -61,7 +90,7 @@ def make_dummy_git_repo_with_orphans():
|
|
|
61
90
|
repo.git.config('user.name', 'Demo User', local=True)
|
|
62
91
|
|
|
63
92
|
# Write a starting commit
|
|
64
|
-
fpath =
|
|
93
|
+
fpath = repo_dpath / 'data1.txt'
|
|
65
94
|
fpath.write_text('data-base')
|
|
66
95
|
repo.git.add(fpath)
|
|
67
96
|
repo.git.commit(a=True, m='base commit')
|