Previously if the repository path wasn't directory, we would claim
that it didn't exist.
error: repository b'/home/rlb/.bup/' does not exist (see "bup help init")
Now that's:
error: /home/rlb/.bup/ is not a repository
Since establish_default_repo is the only caller, and since the
addition of FileNotFoundError and NotADirectoryError has made its work
easy, remove stat_if_exists.
Signed-off-by: Rob Browning <
r...@defaultvalue.org>
Tested-by: Rob Browning <
r...@defaultvalue.org>
---
Proposed for main. (test_git.py does have some related tests)
lib/bup/git.py | 34 ++++++++++++++++++++--------------
lib/bup/helpers.py | 11 -----------
2 files changed, 20 insertions(+), 25 deletions(-)
diff --git a/lib/bup/git.py b/lib/bup/git.py
index c517a66e..38ba76e0 100644
--- a/lib/bup/git.py
+++ b/lib/bup/git.py
@@ -1254,24 +1254,30 @@ def establish_default_repo(path=None, *, must_exist=False):
"""
global repodir
+ def maybe_exit(status, msg):
+ if must_exist:
+ log(f'error: {msg}')
+ sys.exit(status)
+ return False
repodir = path or guess_repo()
top = repo()
- pst = stat_if_exists(top + b'/objects/pack')
- if pst and stat.S_ISDIR(pst.st_mode):
- return True
- if not pst:
- top_st = stat_if_exists(top)
- if not top_st:
- if must_exist:
- log('error: repository %r does not exist (see "bup help init")\n'
- % top)
- sys.exit(15)
- return False
- if must_exist:
- log('error: %s is not a repository\n' % path_msg(top))
- sys.exit(14)
+ try:
+ if stat.S_ISDIR(os.stat(top + b'/objects/pack').st_mode):
+ return True
+ return maybe_exit(14, f'{path_msg(top)} is not a repository\n')
+ except NotADirectoryError:
+ return maybe_exit(14, f'{path_msg(top)} is not a repository\n')
+ except FileNotFoundError:
+ pass
+ try:
+ top_st = os.stat(top)
+ except NotADirectoryError:
+ return maybe_exit(15, f'{path_msg(top)} is not a repository\n')
+ except FileNotFoundError:
+ return maybe_exit(15, f'{path_msg(top)} is missing (see "bup help init")\n')
return False
+
def check_repo_or_die(path=None):
"""Equivalent to git.establish_default_repo(path, must_exist=True)."""
establish_default_repo(path, must_exist=True)
diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py
index beb7dd40..8edace9b 100644
--- a/lib/bup/helpers.py
+++ b/lib/bup/helpers.py
@@ -195,17 +195,6 @@ def lines_until_sentinel(f, sentinel, ex_type):
yield line
-def stat_if_exists(path):
- try:
- return os.stat(path)
- except NotADirectoryError:
- return None
- except OSError as e:
- if e.errno != errno.ENOENT:
- raise
- return None
-
-
def mkdirp(d, mode=None):
"""Recursively create directories on path 'd'.
--
2.47.3