gitw 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64596c0a6c9d3e0f03c3735cf4f3208267b66676ffcdef572416e46c993652ae
4
- data.tar.gz: f4cc05abf07727acd192a4a92af62f4436921d89b7664c883d4ed5cfa738d8a1
3
+ metadata.gz: f8344d45b609fcbbeab7b40f313ff1857c0d13ecef343d0108e7153a35731652
4
+ data.tar.gz: b0bba996c5fc7c43681adf15f8f8496d7541b44e38901969ca8f36f8e5597d1d
5
5
  SHA512:
6
- metadata.gz: 802bd89fb34f1c8a04475a4b417a841b8ef27ed42ead9f495d691fa5cfadfe95249066d43dbfc9f99c262d257960f66dafb47d48c57a4f3d26eaf371994ff2b9
7
- data.tar.gz: a7baf4ab64fcaa695d9137ddf9be34aa4ae8098c828b8c7325ef5c437316a8f079087f4bdd22ada2dfbd730649492c95473f2dba9b820fdbf494b3178e0ed097
6
+ metadata.gz: 95032c399dad23d0dc5eb725a3a270ed11e2f958b893f661daf4dd07a62ac4fd2fa23a5dfac0edec8d01d1077d3ab38fa21bc499e4a7343e6679a113fec14211
7
+ data.tar.gz: 6a0179b3a3810bab724d5daa4cddc307d3ecf0ef7eba19aa02ad37ea9b142f2ba266b5a71d8c1330485cfac31d9e0ee1144333155afc3e252079890ceffd877c
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module Gitw
6
+ module Git
7
+ # git repository url
8
+ class URL
9
+ SSH_RE = %r{
10
+ ^
11
+ (?:(?<user>[^@/]+)@)?
12
+ (?<host>[^:/]+)
13
+ :(?!/)
14
+ (?<path>.*?)
15
+ $
16
+ }x.freeze
17
+
18
+ attr_reader :raw_url, :url, :user, :host, :path
19
+
20
+ def initialize(raw_url)
21
+ @raw_url = raw_url
22
+ @url = nil
23
+ @user = nil
24
+ @host = nil
25
+ @path = nil
26
+
27
+ parse
28
+ end
29
+
30
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
31
+ def parse
32
+ if (match = SSH_RE.match(raw_url))
33
+ @user = match[:user]
34
+ @host = match[:host]
35
+ @path = match[:path]
36
+ @url = raw_url.clone
37
+ elsif (uri = URI.parse(raw_url))
38
+ @user = uri.user
39
+ @host = uri.host
40
+ @path = uri.path
41
+ @url = uri.to_s
42
+ end
43
+ @path = @path.chomp('.git').sub(%r{^/+}, '').sub(%r{/+$}, '')
44
+ end
45
+
46
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
47
+ def path_dirname
48
+ File.dirname(path)
49
+ end
50
+
51
+ def path_basename
52
+ File.basename(path)
53
+ end
54
+
55
+ def to_s
56
+ url.to_s
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/gitw/git_exe.rb CHANGED
@@ -5,6 +5,7 @@ require_relative 'git_error'
5
5
  require_relative 'git_opts'
6
6
  require_relative 'git/status'
7
7
  require_relative 'git/remote_ref'
8
+ require_relative 'git/url'
8
9
 
9
10
  # rubocop:disable Metrics/ClassLength, Metrics/MethodLength, Naming/PredicateName
10
11
  module Gitw
@@ -116,8 +117,13 @@ module Gitw
116
117
  end
117
118
 
118
119
  def toplevel(git_options: nil)
119
- git_dir = rev_parse(git_options: git_options, show_toplevel: nil)
120
- git_dir.strip
120
+ toplevel_dir = rev_parse(git_options: git_options, show_toplevel: nil)
121
+ toplevel_dir.strip
122
+ end
123
+
124
+ def absolute_git_dir(git_options: nil)
125
+ dir = rev_parse(git_options: git_options, absolute_git_dir: nil)
126
+ dir.strip
121
127
  end
122
128
 
123
129
  def is_inside_git_dir(git_options: nil)
