Rebuild site .gitmodules from valid template submodules during bundle

This commit is contained in:
rulingcom committed 2026-10-24 22:27:22 +08:00
1 parent 3c3dc6c8d6
commit a7415f2ca3
1 file changed
+49
+49
View File
@@ -19,6 +19,55 @@ if bundle_update_flag
#end
@original_dir = Dir.pwd
`cd "#{dir_pwd}" && git add app/templates/ app/assets built_in_extensions.rb config/mongoid.yml bin/ && cd "#{@original_dir}"` #using git to add modified files and to avoid git pull not working
#依規則重建站台的 .gitmodules(核心不追蹤此檔;放在上一行 git add 之後,避免帶 .git 的複製版型又被加回索引)
unless ENV['ORBIT_GITMODULES_REBUILT']
ENV['ORBIT_GITMODULES_REBUILT'] = '1'
begin
require 'open3'
if Dir.exist?(File.join(dir_pwd, '.git')) && Dir.exist?(template_path)
Dir.chdir(dir_pwd) do
candidates = {}
if File.exist?('.gitmodules')
entries = {}
Open3.capture3('git', 'config', '-f', '.gitmodules', '--get-regexp', '^submodule\.')[0].each_line do |line|
k, v = line.strip.split(' ', 2)
m = k.to_s.match(/\Asubmodule\.(.+)\.(path|url|branch)\z/)
(entries[m[1]] ||= {})[m[2]] = v.to_s if m
end
entries.each_value { |e| candidates[e['path']] = e if e['path'].to_s.start_with?('app/templates/') }
end
gitlinks = Open3.capture3('git', 'ls-files', '-s', '--', 'app/templates')[0].lines.map { |l| l.strip.split(/\s+/, 4) }.select { |l| l[0] == '160000' }.map { |l| l[3] }
gitlinks.each { |p| candidates[p] ||= {'path' => p} }
keep = {}
candidates.each do |path, e|
next unless gitlinks.include?(path) && File.exist?(File.join(path, 'template.json'))
url = e['url'].to_s
url = Open3.capture3('git', '-C', path, 'remote', 'get-url', 'origin')[0].strip if url.empty?
next if url.empty? || File.basename(path) != File.basename(url).sub(/\.git\z/, '')
remote_head, _, st = Open3.capture3({'GIT_TERMINAL_PROMPT' => '0'}, 'timeout', '20', 'git', 'ls-remote', '--symref', url, 'HEAD')
next unless st.success?
branch = Open3.capture3('git', '-C', path, 'rev-parse', '--abbrev-ref', 'HEAD')[0].strip
branch = e['branch'].to_s if branch.empty? || branch == 'HEAD'
branch = remote_head[%r{ref: refs/heads/(\S+)\s+HEAD}, 1].to_s if branch.empty?
keep[path] = {'url' => url, 'branch' => branch}
end
File.open('.gitmodules', 'w') do |f|
keep.each do |path, e|
f.puts "[submodule \"#{path}\"]"
f.puts "\tpath = #{path}"
f.puts "\turl = #{e['url']}"
f.puts "\tbranch = #{e['branch']}" unless e['branch'].empty?
end
end
system('git', 'rm', '-q', '--cached', '--ignore-unmatch', '--', '.gitmodules')
(gitlinks - keep.keys).each { |p| system('git', 'rm', '-q', '--cached', '--', p) }
end
puts "finish rebuilding .gitmodules in #{dir_pwd}"
end
rescue => e
puts "rebuild .gitmodules failed: #{e.inspect}"
end
end
#Add patchfile to Admin area
@patchfile_li = File.read(app_path+"/patchfile_li.txt").force_encoding('UTF-8')
@patchfile_li_choices = File.read(app_path+"/patchfile_li_choices.txt").force_encoding('UTF-8')