branchtree 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: 983ddedd92a152d2f1e8d0e155b0f561fbcb3fb80e07be7f70770c28dbf6261e
4
+ data.tar.gz: c06bf9c9da1ccf20979d685fe628f309f73a3153516cd4c95ad11ad97ccd20ed
5
+ SHA512:
6
+ metadata.gz: 84e2dfe9e7ff0a3fe2b235b71212e4101185a2f8131d11331d6c2c320e9d852ce123317817a439cad5c424ed7439e9fb5865c21adcf24a6c2b2b30e5ef63b651
7
+ data.tar.gz: '069544c6e2f98eda7c998fe7fc622725f13dcc5e172f1c375182327005fae4851c2f598a6078023bcb7da20d30a9ba6bf9b70e5ab2cea874ca02de458f989ba7'
@@ -0,0 +1,35 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run tests
35
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at smashwilson@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in branchtree.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ branchtree (0.1.0)
5
+ tty-command (~> 0.10.0)
6
+ tty-logger (~> 0.6.0)
7
+ tty-option (~> 0.1.0)
8
+ tty-prompt (~> 0.23.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ diff-lcs (1.4.4)
14
+ pastel (0.8.0)
15
+ tty-color (~> 0.5)
16
+ rake (12.3.3)
17
+ rspec (3.10.0)
18
+ rspec-core (~> 3.10.0)
19
+ rspec-expectations (~> 3.10.0)
20
+ rspec-mocks (~> 3.10.0)
21
+ rspec-core (3.10.1)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-expectations (3.10.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.10.0)
26
+ rspec-mocks (3.10.2)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-support (3.10.2)
30
+ tty-color (0.6.0)
31
+ tty-command (0.10.1)
32
+ pastel (~> 0.8)
33
+ tty-cursor (0.7.1)
34
+ tty-logger (0.6.0)
35
+ pastel (~> 0.8)
36
+ tty-option (0.1.0)
37
+ tty-prompt (0.23.0)
38
+ pastel (~> 0.8)
39
+ tty-reader (~> 0.8)
40
+ tty-reader (0.9.0)
41
+ tty-cursor (~> 0.7)
42
+ tty-screen (~> 0.8)
43
+ wisper (~> 2.0)
44
+ tty-screen (0.8.1)
45
+ wisper (2.0.1)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ branchtree!
52
+ rake (~> 12.0)
53
+ rspec (~> 3.0)
54
+
55
+ BUNDLED WITH
56
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Ash Wilson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Branchtree
2
+
3
+ Command-line tool to interactively manage chains or trees of dependent branches in a git repository.
4
+
5
+ ## Installation
6
+
7
+ Install the gem from Rubygems:
8
+
9
+ $ gem install branchtree
10
+
11
+ ## Usage
12
+
13
+ Configure your desired branch topology by creating a file called `branchtree-map.yml` in your home directory.
14
+
15
+ ```yml
16
+ ---
17
+ - branch: first-branch
18
+ children:
19
+ - branch: second-branch-v1
20
+ children:
21
+ - branch: third-branch-v1
22
+ rebase: true
23
+ - branch: second-branch-v2
24
+ ```
25
+
26
+ View your current place in the branch tree by running:
27
+
28
+ ```
29
+ $ branchtree show
30
+
31
+ # Or:
32
+
33
+ $ branchtree
34
+ ```
35
+
36
+ Interactively check out a different branch in the tree with:
37
+
38
+ ```
39
+ $ branchtree checkout
40
+ ```
41
+
42
+ If you've made commits to a non-leaf branch, run this to propagate changes forward through the tree with merges and rebases:
43
+
44
+ ```
45
+ $ branchtree update
46
+ ```
47
+
48
+ To see the full usage, run:
49
+
50
+ ```
51
+ $ branchtree help
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/branchtree. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/branchtree/blob/master/CODE_OF_CONDUCT.md).
63
+
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the Branchtree project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/branchtree/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "branchtree"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ require_relative 'lib/branchtree/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "branchtree"
5
+ spec.version = Branchtree::VERSION
6
+ spec.authors = ["Ash Wilson"]
7
+ spec.email = ["smashwilson@gmail.com"]
8
+
9
+ spec.summary = %q{CLI to manage chains of dependent git branches}
10
+ spec.description = <<~EOS
11
+ Interactively manage chains or trees of dependent git branches. Merge or rebase to iteratively incorporate new
12
+ changes from upstream or intermediate modifications. View your current place within the tree.
13
+ EOS
14
+ spec.homepage = "https://github.com/smashwilson/branchtree"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/smashwilson/branchtree"
20
+ spec.metadata["changelog_uri"] = "https://github.com/smashwilson/branchtree/blob/main/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_runtime_dependency "tty-command", "~> 0.10.0"
32
+ spec.add_runtime_dependency "tty-logger", "~> 0.6.0"
33
+ spec.add_runtime_dependency "tty-prompt", "~> 0.23.0"
34
+ spec.add_runtime_dependency "tty-option", "~> 0.1.0"
35
+ end
data/exe/branchtree ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "branchtree"
4
+
5
+ Branchtree.execute(ARGV.dup)
data/lib/branchtree.rb ADDED
@@ -0,0 +1,69 @@
1
+ require "tty-command"
2
+ require "tty-prompt"
3
+ require "tty-logger"
4
+
5
+ module Branchtree
6
+ def self.execute(argv)
7
+ command_classes = {
8
+ "show" => Branchtree::Commands::Show,
9
+ "checkout" => Branchtree::Commands::Checkout,
10
+ "update" => Branchtree::Commands::Update,
11
+ "parent" => Branchtree::Commands::Parent,
12
+ "edit" => Branchtree::Commands::Edit,
13
+ "help" => Branchtree::Commands::Help,
14
+ "-h" => Branchtree::Commands::Help,
15
+ "--help" => Branchtree::Commands::Help,
16
+ }
17
+
18
+ command_name = argv.shift || "show"
19
+ command_class = command_classes[command_name]
20
+ unless command_class
21
+ $stderr.puts "Unrecognized command: #{command_name}"
22
+ $stderr.puts "Available commands: #{command_classes.keys.join(", ")}"
23
+ exit 1
24
+ end
25
+ command = command_class.new
26
+ command.parse(argv)
27
+ command.execute
28
+ end
29
+
30
+ module Context
31
+ class << self
32
+ attr_writer :cmd, :qcmd, :prompt
33
+
34
+ def cmd
35
+ @cmd ||= TTY::Command.new(printer: :null)
36
+ end
37
+
38
+ def qcmd
39
+ @qcmd ||= TTY::Command.new(printer: :quiet)
40
+ end
41
+
42
+ def prompt
43
+ @prompt ||= TTY::Prompt.new
44
+ end
45
+
46
+ def logger
47
+ @logger ||= TTY::Logger.new
48
+ end
49
+ end
50
+
51
+ %i[cmd qcmd prompt logger].each do |methodname|
52
+ define_method(methodname) do
53
+ Branchtree::Context.public_send(methodname)
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ require "branchtree/version"
60
+ require "branchtree/branch"
61
+ require "branchtree/tree"
62
+ require "branchtree/situation"
63
+ require "branchtree/commands/common"
64
+ require "branchtree/commands/show"
65
+ require "branchtree/commands/checkout"
66
+ require "branchtree/commands/update"
67
+ require "branchtree/commands/parent"
68
+ require "branchtree/commands/edit"
69
+ require "branchtree/commands/help"
@@ -0,0 +1,197 @@
1
+
2
+ module Branchtree
3
+
4
+ # Represents a git branch in the current repository.
5
+ class Branch
6
+ include Branchtree::Context
7
+
8
+ # Recursively load a Branch instance and its children, if any, from deserialized YAML.
9
+ def self.load(node, parent)
10
+ new(node.fetch("branch"), parent, node.fetch("rebase", false)).tap do |branch|
11
+ node.fetch("children", []).each do |child_node|
12
+ branch.children << load(child_node, branch)
13
+ end
14
+ branch.children.freeze
15
+ end
16
+ end
17
+
18
+ attr_reader :name, :parent, :children
19
+ attr_accessor :info
20
+
21
+ def initialize(name, parent, rebase)
22
+ @name = name
23
+ @parent = parent
24
+ @rebase = rebase
25
+ @children = []
26
+ @info = NullInfo.new(self)
27
+ end
28
+
29
+ def root?
30
+ @parent.nil?
31
+ end
32
+
33
+ def rebase?
34
+ @rebase
35
+ end
36
+
37
+ # Return the String name of the ref that this branch is based on. New changes to this parent ref will
38
+ # be merged in on "apply".
39
+ def parent_branch_name
40
+ return @parent.name if @parent
41
+
42
+ if cmd.run!("git", "rev-parse", "--verify", "--quiet", "refs/heads/main").success?
43
+ "main"
44
+ else
45
+ "master"
46
+ end
47
+ end
48
+
49
+ # Return the full git ref name of this branch.
50
+ def full_ref
51
+ "refs/heads/#{name}"
52
+ end
53
+
54
+ # Checkout this branch with git
55
+ def checkout
56
+ qcmd.run("git", "checkout", name)
57
+ end
58
+
59
+ def merge_parent
60
+ qcmd.run!("git", "merge", parent_branch_name)
61
+ end
62
+
63
+ def rebase_parent
64
+ qcmd.run!("git", "rebase", parent_branch_name)
65
+ end
66
+
67
+ class NullInfo
68
+ def initialize(branch)
69
+ @branch = branch
70
+ end
71
+
72
+ def empty?
73
+ true
74
+ end
75
+
76
+ def valid?
77
+ true
78
+ end
79
+
80
+ def ahead_of_parent
81
+ 0
82
+ end
83
+
84
+ def behind_parent
85
+ 0
86
+ end
87
+
88
+ def has_upstream?
89
+ false
90
+ end
91
+
92
+ def ahead_of_upstream
93
+ 0
94
+ end
95
+
96
+ def behind_upstream
97
+ 0
98
+ end
99
+
100
+ def populate
101
+ # Are we valid?
102
+ valid_result = @branch.cmd.run!("git", "rev-parse", "--verify", "--quiet", @branch.full_ref)
103
+ unless valid_result.success?
104
+ return @branch.info = InvalidInfo.new(@branch)
105
+ end
106
+
107
+ parent_behind, parent_ahead = 0, 0
108
+ if @branch.parent.info.valid?
109
+ # Count ahead-behind from parent
110
+ ahead_behind_parent = @branch.cmd.run(
111
+ "git", "rev-list", "--left-right", "--count", "refs/heads/#{@branch.parent_branch_name}...#{@branch.full_ref}",
112
+ ).out.chomp
113
+ parent_behind, parent_ahead = ahead_behind_parent.split(/\t/, 2).map(&:to_i)
114
+ end
115
+
116
+ # Idenfity if we have an upstream
117
+ upstream_ref, upstream_behind, upstream_ahead = "", 0, 0
118
+ upstream_result = @branch.cmd.run!(
119
+ "git", "rev-parse", "--symbolic-full-name", "#{@branch.name}@{u}",
120
+ )
121
+ if upstream_result.success?
122
+ upstream_ref = upstream_result.out.chomp
123
+
124
+ ahead_behind_upstream = @branch.cmd.run(
125
+ "git", "rev-list", "--left-right", "--count", "#{upstream_ref}...#{@branch.full_ref}",
126
+ ).out.chomp
127
+ upstream_behind, upstream_ahead = ahead_behind_upstream.split(/\t/, 2).map(&:to_i)
128
+ end
129
+
130
+ @branch.info = Info.new(
131
+ branch: @branch,
132
+ ahead_of_parent: parent_ahead,
133
+ behind_parent: parent_behind,
134
+ upstream: upstream_ref,
135
+ ahead_of_upstream: upstream_ahead,
136
+ behind_upstream: upstream_behind,
137
+ )
138
+ end
139
+
140
+ def repopulate
141
+ populate
142
+ end
143
+ end
144
+
145
+ class InvalidInfo < NullInfo
146
+ def empty?
147
+ false
148
+ end
149
+
150
+ def valid?
151
+ false
152
+ end
153
+
154
+ def populate
155
+ self
156
+ end
157
+
158
+ def repopulate
159
+ self
160
+ end
161
+ end
162
+
163
+ class Info
164
+ def initialize(branch:, ahead_of_parent:, behind_parent:, upstream:, ahead_of_upstream:, behind_upstream:)
165
+ @branch = branch
166
+ @ahead_of_parent = ahead_of_parent
167
+ @behind_parent = behind_parent
168
+ @upstream = upstream
169
+ @ahead_of_upstream = ahead_of_upstream
170
+ @behind_upstream = behind_upstream
171
+ end
172
+
173
+ attr_reader :ahead_of_parent, :behind_parent, :upstream, :ahead_of_upstream, :behind_upstream
174
+
175
+ def empty?
176
+ false
177
+ end
178
+
179
+ def valid?
180
+ true
181
+ end
182
+
183
+ def has_upstream?
184
+ @upstream != ""
185
+ end
186
+
187
+ def populate
188
+ self
189
+ end
190
+
191
+ def repopulate
192
+ NullInfo.new(@branch).populate
193
+ end
194
+ end
195
+ end
196
+
197
+ end
@@ -0,0 +1,42 @@
1
+ require "branchtree/commands/common"
2
+
3
+ module Branchtree
4
+ module Commands
5
+ class Checkout < Common
6
+ usage do
7
+ program "branchtree"
8
+ desc "Navigate the branch structure"
9
+ end
10
+
11
+ def execute
12
+ super
13
+
14
+ situation = load_situation
15
+ tree = load_tree
16
+ current_branch = tree.find_branch(situation.current_branch_name)
17
+
18
+ choice = prompt.select("Choose a branch to check out:") do |menu|
19
+ current_index = nil
20
+ index = 1
21
+ tree.depth_first do |level, branch|
22
+ menu.choice "#{' ' * level}#{branch.name}", branch
23
+
24
+ current_index = index if branch == current_branch
25
+ index += 1
26
+ end
27
+ menu.choice "Cancel", :cancel
28
+ menu.default(current_index) unless current_index.nil?
29
+ end
30
+
31
+ if choice == :cancel
32
+ logger.info "Goodbye!"
33
+ exit 0
34
+ end
35
+
36
+ logger.debug "Checking out branch #{choice.name}."
37
+ choice.checkout
38
+ logger.success "Checkout successful."
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,64 @@
1
+ require "tty-option"
2
+
3
+ module Branchtree
4
+ module Commands
5
+ class Common
6
+ include TTY::Option
7
+ include Branchtree::Context
8
+
9
+ usage do
10
+ program "branchtree"
11
+ end
12
+
13
+ option :mapfile do
14
+ short "-m"
15
+ long "--mapfile PATH"
16
+ desc "Path to the YAML file describing desired branch topography"
17
+ default ENV.fetch("BRANCHTREE_MAPFILE", File.join(ENV["HOME"], "branchtree-map.yml"))
18
+ end
19
+
20
+ option :loglevel do
21
+ short "-l"
22
+ long "--log-level LEVEL"
23
+ desc "Choose the logging level for command output"
24
+ default "info"
25
+ permit TTY::Logger::LEVEL_NAMES.keys
26
+ end
27
+
28
+ option :help do
29
+ short "-h"
30
+ long "--help"
31
+ desc "Display this message"
32
+ end
33
+
34
+ def execute
35
+ if params[:help]
36
+ puts help
37
+ exit 0
38
+ end
39
+
40
+ if params[:loglevel]
41
+ logger.log_at(params[:loglevel].to_sym)
42
+ logger.debug "Logging at level #{params[:loglevel]}."
43
+ end
44
+ end
45
+
46
+ def load_situation
47
+ Situation.new.tap(&:read)
48
+ end
49
+
50
+ def load_tree
51
+ logger.debug "Loading mapfile from #{params[:mapfile]}."
52
+ Tree.load(params[:mapfile])
53
+ end
54
+
55
+ def pluralize(quantity, word, plural: "#{word}s")
56
+ if quantity == 1
57
+ "1 #{word}"
58
+ else
59
+ "#{quantity} #{plural}"
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,19 @@
1
+ require "branchtree/commands/common"
2
+
3
+ module Branchtree
4
+ module Commands
5
+ class Edit < Common
6
+ usage do
7
+ program "branchtree"
8
+ desc "Open your branchtree configuration in your default ${EDITOR}."
9
+ end
10
+
11
+ def execute
12
+ super
13
+
14
+ editor = ENV["BRANCHTREE_EDITOR"] || ENV["EDITOR"]
15
+ system "#{editor} #{params[:mapfile]}"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,46 @@
1
+ require "branchtree/commands/common"
2
+ require "branchtree/version"
3
+
4
+ module Branchtree
5
+ module Commands
6
+ class Help < Common
7
+ usage do
8
+ program "branchtree"
9
+ desc "Show a help message."
10
+ end
11
+
12
+ def execute
13
+ super
14
+
15
+ puts <<~HELP
16
+ Branchtree version #{Branchtree::VERSION}.
17
+
18
+ Command-line tool to interactively manage chains or trees of dependent branches in a git repository.
19
+
20
+ Specify your desired branch topography in a YAML file at the path:
21
+ #{params[:mapfile]}
22
+ Override this by specifying the BRANCHTREE_MAPFILE environment variables or specifying the -m/--mapfile
23
+ argument. Format:
24
+
25
+ ```
26
+ ---
27
+ - branch: branch-name
28
+ rebase: true # Update this branch by rebasing onto its parent, or merging? Default: false (merging).
29
+ children:
30
+ - branch: child-branch-0
31
+ - branch: child-branch-1
32
+ ```
33
+
34
+ Run branchtree commands within a git repository containing these branches. Available commands include:
35
+
36
+ branchtree [show] - Display the current tree, including status of each branch.
37
+ branchtree checkout - Interactively navigate to a branch within the tree.
38
+ branchtree update - Propagate new commits from parent branches recursively through their children.
39
+ branchtree parent - Display the parent branch of the current HEAD.
40
+
41
+ Use -h/--help flags to see options for each specific subcommand.
42
+ HELP
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,26 @@
1
+ require "branchtree/commands/common"
2
+
3
+ module Branchtree
4
+ module Commands
5
+ class Parent < Common
6
+ usage do
7
+ program "branchtree"
8
+ desc "Display the parent branch of the current branch."
9
+ end
10
+
11
+ def execute
12
+ super
13
+
14
+ situation = load_situation
15
+ tree = load_tree
16
+ current_branch = tree.find_branch(situation.current_branch_name)
17
+ unless current_branch
18
+ $stderr.puts "The current branch #{situation.current_branch_name} is not within the tree."
19
+ exit 1
20
+ end
21
+
22
+ puts current_branch.parent_branch_name
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ require "branchtree/commands/common"
2
+
3
+ module Branchtree
4
+ module Commands
5
+ class Show < Common
6
+ usage do
7
+ program "branchtree"
8
+ desc "Display the current branch tree and your place within it."
9
+ end
10
+
11
+ def execute
12
+ super
13
+
14
+ situation = load_situation
15
+ tree = load_tree
16
+ current_branch = tree.find_branch(situation.current_branch_name)
17
+
18
+ tree.depth_first do |level, branch|
19
+ logger.debug "Loading branch #{branch.name}."
20
+ branch.info.populate
21
+ end
22
+
23
+ tree.depth_first do |level, branch|
24
+ line = ""
25
+
26
+ if branch == current_branch
27
+ line << "==> "
28
+ else
29
+ line << " "
30
+ end
31
+
32
+ line << " " * level
33
+ line << branch.name
34
+
35
+ if !branch.info.valid?
36
+ line << " (branch missing)"
37
+ else
38
+ if branch.info.behind_parent > 0
39
+ line << " - #{pluralize(branch.info.behind_parent, "commit")} behind parent"
40
+ end
41
+ if branch.info.ahead_of_upstream > 0
42
+ if branch.info.behind_upstream > 0
43
+ line << " - diverged from upstream (#{branch.info.ahead_of_upstream}/#{branch.info.behind_upstream})"
44
+ else
45
+ line << " - #{pluralize(branch.info.ahead_of_upstream, "unpushed commit")}"
46
+ end
47
+ elsif branch.info.behind_upstream > 0
48
+ line << " - #{pluralize(branch.info.behind_upstream, "commit")} behind upstream"
49
+ end
50
+ end
51
+
52
+ puts line
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,100 @@
1
+ require "branchtree/commands/common"
2
+
3
+ module Branchtree
4
+ module Commands
5
+ class Update < Common
6
+ usage do
7
+ program "branchtree"
8
+ desc "Propagate unmerged changes forward through the tree"
9
+ end
10
+
11
+ option :root do
12
+ short "-r"
13
+ long "--root"
14
+ desc "Include unmerged commits from the default branch"
15
+ end
16
+
17
+ option :push do
18
+ short "-p"
19
+ long "--push ENABLED"
20
+ desc "Prompt to push or force-push any modified branches"
21
+ convert :bool
22
+ default "true"
23
+ end
24
+
25
+ def execute
26
+ super
27
+
28
+ situation = load_situation
29
+ tree = load_tree
30
+
31
+ to_push = []
32
+
33
+ tree.breadth_first do |level, branch|
34
+ next if branch.root? && !params[:root]
35
+ branch.info.populate
36
+ next if branch.info.behind_parent.zero?
37
+
38
+ if branch.info.behind_upstream > 0 && branch.info.ahead_upstream > 0
39
+ logger.error "#{branch.name} has diverged from its upstream."
40
+ logger.error "Please resolve this with a force push or reset --hard, then run again."
41
+ exit 1
42
+ end
43
+
44
+ before = branch.info.behind_upstream
45
+
46
+ logger.info "#{branch.name} is #{pluralize(branch.info.behind_parent, "commit")} behind its parent branch."
47
+ logger.info "Checking out #{branch.name}."
48
+ branch.checkout
49
+
50
+ success = false
51
+ if branch.rebase?
52
+ logger.info "Rebasing #{branch.name} on #{branch.parent_branch_name}."
53
+ success = branch.rebase_parent.success?
54
+ else
55
+ logger.info "Merging #{branch.parent_branch_name} into #{branch.name}."
56
+ success = branch.merge_parent.success?
57
+ end
58
+
59
+ unless success
60
+ logger.error "Please resolve these problems and run again."
61
+ exit 1
62
+ end
63
+
64
+ logger.success "#{branch.name} is now up to date."
65
+
66
+ branch.info.repopulate
67
+
68
+ after = branch.info.behind_upstream
69
+ to_push << branch if before != after
70
+ end
71
+
72
+ logger.success "All branches are now up to date."
73
+
74
+ if params[:push] && to_push.size > 0
75
+ chosen = prompt.multi_select("Push changed branches?") do |menu|
76
+ to_push.each do |branch|
77
+ option_name = branch.name.dup
78
+ option_name << " (force)" if branch.rebase?
79
+
80
+ menu.choice option_name, branch
81
+ end
82
+
83
+ menu.default *(1..to_push.size)
84
+ end
85
+
86
+ forced, unforced = chosen.partition(&:rebase?)
87
+ unless forced.empty?
88
+ logger.info "Force pushing #{pluralize(forced.size, "branch", plural: "branches")}."
89
+ qcmd.run("git", "push", "--force-with-lease", "origin", *forced.map(&:name))
90
+ end
91
+ unless unforced.empty?
92
+ logger.info "Pushing #{pluralize(unforced.size, "branch", plural: "branches")}."
93
+ qcmd.run("git", "push", "origin", *unforced.map(&:name))
94
+ end
95
+ logger.success "Goodbye."
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,19 @@
1
+ module Branchtree
2
+
3
+ class Situation
4
+ include Branchtree::Context
5
+
6
+ attr_reader :current_branch_name
7
+
8
+ def initialize
9
+ @current_branch_name = nil
10
+ end
11
+
12
+ def read
13
+ @current_branch_name = cmd.run(
14
+ "git", "rev-parse", "--abbrev-ref", "HEAD"
15
+ ).out.chomp
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,56 @@
1
+ require "yaml"
2
+ require "branchtree/branch"
3
+
4
+ module Branchtree
5
+
6
+ # Represent the branch topology described by a user's YAML file.
7
+ class Branchtree::Tree
8
+ # Load a tree from the topology described in a YAML file.
9
+ def self.load(source)
10
+ doc = YAML.safe_load(File.read(source))
11
+ new(doc.map { |node| Branchtree::Branch.load(node, nil) })
12
+ end
13
+
14
+ attr_reader :roots
15
+
16
+ def initialize(roots)
17
+ @roots = roots
18
+ end
19
+
20
+ # Locate a known branch in the tree by abbreviated ref name, or return nil if none are found.
21
+ def find_branch(name)
22
+ breadth_first do |level, branch|
23
+ return branch if branch.name == name
24
+ end
25
+ nil
26
+ end
27
+
28
+ def depth_first(&block)
29
+ depth_first_from(level: 0, branches: roots, &block)
30
+ end
31
+
32
+ def breadth_first(&block)
33
+ level = 0
34
+ frontier = roots.dup
35
+
36
+ until frontier.empty?
37
+ frontier.each do |branch|
38
+ block.call(level, branch)
39
+ end
40
+
41
+ level += 1
42
+ frontier = frontier.flat_map(&:children)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def depth_first_from(level:, branches:, &block)
49
+ branches.each do |branch|
50
+ block.call(level, branch)
51
+ depth_first_from(level: level + 1, branches: branch.children, &block)
52
+ end
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,3 @@
1
+ module Branchtree
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: branchtree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ash Wilson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tty-command
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-prompt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.23.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.23.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-option
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
69
+ description: |
70
+ Interactively manage chains or trees of dependent git branches. Merge or rebase to iteratively incorporate new
71
+ changes from upstream or intermediate modifications. View your current place within the tree.
72
+ email:
73
+ - smashwilson@gmail.com
74
+ executables:
75
+ - branchtree
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".github/workflows/ci.yml"
80
+ - ".gitignore"
81
+ - ".rspec"
82
+ - CHANGELOG.md
83
+ - CODE_OF_CONDUCT.md
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - branchtree.gemspec
92
+ - exe/branchtree
93
+ - lib/branchtree.rb
94
+ - lib/branchtree/branch.rb
95
+ - lib/branchtree/commands/checkout.rb
96
+ - lib/branchtree/commands/common.rb
97
+ - lib/branchtree/commands/edit.rb
98
+ - lib/branchtree/commands/help.rb
99
+ - lib/branchtree/commands/parent.rb
100
+ - lib/branchtree/commands/show.rb
101
+ - lib/branchtree/commands/update.rb
102
+ - lib/branchtree/situation.rb
103
+ - lib/branchtree/tree.rb
104
+ - lib/branchtree/version.rb
105
+ homepage: https://github.com/smashwilson/branchtree
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ homepage_uri: https://github.com/smashwilson/branchtree
110
+ source_code_uri: https://github.com/smashwilson/branchtree
111
+ changelog_uri: https://github.com/smashwilson/branchtree/blob/main/CHANGELOG.md
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.3.0
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.1.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: CLI to manage chains of dependent git branches
131
+ test_files: []