git_diff_lcs 0.0.6 → 0.1.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: e1b6be903d8a4f4a01f76b6bb78e2760951c57bbf1ae01c1f08025be9a3fe466
4
- data.tar.gz: 336de7d1164b9f315e83866901cc8beffe189bc5143e687f9f441881b0dffd0d
3
+ metadata.gz: 5cf11c03244890d238b1b72e02230eef6bf3827e76af08a0e05b97078cb4f936
4
+ data.tar.gz: 9e0cfc354d65e4457e87cca22517746907ea8362ff30230c21e60c9d40ae9d16
5
5
  SHA512:
6
- metadata.gz: ef89f50965b515f05fbe905aaf2f0b1c1b835d15e0cd0c30401c63f2ab63d78def8c710b26f0e886fe23a0a8b187c6f5a63abdcc83fbbd9e7a4fa3da28d05d38
7
- data.tar.gz: 3d571ae3db7b6e9fc55d6df2d135996cbbfa68312b30803274230d8a30a03fc1bd0f39310c9e7bf81059e695ad61d1e3e7ce1f2f7da3d87c81d8dfda7e74060e
6
+ metadata.gz: 935d894185b4497f6c0770df6aabe85e0cf186364d4e1c0647d8fd6176afbfc2023170ef40d88b897b7d6b3466c15c6a24ed4a79868a984c4de66a740f130410
7
+ data.tar.gz: 8a8f1ef61c6c68132e72c9e909ca1c0526db48ce09f4353e048b49589fdf17fdbdc8209155de4c582bd090b36593994f3bf5339c54e2a797c1816612a44a0676
@@ -1,18 +1,18 @@
1
- name: Ruby
2
-
3
- on: [push,pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v2
10
- - name: Set up Ruby
11
- uses: ruby/setup-ruby@v1
12
- with:
13
- ruby-version: 3.0.0
14
- - name: Run the default task
15
- run: |
16
- gem install bundler -v 2.2.3
17
- bundle install
18
- bundle exec rake
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- git_diff_lcs (0.0.6)
4
+ git_diff_lcs (0.1.0)
5
5
  diff-lcs (= 1.4.4)
6
6
  git (= 1.8.1)
7
7
  thor (= 1.1.0)
data/README.md CHANGED
@@ -1,46 +1,26 @@
1
- # GitDiffLCS
2
-
3
- Generally, git diff --stat does not contain modifications, so we added it to display modifications using the LCS algorithm.
4
-
5
- ## Installation
6
- ```bash
7
- $ gem install git_diff-lcs
8
- ```
9
-
10
- ## How to use
11
-
12
- ### CLI
13
-
14
- ```bash
15
- $ git_diff_lcs diff [GIT_REPOSITORY] [SRC(BRANCH OR COMMIT)] [DEST(BRANCH OR COMMIT)]
16
- $ git_diff_lcs diff https://github.com/btpink-seo/git-diff-lcs.git test/src test/dest
17
- ```
18
-
19
- ### Ruby
20
-
21
- #### GitDiffLCS.diff(repo, src, dest)
22
-
23
- src, dest is branch name or commit
24
-
25
- ```ruby
26
- require 'git_diff_lcs'
27
-
28
- GitDiffLCS.diff('https://github.com/btpink-seo/git-diff-lcs.git', 'test/src', 'test/dest')
29
- # => 5 files changed, 13 insertions(+), 6 deletions(-), 2 modifications(!), total(21)
30
- ```
31
-
32
- #### GitDiffLCS::Stat.new(repo, src, dest)
33
-
34
- ```ruby
35
- require 'git_diff_lcs'
36
-
37
- stat = GitDiffLCS::Stat.new('https://github.com/btpink-seo/git-diff-lcs.git', 'test/src', 'test/dest')
38
- stat.summary
39
- # => 5 files changed, 13 insertions(+), 6 deletions(-), 2 modifications(!), total(21)
40
- stat.insertions
41
- # => 13
42
- stat.deletions
43
- # => 6
44
- stat.modifications
45
- # => 2
46
- ```
1
+ # GitDiffLCS
2
+
3
+ Generally, git diff --stat does not contain modifications, so we added it to display modifications using the LCS algorithm.
4
+
5
+ ## Installation
6
+ ```bash
7
+ $ gem install git_diff-lcs
8
+ ```
9
+
10
+ ## How to use
11
+
12
+ ```bash
13
+ $ git_diff_lcs shortstat [GIT_REPOSITORY or WORKING_DIRECTORY] [SRC(branch or commit)] [DEST(branch or commit)]
14
+ $ git_diff_lcs shortstat https://github.com/btpink-seo/git-diff-lcs.git test/src test/dest
15
+ $ git_diff_lcs shortstat workspace/git-diff-lcs test/src test/dest
16
+ ```
17
+
18
+ ## compare with git diff
19
+
20
+ ```bash
21
+ $ git diff --shortstat test/src test/dest
22
+ 5 files changed, 15 insertions(+), 8 deletions(-)
23
+
24
+ $ git_diff_lcs shortstat https://github.com/btpink-seo/git-diff-lcs.git test/src test/dest
25
+ 5 files changed, 13 insertions(+), 6 deletions(-), 2 modifications(!), total(21)
26
+ ```
data/bin/console CHANGED
@@ -1,18 +1,18 @@
1
- # A file which has a shebang line as its first line is
2
- # granted execute permission.
3
-
4
- #!/usr/bin/env ruby
5
- # frozen_string_literal: true
6
-
7
- require "bundler/setup"
8
- require "git_diff_lcs"
9
-
10
- # You can add fixtures and/or initialization code here to make experimenting
11
- # with your gem easier. You can also use a different console, if you like.
12
-
13
- # (If you use this, don't forget to add pry to your Gemfile!)
14
- # require "pry"
15
- # Pry.start
16
-
17
- require "irb"
18
- IRB.start(__FILE__)
1
+ # A file which has a shebang line as its first line is
2
+ # granted execute permission.
3
+
4
+ #!/usr/bin/env ruby
5
+ # frozen_string_literal: true
6
+
7
+ require "bundler/setup"
8
+ require "git_diff_lcs"
9
+
10
+ # You can add fixtures and/or initialization code here to make experimenting
11
+ # with your gem easier. You can also use a different console, if you like.
12
+
13
+ # (If you use this, don't forget to add pry to your Gemfile!)
14
+ # require "pry"
15
+ # Pry.start
16
+
17
+ require "irb"
18
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/git_diff_lcs CHANGED
@@ -1,7 +1,7 @@
1
- # frozen_string_literal: true
2
-
3
- #!/usr/bin/env ruby
4
-
5
- require "git_diff_lcs"
6
-
7
- GitDiffLCS::CLI.start(ARGV)
1
+ # frozen_string_literal: true
2
+
3
+ #!/usr/bin/env ruby
4
+
5
+ require "git_diff_lcs"
6
+
7
+ GitDiffLCS::CLI.start(ARGV)
data/git_diff_lcs.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/git_diff_lcs/version"
3
+ require_relative "lib/git_diff_lcs/constants"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "git_diff_lcs"
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.required_ruby_version = ">= 2.4.0"
15
15
 
16
16
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features|script)/}) }
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
18
18
  end
