git-index 1.0.1

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: d07ab837d222ffa7cf563c66ef1dab75dac10310e26a80eb8d1aada2e9905f15
4
+ data.tar.gz: fbd5dd13c20b607b50491a5d46433cde722c0803d0dbff56a6fcbc2814e90cbb
5
+ SHA512:
6
+ metadata.gz: 74631ff08768bcf659be1cfdfc1f3aa3230b092e93f47937ccbe2f960db5e75ae6120ad44ecdb4c28445d7c55be0214addc9a34b9432e4f0b8fcb3d4891f0a75
7
+ data.tar.gz: b40d3e81950390e3b670f392eff8cfe1309bb5dde7f73a27cdc1d0d1134508a58ca1d07d9836b046b6fd55ea498fcee2298490c1399b7303bf1b8d0f153e6340
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kirk Haines
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,2 @@
1
+ # git-index
2
+ This is a very simple little tool that creates an index of git repositories, keyed by the commit hash codes for the first and second commits in the repository.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/git-index ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'gitindex'
4
+
5
+ GitIndex::run
data/git-index.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gitindex/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git-index"
8
+ spec.version = GitIndex::VERSION
9
+ spec.authors = ["Kirk Haines"]
10
+ spec.email = ["wyhaines@gmail.com"]
11
+
12
+ spec.summary = %q{This is a very simple little tool that creates an index of git repositories, keyed by the commit hash codes for the first and second commits in the repository.}
13
+ spec.description = <<~EDESC
14
+ This tool takes a list of paths and checks them for git repositories. It
15
+ writes to a sqlite database a table of repositories found, indexed by both
16
+ the first and the second commit hashes on the repository. The rationale is
17
+ that these first couple of commits are unlikely to ever change as the
18
+ result of a rebase, and thus make a fairly reliable fingerprint of the
19
+ identity of the repository.
20
+ EDESC
21
+ spec.homepage = ""
22
+ spec.license = "MIT"
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "bin"
28
+ spec.executables = spec.files.grep(%r{^#{spec.bindir}/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.15"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "minitest", "~> 5.0"
34
+ spec.add_dependency "sqlite3", "~> 1.3"
35
+ end
@@ -0,0 +1,74 @@
1
+
2
+ require 'optparse'
3
+ require 'gitindex/version'
4
+
5
+ module GitIndex
6
+ module Config
7
+ class << self
8
+
9
+ def parse_command_line
10
+ config = {
11
+ :database => "#{ENV['HOME']}/.git-index.db",
12
+ :recurse => false,
13
+ :verbose => false
14
+ }
15
+
16
+ options = OptionParser.new do |opts|
17
+ opts.banner = <<~EBANNER
18
+ Usage: git-index [OPTIONS] PATH1 PATH2 PATHn
19
+
20
+ This tool takes one or more paths and checks them for the presence of a git repository. If one exists, it writes a record into the database of the first and second commit hashes of the repository and the path to the repository.
21
+ EBANNER
22
+ opts.separator ''
23
+ opts.on( '-d', '--database [PATH]', String, 'The database file to write to. Defaults to $HOME/.git-index.db' ) do |path|
24
+ config[:database] = path
25
+ end
26
+ opts.on( '-r', '--recurse', 'Recursively search through the provided directories for git repositories.' ) do
27
+ config[:recurse] = true
28
+ end
29
+ opts.on( '-i', '--insert', 'The command line arguments are assumed to be paths to check for git repositories. This is the default mode of operation.' ) do
30
+ config[:command] = :insert
31
+ end
32
+ opts.on( '-x', '--delete', 'The command line arguments are assumed to be hashes or paths to delete from the databse.' ) do
33
+ config[:command] = :delete
34
+ end
35
+ opts.on( '-l', '--list', 'List the known repositories' ) do
36
+ config[:command] = :list
37
+ end
38
+ opts.on( '-q', '--query', 'The command line arguments are assumed to be hashes to query for int he database. Matches will be returned, one per line, in the same order the hashes appear on the command line.' ) do |hash|
39
+ config[:command] = :query
40
+ end
41
+ opts.on( '-v', '--verbose', 'Provide extra output about actions' ) do
42
+ config[:verbose] = true
43
+ end
44
+ opts.on( '-n', '--dry-run', "Find git repositories, but do not actually store them in the database. This option doesn't do much without also specifying --verbose." ) do
45
+ config[:dryrun] = true
46
+ end
47
+ opts.on( '--version', "Output #{version_string}" ) do
48
+ puts version_string
49
+ exit 0
50
+ end
51
+ end
52
+
53
+ leftover_argv = []
54
+ begin
55
+ options.parse!(ARGV)
56
+ rescue OptionParser::InvalidOption => e
57
+ e.recover ARGV
58
+ leftover_argv << ARGV.shift
59
+ leftover_argv << ARGV.shift if ARGV.any? && ( ARGV.first[0..0] != '-' )
60
+ retry
61
+ end
62
+
63
+ ARGV.replace( leftover_argv ) if leftover_argv.any?
64
+
65
+ config[:command] ||= :insert
66
+ config
67
+ end
68
+
69
+ def version_string
70
+ "git-index v#{GitIndex::VERSION}"
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module GitIndex
2
+ VERSION = "1.0.1"
3
+ end
data/lib/gitindex.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'sqlite3'
2
+ require 'find'
3
+ require 'gitindex/config'
4
+ require 'uri'
5
+
6
+ module GitIndex
7
+
8
+ class << self
9
+ def run
10
+ @config = Config::parse_command_line
11
+
12
+ case @config[ :command ]
13
+ when :delete
14
+ delete_records(database)
15
+ when :insert
16
+ index_git_repositories(database, git_directories)
17
+ when :list
18
+ list_records(database)
19
+ when :query
20
+ query_records(database)
21
+ end
22
+ end
23
+
24
+ def database
25
+ db = SQLite3::Database.new @config[ :database ]
26
+
27
+ begin
28
+ db.execute("select 1 from repositories") do |row|
29
+ break
30
+ end
31
+ rescue SQLite3::Exception => e
32
+ db.execute <<~SQL
33
+ create table repositories (
34
+ hash varchar(160),
35
+ path varchar(250),
36
+ url varchar(250)
37
+ );
38
+ SQL
39
+ end
40
+
41
+ db
42
+ end
43
+
44
+ def git_directories
45
+ if @config[:recurse]
46
+ untrimmed_directories = []
47
+ ARGV.each do | base_path |
48
+ Find.find( base_path ) do |path|
49
+ Find.prune if path.include? '.git'
50
+ Find.prune unless File.directory?(path)
51
+ Find.prune if system("git -C #{File.join(path,'..')} rev-parse --is-inside-work-tree > /dev/null 2>&1")
52
+ untrimmed_directories << File.expand_path( path )
53
+ end
54
+ end
55
+ else
56
+ untrimmed_directories = ARGV
57
+ end
58
+
59
+ untrimmed_directories.select do |dir|
60
+ system("git -C #{dir} rev-parse --is-inside-work-tree > /dev/null 2>&1")
61
+ end
62
+ end
63
+
64
+ def index_git_repositories( db, dirs )
65
+ dirs.each do |dir|
66
+ codes = `git -C #{dir} rev-list --parents HEAD | tail -2`.split("\n")
67
+ remote = `git -C #{dir} config --get remote.origin.url`.strip
68
+ hash = codes.length > 1 ? codes.first : codes.last
69
+
70
+ if hash =~ /^([\w\d]+)\s+([\w\d]+)$/
71
+ hash = "#{$2}#{$1}"
72
+ end
73
+ db.execute("INSERT INTO repositories (hash, path, url) VALUES (?, ?, ?)", [hash, File.expand_path(dir), remote]) unless @config[:dryrun]
74
+ puts "#{hash} -> #{File.expand_path(dir)}" if @config[:verbose]
75
+ end
76
+ end
77
+
78
+ def delete_records(db)
79
+ ARGV.each do |path_or_hash|
80
+ if path_or_hash =~ URI::regexp
81
+ puts "deleting url #{path_or_hash}" if @config[:verbose]
82
+ db.execute("DELETE from repositories WHERE url = ?", [path_or_hash]) unless @config[:dryrun]
83
+ elsif FileTest.exist?(File.expand_path(path_or_hash))
84
+ path_or_hash = File.expand_path(path_or_hash)
85
+ puts "deleting path #{path_or_hash}" if @config[:verbose]
86
+ db.execute("DELETE FROM repositories WHERE path = ?", [path_or_hash]) unless @config[:dryrun]
87
+ else
88
+ puts "deleting hashes like #{path_or_hash}" if @config[:verbose]
89
+ db.execute("DELETE FROM repositories where hash like ?", ["#{path_or_hash}%"]) unless @config[:dryrun]
90
+ end
91
+ end
92
+ end
93
+
94
+ def list_records(db)
95
+ puts "hash,path,url"
96
+ db.execute("SELECT hash, path, url FROM repositories") do |row|
97
+ puts row.join(',')
98
+ end
99
+ end
100
+
101
+ def query_records(db)
102
+ ARGV.each do |hash_or_url|
103
+ db.execute("SELECT hash, path, url from repositories WHERE hash like ? or url = ?", ["#{hash_or_url}%"],[hash_or_url]) do |row|
104
+ puts "#{row[0]}: #{row[1]}|#{row[2]}"
105
+ end
106
+ end
107
+ end
108
+
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-index
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kirk Haines
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: |
70
+ This tool takes a list of paths and checks them for git repositories. It
71
+ writes to a sqlite database a table of repositories found, indexed by both
72
+ the first and the second commit hashes on the repository. The rationale is
73
+ that these first couple of commits are unlikely to ever change as the
74
+ result of a rebase, and thus make a fairly reliable fingerprint of the
75
+ identity of the repository.
76
+ email:
77
+ - wyhaines@gmail.com
78
+ executables:
79
+ - git-index
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/git-index
87
+ - git-index.gemspec
88
+ - lib/gitindex.rb
89
+ - lib/gitindex/config.rb
90
+ - lib/gitindex/version.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: This is a very simple little tool that creates an index of git repositories,
115
+ keyed by the commit hash codes for the first and second commits in the repository.
116
+ test_files: []