Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: support git submodule for vitepress-plugin-git-changelog #185

Closed
northword opened this issue Apr 23, 2024 · 1 comment · Fixed by #189
Closed

feature request: support git submodule for vitepress-plugin-git-changelog #185

northword opened this issue Apr 23, 2024 · 1 comment · Fixed by #189
Labels
enhancement New feature or request

Comments

@northword
Copy link
Member

northword commented Apr 23, 2024

描述

ref: #183 (comment)

部分时候,出于方便其他人贡献或方便管理的角度考虑,会把文档分离成单独的仓库。在网站(除文档外,还包含一些其他组件)最终构建时,需要把文档一起构建。

一个可能的示例结构:

主仓库 website,文档仓库 wiki 以子模块放入 src/wiki

northword@Yoga-Northword MINGW64 /d/Code/Zotero/zotero-chinese-website (feat/git-changelog)
$ tree -d -L 4 -I 'node_modules|dist'
.
`-- src
    |-- plugins
    |   |-- backend
    |   |   |-- src
    |   |   `-- types
    |   |-- components
    |   |   `-- icons
    |   |-- data
    |   `-- types
    |-- public
    |-- styles
    |-- translators
    |   |-- components
    |   |-- data
    |   `-- types
    `-- wiki
        |-- scripts
        `-- src
            |-- assets
            |-- contributing
            |-- csl-dev-guide
            |-- plugin-dev-guide
            `-- user-guide

23 directories


northword@Yoga-Northword MINGW64 /d/Code/Zotero/zotero-chinese-website/src/wiki (vitepress)
$ tree -d -L 4 -I 'node_modules|dist'
.
|-- scripts
`-- src
    |-- assets
    |-- contributing
    |-- csl-dev-guide
    |-- plugin-dev-guide
    |   |-- development
    |   |-- quick-start
    |   |-- reference
    |   `-- use-template
    `-- user-guide
        |-- faqs
        |-- misc
        `-- plugins

14 directories

但这样的结构,vitepress-plugin-git-changelog 插件无法获取 子模块 src/wiki 的 git log,请求增加对子模块的支持。

可能的思路

simple-git 在设置 options 时,可以传入所有 git 支持的参数:

const gitLogsRaw = await git.log({ maxCount: maxGitLogCount  , "-p": true,"--submodule": true});

这样得到的 git log 包含了子模块的提交,但是需要进一步处理,例如:https://stackoverflow.com/questions/10741801/include-submodule-commit-messages-with-git-log

VitePress 官方的是支持读取 submodule 中的时间戳的(src/wiki 下的 md 文件在构建后包含正确的 lastUpdated 时间),

// https://github.com/vuejs/vitepress/blob/main/src/node/utils/getGitTimestamp.ts
import { spawn } from 'cross-spawn'
import fs from 'fs-extra'
import { basename, dirname } from 'path'

const cache = new Map<string, number>()

export function getGitTimestamp(file: string) {
  const cached = cache.get(file)
  if (cached) return cached

  return new Promise<number>((resolve, reject) => {
    const cwd = dirname(file)
    if (!fs.existsSync(cwd)) return resolve(0)
    const fileName = basename(file)
    const child = spawn('git', ['log', '-1', '--pretty="%ai"', fileName], {
      cwd
    })
    let output = ''
    child.stdout.on('data', (d) => (output += String(d)))
    child.on('close', () => {
      const timestamp = +new Date(output)
      cache.set(file, timestamp)
      resolve(timestamp)
    })
    child.on('error', reject)
  })
}

(但离奇的地方在于,我手动执行 git log --pretty="%ai" src/wiki/src/index.md 时,并没有输出)

UPDATE: 似乎是它在获取每一个文件的 git log 时,都是进去了这个文件所在的目录,所以能够获取到子模块的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants