Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve reference for remote git repositories (makes fetchGit work with non-'master' branch) #4638

Merged
merged 6 commits into from May 2, 2022

Conversation

orbekk
Copy link
Contributor

@orbekk orbekk commented Mar 14, 2021

Resolves the HEAD reference from the remote repository instead of assuming "master".

For remote repositories, the HEAD ref is cached with the same cache policy as the local cached repo.

Fixes #4456.

@orbekk
Copy link
Contributor Author

orbekk commented Apr 20, 2021

Update: I've rebased the pull request and added a more explicit test in tests/fetchGit.sh.

@orbekk orbekk changed the title Resolve reference for remote git repositories Resolve reference for remote git repositories (makes fetchGit work with non-'master' branch) Apr 20, 2021
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/562

@minijackson
Copy link
Member

What is needed to get this PR merged ? I'm quite interested in this functionality since it can cause some confusion for Nix newcomers that create projects through GitHub or GitLab.

@orbekk
Copy link
Contributor Author

orbekk commented Jan 14, 2022

@minijackson If someone is willing to review this, I can make the necessary changes (if any) to bring the patches up to date. I haven't heard anything so far.

Copy link
Member

@minijackson minijackson left a comment

Choose a reason for hiding this comment

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

Great stuff! In my opinion, only the first comment about the --symref option is important, the rest are just minor comments.

src/libfetchers/git.cc Outdated Show resolved Hide resolved
{
// Append something in front of the url to prevent collision with the
// cached repo.
Path cachePath = getCacheDir() + "/nix/gitv3/" + hashString(htSHA256, "<ref>|" + actualUrl).to_string(Base32, false);
Copy link
Member

Choose a reason for hiding this comment

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

Can this be factorized with the similar line in fetch?

Copy link
Member

Choose a reason for hiding this comment

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

If I'm not mistaken, it is possible to get the remote HEAD by reading the HEAD file in the cached repository. Is it possible to do it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a helper function for cache path.

HEAD is not currently updated in the cached repo (it is initialized to __nix_dummy_branch). I think we could use it instead of the cache file since it's not used for any other purpose, but it seems more straightforward to just write it to a separate file. Let me know if you still prefer using HEAD.

Copy link
Member

Choose a reason for hiding this comment

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

I personally think that one cache dir is easier to maintain than two, but I do not feel that strongly about it. If you feel this is more straightforward, I won't bother you ^^

Copy link
Member

Choose a reason for hiding this comment

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

On a side note the getCachePath function can also be used on line 418 of git.cc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the pull request to maintain a single git repo as the cache, and storing the HEAD as a symbolic ref within the cache. This complicated the logic in fetch() slightly, so I also added a commit with some refactoring which I think makes this easier to understand overall.

src/libfetchers/git.cc Outdated Show resolved Hide resolved
@orbekk orbekk force-pushed the read_head branch 2 times, most recently from 6926bea to 1467741 Compare January 23, 2022 19:23
@minijackson
Copy link
Member

Built it with nix build -L 'github:NixOS/nix?ref=pull/4638/head' and tested it for a bit, works wonders.

@orbekk orbekk force-pushed the read_head branch 2 times, most recently from 99fabc9 to 375c48d Compare January 29, 2022 22:39
@orbekk
Copy link
Contributor Author

orbekk commented Jan 29, 2022

Built it with nix build -L 'github:NixOS/nix?ref=pull/4638/head' and tested it for a bit, works wonders.

Thank you for the review and additional testing, @minijackson.

@orbekk
Copy link
Contributor Author

orbekk commented Jan 30, 2022

Added another commit to update the documentation to account for these changes, and explains how local repositories are handled.

Fixes #5900.

Copy link
Member

@minijackson minijackson left a comment

Choose a reason for hiding this comment

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

LGTM!

All tests pass on my end.

@orbekk
Copy link
Contributor Author

orbekk commented Jan 31, 2022

Hi @edolstra. I think this pull request is in a good state and there seems to be some interest in this feature. Could you take a look?

@FintanH
Copy link

FintanH commented Mar 1, 2022

Would be good to see this PR land and #5139 resolved :) I ran into this issue while trying to introduce nix flake into a codebase but it choked on fetching a git repository that defaults to main and has no master ref.

@bjornfor
Copy link
Contributor

Needs a rebase.

@orbekk orbekk force-pushed the read_head branch 2 times, most recently from 375c48d to 1979834 Compare April 21, 2022 01:13
@orbekk
Copy link
Contributor Author

orbekk commented Apr 23, 2022

@bjornfor Thanks, I have rebased and update the pull request. Please take a look.

@bjornfor
Copy link
Contributor

@orbekk: Sorry, I was just pointing out that the PR needed a rebase. I don't have commit access nor feel qualified to review.

@orbekk
Copy link
Contributor Author

orbekk commented Apr 23, 2022

Hi @thufschmitt, could you help me find a reviewer?

Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

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

Hi @thufschmitt, could you help me find a reviewer?

I guess I just did that 😛

Looks good overall, just a couple of small suggestions

src/libfetchers/git.cc Outdated Show resolved Hide resolved
src/libfetchers/git.cc Show resolved Hide resolved
Kjetil Orbekk added 6 commits April 29, 2022 18:42
Resolves the HEAD reference from the remote repository instead
of assuming "master".
Extract the handling of a local dirty workdir to a helper function.
The previous head caching implementation stored two paths in the local
cache; one for the cached git repo and another textfile containing the
resolved HEAD ref. This commit instead stores the resolved HEAD by
setting the HEAD ref in the local cache appropriately.
Update the documentation about how `ref` is resolved if it is not
specified.

Add a note about special handling of local workdirs with `git+file`.
These utility functions can be shared between the git and github fetchers.
Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

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

Thanks, looks good :)

I’ll leave it for a couple days in case @edolstra wants to have another look since he’s the author of that part of the code, and I’ll merge it if nothing comes before that

@edolstra edolstra merged commit a26be9f into NixOS:master May 2, 2022
@bjornfor
Copy link
Contributor

bjornfor commented May 2, 2022

Thanks!

@orbekk
Copy link
Contributor Author

orbekk commented May 10, 2022

Thanks for the review @thufschmitt

Misterio77 added a commit to Misterio77/nix that referenced this pull request Jun 11, 2022
NixOS#6290 introduced a regex pattern to account for tags when
resolving sourcehut refs. NixOS#4638 reafactored the code,
accidentally treating the pattern as a regular string, causing all
non-HEAD ref resolving to break.

This fixes the regression and adds more test cases to avoid future
breakage.
github-actions bot pushed a commit that referenced this pull request Jun 13, 2022
#6290 introduced a regex pattern to account for tags when
resolving sourcehut refs. #4638 reafactored the code,
accidentally treating the pattern as a regular string, causing all
non-HEAD ref resolving to break.

This fixes the regression and adds more test cases to avoid future
breakage.

(cherry picked from commit 9f6b463)
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.

short form of fetchGit assumes master branch exists
7 participants