multipull 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 856b4c5e02bb5298fc6efb851bb844eece135e7bee48341f631f8d8a17958d02
4
- data.tar.gz: 3836ec83f7a4ca566aa560179eaf93d1d9eb3ca1ad933c66421a74caf43bcd01
3
+ metadata.gz: 614bc2642ad9fcf6fd4be667ab3ecfd8a1f04e787d958f3fd173aea0daa2c426
4
+ data.tar.gz: '081040ac22af8b8fe383486603ba450b5b0cfa1af678bc126bf2e8604903a1b3'
5
5
  SHA512:
6
- metadata.gz: ac920ccd6dafec05aadce61a4529c3d1ea89a3251ebf01be24b763a3da2c435aacaef4d23cc3bf5b6cb21bea0f0dd2c65c24f99d72868761a521f8b727a9077d
7
- data.tar.gz: e4f9d2111542c94983691ee4c2becd3ad9c30ce35c5bff8defd8d8294bf1251bfbaaff98d1c577efa897d3a55b5d8e5a1e50180e310c09a5dc93df32bf76ecb0
6
+ metadata.gz: d31192840e55c0859641b863dfe637d30ed685aa4eff59702725bd69a33f4ad31fd5e1b96a0a570eef771091d014a41ae6191c4296c1786de82bb6ea182a5843
7
+ data.tar.gz: 35565b8d20c8b40094c69e4f866997032d8f4710ba33e94ae9f54501651167e4623389dcb9c3662468c25dc37afbc9f3c7e3f48c3bd06879c1e284a7ff2fe263
data/README.md CHANGED
@@ -20,6 +20,19 @@ multipull
20
20
  multipull ~/Ruby # directory
