files_in_my_diff 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f67804ba5d277fa3cfcf033a7b3be597e4ca42c8622f6ddd86524791d4093d67
4
+ data.tar.gz: 91e13197b9b629379d1dc371cab25da159070f014610a0f07e7e9ce740fef679
5
+ SHA512:
6
+ metadata.gz: 65db1f926f08f2e6c6513b27429988d8a6c8c55092c20b5e357e34e67db451db0eb92eeeba751cdd1ebc4ab2c2d9438785ddab6946565d138e9ca6cef545ebac
7
+ data.tar.gz: b35919945391ba62a8857e7ca20140861f81847f6f626245468570c7094d388bb5398027d60de2ebbe722ddb5c6f56bfe7f5561eea4201bd7c29e39099396f24
data/.rubocop.yml ADDED
@@ -0,0 +1,30 @@
1
+
2
+ require:
3
+ - rubocop-minitest
4
+ - rubocop-rake
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.2
8
+ NewCops: enable
9
+
10
+
11
+ Layout/LineLength:
12
+ Max: 120
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/ModuleFunction:
18
+ Enabled: false
19
+
20
+ Style/TrailingCommaInArrayLiteral:
21
+ Enabled: true
22
+ EnforcedStyleForMultiline: consistent_comma
23
+
24
+ Style/TrailingCommaInHashLiteral:
25
+ Enabled: true
26
+ EnforcedStyleForMultiline: consistent_comma
27
+
28
+ Style/TrailingCommaInArguments:
29
+ Enabled: true
30
+ EnforcedStyleForMultiline: consistent_comma
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [1.0.0] - 2024-11-09
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Erik T. Madsen
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # FilesInMyDiff
2
+
3
+ A command line tool that takes a git repository path and revision as input. It creates a temporary worktree, checks out the specified revision, and returns a JSON object containing the worktree path, commit SHA, and list of changed files. The tool helps you inspect changes in any git revision without affecting your current working directory.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```bash
18
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ CLI usage as described by the help message:
24
+ ```
25
+ Usage: files_in_my_diff <folder> <revision>
26
+
27
+ folder - Path to git repository
28
+ revision - Git revision (branch, tag, or commit SHA)
29
+ ```
30
+
31
+ If you wish to invoke the logic programmatically, you can use the method `FilesInMyDiff.checkout(folder: <repository path>, revision: <git revision>)`.
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/beatmadsen/files_in_my_diff.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ # auto-correct rubocop offenses
13
+ task default: %i[test rubocop:autocorrect]
data/TODO ADDED
File without changes
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ folder = ARGV[0]
5
+ revision = ARGV[1]
6
+
7
+ if folder.nil? || folder == '--help' || revision.nil?
8
+ puts 'Usage: files_in_my_diff <folder> <revision>'
9
+ puts
10
+ puts 'folder - Path to git repository'
11
+ puts 'revision - Git revision (branch, tag, or commit SHA)'
12
+ exit 1
13
+ end
14
+
15
+ require 'bundler/setup'
16
+ require 'setup_git'
17
+ require 'files_in_my_diff'
18
+ require 'json'
19
+
20
+ begin
21
+ decorated = FilesInMyDiff.checkout(folder:, revision:)
22
+ puts decorated.to_json
23
+ rescue FilesInMyDiff::Error => e
24
+ puts e.message
25
+ exit 1
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FilesInMyDiff
4
+ module Git
5
+ class Adapter
6
+ def initialize(folder:, repo: ::Git.open(folder))
7
+ @repo = repo
8
+ end
9
+
10
+ def diff(revision)
11
+ Diff.new(object: object(revision), revision:)
12
+ end
13
+
14
+ def checkout_worktree(path, revision)
15
+ @repo.add_worktree(path, revision)
16
+ rescue ::Git::FailedError => e
17
+ unless e.result.stderr.include? 'already exists'
18
+ raise CheckoutError,
19
+ "Could not checkout #{revision} to #{path}: #{e.message}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def object(revision)
26
+ if revision.is_a?(::Git::Object::AbstractObject)
27
+ @repo.object(revision.sha)
28
+ else
29
+ @repo.object(revision)
30
+ end
31
+ rescue ::Git::FailedError
32
+ nil
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FilesInMyDiff
4
+ module Git
5
+ class Diff
6
+ def initialize(object:, revision:)
7
+ @object = object
8
+ @revision = revision
9
+ end
10
+
11
+ def validate!
12
+ raise ValidationError, "Revision #{@revision} does not exist" if @object.nil?
13
+ end
14
+
15
+ def sha
16
+ @object.sha
17
+ end
18
+
19
+ def changes
20
+ parent = @object.parents.first
21
+ return [] if parent.nil?
22
+
23
+ parent.diff(@object).map do |change|
24
+ { path: change.path, type: change.type }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'git/adapter'
4
+ require_relative 'git/diff'
5
+
6
+ module FilesInMyDiff
7
+ module Git
8
+ class DiffError < Error; end
9
+ class CheckoutError < Error; end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FilesInMyDiff
4
+ class Resolver
5
+ def initialize(folder:, revision:, file_strategy: TmpDir::FileStrategy, git_strategy: nil)
6
+ @folder = folder
7
+ @revision = revision
8
+ @file_strategy = file_strategy
9
+ @git_strategy = git_strategy
10
+ end
11
+
12
+ def call
13
+ validate_folder!
14
+ diff = git_strategy.diff(@revision)
15
+ diff.validate!
16
+ rd = @file_strategy.revision_dir(diff.sha)
17
+ rd.create!
18
+ git_strategy.checkout_worktree(rd.dir, diff.sha)
19
+ rd.decorate(diff.changes)
20
+ end
21
+
22
+ private
23
+
24
+ def validate_folder!
25
+ raise ValidationError, 'Folder is required' if @folder.nil?
26
+ raise ValidationError, "Folder '#{@folder}' does not exist" unless @file_strategy.dir_exists?(@folder)
27
+ end
28
+
29
+ def git_strategy
30
+ @git_strategy ||= Git::Adapter.new(folder: @folder)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FilesInMyDiff
4
+ module TmpDir
5
+ module FileStrategy
6
+ def self.dir_exists?(folder)
7
+ Dir.exist?(folder)
8
+ end
9
+
10
+ def self.tmpdir
11
+ Dir.tmpdir
12
+ rescue StandardError => e
13
+ raise DirectoryError, "Failed to locate tmpdir: #{e.message}"
14
+ end
15
+
16
+ def self.mkdir_p(path)
17
+ FileUtils.mkdir_p(path)
18
+ rescue SystemCallError => e
19
+ raise DirectoryError, "Failed to create tmp dir for #{path}: #{e.message}"
20
+ end
21
+
22
+ def self.revision_dir(sha)
23
+ RevisionDir.new(sha:)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FilesInMyDiff
4
+ module TmpDir
5
+ class RevisionDir
6
+ attr_reader :dir
7
+
8
+ def initialize(sha:, file_strategy: FileStrategy)
9
+ @sha = sha
10
+ @file_strategy = file_strategy
11
+ end
12
+
13
+ def create!
14
+ tmpdir = @file_strategy.tmpdir
15
+ @dir = File.join(tmpdir, 'files_in_my_diff', @sha)
16
+ @file_strategy.mkdir_p(@dir)
17
+ end
18
+
19
+ def decorate(changes)
20
+ raise DirectoryError, "Directory for #{@sha} not yet created" if @dir.nil?
21
+
22
+ d_changes = changes.map do |change|
23
+ change => { path:, type: }
24
+ { full_path: full_path(path), relative_path: path, type: }
25
+ end
26
+ { dir: @dir, sha: @sha, changes: d_changes }
27
+ end
28
+
29
+ private
30
+
31
+ def full_path(relative_path)
32
+ File.join(@dir, relative_path)
33
+ rescue StandardError => e
34
+ raise FileError, "Failed to create full path for #{relative_path}: #{e.message}"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'tmp_dir/revision_dir'
4
+ require_relative 'tmp_dir/file_strategy'
5
+
6
+ module FilesInMyDiff
7
+ module TmpDir
8
+ class DirectoryError < FilesInMyDiff::Error; end
9
+ class FileError < FilesInMyDiff::Error; end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FilesInMyDiff
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module FilesInMyDiff
6
+ class Error < StandardError; end
7
+ class ValidationError < Error; end
8
+ end
9
+
10
+ require_relative 'files_in_my_diff/version'
11
+ require_relative 'files_in_my_diff/tmp_dir'
12
+ require_relative 'files_in_my_diff/git'
13
+ require_relative 'files_in_my_diff/resolver'
14
+
15
+ module FilesInMyDiff
16
+ def self.root
17
+ File.expand_path('..', __dir__)
18
+ end
19
+
20
+ def self.checkout(folder:, revision:)
21
+ Resolver.new(folder:, revision:).call
22
+ end
23
+ end
data/lib/setup_git.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'git'
4
+
5
+ module Git
6
+ class Base
7
+ def add_worktree(path, revision)
8
+ lib.send(:command, 'worktree', 'add', path, revision)
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: files_in_my_diff
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik T. Madsen
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2024-11-09 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: git
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.3'
26
+ description: A command line tool that takes a git repository path and revision as
27
+ input. It creates a temporary worktree, checks out the specified revision, and returns
28
+ a JSON object containing the worktree path, commit SHA, and list of changed files.
29
+ The tool helps you inspect changes in any git revision without affecting your current
30
+ working directory.
31
+ email:
32
+ - beatmadsen@gmail.com
33
+ executables:
34
+ - files_in_my_diff
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ".rubocop.yml"
39
+ - CHANGELOG.md
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - TODO
44
+ - bin/files_in_my_diff
45
+ - lib/files_in_my_diff.rb
46
+ - lib/files_in_my_diff/git.rb
47
+ - lib/files_in_my_diff/git/adapter.rb
48
+ - lib/files_in_my_diff/git/diff.rb
49
+ - lib/files_in_my_diff/resolver.rb
50
+ - lib/files_in_my_diff/tmp_dir.rb
51
+ - lib/files_in_my_diff/tmp_dir/file_strategy.rb
52
+ - lib/files_in_my_diff/tmp_dir/revision_dir.rb
53
+ - lib/files_in_my_diff/version.rb
54
+ - lib/setup_git.rb
55
+ homepage: https://github.com/beatmadsen/files-in-my-diff
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ homepage_uri: https://github.com/beatmadsen/files-in-my-diff
60
+ source_code_uri: https://github.com/beatmadsen/files-in-my-diff
61
+ changelog_uri: https://github.com/beatmadsen/files-in-my-diff/blob/main/CHANGELOG.md
62
+ rubygems_mfa_required: 'true'
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '3.2'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.6.0.dev
78
+ specification_version: 4
79
+ summary: Checks out a git revision in a new worktree and provides you the changed
80
+ files
81
+ test_files: []