rails-worktree 0.1.0 → 0.1.3

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: e8888a53c51f48da7d20802a75aa290fedafb9a4facb449276ac98f03a35a2ca
4
- data.tar.gz: a6609cd5f4d380f0c62acb8a7bae58079e9c995c032ef04efb2b815386edc612
3
+ metadata.gz: 456c428610bf82c3e0e1ce8f720d22a70659384b87980c48ae7afa8484c1e5d3
4
+ data.tar.gz: d723f2b9631b97304507b63155c9a9b1a7c5b729c8d97e471d55d40032ca2a19
5
5
  SHA512:
6
- metadata.gz: 28b6e35bbe05918a9ae07a794ce883e4c791e20f71d3fb1956b46a1b4c410ed7ee3f5c5382e914fcc2c90a5f78b25428b8a2a03a00a29c4b1191dce5df8d2706
7
- data.tar.gz: d64ef8946b28c67a3f34e6602eed6903a8a2bb27492f576167453e2269ede2235e351ce985153495cb780e4469206da6a12d4274acf74a39b66fd81296e2d2e5
6
+ metadata.gz: 1704fd1870220d15467ad2d3f7de1791eca318265982344cd61519d2202e3198cfc1346305d75e44b6d32e82e5b8b17f188e09ef4a198734f577c00325b356ff
7
+ data.tar.gz: 603b6dc0fed0d99407bed88ef1c3b266fdd8cd9b8fa77b90e774506e41ad8de4f14fe28ebc102007a8c9d4c133f874327da637ffb2a635e7d3b62a6d453794ed
data/README.md CHANGED
@@ -1,174 +1,85 @@
1
1
  # Rails Worktree
2
2
 
3
- A Ruby gem for managing git worktrees in Rails projects with isolated databases and configurations.
3
+ Git worktree management for Rails projects with isolated databases and configurations.
4
4
 
5
5
  ## Features
6
6
 
7
7
  - Create git worktrees with automatic branch creation
8
- - Isolated database per worktree (separate database name)
9
- - Automatic configuration file copying (.env, database.yml, etc.)
8
+ - Isolated databases per worktree (separate development and test databases)
9
+ - Automatic configuration file copying
10
10
  - Copy node_modules from main worktree
11
11
  - Easy cleanup with database dropping and directory removal
12
- - Works across any Rails project (no hardcoded paths)
13
12
 
14
13
  ## Installation
15
14
 
16
- ### In your Rails project Gemfile
15
+ Add to your `Gemfile`:
17
16
 
18
17
  ```ruby
19
- # Add to Gemfile (development group)
20
18
  group :development do
21
19
  gem "rails-worktree"
22
20
  end
23
21
  ```
24
22
 
25
- Then run:
26
-
27
- ```bash
28
- bundle install
29
- ```
30
-
31
- The gem will automatically install a binstub to `bin/worktree` when Rails loads in development mode.
32
-
33
- ### Manual binstub installation
34
-
35
- If needed, you can manually install or reinstall the binstub:
23
+ Run `bundle install`. A binstub will be automatically created at `bin/worktree`.
36
24
 
25
+ Manual binstub installation (if needed):
37
26
  ```bash
38
27
  bundle exec rake worktree:install
39
28
  ```
40
29
 
41
- To uninstall:
42
-
43
- ```bash
44
- bundle exec rake worktree:uninstall
45
- ```
46
-
47
- ### Global installation (optional)
48
-
49
- If you want to use `worktree` command globally:
50
-
51
- ```bash
52
- gem install rails-worktree
53
- ```
54
-
55
30
  ## Usage
56
31
 
57
- ### Create a new worktree
32
+ ### Create a worktree
58
33
 
59
34
  ```bash
60
- bin/worktree feature-branch
61
- # Creates worktree from current branch
62
-
63
- bin/worktree feature-branch main
64
- # Creates worktree from main branch
35
+ bin/worktree feature-branch # From current branch
36
+ bin/worktree feature-branch main # From specific branch
65
37
  ```
66
38
 
67
- This will:
68
- 1. Create a new git worktree in `../feature-branch`
69
- 2. Create a new branch called `feature-branch`
70
- 3. Copy configuration files from main worktree
71
- 4. Set up a separate database (e.g., `myapp_feature-branch`)
72
- 5. Copy node_modules
73
- 6. Run migrations and seed the database
39
+ This creates a new worktree with:
40
+ - Separate databases: `myapp_feature-branch_development` and `myapp_feature-branch_test`
41
+ - Copied configuration files (`.env`, `database.yml`, `Procfile.dev`, credentials)
42
+ - Copied `node_modules`
43
+ - Migrated and seeded databases
74
44
 
