Skip to content

Commit

Permalink
fix: git remote url
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 7, 2024
1 parent c399372 commit 6de0109
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .prettierrc.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import config from '@innei/prettier'

export default config
export default {
...config,
importOrderParserPlugins: ['importAssertions', 'typescript', 'jsx'],
}
76 changes: 60 additions & 16 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { execSync } from 'child_process'

import { config } from 'dotenv'
import path from 'path'
import { config } from 'dotenv'

import NextBundleAnalyzer from '@next/bundle-analyzer'

// const pkg = require('./package.json')
import pkg from './package.json' assert {type: 'json'}

const __dirname = path.resolve()
import pkg from './package.json' assert { type: 'json' }

process.title = 'Shiro (NextJS)'

const env = config().parsed || {}
const isProd = process.env.NODE_ENV === 'production'

let commitHash = ''
let commitUrl = ''
const repoInfo = getRepoInfo()

try {
commitHash = execSync('git log --pretty=format:"%h" -n1')
.toString()
.trim()
} catch (err) {
console.error('Error getting commit hash', err)
if (repoInfo) {
commitHash = repoInfo.hash
commitUrl = repoInfo.url
}

/** @type {import('next').NextConfig} */
// eslint-disable-next-line import/no-mutable-exports
let nextConfig = {
Expand All @@ -36,6 +31,7 @@ let nextConfig = {
env: {
APP_VERSION: pkg.version,
COMMIT_HASH: commitHash,
COMMIT_URL: commitUrl,
},
reactStrictMode: true,
productionBrowserSourceMaps: false,
Expand Down Expand Up @@ -89,13 +85,12 @@ let nextConfig = {
options: {
publicPath: '/_next/',
worker: {
type: "SharedWorker",
type: 'SharedWorker',
// https://v4.webpack.js.org/loaders/worker-loader/#worker
options: {
name: "shiro-ws-worker",
name: 'shiro-ws-worker',
},
},

},
})

Expand Down Expand Up @@ -146,3 +141,52 @@ if (process.env.ANALYZE === 'true') {
}

export default nextConfig

function getRepoInfo() {
try {
// 获取最新的 commit hash
// 获取当前分支名称
const currentBranch = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim()
// 获取当前分支跟踪的远程仓库名称
const remoteName = execSync(`git config branch.${currentBranch}.remote`)
.toString()
.trim()
// 获取当前分支跟踪的远程仓库的 URL
let remoteUrl = execSync(`git remote get-url ${remoteName}`)
.toString()
.trim()

// 获取最新的 commit hash
const hash = execSync('git rev-parse HEAD').toString().trim()
// 转换 git@ 格式的 URL 为 https:// 格式
if (remoteUrl.startsWith('git@')) {
remoteUrl = remoteUrl
.replace(':', '/')
.replace('git@', 'https://')
.replace('.git', '')
} else if (remoteUrl.endsWith('.git')) {
// 对于以 .git 结尾的 https URL,移除 .git
remoteUrl = remoteUrl.slice(0, -4)
}

// 根据不同的 Git 托管服务自定义 URL 生成规则
let webUrl
if (remoteUrl.includes('github.com')) {
webUrl = `${remoteUrl}/commit/${hash}`
} else if (remoteUrl.includes('gitlab.com')) {
webUrl = `${remoteUrl}/-/commit/${hash}`
} else if (remoteUrl.includes('bitbucket.org')) {
webUrl = `${remoteUrl}/commits/${hash}`
} else {
// 对于未知的托管服务,可以返回 null 或一个默认格式
webUrl = `${remoteUrl}/commits/${hash}`
}

return { hash, url: webUrl }
} catch (error) {
console.error('Error fetching repo info:', error)
return null
}
}
17 changes: 12 additions & 5 deletions src/components/layout/footer/FooterInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,18 @@ const PoweredBy: Component = ({ className }) => {
</StyledLink>
}
>
{process.env.COMMIT_HASH && (
<MLink
href={`https://github.com/innei/Shiro/commit/${process.env.COMMIT_HASH}`}
>
开源版本哈希:{process.env.COMMIT_HASH}
这是{' '}
<StyledLink
className="underline"
href="https://github.com/innei/Shiro"
target="_blank"
>
Shiro
</StyledLink>{' '}
的开源版本。
{process.env.COMMIT_HASH && process.env.COMMIT_URL && (
<MLink href={process.env.COMMIT_URL}>
版本哈希:{process.env.COMMIT_HASH.slice(0, 8)}
</MLink>
)}
</FloatPopover>
Expand Down

0 comments on commit 6de0109

Please sign in to comment.