r10kdiff 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/r10kdiff +4 -0
  3. data/lib/r10kdiff.rb +192 -0
  4. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0deb8c2c9bdcc5b28d0cf420f7f5c04bd2212acb
4
+ data.tar.gz: 8d728630df39ef6ca6aab098672d51a1ab58ec08
5
+ SHA512:
6
+ metadata.gz: f9434954c64aa497c4a5fcfa31af68b7c816c30f7c5ad8f6412936e566508f7811294031c72af40cd0e2201d70dba464df93bd60d0f3c2f7cf0da46e5b2f94de
7
+ data.tar.gz: 0b2efcade52590742f3e6e64e08c6de712391314867717cf1569989a23629e1b30fa5d8e717c74cdb73aeff16019bb118671a56ed8519a7e9ac0b0239f14e50c
data/bin/r10kdiff ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'r10kdiff'
4
+ R10kDiff::Commandline.run
data/lib/r10kdiff.rb ADDED
@@ -0,0 +1,192 @@
1
+ require 'optparse'
2
+ require 'set'
3
+
4
+ module R10kDiff
5
+ class PuppetfileDSL
6
+ def initialize(puppetfile_text)
7
+ @forge = 'forge.puppetlabs.com'
8
+ @modules = {}
9
+ eval(puppetfile_text)
10
+ end
11
+ attr_reader :modules
12
+
13
+ def forge(name)
14
+ @forge = name
15
+ end
16
+
17
+ def mod(name, args={})
18
+ args = {:ref => args} if args.is_a? String
19
+ args[:ref] = "master" unless args[:ref]
20
+ args[:forge] = "https://#{@forge}/#{name}"
21
+ @modules[name] = PuppetModule.new name, args
22
+ end
23
+ end
24
+
25
+ class PuppetModule
26
+ def initialize(name, ref:nil, git:nil, forge:nil)
27
+ @name = name
28
+ @ref = ref
29
+ @git = git
30
+ @forge = forge
31
+ end
32
+ attr_reader :name, :ref
33
+
34
+ def git?
35
+ !!@git
36
+ end
37
+
38
+ def different?(other_module)
39
+ # Note that forge & git r10k modules have different naming convention so
40
+ # we don't need to compare url (forge is user/modulename and git is just
41
+ # modulename with :git attr as the url of the module
42
+ @ref != other_module.ref
43
+ end
44
+
45
+ def pretty_version_diff(other_module, include_url)
46
+ basic_compare = "#{ref} -> #{other_module.ref}"
47
+
48
+ # special case for github - generate compate url
49
+ if git? && other_module.git? && include_url
50
+ return "#{name} #{git_https}/compare/#{ref}...#{other_module.ref}"
51
+
52
+ elsif include_url
53
+ return "#{name} #{basic_compare} (#{web_url})"
54
+ else
55
+ return "#{name} #{basic_compare}"
56
+ end
57
+ end
58
+
59
+ def pretty_version(include_url)
60
+ basic_string = "#{name} at #{ref}"
61
+ if include_url
62
+ return "#{basic_string} (#{web_url})"
63
+ else
64
+ return basic_string
65
+ end
66
+
67
+ end
68
+
69
+ def git_https
70
+ if @git.start_with? "https://"
71
+ return @git
72
+ elsif @git.start_with? "git://"
73
+ return @git.gsub(/^git:/, "https:")
74
+ elsif @git.start_with? "git@"
75
+ return @git.gsub(":", "/").gsub(/^git@/, "https://").gsub(/.git$/, "")
76
+ end
77
+ end
78
+
79
+ def web_url
80
+ return git? ? git_https : @forge
81
+ end
82
+ end
83
+
84
+ class PuppetfileDiff
85
+ # Represents the difference between the puppetfile from one commit to another
86
+ def initialize(oldfile, newfile)
87
+ @oldfile = oldfile
88
+ @newfile = newfile
89
+ end
90
+
91
+ def changes
92
+ changed_modules = []
93
+ modules_in_common = Set.new(@oldfile.modules.keys).intersection Set.new(@newfile.modules.keys)
94
+ modules_in_common.each do |name|
95
+ new_module = @newfile.modules[name]
96
+ old_module = @oldfile.modules[name]
97
+ changed_modules << [old_module, new_module] if new_module.different? old_module
98
+ end
99
+ changed_modules
100
+ end
101
+
102
+ def additions
103
+ additions = []
104
+ @newfile.modules.each do |name, new_module|
105
+ additions << new_module unless @oldfile.modules[name]
106
+ end
107
+ additions
108
+ end
109
+
110
+ def removals
111
+ removals = []
112
+ @oldfile.modules.each do |name, old_module|
113
+ removals << old_module unless @newfile.modules[name]
114
+ end
115
+ removals
116
+ end
117
+
118
+ def print_differences(include_url)
119
+ # Print the additions, removals, and changes
120
+ output = []
121
+ puppetfile_changes = false
122
+ if removals.length > 0
123
+ puppetfile_changes = true
124
+ output << "Remove:"
125
+ end
126
+ removals.each do |old|
127
+ output << " #{old.pretty_version(include_url)}"
128
+ end
129
+
130
+ if additions.length > 0
131
+ puppetfile_changes = true
132
+ output << "Add:"
133
+ end
134
+ additions.each do |new|
135
+ output << " #{new.pretty_version(include_url)}"
136
+ end
137
+
138
+ if changes.length > 0
139
+ puppetfile_changes = true
140
+ output << "Change:"
141
+ end
142
+ changes.each do |old, new|
143
+ output << " #{new.pretty_version_diff(old, include_url)}"
144
+ end
145
+
146
+ if !puppetfile_changes
147
+ output << "No changes in Puppetfile"
148
+ end
149
+
150
+ output.each { |x| puts x }
151
+ end
152
+ end
153
+
154
+ class Commandline
155
+ def self.run
156
+ include_urls = false
157
+ opt_parser = OptionParser.new do |opt|
158
+
159
+ opt.banner = <<-EOF
160
+ Usage: r10kdiff [previous-ref] [current-ref]
161
+
162
+ Run from a git repository containing a Puppetfile.
163
+
164
+ previous-ref and current-ref are the git refs to compare
165
+ (optional, default to origin/BRANCH and BRANCH
166
+ where BRANCH is the currently checked-out git branch name)
167
+
168
+ EOF
169
+ opt.on("-h", "--help", "show help dialogue") do
170
+ puts opt_parser
171
+ exit
172
+ end
173
+ opt.on("-u", "--urls", "Include urls and github compare links in output") do
174
+ include_urls = true
175
+ end
176
+ end
177
+ opt_parser.parse!
178
+
179
+ if ARGV.length >= 2
180
+ oldref = ARGV[0]
181
+ newref = ARGV[1]
182
+ else # default to checked-out branch & its corresponding branch on origin
183
+ branch_name = File.basename `git symbolic-ref HEAD`.chomp
184
+ oldref = "origin/#{branch_name}"
185
+ newref = branch_name
186
+ end
187
+ oldfile_raw = PuppetfileDSL.new(`git show #{oldref}:Puppetfile`)
188
+ newfile_raw = PuppetfileDSL.new(`git --no-pager show #{newref}:Puppetfile`)
189
+ PuppetfileDiff.new(oldfile_raw, newfile_raw).print_differences(include_urls)
190
+ end
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: r10kdiff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Cosson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A small script for comparing r10k Puppetfiles between different git refs.
14
+ It's helpful for a development workflow with puppet r10k and Github as the output
15
+ is slightly nicer than 'git diff' and it can generate github compare links for the
16
+ full changesets represented by a change to a Puppetfile.
17
+ email: cosson@venmo.com
18
+ executables:
19
+ - r10kdiff
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/r10kdiff.rb
24
+ - bin/r10kdiff
25
+ homepage: https://github.com/dcosson/r10kdiff
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.14
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Compare r10k Puppetfiles
49
+ test_files: []