最后活跃于 1 month ago

展示代码仓库的镜像地址(添加至 Git 项目的底部)

README.md 原始文件

展示代码仓库的镜像地址

添加代码仓库的镜像地址至 Git 项目的底部

git-mirror.ps1 原始文件
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()]
17param(
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
37Set-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
50function 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
109Main
110
git-mirror.sh 原始文件
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 配置
13declare -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
22parse_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'
38Usage: gitmirror -o <org> [-r <repo>] [options]
39
40Options:
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 显示帮助
50EOF
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
71main() {
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
137main "$@"
138