gitbetter.git

  1import os
  2
  3from pathier import Pathier
  4
  5
  6def execute(command: str) -> int:
  7    """Execute git command.
  8
  9    Equivalent to `os.system(f"git {command}")`
 10
 11    Returns the output of the `os.system` call."""
 12    return os.system(f"git {command}")
 13
 14
 15def new_repo():
 16    """>>> git init -b main"""
 17    execute("init -b main")
 18
 19
 20def loggy():
 21    """Equivalent to `git log --oneline --name-only --abbrev-commit --graph`."""
 22    execute("log --oneline --name-only --abbrev-commit --graph")
 23
 24
 25def status():
 26    """Execute `git status`."""
 27    execute("status")
 28
 29
 30# ======================================Staging/Committing======================================
 31def commit(args: str):
 32    """>>> git commit {args}"""
 33    execute(f"commit {args}")
 34
 35
 36def add(files: list[str] | None = None):
 37    """Stage a list of files.
 38
 39    If no files are given (`files=None`), all files will be staged."""
 40    if not files:
 41        execute("add .")
 42    else:
 43        execute(f'add {" ".join(files)}')
 44
 45
 46def commit_files(files: list[str], message: str):
 47    """Stage and commit a list of files with commit message `message`."""
 48    add(files)
 49    commit(f'-m "{message}"')
 50
 51
 52def initcommit():
 53    """Equivalent to
 54    >>> git add .
 55    >>> git commit -m "Initial commit" """
 56    add()
 57    commit('-m "Initial commit"')
 58
 59
 60def amend(files: list[str] | None = None):
 61    """Stage and commit changes to the previous commit.
 62
 63    If `files` is `None`, all files will be staged.
 64
 65    Equivalent to:
 66    >>> git add {files}
 67    >>> git commit --amend --no-edit
 68    """
 69    add(files)
 70    commit("--amend --no-edit")
 71
 72
 73def tag(id_: str):
 74    """Tag the current commit with `id_`.
 75
 76    Equivalent to `git tag {id_}`."""
 77    execute(f"tag {id_}")
 78
 79
 80# ==========================================Push/Pull==========================================
 81def add_remote_url(url: str, name: str = "origin"):
 82    """Add remote url to repo."""
 83    execute(f"remote add {name} {url}")
 84
 85
 86def push(args: str = ""):
 87    """Equivalent to `git push {args}`."""
 88    execute(f"push {args}")
 89
 90
 91def pull(args: str = ""):
 92    """Equivalent to `git pull {args}`."""
 93    execute(f"pull {args}")
 94
 95
 96def push_new_branch(branch: str):
 97    """Push a new branch to origin with tracking.
 98
 99    Equivalent to `git push -u origin {branch}`."""
100    push(f"-u origin {branch}")
101
102
103def pull_branch(branch: str):
104    """Pull `branch` from origin."""
105    pull(f"origin {branch}")
106
107
108# ============================================Checkout/Branches============================================
109def branch(args: str):
110    """Equivalent to `git branch {args}`."""
111    execute(f"branch {args}")
112
113
114def list_branches():
115    """Print a list of branches."""
116    branch("-vva")
117
118
119def checkout(args: str):
120    """Equivalent to `git checkout {args}`."""
121    execute(f"checkout {args}")
122
123
124def switch_branch(branch_name: str):
125    """Switch to the branch specified by `branch_name`.
126
127    Equivalent to `git checkout {branch_name}`."""
128    checkout(branch_name)
129
130
131def create_new_branch(branch_name: str):
132    """Create and switch to a new branch named with `branch_name`.
133
134    Equivalent to `git checkout -b {branch_name} --track`."""
135    checkout(f"-b {branch_name} --track")
136
137
138def delete_branch(branch_name: str, local_only: bool = True):
139    """Delete `branch_name` from repo.
140
141    #### :params:
142
143    `local_only`: Only delete the local copy of `branch`, otherwise also delete the remote branch on origin and remote-tracking branch."""
144    branch(f"--delete {branch_name}")
145    if not local_only:
146        push(f"origin --delete {branch_name}")
147
148
149def undo():
150    """Undo uncommitted changes.
151
152    Equivalent to `git checkout .`."""
153    checkout(".")
154
155
156def merge(branch_name: str):
157    """Merge branch `branch_name` with currently active branch."""
158    execute(f"merge {branch_name}")
159
160
161# ===============================Requires GitHub CLI to be installed and configured===============================
162
163
164def create_remote(name: str, public: bool = False):
165    """Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo.
166
167    #### :params:
168
169    `name`: The name for the repo.
170
171    `public`: Set to `True` to create the repo as public, otherwise it'll be created as private."""
172    visibility = "--public" if public else "--private"
173    os.system(f"gh repo create {name} {visibility}")
174
175
176def create_remote_from_cwd(public: bool = False):
177    """Use GitHub CLI (must be installed and configured) to create a remote GitHub repo from
178    the current working directory repo and add its url as this repo's remote origin.
179
180    #### :params:
181
182    `public`: Create the GitHub repo as a public repo, default is to create it as private."""
183    visibility = "public" if public else "private"
184    os.system(f"gh repo create --source . --{visibility} --push")
185
186
187def make_private(owner: str, name: str):
188    """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private.
189
190    #### :params:
191
192    `owner`: The repo owner.
193
194    `name`: The name of the repo to edit."""
195    os.system(f"gh repo edit {owner}/{name} --visibility private")
196
197
198def make_public(owner: str, name: str):
199    """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public.
200
201    #### :params:
202
203    `owner`: The repo owner.
204
205    `name`: The name of the repo to edit."""
206    os.system(f"gh repo edit {owner}/{name} --visibility public")
207
208
209def delete_remote(owner: str, name: str):
210    """Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo.
211
212    #### :params:
213
214    `owner`: The repo owner.
215
216    `name`: The name of the remote repo to delete."""
217    os.system(f"gh repo delete {owner}/{name} --yes")
def execute(command: str) -> int:
 7def execute(command: str) -> int:
 8    """Execute git command.
 9
10    Equivalent to `os.system(f"git {command}")`
11
12    Returns the output of the `os.system` call."""
13    return os.system(f"git {command}")

