sigterm_extensions 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +17 -0
  3. data/Gemfile +6 -0
  4. data/LICENSE.md +0 -0
  5. data/README.md +0 -0
  6. data/bin/ctxirb +156 -0
  7. data/lib/git.rb +166 -0
  8. data/lib/git/LICENSE +21 -0
  9. data/lib/git/author.rb +14 -0
  10. data/lib/git/base.rb +551 -0
  11. data/lib/git/base/factory.rb +75 -0
  12. data/lib/git/branch.rb +126 -0
  13. data/lib/git/branches.rb +71 -0
  14. data/lib/git/config.rb +22 -0
  15. data/lib/git/diff.rb +159 -0
  16. data/lib/git/index.rb +5 -0
  17. data/lib/git/lib.rb +1041 -0
  18. data/lib/git/log.rb +128 -0
  19. data/lib/git/object.rb +312 -0
  20. data/lib/git/path.rb +31 -0
  21. data/lib/git/remote.rb +36 -0
  22. data/lib/git/repository.rb +6 -0
  23. data/lib/git/stash.rb +27 -0
  24. data/lib/git/stashes.rb +55 -0
  25. data/lib/git/status.rb +199 -0
  26. data/lib/git/version.rb +5 -0
  27. data/lib/git/working_directory.rb +4 -0
  28. data/lib/sigterm_extensions.rb +75 -0
  29. data/lib/sigterm_extensions/all.rb +12 -0
  30. data/lib/sigterm_extensions/backtrace_cleaner.rb +129 -0
  31. data/lib/sigterm_extensions/callbacks.rb +847 -0
  32. data/lib/sigterm_extensions/concern.rb +169 -0
  33. data/lib/sigterm_extensions/configurable.rb +38 -0
  34. data/lib/sigterm_extensions/core_ext.rb +4 -0
  35. data/lib/sigterm_extensions/core_ext/array.rb +3 -0
  36. data/lib/sigterm_extensions/core_ext/array/extract.rb +19 -0
  37. data/lib/sigterm_extensions/core_ext/array/extract_options.rb +29 -0
  38. data/lib/sigterm_extensions/core_ext/class.rb +3 -0
  39. data/lib/sigterm_extensions/core_ext/class/attribute.rb +139 -0
  40. data/lib/sigterm_extensions/core_ext/class/attribute_accessors.rb +4 -0
  41. data/lib/sigterm_extensions/core_ext/class/subclasses.rb +52 -0
  42. data/lib/sigterm_extensions/core_ext/custom.rb +12 -0
  43. data/lib/sigterm_extensions/core_ext/digest.rb +3 -0
  44. data/lib/sigterm_extensions/core_ext/digest/uuid.rb +51 -0
  45. data/lib/sigterm_extensions/core_ext/enumerable.rb +232 -0
  46. data/lib/sigterm_extensions/core_ext/file.rb +3 -0
  47. data/lib/sigterm_extensions/core_ext/file/atomic.rb +68 -0
  48. data/lib/sigterm_extensions/core_ext/hash.rb +3 -0
  49. data/lib/sigterm_extensions/core_ext/hash/deep_merge.rb +41 -0
  50. data/lib/sigterm_extensions/core_ext/hash/deep_transform_values.rb +44 -0
  51. data/lib/sigterm_extensions/core_ext/hash/except.rb +22 -0
  52. data/lib/sigterm_extensions/core_ext/hash/keys.rb +141 -0
  53. data/lib/sigterm_extensions/core_ext/hash/reverse_merge.rb +23 -0
  54. data/lib/sigterm_extensions/core_ext/hash/slice.rb +24 -0
  55. data/lib/sigterm_extensions/core_ext/kernel.rb +3 -0
  56. data/lib/sigterm_extensions/core_ext/kernel/concern.rb +12 -0
  57. data/lib/sigterm_extensions/core_ext/kernel/reporting.rb +43 -0
  58. data/lib/sigterm_extensions/core_ext/kernel/singleton_class.rb +6 -0
  59. data/lib/sigterm_extensions/core_ext/load_error.rb +7 -0
  60. data/lib/sigterm_extensions/core_ext/module.rb +3 -0
  61. data/lib/sigterm_extensions/core_ext/module/aliasing.rb +29 -0
  62. data/lib/sigterm_extensions/core_ext/module/anonymous.rb +28 -0
  63. data/lib/sigterm_extensions/core_ext/module/attr_internal.rb +36 -0
  64. data/lib/sigterm_extensions/core_ext/module/attribute_accessors.rb +208 -0
  65. data/lib/sigterm_extensions/core_ext/module/attribute_accessors_per_thread.rb +146 -0
  66. data/lib/sigterm_extensions/core_ext/module/concerning.rb +132 -0
  67. data/lib/sigterm_extensions/core_ext/module/delegation.rb +319 -0
  68. data/lib/sigterm_extensions/core_ext/module/redefine_method.rb +38 -0
  69. data/lib/sigterm_extensions/core_ext/module/remove_method.rb +15 -0
  70. data/lib/sigterm_extensions/core_ext/name_error.rb +36 -0
  71. data/lib/sigterm_extensions/core_ext/object.rb +3 -0
  72. data/lib/sigterm_extensions/core_ext/object/blank.rb +153 -0
  73. data/lib/sigterm_extensions/core_ext/object/colors.rb +39 -0
  74. data/lib/sigterm_extensions/core_ext/object/duplicable.rb +47 -0
  75. data/lib/sigterm_extensions/core_ext/object/inclusion.rb +27 -0
  76. data/lib/sigterm_extensions/core_ext/object/instance_variables.rb +28 -0
  77. data/lib/sigterm_extensions/core_ext/object/methods.rb +61 -0
  78. data/lib/sigterm_extensions/core_ext/object/with_options.rb +80 -0
  79. data/lib/sigterm_extensions/core_ext/range.rb +3 -0
  80. data/lib/sigterm_extensions/core_ext/range/compare_range.rb +74 -0
  81. data/lib/sigterm_extensions/core_ext/range/conversions.rb +39 -0
  82. data/lib/sigterm_extensions/core_ext/range/overlaps.rb +8 -0
  83. data/lib/sigterm_extensions/core_ext/securerandom.rb +43 -0
  84. data/lib/sigterm_extensions/core_ext/string.rb +3 -0
  85. data/lib/sigterm_extensions/core_ext/string/access.rb +93 -0
  86. data/lib/sigterm_extensions/core_ext/string/filters.rb +143 -0
  87. data/lib/sigterm_extensions/core_ext/string/starts_ends_with.rb +4 -0
  88. data/lib/sigterm_extensions/core_ext/string/strip.rb +25 -0
  89. data/lib/sigterm_extensions/core_ext/tryable.rb +132 -0
  90. data/lib/sigterm_extensions/descendants_tracker.rb +108 -0
  91. data/lib/sigterm_extensions/gem_methods.rb +47 -0
  92. data/lib/sigterm_extensions/hash_binding.rb +16 -0
  93. data/lib/sigterm_extensions/inflector.rb +339 -0
  94. data/lib/sigterm_extensions/inflector/acronyms.rb +42 -0
  95. data/lib/sigterm_extensions/inflector/inflections.rb +249 -0
  96. data/lib/sigterm_extensions/inflector/inflections/defaults.rb +117 -0
  97. data/lib/sigterm_extensions/inflector/rules.rb +37 -0
  98. data/lib/sigterm_extensions/inflector/version.rb +8 -0
  99. data/lib/sigterm_extensions/interactive_editor.rb +120 -0
  100. data/lib/sigterm_extensions/lazy.rb +34 -0
  101. data/lib/sigterm_extensions/lazy_load_hooks.rb +79 -0
  102. data/lib/sigterm_extensions/option_merger.rb +32 -0
  103. data/lib/sigterm_extensions/ordered_hash.rb +48 -0
  104. data/lib/sigterm_extensions/ordered_options.rb +83 -0
  105. data/lib/sigterm_extensions/paths.rb +235 -0
  106. data/lib/sigterm_extensions/per_thread_registry.rb +58 -0
  107. data/lib/sigterm_extensions/proxy_object.rb +14 -0
  108. data/lib/sigterm_extensions/staging/boot.rb +31 -0
  109. data/lib/sigterm_extensions/staging/boot/bundler_patch.rb +24 -0
  110. data/lib/sigterm_extensions/staging/boot/command.rb +26 -0
  111. data/lib/sigterm_extensions/staging/boot/gemfile_next_auto_sync.rb +79 -0
  112. data/lib/sigterm_extensions/version.rb +4 -0
  113. data/lib/sigterm_extensions/wrappable.rb +16 -0
  114. data/sigterm_extensions.gemspec +42 -0
  115. data/templates/dotpryrc.rb.erb +124 -0
  116. metadata +315 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 846e4f9bb49bb713aaeb4e4cd3663a700e9a6200fc4ae370cd055c6e2a876688
