Skip to content

Commit

Permalink
Remove ".git" after cloning, with option to disable
Browse files Browse the repository at this point in the history
Also make clones shallow (they already were for pins)
  • Loading branch information
mosteo committed Sep 22, 2023
1 parent 2e3a520 commit eb49f82
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/alire/alire-config-builtins.ads
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ package Alire.Config.Builtins is
-- Builtins --
--------------

-- DEPENDENCIES

Dependencies_Git_Keep_Repository : constant Builtin := New_Builtin
(Key => "dependencies.git.keep_repository",
Def => False,
Help =>
"When true, git origins are a proper git repository after deployment. "
& "Otherwise they are deployed as a plain directory.");

Dependencies_Shared : constant Builtin := New_Builtin
(Key => "dependencies.shared",
Def => False,
Expand Down
19 changes: 18 additions & 1 deletion src/alire/alire-origins-deployers-git.adb
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
with Alire.Config.Builtins;
with Alire.Directories;
with Alire.VCSs.Git;

package body Alire.Origins.Deployers.Git is

use Directories.Operators;

------------
-- Deploy --
------------

overriding
function Deploy (This : Deployer; Folder : String) return Outcome is
begin
return VCSs.Git.Handler.Clone (This.Base.URL_With_Commit, Folder);
VCSs.Git.Handler.Clone (This.Base.URL_With_Commit, Folder).Assert;

if Config.Builtins.Dependencies_Git_Keep_Repository.Get then

Trace.Debug ("Keeping git repo from " & This.Base.TTY_URL_With_Commit
& " at " & TTY.URL (Folder));

else

Directories.Delete_Tree (Folder / VCSs.Git.Git_Dir);

end if;

return Outcome_Success;
end Deploy;

-----------
Expand Down
4 changes: 3 additions & 1 deletion src/alire/alire-publish-submit.adb
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ package body Alire.Publish.Submit is
(From => Index.Community_Host
/ User_Info.User_GitHub_Login
/ Index.Community_Repo_Name,
Into => Local_Repo_Path).Assert;
Into => Local_Repo_Path,
Branch => "",
Depth => 1).Assert;

-- We can reuse the pull logic now to set up the local branch
Pull;
Expand Down
10 changes: 9 additions & 1 deletion src/alire/alire-vcss-git.adb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ package body Alire.VCSs.Git is
From : URL;
Into : Directory_Path)
return Outcome
is (This.Clone (From, Into, Branch => ""));
is (This.Clone (From, Into, Branch => "", Depth => 1));

-----------
-- Clone --
Expand Down Expand Up @@ -732,6 +732,14 @@ package body Alire.VCSs.Git is
end return;
end Worktree;

-------------
-- Git_Dir --
-------------

function Git_Dir return Any_Path
is (OS_Lib.Getenv (Name => "GIT_DIR",
Default => ".git"));

-----------------
-- Head_Commit --
-----------------
Expand Down
5 changes: 5 additions & 0 deletions src/alire/alire-vcss-git.ads
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package Alire.VCSs.Git is
-- This is actually returned by e.g. `git worktree`, even if it could be a
-- real commit. I guess the chances are deemed too low.

function Git_Dir return Any_Path;
-- ".git" unless overridden by GIT_DIR

type VCS (<>) is new VCSs.VCS with private;

function Handler return VCS;
Expand All @@ -44,6 +47,8 @@ package Alire.VCSs.Git is
From : URL;
Into : Directory_Path)
return Outcome;
-- Make a shallow clone of the given URL (that may include '#commit). For
-- more precise control, use the following Clone signature.

not overriding
function Clone (This : VCS;
Expand Down
7 changes: 7 additions & 0 deletions testsuite/drivers/alr.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@ def alr_builds_dir() -> str:
return os.path.join(alr_config_dir(), "cache", "builds")


def crate_dirname(crate):
"""
Return the deployment dir of a crate, obtained with `alr get --dirname`
"""
return run_alr("get", "--dirname", crate).out.strip()


def external_compiler_version() -> str:
"""
Return the version of the external compiler
Expand Down
7 changes: 5 additions & 2 deletions testsuite/drivers/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ def assert_installed(prefix : str, milestones : List[str]):
p.out)


def assert_file_exists(path : str):
def assert_file_exists(path : str, wanted : bool = True):
"""
Check that a file exists
"""
assert os.path.exists(path), f"Missing expected file {path}"
if wanted:
assert os.path.exists(path), f"Missing expected file {path}"
else:
assert not os.path.exists(path), f"Unexpected file {path}"


def assert_in_file(path : str, expected : str):
Expand Down
27 changes: 27 additions & 0 deletions testsuite/tests/misc/git-ungit/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Verify the proper "ungitting" of git origins
"""

import os
import shutil
from glob import glob

from drivers.alr import crate_dirname, run_alr
from drivers.asserts import assert_file_exists

# By default, git deployments are shallow and see their .git directory removed
# Check that and that enabling legacy behavior works

cwd = os.getcwd()

run_alr("get", "hello")
os.chdir(crate_dirname("hello"))

# Check root gotten crate
assert_file_exists(".git", wanted=False)

# Check dependency
os.chdir(os.path.join("alire", "cache", "dependencies"))
assert_file_exists(os.path.join(glob("libhello*")[0], ".git"), wanted=False)

print('SUCCESS')
3 changes: 3 additions & 0 deletions testsuite/tests/misc/git-ungit/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
driver: python-script
indexes:
basic_index: {}

0 comments on commit eb49f82

Please sign in to comment.