Class: Ronin::Repos::Repository Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/repos/repository.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents an installed repository.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the repository.

Parameters:

  • path (String)

    The path to the repository.

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ronin/repos/repository.rb', line 52

def initialize(path)
  @path = File.expand_path(path)

  unless File.exist?(@path)
    raise(RepositoryNotFound,"repository does not exist: #{@path.inspect}")
  end

  unless File.directory?(@path)
    raise(RepositoryNotFound,"path is not a directory: #{@path.inspect}")
  end

  @name = File.basename(@path)
end

Instance Attribute Details

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of the repository.

Returns:

  • (String)


41
42
43
# File 'lib/ronin/repos/repository.rb', line 41

def name
  @name
end

#pathString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The path to the repository's directory.

Returns:

  • (String)


36
37
38
# File 'lib/ronin/repos/repository.rb', line 36

def path
  @path
end

Class Method Details

.clone(uri, path, depth: nil) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clones a repository.

Parameters:

  • uri (String, URI::HTTPS)

    The https:// or git@HOST:PATH SSH URI

  • path (String)

    The path to where the repository will be cloned to.

  • depth (Integer, nil) (defaults to: nil)

    The number of commits to clone.

Returns:

Raises:

  • (CommandFailed)

    The git command failed.

  • (CommandNotFailed)

    The git command is not installed.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ronin/repos/repository.rb', line 87

def self.clone(uri,path, depth: nil)
  path = path.to_s
  args = []

  if depth
    args << '--depth' << depth.to_s
  end

  args << uri.to_s
  args << path.to_s

  case system('git','clone',*args)
  when nil
    raise(CommandNotInstalled,"git is not installed")
  when false
    raise(CommandFailed,"command failed: git clone #{args.join(' ')}")
  end

  return new(path)
end

.install(uri, path, branch: nil, tag: nil, **kwargs) ⇒ Repository

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clones and installs a repository from the URI and to the destination path.

Parameters:

  • uri (String, URI::HTTPS)

    The https:// or git@HOST:PATH SSH URI

  • path (String)

    The path to where the repository will be cloned to.

  • branch (String, nil) (defaults to: nil)

    The git branch to pull.

  • tag (Boolean) (defaults to: nil)

    Controls whether to pull git tags in addition to the git commits.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for clone.

Returns:

Raises:



132
133
134
135
136
137
138
139
140
# File 'lib/ronin/repos/repository.rb', line 132

def self.install(uri,path, branch: nil, tag: nil, **kwargs)
  repo = clone(uri,path, **kwargs)

  if branch || tag
    repo.checkout(branch || tag)
  end

  return repo
end

Instance Method Details

#checkout(branch_or_tag) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks out the git branch or tag.

Parameters:

  • branch_or_tag (String)

    The branch or tag name to checkout.

Returns:

  • (true)

    Indicates that the git command executed successfully.

Raises:



215
216
217
218
219
220
221
222
223
224
# File 'lib/ronin/repos/repository.rb', line 215

def checkout(branch_or_tag)
  Dir.chdir(@path) do
    case system('git','checkout',branch_or_tag)
    when nil
      raise(CommandNotInstalled,"git is not installed")
    when false
      raise(CommandFailed,"command failed: git checkout #{branch_or_tag}")
    end
  end
end

#deleteObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes the repository directory.



255
256
257
# File 'lib/ronin/repos/repository.rb', line 255

def delete
  FileUtils.rm_rf(@path)
end

#find_file(relative_path) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds a file within the repository.

Examples:

repo.find_file("wordlists/wordlist.txt")
# => "/home/user/.cache/ronin-repos/foo-repo/wordlists/wordlist.txt"

Parameters:

  • relative_path (String)

    The relative path of the file.

Returns:

  • (String, nil)

    The absolute path of the matching file or nil if no matching file could be found.



312
313
314
315
316
317
318
# File 'lib/ronin/repos/repository.rb', line 312

def find_file(relative_path)
  path = join(relative_path)

  if File.file?(path)
    return path
  end
end

#glob(pattern, &block) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds all files in the repository that matches the glob pattern.

Examples:

repo.glob("wordlists/*.txt")
# => ["/home/user/.cache/ronin-repos/foo-repo/wordlists/cities.txt",
#     "/home/user/.cache/ronin-repos/foo-repo/wordlists/states.txt"]

Parameters:

  • pattern (String)

    The file glob pattern to search for.

