Fixups to improve the conversion process

This commit is contained in:
Dirk Müller 2024-05-17 14:41:42 +02:00
parent 5a353c98d3
commit 073550825c
No known key found for this signature in database

View File

@ -28,61 +28,68 @@ class Git:
self.path.mkdir(parents=True, exist_ok=True) self.path.mkdir(parents=True, exist_ok=True)
self.open() self.open()
def open(self): def git_run(self, args, **kwargs):
subprocess.run( """Run a git command"""
['git', 'init', '--object-format=sha256', '-b', 'factory'], if "env" in kwargs:
envs = kwargs["env"].copy()
del kwargs["env"]
else:
envs = os.environ.copy()
envs["GIT_LFS_SKIP_SMUDGE"] = "1"
envs["GIT_CONFIG_GLOBAL"] = "/dev/null"
return subprocess.run(
["git"] + args,
cwd=self.path, cwd=self.path,
check=True, check=True,
env=envs,
**kwargs,
) )
def open(self):
self.git_run(["init", "--object-format=sha256", "-b", "factory"])
def is_dirty(self): def is_dirty(self):
"""Check if there is something to commit""" """Check if there is something to commit"""
status_str = subprocess.run( status_str = self.git_run(
['git', 'status', '--porcelain=2'], ["status", "--porcelain=2"],
cwd=self.path,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
check=True ).stdout.decode("utf-8")
).stdout.decode('utf-8') return len(list(filter(None, status_str.split("\n")))) > 0
return len(list(filter(None, status_str.split('\n')))) > 0
def branches(self): def branches(self):
br=subprocess.run( br = (
['git', 'for-each-ref', '--format=%(refname:short)', 'refs/heads/'], self.git_run(
cwd=self.path, ["for-each-ref", "--format=%(refname:short)", "refs/heads/"],
check=True, stdout=subprocess.PIPE,
stdout=subprocess.PIPE )
).stdout.decode('utf-8').split() .stdout.decode("utf-8")
.split()
)
if len(br) == 0: if len(br) == 0:
br.append('factory') # unborn branch? br.append("factory") # unborn branch?
return br return br
def branch(self, branch, commit='HEAD'): def branch(self, branch, commit="HEAD"):
commit = subprocess.run( commit = (
['git', 'rev-parse', '--verify', '--end-of-options', commit + '^{commit}'], self.git_run(
cwd=self.path, ["rev-parse", "--verify", "--end-of-options", commit + "^{commit}"],
check=True, stdout=subprocess.PIPE,
stdout=subprocess.PIPE )
).stdout.decode('utf-8').strip() .stdout.decode("utf-8")
return subprocess.run(['git', 'branch', branch, commit], check=True) .strip()
)
return self.git_run(["branch", branch, commit])
def checkout(self, branch): def checkout(self, branch):
"""Checkout into the branch HEAD""" """Checkout into the branch HEAD"""
new_branch = False new_branch = False
if branch not in self.branches(): if branch not in self.branches():
subprocess.run( self.git_run(["branch", "-q", branch, "HEAD"])
['git', 'branch', '-q', branch, 'HEAD'],
cwd=self.path,
check=True
)
new_branch = True new_branch = True
else: else:
ref = f"refs/heads/{branch}" ref = f"refs/heads/{branch}"
if (self.path/'.git'/ref).exists(): if (self.path / ".git" / ref).exists():
subprocess.run( self.git_run(["switch", "-q", branch])
['git', 'checkout', '-q', branch],
cwd=self.path,
check=True
)
return new_branch return new_branch
def commit( def commit(
@ -106,87 +113,73 @@ class Git:
committer_time = committer_time if committer_time else user_time committer_time = committer_time if committer_time else user_time
if self.is_dirty(): if self.is_dirty():
subprocess.run( self.git_run(["add", "--all", "."])
["git", "add", "--all", "."],
cwd=self.path,
check=True,
)
tree_id = subprocess.run( tree_id = (
['git', 'write-tree'], self.git_run(["write-tree"], stdout=subprocess.PIPE)
cwd=self.path, .stdout.decode("utf-8")
check=True, .strip()
stdout=subprocess.PIPE )
).stdout.decode('utf-8').strip()
parent_array = [] parent_array = []
if isinstance(parents, list): if isinstance(parents, list):
for parent in filter(None, parents): for parent in filter(None, parents):
parent_array = parent_array + ['-p', parent] parent_array = parent_array + ["-p", parent]
elif isinstance(parents, str): elif isinstance(parents, str):
parents_array = ['-p', parents] parent_array = ["-p", parents]
commit_id = subprocess.run( commit_id = (
['git', 'commit-tree'] + parent_array + [tree_id], self.git_run(
cwd=self.path, ["commit-tree"] + parent_array + [tree_id],
env={ env={
"GIT_AUTHOR_NAME": user, "GIT_AUTHOR_NAME": user,
"GIT_AUTHOR_EMAIL": user_email, "GIT_AUTHOR_EMAIL": user_email,
"GIT_AUTHOR_DATE": f"{int(user_time.timestamp())} +0000", "GIT_AUTHOR_DATE": f"{int(user_time.timestamp())} +0000",
"GIT_COMMITTER_NAME": committer, "GIT_COMMITTER_NAME": committer,
"GIT_COMMITTER_EMAIL": committer_email, "GIT_COMMITTER_EMAIL": committer_email,
"GIT_COMMITTER_DATE": f"{int(committer_time.timestamp())} +0000", "GIT_COMMITTER_DATE": f"{int(committer_time.timestamp())} +0000",
}, },
input=message.encode('utf-8'), input=message.encode("utf-8"),
check=True, stdout=subprocess.PIPE,
stdout=subprocess.PIPE )
).stdout.decode('utf-8').rstrip() .stdout.decode("utf-8")
subprocess.run( .rstrip()
['git', 'reset', '--soft', commit_id],
cwd=self.path,
check=True,
) )
self.git_run(["reset", "--soft", commit_id])
return commit_id return commit_id
def branch_head(self, branch='HEAD'): def branch_head(self, branch="HEAD"):
return subprocess.run( return (
['git', 'rev-parse', '--verify', '--end-of-options', branch], self.git_run(
cwd=self.path, ["rev-parse", "--verify", "--end-of-options", branch],
check=True, stdout=subprocess.PIPE,
stdout=subprocess.PIPE )
).stdout.decode('utf-8').strip() .stdout.decode("utf-8")
.strip()
)
def set_branch_head(self, branch, commit): def set_branch_head(self, branch, commit):
return subprocess.run( return self.git_run(["update-ref", f"refs/heads/{branch}", commit])
['git', 'branch', '-f', branch, commit],
cwd=self.path,
check=True,
)
def gc(self): def gc(self):
logging.debug(f"Garbage recollect and repackage {self.path}") logging.debug(f"Garbage recollect and repackage {self.path}")
subprocess.run( self.git_run(
["git", "gc", "--auto"], ["gc", "--auto"],
cwd=self.path,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
) )
# def clean(self): # def clean(self):
# for path, _ in self.repo.status().items(): # for path, _ in self.repo.status().items():
# logging.debug(f"Cleaning {path}") # logging.debug(f"Cleaning {path}")
# try: # try:
# (self.path / path).unlink() # (self.path / path).unlink()
# self.repo.index.remove(path) # self.repo.index.remove(path)
# except Exception as e: # except Exception as e:
# logging.warning(f"Error removing file {path}: {e}") # logging.warning(f"Error removing file {path}: {e}")
def add(self, filename): def add(self, filename):
subprocess.run( self.git_run(["add", filename])
['git', 'add', filename],
cwd=self.path,
check=True,
)
def add_default_lfs_gitattributes(self, force=False): def add_default_lfs_gitattributes(self, force=False):
if not (self.path / ".gitattributes").exists() or force: if not (self.path / ".gitattributes").exists() or force:
@ -240,10 +233,8 @@ class Git:
return any(fnmatch.fnmatch(filename, line) for line in patterns) return any(fnmatch.fnmatch(filename, line) for line in patterns)
def remove(self, file: pathlib.Path): def remove(self, file: pathlib.Path):
subprocess.run( self.git_run(
['git', 'rm', '-q', '--ignore-unmatch', file.name], ["rm", "-q", "-f", "--ignore-unmatch", file.name],
cwd=self.path,
check=True,
) )
patterns = self.get_specific_lfs_gitattributes() patterns = self.get_specific_lfs_gitattributes()
if file.name in patterns: if file.name in patterns:
@ -270,22 +261,19 @@ class Git:
if response.status_code not in (201, 409): if response.status_code not in (201, 409):
print(response.data) print(response.data)
url = f"gitea@src.opensuse.org:{org_name}/{repo_name}.git" url = f"gitea@src.opensuse.org:{org_name}/{repo_name}.git"
subprocess.run( self.git_run(
['git', 'remote', 'add', 'origin', url], ["remote", "add", "origin", url],
cwd=self.path,
check=True,
) )
def push(self, force=False): def push(self, force=False):
cmd = ['git', 'push']; if "origin" not in self.git_run(["remote"]).stdout:
if force: logger.warning("Not pushing to remote because no 'origin' configured")
cmd.append('-f') return
cmd.append('origin')
cmd.append('refs/heads/factory');
cmd.append('refs/heads/devel');
subprocess.run(
cmd,
cwd=self.path,
check=True,
)
cmd = ["push"]
if force:
cmd.append("-f")
cmd.append("origin")
cmd.append("refs/heads/factory")
cmd.append("refs/heads/devel")
self.git_run(cmd)