git-it-write/includes/discovery.php
Developer 7728f489bd feat: 本地文件模式 + 多仓库自动发现 (v3.0)
替换 Forgejo API 数据源为本地文件系统读取,支持 30+ 仓库自动发现。
新增 WP-Cron 定时同步、WP-CLI 命令、多站点支持、SEO 字段写入。
base_directory 为空时保留原 API 模式向后兼容。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-10 00:37:12 +08:00

89 lines
2.2 KiB
PHP

<?php
if( ! defined( 'ABSPATH' ) ) exit;
class GIW_Discovery{
/**
* Scan base_directory for subdirectories that contain a .git/ folder.
*
* @param string $base_dir Absolute path to the base directory.
* @return array Array of discovered repos: [ ['name'=>..., 'path'=>..., 'config'=>[...]], ... ]
*/
public static function discover( $base_dir ){
$base_dir = rtrim( $base_dir, '/' );
$repos = array();
if( !is_dir( $base_dir ) ){
GIW_Utils::log( 'Discovery: base_directory does not exist — ' . $base_dir );
return $repos;
}
$entries = scandir( $base_dir );
if( $entries === false ){
GIW_Utils::log( 'Discovery: cannot read base_directory — ' . $base_dir );
return $repos;
}
foreach( $entries as $entry ){
if( $entry === '.' || $entry === '..' ){
continue;
}
$entry_path = $base_dir . '/' . $entry;
if( !is_dir( $entry_path ) ){
continue;
}
// Must contain .git/ to be a repo.
if( !is_dir( $entry_path . '/.git' ) ){
continue;
}
$config = GIW_Repo_Config::load( $entry_path );
$repos[] = array(
'name' => $entry,
'path' => $entry_path,
'config' => $config,
);
}
GIW_Utils::log( 'Discovery: found ' . count( $repos ) . ' repositories in ' . $base_dir );
return $repos;
}
/**
* Find a single repo by directory name within base_dir.
*
* @param string $base_dir Absolute path.
* @param string $repo_name Directory name.
* @return array|false Repo array or false.
*/
public static function find( $base_dir, $repo_name ){
$base_dir = rtrim( $base_dir, '/' );
$repo_path = $base_dir . '/' . $repo_name;
if( !is_dir( $repo_path ) || !is_dir( $repo_path . '/.git' ) ){
return false;
}
return array(
'name' => $repo_name,
'path' => $repo_path,
'config' => GIW_Repo_Config::load( $repo_path ),
);
}
}
?>