Dahistory 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Dahistory.gemspec CHANGED
@@ -24,5 +24,5 @@ Gem::Specification.new do |s|
24
24
 
25
25
  # s.rubyforge_project = "Dahistory"
26
26
  # specify any dependencies here; for example:
27
- # s.add_runtime_dependency "rest-client"
27
+ s.add_runtime_dependency "Exit_Zero"
28
28
  end
data/README.md CHANGED
@@ -15,20 +15,22 @@ Installation
15
15
 
16
16
  gem 'Dahistory'
17
17
 
18
- Usage
18
+ Useage
19
19
  ------
20
20
 
21
- require "Dahistory"
21
+ # In your Chef-Solo recipe or your Ruby code...
22
22
 
23
+ require "Dahistory"
24
+
23
25
  Dahistory "some/file.txt"
24
26
 
25
27
  # Checks your directory (default "./history").
26
28
  # If not found there, saves copy of file in ./pending dir and
27
29
  # raises Dahistory::Pending_File, "pending/HOSTNAME,path,some,file.txt.TIMESTAMP"
28
-
30
+ #
29
31
  # You review the file,
30
32
  # move the file from the pending directory to your source/history dir, and
31
- # re-do your last command (Capistrano, Chef, Puppet, etc.)
33
+ # run your recipe/code again.
32
34
 
33
35
  Override the default settings:
34
36
 
@@ -49,6 +51,45 @@ Both **def Dahistory** and **class Dahistory** are defined.
49
51
  All the code is in one file and less than 150 lines:
