Class: Oxidized::GitCrypt

Inherits:
Output
  • Object
show all
Defined in:
lib/oxidized/output/gitcrypt.rb

Defined Under Namespace

Classes: GitCryptError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Output

#cfg_to_str

Constructor Details

#initializeGitCrypt

Returns a new instance of GitCrypt.



12
13
14
15
16
17
18
19
# File 'lib/oxidized/output/gitcrypt.rb', line 12

def initialize
	@cfg = Oxidized.config.output.gitcrypt
	@gitcrypt_cmd = "/usr/bin/git-crypt"
	@gitcrypt_init = @gitcrypt_cmd + " init"
	@gitcrypt_unlock = @gitcrypt_cmd + " unlock"
	@gitcrypt_lock = @gitcrypt_cmd + " lock"
	@gitcrypt_adduser = @gitcrypt_cmd + " add-gpg-user --trusted "
end

Instance Attribute Details

#commitrefObject (readonly)

Returns the value of attribute commitref.



10
11
12
# File 'lib/oxidized/output/gitcrypt.rb', line 10

def commitref
  @commitref
end

Instance Method Details

#crypt_init(repo) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oxidized/output/gitcrypt.rb', line 39

def crypt_init repo
	repo.chdir do
		system(@gitcrypt_init)
		@cfg.users.each do |user|
			system("#{@gitcrypt_adduser} #{user}")
		end
		File.write(".gitattributes", "* filter=git-crypt diff=git-crypt\n.gitattributes !filter !diff")
		repo.add(".gitattributes")
		repo.commit("Initial commit: crypt all config files")
	end
end

#fetch(node, group) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/oxidized/output/gitcrypt.rb', line 90

def fetch node, group
	begin
		repo, path = yield_repo_and_path(node, group)
		repo = Git.open repo
		unlock repo
		index = repo.index
		# Empty repo ?
		empty = File.exists? index.path
		if empty
			raise 'Empty git repo'
		else
			File.read path
		end
		lock repo
	rescue
		'node not found'
	end
end

#get_diff(node, group, oid1, oid2) ⇒ Object

give a hash with the patch of a diff between 2 revision and the stats (added and deleted lines)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/oxidized/output/gitcrypt.rb', line 149

def get_diff node, group, oid1, oid2
	begin
		diff_commits = nil
		repo, path = yield_repo_and_path(node, group)
		repo = Git.open repo
		unlock repo
		commit = repo.gcommit(oid1)

		if oid2
			commit_old = repo.gcommit(oid2)
			diff = repo.diff(commit_old, commit)
			stats = [diff.stats[:files][node.name][:insertions], diff.stats[:files][node.name][:deletions]]
			diff.each do |patch|
				if /#{node.name}\s+/.match(patch.patch.to_s.lines.first)
					diff_commits = {:patch => patch.patch.to_s, :stat => stats}
					break
				end
			end
		else
			stat = commit.parents[0].diff(commit).stats
			stat = [stat[:files][node.name][:insertions],stat[:files][node.name][:deletions]]
			patch = commit.parents[0].diff(commit).patch
			diff_commits = {:patch => patch, :stat => stat}
		end
		lock repo
		diff_commits
	rescue
		'no diffs'
	ensure
		lock repo
	end
end

#get_version(node, group, oid) ⇒ Object

give the blob of a specific revision



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/oxidized/output/gitcrypt.rb', line 135

def get_version node, group, oid
	begin
		repo, path = yield_repo_and_path(node, group)
		repo = Git.open repo
		unlock repo
		repo.gtree(oid).files[path].contents
	rescue
		'version not found'
	ensure
		lock repo
	end
end

#lock(repo) ⇒ Object



51
52
53
54
55
# File 'lib/oxidized/output/gitcrypt.rb', line 51

def lock repo
	repo.chdir do
		system(@gitcrypt_lock)
	end
end

#setupObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/oxidized/output/gitcrypt.rb', line 21

def setup
	if @cfg.empty?
		Oxidized.asetus.user.output.gitcrypt.user  = 'Oxidized'
		Oxidized.asetus.user.output.gitcrypt.email = '[email protected]'
		Oxidized.asetus.user.output.gitcrypt.repo  =  File.join(Config::Root, 'oxidized.git')
		Oxidized.asetus.save :user
		raise NoConfig, 'no output git config, edit ~/.config/oxidized/config'
	end

	if @cfg.repo.respond_to?(:each)
		@cfg.repo.each do |group, repo|
			@cfg.repo["#{group}="] = File.expand_path repo
		end
	else
		@cfg.repo = File.expand_path @cfg.repo
	end
end

#store(file, outputs, opt = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oxidized/output/gitcrypt.rb', line 63

def store file, outputs, opt={}
	@msg   = opt[:msg]
	@user  = (opt[:user]  or @cfg.user)
	@email = (opt[:email] or @cfg.email)
	@opt   = opt
	@commitref = nil
	repo   = @cfg.repo

	outputs.types.each do |type|
		type_cfg = ''
		type_repo = File.join(File.dirname(repo), type + '.git')
		outputs.type(type).each do |output|
			(type_cfg << output; next) if not output.name
			type_file = file + '--' + output.name
			if @cfg.type_as_directory?
				type_file = type + '/' + type_file
				type_repo = repo
			end
			update type_repo, type_file, output
		end
		update type_repo, file, type_cfg
	end

	update repo, file, outputs.to_cfg
end

#unlock(repo) ⇒ Object



57
58
59
60
61
# File 'lib/oxidized/output/gitcrypt.rb', line 57

def unlock repo
	repo.chdir do
		system(@gitcrypt_unlock)
	end
end

#version(node, group) ⇒ Object

give a hash of all oid revision for the given node, and the date of the commit



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/oxidized/output/gitcrypt.rb', line 110

def version node, group
	begin
		repo, path = yield_repo_and_path(node, group)

		repo = Git.open repo
		unlock repo
		walker = repo.log.path(path)
		i = -1
		tab  = []
		walker.each do |commit|
			hash = {}
			hash[:date] = commit.date.to_s
			hash[:oid] = commit.objectish
			hash[:author] = commit.author
			hash[:message] = commit.message
			tab[i += 1] = hash
		end
		walker.reset
		tab
	rescue
		'node not found'
	end
end