git_tree 0.2.1 → 0.2.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 +4 -4
- data/.rubocop.yml +4 -2
- data/CHANGELOG.md +7 -0
- data/README.md +23 -22
- data/Rakefile +2 -2
- data/bindir/{git_tree_evars → git-tree-evars} +1 -1
- data/bindir/{git_tree_replicate → git-tree-replicate} +1 -1
- data/git_tree.gemspec +2 -1
- data/lib/git_tree/version.rb +1 -1
- data/lib/git_tree.rb +8 -101
- data/lib/git_tree_evars.rb +82 -0
- data/lib/git_tree_replicate.rb +86 -0
- data/lib/util.rb +62 -1
- metadata +22 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 744efed3dbbb3370021be4abf7e50f5105f340f0b00c2fe525416d18665b6561
|
4
|
+
data.tar.gz: 938fdd59658c6ec5a74b120da486ab65d418cb5f34e69b13279bfc3656abdf50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b35184a83646f7ccffd3e34e744da2320163d46f37b2ea3f3f1511cb73d63f13f098e8472da6b792d31101f0b340b49bbc2b4eb29bdd903cae044cf129a2e34d
|
7
|
+
data.tar.gz: 1cc7aaa472d842c40c5f80b747af0d55c55737b2cf4aa32a73c42081898fbc051fc30169554380dcba649b96d33cf3f4125c55efbbffbc449a5a7117191cf8ae
|
data/.rubocop.yml
CHANGED
@@ -66,6 +66,9 @@ Naming/FileName:
|
|
66
66
|
RSpec/ExampleLength:
|
67
67
|
Max: 20
|
68
68
|
|
69
|
+
RSpec/FilePath:
|
70
|
+
Enabled: false
|
71
|
+
|
69
72
|
RSpec/MultipleExpectations:
|
70
73
|
Max: 15
|
71
74
|
|
@@ -88,8 +91,7 @@ Style/RegexpLiteral:
|
|
88
91
|
Enabled: false
|
89
92
|
|
90
93
|
Style/StringConcatenation:
|
91
|
-
|
92
|
-
- spec/**/*
|
94
|
+
Enabled: false
|
93
95
|
|
94
96
|
Style/StringLiterals:
|
95
97
|
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## 0.2.3 / 2023-05-26
|
2
|
+
* Improved help messages.
|
3
|
+
* Renamed executables to `git-tree-replicate` and `git-tree-evars`.
|
4
|
+
|
5
|
+
## 0.2.2 / 2023-05-23
|
6
|
+
* `git_tree_evars` now checks for previous definitions and issues warnings.
|
7
|
+
|
1
8
|
## 0.2.1 / 2023-05-03
|
2
9
|
* Removed the here document wrapper from the output of `git_tree_evars`.
|
3
10
|
|
data/README.md
CHANGED
@@ -5,36 +5,33 @@
|
|
5
5
|
This Ruby gem installs two commands that scan a git directory tree and write out scripts.
|
6
6
|
Directories containing a file called `.ignore` are ignored.
|
7
7
|
|
8
|
-
- The `
|
8
|
+
- The `git-tree-replicate` command writes a script that clones the repos in the tree,
|
9
9
|
and adds any defined remotes.
|
10
10
|
- Any git repos that have already been cloned into the target directory tree are skipped.
|
11
|
-
This means you can rerun `
|
11
|
+
This means you can rerun `git-tree-replicate` as many times as you want, without ill effects.
|
12
12
|
- All remotes in each repo are replicated.
|
13
13
|
|
14
|
-
- The `
|
14
|
+
- The `git-tree-evars` command writes a script that defines environment variables pointing to git repos.
|
15
15
|
|
16
16
|
|
17
17
|
## Usage
|
18
|
-
Both commands
|
19
|
-
the name of the
|
18
|
+
Both commands require one environment variable reference to be passed to them.
|
19
|
+
Enclose the name of the environment variable within single quotes,
|
20
|
+
which will prevent the shell from expanding it before invoking the command.
|
20
21
|
|
21
|
-
You must pass an environment variable to both commands.
|
22
|
-
Enclosing the name of the env var in single quotes,
|
23
|
-
which will prevent the shell from expanding it before invoking either command.
|
24
22
|
|
25
|
-
|
26
|
-
## `Git_tree_replicate` Usage
|
23
|
+
## `git-tree-replicate` Usage
|
27
24
|
The following creates a script in the current directory called `work.sh`,
|
28
25
|
that replicates the desired portions of the directory tree of git repos pointed to by `$work`:
|
29
26
|
```shell
|
30
|
-
$
|
27
|
+
$ git-tree-replicate '$work' > work.sh
|
31
28
|
```
|
32
29
|
|
33
30
|
The generated environment variables will all be relative to the
|
34
|
-
|
35
|
-
You will understand what this means once you
|
31
|
+
path pointed to by the expanded environment variable that you provided.
|
32
|
+
You will understand what this means once you look at the generated script.
|
36
33
|
|
37
|
-
When `
|
34
|
+
When `git-tree-replicate` completes,
|
38
35
|
edit the generated script to suit, then
|
39
36
|
copy it to the target machine and run it.
|
40
37
|
The following example copies the script to `machine2` and runs it:
|
@@ -46,7 +43,7 @@ $ ssh machine2 work.sh
|
|
46
43
|
```
|
47
44
|
|
48
45
|
|
49
|
-
### Generated Script from `
|
46
|
+
### Generated Script from `git-tree-replicate`
|
50
47
|
Following is a sample of one section, which is repeated for every git repo that is processed:
|
51
48
|
You can edit them to suit.
|
52
49
|
|
@@ -60,8 +57,8 @@ if [ ! -d "sinatra/sinatras-skeleton/.git" ]; then
|
|
60
57
|
fi
|
61
58
|
```
|
62
59
|
|
63
|
-
## `
|
64
|
-
The `
|
60
|
+
## `git-tree-evars` Usage
|
61
|
+
The `git-tree-evars` command should be run on the target computer.
|
65
62
|
The command requires only one parameter:
|
66
63
|
an environment variable reference, pointing to the top-level directory to replicate.
|
67
64
|
The environment variable reference must be contained within single quotes to prevent expansion by the shell.
|
@@ -69,13 +66,13 @@ The environment variable reference must be contained within single quotes to pre
|
|
69
66
|
The following appends to any script in the `$work` directory called `.evars`.
|
70
67
|
The script defines environment variables that point to each git repos pointed to by `$work`:
|
71
68
|
```shell
|
72
|
-
$
|
69
|
+
$ git-tree-evars '$work' >> $work/.evars
|
73
70
|
```
|
74
71
|
|
75
72
|
|
76
|
-
### Generated Script from `
|
73
|
+
### Generated Script from `git-tree-evars`
|
77
74
|
Following is a sample of environment variable definitions.
|
78
|
-
You
|
75
|
+
You are expected to edit it to suit.
|
79
76
|
|
80
77
|
```shell
|
81
78
|
export work=/mnt/c/work
|
@@ -106,12 +103,16 @@ $ cd $my_project
|
|
106
103
|
|
107
104
|
|
108
105
|
## Installation
|
109
|
-
Type the following at a shell prompt:
|
106
|
+
Type the following at a shell prompt on the machine you are copying the git tree from, and on the machine that you are copying the git tree to:
|
107
|
+
|
108
|
+
```shell
|
109
|
+
$ yes | sudo apt install cmake libgit2-dev libssh2-1-dev pkg-config
|
110
110
|
|
111
|
-
```ruby
|
112
111
|
$ gem install git_tree
|
113
112
|
```
|
114
113
|
|
114
|
+
To register the new commands, either log out and log back in, or open a new console.
|
115
|
+
|
115
116
|
|
116
117
|
## Additional Information
|
117
118
|
More information is available on
|
data/Rakefile
CHANGED
data/git_tree.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
|
|
13
13
|
The git_tree_evars command writes a script that defines environment variables pointing to git repos.
|
14
14
|
END_OF_DESC
|
15
15
|
spec.email = ['mslinn@mslinn.com']
|
16
|
-
spec.executables = %w[
|
16
|
+
spec.executables = %w[git-tree-evars git-tree-replicate]
|
17
17
|
spec.files = Dir[
|
18
18
|
'{bindir,lib}/**/*',
|
19
19
|
'.rubocop.yml',
|
@@ -41,5 +41,6 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
|
|
41
41
|
spec.summary = 'Installs two commands that scan a git directory tree and write out scripts for replication.'
|
42
42
|
spec.version = GitUrlsVersion::VERSION
|
43
43
|
|
44
|
+
spec.add_dependency 'rainbow'
|
44
45
|
spec.add_dependency 'rugged'
|
45
46
|
end
|
data/lib/git_tree/version.rb
CHANGED
data/lib/git_tree.rb
CHANGED
@@ -1,38 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
# @param root might be "$envar" or a fully qualified directory name ("/a/b/c")
|
7
|
-
def self.command_evars(root = ARGV[0])
|
8
|
-
abort "Error: Argument must start with a dollar sign ($)" unless root.start_with? '$'
|
9
|
-
|
10
|
-
base = MslinnUtil.expand_env root
|
11
|
-
dirs = directories_to_process base
|
12
|
-
|
13
|
-
# puts "# root=#{root}, base=#{base}"
|
14
|
-
puts make_env_vars root, base, dirs
|
15
|
-
end
|
16
|
-
|
17
|
-
# @param root might be "$envar" or a fully qualified directory name ("/a/b/c")
|
18
|
-
def self.command_replicate(root = ARGV[0])
|
19
|
-
abort "Error: Argument must start with a dollar sign ($)" unless root.start_with? '$'
|
1
|
+
require 'find'
|
2
|
+
require 'rainbow/refinement'
|
3
|
+
require 'rugged'
|
4
|
+
require_relative 'util'
|
20
5
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
# puts "# root=#{root}, base=#{base}"
|
25
|
-
puts make_replicate_script root, base, dirs
|
26
|
-
end
|
6
|
+
module GitTree
|
7
|
+
using Rainbow
|
27
8
|
|
28
9
|
# @return array containing directory names to process
|
29
10
|
# Each directory name ends with a slash, to ensure symlinks are dereferences
|
30
11
|
def self.directories_to_process(root)
|
31
12
|
root_fq = File.expand_path root
|
32
|
-
abort "Error: #{root_fq} is a file, instead of a directory. Cannot recurse." if File.file? root_fq
|
13
|
+
abort "Error: #{root_fq} is a file, instead of a directory. Cannot recurse.".red if File.file? root_fq
|
33
14
|
|
34
15
|
root_fq = MslinnUtil.deref_symlink(root_fq).to_s
|
35
|
-
abort "Error: #{root_fq} does not exist. Halting." unless Dir.exist? root_fq
|
16
|
+
abort "Error: #{root_fq} does not exist. Halting.".red unless Dir.exist? root_fq
|
36
17
|
|
37
18
|
result = []
|
38
19
|
Find.find(root_fq) do |path|
|
@@ -47,78 +28,4 @@ module GitTree
|
|
47
28
|
end
|
48
29
|
result.map { |x| x.delete_prefix("#{root_fq}/") }
|
49
30
|
end
|
50
|
-
|
51
|
-
def self.env_var_name(path)
|
52
|
-
name = path.include?('/') ? File.basename(path) : path
|
53
|
-
name.tr(' ', '_').tr('-', '_')
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.help(msg = nil)
|
57
|
-
puts msg if msg
|
58
|
-
puts <<~END_HELP
|
59
|
-
Replicates tree of git repos and writes a bash script to STDOUT that clones the repos in the tree.
|
60
|
-
Adds upstream remotes as required.
|
61
|
-
|
62
|
-
Directories containing a file called .ignore are ignored.
|
63
|
-
END_HELP
|
64
|
-
exit 1
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.make_env_var(name, value)
|
68
|
-
"export #{env_var_name(name)}=#{value}"
|
69
|
-
end
|
70
|
-
|
71
|
-
# @param root should be an "$envar" that points to the root of a directory tree containing git repos.
|
72
|
-
# @param base a fully qualified directory name ("/a/b/c")
|
73
|
-
# @param dirs directory list to process
|
74
|
-
def self.make_env_vars(root, base, dirs)
|
75
|
-
result = []
|
76
|
-
result << make_env_var(env_var_name(base), MslinnUtil.deref_symlink(base))
|
77
|
-
dirs.each do |dir|
|
78
|
-
result << make_env_var(env_var_name(dir), "#{root}/#{dir}")
|
79
|
-
end
|
80
|
-
result.join("\n") + "\n" # rubocop:disable Style/StringConcatenation
|
81
|
-
end
|
82
|
-
|
83
|
-
# @param root should be an "$envar" that points to the root of a directory tree containing git repos.
|
84
|
-
# @param base a fully qualified directory name ("/a/b/c")
|
85
|
-
# @param dirs directory list to process
|
86
|
-
def self.make_replicate_script(root, base, dirs)
|
87
|
-
help "Error: Please specify the subdirectory to traverse.\n\n" if root.to_s.empty?
|
88
|
-
|
89
|
-
Dir.chdir(base) do
|
90
|
-
result = dirs.map { |dir| replicate_one(dir) }
|
91
|
-
result.join "\n"
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def self.replicate_one(dir)
|
96
|
-
output = []
|
97
|
-
project_dir = File.basename dir
|
98
|
-
parent_dir = File.dirname dir
|
99
|
-
repo = Rugged::Repository.new dir
|
100
|
-
origin_url = repo.config['remote.origin.url']
|
101
|
-
|
102
|
-
output << "if [ ! -d \"#{dir}/.git\" ]; then"
|
103
|
-
output << " mkdir -p '#{parent_dir}'"
|
104
|
-
output << " pushd '#{parent_dir}' > /dev/null"
|
105
|
-
output << " git clone #{origin_url}"
|
106
|
-
|
107
|
-
repo.remotes.each do |remote|
|
108
|
-
next if remote.name == 'origin' || remote.url == 'no_push'
|
109
|
-
|
110
|
-
output << " git remote add #{remote.name} '#{remote.url}'"
|
111
|
-
end
|
112
|
-
|
113
|
-
output << ' popd > /dev/null'
|
114
|
-
|
115
|
-
# git_dir_name = File.basename Dir.pwd
|
116
|
-
# if git_dir_name != project_dir
|
117
|
-
# output << ' # Git project directory was renamed, renaming this copy to match original directory structure'
|
118
|
-
# output << " mv #{git_dir_name} #{project_dir}"
|
119
|
-
# end
|
120
|
-
output << "fi"
|
121
|
-
output << ''
|
122
|
-
output
|
123
|
-
end
|
124
31
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative 'git_tree'
|
2
|
+
|
3
|
+
module GitTree
|
4
|
+
using Rainbow
|
5
|
+
|
6
|
+
# @param root might be "$envar" or a fully qualified directory name ("/a/b/c")
|
7
|
+
def self.command_evars(root = ARGV[0])
|
8
|
+
help_evars "Environment variable reference was missing. Please enclose it within single quotes." if root.to_s.empty?
|
9
|
+
help_evars "Environment variable reference must start with a dollar sign ($)." unless root.start_with? '$'
|
10
|
+
|
11
|
+
base = MslinnUtil.expand_env root
|
12
|
+
help_evars "Environment variable '#{root}' is undefined." if base.strip.empty?
|
13
|
+
help_evars "Environment variable '#{root}' points to a non-existant directory (#{base})." unless File.exist?(base)
|
14
|
+
help_evars "Environment variable '#{root}' points to a file (#{base}), not a directory." unless Dir.exist?(base)
|
15
|
+
|
16
|
+
dirs = directories_to_process base
|
17
|
+
puts make_env_vars(root, base, dirs)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.env_var_name(path)
|
21
|
+
name = path.include?('/') ? File.basename(path) : path
|
22
|
+
name.tr(' ', '_').tr('-', '_')
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.help_evars(msg = nil)
|
26
|
+
prog_name = File.basename $PROGRAM_NAME
|
27
|
+
puts "Error: #{msg}\n".red if msg
|
28
|
+
puts <<~END_HELP
|
29
|
+
#{prog_name} - Examines a tree of git repositories and writes a bash script to STDOUT
|
30
|
+
that defines environment variables which point to the repositories in the tree.
|
31
|
+
|
32
|
+
Does not redefine existing environment variables; messages are written to
|
33
|
+
STDERR to indicate environment variables that are not redefined.
|
34
|
+
|
35
|
+
The environment variable must have been exported, for example:
|
36
|
+
|
37
|
+
$ export work=$HOME/work
|
38
|
+
|
39
|
+
Directories containing a file called .ignore are ignored.
|
40
|
+
|
41
|
+
Usage example:
|
42
|
+
|
43
|
+
$ #{prog_name} '$work'
|
44
|
+
|
45
|
+
The name of the environment variable must be preceded by a dollar sign and enclosed within single quotes.
|
46
|
+
END_HELP
|
47
|
+
exit 1
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.make_env_var(name, value)
|
51
|
+
"export #{env_var_name(name)}=#{value}"
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param root should be an "$envar" that points to the root of a directory tree containing git repos.
|
55
|
+
# @param base a fully qualified directory name ("/a/b/c")
|
56
|
+
# @param dirs directory list to process
|
57
|
+
def self.make_env_vars(root, base, dirs)
|
58
|
+
help_evars "Error: Please specify the subdirectory to traverse." if root.to_s.empty?
|
59
|
+
|
60
|
+
result = []
|
61
|
+
result << make_env_var(env_var_name(base), MslinnUtil.deref_symlink(base))
|
62
|
+
dirs.each do |dir|
|
63
|
+
ename = env_var_name dir
|
64
|
+
ename_value = MslinnUtil.expand_env "$#{ename}"
|
65
|
+
ename_value = ename_value.gsub(' ', '\\ ').delete_prefix('\\') unless ename_value.empty?
|
66
|
+
if ename_value.to_s.strip.empty?
|
67
|
+
result << make_env_var(ename, "#{root}/#{dir}")
|
68
|
+
else
|
69
|
+
msg = "$#{ename} was previously defined as #{ename_value}"
|
70
|
+
dir2 = MslinnUtil.expand_env(ename_value)
|
71
|
+
if Dir.exist? dir2
|
72
|
+
warn msg.cyan
|
73
|
+
else
|
74
|
+
msg += ", but that directory does not exist,\n so redefining #{ename} as #{dir}."
|
75
|
+
warn msg.green
|
76
|
+
result << make_env_var(ename, "#{root}/#{dir}")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
result.map { |x| "#{x}\n" }.join + "\n"
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require_relative 'git_tree'
|
2
|
+
|
3
|
+
module GitTree
|
4
|
+
using Rainbow
|
5
|
+
|
6
|
+
# @param root might be "$envar" or a fully qualified directory name ("/a/b/c")
|
7
|
+
def self.command_replicate(root = ARGV[0])
|
8
|
+
help_replicate "Environment variable reference was missing. Please enclose it within single quotes." if root.to_s.empty?
|
9
|
+
help_replicate "Error: Environment variable reference must start with a dollar sign ($)" unless root.start_with? '$'
|
10
|
+
|
11
|
+
base = MslinnUtil.expand_env root
|
12
|
+
help_replicate "Environment variable '#{root}' is undefined." if base.strip.empty?
|
13
|
+
help_replicate "Environment variable '#{root}' points to a non-existant directory (#{base})." unless File.exist?(base)
|
14
|
+
help_replicate "Environment variable '#{root}' points to a file (#{base}), not a directory." unless Dir.exist?(base)
|
15
|
+
|
16
|
+
dirs = directories_to_process base
|
17
|
+
puts make_replicate_script(root, base, dirs)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.help_replicate(msg = nil)
|
21
|
+
prog_name = File.basename $PROGRAM_NAME
|
22
|
+
puts "Error: #{msg}\n".red if msg
|
23
|
+
puts <<~END_HELP
|
24
|
+
#{prog_name} - Replicates a tree of git repositories and writes a bash script
|
25
|
+
to STDOUT that clones the repositories in the tree. Replicates any remotes
|
26
|
+
defined in the source repositories to the target repositories.
|
27
|
+
|
28
|
+
The environment variable must have been exported, for example:
|
29
|
+
|
30
|
+
$ export work=$HOME/work
|
31
|
+
|
32
|
+
Directories containing a file called .ignore are ignored.
|
33
|
+
|
34
|
+
Usage example:
|
35
|
+
Assuming that 'work' is an environment variable that contains the name of a
|
36
|
+
directory that contains a tree of git repositories:
|
37
|
+
|
38
|
+
$ #{prog_name} '$work'
|
39
|
+
|
40
|
+
The name of the environment variable must be preceded by a dollar sign and enclosed within single quotes.
|
41
|
+
END_HELP
|
42
|
+
exit 1
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param root should be an "$envar" that points to the root of a directory tree containing git repos.
|
46
|
+
# @param base a fully qualified directory name ("/a/b/c")
|
47
|
+
# @param dirs directory list to process
|
48
|
+
def self.make_replicate_script(root, base, dirs)
|
49
|
+
help_replicate "Error: Please specify the subdirectory to traverse.\n\n" if root.to_s.empty?
|
50
|
+
|
51
|
+
Dir.chdir(base) do
|
52
|
+
result = dirs.map { |dir| replicate_one(dir) }
|
53
|
+
result.join "\n"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.replicate_one(dir)
|
58
|
+
output = []
|
59
|
+
# project_dir = File.basename dir
|
60
|
+
parent_dir = File.dirname dir
|
61
|
+
repo = Rugged::Repository.new dir
|
62
|
+
origin_url = repo.config['remote.origin.url']
|
63
|
+
|
64
|
+
output << "if [ ! -d \"#{dir}/.git\" ]; then"
|
65
|
+
output << " mkdir -p '#{parent_dir}'"
|
66
|
+
output << " pushd '#{parent_dir}' > /dev/null"
|
67
|
+
output << " git clone #{origin_url}"
|
68
|
+
|
69
|
+
repo.remotes.each do |remote|
|
70
|
+
next if remote.name == 'origin' || remote.url == 'no_push'
|
71
|
+
|
72
|
+
output << " git remote add #{remote.name} '#{remote.url}'"
|
73
|
+
end
|
74
|
+
|
75
|
+
output << ' popd > /dev/null'
|
76
|
+
|
77
|
+
# git_dir_name = File.basename Dir.pwd
|
78
|
+
# if git_dir_name != project_dir
|
79
|
+
# output << ' # Git project directory was renamed, renaming this copy to match original directory structure'
|
80
|
+
# output << " mv #{git_dir_name} #{project_dir}"
|
81
|
+
# end
|
82
|
+
output << 'fi'
|
83
|
+
output << ''
|
84
|
+
output
|
85
|
+
end
|
86
|
+
end
|
data/lib/util.rb
CHANGED
@@ -1,4 +1,66 @@
|
|
1
1
|
module MslinnUtil
|
2
|
+
# @param paths [Array[String]] all start with a leading '/' (they are assumed to be absolute paths).
|
3
|
+
# @return [String] the longest path prefix that is a prefix of all paths in array.
|
4
|
+
# If array is empty, return ''.
|
5
|
+
# If only the leading slash matches, and allow_root_match is true, return '/', else return ''.
|
6
|
+
def self.common_prefix(paths, allow_root_match: false)
|
7
|
+
return '' if paths.empty?
|
8
|
+
|
9
|
+
relative_paths = paths.reject { |x| x.start_with? '/' }
|
10
|
+
abort "Error: common_prefix received relative paths:" + relative_paths.map { |x| " #{x}\n" } \
|
11
|
+
unless relative_paths.empty?
|
12
|
+
|
13
|
+
if paths.length == 1
|
14
|
+
result = paths.first.split('/').slice(0...-1).join('/')
|
15
|
+
return result.empty? && allow_root_match ? '/' : result
|
16
|
+
end
|
17
|
+
|
18
|
+
arr = paths.sort
|
19
|
+
first = arr.first.split('/')
|
20
|
+
last = arr.last.split('/')
|
21
|
+
i = 0
|
22
|
+
i += 1 while first[i] == last[i] && i <= first.length
|
23
|
+
result = first.slice(0, i).join('/')
|
24
|
+
|
25
|
+
result.empty? && allow_root_match ? '/' : result
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param paths [Array[String]] absolute paths to examine
|
29
|
+
# @param level [Int] minimum # of leading directory names in result, origin 1
|
30
|
+
def self.roots(paths, level, allow_root_match: false)
|
31
|
+
abort "Error: level must be positive, but it is #{level}." unless level.positive?
|
32
|
+
return allow_root_match ? '/' : '' if paths.empty?
|
33
|
+
|
34
|
+
abort("Error: level parameter must be positive, #{level} was supplied instead.") if level <= 0
|
35
|
+
|
36
|
+
if paths.length == 1
|
37
|
+
root = File.dirname(paths.first)
|
38
|
+
return allow_root_match ? '/' : '' if root == '/'
|
39
|
+
|
40
|
+
return root
|
41
|
+
end
|
42
|
+
|
43
|
+
loop do
|
44
|
+
paths = trim_to_level(paths, level) # does this change paths in the caller?
|
45
|
+
return paths.first if paths.length == 1
|
46
|
+
|
47
|
+
level -= 1
|
48
|
+
break if level.zero?
|
49
|
+
end
|
50
|
+
|
51
|
+
allow_root_match ? '/' : ''
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param paths [Array[String]] absolute paths to examine
|
55
|
+
# @param level is origin 1
|
56
|
+
def self.trim_to_level(paths, level)
|
57
|
+
result = paths.map do |x|
|
58
|
+
elements = x.split('/').reject(&:empty?)
|
59
|
+
'/' + elements[0..level - 1].join('/')
|
60
|
+
end
|
61
|
+
result.sort.uniq
|
62
|
+
end
|
63
|
+
|
2
64
|
# @return Path to symlink
|
3
65
|
def self.deref_symlink(symlink)
|
4
66
|
require 'pathname'
|
@@ -15,5 +77,4 @@ module MslinnUtil
|
|
15
77
|
ENV.fetch(Regexp.last_match(1), nil)
|
16
78
|
end
|
17
79
|
end
|
18
|
-
|
19
80
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: git_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Slinn
|
8
8
|
autorequire:
|
9
9
|
bindir: bindir
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rainbow
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rugged
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -33,8 +47,8 @@ description: |
|
|
33
47
|
email:
|
34
48
|
- mslinn@mslinn.com
|
35
49
|
executables:
|
36
|
-
-
|
37
|
-
-
|
50
|
+
- git-tree-evars
|
51
|
+
- git-tree-replicate
|
38
52
|
extensions: []
|
39
53
|
extra_rdoc_files: []
|
40
54
|
files:
|
@@ -43,11 +57,13 @@ files:
|
|
43
57
|
- LICENSE.txt
|
44
58
|
- README.md
|
45
59
|
- Rakefile
|
46
|
-
- bindir/
|
47
|
-
- bindir/
|
60
|
+
- bindir/git-tree-evars
|
61
|
+
- bindir/git-tree-replicate
|
48
62
|
- git_tree.gemspec
|
49
63
|
- lib/git_tree.rb
|
50
64
|
- lib/git_tree/version.rb
|
65
|
+
- lib/git_tree_evars.rb
|
66
|
+
- lib/git_tree_replicate.rb
|
51
67
|
- lib/util.rb
|
52
68
|
homepage: https://www.mslinn.com/git/1100-git-tree.html
|
53
69
|
licenses:
|