Execute git command.

Equivalent to os.system(f"git {command}")

Returns the output of the os.system call.

def new_repo():
16def new_repo():
17    """>>> git init -b main"""
18    execute("init -b main")
>>> git init -b main
def loggy():
21def loggy():
22    """Equivalent to `git log --oneline --name-only --abbrev-commit --graph`."""
23    execute("log --oneline --name-only --abbrev-commit --graph")

Equivalent to git log --oneline --name-only --abbrev-commit --graph.

def status():
26def status():
27    """Execute `git status`."""
28    execute("status")

Execute git status.

def commit(args: str):
32def commit(args: str):
33    """>>> git commit {args}"""
34    execute(f"commit {args}")
>>> git commit {args}
def add(files: list[str] | None = None):
37def add(files: list[str] | None = None):
38    """Stage a list of files.
39
40    If no files are given (`files=None`), all files will be staged."""
41    if not files:
42        execute("add .")
43    else:
44        execute(f'add {" ".join(files)}')

Stage a list of files.

If no files are given (files=None), all files will be staged.

def commit_files(files: list[str], message: str):
47def commit_files(files: list[str], message: str):
48    """Stage and commit a list of files with commit message `message`."""
49    add(files)
50    commit(f'-m "{message}"')

Stage and commit a list of files with commit message message.

def initcommit():
53def initcommit():
54    """Equivalent to
55    >>> git add .
56    >>> git commit -m "Initial commit" """
57    add()
58    commit('-m "Initial commit"')

Equivalent to

>>> git add .
>>> git commit -m "Initial commit"
def amend(files: list[str] | None = None):
61def amend(files: list[str] | None = None):
62    """Stage and commit changes to the previous commit.
63
64    If `files` is `None`, all files will be staged.
65
66    Equivalent to:
67    >>> git add {files}
68    >>> git commit --amend --no-edit
69    """
70    add(files)
71    commit("--amend --no-edit")

Stage and commit changes to the previous commit.

If files is None, all files will be staged.

Equivalent to:

>>> git add {files}
>>> git commit --amend --no-edit
def tag(id_: str):
74def tag(id_: str):
75    """Tag the current commit with `id_`.
76
77    Equivalent to `git tag {id_}`."""
78    execute(f"tag {id_}")

Tag the current commit with id_.

Equivalent to git tag {id_}.

def add_remote_url(url: str, name: str = 'origin'):
82def add_remote_url(url: str, name: str = "origin"):
83    """Add remote url to repo."""
84    execute(f"remote add {name} {url}")

Add remote url to repo.

def push(args: str = ''):
87def push(args: str = ""):
88    """Equivalent to `git push {args}`."""
89    execute(f"push {args}")

Equivalent to git push {args}.

def pull(args: str = ''):
92def pull(args: str = ""):
93    """Equivalent to `git pull {args}`."""
94    execute(f"pull {args}")

Equivalent to git pull {args}.

def push_new_branch(branch: str):
 97def push_new_branch(branch: str):
 98    """Push a new branch to origin with tracking.
 99
100    Equivalent to `git push -u origin {branch}`."""
101    push(f"-u origin {branch}")