19
19
  spec.require_paths = ["lib"]
20
20
  spec.bindir = "exe"
data/lib/git_diff_lcs.rb CHANGED
@@ -1,26 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "git_diff_lcs/stat"
4
- require "git_diff_lcs/version"
3
+ require "git_diff_lcs/shortstat"
4
+ require "git_diff_lcs/constants"
5
5
  require "git_diff_lcs/cli"
6
+ require "git_diff_lcs/lib_git"
6
7
 
7
8
  # Git Diff LCS
8
9
  module GitDiffLCS
9
- # Get diff summary with LCS algorithm
10
+ # Get diff --shortstat with LCS algorithm
10
11
  #
11
12
  # Arguments:
12
- # [String] repo: repo git repository address
13
- # [String] src: src src commit or branch
14
- # [String] dest: dest commit or branch
13
+ # [String] git_target: git repository address or working directory
14
+ # [String] src: commit or branch
15
+ # [String] dest: commit or branch
15
16
  #
16
17
  # Return:
17
18
  # [String] diff summary (changed files, insertions, deletions, modifications and total)
18
19
  #
19
20
  # Example:
20
- # >> GitDiffLCS.diff("https://github.com/btpink-seo/git-diff-lcs.git", "test/src", "test/dest")
21
+ # >> GitDiffLCS.shortstat("https://github.com/btpink-seo/git-diff-lcs.git", "test/src", "test/dest")
21
22
  # => 5 files changed, 13 insertions(+), 6 deletions(-), 2 modifications(!), total(21)
