From 91d20d9576f4431318d5bd47c99ae862d0a944c4 Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Mon, 28 Jun 2021 16:20:45 +0200 Subject: [PATCH] Fix conversion of references to commits (#761) * Completed Roots.Editable commands for removal * Completed Alr.Commands.Pin * Reinstated all `alr pin` functionality * Reinstated remaining disabled pin tests * Reinstated remaining disabled tests * Testsuite: fix platform-independent path on Windows * Implement pin commands in alr driver * Testsuite: Implement with command in alr driver * Testsuite: equivalence between manual and cmd-line * Fix git folder deletion on Windows * Fix reference to commit mode --- src/alire/alire-roots-editable.adb | 44 +++++++++++++++++--- src/alire/alire-roots-editable.ads | 9 ++-- src/alire/alire-user_pins.adb | 1 - src/alire/alire-user_pins.ads | 5 ++- src/alr/alr-commands-pin.adb | 2 +- src/alr/alr-commands-withing.adb | 2 +- testsuite/drivers/alr.py | 5 ++- testsuite/tests/with/git-reference/test.py | 36 ++++++++++++++++ testsuite/tests/with/git-reference/test.yaml | 3 ++ 9 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 testsuite/tests/with/git-reference/test.py create mode 100644 testsuite/tests/with/git-reference/test.yaml diff --git a/src/alire/alire-roots-editable.adb b/src/alire/alire-roots-editable.adb index ed3c21ac..464fad49 100644 --- a/src/alire/alire-roots-editable.adb +++ b/src/alire/alire-roots-editable.adb @@ -2,6 +2,7 @@ with Alire.Conditional; with Alire.Dependencies.Diffs; with Alire.Directories; with Alire.Manifest; +with Alire.Origins; with Alire.Roots.Optional; with Alire.User_Pins; with Alire.Utils.User_Input; @@ -306,17 +307,50 @@ package body Alire.Roots.Editable is procedure Add_Remote_Pin (This : in out Root; Crate : Alire.Optional.Crate_Name; Origin : URL; - Commit : String := ""; + Ref : String := ""; Branch : String := "") is + + --------------------------- + -- Convert_Ref_To_Commit -- + --------------------------- + + procedure Convert_Ref_To_Commit is + Ref : constant String := Add_Remote_Pin.Ref; + Commit : constant String := + VCSs.Git.Handler.Remote_Commit (Origin, Ref); + begin + if Commit /= "" then + Put_Info ("Using commit " & TTY.Emph (Commit) + & " for reference " & TTY.Emph (Ref)); + This.Add_Remote_Pin (Crate, Origin, Commit, Branch); + else + Raise_Checked_Error + ("Requested remote reference " & TTY.Emph (Ref) + & " not found in repository " & TTY.URL (Origin)); + end if; + end Convert_Ref_To_Commit; + Temp_Pin : Directories.Temp_File; -- We'll need to fetch the remote to a temporary location to verify -- crate matches. If all goes well, we will keep the download so there -- is no need to redownload on next run. begin + + -- We accept any reference that can be converted to a commit, as commit. + -- This is a bit of a misnomer really in the command-line interface. + + if Ref /= "" and then not Origins.Is_Valid_Commit (Ref) then + Convert_Ref_To_Commit; + return; + end if; + + -- Clone the remote so we can identify the crate and perform other + -- validity checks. + if not VCSs.Git.Handler.Clone - (From => Origin & (if Commit /= "" - then "#" & Commit + (From => Origin & (if Ref /= "" + then "#" & Ref else ""), Into => Temp_Pin.Filename, Branch => Branch, -- May be empty for default branch @@ -336,7 +370,7 @@ package body Alire.Roots.Editable is Temp_Pin.Filename); New_Pin : User_Pins.Pin := User_Pins.New_Remote (URL => Origin, - Commit => Commit, + Commit => Ref, Branch => Branch); Destination : constant Absolute_Path := @@ -364,7 +398,7 @@ package body Alire.Roots.Editable is Alire.Manifest.Append (Crate_File (This.Edit), Crate, User_Pins.New_Remote (URL => Origin, - Commit => Commit, + Commit => Ref, Branch => Branch)); This.Reload_Manifest; diff --git a/src/alire/alire-roots-editable.ads b/src/alire/alire-roots-editable.ads index 0893beb4..f2672ff3 100644 --- a/src/alire/alire-roots-editable.ads +++ b/src/alire/alire-roots-editable.ads @@ -85,16 +85,17 @@ package Alire.Roots.Editable is procedure Add_Remote_Pin (This : in out Root; Crate : Optional.Crate_Name; Origin : URL; - Commit : String := ""; + Ref : String := ""; Branch : String := "") with Pre => - (not (Commit /= "" and then Branch /= "") + (not (Ref /= "" and then Branch /= "") or else raise Checked_Error with - Errors.Set ("Simultaneous Branch and Commit pins are incompatible")) + Errors.Set ("Simultaneous Branch and Ref pins are incompatible")) and then (Crate.Is_Empty or else This.Can_Be_Pinned (Crate.Element)); - -- Add a pin to a remote repo, with optional Commit xor Branch. if + -- Add a pin to a remote repo, with optional Ref xor Branch. If Ref is + -- not a Commit, it will be converted to one using `git ls-remote`. If -- Crate.Is_Empty then Path must point to an Alire workspace for which it -- can be deduced. If Crate.Has_Element, the crates should match. If the -- root does not depend already on the crate, a dependency will be added. diff --git a/src/alire/alire-user_pins.adb b/src/alire/alire-user_pins.adb index 6fb77635..9c09b60d 100644 --- a/src/alire/alire-user_pins.adb +++ b/src/alire/alire-user_pins.adb @@ -1,7 +1,6 @@ with Ada.Directories; with Alire.Directories; -with Alire.Origins; with Alire.Roots.Optional; with Alire.Utils.TTY; with Alire.Utils.User_Input; diff --git a/src/alire/alire-user_pins.ads b/src/alire/alire-user_pins.ads index 63c0da6a..89bc91e4 100644 --- a/src/alire/alire-user_pins.ads +++ b/src/alire/alire-user_pins.ads @@ -1,4 +1,5 @@ with Alire.Optional; +with Alire.Origins; with Alire.TOML_Adapters; with Semantic_Versioning; @@ -65,7 +66,9 @@ package Alire.User_Pins is Commit : String := ""; Branch : String := "") return Pin - with Post => New_Remote'Result.Kind = To_Git; + with + Pre => Commit = "" or else Origins.Is_Valid_Commit (Commit), + Post => New_Remote'Result.Kind = To_Git; function URL (This : Pin) return Alire.URL with Pre => This.Is_Remote; diff --git a/src/alr/alr-commands-pin.adb b/src/alr/alr-commands-pin.adb index 3ceef83e..cf0dccd1 100644 --- a/src/alr/alr-commands-pin.adb +++ b/src/alr/alr-commands-pin.adb @@ -170,7 +170,7 @@ package body Alr.Commands.Pin is New_Root.Add_Remote_Pin (Crate => Optional_Crate, Origin => Cmd.URL.all, - Commit => Cmd.Commit.all, + Ref => Cmd.Commit.all, Branch => Cmd.Branch.all); else diff --git a/src/alr/alr-commands-withing.adb b/src/alr/alr-commands-withing.adb index 95b395b1..96ffebff 100644 --- a/src/alr/alr-commands-withing.adb +++ b/src/alr/alr-commands-withing.adb @@ -194,7 +194,7 @@ package body Alr.Commands.Withing is Root.Add_Remote_Pin (Crate => Crate, Origin => Cmd.URL.all, - Commit => Cmd.Commit.all, + Ref => Cmd.Commit.all, Branch => Cmd.Branch.all); else diff --git a/testsuite/drivers/alr.py b/testsuite/drivers/alr.py index 028e2194..f458e5b2 100644 --- a/testsuite/drivers/alr.py +++ b/testsuite/drivers/alr.py @@ -342,10 +342,13 @@ def alr_with(dep="", path="", url="", commit="", branch="", if path != "" and url != "": raise RuntimeError("Do not specify both path and url") + if manual and dep == "": + raise RuntimeError("Cannot manually add without explicit dependency") + separators = "=^~<>*" # Fix the dependency if no version subset is in dep - if not any([separator in dep for separator in separators]): + if manual and not any([separator in dep for separator in separators]): dep += "*" # Find the separator position diff --git a/testsuite/tests/with/git-reference/test.py b/testsuite/tests/with/git-reference/test.py new file mode 100644 index 00000000..75927801 --- /dev/null +++ b/testsuite/tests/with/git-reference/test.py @@ -0,0 +1,36 @@ +""" +Test conversion of git references into commits during pinning +""" + +from drivers.alr import run_alr, alr_with, init_local_crate +from drivers.asserts import assert_eq, assert_match +from drivers.helpers import init_git_repo + +import os +import subprocess + +# Create a new "remote" repository with a tag that we'll use as reference +init_local_crate("remote") +init_git_repo(".") +subprocess.run(["git", "tag", "v1"]).check_returncode() + +# Verify that pinning to a valid reference succeeds +os.chdir("..") +init_local_crate() +alr_with(path="../remote", commit="v1", manual=False) +p = run_alr("pin") +assert_match("remote file:alire/cache/pins/remote_.{,8} ../remote#.{,8}", + p.out) + +# Remove dependency for next test +alr_with("remote", delete=True, manual=False) +p = run_alr("pin") +assert_eq("There are no pins\n", p.out) + +# Verify that pinning to an invalid reference fails +p = run_alr("with", "--use", "../remote", "--commit", "v2", + complain_on_error=False) +assert_match(".*Requested remote reference v2 not found in repository.*", + p.out) + +print('SUCCESS') diff --git a/testsuite/tests/with/git-reference/test.yaml b/testsuite/tests/with/git-reference/test.yaml new file mode 100644 index 00000000..872fc127 --- /dev/null +++ b/testsuite/tests/with/git-reference/test.yaml @@ -0,0 +1,3 @@ +driver: python-script +indexes: + basic_index: {} -- 2.39.5