-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-exec-if-changed
executable file
·64 lines (48 loc) · 1.54 KB
/
git-exec-if-changed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
paths = []
since = nil
since_last_exec = nil
verbose = false
parser = OptionParser.new do |opts|
opts.banner = 'usage: git-exec-if-changed [<options>] [--] <cmd>...'
opts.on('-p <path>', '--path=<path>', 'Path to test if changed (can be specified multiple times)') do |path|
paths << path
end
opts.on('-s <commit>', '--since=<commit>', 'Find changes since <commit> (use empty string for the root commit)') do |commit|
since = commit == '' ? :root : commit
end
opts.on('-S <history-file>', '--since-last-exec=<history-file>', 'Behave as --since=$(cat <history-file>) if it exists; write the current revision to <history-file> before exec') do |path|
since_last_exec = Pathname(path)
end
opts.on('-v', '--verbose') do |v|
verbose = v
end
end
parser.parse!
if ARGV.empty?
$stderr.puts parser.help
exit 1
end
if paths.empty?
$stderr.puts 'error: no paths specified'
exit 1
end
if since.nil? && since_last_exec.nil?
$stderr.puts 'error: neither --since nor --since-last-exec is specified'
exit 1
end
if since.nil?
if since_last_exec.file?
since = since_last_exec.read.chomp
else
since = :root
end
end
if since == :root || !system('git', 'diff', '--quiet', since, '--', *paths)
$stderr.puts "exec-if-changed: changed since #{since == :root ? '(root)' : since}" if verbose
since_last_exec.write(`git rev-parse HEAD`) if since_last_exec
exec *ARGV
end
puts "exec-if-changed: not changed since #{since == :root ? '(root)' : since}" if verbose