-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto-update.rb
executable file
·54 lines (45 loc) · 1.46 KB
/
auto-update.rb
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
#!/usr/bin/env ruby
#
# Utility to automatically commit all changes to dotfiles.
#
# This script manages a branch named after the hostname of the current machine.
# When ran, it adds a commit for each file that was added, modified or removed.
#
# TODO:
# - Add checks for branch
# - Handle renames/copies
require 'fileutils'
require 'shellwords'
def commit(filename, message)
puts "committing #{filename}"
`git add #{Shellwords.escape(filename)}`
exit $?.exitstatus unless $?.success?
`git commit -m #{Shellwords.escape("auto: #{message}")}`
exit $?.exitstatus unless $?.success?
end
FileUtils.cd File.dirname(__FILE__) do
puts "resetting"
`git reset`
exit $?.exitstatus unless $?.success?
`git status --porcelain`.each_line do |line|
filename = line[3..-2]
case line
when /^ M/, /^MM/
added, deleted = `git diff --numstat #{Shellwords.escape(filename)}`.split("\t")[0..1].map(&:to_i)
commit filename, "modified +#{added} -#{deleted} in #{filename}"
when /^ A/, /^A./, /^\?\?/
commit filename, "added #{filename}"
when /^ D/, /^D./
commit filename, "deleted #{filename}"
end
end
puts "pulling"
res = `git pull --rebase`
exit $?.exitstatus unless $?.success?
unless res.include? "Already up to date."
system "nvim +PlugUpdate +qa"
end
puts "pushing"
`git push -q`
exit $?.exitstatus unless $?.success?
end