From e557f8575cfa493aaddafdb292be87ce079bed15 Mon Sep 17 00:00:00 2001 From: Alois Wohlschlager Date: Sat, 28 Dec 2024 20:12:01 +0100 Subject: [PATCH] libfetchers/mercurial: default to current revision on local repos When using the Mercurial fetcher on a local repository without explicitly specifying a branch or revision, previously always the tip of the default branch would be fetched. This is likely unexpected by the user, and inconsistent with the Git fetcher as well as the dirty case. To reduce surprises and restore consistency, fetch the currently checked out revision instead. Change-Id: Id6d58f958b710f7a9661dc66ad4ddcb8d06a0cdd --- lix/libfetchers/mercurial.cc | 6 ++++++ tests/functional/fetchMercurial.sh | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lix/libfetchers/mercurial.cc b/lix/libfetchers/mercurial.cc index eda49f93c..f99191566 100644 --- a/lix/libfetchers/mercurial.cc +++ b/lix/libfetchers/mercurial.cc @@ -204,6 +204,12 @@ struct MercurialInputScheme : InputScheme return {std::move(storePath), input}; } + + auto tokens = tokenizeString>( + runHg({ "identify", "-R", actualUrl, "-r", ".", "--template", "{branch} {node}" })); + assert(tokens.size() == 2); + input.attrs.insert_or_assign("ref", tokens[0]); + input.attrs.insert_or_assign("rev", tokens[1]); } if (!input.getRef()) input.attrs.insert_or_assign("ref", "default"); diff --git a/tests/functional/fetchMercurial.sh b/tests/functional/fetchMercurial.sh index 624705e9b..001e3ae00 100644 --- a/tests/functional/fetchMercurial.sh +++ b/tests/functional/fetchMercurial.sh @@ -33,11 +33,11 @@ path=$(nix eval --impure --raw --expr "(builtins.fetchMercurial \"file://$repo\" [[ $(cat $path/hello) = unclean ]] hg revert --cwd $repo --all -# Fetch the default branch. +# Fetch the currently checked out revision. path=$(nix eval --impure --raw --expr "(builtins.fetchMercurial \"file://$repo\").outPath") [[ $(cat $path/hello) = world ]] -# In pure eval mode, fetchGit without a revision should fail. +# In pure eval mode, fetchMercurial without a revision should fail. [[ $(nix eval --impure --raw --expr "(builtins.readFile (fetchMercurial \"file://$repo\" + \"/hello\"))") = world ]] (! nix eval --raw --expr "builtins.readFile (fetchMercurial \"file://$repo\" + \"/hello\")") @@ -45,7 +45,7 @@ path=$(nix eval --impure --raw --expr "(builtins.fetchMercurial \"file://$repo\" path2=$(nix eval --impure --raw --expr "(builtins.fetchMercurial { url = \"file://$repo\"; rev = \"$rev2\"; }).outPath") [[ $path = $path2 ]] -# In pure eval mode, fetchGit with a revision should succeed. +# In pure eval mode, fetchMercurial with a revision should succeed. [[ $(nix eval --raw --expr "builtins.readFile (fetchMercurial { url = \"file://$repo\"; rev = \"$rev2\"; } + \"/hello\")") = world ]] # Fetch again. This should be cached. @@ -104,3 +104,14 @@ echo paris > $repo/hello # Passing a `name` argument should be reflected in the output path path5=$(nix eval -vvvvv --impure --refresh --raw --expr "(builtins.fetchMercurial { url = \"file://$repo\"; name = \"foo\"; } ).outPath") [[ $path5 =~ -foo$ ]] + +# Fetch the currently checked out revision on a non-default branch. +hg branch --cwd $repo meow +hg commit --cwd $repo -m 'Bla4' +[[ $(nix eval --impure --raw --expr "(builtins.fetchMercurial \"file://$repo\").branch") = meow ]] +[[ $(nix eval --impure --expr "(builtins.fetchMercurial \"file://$repo\").revCount") = 4 ]] + +# Fetch an older revision currently checked out. +hg update --cwd $repo 2 +[[ $(nix eval --impure --raw --expr "(builtins.fetchMercurial \"file://$repo\").branch") = default ]] +[[ $(nix eval --impure --expr "(builtins.fetchMercurial \"file://$repo\").revCount") = 3 ]]