75
45
  ### Close a worktree
76
46
 
77
47
  ```bash
78
- # From main repository:
79
- bin/worktree --close feature-branch
80
-
81
- # From within the worktree:
82
- bin/worktree --close
48
+ bin/worktree --close feature-branch # From main repo
49
+ bin/worktree --close # From within worktree
83
50
  ```
84
51
 
85
- This will:
86
- 1. Drop the worktree's database
87
- 2. Remove the worktree directory
88
- 3. Delete the branch
89
- 4. Clean up git worktree references
52
+ Drops both databases, removes the directory, deletes the branch, and cleans up git references.
90
53
 
91
- ### Manual initialization (advanced)
54
+ ## Database Configuration
92
55
 
93
- If you need to manually initialize a worktree:
56
+ The gem uses environment variables for database names:
94
57
 
95
- ```bash
96
- cd ../feature-branch
97
- worktree --init feature-branch
98
- ```
58
+ - `DATABASE_NAME_DEVELOPMENT` - Development database name
59
+ - `DATABASE_NAME_TEST` - Test database name
99
60
 
100
- ## How it works
61
+ Your `config/database.yml` should look like:
101
62
 
102
- ### Database isolation
63
+ ```yaml
64
+ development:
65
+ <<: *default
66
+ database: <%= ENV.fetch("DATABASE_NAME_DEVELOPMENT", "myapp_development") %>
67
+ test:
68
+ <<: *default
69
+ database: <%= ENV.fetch("DATABASE_NAME_TEST", "myapp_test") %>
70
+ ```
103
71
 
104
72
  The gem automatically:
105
- - Detects your main database name from `config/database.yml`
106
- - Creates a new database with pattern: `{app_name}_{worktree_name}`
107
- - Updates the worktree's `.env` to set `DATABASE_NAME`
108
- - Modifies `database.yml` to use the `DATABASE_NAME` environment variable
109
-
110
- ### Configuration files
111
-
112
- The following files are copied from the main worktree:
113
- - `.env`
114
- - `config/database.yml`
115
- - `Procfile.dev`
116
- - `config/credentials/development.key`
117
-
118
- ### Node modules
119
-
120
- If `node_modules` exists in the main worktree, it will be copied to the new worktree.
73
+ 1. Sets these variables in the worktree's `.env` file
74
+ 2. Updates the worktree's `database.yml` to use them
75
+ 3. Creates both development and test databases with unique names
121
76
 
122
77
  ## Requirements
123
78
 
124
79
  - Ruby >= 2.6.0
125
- - Git with worktree support
126
80
  - Rails project with standard structure
127
-
128
- ## Project structure
129
-
130
- This gem works with Rails projects that follow the standard structure:
131
-
132
- ```
133
- your-project/
134
- ├── config/
135
- │ ├── database.yml
136
- │ └── credentials/
137
- │ └── development.key
138
- ├── .env
139
- ├── Procfile.dev
140
- └── bin/
141
- ├── rails
142
- └── dev
143
- ```
144
-
145
- ## Development
146
-
147
- To work on the gem:
148
-
149
- ```bash
150
- # Clone or create the gem
151
- cd worktree
152
-
153
- # Make changes to lib/worktree/**
154
-
155
- # Test locally
156
- gem build worktree.gemspec
157
- gem install ./worktree-0.1.0.gem
158
-
159
- # Test in a Rails project
160
- cd /path/to/rails/project
161
- worktree test-branch
162
- ```
81
+ - PostgreSQL (or modify for your database)
163
82
 
164
83
  ## License
165
84
 
166
85
  MIT
167
-
168
- ## Contributing
169
-
170
- 1. Fork it
171
- 2. Create your feature branch (`git checkout -b my-new-feature`)
172
- 3. Commit your changes (`git commit -am 'Add some feature'`)
173
- 4. Push to the branch (`git push origin my-new-feature`)
174
- 5. Create new Pull Request
data/exe/worktree CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "worktree"
3
+ require "rails-worktree"
4
4
 
5
- Worktree::CLI.run(ARGV)
5
+ RailsWorktree::CLI.run(ARGV)
@@ -7,6 +7,6 @@ require_relative "worktree/commands/close"
7
7
  # Load Railtie only if Rails is available
