git_context 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d02e280efb82c55f30a2e33d5505c3dd4fdd5480547bf2aa82242f25bb7614d8
4
+ data.tar.gz: 8f826f42e65c9f25cc281ae4a7cd478b87eb4035000fac0b14bc0f6c6ee0447a
5
+ SHA512:
6
+ metadata.gz: aa4a8d99893336ec6930cecb733452e105b9d23a039596cfb79117e76ab9a02ee11d8240e1a4916dd666e57ee360cb4a95baf23a72d07ef036019c7a652c7b4b
7
+ data.tar.gz: 4ff48917170d7cd43ef5129b9f10461c3b425b79b1829dc7886be19e03e87f52facd19bc15bbac885fb1c63929410c429803bb7cd7909a8a8f72feeeafd38833
@@ -0,0 +1,12 @@
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
12
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ ruby 2.6.6
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in git_context.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ git_context (0.1.0)
5
+ tty-prompt (~> 0.22)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.4.4)
11
+ pastel (0.8.0)
12
+ tty-color (~> 0.5)
13
+ rake (12.3.3)
14
+ rspec (3.9.0)
15
+ rspec-core (~> 3.9.0)
16
+ rspec-expectations (~> 3.9.0)
17
+ rspec-mocks (~> 3.9.0)
18
+ rspec-core (3.9.2)
19
+ rspec-support (~> 3.9.3)
20
+ rspec-expectations (3.9.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-mocks (3.9.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-support (3.9.3)
27
+ tty-color (0.5.2)
28
+ tty-cursor (0.7.1)
29
+ tty-prompt (0.22.0)
30
+ pastel (~> 0.8)
31
+ tty-reader (~> 0.8)
32
+ tty-reader (0.8.0)
33
+ tty-cursor (~> 0.7)
34
+ tty-screen (~> 0.8)
35
+ wisper (~> 2.0)
36
+ tty-screen (0.8.1)
37
+ wisper (2.0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ git_context!
44
+ rake (~> 12.0)
45
+ rspec (~> 3.0)
46
+
47
+ BUNDLED WITH
48
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Albert Salim
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.
@@ -0,0 +1,85 @@
1
+ # git-context
2
+
3
+ `git-context` provides a tool to manage conditional git config.
4
+
5
+ No more committing with the wrong email in a new repository. With `git-context` you can set up a context per directory. Any git repositories within the directory would use the git config specified for that directory.
6
+
7
+ Currently, the supported config values are:
8
+ - `user.name`
9
+ - `user.email`
10
+
11
+ `git-context` uses git config [conditional includes](https://git-scm.com/docs/git-config#_conditional_includes) under the hood.
12
+
13
+ ## Installation
14
+
15
+ Install the `git-context` CLI via `gem install`.
16
+
17
+ ```shell
18
+ $ gem install git_context
19
+ ```
20
+ ## Usage
21
+
22
+ First, set up `git-context` to hook into the user's `~/.git/config`.
23
+
24
+ ```shell
25
+ $ git-context setup
26
+ ```
27
+
28
+ Next, create one or more user profile to contain the user name and email address.
29
+
30
+ ```shell
31
+ $ git-context create_profile
32
+ Please enter profile name: work
33
+ Please enter the name to be used in git config: John Doe
34
+ Please enter the email address to be used in git config: johndoe@company.com
35
+
36
+ $ git-context create_profile
37
+ Please enter profile name: fun
38
+ Please enter the name to be used in git config: Johnny
39
+ Please enter the email address to be used in git config: johnny@wut.lol
40
+ ```
41
+
42
+ Finally, create contexts to use a specified profile in a directory.
43
+
44
+ ```shell
45
+ $ git-context create_context
46
+ Please enter working directory: /Users/john/work
47
+ Please select from existing profiles: work
48
+
49
+ $ git-context create_context
50
+ Please enter working directory: /Users/john/fun
51
+ Please select from existing profiles: fun
52
+ ```
53
+
54
+ Now you can commit as different users easily.
55
+
56
+ ```shell
57
+ $ cd /Users/john/work/project/unicorn
58
+ $ git config user.name
59
+ John Doe
60
+ $ git config user.email
61
+ johndoe@company.com
62
+
63
+ $ cd /Users/john/fun/lol/wut
64
+ $ git config user.name
65
+ Johnny
66
+ $ git config user.email
67
+ johnny@wut.lol
68
+ ```
69
+
70
+ **Tip:** You could also use `git context`. `git` recognizes `git-context` as a custom command.
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/caalberts/git_context.
81
+
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "git_context"
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__)
@@ -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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'git_context'
4
+
5
+ configuration = GitContext::Configuration.new(Dir.home)
6
+ command = ARGV.shift
7
+ GitContext::CLI.new(configuration).exec(command)
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/git_context/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "git_context"
5
+ spec.version = GitContext::VERSION
6
+ spec.authors = ["Albert Salim"]
7
+ spec.email = ["albertlimca@gmail.com"]
8
+
9
+ spec.summary = %q{`git-context` provides a tool to manage conditional git config.}
10
+ spec.description = %q{`git-context` provides a tool to manage conditional git config.}
11
+ spec.homepage = "https://github.com/caalberts/git_context"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/caalberts/git_context"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_runtime_dependency 'tty-prompt', '~> 0.22'
28
+ end
@@ -0,0 +1,13 @@
1
+ require 'git_context/cli'
2
+ require 'git_context/commands'
3
+ require 'git_context/configuration'
4
+ require 'git_context/interaction'
5
+ require 'git_context/version'
6
+
7
+ module GitContext
8
+ class Error < StandardError; end
9
+
10
+ Context = Struct.new(:work_dir, :profile_name)
11
+ Profile = Struct.new(:profile_name, :user)
12
+ User = Struct.new(:name, :email)
13
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ module GitContext
3
+ class CLI
4
+ COMMAND_CLASSES = {
5
+ setup: 'Setup',
6
+ create_profile: 'CreateProfile',
7
+ create_context: 'CreateContext'
8
+ }.freeze
9
+
10
+ def initialize(configuration)
11
+ @configuration = configuration
12
+ @interaction = Interaction.new
13
+ end
14
+
15
+ def exec(command_name)
16
+ command_class = command_for(command_name)
17
+ command = command_class.new(interaction: @interaction, configuration: @configuration)
18
+ command.call
19
+ rescue KeyError
20
+ warn "Unknown command #{command_name}. Supported commands are #{COMMAND_CLASSES.keys.map(&:to_s)}"
21
+ end
22
+
23
+ private
24
+
25
+ def command_for(command)
26
+ klass = COMMAND_CLASSES.fetch(command.to_sym)
27
+ Commands.const_get(klass)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ require 'git_context/commands/base'
3
+ require 'git_context/commands/create_context'
4
+ require 'git_context/commands/create_profile'
5
+ require 'git_context/commands/setup'
6
+
7
+ module GitContext
8
+ module Commands
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitContext
4
+ module Commands
5
+ class Base
6
+ def initialize(configuration:, interaction:)
7
+ @configuration = configuration
8
+ @interaction = interaction
9
+ end
10
+
11
+ def call
12
+ raise NotImplementedError
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module GitContext
3
+ module Commands
4
+ class CreateContext < Base
5
+ def call
6
+ profile_names = @configuration.list_profile_names
7
+ work_dir = @interaction.prompt_work_dir
8
+ profile_name = @interaction.prompt_profile(profile_names)
9
+
10
+ context = Context.new(work_dir, profile_name)
11
+
12
+ @configuration.add_context(context)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module GitContext
3
+ module Commands
4
+ class CreateProfile < Base
5
+ def call
6
+ profile_name = @interaction.prompt_profile_name
7
+ user_name = @interaction.prompt_user_name
8
+ user_email = @interaction.prompt_user_email
9
+
10
+ user = User.new(user_name, user_email)
11
+ profile = Profile.new(profile_name, user)
12
+ @configuration.add_profile(profile)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ module GitContext
3
+ module Commands
4
+ class Setup < Base
5
+ def call
6
+ @configuration.setup
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,105 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+
4
+ module GitContext
5
+ class Configuration
6
+ BASE_STORAGE_DIR = '.gitcontext'
7
+ PROFILES_DIR = 'profiles'
8
+ CONTEXTS_FILENAME = 'contexts'
9
+
10
+ def initialize(home_dir)
11
+ @home_dir = home_dir
12
+ end
13
+
14
+ def setup
15
+ create_base_dir unless exists?(git_context_dir)
16
+ create_contexts_file
17
+ create_profiles_dir unless exists?(profiles_dir)
18
+ include_in_gitconfig
19
+ end
20
+
21
+ def add_profile(profile)
22
+ profile_file = profile_filepath(profile.profile_name)
23
+ touch_file(profile_file)
24
+
25
+ `git config -f "#{profile_file}" --add user.name "#{profile.user.name}"`
26
+ `git config -f "#{profile_file}" --add user.email "#{profile.user.email}"`
27
+ end
28
+
29
+ def list_profile_names
30
+ Dir.entries(profiles_dir) - %w[. ..]
31
+ end
32
+
33
+ def delete_profile(profile)
34
+ profile_file = profile_filepath(profile.profile_name)
35
+ delete_file(profile_file)
36
+ end
37
+
38
+ def add_context(context)
39
+ profile_file = profile_filepath(context.profile_name)
40
+
41
+ `git config -f "#{contexts_filepath}" --add "includeIf.gitdir:#{context.work_dir}/.path" "#{profile_file}"`
42
+ end
43
+
44
+ private
45
+
46
+ def profile_filepath(profile_name)
47
+ profiles_dir.join(profile_name)
48
+ end
49
+
50
+ def create_base_dir
51
+ FileUtils.mkdir(git_context_dir)
52
+ end
53
+
54
+ def create_profiles_dir
55
+ FileUtils.mkdir(profiles_dir)
56
+ end
57
+
58
+ def profiles_dir
59
+ git_context_dir.join(PROFILES_DIR)
60
+ end
61
+
62
+ def create_contexts_file
63
+ touch_file(contexts_filepath)
64
+ end
65
+
66
+ def contexts_filepath
67
+ git_context_dir.join(CONTEXTS_FILENAME)
68
+ end
69
+
70
+ def git_context_dir
71
+ home.join(BASE_STORAGE_DIR)
72
+ end
73
+
74
+ def global_gitconfig_path
75
+ home.join('.gitconfig')
76
+ end
77
+
78
+ def include_in_gitconfig
79
+ return if include_path_exists?
80
+
81
+ `git config -f "#{global_gitconfig_path}" --add "include.path" "#{contexts_filepath}"`
82
+ end
83
+
84
+ def include_path_exists?
85
+ include_path = `git config -f "#{global_gitconfig_path}" include.path`
86
+ include_path.include?(contexts_filepath.to_s)
87
+ end
88
+
89
+ def home
90
+ Pathname.new(@home_dir)
91
+ end
92
+
93
+ def touch_file(config_file_path)
94
+ FileUtils.touch(config_file_path)
95
+ end
96
+
97
+ def delete_file(config_file_path)
98
+ FileUtils.rm(config_file_path)
99
+ end
100
+
101
+ def exists?(dir_or_file)
102
+ FileTest.exists?(dir_or_file)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ require 'tty-prompt'
3
+
4
+ module GitContext
5
+ class Interaction
6
+ def initialize(prompt = TTY::Prompt.new)
7
+ @prompt = prompt
8
+ end
9
+
10
+ def prompt_work_dir
11
+ @prompt.ask('Please enter working directory:')
12
+ end
13
+
14
+ def prompt_profile(saved_profiles)
15
+ @prompt.select('Please select from existing profiles:', saved_profiles)
16
+ end
17
+
18
+ def prompt_profile_name
19
+ @prompt.ask('Please enter profile name:')
20
+ end
21
+
22
+ def prompt_user_name
23
+ @prompt.ask('Please enter the name to be used in git config:')
24
+ end
25
+
26
+ def prompt_user_email
27
+ @prompt.ask('Please enter the email address to be used in git config:')
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module GitContext
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_context
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Albert Salim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tty-prompt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.22'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.22'
27
+ description: "`git-context` provides a tool to manage conditional git config."
28
+ email:
29
+ - albertlimca@gmail.com
30
+ executables:
31
+ - git-context
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".tool-versions"
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - exe/git-context
46
+ - git_context.gemspec
47
+ - lib/git_context.rb
48
+ - lib/git_context/cli.rb
49
+ - lib/git_context/commands.rb
50
+ - lib/git_context/commands/base.rb
51
+ - lib/git_context/commands/create_context.rb
52
+ - lib/git_context/commands/create_profile.rb
53
+ - lib/git_context/commands/setup.rb
54
+ - lib/git_context/configuration.rb
55
+ - lib/git_context/interaction.rb
56
+ - lib/git_context/version.rb
57
+ homepage: https://github.com/caalberts/git_context
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ homepage_uri: https://github.com/caalberts/git_context
62
+ source_code_uri: https://github.com/caalberts/git_context
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.3.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: "`git-context` provides a tool to manage conditional git config."
82
+ test_files: []