Skip to content

fix(target): hash plain source directories when --target-source-path is not a git repo - #576

Open
ParthMozarkar wants to merge 1 commit into
ossf:mainfrom
ParthMozarkar:fix/target-source-non-git
Open

ParthMozarkar wants to merge 1 commit into
ossf:mainfrom
ParthMozarkar:fix/target-source-non-git

Conversation

@ParthMozarkar

Copy link
Copy Markdown

Summary

Target.get_repo_hash() opened the --target-source-path directory with git.Repo(), so a plain directory (e.g. an unpacked release tarball) crashed build-target and run with InvalidGitRepositoryError, and a freshly git init-ed repo with no commits crashed with ValueError.

When the path isn't a git work tree, or HEAD has no commit, this now falls back to hashing the directory contents (relative paths, file modes and bytes, skipping .git) and prints a warning. The hashing is moved into a shared _hash_directory_contents() helper that __get_proj_hash() also uses; its output is unchanged. Git repositories keep their commit-based hash, so existing image tags and build caches stay valid. A missing path still raises.

User Impact

  • User-facing change
  • Internal-only change

--target-source-path now accepts plain source directories and uncommitted repos. docs/config/target-project.md documents how the target image tag is derived. No config or CLI changes; no migration needed.

Release Note / Changelog

  • I updated CHANGELOG.md ([Unreleased]) for user-facing changes
  • No changelog entry needed (internal-only refactor/test/chore)

Validation

Added oss_crs/tests/unit/test_target_repo_hash.py (16 tests; 11 failed before the fix):

  • plain directory: no crash, deterministic, hash changes on content edit, added file, rename, and file mode change
  • git repo with no commits: no crash, .git contents ignored
  • subdirectory of a git repo: no crash
  • missing path still raises NoSuchPathError
  • warning printed for the fallback, not for real git repos
  • clean repo still uses the commit hash; dirty repo still differs from it
  • project hash (no --target-source-path) pinned to its pre-change value

uv run verify: all checks pass (ruff check, ruff format, pyright, unit tests, libCRS unit tests).

Checklist

  • I followed Conventional Commits
  • I updated docs for behavior/config/CLI changes
  • I added/updated tests for behavior changes
  • I considered backward compatibility and migration impact

Closes #567

…is not a git repo

Target.get_repo_hash() opened the user-provided source path with
git.Repo(), so a plain directory (for example an unpacked release tarball)
crashed build-target and run with InvalidGitRepositoryError, and a freshly
initialized repository with no commits crashed with ValueError.

When the path is not a git work tree, or HEAD has no commit, fall back to
hashing the directory contents (relative paths, file modes and bytes,
skipping .git) and print a warning. The hashing is shared with the
existing project-directory hash, whose output is unchanged. Git
repositories keep their commit-based hashes, so existing image tags and
build caches stay valid. A missing path still raises.

Document how the target image tag is derived in target-project.md.

Closes ossf#567

Signed-off-by: ParthMozarkar <greatparth21@gmail.com>
@ParthMozarkar

Copy link
Copy Markdown
Author

@azchin could you take a look when you have time? Happy to switch to a clear error instead if you'd rather require git for this flag.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--target-source-path crashes with InvalidGitRepositoryError when the path is not a git repository

1 participant