git URL转 http URL的ruby脚本

50 阅读1分钟

本地开发过程中公司的库都git前缀,如果想查看这个库的详情时,需要跳转到网页中,手动转换为http地址,比较麻烦; 安装这个小脚本可以后续可以一键转后,直接通过命令行跳转打开地址就可以了;

#!/usr/bin/env ruby

def convert_git_to_https(git_url)
  # 匹配 Git 地址中的用户名、仓库名和仓库路径
  match_data = git_url.match(/git@(.*?):(.*?)\/(.*).git/)
  if match_data
    host = match_data[1]
    username = match_data[2]
    repository = match_data[3]
    # 构造 HTTPS 地址
    https_url = "https://#{host}/#{username}/#{repository}"
    return https_url
  else
    puts "无法解析 Git 地址,请确认输入是否正确。"
    exit
  end
end

# 从命令行参数获取 Git 地址
git_url = ARGV[0]

# 如果没有提供 Git 地址,则打印提示信息并退出
unless git_url
  puts "请提供 Git 地址作为参数。"
  exit
end

# 调用方法将 Git 地址转换为 HTTPS 地址
https_url = convert_git_to_https(git_url)

# 输出 HTTPS 地址
puts "HTTPS 地址:#{https_url}"

转换效果:

image.png