Browse Source

cli: add basic branch management commands (#1514)

This branch / pull request prepared using these commands (except for
2e1791b1e50e05a6332434a3a6f6f25319230989,
17780fff50f32f15ab56939f3c74ca832d568326,
cd0450a3e7608b76fcfb623120656a01fdc6f780 and
04136969ac29f3f82a10d9a3c0faec4ab231afc1 that were committed -- but not
pushed -- using the `git-scm` tooling).

NB: the `reset` command, despite being modified here to accept a
treeish, doesn't appear to reset the HEAD commit correctly (`dulwich
log` continues to report from the pre-reset commit, instead of the
expected resetted-to treeish).
Jelmer Vernooij 5 days ago
parent
commit
50e4f96bda
1 changed files with 62 additions and 1 deletions
  1. 62 1
      dulwich/cli.py

+ 62 - 1
dulwich/cli.py

@@ -429,7 +429,11 @@ class cmd_reset(Command):
             mode = "soft"
         elif "--mixed" in kwopts:
             mode = "mixed"
-        porcelain.reset(".", mode=mode)
+        try:
+            treeish = args.pop(0)
+        except IndexError:
+            treeish = None
+        porcelain.reset(".", mode=mode, treeish=treeish)
 
 
 class cmd_daemon(Command):
@@ -729,6 +733,61 @@ class cmd_check_mailmap(Command):
             print(canonical_identity)
 
 
+class cmd_branch(Command):
+    def run(self, args) -> None:
+        parser = argparse.ArgumentParser()
+        parser.add_argument(
+            "branch",
+            type=str,
+            help="Name of the branch",
+        )
+        parser.add_argument(
+            "-d",
+            "--delete",
+            action="store_true",
+            help="Delete branch",
+        )
+        args = parser.parse_args(args)
+        if not args.branch:
+            print("Usage: dulwich branch [-d] BRANCH_NAME")
+            sys.exit(1)
+
+        if args.delete:
+            porcelain.branch_delete(".", name=args.branch)
+        else:
+            try:
+                porcelain.branch_create(".", name=args.branch)
+            except porcelain.Error as e:
+                sys.stderr.write(f"{e}")
+                sys.exit(1)
+
+
+class cmd_checkout(Command):
+    def run(self, args) -> None:
+        parser = argparse.ArgumentParser()
+        parser.add_argument(
+            "branch",
+            type=str,
+            help="Name of the branch",
+        )
+        parser.add_argument(
+            "-f",
+            "--force",
+            action="store_true",
+            help="Force checkout",
+        )
+        args = parser.parse_args(args)
+        if not args.branch:
+            print("Usage: dulwich checkout BRANCH_NAME [--force]")
+            sys.exit(1)
+
+        try:
+            porcelain.checkout_branch(".", target=args.branch, force=args.force)
+        except porcelain.CheckoutError as e:
+            sys.stderr.write(f"{e}\n")
+            sys.exit(1)
+
+
 class cmd_stash_list(Command):
     def run(self, args) -> None:
         parser = optparse.OptionParser()
@@ -806,8 +865,10 @@ For a list of supported commands, see 'dulwich help -a'.
 commands = {
     "add": cmd_add,
     "archive": cmd_archive,
+    "branch": cmd_branch,
     "check-ignore": cmd_check_ignore,
     "check-mailmap": cmd_check_mailmap,
+    "checkout": cmd_checkout,
     "clone": cmd_clone,
     "commit": cmd_commit,
     "commit-tree": cmd_commit_tree,