Skip to content

Commit

Permalink
Feat: Migrate git shard
Browse files Browse the repository at this point in the history
Refs: #2213
- Migrates git shard to main testsuite project.
- Flattens out the directory structure.
- Some of the deleted utils files might be necessary, `require`
  statements will be resolved in later commits, if that is the case.

Signed-off-by: svteb <[email protected]>
  • Loading branch information
svteb committed Feb 28, 2025
1 parent e4d4660 commit 4e5d408
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
25 changes: 25 additions & 0 deletions spec/modules/git_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "../spec_helper"
require "colorize"

describe "Git" do
it "'installation_found?' should show a git client was located", tags:["git"] do
(GitClient.installation_found?).should be_true
end

it "'git_global_response()' should return the information about the git installation", tags:["git"] do
(git_global_response(true)).should contain("git version")
end

it "'git_local_response()' should return the information about the git installation", tags:["git"] do
(git_local_response(true)).should eq("")
end

it "'git_version()' should return the information about the git version", tags:["git"] do
(git_version(git_global_response)).should match(/(([0-9]{1,3}[\.]){1,2}[0-9]{1,3})/)
(git_version(git_local_response)).should contain("")
end

it "'git_installations()' should return the information about the git installation", tags:["git"] do
(git_installation(true)).should contain("git found")
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "colorize"
require "../src/cnf_testsuite"
require "../src/tasks/utils/utils.cr"
require "../src/modules/tar/tar.cr"
require "../src/modules/git.cr"

ENV["CRYSTAL_ENV"] = "TEST"

Expand Down
4 changes: 4 additions & 0 deletions src/modules/git/constants.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module GitClient
DEFAULT_LOCAL_BINARY_PATH = "tools/git/linux-amd64/git"
BASE_CONFIG = "./config.yml"
end
19 changes: 19 additions & 0 deletions src/modules/git/git.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "log"
require "./utils/system_information.cr"

module GitClient
def self.installation_found?
git_installation.includes?("git found")
end

def self.clone(command)
Log.info { "GitClient.clone command: #{command}" }
status = Process.run("git clone #{command}",
shell: true,
output: output = IO::Memory.new,
error: stderr = IO::Memory.new)
Log.info { "GitClient.clone output: #{output.to_s}" }
Log.info { "GitClient.clone stderr: #{stderr.to_s}" }
{status: status, output: output, error: stderr}
end
end
78 changes: 78 additions & 0 deletions src/modules/git/utils/system_information.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require "colorize"
require "../../src/utils/utils.cr"

def git_installation(verbose=false)
gmsg = "No Global git version found"
lmsg = "No Local git version found"
ggit = git_global_response
Log.for("verbose").info { ggit } if verbose

global_git_version = git_version(ggit, verbose)

if !global_git_version.empty?
gmsg = "Global git found. Version: #{global_git_version}"
stdout_success gmsg
else
stdout_warning gmsg
end

lgit = git_local_response
Log.for("verbose").info { lgit } if verbose

local_git_version = git_version(lgit, verbose)

if !local_git_version.empty?
lmsg = "Local git found. Version: #{local_git_version}"
stdout_success lmsg
else
stdout_warning lmsg
end

# uncomment to fail the installation check
# global_git_version = nil
# local_git_version = nil
# gmsg = "No Global git version found"
# lmsg = "No Local git version found"
if !(global_git_version && local_git_version)
stdout_failure "Git not found"
stdout_failure %Q(
Linux installation instructions for Git can be found here: https://github.com/git-guides/install-git
Install git binary with curl on Linux
On Debian/Ubuntu:
sudo apt-get install git-all
On Fedora
sudo dnf install git-all
)
end
"#{lmsg} #{gmsg}"
end

def git_global_response(verbose=false)
status = Process.run("git version", shell: true, output: git_response = IO::Memory.new, error: stderr = IO::Memory.new)
Log.for("verbose").info { git_response } if verbose
git_response.to_s
end

def git_local_response(verbose=false)
Log.for("verbose").info { local_git_path } if verbose
status = Process.run("#{local_git_path} version", shell: true, output: git_response = IO::Memory.new, error: stderr = IO::Memory.new)
Log.for("verbose").info { git_response.to_s } if verbose
git_response.to_s
end

def git_version(git_response, verbose=false)
# example
# git version 1.9.1
resp = git_response.match /git version (([0-9]{1,3}[\.]){1,2}[0-9]{1,3})/
Log.for("verbose").info { resp } if verbose
if resp
"#{resp && resp.not_nil![1]}"
else
""
end
end
32 changes: 32 additions & 0 deletions src/modules/git/utils/utils.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "totem"
require "colorize"
require "log"
require "file_utils"
require "../constants.cr"

def stdout_info(msg)
puts msg
end

def stdout_success(msg)
puts msg.colorize(:green)
end

def stdout_warning(msg)
puts msg.colorize(:yellow)
end

def stdout_failure(msg)
puts msg.colorize(:red)
end

def local_git_path
if File.exists?(GitClient::BASE_CONFIG)
config = Totem.from_file GitClient::BASE_CONFIG
if config[":git_binary_path"]? && config[":git_binary_path"].as_s?
return config[":git_binary_path"].as_s
end
end

FileUtils.pwd + GitClient::DEFAULT_LOCAL_BINARY_PATH
end

0 comments on commit 4e5d408

Please sign in to comment.