Returns:

  • (Array<String>)

    The absolute paths to the files that match the glob pattern.



334
335
336
337
338
339
340
341
# File 'lib/ronin/repos/repository.rb', line 334

def glob(pattern,&block)
  path    = join(pattern)
  matches = Dir.glob(path)

  if block then matches.each(&block)
  else          matches
  end
end

#has_directory?(relative_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if the repository contains the directory.

Parameters:

  • relative_path (String)

    The relative path of the directory.

Returns:

  • (Boolean)

    Indicates whether the repository contains the directory or not.



294
295
296
# File 'lib/ronin/repos/repository.rb', line 294

def has_directory?(relative_path)
  File.directory?(join(relative_path))
end

#has_file?(relative_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if the repository contains the file.

Parameters:

  • relative_path (String)

    The relative path of the file.

Returns:

  • (Boolean)

    Indicates whether the repository contains the file or not.



281
282
283
# File 'lib/ronin/repos/repository.rb', line 281

def has_file?(relative_path)
  File.file?(join(relative_path))
end

#join(relative_path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a relative path to an absolute path.

Parameters:

  • relative_path (String)

    The relative path of the file.

Returns:

  • (String)

    The absolute path with respect to the repository.



268
269
270
# File 'lib/ronin/repos/repository.rb', line 268

def join(relative_path)
  File.join(@path,relative_path)
end

#last_updated_atTime

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines when the repository was last updated.

Returns:

  • (Time)

    The timestamp of the last commit will be returned.

Since:

  • 0.2.0



164
165
166
167
168
# File 'lib/ronin/repos/repository.rb', line 164

def last_updated_at
  Dir.chdir(@path) do
    Time.parse(`git log --date=iso8601 --pretty="%cd" -1`)
  end
end

#list_files(pattern = '{**/}*.*') ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lists the paths within the repository.

Examples:

repo.list_files('exploits/{**/}*.rb')
# => ["exploits/exploit1.rb", "exploits/exploit2.rb"]

Parameters:

  • pattern (String) (defaults to: '{**/}*.*')

    The optional glob pattern to use to list specific files.

Returns:

  • (Array<String>)

    The matching paths within the repository.



356
357
358
# File 'lib/ronin/repos/repository.rb', line 356

def list_files(pattern='{**/}*.*')
  Dir.glob(pattern, base: @path)
end

#pull(remote: 'origin', branch: nil, tags: nil) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pulls down new git commits.

Parameters:

  • remote (String) (defaults to: 'origin')

    The git remote to pull from.

  • branch (String, nil) (defaults to: nil)

    The git branch to pull.

  • tags (Boolean) (defaults to: nil)

    Controls whether to pull git tags in addition to the git commits.

Returns:

  • (true)

    Indicates that the git command executed successfully.

Raises:



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/ronin/repos/repository.rb', line 187

def pull(remote: 'origin', branch: nil, tags: nil)
  args = []
  args << '--tags' if tags

  args << remote.to_s
  args << branch.to_s if branch

  Dir.chdir(@path) do
    case system('git','pull',*args)
    when nil
      raise(CommandNotInstalled,"git is not installed")
    when false
      raise(CommandFailed,"command failed: git pull #{args.join(' ')}")
    end
  end
end

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the repository to a String.

Returns:

  • (String)

    The name of the repository.



366
367
368
# File 'lib/ronin/repos/repository.rb', line 366

def to_s
  @name
end

#update(branch: nil, tag: nil, **kwargs) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Updates the repository.

Parameters:

  • branch (String, nil) (defaults to: nil)

    The optional git branch to update from.

  • tag (String, nil) (defaults to: nil)

    The optional git tag to update to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for #pull.

Returns:

  • (true)

    Indicates that the git commands executed successfully.

Raises:



244
245
246
247
248
249
250
# File 'lib/ronin/repos/repository.rb', line 244

def update(branch: nil, tag: nil, **kwargs)
  pull(branch: branch, tags: branch.nil?, **kwargs)

  if branch || tag
    checkout(branch || tag)
  end
end

#urlString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The git URL of the repository.

Returns:

  • (String)

    The git: or https:// URL for the repository.

Since:

  • 0.2.0



150
151
152
153
154
# File 'lib/ronin/repos/repository.rb', line 150

def url
  Dir.chdir(@path) do
    `git remote get-url origin`.chomp
  end
end