8
8
  require_relative "worktree/railtie" if defined?(Rails::Railtie)
9
9
 
10
- module Worktree
10
+ module RailsWorktree
11
11
  class Error < StandardError; end
12
12
  end
data/lib/worktree/cli.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Worktree
1
+ module RailsWorktree
2
2
  class CLI
3
3
  def self.run(args)
4
4
  new(args).run
@@ -1,6 +1,6 @@
1
1
  require "fileutils"
2
2
 
3
- module Worktree
3
+ module RailsWorktree
4
4
  module Commands
5
5
  class Close
6
6
  def initialize(args)
@@ -13,7 +13,8 @@ module Worktree
13
13
  detect_worktree_name unless @worktree_name
14
14
 
15
15
  @db_prefix = get_db_prefix
16
- @database_name = "#{@db_prefix}_#{@worktree_name}"
16
+ @dev_database_name = "#{@db_prefix}_#{@worktree_name}_development"
17
+ @test_database_name = "#{@db_prefix}_#{@worktree_name}_test"
17
18
 
18
19
  detect_paths
19
20
 
@@ -21,14 +22,14 @@ module Worktree
21
22
  puts "Main worktree: #{@main_worktree}"
22
23
  puts ""
23
24
 
24
- drop_database
25
+ drop_databases
25
26
  remove_worktree
26
27
  prune_worktrees
27
28
  delete_branch
28
29
 
29
30
  puts ""
30
31
  puts "✓ Worktree '#{@worktree_name}' closed successfully!"
31
- puts " Database #{@database_name} dropped"
32
+ puts " Databases dropped: #{@dev_database_name}, #{@test_database_name}"
32
33
  puts " Worktree removed from #{@worktree_path}"
33
34
  puts " Branch #{@worktree_name} deleted"
34
35
  end
@@ -73,16 +74,27 @@ module Worktree
73
74
  end
74
75
  end
75
76
 
76
- def drop_database
77
- puts "Dropping database #{@database_name}..."
77
+ def drop_databases
78
+ puts "Dropping databases..."
78
79
 
79
80
  Dir.chdir(@worktree_dir) do
80
81
  env_file = ".env"
