easy-worktree 0.0.4__tar.gz → 0.0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: easy-worktree
3
- Version: 0.0.4
3
+ Version: 0.0.7
4
4
  Summary: Git worktree を簡単に管理するための CLI ツール
5
5
  Project-URL: Homepage, https://github.com/igtm/easy-worktree
6
6
  Project-URL: Repository, https://github.com/igtm/easy-worktree
@@ -215,19 +215,16 @@ def get_repository_name(url: str) -> str:
215
215
 
216
216
 
217
217
  def create_hook_template(base_dir: Path):
218
- """post-add hook のテンプレートを作成"""
218
+ """post-add hook のテンプレートと .wt/ 内のファイルを作成"""
219
219
  wt_dir = base_dir / ".wt"
220
- hook_file = wt_dir / "post-add"
221
-
222
- # 既に存在する場合は何もしない
223
- if hook_file.exists():
224
- return
225
220
 
226
221
  # .wt ディレクトリを作成
227
222
  wt_dir.mkdir(exist_ok=True)
228
223
 
229
- # テンプレートを作成
230
- template = """#!/bin/bash
224
+ # post-add hook テンプレート
225
+ hook_file = wt_dir / "post-add"
226
+ if not hook_file.exists():
227
+ template = """#!/bin/bash
231
228
  # Post-add hook for easy-worktree
232
229
  # This script is automatically executed after creating a new worktree
233
230
  #
@@ -256,10 +253,186 @@ def create_hook_template(base_dir: Path):
256
253
  #
257
254
  # echo "Setup completed!"
