rebase_pusher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 210c9f95029d1cae3b877543d0ff198a4265c821be7259f5408f58a4418448e6
4
+ data.tar.gz: 8c7f2eb6e24a355cdb253847085850002e825788b3452abe4ad6a05773b47943
5
+ SHA512:
6
+ metadata.gz: e0923d87a648c7fd793bc13d8156248ae2f025f02f1e016e06aa83c8253020711ed25bff2f491612342698215a1eff1d9352000a163ec13afebe6636e159353e
7
+ data.tar.gz: 1c1a9cfb77237650ff34a277b967a7bb353c868604f86c42d6e35a8f851e27a3226620d65b6ab668594677120b00ebbfcefbf5f5f0b4658c274069cacb2452dd
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require_relative "../lib/rebase_pusher"
6
+ require "pry"
7
+
8
+ Pry.start
data/bin/rebase_push ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require_relative "../lib/rebase_pusher"
6
+
7
+ options = {}
8
+
9
+ ARGV << '-h' if ARGV.empty?
10
+
11
+ # https://docs.ruby-lang.org/en/2.1.0/OptionParser.html
12
+ OptionParser.new do |opts|
13
+ opts.banner =<<~EOF
14
+ Description: this cli rebase all YOUR branches onto repository default branch and force push them to remote real quick
15
+
16
+ Example:
17
+ rebase_push -t reset
18
+ rebase_push -t rebase
19
+ rebase_push -t force_push # this is slow, calm down
20
+ rebase_push -t reset && rebase_push -t rebase
21
+
22
+ Options:
23
+ EOF
24
+
25
+ opts.on_tail("-h", "--help", "Show this message") do
26
+ puts opts
27
+ exit
28
+ end
29
+
30
+ opts.on("-t", "--type [TYPE]", %i[rebase reset force_push], "Select operation type (reset, rebase, force_push)") do |t|
31
+ options[:operation_type] = t
32
+ end
33
+
34
+ opts.on("-v", "--verbose", "Run verbosely") do |v|
35
+ options[:verbose] = v
36
+ end
37
+ end.parse!
38
+
39
+ RebasePusher.new(options).run
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RebasePusher
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rebase_pusher/version"
4
+
5
+ require "optparse"
6
+ require "open3"
7
+ require "json"
8
+
9
+ class RebasePusher
10
+ attr_reader :options
11
+ attr_reader :io
12
+ attr_reader :original_branch
13
+
14
+ def initialize(options, io = $stdout)
15
+ @options = options
16
+ if options[:operation_type].nil?
17
+ warn "TYPE must present"
18
+ exit 1
19
+ end
20
+
21
+ @io = io
22
+ @original_branch = sh("git rev-parse --abbrev-ref HEAD")
23
+ end
24
+
25
+ def run
26
+ io.puts "skipped branches: #{branches - my_branches}"
27
+ sh("git checkout --quiet #{default_branch}")
28
+
29
+ case options[:operation_type]
30
+ when :rebase
31
+ my_branches.each do |branch|
32
+ io.print "." if !options[:verbose]
33
+ sh("git rebase --quiet #{default_branch} #{branch}")
34
+ end
35
+
36
+ io.puts if !options[:verbose]
37
+ io.puts "all my branches are rebased"
38
+ when :reset
39
+ my_branches.each do |branch|
40
+ io.print "." if !options[:verbose]
41
+ sh("git branch --force #{branch} #{branch}@{u}")
42
+ end
43
+
44
+ io.puts if !options[:verbose]
45
+ io.puts "all my branches are reset"
46
+ when :force_push
47
+ # https://git-scm.com/docs/git-push
48
+ # refspec: <src>:<dst>
49
+ my_branches.each do |branch|
50
+ io.print "." if !options[:verbose]
51
+ sh("git push origin --quiet --force-with-lease --force-if-includes #{branch}:#{branch}")
52
+ end
53
+
54
+ io.puts if !options[:verbose]
55
+ io.puts "all my branches are force pushed"
56
+ else
57
+ raise "NOT SUPPORTTED"
58
+ end
59
+
60
+ sh("git checkout --quiet #{original_branch}")
61
+ end
62
+
63
+ private
64
+
65
+ # Ensure I never touch other people's branch
66
+ def my_branches
67
+ @my_branches ||= branches.select do |branch|
68
+ merge_base_commitid = sh("git merge-base #{default_branch} #{branch}").chomp
69
+ author_emails = sh("git log --format='%ae' #{merge_base_commitid}..#{branch}").split("\n").uniq
70
+
71
+ !author_emails.empty? && author_emails.all? { |email| email == my_email }
72
+ end
73
+ end
74
+
75
+ def default_branch
76
+ @default_branch ||= sh("git rev-parse --abbrev-ref origin/HEAD").chomp
77
+ end
78
+
79
+ def branches
80
+ @branches ||= sh('git branch | grep "[^* ]+" -Eo').split("\n")
81
+ end
82
+
83
+ def my_email
84
+ @my_email ||= sh("git config --get user.email").chomp
85
+ end
86
+
87
+ def sh(cmd)
88
+ if options[:verbose]
89
+ io.puts "running command: #{cmd}"
90
+ end
91
+
92
+ out, err, status = Open3.capture3(*cmd)
93
+
94
+ if status.success?
95
+ out
96
+ else
97
+ info = {
98
+ output: out,
99
+ error: err,
100
+ status: status,
101
+ backtraces: caller
102
+ }
103
+
104
+ warn info.to_json
105
+ raise "command failed: #{cmd}"
106
+ end
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rebase_pusher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lijunwei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: git rebase and push for all my branches
14
+ email:
15
+ - ljw532344863@sina.com
16
+ executables:
17
+ - rebase_push
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/console
22
+ - bin/rebase_push
23
+ - lib/rebase_pusher.rb
24
+ - lib/rebase_pusher/version.rb
25
+ homepage: https://github.com/liijunwei/rebase_pusher
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ allowed_push_host: https://rubygems.org/
30
+ rubygems_mfa_required: 'true'
31
+ homepage_uri: https://github.com/liijunwei/rebase_pusher
32
+ source_code_uri: https://github.com/liijunwei/rebase_pusher
33
+ changelog_uri: https://github.com/liijunwei/rebase_pusher
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.6.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.3.22
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: git rebase and push for all my branches
53
+ test_files: []