siemens / kas

Setup tool for bitbake based projects
MIT License
339 stars 145 forks source link

Support of repo checkouts created by GIT worktree command #107

Open steerpike68 opened 7 months ago

steerpike68 commented 7 months ago

When I checkout a KAS/Yocto based GIT repo branch into a working folder using the GIT worktree feature, the build fails at an early stage:

$ kas/kas-container -v --ssh-dir ${HOME}/.ssh build kas/project-latest.yml

I've replaced the name of my local worktree folder with \<worktreefolder> in the command above. This path/folder contains a .git file which is pointing to the main repo this worktree is based upon. I assume that the current kas version instead expects a .git folder.

When I instead create the local folder by cloning the repo and checkout the correct branch, the same command works fine.

jan-kiszka commented 7 months ago

Kas includes are always repo-relative, referring to the root of the repo they are in. Maybe that helps already? Otherwise, we may need some set of config files to reproduce.

steerpike68 commented 7 months ago

OK, I don't think its a problem of the config files or if includes are used. To reproduce, just take a simple kas-container based project you already have as a git repo with at least 2 different branches which I here call A and B. First clone the repo to folder /some_path/A/.git and check out branch A into A. Then cd to the folder A and start the kas-container build by the command you normally would use to build the project. That should work. Then from folder A use command "git worktree add ../B B" to create a second checkout folder B of branch B. "git worktree list" should show now: /some_path/A c-id [A] /some_path/B c-id [B] Then do "cd ../B" and use the same build command as used for folder/branch A. This should fail - even if that project doesn't use any includes at all.

jan-kiszka commented 7 months ago

Cannot confirm, even when using a kas file that comes with includes.

gmcn42 commented 7 months ago

I am having the same issue @steerpike68 describes.

I think I may have found the reason: The .git file, which is generated in using git worktree add <worktree-path> only contains a reference to the original repo as an absolute path like

gitdir: /home/myhome/original-git-repo/.git/worktrees/linked-worktree-name

which will not be available inside the kas container as it only mounts the linked worktree to /repo, not the original repo.

We're seeing the error message because then git rev-parse --show-toplevel fails (as it's not actually a working git repo inside the container) and Repo.get_root_path returns the PWD as a fallback path: https://github.com/siemens/kas/blob/66261547b75885786777a0b9c8a4400ab81d432e/kas/repos.py#L232

This will work fine in situations where kas is started from the toplevel of the repo but not when it is started from a subdirectory.

Not sure if we can work around this sensibly as we'd need some hacky additional mounts into the container to make that .git reference work.