Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite urls with insteadOf option #35

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ Track changes in a [repo](https://gerrit.googlesource.com/git-repo/+/master/#rep
* `jobs`: *Optional.* number of jobs to run in parallel (default: 0; based on number of CPU cores)
Reduce this if you observe network errors.

* `rewrite`: *Optional.* Any URL that starts with this value will be rewritten with the give value.
Similar to git url insteadOf option.
Example: rewrite http(s):// to git://

```yaml
rewrite:
"http://": "git://"
"https://": "git://"
```

* `check_jobs`: for check step only: number of jobs to run in parallel (default: jobs\*2,
2 if jobs is undefined).

Expand Down
8 changes: 5 additions & 3 deletions repo_resource/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ def check(instream) -> list:
config.revision,
config.name,
config.depth)
repo.init()
repo.update_manifest(jobs=check_jobs)
version = repo.currentVersion()
version = repo \
.set_rewrite(config.rewrite) \
.init() \
.update_manifest(jobs=check_jobs) \
.currentVersion()
except Exception as e:
raise e
finally:
Expand Down
24 changes: 24 additions & 0 deletions repo_resource/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class SourceConfiguration(NamedTuple):
depth: int = -1
jobs: int = 0
check_jobs: int = DEFAULT_CHECK_JOBS
rewrite: str = None


def source_config_from_payload(payload):
Expand Down Expand Up @@ -253,6 +254,25 @@ def __get_remote_url(self, remote):
def __get_remote_revision(self, remote):
return self.__remote_revision[remote]

def set_rewrite(self, matrix: dict = None):
if matrix is None:
return self

with redirect_stdout(sys.stderr), \
tempfile.TemporaryDirectory() as tempdir:
gitrepo = git.Repo.init(tempdir)
print("Applying rewrite rules")
for from_url, to_url in matrix.items():
print('{} -> {}'.format(from_url, to_url))
gitrepo \
.config_writer(config_level='global') \
.set_value(
'url "{}"'.format(to_url),
"insteadOf", from_url
) \
.release()
return self

def init(self):
self.__change_to_workdir()
try:
Expand All @@ -276,6 +296,7 @@ def init(self):
print('Downloading manifest from {}'.format(self.__url))
repo._Main(repo_cmd)
print('repo has been initialized in {}'.format(self.__workdir))
return self

except Exception as e:
raise (e)
Expand Down Expand Up @@ -303,6 +324,7 @@ def sync(self, version: Version, jobs: int = 0):
repo._Main(repo_cmd)
if os.listdir(self.__workdir) == []:
raise Exception('Sync failed. Is manifest correct?')
return self
except Exception as e:
raise (e)
finally:
Expand All @@ -320,6 +342,7 @@ def save_manifest(self, filename):
full_path = self.__workdir / filename
print('Saving manifest to {}'.format(full_path))
self.__version.to_file(full_path)
return self

def currentVersion(self) -> Version:
return self.__version
Expand Down Expand Up @@ -424,6 +447,7 @@ def update_manifest(self, jobs):
strip_text=True
)
)
return self

except FileNotFoundError as e:
with redirect_stdout(sys.stderr):
Expand Down
7 changes: 4 additions & 3 deletions repo_resource/in_.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def in_(instream, dest_dir='.'):
try:
repo = common.Repo(config.url, config.revision,
config.name, config.depth, workdir=Path(dest_dir))
repo.init()
repo.sync(requested_version, config.jobs)
repo.update_version()
repo.set_rewrite(config.rewrite) \
.init() \
.sync(requested_version, config.jobs) \
.update_version()
except Exception as e:
raise e
finally:
Expand Down
17 changes: 17 additions & 0 deletions repo_resource/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def setUp(self):
'name': 'aosp_device_fixed.xml'
}
}
self.demo_manifests_to_rewrite_source = {
'source': {
'url':
'https://unreachable-github.com/makohoek/demo-manifests.git',
'revision': 'main',
'name': 'aosp_device_fixed.xml',
'rewrite': {
'https://unreachable-github.com':
'https://github.com'
},
},
}
self.demo_manifests_source_norev = {
'source': {
'url': 'https://github.com/makohoek/demo-manifests.git',
Expand Down Expand Up @@ -99,6 +111,11 @@ def test_unreachable_manifest(self):
with self.assertRaises(repo.error.GitError):
check.check(instream)

def test_rewrite_manifest(self):
rewrite_url_data = self.demo_manifests_to_rewrite_source
instream = StringIO(json.dumps(rewrite_url_data))
check.check(instream)

def test_unknown_revision(self):
unknown_revision_data = self.aosp_platform_source
unknown_revision_data['source']['revision'] = 'unknown'
Expand Down
Loading