展示代码仓库的镜像地址
添加代码仓库的镜像地址至 Git 项目的底部
git-mirror.ps1
· 3.0 KiB · PowerShell
原始檔案
#!/usr/bin/env pwsh
<#
.SYNOPSIS
展示代码仓库的镜像地址(支持横排/竖排)
.DESCRIPTION
File: git-mirror.ps1
URL: https://fx4.cn/gitmirror
Author: Jetsung Chan <[email protected]>
Version: 0.2.0
UpdatedAt: 2025-12-10
.EXAMPLE
./git-mirror.ps1 -Org "jetsung" -Repo "sh" -Horizontal
./git-mirror.ps1 -Org "myorg" -GitHub -GitCode -H
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, HelpMessage = "组织名")]
[Alias('o')]
[string]$Org,
[Parameter(HelpMessage = "仓库名(默认当前目录名)")]
[Alias('r')]
[string]$Repo,
[Alias('gh')][switch]$GitHub,
[Alias('ag')][switch]$GitCode,
[Alias('fg')][switch]$Framagit,
[Alias('cb')][switch]$Codeberg,
[Alias('cp')][switch]$Codeup,
# 新增:横排模式
[Alias('H')]
[switch]$Horizontal
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# 平台配置
$Platforms = @{
MYCODE = "https://git.jetsung.com"
FRAMAGIT = "https://framagit.org"
ATOMGIT = "https://atomgit.com"
GITHUB = "https://github.com"
CODEBERG = "https://codeberg.org"
CODEUP = "https://codeup.aliyun.com/jetsung"
}
function Main {
# 自动获取仓库名
if ([string]::IsNullOrWhiteSpace($Repo)) {
$Repo = Split-Path -Leaf -Path (Get-Location).Path
}
if ([string]::IsNullOrWhiteSpace($Repo)) {
Write-Error "Error: 无法确定仓库名"
exit 1
}
# 格式校验
if ($Org -notmatch '^[a-zA-Z0-9_-]+$' -or $Repo -notmatch '^[a-zA-Z0-9_-]+$') {
Write-Error "Error: org 和 repo 只能包含字母、数字、- 和 _"
exit 1
}
# 组织名映射(支持特殊映射)
$OrgMap = @{
MYCODE = $Org
FRAMAGIT = $Org
ATOMGIT = $Org
GITHUB = $Org
CODEBERG = $Org
CODEUP = $Org
}
switch ($Org) {
'idev' { $OrgMap.GITHUB = 'idevsig'; $OrgMap.ATOMGIT = 'idev' }
'tiny' { $OrgMap.GITHUB = 'tinyzen'; $OrgMap.ATOMGIT = 'tinyzen' }
}
# 收集所有要显示的链接
$links = [System.Collections.Generic.List[string]]::new()
$links.Add("[MyCode]($($Platforms.MYCODE)/$($OrgMap.MYCODE)/$Repo)")
if ($Framagit) { $links.Add("[Framagit]($($Platforms.FRAMAGIT)/$($OrgMap.FRAMAGIT)/$Repo)") }
if ($GitCode) { $links.Add("[AtomGit]($($Platforms.ATOMGIT)/$($OrgMap.ATOMGIT)/$Repo)") }
if ($GitHub) { $links.Add("[GitHub]($($Platforms.GITHUB)/$($OrgMap.GITHUB)/$Repo)") }
if ($Codeberg) { $links.Add("[Codeberg]($($Platforms.CODEBERG)/$($OrgMap.CODEBERG)/$Repo)") }
if ($Codeup) { $links.Add("[Codeup]($($Platforms.CODEUP)/$($OrgMap.CODEUP)/$Repo)") }
# 输出标题
Write-Host "`n## 仓库镜像`n"
if ($Horizontal) {
# 横排:完美居中的 ● 分隔
$links -join " ● "
Write-Host "`n"
}
else {
# 竖排(默认)
foreach ($link in $links) {
Write-Output "- $link"
}
Write-Host ""
}
}
Main
| 1 | #!/usr/bin/env pwsh |
| 2 | <# |
| 3 | .SYNOPSIS |
| 4 | 展示代码仓库的镜像地址(支持横排/竖排) |
| 5 | .DESCRIPTION |
| 6 | File: git-mirror.ps1 |
| 7 | URL: https://fx4.cn/gitmirror |
| 8 | Author: Jetsung Chan <i@jetsung.com> |
| 9 | Version: 0.2.0 |
| 10 | UpdatedAt: 2025-12-10 |
| 11 | .EXAMPLE |
| 12 | ./git-mirror.ps1 -Org "jetsung" -Repo "sh" -Horizontal |
| 13 | ./git-mirror.ps1 -Org "myorg" -GitHub -GitCode -H |
| 14 | #> |
| 15 | |
| 16 | [CmdletBinding()] |
| 17 | param( |
| 18 | [Parameter(Mandatory = $true, HelpMessage = "组织名")] |
| 19 | [Alias('o')] |
| 20 | [string]$Org, |
| 21 | |
| 22 | [Parameter(HelpMessage = "仓库名(默认当前目录名)")] |
| 23 | [Alias('r')] |
| 24 | [string]$Repo, |
| 25 | |
| 26 | [Alias('gh')][switch]$GitHub, |
| 27 | [Alias('ag')][switch]$GitCode, |
| 28 | [Alias('fg')][switch]$Framagit, |
| 29 | [Alias('cb')][switch]$Codeberg, |
| 30 | [Alias('cp')][switch]$Codeup, |
| 31 | |
| 32 | # 新增:横排模式 |
| 33 | [Alias('H')] |
| 34 | [switch]$Horizontal |
| 35 | ) |
| 36 | |
| 37 | Set-StrictMode -Version Latest |
| 38 | $ErrorActionPreference = 'Stop' |
| 39 | |
| 40 | # 平台配置 |
| 41 | $Platforms = @{ |
| 42 | MYCODE = "https://git.jetsung.com" |
| 43 | FRAMAGIT = "https://framagit.org" |
| 44 | ATOMGIT = "https://atomgit.com" |
| 45 | GITHUB = "https://github.com" |
| 46 | CODEBERG = "https://codeberg.org" |
| 47 | CODEUP = "https://codeup.aliyun.com/jetsung" |
| 48 | } |
| 49 | |
| 50 | function Main { |
| 51 | # 自动获取仓库名 |
| 52 | if ([string]::IsNullOrWhiteSpace($Repo)) { |
| 53 | $Repo = Split-Path -Leaf -Path (Get-Location).Path |
| 54 | } |
| 55 | if ([string]::IsNullOrWhiteSpace($Repo)) { |
| 56 | Write-Error "Error: 无法确定仓库名" |
| 57 | exit 1 |
| 58 | } |
| 59 | |
| 60 | # 格式校验 |
| 61 | if ($Org -notmatch '^[a-zA-Z0-9_-]+$' -or $Repo -notmatch '^[a-zA-Z0-9_-]+$') { |
| 62 | Write-Error "Error: org 和 repo 只能包含字母、数字、- 和 _" |
| 63 | exit 1 |
| 64 | } |
| 65 | |
| 66 | # 组织名映射(支持特殊映射) |
| 67 | $OrgMap = @{ |
| 68 | MYCODE = $Org |
| 69 | FRAMAGIT = $Org |
| 70 | ATOMGIT = $Org |
| 71 | GITHUB = $Org |
| 72 | CODEBERG = $Org |
| 73 | CODEUP = $Org |
| 74 | } |
| 75 | |
| 76 | switch ($Org) { |
| 77 | 'idev' { $OrgMap.GITHUB = 'idevsig'; $OrgMap.ATOMGIT = 'idev' } |
| 78 | 'tiny' { $OrgMap.GITHUB = 'tinyzen'; $OrgMap.ATOMGIT = 'tinyzen' } |
| 79 | } |
| 80 | |
| 81 | # 收集所有要显示的链接 |
| 82 | $links = [System.Collections.Generic.List[string]]::new() |
| 83 | |
| 84 | $links.Add("[MyCode]($($Platforms.MYCODE)/$($OrgMap.MYCODE)/$Repo)") |
| 85 | |
| 86 | if ($Framagit) { $links.Add("[Framagit]($($Platforms.FRAMAGIT)/$($OrgMap.FRAMAGIT)/$Repo)") } |
| 87 | if ($GitCode) { $links.Add("[AtomGit]($($Platforms.ATOMGIT)/$($OrgMap.ATOMGIT)/$Repo)") } |
| 88 | if ($GitHub) { $links.Add("[GitHub]($($Platforms.GITHUB)/$($OrgMap.GITHUB)/$Repo)") } |
| 89 | if ($Codeberg) { $links.Add("[Codeberg]($($Platforms.CODEBERG)/$($OrgMap.CODEBERG)/$Repo)") } |
| 90 | if ($Codeup) { $links.Add("[Codeup]($($Platforms.CODEUP)/$($OrgMap.CODEUP)/$Repo)") } |
| 91 | |
| 92 | # 输出标题 |
| 93 | Write-Host "`n## 仓库镜像`n" |
| 94 | |
| 95 | if ($Horizontal) { |
| 96 | # 横排:完美居中的 ● 分隔 |
| 97 | $links -join " ● " |
| 98 | Write-Host "`n" |
| 99 | } |
| 100 | else { |
| 101 | # 竖排(默认) |
| 102 | foreach ($link in $links) { |
| 103 | Write-Output "- $link" |
| 104 | } |
| 105 | Write-Host "" |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Main |
| 110 |
git-mirror.sh
· 4.2 KiB · Bash
原始檔案
#!/usr/bin/env bash
#============================================================
# File: git-mirror.sh
# Description: 展示代码仓库的镜像地址(支持完美横排)
# Author: Jetsung Chan <[email protected]>
# Version: 0.2.0
# UpdatedAt: 2025-12-10
#============================================================
[[ -n "${DEBUG:-}" ]] && set -eux || set -euo pipefail
# 平台 URL 配置
declare -A PLATFORMS=(
["MYCODE"]="https://git.jetsung.com"
["FRAMAGIT"]="https://framagit.org"
["ATOMGIT"]="https://atomgit.com"
["GITHUB"]="https://github.com"
["CODEBERG"]="https://codeberg.org"
["CODEUP"]="https://codeup.aliyun.com/jetsung"
)
parse_args() {
local org="" repo=""
local github="" atomgit="" framagit="" codeup="" codeberg="" horizontal=""
while [[ $# -gt 0 ]]; do
case "$1" in
-o|--org) org="${2:-}"; shift 2 ;;
-r|--repo) repo="${2:-}"; shift 2 ;;
-gh|--github) github="YES"; shift ;;
-ag|--atomgit) atomgit="YES"; shift ;;
-fg|--framagit) framagit="YES"; shift ;;
-cb|--codeberg) codeberg="YES"; shift ;;
-cp|--codeup) codeup="YES"; shift ;;
-H|--horizontal) horizontal="YES"; shift ;;
-h|--help)
cat <<'EOF'
Usage: gitmirror -o <org> [-r <repo>] [options]
Options:
-o, --org 组织名(必填)
-r, --repo 仓库名(默认当前目录)
-gh, --github 显示 GitHub
-ag, --atomgit 显示 AtomGit
-fg, --framagit 显示 Framagit
-cb, --codeberg 显示 Codeberg
-cp, --codeup 显示 Codeup
-H, --horizontal 横排显示(完美居中 ● 分隔)
-h, --help 显示帮助
EOF
exit 0
;;
*)
echo "未知选项: $1" >&2
echo "使用 --help 查看帮助" >&2
exit 1
;;
esac
done
ORG="$org"
REPO="$repo"
GITHUB="$github"
ATOMGIT="$atomgit"
FRAMAGIT="$framagit"
CODEUP="$codeup"
CODEBERG="$codeberg"
HORIZONTAL="$horizontal"
}
main() {
# 初始化全局变量
ORG="" REPO="" GITHUB="" ATOMGIT="" FRAMAGIT="" CODEUP="" CODEBERG="" HORIZONTAL=""
parse_args "$@"
# 必填校验
[[ -z "$ORG" ]] && { echo "Error: 缺少 -o/--org 参数" >&2; exit 1; }
# REPO 默认当前目录名
[[ -z "$REPO" ]] && REPO=$(basename "$PWD")
[[ -z "$REPO" ]] && { echo "Error: 无法确定不了仓库名" >&2; exit 1; }
# 简单格式校验
if ! [[ "$ORG" =~ ^[a-zA-Z0-9_-]+$ && "$REPO" =~ ^[a-zA-Z0-9_-]+$ ]]; then
echo "Error: org 和 repo 只能包含字母、数字、- 和 _" >&2
exit 1
fi
# 特殊组织映射
ORG_FRAMAGIT="$ORG"
ORG_CODEUP="$ORG"
ORG_ATOMGIT="$ORG"
ORG_GITHUB="$ORG"
ORG_CODEBERG="$ORG"
case "$ORG" in
idev) ORG_GITHUB="idevsig"; ORG_ATOMGIT="idev" ;;
tiny) ORG_GITHUB="tinyzen"; ORG_ATOMGIT="tinyzen" ;;
esac
echo
echo "## 仓库镜像"
echo
# 收集所有要显示的链接
local links=()
links+=("[MyCode](${PLATFORMS[MYCODE]}/${ORG}/${REPO})")
[[ -n "$FRAMAGIT" ]] && links+=("[Framagit](${PLATFORMS[FRAMAGIT]}/${ORG_FRAMAGIT}/${REPO})")
[[ -n "$ATOMGIT" ]] && links+=("[AtomGit](${PLATFORMS[ATOMGIT]}/${ORG_ATOMGIT}/${REPO})")
[[ -n "$GITHUB" ]] && links+=("[GitHub](${PLATFORMS[GITHUB]}/${ORG_GITHUB}/${REPO})")
[[ -n "$CODEBERG" ]] && links+=("[Codeberg](${PLATFORMS[CODEBERG]}/${ORG_CODEBERG}/${REPO})")
[[ -n "$CODEUP" ]] && links+=("[Codeup](${PLATFORMS[CODEUP]}/${ORG_CODEUP}/${REPO})")
if [[ "$HORIZONTAL" == "YES" ]]; then
# 完美横排:每个链接之间用「 ● 」分隔(左右各一个空格)
local first=1
for link in "${links[@]}"; do
if (( first )); then
printf "%s" "$link"
first=0
else
printf " ● %s" "$link"
fi
done
echo
echo
# 空一行,保持和竖排一致
else
# 竖排(原样)
for link in "${links[@]}"; do
echo "- $link"
done
echo
fi
}
main "$@"
| 1 | #!/usr/bin/env bash |
| 2 | #============================================================ |
| 3 | # File: git-mirror.sh |
| 4 | # Description: 展示代码仓库的镜像地址(支持完美横排) |
| 5 | # Author: Jetsung Chan <[email protected]> |
| 6 | # Version: 0.2.0 |
| 7 | # UpdatedAt: 2025-12-10 |
| 8 | #============================================================ |
| 9 | |
| 10 | [[ -n "${DEBUG:-}" ]] && set -eux || set -euo pipefail |
| 11 | |
| 12 | # 平台 URL 配置 |
| 13 | declare -A PLATFORMS=( |
| 14 | ["MYCODE"]="https://git.jetsung.com" |
| 15 | ["FRAMAGIT"]="https://framagit.org" |
| 16 | ["ATOMGIT"]="https://atomgit.com" |
| 17 | ["GITHUB"]="https://github.com" |
| 18 | ["CODEBERG"]="https://codeberg.org" |
| 19 | ["CODEUP"]="https://codeup.aliyun.com/jetsung" |
| 20 | ) |
| 21 | |
| 22 | parse_args() { |
| 23 | local org="" repo="" |
| 24 | local github="" atomgit="" framagit="" codeup="" codeberg="" horizontal="" |
| 25 | |
| 26 | while [[ $# -gt 0 ]]; do |
| 27 | case "$1" in |
| 28 | -o|--org) org="${2:-}"; shift 2 ;; |
| 29 | -r|--repo) repo="${2:-}"; shift 2 ;; |
| 30 | -gh|--github) github="YES"; shift ;; |
| 31 | -ag|--atomgit) atomgit="YES"; shift ;; |
| 32 | -fg|--framagit) framagit="YES"; shift ;; |
| 33 | -cb|--codeberg) codeberg="YES"; shift ;; |
| 34 | -cp|--codeup) codeup="YES"; shift ;; |
| 35 | -H|--horizontal) horizontal="YES"; shift ;; |
| 36 | -h|--help) |
| 37 | cat <<'EOF' |
| 38 | Usage: gitmirror -o <org> [-r <repo>] [options] |
| 39 | |
| 40 | Options: |
| 41 | -o, --org 组织名(必填) |
| 42 | -r, --repo 仓库名(默认当前目录) |
| 43 | -gh, --github 显示 GitHub |
| 44 | -ag, --atomgit 显示 AtomGit |
| 45 | -fg, --framagit 显示 Framagit |
| 46 | -cb, --codeberg 显示 Codeberg |
| 47 | -cp, --codeup 显示 Codeup |
| 48 | -H, --horizontal 横排显示(完美居中 ● 分隔) |
| 49 | -h, --help 显示帮助 |
| 50 | EOF |
| 51 | exit 0 |
| 52 | ;; |
| 53 | *) |
| 54 | echo "未知选项: $1" >&2 |
| 55 | echo "使用 --help 查看帮助" >&2 |
| 56 | exit 1 |
| 57 | ;; |
| 58 | esac |
| 59 | done |
| 60 | |
| 61 | ORG="$org" |
| 62 | REPO="$repo" |
| 63 | GITHUB="$github" |
| 64 | ATOMGIT="$atomgit" |
| 65 | FRAMAGIT="$framagit" |
| 66 | CODEUP="$codeup" |
| 67 | CODEBERG="$codeberg" |
| 68 | HORIZONTAL="$horizontal" |
| 69 | } |
| 70 | |
| 71 | main() { |
| 72 | # 初始化全局变量 |
| 73 | ORG="" REPO="" GITHUB="" ATOMGIT="" FRAMAGIT="" CODEUP="" CODEBERG="" HORIZONTAL="" |
| 74 | |
| 75 | parse_args "$@" |
| 76 | |
| 77 | # 必填校验 |
| 78 | [[ -z "$ORG" ]] && { echo "Error: 缺少 -o/--org 参数" >&2; exit 1; } |
| 79 | |
| 80 | # REPO 默认当前目录名 |
| 81 | [[ -z "$REPO" ]] && REPO=$(basename "$PWD") |
| 82 | [[ -z "$REPO" ]] && { echo "Error: 无法确定不了仓库名" >&2; exit 1; } |
| 83 | |
| 84 | # 简单格式校验 |
| 85 | if ! [[ "$ORG" =~ ^[a-zA-Z0-9_-]+$ && "$REPO" =~ ^[a-zA-Z0-9_-]+$ ]]; then |
| 86 | echo "Error: org 和 repo 只能包含字母、数字、- 和 _" >&2 |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | # 特殊组织映射 |
| 91 | ORG_FRAMAGIT="$ORG" |
| 92 | ORG_CODEUP="$ORG" |
| 93 | ORG_ATOMGIT="$ORG" |
| 94 | ORG_GITHUB="$ORG" |
| 95 | ORG_CODEBERG="$ORG" |
| 96 | case "$ORG" in |
| 97 | idev) ORG_GITHUB="idevsig"; ORG_ATOMGIT="idev" ;; |
| 98 | tiny) ORG_GITHUB="tinyzen"; ORG_ATOMGIT="tinyzen" ;; |
| 99 | esac |
| 100 | |
| 101 | echo |
| 102 | echo "## 仓库镜像" |
| 103 | echo |
| 104 | |
| 105 | # 收集所有要显示的链接 |
| 106 | local links=() |
| 107 | links+=("[MyCode](${PLATFORMS[MYCODE]}/${ORG}/${REPO})") |
| 108 | [[ -n "$FRAMAGIT" ]] && links+=("[Framagit](${PLATFORMS[FRAMAGIT]}/${ORG_FRAMAGIT}/${REPO})") |
| 109 | [[ -n "$ATOMGIT" ]] && links+=("[AtomGit](${PLATFORMS[ATOMGIT]}/${ORG_ATOMGIT}/${REPO})") |
| 110 | [[ -n "$GITHUB" ]] && links+=("[GitHub](${PLATFORMS[GITHUB]}/${ORG_GITHUB}/${REPO})") |
| 111 | [[ -n "$CODEBERG" ]] && links+=("[Codeberg](${PLATFORMS[CODEBERG]}/${ORG_CODEBERG}/${REPO})") |
| 112 | [[ -n "$CODEUP" ]] && links+=("[Codeup](${PLATFORMS[CODEUP]}/${ORG_CODEUP}/${REPO})") |
| 113 | |
| 114 | if [[ "$HORIZONTAL" == "YES" ]]; then |
| 115 | # 完美横排:每个链接之间用「 ● 」分隔(左右各一个空格) |
| 116 | local first=1 |
| 117 | for link in "${links[@]}"; do |
| 118 | if (( first )); then |
| 119 | printf "%s" "$link" |
| 120 | first=0 |
| 121 | else |
| 122 | printf " ● %s" "$link" |
| 123 | fi |
| 124 | done |
| 125 | echo |
| 126 | echo |
| 127 | # 空一行,保持和竖排一致 |
| 128 | else |
| 129 | # 竖排(原样) |
| 130 | for link in "${links[@]}"; do |
| 131 | echo "- $link" |
| 132 | done |
| 133 | echo |
| 134 | fi |
| 135 | } |
| 136 | |
| 137 | main "$@" |
| 138 |