Skip to content

Conversation

@misteriaud
Copy link
Member

@misteriaud misteriaud commented Jan 28, 2026

Summary

Fixes dda env dev start to work from git worktrees with arbitrary directory names instead of requiring the directory to be named exactly as the repository.

Problem

The command failed when run from worktrees like /path/to/datadog-agent/.worktrees/my_feature_branch with error:

Local repository not found: datadog-agent

The code assumed repository directories were named exactly as the repository (e.g., "datadog-agent"), which breaks with worktrees that have arbitrary directory names.

Solution

Added git-aware repository resolution that uses git config --get remote.origin.url to identify repositories by their remote URL rather than directory name. This works for:

  • Git worktrees (regardless of directory name)
  • Regular git repositories
  • Non-git directories (backward compatible fallback)

Testing

  • Verified with real worktree: /tmp/datadog-agent/.worktrees/test_observer_feature
  • Container successfully created and worktree directory correctly mounted

@misteriaud misteriaud requested a review from a team as a code owner January 28, 2026 14:06
Comment on lines 474 to 479
# Strategy 2: Check parent directory (existing behavior, backward compatible)
parent_repo_path = cwd.parent / repo_name
if parent_repo_path.is_dir():
if self._is_matching_repository(parent_repo_path, repo_name):
return parent_repo_path
# Fallback: If not a git repo but directory exists, use it for backward compat
return parent_repo_path
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 thought: ‏I'm not sure if we want to keep this behavior. In which case we would like to cover this ?

@misteriaud misteriaud marked this pull request as draft January 28, 2026 14:14
@misteriaud misteriaud force-pushed the fix/git-worktree-detection branch from b2c2717 to 78ce6fe Compare January 28, 2026 15:01
Previously, `dda env dev start` failed when run from git worktrees with
arbitrary directory names because it assumed repository directories were
named exactly as the repository (e.g., "datadog-agent").

This commit adds git-aware repository resolution that:
- Detects git repositories using remote URL, working for both regular
  repos and worktrees regardless of directory name
- Maintains full backward compatibility with non-git directory lookup
- Handles edge cases: missing git, no remote, nested structures

Implementation:
- Added `_resolve_repository_path()` with multi-strategy resolution
- Added `_is_matching_repository()` using git remote URL detection
- Updated repository mounting to use new git-aware resolver
- Added comprehensive tests for worktree scenarios

Fixes issue where worktrees like `.worktrees/my_feature` would fail
with "Local repository not found: datadog-agent"

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@misteriaud misteriaud force-pushed the fix/git-worktree-detection branch from 78ce6fe to 68a1c74 Compare January 28, 2026 15:17
@misteriaud misteriaud marked this pull request as ready for review January 28, 2026 15:23
When running from a git worktree, the worktree's .git file points to the
main repository's .git/worktrees/ directory. If we only mount the worktree
directory, git commands fail inside the container because the referenced
.git directory doesn't exist.

This change detects worktrees by checking if .git is a file (not a directory),
reads the gitdir path, and resolves to the main repository root. The main
repository is then mounted instead, ensuring git commands work correctly
inside the container.

Updated test to verify worktrees resolve to main repo path.
- repo_path() now detects if running from a worktree and returns the correct
  working directory path (e.g., /root/repos/repo/.worktrees/branch)
- Mount destination is always the base repo path without worktree subdirectory
- Detects external worktrees and shows helpful error message
- Working directory correctly points to worktree subdirectory when running commands
@misteriaud misteriaud marked this pull request as draft January 29, 2026 10:35
@misteriaud misteriaud changed the title Fix git worktree detection for dda env dev start [WIP] Fix git worktree detection for dda env dev start Jan 29, 2026
@misteriaud misteriaud closed this Jan 29, 2026
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.

2 participants