Class: Makit::Services::RepositoryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/services/repository_manager.rb

Overview

Service class responsible for git repository management operations Handles cloning, pulling, logging, and working directory setup

Class Method Summary collapse

Class Method Details

.clone_or_pull_repository(git_repository) ⇒ Array<Makit::V1::Command>

Clone repository if it doesn’t exist, otherwise pull latest changes

Parameters:

  • git_repository (String)

    the git repository URL

Returns:



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/makit/services/repository_manager.rb', line 70

def clone_or_pull_repository(git_repository)
  commands = []
  clone_dir = Directories.get_clone_directory(git_repository)

  commands << if Dir.exist?(clone_dir)
    pull_repository(git_repository)
  else
    clone_repository(git_repository)
  end
  commands
end

.clone_repository(git_repository) ⇒ Array<Makit::V1::Command>

Clone a git repository to a local directory

Parameters:

  • git_repository (String)

    the git repository URL

Returns:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/makit/services/repository_manager.rb', line 39

def clone_repository(git_repository)
  Validator.validate_url_parameter(git_repository)
  commands = []

  clone_dir = Directories.get_clone_directory(git_repository)
  unless Dir.exist?(clone_dir)
    commands << Makit::Commands::Runner.default.execute("git clone #{git_repository} #{clone_dir}")
  end

  commands
end

.get_latest_commit(clone_dir, commands) ⇒ String

Get the latest commit hash from a repository

Parameters:

  • clone_dir (String)

    the local clone directory

  • commands (Array)

    array to append command results to

Returns:

  • (String)

    the latest commit hash



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/makit/services/repository_manager.rb', line 117

def get_latest_commit(clone_dir, commands)
  Dir.chdir(clone_dir) do
    git_log = Makit::Commands::Runner.default.execute("git log -n 1 --date=iso")
    commands << git_log

    commit = git_log.output.match(/^commit ([0-9a-f]{#{Validator::COMMIT_HASH_LENGTH}})$/i)&.captures&.first

    if commit.nil? || commit.empty? || !commit.match?(/\\A[0-9a-f]{#{Validator::COMMIT_HASH_LENGTH}}\\z/i)
      raise Makit::InvalidCommitError, "invalid commit: #{commit}"
    end

    commit
  end
end

.get_repository_log(git_repository, limit, skip) ⇒ Array<Makit::V1::GitLogEntry>

Get git log information for a repository

Parameters:

  • git_repository (String)

    the git repository URL

  • limit (Integer)

    maximum number of commits to retrieve

  • skip (Integer)

    number of commits to skip

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/makit/services/repository_manager.rb', line 88

def get_repository_log(git_repository, limit, skip)
  Validator.validate_url_parameter(git_repository)
  Validator.validate_pagination_parameters(limit, skip)

  clone_dir = Directories.get_clone_directory(git_repository)
  Validator.validate_directory_exists(clone_dir, "clone directory")

  Dir.chdir(clone_dir) do
    log_command = Makit::Commands::Runner.default.execute("git log -n #{limit} --skip #{skip} --date=iso")
    parse_git_log_output(log_command)
  end
end

.initialize_repository(directory) ⇒ Makit::V1::Command

Initialize a new git repository in the specified directory

Parameters:

  • directory (String)

    the directory path to initialize

Returns:

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/makit/services/repository_manager.rb', line 17

def initialize_repository(directory)
  Validator.validate_directory_parameter(directory)
  FileUtils.mkdir_p(directory)

  raise Makit::DirectoryError, "directory does not exist: #{directory}" unless Dir.exist?(directory)

  Dir.chdir(directory) do
    File.write(".gitignore", Makit::Content::GITIGNORE) unless File.exist?(".gitignore")
    init_command = Makit::Commands::Runner.default.execute("git init")

    if init_command.exit_code != 0
      raise Makit::GitError, "failed to initialize local repository: #{directory}\\n#{Makit::Humanize.get_command_summary(init_command)}"
    end

    init_command
  end
end

.pull_repository(git_repository) ⇒ Makit::V1::Command

Pull the latest changes from the remote repository

Parameters:

  • git_repository (String)

    the git repository URL

Returns:



55
56
57
58
59
60
61
62
63
64
# File 'lib/makit/services/repository_manager.rb', line 55

def pull_repository(git_repository)
  Validator.validate_url_parameter(git_repository)
  clone_dir = Directories.get_clone_directory(git_repository)

  Validator.validate_directory_exists(clone_dir, "clone directory")

  Dir.chdir(clone_dir) do
    execute_git_pull(clone_dir)
  end
end

.setup_work_directory(repository) ⇒ nil

Set up a work directory for a repository

Parameters:

  • repository (String)

    the git repository URL

Returns:

  • (nil)

    always returns nil



105
106
107
108
109
110
# File 'lib/makit/services/repository_manager.rb', line 105

def setup_work_directory(repository)
  Validator.validate_url_parameter(repository)
  clone_or_pull_repository(repository)
  create_work_directory(repository)
  nil
end

.validate_clone_directory(url) ⇒ String

Validate that a clone directory exists for the given repository

Parameters:

  • url (String)

    the git repository URL

Returns:

  • (String)

    the clone directory path



136
137
138
139
140
# File 'lib/makit/services/repository_manager.rb', line 136

def validate_clone_directory(url)
  clone_dir = Directories.get_clone_directory(url)
  Validator.validate_directory_exists(clone_dir, "clone directory")
  clone_dir
end