TOP ▲
itcore TOP
> TIPS
> tool_git.php
タグ:tool git file ソース TOOL git&file | itcore 2021年
<?php
//--------------------------------------------------------------
// git&file情報 git.php
//--------------------------------------------------------------
include_once "../.env.php"; // 環境依存インクルード
include_once "inc_common.php"; // 共通インクルード
list($datetime1, $datetime2, $self, $menu, $today) = uInit(); // 初期設定
$title = "git&file情報";
include_once "inc_header.php"; // ヘッダインクルード
// デバッグ情報
if (20210428 == $today) {
u_debug_echo();
}
$dir_prj = "/var/prj";
$today = date("Ymd");
$color_cmd = "crimson"; // コマンドの色
$ar = uOpendirArrayGet($dir_prj);
$a_prj = [];
foreach ($ar as $p1) {
$dir = "$dir_prj/$p1";
if (is_dir($dir) && "xxxx-" == substr($p1, 0, 5)) {
$a_prj[$p1] = $dir;
}
}
// 制御系データ
$type = "site"; // git情報
$mode = uForm("mode");
$prj = uForm("prj");
$dir = uForm("dir");
$file = uForm("file");
$subdir = uForm("subdir");
$git_cmd = uForm("git_cmd");
$diff_from = uForm("diff_from");
$diff_to = uForm("diff_to");
$html_debug = "";
// 共通的な変数の初期化
$html_rows = $html_rows2 = $html_td = "";
$no = 0;
//------------------------------------------
// cat ファイルの表示 grep.phpから呼ばれる。
//------------------------------------------
if ("cat" == $mode) {
$line_no = uForm("line_no"); // 行番号 表示
u1_cmd_cat("cat $file", $line_no);
echo uJsClose(); // 閉じるボタン
exit();
}
//--------------------
// プロジェクト一覧表示
//--------------------
$html_prj = "";
$html_prj .= u_html_prj("git config --list");
$no = 0;
$html_td = $html_find = "";
foreach ($a_prj as $p1 => $d1) {
$no++;
$html_td .= "<tr><td align=right>$no"; // No site
$html_td .= "<td>$d1";
$s1 = " <a href=$self?mode=file&prj=$p1&dir=$d1>file</a>\n";
$s1 .= "<a href=$self?mode=prj&prj=$p1&dir=$d1>status</a>\n";
$s1 .= " <a href=$self?mode=branch&prj=$p1&dir=$d1>branch</a>\n";
$html_td .= "<td>$s1";
if ("" == $mode) {
$html_find .= "<h2>24H以内に更新したファイル($d1)</h2>\n";
$cmd = "find $d1 -type f -mtime -1 |xargs ls -l --time-style='+%Y-%m-%d %H:%M:%S'|grep -v '\.git' |grep -v storage |sed -e 's/ */ /g' |cut -f 6- -d ' ' |sort -r";
$html_find .= u_cmd_find($cmd);
}
}
echo <<<EOT
<a href=$self>top</a>
$html_prj
<form name=form1 method=post action=$self>
<h2>プロジェクト一覧</h2>
<table>
<tr><th>No<th>ディレクトリ<th>file git
$html_td
</table>
$html_find
EOT;
$rows = "";
if ("" != $prj) {
$s1 = "<a href=$self?mode=prj&prj=$prj&dir=$dir>status</a>\n";
$s1 .= " <a href=$self?mode=file&prj=$prj&dir=$dir>file</a>\n";
$s1 .= " <a href=$self?mode=branch&prj=$prj&dir=$dir>branch</a>\n";
}
echo <<<EOT
<input type=hidden name=mode value=$mode>
<input type=hidden name=prj value=$prj>
<input type=hidden name=dir value=$dir>
<input type=hidden name=file value=$file>
EOT;
//--------------------
// 環境関連のコマンド
//--------------------
if ("prj_cmd" == $mode) {
//echo "debug107 git_cmd=$git_cmd<br>\n";
$no = 0;
$html_td = "";
foreach ($a_prj as $p1 => $d1) {
$no++;
list($cmd_html, $ret_html) = u_git_cmd($d1, $git_cmd);
$html_td .= "<tr><td align=right>$no<td>$p1<td>[$d1]<br>";
$html_td .= "$ret_html";
}
// 画面表示
echo <<<EOT
<h2>設定情報</h2>
<h3>$git_cmd</h3>
<table>
<tr><th>No<th>prj<th algin=left>$git_cmd
$html_td
</table>
EOT;
}
//--------------------
// 環境 git branch
//--------------------
if ("branch" == $mode) {
// gitコマンド実行
$html_rows .= u_git_cmd_html(
$dir,
"git fetch --prune",
"削除されたリモートブランチ情報をクリアします。"
);
$html_rows .= u_git_cmd_html(
$dir,
"git branch -a",
"ローカルとリモートのブランチを表示します。"
);
$html_rows .= u_git_cmd_html(
$dir,
"git branch -vv",
"追跡しているブランチが正しいか確認して下さい。"
);
echo <<<EOT
<h2>branch</h2>
<h3>現在のbranch</h3>
<button type="submit" name=sub_prj class="btn btn-primary btn-sm">
再表示
</button>
<table>
$html_rows
</table>
<h3>新規ブランチ作成</h3>
メンテナンスをするときは、新たにブランチを作成します。<br>
まずリモートから最新のrelaseをpullします。<br>
そして、新しいブランチを作成します。<br>
xxxは適当なキーワードを指定してください。例:login<br>
新しいブランチを作成したら、リモートへpushします。<br>
<font color=$color_cmd>
cd $dir<br>
git checkout release<br>
git pull<br>
git checkout -b ${today}_${prj}_xxx<br>
git push -u origin HEAD<br>
</font>
<h3>プログラム修正</h3>
vimでプログラムを修正します。<br>
プログラムを修正したら、:Prettierで整形します。(vimの中で実行)<br>
整形が崩れる場合は、対象ブロックの前にprettier-ignoreの指定をします。<br>
<!-- prettier-ignore --><br />
@section('body')<br>
<table>
<tr><th>vimコマンド<th>内容(カスタム設定含む)
<tr><td class=u-cmd>:se nonu<td>行番号を表示しない。
<tr><td class=u-cmd>:se nu<td>行番号を表示する。
<tr><td class=u-cmd>:Prettier<td>コードを整形する。
<tr><td class=u-cmd>%<td>カッコだけでなくdivタグなどの対も確認できる。
</table>
修正したファイルは git status で確認できます。<br>
ある程度修正したら、エラーがない状態で git add . を実行してステージングにあげてください。<br>
ステージングにあげていない修正内容は git diff で確認できます。<br>
ステージングあげた修正内容は git diff --chached で確認できます。<br>
<font color=$color_cmd>
git status<br>
git diff<br>
git diff --cached<br>
</font>
<h3>コミット</h3>
プログラムの修正が完了して、動作確認をしたらコミットをしてください。<br>
ステージングされているものがコミット対象となりますので、ステージングにあげるのを忘れないようにしてください。<br>
vimを開いていると、.swpファイルが作成されています。余分なファイルが含まれていないか、git status で確認します。<br>
xxxは修正した内容を完結に記述してください。例:Google認証を追加<br>
コミットしたらリモートへpushします。<br>
<font color=$color_cmd>
cd $dir<br>
git add .<br>
git status<br>
git commit -m 'xxx'<br>
git push<br>
</font>
<h3>PR(プルリク)作成</h3>
単体テストが終了したら、githubでPRを作成してください。<br>
レビュー担当者が修正内容をチェックしてマージを行います。<br>
<font color=$color_cmd>
Compare & pull request 又は Nre pull request<br>
base:releasse compare:自分が作成したブランチ<br>
タイトルと内容を入力<br>
Create pull request<br>
レビュアーをアサインする。
</font>
<h3>ブランチ削除</h3>
PRがマージされたらローカルのブランチを削除してください。<br>
<font color=$color_cmd>
git checkout release<br>
git branch<br>
git branch -D ${today}_${prj}_[keyword]<br>
</font>
EOT;
}
//--------------------
// 環境 git statusなど
//--------------------
if ("prj" == $mode) {
$html_rows .= u_git_cmd_html($dir, "git status");
$html_rows .= u_git_cmd_html($dir, "git diff");
$html_rows .= u_git_cmd_html($dir, "git diff --cached");
if ("" != uForm("sub_prj")) {
$diff_from = "";
$diff_to = "";
} // ラジオボタン選択リセット
$html_rows2 = u_git_cmd_log(
$dir,
"git log -30 --pretty='format:%h %cd [%an] %s ' --date=iso "
);
if ("" != uForm("sub_git_diff_commit")) {
$s1 = "git diff $diff_from $diff_to";
$html_rows2 .= u_git_cmd_html($dir, $s1);
}
echo <<<EOT
<h2>現在の修正状況</h2>
<table>
<tr><th>ディレクトリ<td>$dir/$subdir
</table>
<br>
<button type="submit" name=sub_prj class="btn btn-primary btn-sm">
再表示
</button>
<br><br>
<table>
$html_rows
</table>
<h3>過去の修正状況</h3>
<table>
$html_rows2
</table>
EOT;
}
//--------------------
// ファイル一覧
//--------------------
if ("file" == $mode || "diff_file" == $mode) {
$dir1 = "$dir/$subdir"; // 最後に/がついている
//echo "debug269 dir1=$dir1 subdir=$subdir<br>\n";
$subdir1 = dirname(uGet($subdir));
if ("." == $subdir1) {
$subdir1 = "";
}
if ("" != $subdir1) {
$subdir1 .= "/";
}
if ("" != $subdir) {
$fname = "<a href=$self?mode=file&prj=$prj&dir=$dir&subdir=$subdir1>上位ディレクトリ</a>";
} else {
//$fname = "<a href=$self?mode=file&prj=$prj&dir=$dir&subdir=$subdir1>上位ディレクトリ</a>";
$fname = "上位ディレクトリ";
}
$html_rows .= "<tr><td align=right><td>$fname\n";
if ("diff_file" != $mode) {
$dir1 = uDir($dir1); // 最後に/があればとる。
$a_fname = uOpendirArrayGet($dir1);
$no = 0;
foreach ($a_fname as $fname) {
$no++;
$file1 = "$dir1/$fname";
if (is_dir($file1)) {
// ディレクトリの場合
$fname = "<a href=$self?mode=$mode&prj=$prj&dir=$dir&subdir=$subdir$fname/>$subdir$fname/</a>";
$html_rows .= "<tr><td align=right>$no<td>$fname\n";
} else {
// ファイルの場合
$s2 = "<a href=$self?mode=diff_file&prj=$prj&dir=$dir&file=$file1&subdir=$subdir$fname/#log>$subdir$fname</a>";
$html_rows .= "<tr><td align=right>$no<td>$s2\n";
}
}
}
// git diff
if ("" != $file) {
if ("" != uForm("sub_diff_file")) {
$diff_from = "";
$diff_to = "";
} // ラジオボタン選択リセット
if ("diff_file" == $mode) {
$html_rows2 .= "<div id=log>";
$html_rows2 .= u_git_cmd_log(
$dir,
"git log -30 --pretty='format:%h %cd [%an] %s ' --date=iso -- $file"
);
}
if ("" != uForm("sub_git_diff_commit")) {
$s1 = "git diff $diff_from $diff_to";
if ("" != $file) {
$s1 .= " -- $file";
}
$html_rows2 .= u_git_cmd_html($dir, $s1);
}
$html_rows2 .= u_git_cmd_html($dir, "cat $file");
}
$html_file = "";
if ("" != $file) {
$html_file = "<tr><th>file<td>$file";
}
// ショートカット
$a_shortcut = [];
$a_shortcut[] = "";
$a_shortcut[] = "app/Http/Controllers/";
$a_shortcut[] = "resources/views/";
$a_shortcut[] = "public/";
$html_shortcut = "";
foreach ($a_shortcut as $shortcut) {
$d2 = "/var/prj/$prj";
$d1 = "$d2/$shortcut";
$d1 = str_replace("//", "/", $d1);
//echo "debug338 prj=$prj d1=$d1<br>\n";
if (is_dir($d1)) {
$s1 = "<a href=$self?mode=file&prj=$prj&dir=$d2&subdir=$shortcut>$d1</a>";
$html_shortcut .= "$s1<br>\n";
}
}
echo <<<EOT
<h2>ショートカット一覧</h2>
$html_shortcut
<h2>ファイル一覧</h2>
<table>
<tr><th>ディレクトリ<td>$dir/$subdir
$html_file
</table>
<br>
<table>
<tr><th>No<th>ファイル/ディレクトリ
$html_rows
</table>
<br>
<table>
$html_rows2
</table>
EOT;
}
//--------------------
// アプリ関数
//--------------------
// list ($cmd_html, $ret_html) = u_git_cmd($dir, $cmd); // gitコマンド実行
function u_git_cmd($dir, $cmd)
{
$ret = shell_exec("cd $dir; $cmd");
// 加工
$a_line = explode("\n", $ret);
$s1 = "";
$no = $no_end = 0;
foreach ($a_line as $line) {
if (" " == $line) {
$line = "";
} // 空行の場合はdiffでつく空白を削除する
$line_pre = $line; // サニタイズ前
$line = uHtmlPre($line);
// 全角空白(■)を赤く表示する。
$line = str_replace("■", "<font color=red>■</font>", $line);
// 行末の空白を赤く表示する。
if (" " == substr($line_pre, -1)) {
$line .= "<font color=red>[行末空白]</font>";
}
// diff 差分を色付け
$s2 = substr($line, 0, 1);
if ("-" == $s2) {
$line = "<font color=red>$line</font>";
}
if ("+" == $s2) {
$line = "<font color=blue>$line</font>";
}
// 行番号をつける。 @@ -428,6 +428,10 @@ function u_list2($list2) {
if ("@@ " == substr($line_pre, 0, 3)) {
$a_word = explode(" ", $line_pre . " ");
$word = $a_word[2]; // +428,10
$a_no = explode(",", substr($word, 1) . ","); // [428, 10]
$no = $a_no[0];
// @@ -0,0 +1 @@
if ("" == $a_no[1]) {
$a_no[1] = 0;
// echo "debug448 a_no[1]='". $a_no[1]. "' line=$line<br>\n";
}
$no_end = $no + $a_no[1];
} elseif (0 < $no && $no <= $no_end) {
if ("-" == $s2) {
$line = str_repeat(" ", strlen($no) + 1) . $line;
} else {
$line = "$no $line";
$no++;
}
}
// diff ファイルの表示
$s2 = substr($line, 0, 4);
//echo "debug434 s2=$s2 line=$line<br>\n";
if ("diff" == $s2) {
if ("" != $s1) {
$s1 .= "<br>";
} // ファイル毎に1行空ける。
$line = "[<b>$line</b>]";
}
$s1 .= "$line\n";
}
// HTML整形
$cmd_html = uHtmlPre($cmd);
$s1 = uChomp($s1);
$ret_html = nl2br($s1);
return [$cmd_html, $ret_html];
}
// $html_rows .= u_git_cmd_html($dir, $cmd); // gitコマンド実行 html
function u_git_cmd_html($dir, $cmd, $desc = "")
{
global $color_cmd;
list($cmd_html, $ret_html) = u_git_cmd($dir, $cmd);
$html_rows = "<tr><th align=left>";
if ("" != $desc) {
$html_rows .= "$desc<br>";
}
$html_rows .= "<font color=$color_cmd>$cmd_html</font>";
$html_rows .= "<tr><td>$ret_html";
return $html_rows;
}
// list ($cmd_html, $ret_html) = u_git_cmd_log($dir, $cmd); // git logコマンド実行
function u_git_cmd_log($dir, $cmd)
{
global $diff_from, $diff_to;
$ret = shell_exec("cd $dir; $cmd");
// ラジオボタンをつける。
$a_line = explode("\n", $ret);
$s1 = "";
$no = 0;
foreach ($a_line as $line) {
$no++;
$line = htmlspecialchars($line);
$a_cell = explode(" ", $line);
$cid = $a_cell[0];
$checked_from = "";
if (("" == $diff_from && 2 == $no) || $cid == $diff_from) {
$checked_from = "checked";
}
$checked_to = "";
if (("" == $diff_to && 1 == $no) || $cid == $diff_to) {
$checked_to = "checked";
}
$a_cell[0] = "<input type=radio name=diff_from $checked_from value=$cid>";
$a_cell[0] .= "<input type=radio name=diff_to $checked_to value=$cid> $cid";
$s1 .= implode(" ", $a_cell) . "\n";
}
// HTML整形
$cmd_html = uHtmlPre($cmd);
//$s1 = uChomp($s1);
$s1 .=
"<input type=submit name=sub_git_diff_commit value='指定したコミット間で git diff'>\n";
$ret_html = nl2br($s1);
$html_rows = "<tr><th align=left>$cmd_html<tr><td>$ret_html";
return $html_rows;
}
// $html_prj = u_html_prj("git config --list"); // 環境情報
function u_html_prj($git_cmd)
{
global $self;
$s2 = urlencode($git_cmd);
$s1 = "<a href=$self?mode=prj_cmd&git_cmd=$s2>$git_cmd</a>\n";
return $s1;
}
// cat出力
function u1_cmd_cat($cmd, $line_no)
{
$ret = shell_exec($cmd);
//echo "<h3>" . uHtmlPre($cmd) . "</h3>\n";
echo "■" . uHtmlPre($cmd) . "<br>\n";
$a_line = explode("\n", $ret);
$no = 0;
foreach ($a_line as $line) {
$no++;
$s1 = uHtmlPre($line) . "<br>\n";
if ($no == $line_no) {
echo "<span id=line_no_$no><font color=red><b>$no: $s1</b></font></span>";
} elseif ("" != $line_no) {
echo "$no: $s1";
} else {
echo $s1;
}
}
}
// find 最近更新したファイル一覧
function u_cmd_find($cmd)
{
$html = "";
$html .= "<h3>$cmd</h3>\n";
$output = [];
exec($cmd, $output, $status);
//$html .= "<textarea rows=5>\n";
foreach ($output as $line) {
list($date, $time, $file) = explode(" ", $line);
$s1 = "$date $time <a target=_blank href=git.php?mode=cat&file=$file>$file</a>";
//$s1 = htmlspecialchars($line);
//$s1 = str_replace(" ", " ", $s1);
$html .= "$s1<br>\n";
}
//$html .= "</textarea><br>\n";
return $html;
}
//--------------------
// フッタ
//--------------------
?>
</form>
<hr>
<?php echo $html_debug;
?>