258
255
  """
256
+ hook_file.write_text(template)
257
+ # 実行権限を付与
258
+ hook_file.chmod(0o755)
259
+
260
+ # .gitignore
261
+ gitignore_file = wt_dir / ".gitignore"
262
+ if not gitignore_file.exists():
263
+ gitignore_content = "post-add.local\n"
264
+ gitignore_file.write_text(gitignore_content)
265
+
266
+ # README.md (言語に応じて)
267
+ readme_file = wt_dir / "README.md"
268
+ if not readme_file.exists():
269
+ if is_japanese():
270
+ readme_content = """# easy-worktree フック
271
+
272
+ このディレクトリには、easy-worktree (wt コマンド) のフックスクリプトが格納されています。
273
+
274
+ ## wt コマンドとは
275
+
276
+ `wt` は Git worktree を簡単に管理するための CLI ツールです。複数のブランチで同時に作業する際に、ブランチごとに独立したディレクトリ(worktree)を作成・管理できます。
277
+
278
+ ### 基本的な使い方
279
+
280
+ ```bash
281
+ # リポジトリをクローン
282
+ wt clone <repository_url>
283
+
284
+ # 新しい worktree を作成(新規ブランチ)
285
+ wt add <作業名>
286
+
287
+ # 既存ブランチから worktree を作成
288
+ wt add <作業名> <既存ブランチ名>
289
+
290
+ # エイリアスを作成(current エイリアスで現在の作業を切り替え)
291
+ wt add <作業名> --alias current
292
+
293
+ # worktree 一覧を表示
294
+ wt list
295
+
296
+ # worktree を削除
297
+ wt rm <作業名>
298
+ ```
299
+
300
+ 詳細は https://github.com/igtm/easy-worktree を参照してください。
301
+
302
+ ## エイリアスとは
303
+
304
+ エイリアスは、worktree へのシンボリックリンク(symbolic link)です。同じエイリアス名で異なる worktree を指すことで、固定されたパスで複数のブランチを切り替えられます。
305
+
306
+ ### エイリアスの便利な使い方
307
+
308
+ **VSCode ワークスペースでの活用**
309
+
310
+ `current` などの固定エイリアスを VSCode のワークスペースとして開くことで、worktree を切り替えても VSCode を開き直す必要がなくなります。
311
+
312
+ ```bash
313
+ # 最初の作業
314
+ wt add feature-a --alias current
315
+ code current # VSCode で current を開く
316
+
317
+ # 別の作業に切り替え(VSCode は開いたまま)
318
+ wt add feature-b --alias current
319
+ # current エイリアスが feature-b を指すようになる
320
+ ```
321
+
322
+ このように、エイリアスを使うことで:
323
+ - VSCode のワークスペース設定が維持される
324
+ - 拡張機能の設定やウィンドウレイアウトが保持される
325
+ - ブランチ切り替えのたびにエディタを開き直す手間が不要
326
+
327
+ ## post-add フック
328
+
329
+ `post-add` フックは、worktree 作成後に自動実行されるスクリプトです。
330
+
331
+ ### 使用例
332
+
333
+ - 依存関係のインストール(npm install, pip install など)
334
+ - 設定ファイルのコピー(.env ファイルなど)
335
+ - ディレクトリの初期化
336
+ - VSCode ワークスペースの作成
337
+
338
+ ### 利用可能な環境変数
339
+
340
+ - `WT_WORKTREE_PATH`: 作成された worktree のパス
341
+ - `WT_WORKTREE_NAME`: worktree の名前
342
+ - `WT_BASE_DIR`: _base/ ディレクトリのパス
343
+ - `WT_BRANCH`: ブランチ名
344
+ - `WT_ACTION`: アクション名(常に "add")
345
+
346
+ ### post-add.local について
347
+
348
+ `post-add.local` は、個人用のローカルフックです。このファイルは `.gitignore` に含まれているため、リポジトリにコミットされません。チーム全体で共有したいフックは `post-add` に、個人的な設定は `post-add.local` に記述してください。
349
+
350
+ `post-add` が存在する場合のみ、`post-add.local` も自動的に実行されます。
351
+ """
352
+ else:
353
+ readme_content = """# easy-worktree Hooks
354
+
355
+ This directory contains hook scripts for easy-worktree (wt command).
356
+
357
+ ## What is wt command?
358
+
359
+ `wt` is a CLI tool for easily managing Git worktrees. When working on multiple branches simultaneously, you can create and manage independent directories (worktrees) for each branch.
360
+
361
+ ### Basic Usage
362
+
363
+ ```bash
364
+ # Clone a repository
365
+ wt clone <repository_url>
366
+
367
+ # Create a new worktree (new branch)
368
+ wt add <work_name>
369
+
370
+ # Create a worktree from an existing branch
371
+ wt add <work_name> <existing_branch_name>
259
372
 
260
- hook_file.write_text(template)
261
- # 実行権限を付与
262
- hook_file.chmod(0o755)
373
+ # Create an alias (use "current" alias to switch between tasks)
374
+ wt add <work_name> --alias current
375
+
376
+ # List worktrees
377
+ wt list
378
+
379
+ # Remove a worktree
380
+ wt rm <work_name>
381
+ ```
382
+
383
+ For more details, see https://github.com/igtm/easy-worktree
384
+
385
+ ## What are Aliases?
386
+
387
+ Aliases are symbolic links to worktrees. By pointing the same alias name to different worktrees, you can switch between multiple branches using a fixed path.
388
+
389
+ ### Smart Use of Aliases
390
+
391
+ **Using with VSCode Workspace**
392
+
393
+ By opening a fixed alias like `current` as a VSCode workspace, you can switch worktrees without needing to reopen VSCode.
394
+
395
+ ```bash
396
+ # First task
397
+ wt add feature-a --alias current
398
+ code current # Open current in VSCode
399
+
400
+ # Switch to another task (VSCode stays open)
401
+ wt add feature-b --alias current
402
+ # The current alias now points to feature-b
403
+ ```
404
+
405
+ Benefits of using aliases:
406
+ - VSCode workspace settings are preserved
407
+ - Extension settings and window layouts are maintained
408
+ - No need to reopen the editor when switching branches
409
+
410
+ ## post-add Hook
411
+
412
+ The `post-add` hook is a script that runs automatically after creating a worktree.
413
+
414
+ ### Use Cases
415
+
416
+ - Install dependencies (npm install, pip install, etc.)
417
+ - Copy configuration files (.env files, etc.)
418
+ - Initialize directories
419
+ - Create VSCode workspaces
420
+
421
+ ### Available Environment Variables
422
+
423
+ - `WT_WORKTREE_PATH`: Path to the created worktree
424
+ - `WT_WORKTREE_NAME`: Name of the worktree
425
+ - `WT_BASE_DIR`: Path to the _base/ directory
426
+ - `WT_BRANCH`: Branch name
427
+ - `WT_ACTION`: Action name (always "add")
428
+
429
+ ### About post-add.local
430
+
431
+ `post-add.local` is for personal local hooks. This file is included in `.gitignore`, so it won't be committed to the repository. Use `post-add` for hooks you want to share with the team, and `post-add.local` for your personal settings.
432
+
433
+ `post-add.local` is automatically executed only when `post-add` exists.
434
+ """
435
+ readme_file.write_text(readme_content)
263
436
 
264
437
 
265
438
  def find_base_dir() -> Path | None:
@@ -441,6 +614,24 @@ def cmd_add(args: list[str]):
441
614
  print(msg('fetching'))
442
615
  run_command(["git", "fetch", "--all"], cwd=base_dir)
443
616
 
617
+ # _base/ を base branch の最新に更新
618
+ # 現在のブランチを取得
619
+ result = run_command(
620
+ ["git", "rev-parse", "--abbrev-ref", "HEAD"],
621
+ cwd=base_dir,
622
+ check=False
623
+ )
624
+ if result.returncode == 0:
625
+ current_branch = result.stdout.strip()
626
+ # リモートブランチが存在する場合は pull
627
+ result = run_command(
628
+ ["git", "rev-parse", "--verify", f"origin/{current_branch}"],
629
+ cwd=base_dir,
630
+ check=False
631
+ )
632
+ if result.returncode == 0:
633
+ run_command(["git", "pull", "origin", current_branch], cwd=base_dir, check=False)
634
+
444
635
  # ブランチ名が指定されている場合は既存ブランチをチェックアウト
445
636
  # 指定されていない場合は新しいブランチを作成
446
637
  branch_name = None
@@ -532,16 +723,26 @@ def cmd_add(args: list[str]):
532
723
  # エイリアスを作成
533
724
  if alias_name:
534
725
  alias_path = base_dir.parent / alias_name
535
- if alias_path.exists():
536
- # 既存のエイリアスを削除して上書き
537
- if alias_path.is_symlink():
538
- alias_path.unlink()
539
- alias_path.symlink_to(worktree_path, target_is_directory=True)
540
- print(msg('alias_updated', alias_name, work_name))
541
- else:
542
- print(msg('error', f'{alias_name} exists but is not a symlink'), file=sys.stderr)
726
+
727
+ # 既存かどうかをチェック
728
+ is_updating = alias_path.is_symlink()
729
+
730
+ # 既存のシンボリックリンクを削除
731
+ if alias_path.is_symlink():
732
+ alias_path.unlink()
733
+ elif alias_path.exists():
734
+ # シンボリックリンクではないファイル/ディレクトリが存在
735
+ print(msg('error', f'{alias_name} exists but is not a symlink'), file=sys.stderr)
736
+ # post-add hook を実行
737
+ run_post_add_hook(worktree_path, work_name, base_dir, branch_name)
738
+ sys.exit(0) # worktree は作成できたので正常終了
739
+
740
+ # シンボリックリンクを作成
741
+ alias_path.symlink_to(worktree_path, target_is_directory=True)
742
+
743
+ if is_updating:
744
+ print(msg('alias_updated', alias_name, work_name))
543
745
  else:
544
- alias_path.symlink_to(worktree_path, target_is_directory=True)
545
746
  print(msg('alias_created', alias_name, work_name))
546
747
 
547
748
  # post-add hook を実行
@@ -714,6 +915,14 @@ def cmd_clean(args: list[str]):
714
915
  # worktree 情報を取得
715
916
  worktrees = get_worktree_info(base_dir)
716
917
 
918
+ # エイリアスで使われている worktree を取得
919
+ parent_dir = base_dir.parent
920
+ aliased_worktrees = set()
921
+ for item in parent_dir.iterdir():
922
+ if item.is_symlink() and item.name != '_base':
923
+ target = item.resolve()
924
+ aliased_worktrees.add(target)
925
+
717
926
  # 削除対象を抽出(_baseは除外)
718
927
  targets = []
719
928
  now = datetime.now()
@@ -725,6 +934,10 @@ def cmd_clean(args: list[str]):
725
934
  if path.name == '_base':
726
935
  continue
727
936
 
937
+ # エイリアスで使われている worktree は除外
938
+ if path in aliased_worktrees:
939
+ continue
940
+
728
941
  # clean状態のものだけが対象
729
942
  if not wt.get('is_clean'):
730
943
  continue
@@ -966,7 +1179,7 @@ def show_help():
966
1179
 
967
1180
  def show_version():
968
1181
  """Show version information"""
969
- print("easy-worktree version 0.0.4")
1182
+ print("easy-worktree version 0.0.7")
970
1183
 
971
1184
 
972
1185
  def main():
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "easy-worktree"
7
- version = "0.0.4"
7
+ version = "0.0.7"
8
8
  description = "Git worktree を簡単に管理するための CLI ツール"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
File without changes
File without changes
File without changes
File without changes