public
Description: Syncs one directory to another (example: a git project to an svn repo)
Clone URL: git://github.com/technoweenie/duplikate.git
add report, dry_run
osteele (author)
Sat Apr 05 19:07:49 -0700 2008
commit  17034859650054beec134a5e04d8c51de259cfe8
tree    39dd1fdcf2410cf48ef67698d473e9b43faebe33
parent  3c067f2106033c5848dfcf697b52f7736e9e19cd
0
...
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
12
13
14
15
16
17
18
 
...
4
5
6
 
 
 
 
 
7
8
9
10
11
12
13
14
15
16
17
18
 
19
20
0
@@ -4,14 +4,16 @@ duplikate
0
 # process the differences between the two directories:
0
 
0
 dupe = Duplikate.process 'my-git-repo', 'my-svn-repo'
0
-dupe.added_files => [..., ...]
0
-dupe.added_directories => [..., ...]
0
-dupe.deleted_files => [..., ...]
0
-dupe.deleted_directories => [..., ...]
0
-dupe.existing_files => [..., ...]
0
+dupe.added_files #=> [..., ...]
0
+dupe.added_directories #=> [..., ...]
0
+dupe.deleted_files #=> [..., ...]
0
+dupe.deleted_directories #=> [..., ...]
0
+dupe.existing_files #=> [..., ...]
0
+
0
+dupe.report
0
 
0
 # Or actually make the changes and commit in svn:
0
 
0
 Duplikate.execute 'my-git-repo', 'my-svn-repo'
0
 
0
-TODO: Actual real-world usage!
0
\ No newline at end of file
0
+TODO: Actual real-world usage!
...
17
18
19
 
 
 
 
 
 
 
20
21
22
...
32
33
34
 
 
 
 
 
 
 
 
35
36
37
...
52
53
54
 
 
 
 
 
55
56
57
...
70
71
72
 
 
 
 
 
 
 
 
 
 
 
 
73
74
75
...
123
124
125
126
127
 
...
17
18
19
20
21
22
23
24
25
26
27
28
29
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
...
67
68
69
70
71
72
73
74
75
76
77
...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
...
155
156
157
 
158
159
0
@@ -17,6 +17,13 @@ class Duplikate
0
     dupe
0
   end
0
   
0
+ # Display the commands that +execute+ would execute.
0
+ def self.dry_run(source, dest, options = nil)
0
+ dupe = new(source, dest, options)
0
+ dupe.dry_run
0
+ dupe
0
+ end
0
+
0
   # Actually syncs the source path to the destination path. This
0
   # calls #process if needed, and then:
0
   #
0
@@ -32,6 +39,14 @@ class Duplikate
0
     dupe
0
   end
0
 
0
+ # Print the names of files and directories that would be added or
0
+ # deleted.
0
+ def self.report(source, dest, options = nil)
0
+ dupe = new(source, dest, options)
0
+ dupe.report
0
+ dupe
0
+ end
0
+
0
   def initialize(source, dest, options = nil)
0
     @options = options || {}
0
     @debug = @options[:debug]
0
@@ -52,6 +67,11 @@ class Duplikate
0
     process_path
0
   end
0
   
0
+ def dry_run(msg="<commit message>")
0
+ execute(msg, true)
0
+ puts commands.join("\n")
0
+ end
0
+
0
   def execute(message, test_mode = false)
0
     process if @existing_files.nil?
0
     unless test_mode
0
@@ -70,6 +90,18 @@ class Duplikate
0
     @commands << "ci -m '#{message}'"
0
     test_mode || execute_commands
0
   end
0
+
0
+ def report
0
+ rows = %w[added_files added_directories deleted_files deleted_directories existing_files].each do |name|
0
+ files = self.send name.intern
0
+ if files.any?
0
+ puts name.capitalize.gsub(/_(\w)/) { " #{$1.upcase}" }
0
+ puts '=' * name.to_s.length
0
+ puts files.join(', ')
0
+ puts
0
+ end
0
+ end
0
+ end
0
 
0
 protected
0
   def svn_command(args)
0
@@ -123,4 +155,4 @@ protected
0
       @added_files << file.to_s
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
54
55
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
58
59
...
92
93
94
95
96
 
...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
...
115
116
117
 
118
119
0
@@ -54,6 +54,29 @@ describe Duplikate do
0
     @duplikate.commands.should include("rm foo/deleteme")
0
     @duplikate.commands.should include("ci -m 'running spec'")
0
   end
0
+
0
+ describe :report do
0
+ it "prints changes" do
0
+ text = capturing_stdout { @duplikate.report }
0
+ text.should =~ /Added Files\n=+\naddme.txt/
0
+ text.should =~ /Added Directories\n=+\nfoo\/addme/
0
+ text.should =~ /Deleted Files\n=+\ndeleteme.txt/
0
+ text.should =~ /Deleted Directories\n=+\ndeleteme/
0
+ end
0
+ end
0
+
0
+ describe :dry_run do
0
+ it "prints commands" do
0
+ lines = capturing_stdout { @duplikate.dry_run }.split("\n")
0
+ lines.length.should == 6
0
+ lines.should include("add addme.txt")
0
+ lines.should include("add foo/addme")
0
+ lines.should include("rm deleteme.txt")
0
+ lines.should include("rm deleteme")
0
+ lines.should include("rm foo/deleteme")
0
+ lines.should include("ci -m '<commit message>'")
0
+ end
0
+ end
0
 end
0
 
0
 describe Duplikate, "syncing two directories" do
0
@@ -92,4 +115,4 @@ describe Duplikate, "syncing two directories" do
0
     file = 'foo/same/changed.txt'
0
     IO.read(@source + file).should == IO.read(@dest + file)
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
1
2
 
3
4
5
...
8
9
10
 
 
 
 
 
 
 
 
 
 
 
11
12
13
 
...
1
2
3
4
5
6
...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
24
25
0
@@ -1,5 +1,6 @@
0
 require 'rubygems'
0
 require 'spec'
0
+require 'stringio'
0
 
0
 begin
0
   require 'ruby-debug'
0
@@ -8,5 +9,16 @@ rescue LoadError
0
   # no debugging
0
 end
0
 
0
+def capturing_stdout
0
+ saved_stdout = $stdout
0
+ begin
0
+ $stdout = StringIO.new
0
+ yield
0
+ $stdout.string
0
+ ensure
0
+ $stdout = saved_stdout
0
+ end
0
+end
0
+
0
 $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
0
-require 'duplikate'
0
\ No newline at end of file
0
+require 'duplikate'

Comments

    No one has commented yet.