81
- if File.exist?(env_file) && File.read(env_file).match?(/^DATABASE_NAME=#{@database_name}/)
82
+ env_content = File.exist?(env_file) ? File.read(env_file) : ""
83
+
84
+ # Drop development database
85
+ if env_content.match?(/^DATABASE_NAME_DEVELOPMENT=#{@dev_database_name}/)
82
86
  system("RAILS_ENV=development bin/rails db:drop 2>/dev/null") ||
83
- puts("Warning: Could not drop database #{@database_name}")
87
+ puts("Warning: Could not drop development database #{@dev_database_name}")
88
+ else
89
+ puts "Warning: DATABASE_NAME_DEVELOPMENT not set in .env, skipping development database drop"
90
+ end
91
+
92
+ # Drop test database
93
+ if env_content.match?(/^DATABASE_NAME_TEST=#{@test_database_name}/)
94
+ system("RAILS_ENV=test bin/rails db:drop 2>/dev/null") ||
95
+ puts("Warning: Could not drop test database #{@test_database_name}")
84
96
  else
85
- puts "Warning: DATABASE_NAME not set to #{@database_name} in .env, skipping database drop"
97
+ puts "Warning: DATABASE_NAME_TEST not set in .env, skipping test database drop"
86
98
  end
87
99
  end
88
100
  end
@@ -1,4 +1,4 @@
1
- module Worktree
1
+ module RailsWorktree
2
2
  module Commands
3
3
  class Create
4
4
  def initialize(args)
@@ -14,6 +14,7 @@ module Worktree
14
14
 
15
15
  @base_branch ||= current_branch
16
16
  worktree_path = "../#{@worktree_name}"
17
+ absolute_path = File.expand_path(worktree_path)
17
18
 
18
19
  puts "Creating worktree '#{@worktree_name}' from branch '#{@base_branch}' at #{worktree_path}..."
19
20
 
@@ -30,6 +31,10 @@ module Worktree
30
31
  Dir.chdir(worktree_path) do
31
32
  Init.new([@worktree_name]).run
32
33
  end
34
+
35
+ puts ""
36
+ puts "To switch to the new worktree:"
37
+ puts " cd #{absolute_path}"
33
38
  end
34
39
 
35
40
  private
@@ -1,6 +1,6 @@
1
1
  require "fileutils"
2
2
 
3
- module Worktree
3
+ module RailsWorktree
4
4
  module Commands
5
5
  class Init
6
6
  def initialize(args)
@@ -16,21 +16,24 @@ module Worktree
16
16
 
17
17
  @main_worktree = get_main_worktree
18
18
  @db_prefix = get_db_prefix
19
- @database_name = "#{@db_prefix}_#{@worktree_name}"
19
+ @dev_database_name = "#{@db_prefix}_#{@worktree_name}_development"
20
+ @test_database_name = "#{@db_prefix}_#{@worktree_name}_test"
20
21
 
21
22
  puts "Initializing worktree '#{@worktree_name}'..."
22
23
  puts "Main worktree: #{@main_worktree}"
23
- puts "Database: #{@database_name}"
24
+ puts "Development database: #{@dev_database_name}"
25
+ puts "Test database: #{@test_database_name}"
24
26
 
25
27
  copy_config_files
26
- set_database_name
28
+ set_database_names
27
29
  update_database_yml
28
30
  copy_node_modules
29
31
  setup_database
30
32
 
31
33
  puts ""
32
34
  puts "✓ Worktree initialized successfully!"
33
- puts " Database: #{@database_name}"
35
+ puts " Development database: #{@dev_database_name}"
36
+ puts " Test database: #{@test_database_name}"
34
37
  puts " Configuration files copied"
35
38
  puts ""
36
39
  puts "To start the development server: bin/dev"
@@ -44,12 +47,16 @@ module Worktree
44
47
  end
45
48
 
46
49
  def get_db_prefix
50
+ # Try to get prefix from database.yml first
47
51
  database_yml = File.join(@main_worktree, "config/database.yml")
48
- return nil unless File.exist?(database_yml)
52
+ if File.exist?(database_yml)
53
+ content = File.read(database_yml)
54
+ match = content.match(/database:\s*(\w+)_development/)
55
+ return match[1] if match
56
+ end
49
57
 
50
- content = File.read(database_yml)
51
- match = content.match(/database:\s*(\w+)_development/)
52
- match ? match[1] : nil
58
+ # Fall back to using the Rails app name (directory name of main worktree)
59
+ File.basename(@main_worktree)
53
60
  end
54
61
 
55
62
  def copy_config_files
@@ -73,32 +80,57 @@ module Worktree
73
80
  end
74
81
  end
75
82
 
76
- def set_database_name
77
- puts "Setting DATABASE_NAME=#{@database_name} in .env..."
83
+ def set_database_names
84
+ puts "Setting database names in .env..."
78
85
 
79
86
  env_file = ".env"
80
87
  return unless File.exist?(env_file)
81
88
 
82
89
  content = File.read(env_file)
83
- if content.match?(/^DATABASE_NAME=/)
84
- content.gsub!(/^DATABASE_NAME=.*$/, "DATABASE_NAME=#{@database_name}")
90
+
91
+ # Set development database name
92
+ if content.match?(/^DATABASE_NAME_DEVELOPMENT=/)
93
+ content.gsub!(/^DATABASE_NAME_DEVELOPMENT=.*$/, "DATABASE_NAME_DEVELOPMENT=#{@dev_database_name}")
85
94
  else
86
- content += "\nDATABASE_NAME=#{@database_name}\n"
95
+ content += "\nDATABASE_NAME_DEVELOPMENT=#{@dev_database_name}\n"
96
+ end
97
+
98
+ # Set test database name
99
+ if content.match?(/^DATABASE_NAME_TEST=/)
100
+ content.gsub!(/^DATABASE_NAME_TEST=.*$/, "DATABASE_NAME_TEST=#{@test_database_name}")
101
+ else
102
+ content += "DATABASE_NAME_TEST=#{@test_database_name}\n"
87
103
  end
88
104
 
89
105
  File.write(env_file, content)
90
106
  end
91
107
 
92
108
  def update_database_yml
93
- puts "Updating database.yml to use DATABASE_NAME..."
109
+ puts "Updating database.yml to use separate database names..."
94
110
 
95
111
  database_yml = "config/database.yml"
96
112
  return unless File.exist?(database_yml)
97
113
 
98
114
  content = File.read(database_yml)
115
+
116
+ # Update development database
117
+ content.gsub!(
118
+ /database:\s*<%= ENV\.fetch\("DATABASE_NAME",\s*"#{@db_prefix}_development"\s*%>/,
119
+ "database: <%= ENV.fetch(\"DATABASE_NAME_DEVELOPMENT\", \"#{@db_prefix}_development\") %>"
120
+ )
99
121
  content.gsub!(
100
122
  /database:\s*#{@db_prefix}_development/,
101
- "database: <%= ENV.fetch(\"DATABASE_NAME\", \"#{@db_prefix}_development\") %>"
123
+ "database: <%= ENV.fetch(\"DATABASE_NAME_DEVELOPMENT\", \"#{@db_prefix}_development\") %>"
124
+ )
125
+
126
+ # Update test database
127
+ content.gsub!(
128
+ /database:\s*<%= ENV\.fetch\("DATABASE_NAME",\s*"#{@db_prefix}_test"\s*%>/,
129
+ "database: <%= ENV.fetch(\"DATABASE_NAME_TEST\", \"#{@db_prefix}_test\") %>"
130
+ )
131
+ content.gsub!(
132
+ /database:\s*#{@db_prefix}_test/,
133
+ "database: <%= ENV.fetch(\"DATABASE_NAME_TEST\", \"#{@db_prefix}_test\") %>"
102
134
  )
103
135
 
104
136
  File.write(database_yml, content)
@@ -116,13 +148,15 @@ module Worktree
116
148
  end
117
149
 
118
150
  def setup_database
119
- puts "Creating database #{@database_name}..."
120
- system("RAILS_ENV=development bin/rails db:create") || puts("Warning: Could not create database")
151
+ puts "Creating databases..."
152
+ system("RAILS_ENV=development bin/rails db:create") || puts("Warning: Could not create development database")
153
+ system("RAILS_ENV=test bin/rails db:create") || puts("Warning: Could not create test database")
121
154
 
122
155
  puts "Running migrations..."
123
156
  system("RAILS_ENV=development bin/rails db:migrate") || puts("Warning: Could not run migrations")
157
+ system("RAILS_ENV=test bin/rails db:migrate") || puts("Warning: Could not run test migrations")
124
158
 
125
- puts "Seeding database..."
159
+ puts "Seeding development database..."
126
160
  system("RAILS_ENV=development bin/rails db:seed") || puts("Warning: Could not seed database")
127
161
  end
128
162
  end
@@ -1,6 +1,6 @@
1
1
  require "rails/railtie"
2
2
 
3
- module Worktree
3
+ module RailsWorktree
4
4
  class Railtie < Rails::Railtie
5
5
  railtie_name :worktree
6
6
 
@@ -20,9 +20,9 @@ module Worktree
20
20
  #!/usr/bin/env ruby
21
21
 
22
22
  require "bundler/setup"
23
- require "worktree"
23
+ require "rails-worktree"
24
24
 
25
- Worktree::CLI.run(ARGV)
25
+ RailsWorktree::CLI.run(ARGV)
26
26
  RUBY
27
27
 
28
28
  FileUtils.chmod("+x", binstub_path)
@@ -12,9 +12,9 @@ namespace :worktree do
12
12
  #!/usr/bin/env ruby
13
13
 
14
14
  require "bundler/setup"
15
- require "worktree"
15
+ require "rails-worktree"
16
16
 
17
- Worktree::CLI.run(ARGV)
17
+ RailsWorktree::CLI.run(ARGV)
18
18
  RUBY
19
19
 
20
20
  File.chmod(0755, binstub_path)
@@ -1,3 +1,3 @@
1
- module Worktree
1
+ module RailsWorktree
2
2
  VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-worktree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Ulleberg
@@ -21,7 +21,6 @@ files:
21
21
  - README.md
22
22
  - exe/worktree
23
23
  - lib/rails-worktree.rb
24
- - lib/worktree.rb
25
24
  - lib/worktree/cli.rb
26
25
  - lib/worktree/commands/close.rb
27
26
  - lib/worktree/commands/create.rb
data/lib/worktree.rb DELETED
@@ -1,12 +0,0 @@
1
- require_relative "worktree/version"
2
- require_relative "worktree/cli"
3
- require_relative "worktree/commands/create"
4
- require_relative "worktree/commands/init"
5
- require_relative "worktree/commands/close"
6
-
7
- # Load Railtie only if Rails is available
8
- require_relative "worktree/railtie" if defined?(Rails::Railtie)
9
-
10
- module Worktree
11
- class Error < StandardError; end
12
- end