Skip to content

Commit 0f24d8f

Browse files
committed
Merge pull request #100 from luisdelarosa/check_for_merge_commits_in_yaml_squashed
Show informative error message when a merge conflict is detected in a YAML file.
2 parents 85b4d64 + 883de73 commit 0f24d8f

File tree

10 files changed

+125
-38
lines changed

10 files changed

+125
-38
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
##### Enhancements
66

7+
* Show informative error message when a merge conflict is detected in a YAML file.
8+
[Luis de la Rosa](https://github.com/luisdelarosa)
9+
[#69](https://github.com/CocoaPods/Core/issues/69)
10+
[#100](https://github.com/CocoaPods/Core/pull/100)
11+
712
* Added a check to the linter to ensure that the `social_media_url` has
813
been changed from the example value.
914
[Richard Lee](https://github.com/dlackty)

lib/cocoapods-core.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Informative < PlainInformative; end
2828
autoload :Source, 'cocoapods-core/source'
2929
autoload :Specification, 'cocoapods-core/specification'
3030
autoload :StandardError, 'cocoapods-core/standard_error'
31-
autoload :YAMLConverter, 'cocoapods-core/yaml_converter'
31+
autoload :YAMLHelper, 'cocoapods-core/yaml_helper'
3232

3333
# TODO: Fix
3434
#

lib/cocoapods-core/lockfile.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def initialize(hash)
4040
def self.from_file(path)
4141
return nil unless path.exist?
4242
require 'yaml'
43-
hash = File.open(path) { |f| YAML.load(f) }
43+
hash = File.open(path) { |f| YAMLHelper.load(f) }
4444
unless hash && hash.is_a?(Hash)
4545
raise Informative, "Invalid Lockfile in `#{path}`"
4646
end
@@ -312,7 +312,7 @@ def to_yaml
312312
'SPEC CHECKSUMS',
313313
'COCOAPODS',
314314
]
315-
YAMLConverter.convert_hash(to_hash, keys_hint, "\n\n")
315+
YAMLHelper.convert_hash(to_hash, keys_hint, "\n\n")
316316
end
317317

318318
#-------------------------------------------------------------------------#

lib/cocoapods-core/podfile.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def self.from_yaml(path)
266266
if string.respond_to?(:encoding) && string.encoding.name != 'UTF-8'
267267
string.encode!('UTF-8')
268268
end
269-
hash = YAML.load(string)
269+
hash = YAMLHelper.load(string)
270270
from_hash(hash, path)
271271
end
272272

lib/cocoapods-core/specification/set/statistics.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def github_pushed_at(set)
155155
def cache
156156
unless @cache
157157
if cache_file && cache_file.exist?
158-
@cache = YAML.load(cache_file.read)
158+
@cache = YAMLHelper.load(cache_file.read)
159159
else
160160
@cache = {}
161161
end

lib/cocoapods-core/yaml_converter.rb lib/cocoapods-core/yaml_helper.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Pod
1515
# The missing features include:
1616
# - Strings are never quoted even when ambiguous.
1717
#
18-
class YAMLConverter
18+
class YAMLHelper
1919

2020
class << self
2121

@@ -41,6 +41,18 @@ def convert_hash(value, hash_keys_hint, line_separator = "\n")
4141
result << "\n"
4242
end
4343

44+
# Load a YAML file and provide more informative error messages in special cases like merge conflict.
45+
# @param A YAML string.
46+
def load(yaml_string)
47+
YAML.load(yaml_string)
48+
rescue Exception => exception
49+
if yaml_has_merge_error(yaml_string)
50+
raise Informative, 'Merge conflict(s) detected'
51+
else
52+
raise exception
53+
end
54+
end
55+
4456
#-----------------------------------------------------------------------#
4557

4658
private
@@ -120,6 +132,13 @@ def process_hash(hash, hash_keys_hint = nil, line_separator = "\n")
120132
key_lines * line_separator
121133
end
122134

135+
# Check for merge errors in a YAML string.
136+
# @param A YAML string.
137+
# @return If a merge error was detected or not.
138+
def yaml_has_merge_error(yaml_string)
139+
yaml_string.include?('<<<<<<< HEAD')
140+
end
141+
123142
#-----------------------------------------------------------------------#
124143

125144
private

spec/lockfile_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def self.specs
6565
end
6666

6767
it 'stores the initialization hash' do
68-
lockfile = Lockfile.new(YAML.load(Sample.yaml))
69-
lockfile.internal_data.should == YAML.load(Sample.yaml)
68+
lockfile = Lockfile.new(YAMLHelper.load(Sample.yaml))
69+
lockfile.internal_data.should == YAMLHelper.load(Sample.yaml)
7070
end
7171

7272
it 'loads from a file' do
7373
File.open(@tmp_path, 'w') { |f| f.write(Sample.yaml) }
7474
lockfile = Lockfile.from_file(@tmp_path)
75-
lockfile.internal_data.should == YAML.load(Sample.yaml)
75+
lockfile.internal_data.should == YAMLHelper.load(Sample.yaml)
7676
end
7777

7878
it "returns nil if it can't find the initialization file" do
@@ -331,7 +331,7 @@ def self.specs
331331
end
332332

333333
it 'generates a valid YAML representation' do
334-
YAML.load(@lockfile.to_yaml).should == YAML.load(Sample.yaml)
334+
YAMLHelper.load(@lockfile.to_yaml).should == YAMLHelper.load(Sample.yaml)
335335
end
336336

337337
it "serializes correctly `:head' dependencies" do
@@ -414,7 +414,7 @@ def self.specs
414414
end
415415

416416
it 'it includes all the information that it is expected to store' do
417-
@lockfile.internal_data.should == YAML.load(Sample.yaml)
417+
@lockfile.internal_data.should == YAMLHelper.load(Sample.yaml)
418418
end
419419
end
420420

spec/podfile_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ module Pod
196196
generate_bridge_support: true
197197
set_arc_compatibility_flag: true
198198
EOF
199-
YAML.load(podfile.to_yaml).should == YAML.load(expected)
199+
YAMLHelper.load(podfile.to_yaml).should == YAMLHelper.load(expected)
200200
end
201201

202202
it 'includes inhibit warnings per pod' do

spec/specification/set/statistics_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ module Pod
9595

9696
it 'saves the cache after computing the creation date of a set' do
9797
@stats.creation_date(@set)
98-
cache_hash = YAML.load(@cache_file.read)
98+
cache_hash = YAMLHelper.load(@cache_file.read)
9999
cache_hash['JSONKit'][:creation_date].should == Time.parse('2011-09-12 10:49:04 +0200')
100100
end
101101

102102
it 'saves the cache after computing the creation date of many sets' do
103103
sets = [@set, @source.search_by_name('libPusher').first]
104104
@stats.creation_dates(sets)
105-
cache_hash = YAML.load(@cache_file.read)
105+
cache_hash = YAMLHelper.load(@cache_file.read)
106106
cache_hash['JSONKit'][:creation_date].should == Time.parse('2011-09-12 10:49:04 +0200')
107107
cache_hash['libPusher'][:creation_date].should == Time.parse('2012-02-01 17:05:58 +0100')
108108
end
@@ -128,7 +128,7 @@ module Pod
128128

129129
it 'saves the cache after retrieving GitHub information' do
130130
@stats.github_watchers(@set)
131-
saved_cache = YAML.load(@cache_file.read)
131+
saved_cache = YAMLHelper.load(@cache_file.read)
132132
saved_cache['JSONKit'][:gh_date] = nil
133133
@cache_hash['JSONKit'][:gh_date] = nil
134134
saved_cache.should == @cache_hash
@@ -141,7 +141,7 @@ module Pod
141141

142142
it 'stores in the cache time of the last access to the GitHub API' do
143143
@stats.github_watchers(@set)
144-
saved_cache = YAML.load(@cache_file.read)
144+
saved_cache = YAMLHelper.load(@cache_file.read)
145145
time_delta = (Time.now - saved_cache['JSONKit'][:gh_date])
146146
time_delta.should < 60
147147
end

0 commit comments

Comments
 (0)