Skip to content

Commit

Permalink
Remove ".git" after cloning, with option to disable (#1463)
Browse files Browse the repository at this point in the history
* Remove ".git" after cloning, with option to disable

Also make clones shallow (they already were for pins)

* Fix test
  • Loading branch information
mosteo committed Sep 25, 2023
1 parent df61076 commit 889dd54
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 9 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 => True,
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
5 changes: 1 addition & 4 deletions testsuite/tests/get/git-local/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
Retrieve a release from a local git repository
"""

from glob import glob

from drivers.alr import init_local_crate, run_alr
from drivers.asserts import assert_match
from drivers.alr import alr_with, init_local_crate, run_alr
from drivers.helpers import compare, contents

# Get the release
Expand Down
43 changes: 43 additions & 0 deletions testsuite/tests/misc/git-ungit/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Verify the proper "ungitting" of git origins
"""

import os
import shutil

from drivers.alr import alr_with, crate_dirname, init_local_crate, 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()
foo_dir = crate_dirname("libfoo")

# By default .git should not be there
for wanted in [False, True]:
run_alr("get", "libfoo")

# Check root gotten crate
assert_file_exists(os.path.join(foo_dir, ".git"),
wanted=wanted)

# Check as dependency
init_local_crate()
alr_with("libfoo")

assert_file_exists(os.path.join("alire", "cache", "dependencies",
foo_dir, ".git"),
wanted=wanted)

if not wanted:
# Enable for next round
run_alr("config", "--global", "--set",
"dependencies.git.keep_repository", "true")

# Cleanup for next round
os.chdir(cwd)
shutil.rmtree(crate_dirname("libfoo"))
shutil.rmtree("xxx")

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:
git_index: {}

0 comments on commit 889dd54

Please sign in to comment.