22
- def self.diff(repo, src, dest)
23
- diff = GitDiffLCS::Stat.new(repo, src, dest)
24
- diff.errors.empty? ? diff.summary : diff.errors.first
23
+ def self.shortstat(git_target, src, dest)
24
+ shortstat = GitDiffLCS::Shortstat.new(git_target, src, dest)
25
+ shortstat.errors.empty? ? shortstat.summary : shortstat.errors.first
25
26
  end
26
27
  end
@@ -2,7 +2,6 @@
2
2
 
3
3
  require "thor"
4
4
 
5
- # Git Diff LCS
6
5
  module GitDiffLCS
7
6
  # Command Line Interface
8
7
  class CLI < Thor
@@ -10,9 +9,9 @@ module GitDiffLCS
10
9
  true
11
10
  end
12
11
 
13
- desc "diff GIT_REPOSITORY SRC(BRANCH OR COMMIT) DEST(BRANCH OR COMMIT)", "get diff with LCS"
14
- def diff(repo, src, dest)
15
- puts GitDiffLCS.diff(repo, src, dest)
12
+ desc "shortstat GIT_REPOSITORY SRC(BRANCH OR COMMIT) DEST(BRANCH OR COMMIT)", "get diff with LCS"
13
+ def shortstat(repo, src, dest)
14
+ puts GitDiffLCS.shortstat(repo, src, dest)
16
15
  end
17
16
  end
