Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ temp_root = tempfile.mkdtemp()
|
|
| 10 |
REPO_TARGET_NAME = "target_repo"
|
| 11 |
REPO_SOURCE_NAME = "source_repo"
|
| 12 |
|
|
|
|
| 13 |
def clone_repo(url, name):
|
| 14 |
dest = os.path.join(temp_root, name)
|
| 15 |
if os.path.exists(dest):
|
|
@@ -20,6 +21,7 @@ def clone_repo(url, name):
|
|
| 20 |
except subprocess.CalledProcessError as e:
|
| 21 |
return None, f"クローン失敗: {e.stderr.strip()}"
|
| 22 |
|
|
|
|
| 23 |
def get_relative_diff_files(repo_a_path, repo_b_path):
|
| 24 |
try:
|
| 25 |
result = subprocess.run(
|
|
@@ -39,6 +41,7 @@ def get_relative_diff_files(repo_a_path, repo_b_path):
|
|
| 39 |
except Exception as e:
|
| 40 |
return [f"エラー: {str(e)}"]
|
| 41 |
|
|
|
|
| 42 |
def run_comparison(target_url, source_url):
|
| 43 |
repo_a, err1 = clone_repo(target_url, REPO_TARGET_NAME)
|
| 44 |
repo_b, err2 = clone_repo(source_url, REPO_SOURCE_NAME)
|
|
@@ -51,20 +54,19 @@ def run_comparison(target_url, source_url):
|
|
| 51 |
diff_files = get_relative_diff_files(repo_a, repo_b)
|
| 52 |
return gr.update(choices=diff_files, value=diff_files), diff_files, ""
|
| 53 |
|
|
|
|
| 54 |
def copy_files(selected_files):
|
| 55 |
repo_a = os.path.join(temp_root, REPO_TARGET_NAME)
|
| 56 |
repo_b = os.path.join(temp_root, REPO_SOURCE_NAME)
|
| 57 |
|
| 58 |
updated = []
|
| 59 |
for rel_path in selected_files:
|
| 60 |
-
src = os.path.join(repo_b, rel_path)
|
| 61 |
-
dst = os.path.join(repo_a, rel_path)
|
| 62 |
|
| 63 |
if not os.path.exists(src):
|
| 64 |
continue
|
| 65 |
-
|
| 66 |
-
# 同一ファイルをスキップ
|
| 67 |
-
if os.path.abspath(src) == os.path.abspath(dst):
|
| 68 |
continue
|
| 69 |
|
| 70 |
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
|
@@ -74,15 +76,15 @@ def copy_files(selected_files):
|
|
| 74 |
return f"{len(updated)} 個のファイルをコピーしました:\n" + "\n".join(updated)
|
| 75 |
|
| 76 |
# 差分が存在するかチェックしてGitHubへPush
|
| 77 |
-
def git_commit_and_push(token, commit_message):
|
| 78 |
repo_path = os.path.join(temp_root, REPO_TARGET_NAME)
|
| 79 |
if not os.path.exists(repo_path):
|
| 80 |
return "エラー:対象リポジトリが存在しません"
|
| 81 |
|
| 82 |
try:
|
| 83 |
-
# Git
|
| 84 |
-
subprocess.run(["git", "config", "user.name",
|
| 85 |
-
subprocess.run(["git", "config", "user.email",
|
| 86 |
|
| 87 |
subprocess.run(["git", "add", "."], cwd=repo_path, check=True)
|
| 88 |
|
|
@@ -122,13 +124,15 @@ with gr.Blocks(title="Git差分アップデーター") as demo:
|
|
| 122 |
diff_btn = gr.Button("差分取得&クローン")
|
| 123 |
diff_status = gr.Textbox(label="ステータス", interactive=False)
|
| 124 |
error_msg = gr.Textbox(label="エラー", visible=False, interactive=False)
|
| 125 |
-
diff_checkboxes = gr.CheckboxGroup(label="差分ファイル一覧(コピーしたいものを選択)", choices=[]
|
| 126 |
|
| 127 |
copy_btn = gr.Button("選択ファイルを上書きコピー")
|
| 128 |
copy_result = gr.Textbox(label="コピー結果", lines=10, interactive=False)
|
| 129 |
|
| 130 |
gr.Markdown("### 🔐 GitHub Push 設定")
|
| 131 |
token_input = gr.Textbox(label="GitHub Personal Access Token(公開しないでください)", type="password")
|
|
|
|
|
|
|
| 132 |
commit_msg = gr.Textbox(label="コミットメッセージ", value="Update from comparison tool")
|
| 133 |
push_btn = gr.Button("Push(mainブランチへ)")
|
| 134 |
push_result = gr.Textbox(label="Push結果", lines=3, interactive=False)
|
|
@@ -136,6 +140,8 @@ with gr.Blocks(title="Git差分アップデーター") as demo:
|
|
| 136 |
diff_btn.click(fn=run_comparison, inputs=[target_url, source_url],
|
| 137 |
outputs=[diff_checkboxes, diff_status, error_msg])
|
| 138 |
copy_btn.click(fn=copy_files, inputs=[diff_checkboxes], outputs=copy_result)
|
| 139 |
-
push_btn.click(fn=git_commit_and_push,
|
|
|
|
|
|
|
| 140 |
|
| 141 |
demo.launch()
|
|
|
|
| 10 |
REPO_TARGET_NAME = "target_repo"
|
| 11 |
REPO_SOURCE_NAME = "source_repo"
|
| 12 |
|
| 13 |
+
# GitHubリポジトリをクローン
|
| 14 |
def clone_repo(url, name):
|
| 15 |
dest = os.path.join(temp_root, name)
|
| 16 |
if os.path.exists(dest):
|
|
|
|
| 21 |
except subprocess.CalledProcessError as e:
|
| 22 |
return None, f"クローン失敗: {e.stderr.strip()}"
|
| 23 |
|
| 24 |
+
# 差分ファイル一覧取得(.git関連除外)
|
| 25 |
def get_relative_diff_files(repo_a_path, repo_b_path):
|
| 26 |
try:
|
| 27 |
result = subprocess.run(
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
return [f"エラー: {str(e)}"]
|
| 43 |
|
| 44 |
+
# 差分を取得してUIに反映
|
| 45 |
def run_comparison(target_url, source_url):
|
| 46 |
repo_a, err1 = clone_repo(target_url, REPO_TARGET_NAME)
|
| 47 |
repo_b, err2 = clone_repo(source_url, REPO_SOURCE_NAME)
|
|
|
|
| 54 |
diff_files = get_relative_diff_files(repo_a, repo_b)
|
| 55 |
return gr.update(choices=diff_files, value=diff_files), diff_files, ""
|
| 56 |
|
| 57 |
+
# 選択ファイルをコピー
|
| 58 |
def copy_files(selected_files):
|
| 59 |
repo_a = os.path.join(temp_root, REPO_TARGET_NAME)
|
| 60 |
repo_b = os.path.join(temp_root, REPO_SOURCE_NAME)
|
| 61 |
|
| 62 |
updated = []
|
| 63 |
for rel_path in selected_files:
|
| 64 |
+
src = os.path.abspath(os.path.join(repo_b, rel_path))
|
| 65 |
+
dst = os.path.abspath(os.path.join(repo_a, rel_path))
|
| 66 |
|
| 67 |
if not os.path.exists(src):
|
| 68 |
continue
|
| 69 |
+
if src == dst:
|
|
|
|
|
|
|
| 70 |
continue
|
| 71 |
|
| 72 |
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
|
|
|
| 76 |
return f"{len(updated)} 個のファイルをコピーしました:\n" + "\n".join(updated)
|
| 77 |
|
| 78 |
# 差分が存在するかチェックしてGitHubへPush
|
| 79 |
+
def git_commit_and_push(token, commit_message, user_name, user_email):
|
| 80 |
repo_path = os.path.join(temp_root, REPO_TARGET_NAME)
|
| 81 |
if not os.path.exists(repo_path):
|
| 82 |
return "エラー:対象リポジトリが存在しません"
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
# Git user設定
|
| 86 |
+
subprocess.run(["git", "config", "user.name", user_name], cwd=repo_path, check=True)
|
| 87 |
+
subprocess.run(["git", "config", "user.email", user_email], cwd=repo_path, check=True)
|
| 88 |
|
| 89 |
subprocess.run(["git", "add", "."], cwd=repo_path, check=True)
|
| 90 |
|
|
|
|
| 124 |
diff_btn = gr.Button("差分取得&クローン")
|
| 125 |
diff_status = gr.Textbox(label="ステータス", interactive=False)
|
| 126 |
error_msg = gr.Textbox(label="エラー", visible=False, interactive=False)
|
| 127 |
+
diff_checkboxes = gr.CheckboxGroup(label="差分ファイル一覧(コピーしたいものを選択)", choices=[])
|
| 128 |
|
| 129 |
copy_btn = gr.Button("選択ファイルを上書きコピー")
|
| 130 |
copy_result = gr.Textbox(label="コピー結果", lines=10, interactive=False)
|
| 131 |
|
| 132 |
gr.Markdown("### 🔐 GitHub Push 設定")
|
| 133 |
token_input = gr.Textbox(label="GitHub Personal Access Token(公開しないでください)", type="password")
|
| 134 |
+
user_name = gr.Textbox(label="Gitユーザー名", value="your-name")
|
| 135 |
+
user_email = gr.Textbox(label="Gitメールアドレス", value="your@email.com")
|
| 136 |
commit_msg = gr.Textbox(label="コミットメッセージ", value="Update from comparison tool")
|
| 137 |
push_btn = gr.Button("Push(mainブランチへ)")
|
| 138 |
push_result = gr.Textbox(label="Push結果", lines=3, interactive=False)
|
|
|
|
| 140 |
diff_btn.click(fn=run_comparison, inputs=[target_url, source_url],
|
| 141 |
outputs=[diff_checkboxes, diff_status, error_msg])
|
| 142 |
copy_btn.click(fn=copy_files, inputs=[diff_checkboxes], outputs=copy_result)
|
| 143 |
+
push_btn.click(fn=git_commit_and_push,
|
| 144 |
+
inputs=[token_input, commit_msg, user_name, user_email],
|
| 145 |
+
outputs=push_result)
|
| 146 |
|
| 147 |
demo.launch()
|