diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-08-20 15:05:30 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-08-20 15:05:30 +0300 |
commit | bfae42c8a8dbe5570f5d530e927df2b7a071248c (patch) | |
tree | bacd3ffd60296694805d36007aab26a9b1fe32d3 | |
parent | 3b3e05cbf81fb5f10c924db1e31f361353cef251 (diff) |
Add support for wildcard project mapping specification.
For example "* = subtree/*" means "mirror all source project under
subtree/ on target (multi-component source paths are preserved of course,
for example foo/bar/baz.git becomes subtree/foo/bar/baz.git on target)".
Change-Id: Ic54605ceeae24e0add70081044e9b3cd6b7b296b
-rwxr-xr-x | git-gerrit-mirror | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/git-gerrit-mirror b/git-gerrit-mirror index 1ca07cb..4771ebf 100755 --- a/git-gerrit-mirror +++ b/git-gerrit-mirror @@ -22,9 +22,9 @@ class MirrorConfig(object): self.host_map = {} self.parse() - def store_host_exceptions(self, host, host_vars, host_exceptions): + def store_host_info(self, host, host_vars, host_proj_map, host_wildcard_map): if host and self.get_bool(host_vars.get("active", "true")): - self.host_map[host] = (host_vars, host_exceptions) + self.host_map[host] = (host_vars, host_proj_map, host_wildcard_map) def get_bool(self, val): val2 = val.lower() @@ -38,7 +38,8 @@ class MirrorConfig(object): in_file = open(self.fname) host = None host_vars = {} - host_exceptions = {} + host_proj_map = {} + host_wildcard_map = {} for l in in_file: l = l.strip() if not l: @@ -46,12 +47,13 @@ class MirrorConfig(object): if l[0] == "#": continue if l[0] == "[": - self.store_host_exceptions(host, host_vars, host_exceptions) + self.store_host_info(host, host_vars, host_proj_map, host_wildcard_map) host = l[1:-1] if not host.startswith(DEST_KEYWORD): assert "://" in host, "URL schema is required in " + l assert "/" not in host.split("://")[1], "Only hostname (no path) allowed in " + l - host_exceptions = {} + host_proj_map = {} + host_wildcard_map = {} host_vars = {} else: fields = l.split("=", 1) @@ -65,9 +67,14 @@ class MirrorConfig(object): # to the same name if len(fields) == 1: fields.append(fields[0]) - host_exceptions[fields[0]] = fields[1] + stars = fields[0].count("*") + assert stars <= 1, "Only one * allowed in pattern: " + l + if stars: + host_wildcard_map[fields[0]] = fields[1] + else: + host_proj_map[fields[0]] = fields[1] # Store last block - self.store_host_exceptions(host, host_vars, host_exceptions) + self.store_host_info(host, host_vars, host_proj_map, host_wildcard_map) in_file.close() def get_hosts(self, substr_match=""): @@ -82,7 +89,16 @@ class MirrorConfig(object): def get_mirror_repo(self, host, source_repo): """Get mirror path for a repo. By default it is equal to source path, but if exception was defined, it is different.""" - return self.host_map[host][1].get(source_repo, source_repo) + if source_repo in self.host_map[host][1]: + return self.host_map[host][1][source_repo] + for pat, repl in self.host_map[host][2].items(): + # Convert glob-style pattern to regexp, then use it + # for match and substitution + pat = pat.replace("*", "(.+)") + m = re.match(pat, source_repo) + if m: + return repl.replace("*", m.group(1)) + return source_repo def get_repo_map(self, host): "Get all mirror repo paths as dict (key - src path, value - dest path)" |