18
17
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module GitDiffLCS
6
+ # gem version
7
+ VERSION = "0.1.0"
8
+
9
+ # Source folder
10
+ SRC_FOLDER = "src_#{SecureRandom.uuid}"
11
+
12
+ # Destination folder
13
+ DEST_FOLDER = "dest_#{SecureRandom.uuid}"
14
+
15
+ # Initial diff count number
16
+ INIT_COUNT = [0, 0, 0].freeze
17
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "git"
4
+
5
+ module GitDiffLCS
6
+ # Git
7
+ class LibGit
8
+ attr_reader :git
9
+
10
+ # initialize GitDiffLCS::LibGit
11
+ #
12
+ # Arguments:
13
+ # [String] git_target: git repository address or working directory
14
+ # [String] dir: temporary folder
15
+ # [String] src: commit or branch
16
+ # [String] dest: commit or branch
17
+ def initialize(git_target, dir, src, dest)
18
+ @dir = dir
19
+ @src = src
20
+ @dest = dest
21
+ get_git(git_target)
22
+ end
23
+
24
+ def diff
25
+ @diff ||= @git.diff(@src, @dest)
26
+ end
27
+
28
+ private
29
+
30
+ # Git.open or Git.clone
31
+ def get_git(git_target)
32
+ git_clone(git_target)
33
+ rescue Git::GitExecuteError
34
+ git_open(git_target)
35
+ end
36
+
37
+ # If there is no folder, create a new one.
38
+ def copy_with_path(src_file, dest_file)
39
+ FileUtils.mkpath(File.dirname(dest_file))
40
+ FileUtils.cp(src_file, dest_file)
41
+ rescue Errno::ENOENT
42
+ nil
43
+ end
44
+
45
+ def copy_to_tmp(commit, src_dir, dest_dir)
46
+ @git.checkout(commit)
47
+ diff.name_status.each do |filename, _|
48
+ copy_with_path("#{@dir}/#{src_dir}/#{filename}", "#{@dir}/#{dest_dir}/#{filename}")
49
+ end
50
+ end
51
+
52
+ # git open and copy to destination folder for compare
53
+ def git_open(working_dir)
54
+ @git = Git.open(working_dir)
55
+ prev_branch = @git.branch.name
56
+ @git.checkout(@src)
57
+ copy_to_tmp(@dest, working_dir, GitDiffLCS::DEST_FOLDER)
58
+ copy_to_tmp(@src, working_dir, GitDiffLCS::SRC_FOLDER)
59
+ @git.checkout(prev_branch)
60
+ end
61
+
62
+ # git clone and copy to destination folder for compare
63
+ def git_clone(repo)
64
+ @git = Git.clone(repo, GitDiffLCS::SRC_FOLDER, path: @dir)
65
+ @git.checkout(@src)
66
+ copy_to_tmp(@dest, GitDiffLCS::SRC_FOLDER, GitDiffLCS::DEST_FOLDER)
67
+ @git.checkout(@src)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "diff/lcs"
4
+ require "git"
5
+
6
+ module GitDiffLCS
7
+ # Shortstat
8
+ class Shortstat
9
+ attr_reader :add, :del, :mod, :errors
10
+ alias insertions add
11
+ alias deletions del
12
+ alias modifications mod
13
+
14
+ # initialize GitDiffLCS::Shortstat
15
+ #
16
+ # Arguments:
17
+ # [String] git_target: git repository address or working directory
18
+ # [String] src: commit or branch
19
+ # [String] dest: commit or branch
20
+ def initialize(git_target, src, dest)
21
+ @dir = Dir.mktmpdir
22
+ @add, @del, @mod = *GitDiffLCS::INIT_COUNT
23
+ @errors = []
24
+ @git = GitDiffLCS::LibGit.new(git_target, @dir, src, dest)
25
+ calculate
26
+ rescue Git::GitExecuteError, ArgumentError
27
+ @errors << "[ERROR] wrong arguments"
28
+ ensure
29
+ FileUtils.rm_rf(@dir)
30
+ end
31
+
32
+ # Get diff summary
33
+ # changed files, insertions, deletions, modifications and total
34
+ #
35
+ # Return:
36
+ # [String] diff summary
37
+ def summary
38
+ " #{@git.diff.name_status.keys.size} files changed, "\
39
+ "#{@add} insertions(+), #{@del} deletions(-), #{@mod} modifications(!), "\
40
+ "total(#{@add + @del + @mod})"
41
+ end
42
+
43
+ private
44
+
45
+ # add each count variable
46
+ def add_result(diff)
47
+ @add += 1 if diff.adding?
48
+ @del += 1 if diff.deleting?
49
+ @mod += 1 if diff.changed?
50
+ end
51
+
52
+ # count diff
53
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
54
+ def calculate
55
+ @git.diff.name_status.each do |filename, status|
56
+ src_filename = "#{@dir}/#{GitDiffLCS::SRC_FOLDER}/#{filename}"
57
+ dest_filename = "#{@dir}/#{GitDiffLCS::DEST_FOLDER}/#{filename}"
58
+
59
+ case status
60
+ when "A"
61
+ @add += File.open(dest_filename).readlines.size
62
+ next
63
+ when "D"
64
+ @del += File.open(src_filename).readlines.size
65
+ next
66
+ else
67
+ src = File.open(src_filename)
68
+ dest = File.open(dest_filename)
69
+ Diff::LCS.sdiff(src.readlines, dest.readlines).each { |d| add_result(d) }
70
+ end
71
+ end
72
+ end
73
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_diff_lcs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SEO SANG HYUN
@@ -74,8 +74,9 @@ files:
74
74
  - git_diff_lcs.gemspec
75
75
  - lib/git_diff_lcs.rb
76
76
  - lib/git_diff_lcs/cli.rb
77
- - lib/git_diff_lcs/stat.rb
78
- - lib/git_diff_lcs/version.rb
77
+ - lib/git_diff_lcs/constants.rb
78
+ - lib/git_diff_lcs/lib_git.rb
79
+ - lib/git_diff_lcs/shortstat.rb
79
80
  homepage: https://github.com/btpink-seo/git-diff-lcs
80
81
  licenses:
81
82
  - MIT