@@ -1 +1,203 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ require 'gitw/git_exe'
6
+
7
+ module Gitw
8
+ # git repository service
9
+ class Repository
10
+ extend Forwardable
11
+
12
+ attr_reader :base_dir
13
+
14
+ def initialize(base_dir = '.', **options)
15
+ @base_dir = File.expand_path(base_dir)
16
+ @git_dir = options[:git_dir] || options['git_dir']
17
+ @worktree_dir = options[:worktree_dir] || options['worktree_dir']
18
+ end
19
+
20
+ def git_dir
21
+ return @git_dir if @git_dir
22
+ end
23
+
24
+ def worktree_dir
25
+ return @worktree_dir if @worktree_dir
26
+ end
27
+
28
+ def in_repository?
29
+ return false unless File.directory?(base_dir)
30
+ return true if inside_work_tree? || inside_git_dir?
31
+
32
+ false
33
+ end
34
+
35
+ def inside_work_tree?
36
+ git.is_inside_work_tree
37
+ rescue Gitw::GitError
38
+ false
39
+ end
40
+
41
+ def inside_git_dir?
42
+ git.is_inside_git_dir
43
+ rescue Gitw::GitError
44
+ false
45
+ end
46
+
47
+ def toplevel
48
+ git.toplevel
49
+ rescue Gitw::GitError
50
+ nil
51
+ end
52
+
53
+ def absolute_git_dir
54
+ git.absolute_git_dir
55
+ rescue Gitw::GitError
56
+ nil
57
+ end
58
+
59
+ def bare?
60
+ git.is_bare_repository
61
+ rescue Gitw::GitError
62
+ false
63
+ end
64
+
65
+ def root_dir
66
+ return absolute_git_dir if bare?
67
+
68
+ git.toplevel
69
+ rescue Gitw::GitError
70
+ nil
71
+ end
72
+
73
+ def init(**options)
74
+ FileUtils.mkdir_p base_dir
75
+ git.init(base_dir, **options)
76
+
77
+ self
78
+ rescue Errno::EACCES, Gitw::GitError
79
+ nil
80
+ end
81
+
82
+ def clone_from(url, **options)
83
+ FileUtils.mkdir_p base_dir
84
+ git(git_base_options).clone(url.to_s, base_dir, **options)
85
+
86
+ self
87
+ rescue Errno::EACCES, Gitw::GitError
88
+ nil
89
+ end
90
+
91
+ def git(options = git_options)
92
+ Dir.chdir(base_dir) do
93
+ Gitw::GitExe.new(options: options)
94
+ end
95
+ end
96
+
97
+ def git_options
98
+ git_base_options.merge(
99
+ {
100
+ dir: base_dir
101
+ }
102
+ ).compact
103
+ end
104
+
105
+ def git_base_options
106
+ {
107
+ git_dir: git_dir,
108
+ work_tree: worktree_dir
109
+ }.compact
110
+ end
111
+
112
+ # def status
113
+ # @status ||= Repository::Status.new(self)
114
+ # end
115
+
116
+ # def remote
117
+ # @remote ||= Repository::Remote.new(self)
118
+ # end
119
+
120
+ # #
121
+
122
+ # ADD_OPTIONS = GitOptions.new()
123
+
124
+ # def add(*files, **options)
125
+ # add_options = ADD_OPTIONS.inline(options)
126
+
127
+ # result = gitexec('add', *add_options, '--', *files)
128
+ # return unless result.success?
129
+
130
+ # self
131
+ # end
132
+
133
+ # COMMIT_OPTIONS = GitOptions.new(
134
+ # GitOption.new(:all, short: :a),
135
+ # GitOption.new(:message, short: :m, has_argument: true),
136
+ # )
137
+
138
+ # def commit(*files, **options)
139
+ # commit_options = COMMIT_OPTIONS.inline(options)
140
+
141
+ # result = gitexec('commit', *commit_options, '--', *files)
142
+ # return unless result.success?
143
+
144
+ # self
145
+ # end
146
+
147
+ # FETCH_OPTIONS = GitOptions.new(
148
+ # GitOption.new(:all),
149
+ # GitOption.new(:tags),
150
+ # )
151
+ # def fetch(repository=nil, refspec=nil, **options)
152
+ # fetch_options = FETCH_OPTIONS.inline(options)
153
+ # result = gitexec('fetch', *fetch_options, repository, refspec)
154
+ # return unless result.success?
155
+
156
+ # self
157
+ # end
158
+
159
+ # PULL_OPTIONS = GitOptions.new(
160
+ # GitOption.new(:all),
161
+ # GitOption.new(:tags),
162
+ # )
163
+ # def pull(repository=nil, refspec=nil, **options)
164
+ # pull_options = PULL_OPTIONS.inline(options)
165
+ # result = gitexec('pull', *pull_options, repository, refspec)
166
+ # return unless result.success?
167
+
168
+ # self
169
+ # end
170
+
171
+ # PUSH_OPTIONS = GitOptions.new(
172
+ # GitOption.new(:all),
173
+ # GitOption.new(:mirror),
174
+ # GitOption.new(:tags),
175
+ # )
176
+ # def push(repository=nil, refspec=nil, **options)
177
+ # push_options = PUSH_OPTIONS.inline(options)
178
+ # result = gitexec('push', *push_options, repository, refspec)
179
+ # return unless result.success?
180
+
181
+ # self
182
+ # end
183
+
184
+ def self.at(directory = '.', **options)
185
+ repository = new(directory, **options)
186
+ return nil unless repository.in_repository?
187
+
188
+ repository
189
+ end
190
+
191
+ def self.init(directory = '.', **options)
192
+ new(directory, **options).init(**options)
193
+ end
194
+
195
+ def self.clone(from, to = nil, **options)
196
+ return unless from
197
+
198
+ from_url = Gitw::Git::URL.new(from)
199
+ to ||= from_url.basename
200
+ new(to, **options).clone_from(from_url, **options)
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitw
4
+ # base RepositoryError exception
5
+ class RepositoryError < StandardError
6
+ end
7
+ end
data/lib/gitw/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gitw
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Tych
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-23 00:00:00.000000000 Z
11
+ date: 2023-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -191,10 +191,12 @@ files:
191
191
  - lib/gitw/exec.rb
192
192
  - lib/gitw/git/remote_ref.rb
193
193
  - lib/gitw/git/status.rb
194
+ - lib/gitw/git/url.rb
194
195
  - lib/gitw/git_error.rb
195
196
  - lib/gitw/git_exe.rb
196
197
  - lib/gitw/git_opts.rb
197
198
  - lib/gitw/repository.rb
199
+ - lib/gitw/repository_error.rb
198
200
  - lib/gitw/version.rb
199
201
  - scripts/console
200
202
  - scripts/setup