-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby_git.rb
More file actions
166 lines (156 loc) · 5.69 KB
/
ruby_git.rb
File metadata and controls
166 lines (156 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# frozen_string_literal: true
require_relative 'ruby_git/command_line'
require_relative 'ruby_git/errors'
require_relative 'ruby_git/option_validators'
require_relative 'ruby_git/repository'
require_relative 'ruby_git/status'
require_relative 'ruby_git/version'
require_relative 'ruby_git/worktree'
require 'logger'
# The RubyGit module provides a Ruby API that is an object-oriented wrapper around
# the `git` command line. It is intended to make automating both simple and complex Git
# interactions easier. To accomplish this, it ties each action you can do with `git` to
# the type of object that action operates on.
#
# There are three main objects in RubyGit:
# * {Worktree}: The directory tree of actual checked
# out files. The working tree normally contains the contents of the HEAD commit's
# tree, plus any local changes that you have made but not yet committed.
# * Index: The index is used as a staging area between your
# working tree and your repository. You can use the index to build up a set of changes
# that you want to commit together. When you create a commit, what is committed is what is
# currently in the index, not what is in your working directory.
# * Repository: The repository stores the files in a project,
# their history, and other meta data like commit information, tags, and branches.
#
# @api public
#
module RubyGit
@logger = Logger.new(nil)
class << self
# The logger used by the RubyGit gem (Logger.new(nil) by default)
#
# @example Using the logger
# RubyGit.logger.debug('Debug message')
#
# @example Setting the logger
# require 'logger'
# require 'stringio'
# log_device = StringIO.new
# RubyGit.logger = Logger.new(log_device, level: Logger::DEBUG)
# RubyGit.logger.debug('Debug message')
# log_device.string.include?('Debug message')
# => true
#
# @return [Logger] the logger used by the RubyGit gem
#
attr_accessor :logger
end
@binary_path = 'git'
class << self
# The path to the git binary used by the RubyGit gem
#
# * If the path is a command name, the command is search for in the PATH.
#
# * If the path is a relative path, it relative to the current directory (not
# recommended as this will change as the current directory is changed).
#
# * If the path is an absolute path, it is used as is.
#
# @example
# RubyGit.binary_path
# => "git"
#
# @return [String]
#
attr_accessor :binary_path
end
# Create an empty Git repository under the root working tree `path`
#
# If the repository already exists, it will not be overwritten.
#
# @see https://git-scm.com/docs/git-init git-init
#
# @example
# worktree = Worktree.init(worktree_path, initial_branch: 'main')
#
# @param worktree_path [String] the root path of a worktree
# @param initial_branch [String] the initial branch in the newly created repository
#
# @raise [RubyGit::Error] if worktree_path is not a directory
#
# @return [RubyGit::Worktree] the worktree whose root is at `path`
#
def self.init(worktree_path, initial_branch:)
RubyGit::Worktree.init(worktree_path, initial_branch:)
end
# Open an existing Git working tree that contains worktree_path
#
# @see https://git-scm.com/docs/git-open git-open
#
# @example
# worktree = Worktree.open(worktree_path)
#
# @param [String] worktree_path the root path of a worktree
#
# @raise [RubyGit::Error] if `worktree_path` does not exist, is not a directory, or is not within
# a Git worktree.
#
# @return [RubyGit::Worktree] the worktree that contains `worktree_path`
#
def self.open(worktree_path)
RubyGit::Worktree.open(worktree_path)
end
# Copy the remote repository and checkout the default branch
#
# Clones the repository referred to by `repository_url` into a newly created
# directory, creates remote-tracking branches for each branch in the cloned repository,
# and checks out the default branch in the worktree whose root directory is `to_path`.
#
# @see https://git-scm.com/docs/git-clone git-clone
#
# @example Using default for Worktree path
# FileUtils.pwd
# => "/Users/jsmith"
# worktree = Worktree.clone('https://github.com/main-branch/ruby_git.git')
# worktree.path
# => "/Users/jsmith/ruby_git"
#
# @example Using a specified worktree_path
# FileUtils.pwd
# => "/Users/jsmith"
# worktree_path = '/tmp/project'
# worktree = Worktree.clone('https://github.com/main-branch/ruby_git.git', to_path: worktree_path)
# worktree.path
# => "/tmp/project"
#
# @param [String] repository_url a reference to a Git repository
#
# @param [String] to_path where to put the checked out working tree once the repository is cloned
#
# `to_path` will be created if it does not exist. An error is raised if `to_path` exists and
# not an empty directory.
#
# @raise [RubyGit::Error] if (1) `repository_url` is not valid or does not point to a valid repository OR
# (2) `to_path` is not an empty directory.
#
# @return [RubyGit::Worktree] the working tree checked out from the cloned repository
#
def self.clone(repository_url, to_path: '')
RubyGit::Worktree.clone(repository_url, to_path:)
end
# The version of git referred to by the path
#
# @example for version 2.28.0
# RubyGit.binary_version #=> [2, 28, 0]
#
# @return [Array<Integer>] an array of integers representing the version.
#
# @raise [RuntimeError] if path was not set via `path=` and either PATH is not set
# or git was not found on the path.
#
def self.binary_version
version_string = RubyGit::CommandLine.run('version').stdout[/\d+\.\d+(\.\d+)+/]
version_string.split('.').collect(&:to_i)
end
end