[PATCH 1/1] establish_default_repo: report non-dir top; drop stat_if_exists

0 views
Skip to first unread message

Rob Browning

unread,
Feb 22, 2026, 4:14:24 PM (4 days ago) Feb 22
to bup-...@googlegroups.com
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

Rob Browning

unread,
Feb 23, 2026, 2:30:46 PM (3 days ago) Feb 23
to bup-...@googlegroups.com
Rob Browning <r...@defaultvalue.org> writes:

> Proposed for main. (test_git.py does have some related tests)

Pushed.

--
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4
Reply all
Reply to author
Forward
0 new messages