4
+ data.tar.gz: 73ae7724f37986713f8cde129c2586f5b16d47926af16e0776fa932576421f37
5
+ SHA512:
6
+ metadata.gz: 7d9115e03a2dd303772f032d705964d933f75e7d500404574429f2c82dc2877157896291a1332ebb0da6681a599fef1315b925ac878cd951622407ef6fe33094
7
+ data.tar.gz: 01c87397cb8295b8a514eda835b634de142932dbb77b41847d7955de75bd2e38f4473ab7c4bd270ef0778065d9c93cfa20f7a057de66b9209ccc65fa7f3f2b46
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## Version 0.0.4
4
+ * Forked much from activesupport, thanks to the authors :)
5
+ * Just too much for me to recall :)
6
+
7
+ ## Version 0.0.3
8
+ * Bundler patch
9
+ * ctxirb can now live in other ruby/bundler projects
10
+
11
+ ## Version 0.0.2
12
+ * Git extension
13
+
14
+ ## Version 0.0.1
15
+ * Initial commit
16
+ * Inflector extension
17
+ * Core extensions (initial version)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ ruby '2.6.5'
5
+
6
+ gem 'irbtools-more', require: 'irbtools/binding'
File without changes
File without changes
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'irb'
5
+ require 'irb/completion'
6
+ require 'irb/ext/save-history'
7
+ require 'highline/import'
8
+ require 'pry'
9
+ lib = File.expand_path("../lib", __dir__)
10
+ $:.unshift(lib) unless $LOAD_PATH.include?(lib)
11
+
12
+ pwd_lib = File.expand_path('lib', Dir.pwd)
13
+ $:.unshift(pwd_lib) if File.exists?(pwd_lib)
14
+ #puts $:
15
+
16
+ require 'sigterm_extensions'
17
+
18
+
19
+ def colorize_objects!
20
+ # Method to pretty-print object methods
21
+ # Coded by sebastian delmont
22
+ # http://snippets.dzone.com/posts/show/2916
23
+ Object.class_eval do
24
+ const_set :ANSI_BOLD , "\033[1m"
25
+ const_set :ANSI_RESET , "\033[0m"
26
+ const_set :ANSI_LGRAY , "\033[0;37m"
27
+ const_set :ANSI_GRAY , "\033[1;30m"
28
+ # Print object's methods
29
+ def pm(*options)
30
+ methods = self.methods
31
+ methods -= Object.methods unless options.include? :more
32
+ filter = options.select {|opt| opt.kind_of? Regexp}.first
33
+ methods = methods.select {|name| name =~ filter} if filter
34
+
35
+ data = methods.sort.collect do |name|
36
+ method = self.method(name)
37
+ if method.arity == 0
38
+ args = "()"
39
+ elsif method.arity > 0
40
+ n = method.arity
41
+ args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
42
+ elsif method.arity < 0
43
+ n = -method.arity
44
+ args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
45
+ end
46
+ klass = $1 if method.inspect =~ /Method: (.*?)#/
47
+ [name, args, klass]
48
+ end
49
+ max_name = data.collect {|item| item[0].size}.max
50
+ max_args = data.collect {|item| item[1].size}.max
51
+ data.each do |item|
52
+ print " #{ANSI_BOLD}#{item[0].to_s.rjust(max_name)}#{ANSI_RESET}"
53
+ print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
54
+ print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
55
+ end
56
+ data.size
57
+ end
58
+ end
59
+ end
60
+
61
+ module IRB # :nodoc:
62
+ def self.start_session(binding)
63
+ unless @__initialized
64
+ args = ARGV
65
+ ARGV.replace(ARGV.dup)
66
+ IRB.setup(nil)
67
+ ARGV.replace(args)
68
+ @__initialized = true
69
+ end
70
+
71
+ workspace = WorkSpace.new(binding)
72
+
73
+ irb = Irb.new(workspace)
74
+
75
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
76
+ @CONF[:MAIN_CONTEXT] = irb.context
77
+ @CONF[:SAVE_HISTORY] = 10000
78
+ @CONF[:HISTORY_FILE] = File.join(ENV['PWD'], '.irb-history')
79
+ @CONF[:BACK_TRACE_LIMIT] = 16
80
+ @CONF[:USE_READLINE] = true
81
+ @CONF[:AUTO_INDENT] = true
82
+ histfile = @CONF[:HISTORY_FILE]
83
+
84
+ #puts @CONF
85
+ maxsize = @CONF[:SAVE_HISTORY]
86
+
87
+ if File::exists?(histfile)
88
+ lines = IO::readlines(histfile).collect { |line| line.chomp }
89
+ puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
90
+ Readline::HISTORY.push(*lines)
91
+ end
92
+
93
+ Kernel::at_exit do
94
+ lines = Readline::HISTORY.to_a.reverse.uniq.reverse
95
+ lines = lines[-maxsize, maxsize] if lines.length > maxsize
96
+ puts "Saving #{lines.length} history lines to '#{histfile}'."
97
+ File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
98
+ end
99
+
100
+ colorize_objects!
101
+
102
+ catch(:IRB_EXIT) do
103
+ irb.eval_input
104
+ end
105
+ end
106
+ end
107
+
108
+ Signal.trap("INT") {
109
+ confirm = ask("\033[31mExit? [Y/n] \033[0m") { |yn| yn.limit = 1, yn.validate = /[yn]/i }
110
+ exit unless confirm.downcase == 'n'
111
+ }
112
+
113
+ #ENV['BUNDLE_GEMFILE'] = File.expand_path('Gemfile', Dir.pwd)
114
+ #bundler_runtime = Bundler.setup
115
+ #bundler_runtime.setup(:default,'development')
116
+
117
+ #eval File.read('Gemfile') if File.exists? 'Gemfile'
118
+ #Gem.refresh
119
+ #load_everything!
120
+ #pry
121
+ #puts $:
122
+
123
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', Dir.pwd)
124
+ require 'bundler/setup'
125
+
126
+ ctx_files = Dir["#{Dir.pwd}/lib/*.rb"]
127
+ ctx_files.each do |f|
128
+ require f
129
+ end
130
+
131
+ def reload!(print = true)
132
+ puts 'Reloading ...' if print
133
+ # Silence warnings
134
+ warn_level = $VERBOSE
135
+ $VERBOSE = nil
136
+ # Main project directory.
137
+ root_dir = Dir.pwd #File.expand_path('..', __dir__)
138
+ sigterm_dir = File.expand_path('..', __dir__)
139
+ # Directories within the project that should be reloaded.
140
+ reload_dirs = %w{lib}
141
+ # Loop through and reload every file in all relevant project directories.
142
+ reload_dirs.each do |dir|
143
+ Dir.glob("#{root_dir}/#{dir}/**/*.rb").each { |f| load(f) }
144
+ end
145
+ # Reload sigterm extensions directory as well
146
+ reload_dirs.each do |dir|
147
+ Dir.glob("#{sigterm_dir}/#{dir}/**/*.rb").each { |f| load(f) }
148
+ end
149
+ # Re-enable warnings
150
+ $VERBOSE = warn_level
151
+ # Return true when complete.
152
+ true
153
+ end
154
+
155
+ IRB.start_session(binding)
156
+
@@ -0,0 +1,166 @@
1
+ # Add the directory containing this file to the start of the load path if it
2
+ # isn't there already.
3
+ $:.unshift(File.dirname(__FILE__)) unless
4
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require 'git/author'
7
+ require 'git/base'
8
+ require 'git/branch'
9
+ require 'git/branches'
10
+ require 'git/config'
11
+ require 'git/diff'
12
+ require 'git/index'
13
+ require 'git/lib'
14
+ require 'git/log'
15
+ require 'git/object'
16
+ require 'git/path'
17
+ require 'git/remote'
18
+ require 'git/repository'
19
+ require 'git/status'
20
+ require 'git/stash'
21
+ require 'git/stashes'
22
+ require 'git/working_directory'
23
+
24
+ lib = Git::Lib.new(nil, nil)
25
+ unless lib.meets_required_version?
26
+ $stderr.puts "[WARNING] The git gem requires git #{lib.required_command_version.join('.')} or later, but only found #{lib.current_command_version.join('.')}. You should probably upgrade."
27
+ end
28
+
29
+ # Git/Ruby Library
30
+ #
31
+ # This provides bindings for working with git in complex
32
+ # interactions, including branching and merging, object
33
+ # inspection and manipulation, history, patch generation
34
+ # and more. You should be able to do most fundamental git
35
+ # operations with this library.
36
+ #
37
+ # This module provides the basic functions to open a git
38
+ # reference to work with. You can open a working directory,
39
+ # open a bare repository, initialize a new repo or clone an
40
+ # existing remote repository.
41
+ #
42
+ # Author:: Scott Chacon (mailto:schacon@gmail.com)
43
+ # License:: MIT License
44
+ module Git
45
+
46
+ #g.config('user.name', 'Scott Chacon') # sets value
47
+ #g.config('user.email', 'email@email.com') # sets value
48
+ #g.config('user.name') # returns 'Scott Chacon'
49
+ #g.config # returns whole config hash
50
+ def config(name = nil, value = nil)
51
+ lib = Git::Lib.new
52
+ if(name && value)
53
+ # set value
54
+ lib.config_set(name, value)
55
+ elsif (name)
56
+ # return value
57
+ lib.config_get(name)
58
+ else
59
+ # return hash
60
+ lib.config_list
61
+ end
62
+ end
63
+
64
+ def self.configure
65
+ yield Base.config
66
+ end
67
+
68
+ def self.config
69
+ return Base.config
70
+ end
71
+
72
+ def global_config(name = nil, value = nil)
73
+ self.class.global_config(name, value)
74
+ end
75
+
76
+ # open a bare repository
77
+ #
78
+ # this takes the path to a bare git repo
79
+ # it expects not to be able to use a working directory
80
+ # so you can't checkout stuff, commit things, etc.
81
+ # but you can do most read operations
82
+ def self.bare(git_dir, options = {})
83
+ Base.bare(git_dir, options)
84
+ end
85
+
86
+ # clones a remote repository
87
+ #
88
+ # options
89
+ # :bare => true (does a bare clone)
90
+ # :repository => '/path/to/alt_git_dir'
91
+ # :index => '/path/to/alt_index_file'
92
+ #
93
+ # example
94
+ # Git.clone('git://repo.or.cz/rubygit.git', 'clone.git', :bare => true)
95
+ #
96
+ def self.clone(repository, name, options = {})
97
+ Base.clone(repository, name, options)
98
+ end
99
+
100
+ # Export the current HEAD (or a branch, if <tt>options[:branch]</tt>
101
+ # is specified) into the +name+ directory, then remove all traces of git from the
102
+ # directory.
103
+ #
104
+ # See +clone+ for options. Does not obey the <tt>:remote</tt> option,
105
+ # since the .git info will be deleted anyway; always uses the default
106
+ # remote, 'origin.'
107
+ def self.export(repository, name, options = {})
108
+ options.delete(:remote)
109
+ repo = clone(repository, name, {:depth => 1}.merge(options))
110
+ repo.checkout("origin/#{options[:branch]}") if options[:branch]
111
+ Dir.chdir(repo.dir.to_s) { FileUtils.rm_r '.git' }
112
+ end
113
+
114
+ # Same as g.config, but forces it to be at the global level
115
+ #
116
+ #g.config('user.name', 'Scott Chacon') # sets value
117
+ #g.config('user.email', 'email@email.com') # sets value
118
+ #g.config('user.name') # returns 'Scott Chacon'
119
+ #g.config # returns whole config hash
120
+ def self.global_config(name = nil, value = nil)
121
+ lib = Git::Lib.new(nil, nil)
122
+ if(name && value)
123
+ # set value
124
+ lib.global_config_set(name, value)
125
+ elsif (name)
126
+ # return value
127
+ lib.global_config_get(name)
128
+ else
129
+ # return hash
130
+ lib.global_config_list
131
+ end
132
+ end
133
+
134
+ # initialize a new git repository, defaults to the current working directory
135
+ #
136
+ # options
137
+ # :repository => '/path/to/alt_git_dir'
138
+ # :index => '/path/to/alt_index_file'
139
+ def self.init(working_dir = '.', options = {})
140
+ Base.init(working_dir, options)
141
+ end
142
+
143
+ # returns a Hash containing information about the references
144
+ # of the target repository
145
+ #
146
+ # @param [String|NilClass] location the target repository location or nil for '.'
147
+ # @return [{String=>Hash}] the available references of the target repo.
148
+ def self.ls_remote(location=nil)
149
+ Git::Lib.new.ls_remote(location)
150
+ end
151
+
152
+ # open an existing git working directory
153
+ #
154
+ # this will most likely be the most common way to create
155
+ # a git reference, referring to a working directory.
156
+ # if not provided in the options, the library will assume
157
+ # your git_dir and index are in the default place (.git/, .git/index)
158
+ #
159
+ # options
160
+ # :repository => '/path/to/alt_git_dir'
161
+ # :index => '/path/to/alt_index_file'
162
+ def self.open(working_dir, options = {})
163
+ Base.open(working_dir, options)
164
+ end
165
+
166
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2008 Scott Chacon
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,14 @@
1
+ module Git
2
+ class Author
3
+ attr_accessor :name, :email, :date
4
+
5
+ def initialize(author_string)
6
+ if m = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
7
+ @name = m[1]
8
+ @email = m[2]
9
+ @date = Time.at(m[3].to_i)
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,551 @@
1
+ require 'git/base/factory'
2
+
3
+ module Git
4
+
5
+ class Base
6
+
7
+ include Git::Base::Factory
8
+
9
+ # opens a bare Git Repository - no working directory options
10
+ def self.bare(git_dir, opts = {})
11
+ self.new({:repository => git_dir}.merge(opts))
12
+ end
13
+
14
+ # clones a git repository locally
15
+ #
16
+ # repository - http://repo.or.cz/w/sinatra.git
17
+ # name - sinatra
18
+ #
19
+ # options:
20
+ # :repository
21
+ #
22
+ # :bare
23
+ # or
24
+ # :working_directory
25
+ # :index_file
26
+ #
27
+ def self.clone(repository, name, opts = {})
28
+ # run git-clone
29
+ self.new(Git::Lib.new.clone(repository, name, opts))
30
+ end
31
+
32
+ # Returns (and initialize if needed) a Git::Config instance
33
+ #
34
+ # @return [Git::Config] the current config instance.
35
+ def self.config
36
+ return @@config ||= Config.new
37
+ end
38
+
39
+ # initializes a git repository
40
+ #
41
+ # options:
42
+ # :bare
43
+ # :index
44
+ # :repository
45
+ #
46
+ def self.init(working_dir, opts = {})
47
+ opts[:working_directory] ||= working_dir
48
+ opts[:repository] ||= File.join(opts[:working_directory], '.git')
49
+
50
+ FileUtils.mkdir_p(opts[:working_directory]) if opts[:working_directory] && !File.directory?(opts[:working_directory])
51
+
52
+ init_opts = {
53
+ :bare => opts[:bare]
54
+ }
55
+
56
+ opts.delete(:working_directory) if opts[:bare]
57
+
58
+ # Submodules have a .git *file* not a .git folder.
59
+ # This file's contents point to the location of
60
+ # where the git refs are held (In the parent repo)
61
+ if File.file?('.git')
62
+ git_file = File.open('.git').read[8..-1].strip
63
+ opts[:repository] = git_file
64
+ opts[:index] = git_file + '/index'
65
+ end
66
+
67
+ Git::Lib.new(opts).init(init_opts)
68
+
69
+ self.new(opts)
70
+ end
71
+
72
+ # opens a new Git Project from a working directory
73
+ # you can specify non-standard git_dir and index file in the options
74
+ def self.open(working_dir, opts={})
75
+ self.new({:working_directory => working_dir}.merge(opts))
76
+ end
77
+
78
+ def initialize(options = {})
79
+ if working_dir = options[:working_directory]
80
+ options[:repository] ||= File.join(working_dir, '.git')
81
+ options[:index] ||= File.join(working_dir, '.git', 'index')
82
+ end
83
+ if options[:log]
84
+ @logger = options[:log]
85
+ @logger.info("Starting Git")
86
+ else
87
+ @logger = nil
88
+ end
89
+
90
+ @working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
91
+ @repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
92
+ @index = options[:index] ? Git::Index.new(options[:index], false) : nil
93
+ end
94
+
95
+ # changes current working directory for a block
96
+ # to the git working directory
97
+ #
98
+ # example
99
+ # @git.chdir do
100
+ # # write files
101
+ # @git.add
102
+ # @git.commit('message')
103
+ # end
104
+ def chdir # :yields: the Git::Path
105
+ Dir.chdir(dir.path) do
106
+ yield dir.path
107
+ end
108
+ end
109
+
110
+ #g.config('user.name', 'Scott Chacon') # sets value
111
+ #g.config('user.email', 'email@email.com') # sets value
112
+ #g.config('user.name') # returns 'Scott Chacon'
113
+ #g.config # returns whole config hash
114
+ def config(name = nil, value = nil)
115
+ if(name && value)
116
+ # set value
117
+ lib.config_set(name, value)
118
+ elsif (name)
119
+ # return value
120
+ lib.config_get(name)
121
+ else
122
+ # return hash
123
+ lib.config_list
124
+ end
125
+ end
126
+
127
+ # returns a reference to the working directory
128
+ # @git.dir.path
129
+ # @git.dir.writeable?
130
+ def dir
131
+ @working_directory
132
+ end
133
+
134
+ # returns reference to the git index file
135
+ def index
136
+ @index
137
+ end
138
+
139
+ # returns reference to the git repository directory
140
+ # @git.dir.path
141
+ def repo
142
+ @repository
143
+ end
144
+
145
+ # returns the repository size in bytes
146
+ def repo_size
147
+ Dir.chdir(repo.path) do
148
+ return `du -s`.chomp.split.first.to_i
149
+ end
150
+ end
151
+
152
+ def set_index(index_file, check = true)
153
+ @lib = nil
154
+ @index = Git::Index.new(index_file.to_s, check)
155
+ end
156
+
157
+ def set_working(work_dir, check = true)
158
+ @lib = nil
159
+ @working_directory = Git::WorkingDirectory.new(work_dir.to_s, check)
160
+ end
161
+
162
+ # returns +true+ if the branch exists locally
163
+ def is_local_branch?(branch)
164
+ branch_names = self.branches.local.map {|b| b.name}
165
+ branch_names.include?(branch)
166
+ end
167
+
168
+ # returns +true+ if the branch exists remotely
169
+ def is_remote_branch?(branch)
170
+ branch_names = self.branches.remote.map {|b| b.name}
171
+ branch_names.include?(branch)
172
+ end
173
+
174
+ # returns +true+ if the branch exists
175
+ def is_branch?(branch)
176
+ branch_names = self.branches.map {|b| b.name}
177
+ branch_names.include?(branch)
178
+ end
179
+
180
+ # this is a convenience method for accessing the class that wraps all the
181
+ # actual 'git' forked system calls. At some point I hope to replace the Git::Lib
182
+ # class with one that uses native methods or libgit C bindings
183
+ def lib
184
+ @lib ||= Git::Lib.new(self, @logger)
185
+ end
186
+
187
+ # will run a grep for 'string' on the HEAD of the git repository
188
+ #
189
+ # to be more surgical in your grep, you can call grep() off a specific
190
+ # git object. for example:
191
+ #
192
+ # @git.object("v2.3").grep('TODO')
193
+ #
194
+ # in any case, it returns a hash of arrays of the type:
195
+ # hsh[tree-ish] = [[line_no, match], [line_no, match2]]
196
+ # hsh[tree-ish] = [[line_no, match], [line_no, match2]]
197
+ #
198
+ # so you might use it like this:
199
+ #
200
+ # @git.grep("TODO").each do |sha, arr|
201
+ # puts "in blob #{sha}:"
202
+ # arr.each do |match|
203
+ # puts "\t line #{match[0]}: '#{match[1]}'"
204
+ # end
205
+ # end
206
+ def grep(string, path_limiter = nil, opts = {})
207
+ self.object('HEAD').grep(string, path_limiter, opts)
208
+ end
209
+
210
+ # updates the repository index using the working directory content
211
+ #
212
+ # @git.add('path/to/file')
213
+ # @git.add(['path/to/file1','path/to/file2'])
214
+ # @git.add(:all => true)
215
+ #
216
+ # options:
217
+ # :all => true
218
+ #
219
+ # @param [String,Array] paths files paths to be added (optional, default='.')
220
+ # @param [Hash] options
221
+ def add(*args)
222
+ if args[0].instance_of?(String) || args[0].instance_of?(Array)
223
+ self.lib.add(args[0],args[1]||{})
224
+ else
225
+ self.lib.add('.', args[0]||{})
226
+ end
227
+ end
228
+
229
+ # removes file(s) from the git repository
230
+ def remove(path = '.', opts = {})
231
+ self.lib.remove(path, opts)
232
+ end
233
+
234
+ # resets the working directory to the provided commitish
235
+ def reset(commitish = nil, opts = {})
236
+ self.lib.reset(commitish, opts)
237
+ end
238
+
239
+ # resets the working directory to the commitish with '--hard'
240
+ def reset_hard(commitish = nil, opts = {})
241
+ opts = {:hard => true}.merge(opts)
242
+ self.lib.reset(commitish, opts)
243
+ end
244
+
245
+ # cleans the working directory
246
+ #
247
+ # options:
248
+ # :force
249
+ # :d
250
+ #
251
+ def clean(opts = {})
252
+ self.lib.clean(opts)
253
+ end
254
+
255
+ # returns the most recent tag that is reachable from a commit
256
+ #
257
+ # options:
258
+ # :all
259
+ # :tags
260
+ # :contains
261
+ # :debug
262
+ # :exact_match
263
+ # :dirty
264
+ # :abbrev
265
+ # :candidates
266
+ # :long
267
+ # :always
268
+ # :match
269
+ #
270
+ def describe(committish=nil, opts={})
271
+ self.lib.describe(committish, opts)
272
+ end
273
+
274
+ # reverts the working directory to the provided commitish.
275
+ # Accepts a range, such as comittish..HEAD
276
+ #
277
+ # options:
278
+ # :no_edit
279
+ #
280
+ def revert(commitish = nil, opts = {})
281
+ self.lib.revert(commitish, opts)
282
+ end
283
+
284
+ # commits all pending changes in the index file to the git repository
285
+ #
286
+ # options:
287
+ # :all
288
+ # :allow_empty
289
+ # :amend
290
+ # :author
291
+ #
292
+ def commit(message, opts = {})
293
+ self.lib.commit(message, opts)
294
+ end
295
+
296
+ # commits all pending changes in the index file to the git repository,
297
+ # but automatically adds all modified files without having to explicitly
298
+ # calling @git.add() on them.
299
+ def commit_all(message, opts = {})
300
+ opts = {:add_all => true}.merge(opts)
301
+ self.lib.commit(message, opts)
302
+ end
303
+
304
+ # checks out a branch as the new git working directory
305
+ def checkout(branch = 'master', opts = {})
306
+ self.lib.checkout(branch, opts)
307
+ end
308
+
309
+ # checks out an old version of a file
310
+ def checkout_file(version, file)
311
+ self.lib.checkout_file(version,file)
312
+ end
313
+
314
+ # fetches changes from a remote branch - this does not modify the working directory,
315
+ # it just gets the changes from the remote if there are any
316
+ def fetch(remote = 'origin', opts={})
317
+ self.lib.fetch(remote, opts)
318
+ end
319
+
320
+ # pushes changes to a remote repository - easiest if this is a cloned repository,
321
+ # otherwise you may have to run something like this first to setup the push parameters:
322
+ #
323
+ # @git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
324
+ #
325
+ def push(remote = 'origin', branch = 'master', opts = {})
326
+ # Small hack to keep backwards compatibility with the 'push(remote, branch, tags)' method signature.
327
+ opts = {:tags => opts} if [true, false].include?(opts)
328
+
329
+ self.lib.push(remote, branch, opts)
330
+ end
331
+
332
+ # merges one or more branches into the current working branch
333
+ #
334
+ # you can specify more than one branch to merge by passing an array of branches
335
+ def merge(branch, message = 'merge')
336
+ self.lib.merge(branch, message)
337
+ end
338
+
339
+ # iterates over the files which are unmerged
340
+ def each_conflict(&block) # :yields: file, your_version, their_version
341
+ self.lib.conflicts(&block)
342
+ end
343
+
344
+ # pulls the given branch from the given remote into the current branch
345
+ #
346
+ # @git.pull # pulls from origin/master
347
+ # @git.pull('upstream') # pulls from upstream/master
348
+ # @git.pull('upstream', 'develope') # pulls from upstream/develop
349
+ #
350
+ def pull(remote='origin', branch='master')
351
+ self.lib.pull(remote, branch)
352
+ end
353
+
354
+ # returns an array of Git:Remote objects
355
+ def remotes
356
+ self.lib.remotes.map { |r| Git::Remote.new(self, r) }
357
+ end
358
+
359
+ # adds a new remote to this repository
360
+ # url can be a git url or a Git::Base object if it's a local reference
361
+ #
362
+ # @git.add_remote('scotts_git', 'git://repo.or.cz/rubygit.git')
363
+ # @git.fetch('scotts_git')
364
+ # @git.merge('scotts_git/master')
365
+ #
366
+ # Options:
367
+ # :fetch => true
368
+ # :track => <branch_name>
369
+ def add_remote(name, url, opts = {})
370
+ url = url.repo.path if url.is_a?(Git::Base)
371
+ self.lib.remote_add(name, url, opts)
372
+ Git::Remote.new(self, name)
373
+ end
374
+
375
+ # sets the url for a remote
376
+ # url can be a git url or a Git::Base object if it's a local reference
377
+ #
378
+ # @git.set_remote_url('scotts_git', 'git://repo.or.cz/rubygit.git')
379
+ #
380
+ def set_remote_url(name, url)
381
+ url = url.repo.path if url.is_a?(Git::Base)
382
+ self.lib.remote_set_url(name, url)
383
+ Git::Remote.new(self, name)
384
+ end
385
+
386
+ # removes a remote from this repository
387
+ #
388
+ # @git.remove_remote('scott_git')
389
+ def remove_remote(name)
390
+ self.lib.remote_remove(name)
391
+ end
392
+
393
+ # returns an array of all Git::Tag objects for this repository
394
+ def tags
395
+ self.lib.tags.map { |r| tag(r) }
396
+ end
397
+
398
+ # Creates a new git tag (Git::Tag)
399
+ # Usage:
400
+ # repo.add_tag('tag_name', object_reference)
401
+ # repo.add_tag('tag_name', object_reference, {:options => 'here'})
402
+ # repo.add_tag('tag_name', {:options => 'here'})
403
+ #
404
+ # Options:
405
+ # :a | :annotate -> true
406
+ # :d -> true
407
+ # :f -> true
408
+ # :m | :message -> String
409
+ # :s -> true
410
+ #
411
+ def add_tag(name, *opts)
412
+ self.lib.tag(name, *opts)
413
+ self.tag(name)
414
+ end
415
+
416
+ # deletes a tag
417
+ def delete_tag(name)
418
+ self.lib.tag(name, {:d => true})
419
+ end
420
+
421
+ # creates an archive file of the given tree-ish
422
+ def archive(treeish, file = nil, opts = {})
423
+ self.object(treeish).archive(file, opts)
424
+ end
425
+
426
+ # repacks the repository
427
+ def repack
428
+ self.lib.repack
429
+ end
430
+
431
+ def gc
432
+ self.lib.gc
433
+ end
434
+
435
+ def apply(file)
436
+ if File.exist?(file)
437
+ self.lib.apply(file)
438
+ end
439
+ end
440
+
441
+ def apply_mail(file)
442
+ self.lib.apply_mail(file) if File.exist?(file)
443
+ end
444
+
445
+ # Shows objects
446
+ #
447
+ # @param [String|NilClass] objectish the target object reference (nil == HEAD)
448
+ # @param [String|NilClass] path the path of the file to be shown
449
+ # @return [String] the object information
450
+ def show(objectish=nil, path=nil)
451
+ self.lib.show(objectish, path)
452
+ end
453
+
454
+ ## LOWER LEVEL INDEX OPERATIONS ##
455
+
456
+ def with_index(new_index) # :yields: new_index
457
+ old_index = @index
458
+ set_index(new_index, false)
459
+ return_value = yield @index
460
+ set_index(old_index)
461
+ return_value
462
+ end
463
+
464
+ def with_temp_index &blk
465
+ # Workaround for JRUBY, since they handle the TempFile path different.
466
+ # MUST be improved to be safer and OS independent.
467
+ if RUBY_PLATFORM == 'java'
468
+ temp_path = "/tmp/temp-index-#{(0...15).map{ ('a'..'z').to_a[rand(26)] }.join}"
469
+ else
470
+ tempfile = Tempfile.new('temp-index')
471
+ temp_path = tempfile.path
472
+ tempfile.close
473
+ tempfile.unlink
474
+ end
475
+
476
+ with_index(temp_path, &blk)
477
+ end
478
+
479
+ def checkout_index(opts = {})
480
+ self.lib.checkout_index(opts)
481
+ end
482
+
483
+ def read_tree(treeish, opts = {})
484
+ self.lib.read_tree(treeish, opts)
485
+ end
486
+
487
+ def write_tree
488
+ self.lib.write_tree
489
+ end
490
+
491
+ def write_and_commit_tree(opts = {})
492
+ tree = write_tree
493
+ commit_tree(tree, opts)
494
+ end
495
+
496
+ def update_ref(branch, commit)
497
+ branch(branch).update_ref(commit)
498
+ end
499
+
500
+
501
+ def ls_files(location=nil)
502
+ self.lib.ls_files(location)
503
+ end
504
+
505
+ def with_working(work_dir) # :yields: the Git::WorkingDirectory
506
+ return_value = false
507
+ old_working = @working_directory
508
+ set_working(work_dir)
509
+ Dir.chdir work_dir do
510
+ return_value = yield @working_directory
511
+ end
512
+ set_working(old_working)
513
+ return_value
514
+ end
515
+
516
+ def with_temp_working &blk
517
+ tempfile = Tempfile.new("temp-workdir")
518
+ temp_dir = tempfile.path
519
+ tempfile.close
520
+ tempfile.unlink
521
+ Dir.mkdir(temp_dir, 0700)
522
+ with_working(temp_dir, &blk)
523
+ end
524
+
525
+
526
+ # runs git rev-parse to convert the objectish to a full sha
527
+ #
528
+ # @git.revparse("HEAD^^")
529
+ # @git.revparse('v2.4^{tree}')
530
+ # @git.revparse('v2.4:/doc/index.html')
531
+ #
532
+ def revparse(objectish)
533
+ self.lib.revparse(objectish)
534
+ end
535
+
536
+ def ls_tree(objectish)
537
+ self.lib.ls_tree(objectish)
538
+ end
539
+
540
+ def cat_file(objectish)
541
+ self.lib.object_contents(objectish)
542
+ end
543
+
544
+ # returns the name of the branch the working directory is currently on
545
+ def current_branch
546
+ self.lib.branch_current
547
+ end
548
+
549
+ end
550
+
551
+ end