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

Pin a crate in a remote monorepo #1683

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/alire/alire-roots-editable.adb
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ package body Alire.Roots.Editable is
Crate : Alire.Optional.Crate_Name;
Origin : URL;
Ref : String := "";
Branch : String := "")
Branch : String := "";
Subdir : Relative_Path := "")
is

---------------------------
Expand Down Expand Up @@ -444,6 +445,9 @@ package body Alire.Roots.Editable is
package Adirs renames Ada.Directories;
begin

-- TODO: Probably need to ensure the subdir exists and there is a
-- alire.toml manifest. Where this should go?

-- Put in place the checkout, as it is valid if we reached this point

if not Adirs.Exists (This.Edit.Pins_Dir) then
Expand All @@ -464,7 +468,8 @@ package body Alire.Roots.Editable is
Crate,
User_Pins.New_Remote (URL => Origin,
Commit => Ref,
Branch => Branch));
Branch => Branch,
Subdir => Subdir));
This.Reload_Manifest;

-- And update lockfile. We need to call Deploy on the pin (although
Expand Down
3 changes: 2 additions & 1 deletion src/alire/alire-roots-editable.ads
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ package Alire.Roots.Editable is
Crate : Optional.Crate_Name;
Origin : URL;
Ref : String := "";
Branch : String := "")
Branch : String := "";
Subdir : Relative_Path := "")
with Pre =>
(not (Ref /= "" and then Branch /= "")
or else raise Checked_Error with
Expand Down
40 changes: 36 additions & 4 deletions src/alire/alire-user_pins.adb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package body Alire.User_Pins is
package Keys is
Branch : constant String := "branch";
Commit : constant String := "commit";
Subdir : constant String := "subdir";
Internal : constant String := "lockfiled";
Path : constant String := "path";
URL : constant String := "url";
Expand All @@ -42,14 +43,16 @@ package body Alire.User_Pins is
-- New_Remote --
----------------

function New_Remote (URL : Alire.URL;
function New_Remote (URL : Alire.URL;
Commit : String := "";
Branch : String := "")
Branch : String := "";
Subdir : Alire.Relative_Path := "")
return Pin
is (Kind => To_Git,
URL => +URL,
Commit => +Commit,
Branch => +Branch,
Subdir => +Subdir,
Local_Path => <>);

-----------
Expand Down Expand Up @@ -90,6 +93,9 @@ package body Alire.User_Pins is
then ", branch='" & (+This.Branch) & "'"
elsif This.Commit /= ""
then ", commit='" & (+This.Commit) & "'"
else "")
& (if This.Subdir /= ""
then ", subdir='" & (+This.Subdir) & "'"
else ""))
& " }");

Expand Down Expand Up @@ -270,8 +276,12 @@ package body Alire.User_Pins is
-- At this point, we have the sources at Destination. Last checks ensue.

declare
Root : Roots.Optional.Root :=
Roots.Optional.Detect_Root (Destination);
use Directories.Operators; -- "/"

Subdir : constant String := +This.Subdir;
Root : Roots.Optional.Root := Roots.Optional.Detect_Root
((if Subdir = "" then Destination
else Destination / Subdir));
begin

-- Check crate name mismatch
Expand Down Expand Up @@ -312,6 +322,9 @@ package body Alire.User_Pins is
else Origins.Short_Commit (+This.Commit))
elsif Branch (This).Has_Element
then "#" & TTY.Emph (+This.Branch)
else "")
& (if Subdir (This).Has_Element
dalybrown marked this conversation as resolved.
Show resolved Hide resolved
then "#" & TTY.Emph (+This.Subdir)
else ""));

----------
Expand Down Expand Up @@ -398,6 +411,11 @@ package body Alire.User_Pins is
Result.Branch :=
+This.Checked_Pop (Keys.Branch, TOML_String).As_String;
end if;

if This.Contains (Keys.Subdir) then
Result.Subdir :=
+This.Checked_Pop (Keys.Subdir, TOML_String).As_String;
end if;
end return;

else
Expand Down Expand Up @@ -465,6 +483,7 @@ package body Alire.User_Pins is
TOML_String).As_String,
Branch => <>,
Commit => <>,
Subdir => <>,
Local_Path => <>);
begin
if This.Contains (Keys.Branch)
Expand All @@ -488,6 +507,13 @@ package body Alire.User_Pins is
"branch cannot be the empty string");
end if;

