-
Notifications
You must be signed in to change notification settings - Fork 87
fix(plugin-git): support submodule #621
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
08ad89f
ee60c9e
79d56fb
eeafcb8
aaa7746
bc24cb1
e3f22c1
59f88e1
19afa26
366a4f5
0fa382d
b8fbd36
12d2d17
affa47c
52e6a36
80fc507
f29e845
901b1ce
5616503
6f707d3
209247f
99fa0e4
9f9b7d4
fa93008
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { spawn } from 'node:child_process' | |
| import type { GitContributorInfo } from '../../shared/index.js' | ||
| import type { GitPluginOptions } from '../options.js' | ||
| import type { MergedRawCommit, RawCommit } from '../typings.js' | ||
| import { path } from 'vuepress/utils' | ||
| import { logger } from './logger.js' | ||
|
|
||
| const INFO_SPLITTER = '[|]' | ||
|
|
@@ -77,6 +78,58 @@ const runGitLog = (args: string[], cwd: string): Promise<string> => | |
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * Get git repository root directory for a given file path. | ||
| * | ||
| * This function runs `git rev-parse --show-toplevel` in the directory of the | ||
| * target file to determine the top-level directory of the git repository. | ||
| * | ||
| * @param filePath - File path (relative or absolute) whose repository root is requested | ||
| * @param cwd - Current working directory | ||
| * @returns Promise that resolves to normalized git root path, or null if not in a git repository | ||
| */ | ||
| const getGitRepoRoot = ( | ||
| filePath: string, | ||
| cwd: string, | ||
| ): Promise<string | null> => | ||
| new Promise((resolve) => { | ||
| const absFilePath = path.isAbsolute(filePath) | ||
| ? filePath | ||
| : path.resolve(cwd, filePath) | ||
| const dir = path.dirname(absFilePath) | ||
| const gitProcess = spawn('git', ['rev-parse', '--show-toplevel'], { | ||
| cwd: dir, | ||
| stdio: ['ignore', 'pipe', 'pipe'], | ||
| }) | ||
|
|
||
| let stdoutData = '' | ||
| let stderrData = '' | ||
|
|
||
| gitProcess.stdout.on('data', (chunk: Buffer) => { | ||
| stdoutData += chunk.toString('utf-8') | ||
| }) | ||
|
|
||
| gitProcess.stderr.on('data', (chunk: Buffer) => { | ||
| stderrData += chunk.toString('utf-8') | ||
| }) | ||
|
|
||
| gitProcess.on('error', (error) => { | ||
| logger.error(`Failed to spawn git rev-parse: ${error.message}`) | ||
| resolve(null) | ||
| }) | ||
|
|
||
| gitProcess.on('close', (code) => { | ||
| if (code === 0) { | ||
| resolve(path.normalize(stdoutData.trim())) | ||
| } else { | ||
| logger.error( | ||
| `git rev-parse failed (code=${code}): ${stderrData.trim()}`, | ||
| ) | ||
| resolve(null) | ||
|
Mister-Hope marked this conversation as resolved.
Outdated
|
||
| } | ||
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * Get raw commits for a specific file | ||
| * | ||
|
|
@@ -96,18 +149,31 @@ export const getRawCommits = async ( | |
| options: GitPluginOptions, | ||
| ): Promise<RawCommit[]> => { | ||
| const format = getGitLogFormat(options) | ||
| const gitRoot = await getGitRepoRoot(filepath, cwd) | ||
|
|
||
|
Comment on lines
157
to
159
|
||
| try { | ||
| let _filepath = filepath | ||
|
Mister-Hope marked this conversation as resolved.
Outdated
|
||
| if (gitRoot) { | ||
| // Resolve to absolute path first, then convert to repo-relative path | ||
| const absFilePath = path.isAbsolute(_filepath) | ||
| ? _filepath | ||
| : path.resolve(cwd, _filepath) | ||
|
|
||
| _filepath = path.relative(gitRoot, absFilePath) | ||
| } else { | ||
| logger.warn('Get git repo root error!') | ||
|
Mister-Hope marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const stdout = await runGitLog( | ||
| [ | ||
| '--max-count=-1', | ||
| `--format=${format}${COMMIT_SPLITTER}`, | ||
| '--date=unix', | ||
| '--follow', | ||
| '--', | ||
| filepath, | ||
| _filepath, | ||
| ], | ||
| cwd, | ||
| gitRoot || cwd, | ||
|
Comment on lines
157
to
+198
|
||
| ) | ||
|
|
||
| return stdout | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.