Class: Gitrepox::Gitrepoxgroup

Inherits:
Object
  • Object
show all
Defined in:
lib/gitrepox/gitrepoxgroup.rb

Instance Method Summary collapse

Constructor Details

#initialize(home_pn, log_filename, yaml_fname_or_start_path_array) ⇒ Gitrepoxgroup

Returns a new instance of Gitrepoxgroup.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gitrepox/gitrepoxgroup.rb', line 3

def initialize(home_pn, log_filename, yaml_fname_or_start_path_array)
  @home_pn = home_pn
  @log_filename = log_filename
  @obj = []
  # p "yaml_fname_or_start_path_array=#{yaml_fname_or_start_path_array}"
  yaml_fname_or_start_path_array.each do |yaml_fname_or_start_path|
    pn = Pathname.new(yaml_fname_or_start_path)
    # p "initialize pn=#{pn}"
    if pn.file?
      make_parent_pn_info_from_yaml_file(pn)
      # @content = File.readlines(pn).map{ |line| line.chomp }.join("\n")
    else
      # @obj.concat(find_by_popen3(pn))
      # p "initialize pn=#{pn}"
      @obj += find_git(pn)
      # p "init @obj=#{@obj}"
    end
  end
end

Instance Method Details

#find_by_popen3(start_path) ⇒ Object



60
61
62
63
# File 'lib/gitrepox/gitrepoxgroup.rb', line 60

def find_by_popen3(start_path)
  _, stdout, = *Open3.popen3("find #{start_path} -name .git ")
  listup(stdout)
end

#find_git(start_pn) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gitrepox/gitrepoxgroup.rb', line 42

def find_git(start_pn)
  # p "find_git start_pn=#{start_pn}"
  dir_pns = []
  git_pns = []
  start_pn.children.each do |item|
    name = item.basename.to_s
    git_pns << item if name =~ /^\.git$/
    dir_pns << item if item.directory?
  end
  if git_pns.empty?
    dir_pns.each do |dir_pn|
      git_pns += find_git(dir_pn)
    end
  end
  # p "find_git git_pns=#{git_pns}"
  git_pns
end

#get_and_uploadObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gitrepox/gitrepoxgroup.rb', line 110

def get_and_upload
  @obj.each do |hash|
    content = gitrepox_repo(@home_pn, hash)
    next unless content

    count = Util.get_count
    filename = [hash['filename_base'], count, '.csv'].join('')
    @gsession ||= Gsession.new
    result = @gsession.upload_from_string(content, filename)
    Util.file_append(@log_filename, result) if result
  end
end

#get_git_repo_info(pn) ⇒ Object



181
182
183
184
185
186
# File 'lib/gitrepox/gitrepoxgroup.rb', line 181

def get_git_repo_info(pn)
  content = nil
  content_array = get_repo_info_list_in_csv(pn)
  content = content_array.join("\n") if content_array.size.positive?
  content
end

#get_repo_info_list_in_csv(pn) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gitrepox/gitrepoxgroup.rb', line 166

def get_repo_info_list_in_csv(pn)
  # ary = []
  _, ary = list_repo(pn).each_with_object([[0], []]) do |item, memo|
    # item.show
    head_line = memo[0]
    ary = memo[1]
    if (head_line[0]).zero?
      ary << item.to_csv_header
      head_line[0] = 1
    end
    ary << item.to_csv_data
  end
  ary
end

#gitrepox_repo(home_pn, value) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/gitrepox/gitrepoxgroup.rb', line 154

def gitrepox_repo(home_pn, value)
  pn = home_pn
  if value.instance_of?(Hash)
    value['paths'].each do |name|
      pn += name
    end
  else
    pn = value
  end
  get_git_repo_info(pn)
end

#list_repo(src_pn) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gitrepox/gitrepoxgroup.rb', line 123

def list_repo(src_pn)
  return [] if src_pn.file?

  parts = src_pn.children.map do |pn|
    [pn, "#{pn}.git"]
  end.partition do |pns|
    pns[1].exist?
  end
  parts[0].select do |pns|
    x = pns[0]
    g = Gitrepox.new(x)
    pns << g
    x.exist?
  end.map  do |pns|
    item = pns.first.basename
    full_path = pns.first.realpath
    g = pns.last
    remotes = g.get_remotes
    next unless remotes.size.positive?

    remotes.map do |gr|
      g.data[gr.name] = {}
      g.data[gr.name][:full_path] = full_path
      g.data[gr.name][:basename] = item
      g.data[gr.name][:url] = gr.url
      g.data[gr.name][:fetch_opts] = gr.fetch_opts
    end
    g
  end.select { |item| !item.nil? }
end

#listup(io) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gitrepox/gitrepoxgroup.rb', line 65

def listup(io)
  xhs = {}
  while (line = io.gets)
    line.chomp!
    pn = Pathname.new(line)
    # p pn
    pp_pn = pn.parent.parent
    xhs[pp_pn.to_s] = pp_pn
  end

  filename_base = 'gitx_hr_'
  xhs.keys.map  do |path|
    pn = xhs[path]
    hs = {}
    str = pn.relative_path_from(@home_pn).to_s
    hs['paths'] = str.split('/')
    hs['filename_base'] = filename_base
    @obj << hs
  end
  @obj
end

#listup_in_pathname_array(pn_array) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/gitrepox/gitrepoxgroup.rb', line 87

def listup_in_pathname_array(pn_array)
  xhs = {}
  pn_array.each do |pn|
    pp_pn = pn.parent.parent
    xhs[pp_pn.to_s] = pp_pn
  end

  make_parent_pn_info(xhs)
end

#make_parent_pn_info(xhs) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitrepox/gitrepoxgroup.rb', line 97

def make_parent_pn_info(xhs)
  filename_base = 'gitx_hr_'
  xhs.keys.map  do |path|
    pn = xhs[path]
    hs = {}
    str = pn.relative_path_from(@home_pn).to_s
    hs['paths'] = str.split('/')
    hs['filename_base'] = filename_base
    @obj << hs
  end
  @obj
end

#make_parent_pn_info_from_yaml_file(yaml_pn) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitrepox/gitrepoxgroup.rb', line 23

def make_parent_pn_info_from_yaml_file(yaml_pn)
  @content = File.read(yaml_pn)
  # p @content
  # obj = YAML.safe_load(@content, symbolize_names: true)
  # obj = YAML.safe_load(@content, aliases: true)
  obj = YAML.safe_load(@content, permitted_classes: [Symbol])
  obj.each_key do |filename_base|
    obj[filename_base].each do |hash|
      hs = {}
      path = File.join(hash['paths'])
      pn = Pathname.new(path)
      str = pn.relative_path_from(@home_pn).to_s
      hs['paths'] = str.split('/')
      hs['filename_base'] = filename_base
      @obj << hs
    end
  end
end