Push a new branch to origin with tracking.

Equivalent to git push -u origin {branch}.

def pull_branch(branch: str):
104def pull_branch(branch: str):
105    """Pull `branch` from origin."""
106    pull(f"origin {branch}")

Pull branch from origin.

def branch(args: str):
110def branch(args: str):
111    """Equivalent to `git branch {args}`."""
112    execute(f"branch {args}")

Equivalent to git branch {args}.

def list_branches():
115def list_branches():
116    """Print a list of branches."""
117    branch("-vva")

Print a list of branches.

def checkout(args: str):
120def checkout(args: str):
121    """Equivalent to `git checkout {args}`."""
122    execute(f"checkout {args}")

Equivalent to git checkout {args}.

def switch_branch(branch_name: str):
125def switch_branch(branch_name: str):
126    """Switch to the branch specified by `branch_name`.
127
128    Equivalent to `git checkout {branch_name}`."""
129    checkout(branch_name)

Switch to the branch specified by branch_name.

Equivalent to git checkout {branch_name}.

def create_new_branch(branch_name: str):
132def create_new_branch(branch_name: str):
133    """Create and switch to a new branch named with `branch_name`.
134
135    Equivalent to `git checkout -b {branch_name} --track`."""
136    checkout(f"-b {branch_name} --track")

Create and switch to a new branch named with branch_name.

Equivalent to git checkout -b {branch_name} --track.

def delete_branch(branch_name: str, local_only: bool = True):
139def delete_branch(branch_name: str, local_only: bool = True):
140    """Delete `branch_name` from repo.
141
142    #### :params:
143
144    `local_only`: Only delete the local copy of `branch`, otherwise also delete the remote branch on origin and remote-tracking branch."""
145    branch(f"--delete {branch_name}")
146    if not local_only:
147        push(f"origin --delete {branch_name}")

Delete branch_name from repo.

:params:

local_only: Only delete the local copy of branch, otherwise also delete the remote branch on origin and remote-tracking branch.

def undo():
150def undo():
151    """Undo uncommitted changes.
152
153    Equivalent to `git checkout .`."""
154    checkout(".")

Undo uncommitted changes.

Equivalent to git checkout ..

def merge(branch_name: str):
157def merge(branch_name: str):
158    """Merge branch `branch_name` with currently active branch."""
159    execute(f"merge {branch_name}")

Merge branch branch_name with currently active branch.

def create_remote(name: str, public: bool = False):
165def create_remote(name: str, public: bool = False):
166    """Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo.
167
168    #### :params:
169
170    `name`: The name for the repo.
171
172    `public`: Set to `True` to create the repo as public, otherwise it'll be created as private."""
173    visibility = "--public" if public else "--private"
174    os.system(f"gh repo create {name} {visibility}")

Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo.

:params:

name: The name for the repo.

public: Set to True to create the repo as public, otherwise it'll be created as private.

def create_remote_from_cwd(public: bool = False):
177def create_remote_from_cwd(public: bool = False):
178    """Use GitHub CLI (must be installed and configured) to create a remote GitHub repo from
179    the current working directory repo and add its url as this repo's remote origin.
180
181    #### :params:
182
183    `public`: Create the GitHub repo as a public repo, default is to create it as private."""
184    visibility = "public" if public else "private"
185    os.system(f"gh repo create --source . --{visibility} --push")

Use GitHub CLI (must be installed and configured) to create a remote GitHub repo from the current working directory repo and add its url as this repo's remote origin.

:params:

public: Create the GitHub repo as a public repo, default is to create it as private.

def make_private(owner: str, name: str):
188def make_private(owner: str, name: str):
189    """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private.
190
191    #### :params:
192
193    `owner`: The repo owner.
194
195    `name`: The name of the repo to edit."""
196    os.system(f"gh repo edit {owner}/{name} --visibility private")

Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private.

:params:

owner: The repo owner.

name: The name of the repo to edit.

def make_public(owner: str, name: str):
199def make_public(owner: str, name: str):
200    """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public.
201
202    #### :params:
203
204    `owner`: The repo owner.
205
206    `name`: The name of the repo to edit."""
207    os.system(f"gh repo edit {owner}/{name} --visibility public")

Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public.

:params:

owner: The repo owner.

name: The name of the repo to edit.

def delete_remote(owner: str, name: str):
210def delete_remote(owner: str, name: str):
211    """Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo.
212
213    #### :params:
214
215    `owner`: The repo owner.
216
217    `name`: The name of the remote repo to delete."""
218    os.system(f"gh repo delete {owner}/{name} --yes")

Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo.

:params:

owner: The repo owner.

name: The name of the remote repo to delete.