swhid 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: 17e5d6a13e7f6d44bed90224e6ff6b1418bd9501b7bd14c1ef00d5d7adbbeb3c
4
- data.tar.gz: 712d0c4136e0ac2c5d11b38b73cbf77d75bbf87b188fc998a3f716e4f9e8ff98
3
+ metadata.gz: ee71514f26b0e91e02344d741fdfb56dc4aa5d9314e30dcc3c5d85caa68f8973
4
+ data.tar.gz: 4f087cd83b505e88543e991aa24d8fb88368596e4b108128de52d562e2d81025
5
5
  SHA512:
6
- metadata.gz: 7fa9985d872d26ade840cdb509319fe27795dbd7bbac18b9a4141eb291dcee55cdeb5be31ad94a2fd557b2d676124016c955303bee9a7b4dac1ff698fc22ddc9
7
- data.tar.gz: e99b1253233fd0862a7b57da14ca6367036e5cb50b4d153ffe154165be2635bf8f7579a5a8d3ae10757972dfc511d591c9485bf66cca001db8336b5dc08d4349
6
+ metadata.gz: a94007e0ee145b967ec7f2ac0007dcf996207990bec1af8ce3a63fc4f0ddd0e4a59afe6bf1385a4e0f9b9b033a3f0301657835751e653b1f1ed5fb457a3a6b90
7
+ data.tar.gz: 0f8a27c61f50d91519564fe4f7a6483b23415c600ffc6d49df26141388faf71ab547aa06e56450a6df9d95b91d6560c3c773a65c80682e6d87d5538858fd5e68
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2026-01-12
4
+
5
+ ### Added
6
+ - Windows support for directory SWHID computation
7
+ - File permissions read from Git index on Windows where filesystem permissions unavailable
8
+ - Optional `permissions:` parameter for `FromFilesystem.from_directory_path` to pass explicit file modes
9
+ - CI testing on Windows (Ruby 3.4 and 4.0)
10
+
11
+ ### Changed
12
+ - Archive extraction in tests now uses pure Ruby (Zlib/TarReader/Zip) instead of shell commands
13
+
14
+ ## [0.3.1] - 2025-11-23
15
+
16
+ ### Fixed
17
+ - Snapshot implementation now includes HEAD symbolic reference
18
+ - Extra headers extraction for signed commits (gpgsig, mergetag, etc.)
19
+ - Tag-of-tag support (tags pointing to other tag objects)
20
+ - Extra headers extraction for signed tags
21
+
3
22
  ## [0.3.0] - 2025-11-23
4
23
 
5
24
  - `directory` CLI command - Generate SWHID for directory from filesystem
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andrew Nesbitt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -261,6 +261,36 @@ SWHIDs can include optional qualifiers to provide context:
261
261
 
262
262
  The hash computation for content, directory, revision, and release objects is compatible with Git's object hashing. This means you can use this gem to compute the same hashes that Git would produce for the same objects.
263
263
 
264
+ ## Windows Support
265
+
266
+ The library works on Windows with some considerations for file permissions.
267
+
268
+ Directory SWHIDs include file permission bits (executable vs non-executable). Windows doesn't store Unix-style permissions, so the library uses these strategies:
269
+
270
+ 1. **Files in a Git repository**: Permissions are read from the Git index, which works correctly on all platforms.
271
+
272
+ 2. **Extracted archives**: When extracting tar.gz files, pass the permissions from tar headers:
273
+
274
+ ```ruby
275
+ require 'zlib'
276
+ require 'rubygems/package'
277
+
278
+ permissions = {}
279
+ Zlib::GzipReader.open(tarball) do |gz|
280
+ Gem::Package::TarReader.new(gz) do |tar|
281
+ tar.each do |entry|
282
+ next unless entry.file?
283
+ File.binwrite(dest_path, entry.read)
284
+ permissions[dest_path] = entry.header.mode
285
+ end
286
+ end
287
+ end
288
+
289
+ swhid = Swhid::FromFilesystem.from_directory_path(extract_dir, permissions: permissions)
290
+ ```
291
+
292
+ 3. **Other directories**: Without Git or explicit permissions, Windows will treat all files as non-executable. If the directory contains executable files, the hash will differ from Linux/macOS.
293
+
264
294
  ## Development
265
295
 
266
296
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -274,3 +304,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/andrew
274
304
  ## Code of Conduct
275
305
 
276
306
  Everyone interacting in the Swhid project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/andrew/swhid/blob/main/CODE_OF_CONDUCT.md).
307
+
308
+ ## License
309
+
310
+ [MIT](LICENSE)
@@ -5,15 +5,23 @@ require "find"
5
5
 
6
6
  module Swhid
7
7
  module FromFilesystem
8
- def self.from_directory_path(path)
8
+ def self.from_directory_path(path, git_repo: nil, permissions: nil)
9
9
  raise ArgumentError, "Path does not exist: #{path}" unless File.exist?(path)