@@ -1,118 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "securerandom"
4
- require "diff/lcs"
5
- require "git"
6
-
7
- # Git Diff LCS
8
- module GitDiffLCS
9
- # Stat
10
- class Stat
11
- # Source folder
12
- SRC_FOLDER = "src_#{SecureRandom.uuid}"
13
-
14
- # Destination folder
15
- DEST_FOLDER = "dest_#{SecureRandom.uuid}"
16
-
17
- # Initial diff count number
18
- INIT_COUNT = [0, 0, 0].freeze
19
-
20
- # Initial array for target_files and errors
21
- INIT_ARRAY = Array.new(2, [])
22
-
23
- attr_reader :add, :del, :mod, :errors
24
- alias insertions add
25
- alias deletions del
26
- alias modifications mod
27
-
28
- # initialize GitDiffLCS::Stat
29
- #
30
- # Arguments:
31
- # [String] repo: repo git repository address
32
- # [String] src: src src commit or branch
33
- # [String] dest: dest commit or branch
34
- def initialize(repo, src, dest)
35
- @go_next = false
36
- @dir = Dir.mktmpdir
37
- @add, @del, @mod = *INIT_COUNT
38
- @target_files, @errors = *Array.new(2, [])
39
- @diff = git_clone(repo, src, dest).diff(src, dest)
40
- calculate
41
- rescue Git::GitExecuteError
42
- @errors << "[ERROR] wrong git info(repository or src or dest)"
43
- ensure
44
- FileUtils.rm_rf(@dir)
45
- end
46
-
47
- # Get diff summary
48
- # changed files, insertions, deletions, modifications and total
49
- #
50
- # Return:
51
- # [String] diff summary
52
- #
53
- # Example:
54
- # >> stat = GitDiffLCS::Stat.new("https://github.com/btpink-seo/git-diff-lcs.git", "test/src", "test/dest")
55
- # >> stat.summary
56
- # => 5 files changed, 13 insertions(+), 6 deletions(-), 2 modifications(!), total(21)
57
- def summary
58
- total = @add + @del + @mod
59
- changed = @target_files.size
60
- "#{changed} files changed, #{@add} insertions(+), #{@del} deletions(-), #{@mod} modifications(!), total(#{total})"
61
- end
62
-
63
- private
64
-
65
- # git clone and copy to destination folder for compare
66
- def git_clone(repo, src, dest)
67
- git = Git.clone(repo, SRC_FOLDER, path: @dir)
68
- git.checkout(dest)
69
- FileUtils.copy_entry("#{@dir}/#{SRC_FOLDER}", "#{@dir}/#{DEST_FOLDER}")
70
- git.checkout(src)
71
- git
72
- end
73
-
74
- # open_src_file
75
- # if dosen't exist file, add insertions all dest line length
76
- def open_src_file(src_filename, dest_filename)
77
- File.open(src_filename)
78
- rescue Errno::ENOENT
79
- # new file in dest
80
- @add += open_dest_file(nil, dest_filename).readlines.size
81
- @go_next = true
82
- end
83
-
84
- # open_dest_file
85
- # if dosen't exist file, add deletions all src line length
86
- def open_dest_file(src, dest_filename)
87
- File.open(dest_filename)
88
- rescue Errno::ENOENT
89
- # deleted file in dest
90
- @del += src.readlines.size
91
- @go_next = true
92
- end
93
-
94
- # add each count variable
95
- def add_result(diff)
96
- @add += 1 if diff.adding?
97
- @del += 1 if diff.deleting?
98
- @mod += 1 if diff.changed?
99
- end
100
-
101
- # count diff
102
- def calculate
103
- @target_files = @diff.name_status.keys
104
- @target_files.each do |file|
105
- src_filename = "#{@dir}/#{SRC_FOLDER}/#{file}"
106
- dest_filename = "#{@dir}/#{DEST_FOLDER}/#{file}"
107
-
108
- src = open_src_file(src_filename, dest_filename)
109
- dest = open_dest_file(src, dest_filename)
110
-
111
- next if @go_next && !(@go_next = !@go_next)
112
- next if FileUtils.cmp(src_filename, dest_filename)
113
-
114
- Diff::LCS.sdiff(src.readlines, dest.readlines).each { |d| add_result(d) }
115
- end
116
- end
117
- end
118
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module GitDiffLCS
4
- VERSION = "0.0.6"
5
- end