fix_tsv_conflict 0.1.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: 93f8545e521c8c3a0b91d53a8dc35eb2414d1da1a7bb8fc673e0a9697ec2d1b2
4
+ data.tar.gz: e410a589bfd588143133abc59092e2ccf820f01636424f43ea168db0e02680f7
5
+ SHA512:
6
+ metadata.gz: f79e153a54393de55d5bba7f1bb985f12f7d4235081e54e588173d7b6063752ac19d49d21bc59f579e1668be005c766524087ff082657c82eaed3a7a3d424204
7
+ data.tar.gz: f259d5bc68b0ca3ff4359f53082b4edd180df68c29c5319b6d9384e3264c6869aea804c22a73c56c88fad594c4fd333ab9b40c78ee29e73b556c3882dd66e84d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fix_tsv_conflict.gemspec
6
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :minitest, include: %w(test) do
2
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
3
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb"}
4
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
5
+ end
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # FixTSVConflict
2
+
3
+ [![Build Status](https://travis-ci.org/a2ikm/fix_tsv_conflict.svg?branch=master)](https://travis-ci.org/a2ikm/fix_tsv_conflict)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fix_tsv_conflict'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fix_tsv_conflict
20
+
21
+ ## Usage
22
+
23
+ $ fix_tsv_conflict [<options>] <tsv>
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/a2ikm/fix_tsv_conflict.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fix_tsv_conflict"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +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
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "fix_tsv_conflict"
4
+ FixTSVConflict::CLI.new.run
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fix_tsv_conflict/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fix_tsv_conflict"
8
+ spec.version = FixTSVConflict::VERSION
9
+ spec.authors = ["Masato Ikeda"]
10
+ spec.email = ["masato.ikeda@gmail.com"]
11
+
12
+ spec.summary = %q{Fix git conflicts in TSV file.}
13
+ spec.description = %q{Fix git conflicts in TSV file.}
14
+ spec.homepage = "https://github.com/a2ikm/fix_tsv_conflict"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "guard"
27
+ spec.add_development_dependency "guard-minitest"
28
+ end
@@ -0,0 +1,11 @@
1
+ require "fix_tsv_conflict/version"
2
+ require "fix_tsv_conflict/cli"
3
+
4
+ module FixTSVConflict
5
+ TAB = "\t"
6
+ LF = "\n"
7
+
8
+ LEFT = "<<<<<<<"
9
+ SEP = "======="
10
+ RIGHT = ">>>>>>>"
11
+ end
@@ -0,0 +1,47 @@
1
+ require "fix_tsv_conflict/repairman"
2
+ require "optparse"
3
+
4
+ module FixTSVConflict
5
+ class CLI
6
+ def run(argv = ARGV)
7
+ path, options = handle_argv(argv)
8
+
9
+ source = File.read(path)
10
+ output = Repairman.new.repair(source)
11
+
12
+ if options[:override]
13
+ File.open(path, "w") { |f| f.write(output) }
14
+ else
15
+ puts output
16
+ end
17
+ end
18
+
19
+ def handle_argv(argv)
20
+ options = {}
21
+ op = option_parser(options)
22
+ op.parse!(argv)
23
+
24
+ path = argv.shift
25
+ abort(op.help) if path.nil?
26
+
27
+ [path, options]
28
+ end
29
+
30
+ def option_parser(options)
31
+ OptionParser.new do |op|
32
+ op.banner = <<-BANNER
33
+ Usage: #{executable} [<options>] <tsv>
34
+ Options:
35
+ BANNER
36
+
37
+ op.on "-O", "--override", "Override source file" do
38
+ options[:override] = true
39
+ end
40
+ end
41
+ end
42
+
43
+ def executable
44
+ File.basename($0)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,51 @@
1
+ module FixTSVConflict
2
+ class DiffPrinter
3
+ def initialize(stderr: $stderr)
4
+ @stderr = stderr
5
+ @left, @right = {}, {}
6
+ end
7
+
8
+ def print(cols, left, lbranch, right, rbranch)
9
+ @lbranch, @rbranch = lbranch, rbranch
10
+
11
+ left = left.chomp.split(TAB)
12
+ right = right.chomp.split(TAB)
13
+
14
+ cols.each do |col, i|
15
+ l, r = left[i], right[i]
16
+ if l == r
17
+ flush_conflicts if in_conflict?
18
+ print_col_and_value(col, l)
19
+ else
20
+ @left[col] = l
21
+ @right[col] = r
22
+ end
23
+ end
24
+
25
+ flush_conflicts
26
+ end
27
+
28
+ def flush_conflicts
29
+ @stderr.puts "#{LEFT} #{@lbranch}"
30
+ @left.each do |c, v|
31
+ print_col_and_value(c, v)
32
+ end
33
+ @stderr.puts SEP
34
+ @right.each do |c, v|
35
+ print_col_and_value(c, v)
36
+ end
37
+ @stderr.puts "#{RIGHT} #{@rbranch}"
38
+
39
+ @left.clear
40
+ @right.clear
41
+ end
42
+
43
+ def print_col_and_value(col, value)
44
+ @stderr.puts [col, value].join(TAB)
45
+ end
46
+
47
+ def in_conflict?
48
+ @left.length > 0 || @right.length > 0
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,144 @@
1
+ require "fix_tsv_conflict/diff_printer"
2
+
3
+ module StringExt
4
+ BLANK_RE = /\A[[:space:]]*\z/
5
+ refine String do
6
+ def blank?
7
+ BLANK_RE === self
8
+ end
9
+ end
10
+ end
11
+
12
+ module FixTSVConflict
13
+ class Repairman
14
+ using StringExt
15
+
16
+ def initialize(stdin: $stdin, stderr: $stderr)
17
+ @stdin = stdin
18
+ @stderr = stderr
19
+ end
20
+
21
+ def repair(source)
22
+ result = []
23
+ branch, left, right = nil, [], []
24
+
25
+ source.each_line.with_index do |line, i|
26
+ parse_header(line) if i.zero?
27
+ if branch
28
+ if line.start_with?(RIGHT)
29
+ @rbranch = line.chomp.split(" ").last
30
+ result += resolve(left, right)
31
+ branch = nil
32
+ elsif line.start_with?(SEP)
33
+ branch = right
34
+ else
35
+ branch << line
36
+ end
37
+ else
38
+ if line.start_with?(LEFT)
39
+ @lbranch = line.chomp.split(" ").last
40
+ branch = left
41
+ else
42
+ result << line
43
+ end
44
+ end
45
+ end
46
+ result.join
47
+ end
48
+
49
+ def resolve(left, right)
50
+ left = index_by_id(left.reject(&:blank?))
51
+ right = index_by_id(right.reject(&:blank?))
52
+ (left.keys + right.keys).uniq.sort.map do |id|
53
+ l = left[id]
54
+ r = right[id]
55
+ if l && r
56
+ select(l, r)
57
+ else
58
+ l || r
59
+ end
60
+ end
61
+ end
62
+
63
+ def select(l, r)
64
+ selected = if l.rstrip == r.rstrip
65
+ pick_by_trailing_tabs(l, r)
66
+ else
67
+ prompt(l, r)
68
+ end
69
+ end
70
+
71
+ def prompt(l, r)
72
+ print_diff(l, r)
73
+ prompt_select(l, r)
74
+ end
75
+
76
+ def print_diff(l, r)
77
+ printer = DiffPrinter.new(stderr: @stderr)
78
+ printer.print(@cols, l, @lbranch, r, @rbranch)
79
+ end
80
+
81
+ def prompt_select(l, r)
82
+ text = <<-TEXT
83
+ Which do you want keep?
84
+
85
+ 1) #{@lbranch}
86
+ 2) #{@rbranch}
87
+ 3) both of them
88
+
89
+ Please enter 1, 2, or 3:
90
+ TEXT
91
+
92
+ @stderr.print text.chomp
93
+
94
+ loop do
95
+ case selected = @stdin.gets.strip
96
+ when "1"
97
+ break l
98
+ when "2"
99
+ break r
100
+ when "3"
101
+ break "#{LEFT} #{@lbranch}\n#{l}#{SEP}\n#{r}#{RIGHT} #{@rbranch}\n"
102
+ else
103
+ text = <<-TEXT
104
+ Invalid input: #{selected}
105
+ Please enter 1, 2, or 3:
106
+ TEXT
107
+ @stderr.print text.chomp
108
+ end
109
+ end
110
+ end
111
+
112
+ def index_by_id(lines)
113
+ result = {}
114
+ lines.each do |line|
115
+ id = line.split(TAB, 2).first
116
+ result[id] = line
117
+ end
118
+ result
119
+ end
120
+
121
+ def parse_header(line)
122
+ @cols = {}
123
+ line.chomp.split(TAB).each.with_index do |col, i|
124
+ @cols[col] = i
125
+ end
126
+ @tabs = @cols.length - 1
127
+ end
128
+
129
+ def pick_by_trailing_tabs(l, r)
130
+ ltabs = l.count(TAB)
131
+ rtabs = r.count(TAB)
132
+
133
+ if ltabs == @tabs
134
+ l
135
+ elsif rtabs == @tabs
136
+ r
137
+ else
138
+ # both are wrong.
139
+ # so this is a determistic picking.
140
+ ltabs < rtabs ? l : r
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ module FixTSVConflict
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fix_tsv_conflict
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masato Ikeda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Fix git conflicts in TSV file.
84
+ email:
85
+ - masato.ikeda@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Guardfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - exec/fix_tsv_conflict
99
+ - fix_tsv_conflict.gemspec
100
+ - lib/fix_tsv_conflict.rb
101
+ - lib/fix_tsv_conflict/cli.rb
102
+ - lib/fix_tsv_conflict/diff_printer.rb
103
+ - lib/fix_tsv_conflict/repairman.rb
104
+ - lib/fix_tsv_conflict/version.rb
105
+ homepage: https://github.com/a2ikm/fix_tsv_conflict
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Fix git conflicts in TSV file.
128
+ test_files: []