Эх сурвалжийг харах

git-branchp: support git-branchless

raylu 9 сар өмнө
parent
commit
c60143d1c5
1 өөрчлөгдсөн 15 нэмэгдсэн , 5 устгасан
  1. 15 5
      git-branchp

+ 15 - 5
git-branchp

@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
 # vim: set noet sw=4 ts=4:
 
+import pathlib
 import subprocess
 import sys
 
@@ -18,16 +19,18 @@ def main():
 	print()
 
 	if delete and len(to_delete) > 0:
-		subprocess.run([b'git', b'branch', b'-D'] + to_delete, check=True)
+		if has_branchless():
+			run([b'git', b'hide']  + [b'draft()::' + branch for branch in to_delete])
+		else:
+			run([b'git' b'branch', b'-D'] + to_delete)
 
 def all_remotes():
-	refs = subprocess.run(['git', 'for-each-ref', '--format=%(refname:short)', 'refs/remotes'],
-			capture_output=True, check=True)
+	refs = run(['git', 'for-each-ref', '--format=%(refname:short)', 'refs/remotes'], capture_output=True)
 	return frozenset(refs.stdout.rstrip(b'\n').split(b'\n'))
 
 def iter_locals():
-	refs = subprocess.run(['git', 'for-each-ref', '--format=%(refname:short) %(upstream)', 'refs/heads'],
-			capture_output=True, check=True)
+	refs = run(['git', 'for-each-ref', '--format=%(refname:short) %(upstream)', 'refs/heads'],
+			capture_output=True)
 	for line in refs.stdout.split(b'\n'):
 		if not line:
 			continue
@@ -38,5 +41,12 @@ def iter_locals():
 		remote = remote.removeprefix(b'refs/remotes/')
 		yield local, remote
 
+def has_branchless() -> bool:
+	repo_root = run(['git', 'rev-parse', '--show-toplevel'], capture_output=True, text=True).stdout.rstrip('\n')
+	return pathlib.Path(repo_root, '.git/branchless/config').is_file()
+
+def run(cmd, capture_output=False, text=None):
+	return subprocess.run(cmd, capture_output=capture_output, check=True, text=text)
+
 if __name__ == '__main__':
 	main()