project: Extract project envvar generation to GetEnvVars

Move project environment variable setup from subcmds/forall.py to a
reusable Project.GetEnvVars() helper method.

Bug: 513329573
Change-Id: I3b4b113aa5a086e5fa5eaf4461c7ce517d928610
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/583881
Tested-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Gavin Mak
2026-05-26 11:10:44 -07:00
committed by gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com
parent c883613e31
commit e0bd39c691
3 changed files with 133 additions and 21 deletions
+45 -1
View File
@@ -28,7 +28,7 @@ import sys
import tarfile
import tempfile
import time
from typing import List, NamedTuple, Optional
from typing import Dict, List, NamedTuple, Optional
import urllib.parse
from color import Coloring
@@ -651,6 +651,50 @@ class Project:
return self.relpath
return os.path.join(self.manifest.path_prefix, self.relpath)
def GetEnvVars(self, local: bool = True) -> Dict[str, str]:
"""Get project-context environment variables.
Args:
local: If True, REPO_PATH is relative to the local (sub)manifest.
If False, it is relative to the outermost manifest.
Returns:
A dictionary mapping environment variable names to their values.
Environment Variables:
See the Environment section in `repo help forall` or
`subcmds/forall.py` for details on the available variables.
Note that `forall.py` also documents some extra variables that are
specific to how the `repo forall` command iterates over projects
(e.g., `REPO_COUNT` and `REPO_I`).
"""
env = {}
def setenv(name, val):
if val is None:
val = ""
env[name] = val
setenv("REPO_PROJECT", self.name)
setenv("REPO_OUTERPATH", self.manifest.path_prefix)
setenv("REPO_INNERPATH", self.relpath)
setenv("REPO_PATH", self.RelPath(local=local))
setenv("REPO_REMOTE", self.remote.name)
try:
lrev = "" if self.manifest.IsMirror else self.GetRevisionId()
except ManifestInvalidRevisionError:
lrev = ""
setenv("REPO_LREV", lrev)
setenv("REPO_RREV", self.revisionExpr)
setenv("REPO_UPSTREAM", self.upstream)
setenv("REPO_DEST_BRANCH", self.dest_branch)
for annotation in self.annotations:
setenv(f"REPO__{annotation.name}", annotation.value)
return env
def SetRevision(self, revisionExpr, revisionId=None):
"""Set revisionId based on revision expression and id"""
self.revisionExpr = revisionExpr