From 51e95ec736648489c67f4cacd0dfd294eebe456c Mon Sep 17 00:00:00 2001 From: Alejandro R Mosteo Date: Mon, 9 Nov 2020 09:39:20 +0100 Subject: [PATCH] Fix: on `publish --tar`, allow git without remote (#614) If we are only using git to generate the archive, we do not need the remote as it won't be referenced by the origin. --- src/alire/alire-publish.adb | 18 +++++++++++++---- src/alire/alire-vcss-git.adb | 39 ++++++++++++++++++++++++------------ src/alire/alire-vcss-git.ads | 16 ++++++++++----- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/alire/alire-publish.adb b/src/alire/alire-publish.adb index a9eece87..74d2f0bc 100644 --- a/src/alire/alire-publish.adb +++ b/src/alire/alire-publish.adb @@ -77,12 +77,20 @@ package body Alire.Publish is --------------------- -- Check_Git_Clean -- --------------------- - - procedure Check_Git_Clean (Path : Any_Path) is + -- Check that the repo is clean. If we need it only for generating an + -- archive, that is enough; otherwise, check that we are in sync with + -- the remote to which the origin will point to. + procedure Check_Git_Clean (Path : Any_Path; For_Archiving : Boolean) is use all type VCSs.Git.States; Git : constant VCSs.Git.VCS := VCSs.Git.Handler; begin case Git.Status (Path) is + when No_Remote => + if For_Archiving then + Log_Success ("Local repository is clean (without remote)."); + else + Git_Error ("No remote configured", Path); + end if; when Clean => Log_Success ("Local repository is clean."); when Ahead => @@ -304,6 +312,8 @@ package body Alire.Publish is procedure Prepare_Archive (Context : in out Data) with Pre => Context.Root.Is_Valid; + -- Prepare a tar file either using git archive (if git repo detected) or + -- plain tar otherwise. procedure Prepare_Archive (Context : in out Data) is use Utils; @@ -382,7 +392,7 @@ package body Alire.Publish is begin if Is_Repo then - Check_Git_Clean (Root.Path); + Check_Git_Clean (Root.Path, For_Archiving => True); else Trace.Warning ("Not in a git repository, assuming plain sources."); end if; @@ -787,7 +797,7 @@ package body Alire.Publish is -- Do not continue if the local repo is dirty - Check_Git_Clean (Root.Value.Path); + Check_Git_Clean (Root.Value.Path, For_Archiving => False); -- If given a revision, extract commit and verify it exists locally diff --git a/src/alire/alire-vcss-git.adb b/src/alire/alire-vcss-git.adb index 0ede27ea..4363d949 100644 --- a/src/alire/alire-vcss-git.adb +++ b/src/alire/alire-vcss-git.adb @@ -204,14 +204,21 @@ package body Alire.VCSs.Git is -- Remote -- ------------ - function Remote (This : VCS; Path : Directory_Path) return String is + function Remote (This : VCS; + Path : Directory_Path; + Checked : Boolean := True) + return String is pragma Unreferenced (This); Guard : Directories.Guard (Directories.Enter (Path)) with Unreferenced; Output : constant Utils.String_Vector := Run_Git_And_Capture (Empty_Vector & "remote"); begin if Output.Is_Empty then - Raise_Checked_Error ("No remote is configured"); + if Checked then + Raise_Checked_Error ("No remote is configured"); + else + return ""; + end if; else return Output.First_Element; end if; @@ -258,17 +265,23 @@ package body Alire.VCSs.Git is else -- Retrieve revisions from remote branch tip up to our local HEAD. If -- not empty, we are locally ahead. - if Run_Git_And_Capture - (Empty_Vector - & "rev-list" - & String'(This.Remote (Repo) & "/" & This.Branch (Repo) - & "..HEAD")).Is_Empty - then - return Clean; - else - -- At least one local commit not pushed to the remote - return Ahead; - end if; + declare + Remote : constant String := This.Remote (Repo, Checked => False); + begin + if Remote = "" then + return No_Remote; + elsif Run_Git_And_Capture + (Empty_Vector + & "rev-list" + & String'(Remote & "/" & This.Branch (Repo) + & "..HEAD")).Is_Empty + then + return Clean; + else + -- At least one local commit not pushed to the remote + return Ahead; + end if; + end; end if; end Status; diff --git a/src/alire/alire-vcss-git.ads b/src/alire/alire-vcss-git.ads index 2e73a36a..59390e8b 100644 --- a/src/alire/alire-vcss-git.ads +++ b/src/alire/alire-vcss-git.ads @@ -40,17 +40,23 @@ package Alire.VCSs.Git is -- Check if a repo exists at Path not overriding - function Remote (This : VCS; Path : Directory_Path) return String; - -- Retrieve current remote name (usually "origin") + function Remote (This : VCS; + Path : Directory_Path; + Checked : Boolean := True) + return String; + -- Retrieve current remote name (usually "origin"). If checked, raise + -- Checked_Error when no remote configured. Otherwise, return ""; overriding function Update (This : VCS; Repo : Directory_Path) return Outcome; - type States is (Clean, Ahead, Dirty); - -- Three states we are interested in for publishing: clean and up-to-date, - -- clean but not yet pushed, and dirty. + type States is (Dirty, -- Uncommited local changes + No_Remote, -- Clean, no remote configured + Clean, -- Clean, up to date with remote + Ahead); -- Clean, ahead of remote (needs pull) + -- States we are interested in for publishing not overriding function Status (This : VCS; -- 2.39.5