21
21
  ```
22
22
 
23
+ Options after `--` are passed to `git pull` as is.
24
+
25
+ ```sh
26
+ multipull ~/Ruby -- --ff-only
27
+ multipull ~/Ruby -- -X ours
28
+ ```
29
+
30
+ Use `-P N` to pull N repositories in parallel. The output of each repository is shown as a block when it finishes, so the output does not get mixed up. Since prompts cannot be answered in parallel mode, repositories that require entering a password are treated as failed.
31
+
32
+ ```sh
33
+ multipull -P 8 ~/Ruby
34
+ ```
35
+
23
36
  ## What is this?
24
37
 
25
38
  The name of the command multipull was borrowed from the following article.
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'open3'
3
4
  require 'optparse'
5
+ require 'rbconfig'
4
6
  require_relative 'terminal_color'
5
7
 
6
8
  module Multipull
@@ -11,37 +13,59 @@ module Multipull
11
13
  # @options = {}
12
14
  @dirs = []
13
15
  @git_opts = []
16
+ @jobs = 1
17
+ @success = true
14
18
  end
15
19
 
16
20
  def run
17
21
  parse_options
18
22
  main
23
+ exit(1) unless @success
19
24
  end
20
25
 
21
26
  private
22
27
 
23
- def parse_options
28
+ def parse_options(argv = ARGV)
24
29
  opts = OptionParser.new
25
- opts.banner = 'Usage: multipull [options] [dir...]'
30
+ opts.banner = 'Usage: multipull [options] [dir...] [-- git-pull-options...]'
31
+ # Prevent abbreviations like -v from being expanded to --version
32
+ opts.require_exact = true
33
+
34
+ opts.on('-P', '--parallel N', Integer, 'Number of repositories to pull in parallel (default: 1)') do |n|
35
+ raise OptionParser::InvalidArgument, n.to_s if n < 1
36
+
37
+ @jobs = n
38
+ end
26
39
 
27
40
  opts.on('--version', 'Show version') do
28
41
  puts "multipull #{VERSION}"
29
42
  exit
30
43
  end
31
44
 
32
- opts.on('--help', 'Show help') do
45
+ opts.on('-h', '--help', 'Show help') do
33
46
  puts opts
34
47
  exit
35
48
  end
36
49
 
50
+ args = argv.dup
51
+ passthrough = []
52
+ if (i = args.index('--'))
53
+ passthrough = args[(i + 1)..]
54
+ args = args[0...i]
55
+ end
56
+
37
57
  begin
38
- opts.parse!
58
+ opts.parse!(args)
39
59
  rescue OptionParser::InvalidOption => e
40
60
  @git_opts += e.args
41
61
  retry
62
+ rescue OptionParser::ParseError => e
63
+ warn "multipull: #{e.message}"
64
+ exit 1
42
65
  end
43
66
 
44
- @dirs = ARGV.empty? ? ['.'] : ARGV
67
+ @git_opts += passthrough
68
+ @dirs = args.empty? ? ['.'] : args
45
69
  end
46
70
 
47
71
  def start_banner(dirs)
@@ -54,30 +78,16 @@ module Multipull
54
78
  dirs.each do |dir|
55
79
  unless File.directory?(dir)
56
80
  puts magenta("#{dir} is not a directory")
81
+ @success = false
57
82
  next
58
83
  end
59
84
 
60
- skipped = []
61
- succeeded = []
62
- failed = []
63
- Dir.chdir(dir) do
64
- Dir.glob('*/').each do |subdir|
65
- Dir.chdir(subdir) do
66
- dirname = subdir.delete_suffix('/')
67
- if File.directory?('.git')
68
- puts blue(bold(dirname))
69
- else
70
- skipped << dirname
71
- next
72
- end
73
- if system("git pull #{@git_opts.join(' ')}")
74
- succeeded << dirname
75
- else
76
- failed << dirname
77
- end
78
- end
79
- end
80
- end
85
+ repos, skipped = Dir.glob('*/', base: dir)
86
+ .map { |subdir| subdir.delete_suffix('/') }
87
+ .partition { |name| File.exist?(File.join(dir, name, '.git')) }
88
+ results = @jobs > 1 ? pull_parallel(dir, repos) : pull_sequential(dir, repos)
89
+ succeeded, failed = repos.partition { |name| results[name] }
90
+
81
91
  unless skipped.empty?
82
92
  warn \
83
93
  bold('Skipped : ') + skipped.join(' ')
@@ -86,11 +96,97 @@ module Multipull
86
96
  warn \
87
97
  bold(green('Succeeded : ')) + succeeded.join(' ')
88
98
  end
89
- unless failed.empty?
90
- warn \
91
- bold(magenta('Failed : ')) + failed.join(' ')
99
+ next if failed.empty?
100
+
101
+ @success = false
102
+ warn \
103
+ bold(magenta('Failed : ')) + failed.join(' ')
104
+ end
105
+ end
106
+
107
+ def pull_sequential(dir, repos)
108
+ repos.to_h do |name|
109
+ puts blue(bold(name))
110
+ [name, system('git', 'pull', *@git_opts, chdir: File.join(dir, name)) == true]
111
+ end
112
+ end
113
+
114
+ # Each repository's output is buffered and printed as a single block
115
+ # when it finishes, so that output from different repositories never interleaves.
116
+ def pull_parallel(dir, repos)
117
+ queue = Queue.new
118
+ repos.each { |name| queue << name }
119
+ queue.close
120
+ results = {}
121
+ mutex = Mutex.new
122
+
123
+ workers = Array.new([@jobs, repos.size].min) do
124
+ Thread.new do
125
+ while (name = queue.pop)
126
+ output, ok = capture_pull(File.join(dir, name))
127
+ mutex.synchronize do
128
+ results[name] = ok
129
+ puts blue(bold(name))
130
+ print output
131
+ puts unless output.empty? || output.end_with?("\n")
132
+ $stdout.flush
133
+ end
134
+ end
135
+ end
136
+ end
137
+ workers.each(&:join)
138
+ results
139
+ end
140
+
141
+ def capture_pull(path)
142
+ # A new session prevents configured SSH commands from reading /dev/tty,
143
+ # while preserving their identity files, ports, and other options.
144
+ Open3.popen2e(non_interactive_env(path), RbConfig.ruby, '-e',
145
+ 'Process.setsid if Process.respond_to?(:setsid); exec(*ARGV)',
146
+ '--', 'git', 'pull', *@git_opts, chdir: path) do |stdin, output, child|
147
+ stdin.close
148
+ begin
149
+ [output.read, child.value.success?]
150
+ ensure
151
+ # Detached children do not receive the parent's terminal signals.
152
+ stop_pull(child.pid) if child.alive?
92
153
  end
93
154
  end
155
+ rescue SystemCallError => e
156
+ ["#{e.message}\n", false]
157
+ end
158
+
159
+ def stop_pull(pid)
160
+ targets = Process.respond_to?(:setsid) ? [-pid, pid] : [pid]
161
+ targets.each do |target|
162
+ Process.kill('KILL', target)
163
+ break
164
+ rescue Errno::ESRCH
165
+ # The child may exit, or not have created its session yet.
166
+ next
167
+ end
168
+ end
169
+
170
+ # Prompts cannot be answered while running in parallel, so make git and ssh
171
+ # fail instead of waiting for input.
172
+ def non_interactive_env(path)
173
+ env = {
174
+ 'GIT_TERMINAL_PROMPT' => '0',
175
+ 'GIT_ASKPASS' => '',
176
+ 'SSH_ASKPASS' => '',
177
+ 'SSH_ASKPASS_REQUIRE' => 'never'
178
+ }
179
+ unless ENV['GIT_SSH_COMMAND'] || ENV['GIT_SSH'] || ssh_command_configured?(path)
180
+ env['GIT_SSH_COMMAND'] = 'ssh -o BatchMode=yes'
181
+ end
182
+ env
183
+ end
184
+
185
+ def ssh_command_configured?(path)
186
+ _output, status = Open3.capture2e('git', 'config', '--get', 'core.sshCommand', chdir: path)
187
+ status.success?
188
+ rescue SystemCallError
189
+ false
94
190
  end
95
191
  end
96
192
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Multipull
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multipull
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2022-08-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Git pull multiple repositories at once
14
13
  email:
@@ -28,7 +27,6 @@ homepage: https://github.com/kojix2/multipull
28
27
  licenses:
29
28
  - MIT
30
29
  metadata: {}
31
- post_install_message:
32
30
  rdoc_options: []
33
31
  require_paths:
34
32
  - lib
@@ -43,8 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
41
  - !ruby/object:Gem::Version
44
42
  version: '0'
45
43
  requirements: []
46
- rubygems_version: 3.3.7
47
- signing_key:
44
+ rubygems_version: 4.0.20
48
45
  specification_version: 4
49
46
  summary: Git pull multiple repositories at once
50
47
  test_files: []