Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kintsugi: Support installing driver globally. #29

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ And see the magic happen! :sparkles:

### Git merge driver

To use Kintsugi as a Git merge driver, follow these steps:
You can setup Kintsugi to automatically resolve conflicts that occur in `pbxproj` files when such conflicts occur.

- Add it as driver to Git config file by running the following:
#### Automatic install

Run `kintsugi install-driver`. This will install Kintsugi as a merge driver globally. Note that Kintsugi needs to be in your `PATH`.

❗ Do not install with bundler because the installation might succeed even if Kintsugi is not in `PATH`.

#### Manual install

- Add Kintsugi as driver to Git config file by running the following:
```sh
git config merge.kintsugi.name "Kintsugi driver" # Or any other name you prefer
git config merge.kintsugi.driver "kintsugi driver %O %A %B %P"
git config merge.kintsugi.driver "<path_to_kintsugi> driver %O %A %B %P"
```

`kintsugi` should be in your `PATH`.
Run `git config` with `--global` to add this to the global config file.

- Add the following line to the `.gitattributes` file at the root of the repository:
Expand Down
102 changes: 100 additions & 2 deletions lib/kintsugi/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class CLI
# Subcommands of Kintsugi CLI.
attr_reader :subcommands

# Root command Kintsugi CLI.
# Root command of Kintsugi CLI.
attr_reader :root_command

def initialize
@subcommands = {
"driver" => create_driver_subcommand
"driver" => create_driver_subcommand,
"install-driver" => create_install_driver_subcommand,
"uninstall-driver" => create_uninstall_driver_subcommand
}.freeze
@root_command = create_root_command
end
Expand Down Expand Up @@ -56,6 +58,102 @@ def create_driver_subcommand
)
end

def create_install_driver_subcommand
option_parser =
OptionParser.new do |opts|
opts.banner = "Usage: kintsugi install-driver\n" \
"Installs Kintsugi as a Git merge driver globally. "

opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end

action = lambda { |_, arguments, _|
if arguments.count != 0
puts "Incorrect number of arguments to 'install-driver' subcommand\n\n"
puts option_parser
exit(1)
end

if `which kintsugi`.chomp.empty?
puts "Can only install Kintsugi globally if Kintsugi is in your PATH"
exit(1)
end

install_kintsugi_driver_globally
puts "Done! 🪄"
}

Command.new(
option_parser: option_parser,
action: action,
description: "Installs Kintsugi as a Git merge driver globally"
)
end

def install_kintsugi_driver_globally
`git config --global merge.kintsugi.name "Kintsugi driver"`
`git config --global merge.kintsugi.driver "kintsugi driver %O %A %B %P"`

attributes_file_path = global_attributes_file_path
merge_using_kintsugi_line = "'*.pbxproj merge=kintsugi'"
`grep -sqxF #{merge_using_kintsugi_line} "#{attributes_file_path}" \
|| echo #{merge_using_kintsugi_line} >> "#{attributes_file_path}"`
end

def global_attributes_file_path
# The logic to decide the path to the global attributes file is described at:
# https://git-scm.com/docs/gitattributes.
config_attributes_file_path = `git config --global core.attributesfile`
return config_attributes_file_path unless config_attributes_file_path.empty?

if ENV["XDG_CONFIG_HOME"].nil? || ENV["XDG_CONFIG_HOME"].empty?
File.join(ENV["HOME"], ".config/git/attributes")
else
File.join(ENV["XDG_CONFIG_HOME"], "git/attributes")
end
end

def create_uninstall_driver_subcommand
option_parser =
OptionParser.new do |opts|
opts.banner = "Usage: kintsugi uninstall-driver\n" \
"Uninstalls Kintsugi as a Git merge driver that was previously installed globally."

opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end

action = lambda { |_, arguments, _|
if arguments.count != 0
puts "Incorrect number of arguments to 'uninstall-driver' subcommand\n\n"
puts option_parser
exit(1)
end

uninstall_kintsugi_driver_globally
puts "Done!"
}

Command.new(
option_parser: option_parser,
action: action,
description: "Uninstalls Kintsugi as a Git merge driver that was previously installed " \
"globally."
)
end

def uninstall_kintsugi_driver_globally
`git config --global --unset merge.kintsugi.name`
`git config --global --unset merge.kintsugi.driver`

`sed -i '' '/\*.pbxproj\ merge=kintsugi/d' "#{global_attributes_file_path}"`
end

def create_root_command
root_option_parser = OptionParser.new do |opts|
opts.banner = "Kintsugi, version #{Version::STRING}\n" \
Expand Down