if This.Contains (Keys.Subdir) then
Result.Subdir :=
+This.Checked_Pop (Keys.Subdir, TOML_String).As_String;
This.Assert (+Result.Subdir /= "",
"subdir cannot be the empty string");
end if;

-- TEST: empty branch value

This.Report_Extra_Keys;
Expand Down Expand Up @@ -568,6 +594,12 @@ package body Alire.User_Pins is
Table.Set (Keys.Branch,
Create_String (Branch (This).Element.Ptr.all));
end if;

-- TODO: Should this use the "VFS.Attempt_Portable"?
if Subdir (This).Has_Element then
Table.Set (Keys.Subdir,
Create_String (Subdir (This).Element.Ptr.all));
end if;
end if;

Table.Set (Keys.Path,
Expand Down
18 changes: 16 additions & 2 deletions src/alire/alire-user_pins.ads
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ package Alire.User_Pins is

-- Remote pins

function New_Remote (URL : Alire.URL;
function New_Remote (URL : Alire.URL;
Commit : String := "";
Branch : String := "")
Branch : String := "";
Subdir : Alire.Relative_Path := "")
return Pin
with
Pre => Commit = "" or else VCSs.Git.Is_Valid_Commit (Commit),
Expand All @@ -79,6 +80,9 @@ package Alire.User_Pins is
function Commit (This : Pin) return Optional.String
with Pre => This.Is_Remote;

function Subdir (This : Pin) return Optional.String
with Pre => This.Is_Remote;

function TTY_URL_With_Reference (This : Pin;
Detailed : Boolean := False)
return String
Expand Down Expand Up @@ -137,6 +141,7 @@ private
URL : UString;
Branch : UString; -- Optional
Commit : UString; -- Optional
Subdir : UString; -- Optional
Local_Path : Unbounded_Absolute_Path;
-- Empty until the pin is locally deployed
when To_Path =>
Expand Down Expand Up @@ -164,6 +169,15 @@ private
then Optional.Strings.Empty
else Optional.Strings.Unit (+This.Commit));

------------
-- Subdir --
------------

function Subdir (This : Pin) return Optional.String
is (if +This.Subdir = ""
then Optional.Strings.Empty
else Optional.Strings.Unit (+This.Subdir));

---------------
-- Is_Remote --
---------------
Expand Down
19 changes: 15 additions & 4 deletions src/alr/alr-commands-pin.adb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ package body Alr.Commands.Pin is
(Crate => Optional_Crate,
Origin => Cmd.URL.all,
Ref => Cmd.Commit.all,
Branch => Cmd.Branch.all);
Branch => Cmd.Branch.all,
Subdir => Cmd.Subdir.all);

else

Expand Down Expand Up @@ -257,9 +258,11 @@ package body Alr.Commands.Pin is
& " instead of looking for indexed releases."
& " An optional reference can be specified with --commit;"
& " the pin will be frozen at the commit currently matching"
& " the reference. Alternatively, a branch to track can be"
& " specified with --branch. Use `alr update` to refresh the"
& " tracking pin contents.")
& " the reference. Alternatively, a branch to track can be"
& " specified with --branch. Finally, if pinning a monorepo,"
& " the relative path to the crate can be specified with"
& " --subdir. Use `alr update` to refresh the tracking pin"
& " contents.")
);

--------------------
Expand Down Expand Up @@ -297,6 +300,14 @@ package body Alr.Commands.Pin is
Argument => "REF",
Help => "Reference to be retrieved from repository");

Define_Switch
(Config => Config,
Output => Cmd.Subdir'Access,
Long_Switch => "--subdir=",
Argument => "PATH",
Help =>
"Path to the crate relative to the root of the repository");

Define_Switch
(Config => Config,
Output => Cmd.URL'Access,
Expand Down
3 changes: 2 additions & 1 deletion src/alr/alr-commands-pin.ads
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ package Alr.Commands.Pin is
overriding
function Usage_Custom_Parameters (Cmd : Command) return String
is ("[[crate[=<version>]]"
& " | crate --use=<path> [--commit=REF] [--branch=NAME]"
& " | crate --use=<path> [--commit=REF] [--branch=NAME] [--subdir=PATH]"
& " | --all]");

private

type Command is new Commands.Command with record
Branch : aliased GNAT.Strings.String_Access;
Commit : aliased GNAT.Strings.String_Access;
Subdir : aliased GNAT.Strings.String_Access;
Pin_All : aliased Boolean;
Unpin : aliased Boolean;
URL : aliased GNAT.Strings.String_Access;
Expand Down
Loading