50
52
  [lib/Dahistory.rb](https://github.com/da99/Dahistory/blob/master/lib/Dahistory.rb)
51
53
 
54
+ Useage: Dahistory + Git
55
+ -----------------------
56
+
57
+ Dahistory has some methods that can be used as shortcuts when using git.
58
+
59
+ Dahistory { |o|
60
+
61
+ o.file "some/file.txt"
62
+
63
+ o.git_add_commit
64
+
65
+ # ... is a shortcut for:
66
+ o.on_raise_pending {
67
+ `git add #{o.backup_file}`
68
+ `git commit -m "Backup: #{o.backup_file}"`
69
+ }
70
+
71
+ }
72
+
73
+ `git_push` does the same above, but with "git push":
74
+
75
+ o.git_add_commit_push
76
+
77
+ # or...
78
+ o.git_add_commit_push 'my_remote my_branch'
79
+
80
+ You can specify the git repo:
81
+
82
+ Dahistory { |o|
83
+
84
+ o.file "some/file.txt"
85
+
86
+ Dir.chdir("/path/to/project") {
87
+ o.git_add_commit
88
+ # or...
89
+ o.git_add_commit_push
90
+ }
91
+ }
92
+
52
93
  Run Tests
53
94
  ---------
54
95
 
@@ -1,3 +1,3 @@
1
1
  class Dahistory
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/Dahistory.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'Dahistory/version'
2
+ require 'Exit_Zero'
2
3
 
3
4
  def Dahistory file = nil
4
5
  da = Dahistory.new { |o|
@@ -17,6 +18,7 @@ class Dahistory
17
18
  module Base
18
19
 
19
20
  def initialize file_path = nil
21
+ @git = false
20
22
  @file = nil
21
23
  file!(file_path) if file_path
22
24
  dirs './history'
@@ -84,6 +86,14 @@ class Dahistory
84
86
  @backup_file = str
85
87
  end
86
88
 
89
+ def git_add_commit
90
+ @git = :commit
91
+ end
92
+
93
+ def git_add_commit_push args = ""
94
+ @git = args
95
+ end
96
+
87
97
  def save
88
98
 
89
99
  content = File.read(file)
@@ -93,6 +103,17 @@ class Dahistory
93
103
 
94
104
  if !old
95
105
  File.write(backup_file, content) unless self.class.find_file_copy(file, pending_dir)
106
+
107
+
108
+ if @git
109
+ Exit_Zero "git add #{backup_file}"
110
+ Exit_Zero %! git commit -m "Backup: #{backup_file}"!
111
+ end
112
+
113
+ if @git.is_a?(String)
114
+ Exit_Zero %! git push #{@git} !
115
+ end
116
+
96
117
  on_raise_pending.call if on_raise_pending
97
118
  raise Pending, backup_file
98
119
  end
data/spec/main.rb CHANGED
@@ -12,18 +12,43 @@ def chdir
12
12
  end
13
13
 
14
14
  def reset_dirs
15
- `rm -rf #{FOLDER}`
16
- `mkdir #{FOLDER}`
17
- `mkdir #{FOLDER}/files`
18
- `mkdir #{FOLDER}/history`
19
- `mkdir #{FOLDER}/history/blue`
20
- `mkdir #{FOLDER}/history/red`
21
- `mkdir #{FOLDER}/history/yellow`
22
- `mkdir #{FOLDER}/pending`
15
+ Exit_Zero "rm -rf #{FOLDER}"
16
+ Exit_Zero "mkdir #{FOLDER}"
17
+ Exit_Zero "mkdir #{FOLDER}/files"
18
+ Exit_Zero "mkdir #{FOLDER}/history"
19
+ Exit_Zero "mkdir #{FOLDER}/history/blue"
20
+ Exit_Zero "mkdir #{FOLDER}/history/red"
21
+ Exit_Zero "mkdir #{FOLDER}/history/yellow"
22
+ Exit_Zero "mkdir #{FOLDER}/pending"
23
23
  end
24
24
 
25
25
  reset_dirs
26
26
 
27
+ shared( "git" ) {
28
+
29
+ before {
30
+ @proj = "#{FOLDER}/project_#{rand 1000}"
31
+ @git_repo = "#{@proj}.git"
32
+ Exit_Zero "mkdir -p #{@git_repo}"
33
+ Exit_Zero "cd #{@git_repo} && git init --bare"
34
+ Exit_Zero "mkdir -p #{@proj}"
35
+
36
+ Dir.chdir(@proj) {
37
+ Exit_Zero "mkdir files"
38
+ Exit_Zero "mkdir history"
39
+ Exit_Zero "mkdir pending"
40
+ Exit_Zero "git init"
41
+ Exit_Zero "git remote add origin #{@git_repo}"
42
+ Exit_Zero "touch README.md"
43
+ Exit_Zero "git add ."
44
+ Exit_Zero %! git commit -m "First commit." !
45
+ Exit_Zero "git push -u origin master"
46
+ }
47
+ @file = "files/#{rand 1000}.txt"
48
+ }
49
+
50
+ }
51
+
27
52
  # ======== Include the tests.
28
53
  if ARGV.size > 1 && ARGV[1, ARGV.size - 1].detect { |a| File.exists?(a) }
29
54
  # Do nothing. Bacon grabs the file.
@@ -84,3 +84,59 @@ describe "Dahistory: existing file in ./history" do
84
84
 
85
85
  end # === Dahistory: existing file in ./history
86
86
 
87
+ describe "Dahistory :git_add_commit" do
88
+
89
+ behaves_like "git"
90
+
91
+ it "adds backup file as a commit" do
92
+ target = nil
93
+
94
+ Dir.chdir(@proj) {
95
+ File.write @file, @file
96
+ Dahistory { |o|
97
+ o.file @file
98
+ o.git_add_commit
99
+ target = "Backup: #{o.backup_file}"
100
+ } rescue nil
101
+
102
+ Exit_Zero('git log -n 1 --oneline --decorate=short')
103
+ .out[target].should == target
104
+ }
105
+ end
106
+
107
+ end # === Dahistory :git_add_commit
108
+
109
+ describe "Dahistory :git_add_commit_push" do
110
+
111
+ behaves_like "git"
112
+
113
+ it "pushs git repo to remote" do
114
+ target = "Backup: #{@file}"
115
+
116
+ Dir.chdir(@proj) {
117
+ File.write @file, @file
118
+ Dahistory { |o|
119
+ o.file @file
120
+ o.git_add_commit_push
121
+ } rescue nil
122
+
123
+ Exit_Zero('git push 2>&1').out["Everything up-to-date"].should == "Everything up-to-date"
124
+ }
125
+ end
126
+
127
+ it "passes argument to 'git push'" do
128
+ target = "Backup: #{@file}"
129
+
130
+ Dir.chdir(@proj) {
131
+ File.write @file, @file
132
+ should.raise(Exit_Zero::Non_Zero) {
133
+ Dahistory { |o|
134
+ o.file @file
135
+ o.git_add_commit_push "old_remote someting"
136
+ }
137
+ }.message.should.match %r!'old_remote' does not appear to be a git repository!
138
+ }
139
+ end
140
+
141
+ end # === Dahistory :git_add_commit_push
142
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Dahistory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000 Z
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bacon
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: Exit_Zero
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: Compares file to other files in specified dir(s) and backups if it does
79
95
  not exist.
80
96
  email: