From 83d5585c856fea92fde2a156fcd5cfd448114d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=20=E6=86=B2=E5=A4=AA=E9=83=8E?= Date: Tue, 12 Nov 2024 19:32:55 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20`git=20status`=20=E3=81=AE?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=82=88=E3=82=8A=E6=9B=B4=E6=96=B0=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AE=E4=B8=80=E8=A6=A7=E3=82=92?= =?UTF-8?q?=E5=BE=97=E3=82=8B=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + Makefile | 9 ++++ bin/getModifiedFilePath.php | 90 +++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 bin/getModifiedFilePath.php diff --git a/.gitignore b/.gitignore index 4303942..9f80d96 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ * !.gitignore +!bin/ +!bin/** !LICENSE !Makefile !README.md diff --git a/Makefile b/Makefile index bbe03cc..e5d0bd5 100644 --- a/Makefile +++ b/Makefile @@ -42,3 +42,12 @@ open: else \ echo "Output file not found: output/php-chunked-xhtml/index.html"; \ fi + +open-modified: + PATHS=`php bin/getModifiedFilePath.php`; \ + \ + if [ -n "$$PATHS" ]; then \ + open $$PATHS; \ + else \ + echo "Modified file not found"; \ + fi diff --git a/bin/getModifiedFilePath.php b/bin/getModifiedFilePath.php new file mode 100644 index 0000000..11b516a --- /dev/null +++ b/bin/getModifiedFilePath.php @@ -0,0 +1,90 @@ + str_ends_with($file, '.xml'), + ); + + $pageIds = array_filter(array_map( + fn (string $file) => getPageIdFromFile(LANG . '/' . $file), + $modifiedFiles, + )); + + $pathes = array_map( + fn (string $id) => "output/php-chunked-xhtml/{$id}.html", + $pageIds, + ); + + echo implode("\n", $pathes) . PHP_EOL; +} + +/** + * 指定されたパスの git-status を取得 + */ +function getGitStatusOutput(string $path): string +{ + return `git -C {$path} status --porcelain=1`; +} + +/** + * git-status の出力より更新されたファイルの一覧を取得 + * + * @return string[] + */ +function getModifiedFilesFromGitStatusOutput(string $output): array +{ + $files = []; + + foreach (explode("\n", $output) as $line) { + $status = substr($line, 0, 2); + $file = substr($line, 3); + + if (!in_array($status, [ + '??', // 新規の未追跡ファイル(Untracked) + 'A ', // インデックスに追加された新規ファイル(Added to index) + 'M ', // インデックスが修正されたファイル(Modified in index) + 'AM', // インデックスに追加され、ワーキングディレクトリでも変更されたファイル + 'MM', // インデックスが修正され、ワーキングディレクトリでも修正されたファイル + 'RM', // インデックスでリネームされ、ワーキングディレクトリで修正されたファイル + 'CM', // インデックスでコピーされ、ワーキングディレクトリで修正されたファイル + ' M', // ワーキングディレクトリで変更されたファイル(Modified in working tree) + ])) { + continue; + } + + $files[] = preg_match('/^\S+ -> (\S+)$/', $file, $matches) + ? $matches[1] + : $file; + } + + return $files; +} + +function getPageIdFromFile(string $file): ?string +{ + $xml = new XMLReader(); + $xml->open($file); + + while ($xml->nodeType !== XMLReader::ELEMENT) { + if (!@$xml->read()) { + return null; + } + } + + $id = $xml->getAttribute('xml:id'); + + return $id; +}