From 6cf51a798e255b971fd4895103dbba653611810d Mon Sep 17 00:00:00 2001 From: Otto Liljalaakso Date: Jan 25 2023 01:48:45 +0000 Subject: [PATCH 1/3] Switch load_branch_merge to use multiple return Previously, method load_branch_merge was written so that it exited either due to an exception, or by reaching the end. This is readable, because there are only two cases to consider: Either self.dist is defined and contained the value to store in branch_merge, or it is not, in which case the value is derived from Git repo branch setup. All other situations throw an exception. In the next commit, additional third case that can exit normally is added: The case of no Git repository, which will allow downstreams such as fedpkg to define a default branch_merge to use in this case. Detecting this situation involves handling an exception, so handling it using only a single return would produce needlessly complicated code. On the other hand, it is very easy to write when using multiple returns. Thus, change load_branch_merge to use multiple return. Signed-off-by: Otto Liljalaakso --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 0f198db..0e4cfdd 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -389,24 +389,25 @@ class Commands(object): if self.dist: self._branch_merge = self.dist - else: - try: - localbranch = self.repo.active_branch.name - except TypeError as e: - raise rpkgError('Repo in inconsistent state: %s' % e) - try: - merge = self.repo.git.config('--get', 'branch.%s.merge' % localbranch) - except git.GitCommandError: - msg = ( - 'Unable to find remote branch. Use --release\n' - 'If current branch has to track a remote branch, fix it with command:\n' - ' git branch -u origin/%s' % localbranch - ) - raise rpkgError(msg) - # Trim off the refs/heads so that we're just working with - # the branch name - merge = merge.replace('refs/heads/', '', 1) - self._branch_merge = merge + return + + try: + localbranch = self.repo.active_branch.name + except TypeError as e: + raise rpkgError('Repo in inconsistent state: %s' % e) + try: + merge = self.repo.git.config('--get', 'branch.%s.merge' % localbranch) + except git.GitCommandError: + msg = ( + 'Unable to find remote branch. Use --release\n' + 'If current branch has to track a remote branch, fix it with command:\n' + ' git branch -u origin/%s' % localbranch + ) + raise rpkgError(msg) + # Trim off the refs/heads so that we're just working with + # the branch name + merge = merge.replace('refs/heads/', '', 1) + self._branch_merge = merge @property def branch_remote(self): From c2fd01bed5bd056c3d2b47b106dccfbc5713df9e Mon Sep 17 00:00:00 2001 From: Otto Liljalaakso Date: Jan 25 2023 01:48:45 +0000 Subject: [PATCH 2/3] Allow downstreams to define a default release Previously, when not inside a Git repository, rpkg-based tools have required manually specifying --release option. This is quite unwieldy, even though such use is quite common. For example, initial work on new packages is often done without initializing local and remote Git repositories. Allow downstream tools to define a default release, to be used when there is no Git repository. In Fedora, for example, this can be defined as 'rawhide', which exactly matches the Fedora principle of making modifications in Rawhide first. Downstreams define the default branch name by overriding the default_branch_merge() method. The default implementation raises an exception, which matches the earlier behavior. Signed-off-by: Otto Liljalaakso --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 0e4cfdd..d101c2c 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -392,6 +392,12 @@ class Commands(object): return try: + repo = self.repo + except rpkgError: + self._branch_merge = self.default_branch_merge() + return + + try: localbranch = self.repo.active_branch.name except TypeError as e: raise rpkgError('Repo in inconsistent state: %s' % e) @@ -409,6 +415,11 @@ class Commands(object): merge = merge.replace('refs/heads/', '', 1) self._branch_merge = merge + def default_branch_merge(self): + """Get the default branch used when Git repository is not found.""" + + raise rpkgError('Unable to find Git repo. Use --release\n') + @property def branch_remote(self): """This property ensures the branch_remote attribute""" From f3c707656e8cf5a21267e89272d2cc7a11d6bbb2 Mon Sep 17 00:00:00 2001 From: Otto Liljalaakso Date: Jan 25 2023 01:48:45 +0000 Subject: [PATCH 3/3] Use local branch name as release when there is no remote Previously, release name was derived from the remote Git branch associated with the active branch. If the remote branch was not set, user had to manually specify the --release argument. To better support cases where a remote repository does not exist, default to local branch name in case the remote has not been defined. This is useful e.g. if a local Git repository and commits are used when creating a new, complicated package. In case the local branch name does not match any supported release, the problem is detected in method load_rpmdefines using the check that already checks if the remote branch name matches any release. Signed-off-by: Otto Liljalaakso --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index d101c2c..b22d0f0 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -404,12 +404,10 @@ class Commands(object): try: merge = self.repo.git.config('--get', 'branch.%s.merge' % localbranch) except git.GitCommandError: - msg = ( - 'Unable to find remote branch. Use --release\n' - 'If current branch has to track a remote branch, fix it with command:\n' - ' git branch -u origin/%s' % localbranch - ) - raise rpkgError(msg) + # Remote branch not defined, use the local name + self._branch_merge = localbranch + return + # Trim off the refs/heads so that we're just working with # the branch name merge = merge.replace('refs/heads/', '', 1)