2022-11-02 07:59:25 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2022-11-02 08:50:54 +01:00
|
|
|
|
2022-11-02 07:59:25 +01:00
|
|
|
import yaml
|
2022-11-02 08:50:54 +01:00
|
|
|
|
|
|
|
from lib.binary import is_binary_or_large
|
2022-11-02 07:59:25 +01:00
|
|
|
from lib.db import DB
|
|
|
|
from lib.git import Git
|
|
|
|
from lib.obs import OBS
|
2022-11-06 09:49:52 +01:00
|
|
|
from lib.proxy_sha256 import ProxySHA256
|
2022-11-02 07:59:25 +01:00
|
|
|
from lib.tree_builder import TreeBuilder
|
2022-11-06 09:49:52 +01:00
|
|
|
from lib.user import User
|
2022-11-02 07:59:25 +01:00
|
|
|
|
2022-11-02 08:50:54 +01:00
|
|
|
|
2022-11-02 07:59:25 +01:00
|
|
|
class GitExporter:
|
2022-11-03 13:17:49 +01:00
|
|
|
def __init__(self, api_url, project, package, repodir, cachedir):
|
2022-11-06 10:57:32 +01:00
|
|
|
self.obs = OBS(api_url)
|
2022-11-02 07:59:25 +01:00
|
|
|
self.project = project
|
|
|
|
self.package = package
|
2022-11-07 19:40:19 +01:00
|
|
|
self.db = DB()
|
|
|
|
self.proxy_sha256 = ProxySHA256(self.obs, self.db)
|
2022-11-02 07:59:25 +01:00
|
|
|
self.git = Git(
|
2022-11-03 20:14:56 +01:00
|
|
|
repodir / package,
|
2022-11-02 07:59:25 +01:00
|
|
|
committer="Git OBS Bridge",
|
|
|
|
committer_email="obsbridge@suse.de",
|
2022-11-06 12:18:16 +01:00
|
|
|
)
|
|
|
|
if self.git.exists():
|
|
|
|
self.git.open()
|
|
|
|
else:
|
|
|
|
self.git.create()
|
|
|
|
self.git.add_gitea_remote(package)
|
2022-11-02 07:59:25 +01:00
|
|
|
self.state_file = os.path.join(self.git.path, ".git", "_flat_state.yaml")
|
|
|
|
self.gc_interval = 200
|
2022-11-03 13:17:49 +01:00
|
|
|
self.cachedir = cachedir
|
2022-11-02 07:59:25 +01:00
|
|
|
|
|
|
|
def set_gc_interval(self, gc):
|
|
|
|
self.gc_interval = gc
|
|
|
|
|
2022-11-06 09:49:52 +01:00
|
|
|
def check_repo_state(self, flats, branch_state):
|
2022-11-02 07:59:25 +01:00
|
|
|
state_data = dict()
|
|
|
|
if os.path.exists(self.state_file):
|
|
|
|
with open(self.state_file, "r") as f:
|
|
|
|
state_data = yaml.safe_load(f)
|
|
|
|
if type(state_data) != dict:
|
|
|
|
state_data = {}
|
|
|
|
left_to_commit = []
|
|
|
|
for flat in reversed(flats):
|
|
|
|
found_state = False
|
|
|
|
for branch in ["factory", "devel"]:
|
|
|
|
if flat.commit.dbid == state_data.get(branch):
|
|
|
|
branch_state[branch] = flat.commit
|
|
|
|
flat.commit.git_commit = self.git.branch_head(branch)
|
|
|
|
logging.debug(
|
|
|
|
f"Found {self.git.path}'s {branch} branch in state {flat}"
|
|
|
|
)
|
|
|
|
left_to_commit = []
|
|
|
|
found_state = True
|
|
|
|
if not found_state:
|
|
|
|
left_to_commit.append(flat)
|
2022-11-06 09:49:52 +01:00
|
|
|
return left_to_commit
|
|
|
|
|
|
|
|
def export_as_git(self):
|
2022-11-07 19:40:19 +01:00
|
|
|
tree = TreeBuilder(self.db).build(self.project, self.package)
|
2022-11-06 09:49:52 +01:00
|
|
|
flats = tree.as_flat_list()
|
|
|
|
|
|
|
|
branch_state = {"factory": None, "devel": None}
|
|
|
|
left_to_commit = self.check_repo_state(flats, branch_state)
|
|
|
|
|
|
|
|
if not left_to_commit:
|
|
|
|
return
|
|
|
|
|
|
|
|
logging.info(f"Commiting into {self.git.path}")
|
|
|
|
self.run_gc()
|
|
|
|
users = dict()
|
2022-11-02 07:59:25 +01:00
|
|
|
|
|
|
|
for flat in left_to_commit:
|
2022-11-06 09:49:52 +01:00
|
|
|
if flat.commit.userid not in users:
|
2022-11-07 19:40:19 +01:00
|
|
|
users[flat.commit.userid] = User.find(self.db, flat.commit.userid)
|
2022-11-06 09:49:52 +01:00
|
|
|
flat.user = users[flat.commit.userid]
|
|
|
|
self.gc_cnt -= 1
|
|
|
|
if self.gc_cnt <= 0 and self.gc_interval:
|
|
|
|
self.run_gc()
|
2022-11-02 07:59:25 +01:00
|
|
|
logging.debug(f"Committing {flat}")
|
2022-11-02 18:07:24 +01:00
|
|
|
self.commit_flat(flat, branch_state)
|
2022-11-02 07:59:25 +01:00
|
|
|
|
2022-11-06 09:49:52 +01:00
|
|
|
def run_gc(self):
|
|
|
|
self.gc_cnt = self.gc_interval
|
|
|
|
self.git.gc()
|
|
|
|
|
2022-11-07 19:40:19 +01:00
|
|
|
def is_lfs_file(self, package, filename, size):
|
|
|
|
if not is_binary_or_large(filename, size):
|
|
|
|
return False
|
|
|
|
return not self.proxy_sha256.is_text(package, filename)
|
|
|
|
|
2022-11-03 10:37:26 +01:00
|
|
|
def commit_file(self, flat, file, size, md5):
|
|
|
|
# have such files been detected as text mimetype before?
|
2022-11-07 19:40:19 +01:00
|
|
|
if self.is_lfs_file(flat.commit.package, file.name, size):
|
2022-11-03 10:37:26 +01:00
|
|
|
file_sha256 = self.proxy_sha256.get_or_put(
|
|
|
|
flat.commit.project,
|
|
|
|
flat.commit.package,
|
|
|
|
file.name,
|
|
|
|
flat.commit.expanded_srcmd5,
|
|
|
|
md5,
|
|
|
|
size,
|
|
|
|
)
|
2022-11-07 19:40:19 +01:00
|
|
|
# as it's newly registered, it might be a text file now, so double check
|
|
|
|
if not self.proxy_sha256.is_text(flat.commit.package, file.name):
|
|
|
|
self.git.add_lfs(file.name, file_sha256, size)
|
|
|
|
return
|
|
|
|
self.commit_non_lfs_file(flat, file, md5)
|
|
|
|
|
|
|
|
def commit_non_lfs_file(self, flat, file, md5):
|
|
|
|
self.obs.change_url(flat.commit.api_url)
|
|
|
|
self.obs.download(
|
|
|
|
flat.commit.project,
|
|
|
|
flat.commit.package,
|
|
|
|
file.name,
|
|
|
|
flat.commit.expanded_srcmd5,
|
|
|
|
self.git.path,
|
|
|
|
self.cachedir,
|
|
|
|
file_md5=md5,
|
|
|
|
)
|
|
|
|
self.git.add(file)
|
2022-11-03 10:37:26 +01:00
|
|
|
|
|
|
|
def branch_fits_parent1(self, flat, branch_state):
|
|
|
|
if branch_state[flat.branch] is None:
|
|
|
|
# everything fits nothing
|
|
|
|
return True
|
|
|
|
return flat.parent1 == branch_state[flat.branch]
|
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
def commit_flat(self, flat, branch_state):
|
2022-11-02 07:59:25 +01:00
|
|
|
parents = []
|
|
|
|
self.git.checkout(flat.branch)
|
2022-11-02 20:55:09 +01:00
|
|
|
|
2022-11-03 10:37:26 +01:00
|
|
|
if flat.parent1:
|
|
|
|
if not self.branch_fits_parent1(flat, branch_state):
|
2022-11-06 09:49:52 +01:00
|
|
|
logging.debug(f"Reset {flat.branch} onto {flat.parent1.short_string()}")
|
2022-11-03 15:03:41 +01:00
|
|
|
assert flat.parent1.git_commit
|
2022-11-03 10:37:26 +01:00
|
|
|
self.git.set_branch_head(flat.branch, flat.parent1.git_commit)
|
|
|
|
self.git.checkout(flat.branch)
|
|
|
|
parents.append(flat.parent1.git_commit)
|
|
|
|
if flat.parent2:
|
2022-11-03 15:03:41 +01:00
|
|
|
assert flat.parent2.git_commit
|
2022-11-03 10:37:26 +01:00
|
|
|
parents.append(flat.parent2.git_commit)
|
|
|
|
|
2022-11-04 20:55:59 +01:00
|
|
|
# create file if not existant
|
|
|
|
self.git.add_default_lfs_gitattributes(force=False)
|
2022-11-02 20:55:09 +01:00
|
|
|
|
2022-11-02 18:07:24 +01:00
|
|
|
to_download, to_delete = flat.commit.calc_delta(branch_state[flat.branch])
|
2022-11-02 07:59:25 +01:00
|
|
|
for file in to_delete:
|
|
|
|
self.git.remove(file)
|
2022-11-02 20:55:09 +01:00
|
|
|
for file, size, md5 in to_download:
|
2022-11-03 10:37:26 +01:00
|
|
|
self.commit_file(flat, file, size, md5)
|
2022-11-02 18:27:17 +01:00
|
|
|
|
2022-11-02 07:59:25 +01:00
|
|
|
commit = self.git.commit(
|
2022-11-06 09:49:52 +01:00
|
|
|
flat.user.realname,
|
|
|
|
flat.user.email,
|
2022-11-02 07:59:25 +01:00
|
|
|
flat.commit.commit_time,
|
2022-11-06 10:14:55 +01:00
|
|
|
flat.commit.git_commit_message(),
|
2022-11-02 07:59:25 +01:00
|
|
|
parents=parents,
|
|
|
|
)
|
|
|
|
flat.commit.git_commit = commit
|
|
|
|
branch_state[flat.branch] = flat.commit
|
|
|
|
with open(self.state_file, "w") as f:
|
|
|
|
data = {}
|
|
|
|
for branch in ["factory", "devel"]:
|
|
|
|
commit = branch_state[branch]
|
|
|
|
if commit:
|
|
|
|
data[branch] = commit.dbid
|
|
|
|
yaml.dump(data, f)
|