dpu 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dpu/version.rb +1 -1
  3. data/lib/dpu.rb +48 -11
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35801d7b29d43596f69f0716053617ce26a677abf15bf311344f88b7c4b577eb
4
- data.tar.gz: 895ce16b1cec4f36696cc1a733391344fb7a95bd1f817efd5c19dbd3fe7b8c2d
3
+ metadata.gz: 7e767334841a8cf8e60953a8edde125a40e62a488bd6837ae8f798ff0f9c468a
4
+ data.tar.gz: 79c11100a4390dbb54bb6186facd6226b3c981fbb14d98512a5bc772c654fe45
5
5
  SHA512:
6
- metadata.gz: 87d647fcb8d0045905b7b54fdaf328af437b9157e4db6a597cee26cb4f5f7e2f687fd2bba0719833ae00229ffe96a31efe4e8bce9f8800501486160470e87a21
7
- data.tar.gz: 6ffb7f6c8abb157673a69ad184831d3f34e9935635716bde26fd16214bfd46bc6d16f7fa02cd9f453106e5b6f30dc3e71d7ab5872fbff97b2ac0ce43f641ef78
6
+ metadata.gz: 3cd1dd74f4222f3962abee264da6d4ac90ef9f5bfb674ccfeb78c03de5c8b575b52897c47a44a3f3ec93ff5abb38b97cfca251069d06ebd2c4adb5be0eebc6da
7
+ data.tar.gz: d19919ff4b281af1b7604cdd586bece18e9f1a94fc519d947647c3196743c464d9c7b3a03edb92f0749f81857aa97a2dbbdbda8eacb1c39a1c45bfca9a79b1fa
data/lib/dpu/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dpu
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/dpu.rb CHANGED
@@ -8,7 +8,13 @@ module Dpu
8
8
 
9
9
  class << self
10
10
  GITHUB_REPOSITORY_URI_TEMPLATE = "https://github.com/%{account_name}/%{repository_name}"
11
- REMOTE_URL_PATTERN = [
11
+ SOURCEHUT_REPOSITORY_URI_TEMPLATE = "https://git.sr.ht/~%{account_name}/%{repository_name}"
12
+ REPOSITORY_URI_TEMPLATES = {
13
+ github: GITHUB_REPOSITORY_URI_TEMPLATE,
14
+ sourcehut: SOURCEHUT_REPOSITORY_URI_TEMPLATE,
15
+ }
16
+
17
+ GITHUB_REMOTE_URL_PATTERN = [
12
18
  %r{\Agit://github\.com/(?<account_name>[^/]+)/(?<repository_name>.+)(?=\.git)},
13
19
  %r{\Ahttps?://github\.com/(?<account_name>[^/]+)/(?<repository_name>.+)(?=\.git)},
14
20
  %r{\Agit@github\.com:(?<account_name>[^/]+)/(?<repository_name>.+)(?=\.git)},
@@ -16,19 +22,37 @@ module Dpu
16
22
  ].then { |patterns|
17
23
  Regexp.union(*patterns)
18
24
  }
25
+ SOURCEHUT_REMOTE_URL_PATTERN = [
26
+ %r{\Ahttps://git\.sr\.ht/\~(?<account_name>[^/]+)/(?<repository_name>.+)},
27
+ %r{\Agit@git\.sr\.ht:\~(?<account_name>[^/]+)/(?<repository_name>.+)},
28
+ ].then { |patterns|
29
+ Regexp.union(*patterns)
30
+ }
31
+ REMOTE_URL_PATTERNS = {
32
+ github: GITHUB_REMOTE_URL_PATTERN,
33
+ sourcehut: SOURCEHUT_REMOTE_URL_PATTERN,
34
+ }
35
+
36
+ REF_PREFIX = {
37
+ github: 'blob',
38
+ sourcehut: 'tree',
39
+ }
19
40
 
20
41
  def determine_permanent_uri(path_or_link, start_line_number = nil, end_line_number = nil)
21
42
  path = path_or_link.realpath
22
43
  relative_path = determine_relative_path(path)
23
44
 
45
+ remote_url = get_remote_url(path)
46
+ vcs_service = determine_vcs_service(remote_url)
47
+
24
48
  permanent_uri_parts = [
25
- determine_repository_uri(path),
26
- "blob",
49
+ determine_repository_uri(vcs_service, remote_url),
50
+ REF_PREFIX[vcs_service],
27
51
  find_same_content_version(path, relative_path) || determine_commit_id(path),
28
52
  relative_path,
29
53
  ]
30
54
  permanent_uri = URI(permanent_uri_parts.join("/"))
31
- permanent_uri.fragment = determine_fragment(start_line_number, end_line_number)
55
+ permanent_uri.fragment = determine_fragment(vcs_service, start_line_number, end_line_number)
32
56
  return permanent_uri
33
57
  end
34
58
 
@@ -49,16 +73,28 @@ module Dpu
49
73
  return relative_path
50
74
  end
51
75
 
52
- def determine_repository_uri(path)
53
- stdout = run_command("git remote get-url origin", chdir: path.dirname)
54
- repository_http_or_ssh_url = stdout.chomp
76
+ def get_remote_url(path)
77
+ return run_command("git remote get-url origin", chdir: path.dirname).chomp
78
+ end
79
+
80
+ def determine_vcs_service(repository_http_or_ssh_url)
81
+ if GITHUB_REMOTE_URL_PATTERN.match?(repository_http_or_ssh_url)
82
+ return :github
83
+ end
84
+ if SOURCEHUT_REMOTE_URL_PATTERN.match?(repository_http_or_ssh_url)
85
+ return :sourcehut
86
+ end
87
+
88
+ raise "unknown VCS service: #{repository_http_or_ssh_url}"
89
+ end
55
90
 
56
- md = REMOTE_URL_PATTERN.match(repository_http_or_ssh_url)
91
+ def determine_repository_uri(vcs_service, repository_http_or_ssh_url)
92
+ md = REMOTE_URL_PATTERNS[vcs_service].match(repository_http_or_ssh_url)
57
93
  if !md
58
94
  return URI(repository_http_or_ssh_url)
59
95
  end
60
96
 
61
- url = GITHUB_REPOSITORY_URI_TEMPLATE % {
97
+ url = REPOSITORY_URI_TEMPLATES[vcs_service] % {
62
98
  account_name: md[:account_name],
63
99
  repository_name: md[:repository_name],
64
100
  }
@@ -88,10 +124,11 @@ module Dpu
88
124
  return same_content_version
89
125
  end
90
126
 
91
- def determine_fragment(start_line_number, end_line_number)
127
+ def determine_fragment(vcs_service, start_line_number, end_line_number)
92
128
  return nil if !start_line_number
93
129
  return "L#{start_line_number}" if !end_line_number || start_line_number == end_line_number
94
- return "L#{start_line_number}-L#{end_line_number}"
130
+ return "L#{start_line_number}-L#{end_line_number}" if vcs_service == :github
131
+ return "L#{start_line_number}-#{end_line_number}" if vcs_service == :sourcehut
95
132
  end
96
133
  end
97
134
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dpu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya.Nishida.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-09 00:00:00.000000000 Z
11
+ date: 2023-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debug