-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitup.rb
More file actions
executable file
·146 lines (133 loc) · 4.25 KB
/
gitup.rb
File metadata and controls
executable file
·146 lines (133 loc) · 4.25 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env ruby
class Gitup
DEFAULT_APPLICATION = 'Transmit'
def initialize(argv)
# Set default application
@application = DEFAULT_APPLICATION
# Parse ARGV for application option.
# This is awkward, but I'm too lazy to learn how to use OptionParser.
argv.join('|').scan(/--application=[\w\s]+/) do |app|
@application = app.match(/[\w\s]+$/)[0]
argv.delete app
end
# This option is a bit easier to find.
@skip_preview = argv.delete('-s') || argv.delete('--skip-preview')
# Do the help thing.
if argv.include?('-h') || argv.include?('--help')
@help = true
elsif argv.empty?
# This just asks git-log for the last commit
@options = '-1'
else
# If any other options/arguments are given, just pass them on to git-log
options_array = []
argv.each do |arg|
# Make sure options with a space are quoted
options_array<< arg.gsub(/=((.*\s)+.*)$/, '="\1"')
end
@options = options_array.join ' '
end
@git_dir = `git rev-parse --show-cdup`.strip
@ignore_file = @git_dir + '.gitupignore'
end
# Do the thing
def process!
if @help
help
elsif commits.empty?
puts "No files found."
else
if @skip_preview
upload_files
else
prompt
end
end
end
# Get the file list from git-log
def git_log
puts "git log --name-status --relative --pretty=format:\">>> %h %s\" #{@options}"
@git_log ||= `git log --name-status --relative --pretty=format:">>> %h %s" #{@options}`
end
# Get groups of files (grouped by commit)
def commits
@commits ||= git_log.split(/^\n?>>> /).collect do |group|
files = group.split("\n")
# The name is the commit message
name = files.shift
# What's left is the list of files
files.collect! do |file|
# Collect the ones that were added or modified
file.sub(/[AM]\t/, '') if file =~ /[AM]\t/
end
# Get rid of the nils
files.compact!
# We don't want any files that have been deleted
files.delete_if { |file| !File.exists?(file) }
# Exclude files matching .gitupignore
if File.exists?(@ignore_file)
@ignores = File.readlines(@ignore_file).collect { |line| line.strip }
@ignores.each do |ignore|
files.delete_if { |file| file.match(ignore) }
end
end
# Make the hash for the commit
{ :name => name, :files => files } unless name.nil? || files.nil? || files.empty?
end.compact
end
# Get the entire list of files
def file_list
files = commits.collect { |commit| commit[:files] }.flatten
end
# Displays help info
def help
puts "
Usage: gitup.rb [-s|--skip-preview] [--application=<app_name>] <git-log options>
Calling gitup.rb without arguments will build the file list from the most recent commit.
See Commit Limiting in git-log help for options on specifying the commits.
Other options:
--application=<app_name> Specify an application other than Transmit.
-h, --help Show this message.
-s, --skip-preview Send files straight to Transmit without a prompt.
"
end
# Lists commits and prompts for action
def prompt
# List commits and file counts
commits.each do |commit|
puts commit[:files].size.to_s.rjust(6) + ' Files:' + commit[:name]
end
# Prompt for action
print "Continue/List Files/Abort [Cla] "
input = STDIN.gets.chomp.strip.downcase
# Respond accordingly
case input
when 'l'
list_files
when 'c', ''
upload_files
when 'a'
puts " Aborted"
exit
else
prompt
end
end
# Sends files to Transmit
def upload_files
puts "Files sent to #{@application}!"
puts `open -ga '#{@application}' #{file_list.collect{|file| '"' + file + '"' }.join(' ')}`
end
# Displays the complete file list
def list_files
commits.each do |commit|
puts "========================================================="
puts "Files: #{commit[:name]}"
puts "---------------------------------------------------------"
puts commit[:files].join("\n")
end
puts "========================================================="
prompt
end
end
Gitup.new(ARGV).process!