10
10
  raise ArgumentError, "Path is not a directory: #{path}" unless File.directory?(path)
11
11
 
12
- entries = build_entries(path)
12
+ git_repo ||= discover_git_repo(path)
13
+ entries = build_entries(path, git_repo: git_repo, permissions: permissions)
13
14
  Swhid.from_directory(entries)
14
15
  end
15
16
 
16
- def self.build_entries(dir_path)
17
+ def self.discover_git_repo(path)
18
+ require "rugged"
19
+ Rugged::Repository.discover(path)
20
+ rescue Rugged::RepositoryError, Rugged::OSError
21
+ nil
22
+ end
23
+
24
+ def self.build_entries(dir_path, git_repo: nil, permissions: nil)
17
25
  entries = []
18
26
 
19
27
  Dir.foreach(dir_path) do |name|
@@ -28,9 +36,9 @@ module Swhid
28
36
  target_hash = Swhid.from_content(target_content).object_hash
29
37
  { name: name, type: :symlink, target: target_hash }
30
38
  elsif stat.directory?
31
- target_swhid = from_directory_path(full_path)
39
+ target_swhid = from_directory_path(full_path, git_repo: git_repo, permissions: permissions)
32
40
  { name: name, type: :dir, target: target_swhid.object_hash }
33
- elsif stat.executable?
41
+ elsif file_executable?(full_path, stat, git_repo, permissions)
34
42
  content = File.binread(full_path)
35
43
  target_hash = Swhid.from_content(content).object_hash
36
44
  { name: name, type: :exec, target: target_hash }
@@ -45,5 +53,42 @@ module Swhid
45
53
 
46
54
  entries
47
55
  end
56
+
57
+ def self.file_executable?(full_path, stat, git_repo, permissions = nil)
58
+ # Check explicit permissions map first (from tar extraction, etc.)
59
+ if permissions
60
+ mode = permissions[full_path] || permissions[File.expand_path(full_path)]
61
+ return (mode & 0o111) != 0 if mode
62
+ end
63
+
64
+ # Check Git index for tracked files
65
+ if git_repo
66
+ relative_path = relative_path_in_repo(full_path, git_repo)
67
+ if relative_path
68
+ entry = git_repo.index[relative_path]
69
+ if entry
70
+ mode = entry[:mode]
71
+ return (mode & 0o111) != 0
72
+ end
73
+ end
74
+ end
75
+
76
+ # Fall back to filesystem
77
+ stat.executable?
78
+ end
79
+
80
+ def self.relative_path_in_repo(full_path, git_repo)
81
+ repo_workdir = git_repo.workdir
82
+ return nil unless repo_workdir
83
+
84
+ full_path = File.expand_path(full_path)
85
+ repo_workdir = File.expand_path(repo_workdir)
86
+
87
+ return nil unless full_path.start_with?(repo_workdir)
88
+
89
+ relative = full_path.sub(repo_workdir, "")
90
+ relative = relative[1..] if relative.start_with?("/") || relative.start_with?("\\")
91
+ relative.tr("\\", "/")
92
+ end
48
93
  end
49
94
  end
@@ -22,8 +22,8 @@ module Swhid
22
22
  message: commit.message
23
23
  }
24
24
 
25
- # Extract extra headers if present
26
- extra_headers = extract_extra_headers(commit)
25
+ # Extract extra headers if present (like gpgsig, svn headers, etc)
26
+ extra_headers = extract_extra_headers(repo, commit)
27
27
  metadata[:extra_headers] = extra_headers unless extra_headers.empty?
28
28
 
29
29
  Swhid.from_revision(metadata)
@@ -42,6 +42,7 @@ module Swhid
42
42
  if tag_obj.is_a?(Rugged::Tag::Annotation)
43
43
  target_type = case tag_obj.target
44
44
  when Rugged::Commit then "rev"
45
+ when Rugged::Tag::Annotation then "rel"
45
46
  when Rugged::Tree then "dir"
46
47
  when Rugged::Blob then "cnt"
47
48
  else "rev"
@@ -59,6 +60,10 @@ module Swhid
59
60
  metadata[:author_timezone] = format_timezone(tag_obj.tagger[:time])
60
61
  end
61
62
 
63
+ # Extract extra headers if present (like gpgsig for signed tags)
64
+ extra_headers = extract_tag_extra_headers(repo, tag_obj)
65
+ metadata[:extra_headers] = extra_headers unless extra_headers.empty?
66
+
62
67
  Swhid.from_release(metadata)
63
68
  else
64
69
  # Lightweight tag - points directly to commit
@@ -70,12 +75,27 @@ module Swhid
70
75
  repo = Rugged::Repository.new(repo_path)
71
76
  branches = []
72
77
 
