diff --git a/buildbot/changes/hgbuildbot.py b/buildbot/changes/hgbuildbot.py
index <HASH>..<HASH> 100644
--- a/buildbot/changes/hgbuildbot.py
+++ b/buildbot/changes/hgbuildbot.py
@@ -92,7 +92,7 @@ def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
     if branch is None:
         if branchtype is not None:
             if branchtype == 'dirname':
-                branch = os.path.basename(os.getcwd())
+                branch = os.path.basename(repo.root)
             if branchtype == 'inrepo':
                 branch = workingctx(repo).branch()
allow hgbuildbot to work properly with branchtype=dirname but where cwd is not the repo