|
|
module GitHelper
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
# 版本库目录空间
|
|
|
def repo_namespace(user_login, shixun_identifier)
|
|
|
"#{user_login}/#{shixun_identifier}.git"
|
|
|
end
|
|
|
|
|
|
# 版本库文件内容,带转码
|
|
|
def git_fle_content(repo_path, path)
|
|
|
begin
|
|
|
content = GitService.file_content(repo_path: repo_path, path: path)["content"]
|
|
|
logger.info("@@@@@@@@@@@@@@@@@@#{content}")
|
|
|
|
|
|
decode_content = nil
|
|
|
if content.present?
|
|
|
content = Base64.decode64(content)
|
|
|
cd = CharDet.detect(content)
|
|
|
logger.info "encoding: #{cd['encoding']} confidence: #{cd['confidence']}"
|
|
|
|
|
|
decode_content =
|
|
|
if cd["encoding"] == 'GB18030' && cd['confidence'] > 0.8
|
|
|
content.encode('UTF-8', 'GBK', {:invalid => :replace, :undef => :replace, :replace => ' '})
|
|
|
else
|
|
|
content.force_encoding('UTF-8')
|
|
|
end
|
|
|
end
|
|
|
|
|
|
decode_content
|
|
|
rescue Exception => e
|
|
|
uid_logger_error(e.message)
|
|
|
raise Educoder::TipException.new("文档内容获取异常")
|
|
|
end
|
|
|
end
|
|
|
|
|
|
# 更新文件代码
|
|
|
# content: 文件内容;message:提交描述
|
|
|
def update_file_content(content, repo_path, path, mail, username, message)
|
|
|
GitService.update_file(repo_path: repo_path, file_path: path, message: message,
|
|
|
content: content, author_name: username, author_email: mail)
|
|
|
end
|
|
|
|
|
|
# 版本库Fork功能
|
|
|
def project_fork(container, original_rep_path, username)
|
|
|
raise Educoder::TipException.new("fork源路径为空,fork失败!") if original_rep_path.blank?
|
|
|
# 将要生成的仓库名字
|
|
|
new_repo_name = "#{username}/#{container.try(:identifier)}#{ Time.now.strftime("%Y%m%d%H%M%S")}"
|
|
|
uid_logger("start fork container: repo_name is #{new_repo_name}")
|
|
|
GitService.fork_repository(repo_path: original_rep_path, fork_repository_path: (new_repo_name + ".git"))
|
|
|
container.update_attributes!(:repo_name => new_repo_name)
|
|
|
end
|
|
|
end |