78
+ # Check for HEAD first
79
+ head_path = File.join(repo.path, "HEAD")
80
+ if File.exist?(head_path)
81
+ head_content = File.read(head_path).strip
82
+ if head_content.start_with?("ref:")
83
+ # HEAD is a symbolic ref
84
+ target_ref = head_content.sub("ref: ", "")
85
+ branches << {
86
+ name: "HEAD",
87
+ target_type: "alias",
88
+ target: target_ref
89
+ }
90
+ end
91
+ end
92
+
73
93
  # Get all references (branches and tags)
74
94
  repo.references.each do |ref|
75
95
  ref_name = ref.name
76
96
 
77
97
  if ref.type == :symbolic
78
- # This is an alias (like HEAD pointing to refs/heads/main)
98
+ # This is an alias (symbolic ref)
79
99
  target_ref_name = ref.target
80
100
  branches << {
81
101
  name: ref_name,
@@ -125,11 +145,75 @@ module Swhid
125
145
  format("%s%02d%02d", sign, hours, minutes)
126
146
  end
127
147
 
128
- def self.extract_extra_headers(commit)
148
+ def self.extract_extra_headers(repo, commit)
129
149
  # Rugged doesn't expose extra headers directly
130
- # We would need to parse the raw commit object for this
131
- # For now, return empty array
132
- []
150
+ # We need to parse the raw commit object
151
+ raw_data = repo.read(commit.oid).data
152
+ lines = raw_data.split("\n")
153
+
154
+ extra_headers = []
155
+ in_headers = true
156
+
157
+ lines.each do |line|
158
+ # Stop when we hit the blank line before the message
159
+ if line.empty?
160
+ in_headers = false
161
+ next
162
+ end
163
+
164
+ next unless in_headers
165
+
166
+ # Skip standard headers
167
+ next if line.start_with?("tree ", "parent ", "author ", "committer ")
168
+
169
+ # Extract extra headers (like gpgsig, mergetag, svn-repo-uuid, etc)
170
+ if line.start_with?(" ")
171
+ # Continuation of previous header
172
+ if extra_headers.any?
173
+ extra_headers.last[1] += "\n#{line[1..]}"
174
+ end
175
+ elsif line.include?(" ")
176
+ key, value = line.split(" ", 2)
177
+ extra_headers << [key, value]
178
+ end
179
+ end
180
+
181
+ extra_headers
182
+ end
183
+
184
+ def self.extract_tag_extra_headers(repo, tag)
185
+ # Parse raw tag object for extra headers
186
+ raw_data = repo.read(tag.oid).data
187
+ lines = raw_data.split("\n")
188
+
189
+ extra_headers = []
190
+ in_headers = true
191
+
192
+ lines.each do |line|
193
+ # Stop when we hit the blank line before the message
194
+ if line.empty?
195
+ in_headers = false
196
+ next
197
+ end
198
+
199
+ next unless in_headers
200
+
201
+ # Skip standard tag headers
202
+ next if line.start_with?("object ", "type ", "tag ", "tagger ")
203
+
204
+ # Extract extra headers (like gpgsig for signed tags)
205
+ if line.start_with?(" ")
206
+ # Continuation of previous header
207
+ if extra_headers.any?
208
+ extra_headers.last[1] += "\n#{line[1..]}"
209
+ end
210
+ elsif line.include?(" ")
211
+ key, value = line.split(" ", 2)
212
+ extra_headers << [key, value]
213
+ end
214
+ end
215
+
216
+ extra_headers
133
217
  end
134
218
  end
135
219
  end
@@ -39,6 +39,11 @@ module Swhid
39
39
  lines << author_line
40
40
  end
41
41
 
42
+ extra_headers = metadata[:extra_headers] || []
43
+ extra_headers.each do |key, value|
44
+ lines << format_header_line(key, value)
45
+ end
46
+
42
47
  result = lines.join("\n") + "\n"
43
48
 
44
49
  if metadata[:message]
@@ -54,6 +59,11 @@ module Swhid
54
59
  "#{prefix} #{person_escaped} #{timestamp} #{tz}"
55
60
  end
56
61
 
62
+ def self.format_header_line(key, value)
63
+ value_escaped = value.gsub("\n", "\n ")
64
+ "#{key} #{value_escaped}"
65
+ end
66
+
57
67
  def self.extract_target(target)
58
68
  case target
59
69
  when Hash
data/lib/swhid/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Swhid
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swhid
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
  - Andrew Nesbitt
@@ -35,6 +35,7 @@ extra_rdoc_files: []
35
35
  files:
36
36
  - CHANGELOG.md
37
37
  - CODE_OF_CONDUCT.md
38
+ - LICENSE
38
39
  - README.md
39
40
  - Rakefile
40
41
  - benchmark/benchmark.rb
@@ -72,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  - !ruby/object:Gem::Version
73
74
  version: '0'
74
75
  requirements: []
75
- rubygems_version: 3.6.9
76
+ rubygems_version: 4.0.1
76
77
  specification_version: 4
77
78
  summary: Generate and parse SoftWare Hash IDentifiers (SWHIDs)
78
79
  test_files: []