33from typing import Iterable
44from pathlib import Path
55import random
6- from ..utils import run_cmd
7-
8-
9- class FailToPushToGitHubException (Exception ):
10- """Exception for failure to push a branch to GitHub."""
11-
12- def __init__ (self , branch : str , branch_alt : str ):
13- msg = f"Failed to push the branch { branch } to GitHub!"
14- if branch_alt :
15- msg += f" Pushed to { branch_alt } instead."
16- super ().__init__ (msg )
6+ from dulwich import porcelain
7+ from dulwich .repo import Repo
178
189
1910def config_git (local_repo_dir : str | Path , user_email : str , user_name : str ):
@@ -22,18 +13,9 @@ def config_git(local_repo_dir: str | Path, user_email: str, user_name: str):
2213 :param user_email: The email of the user (no need to be a valid one).
2314 :param user_name: The name of the user.
2415 """
25- cmd = f"""git config --global --add safe.directory { local_repo_dir } \
26- && git config --global user.email "{ user_email } " \
27- && git config --global user.name "{ user_name } "
28- """
29- run_cmd (cmd )
30-
31-
32- def create_branch (branch : str ) -> None :
33- """Create a new local branch.
34- :param branch: The new local branch to create.
35- """
36- run_cmd (f"git checkout -b { branch } " )
16+ config = Repo (local_repo_dir ).get_config ()
17+ config .set (b"user" , b"email" , user_email .encode ())
18+ config .set (b"user" , b"name" , user_name .encode ())
3719
3820
3921def switch_branch (branch : str , fetch : bool ) -> None :
@@ -42,8 +24,8 @@ def switch_branch(branch: str, fetch: bool) -> None:
4224 :param fetch: If true, fetch the branch from remote first.
4325 """
4426 if fetch :
45- run_cmd ( f"git fetch origin { branch } " )
46- run_cmd ( f"git checkout { branch } " )
27+ porcelain . fetch ( repo = ". " )
28+ porcelain . checkout ( repo = "." , target = branch )
4729
4830
4931def gen_temp_branch (
@@ -67,26 +49,27 @@ def push_branch(branch: str, branch_alt: str = ""):
6749 :param branch_alt: An alternative branch name to push to GitHub.
6850 """
6951 try :
70- run_cmd ( f"git push origin { branch } " )
52+ porcelain . push ( repo = "." , refspecs = branch )
7153 except Exception as err :
7254 if branch_alt :
73- cmd = f"""git checkout { branch } \
74- && git checkout -b { branch_alt } \
75- && git push origin { branch_alt }
76- """
77- run_cmd (cmd )
78- raise FailToPushToGitHubException (branch , branch_alt ) from err
55+ porcelain .checkout (repo = "." , target = branch )
56+ porcelain .checkout (repo = "." , new_branch = branch_alt )
57+ porcelain .push (repo = "." , refspecs = branch_alt )
58+ else :
59+ raise err
7960
8061
8162def commit_benchmarks (bench_dir : str | Path ):
8263 """Commit changes in the benchmark directory.
8364 :param bench_dir: The benchmark directory.
8465 """
85- run_cmd (f"git add { bench_dir } && git commit -m 'add benchmarks'" )
66+ porcelain .add (paths = bench_dir )
67+ porcelain .commit (message = "Add benchmarks." )
8668
8769
8870def commit_profiling (prof_dir : str | Path ):
8971 """Commit changes in the profiling directory.
9072 :param prof_dir: The profiling directory.
9173 """
92- run_cmd (f"git add { prof_dir } && git commit -m 'update profiling results'" )
74+ porcelain .add (paths = prof_dir )
75+ porcelain .commit (message = "Updating profiling results." )
0 commit comments