gitbetter.git

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

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

def commit(args: str):
25def commit(args: str):
26    """>>> git commit {args}"""
27    execute(f"commit {args}")
>>> git commit {args}
def add(files: list[str] | None = None):
30def add(files: list[str] | None = None):
31    """Stage a list of files.
32
33    If no files are given (`files=None`), all files will be staged."""
34    if not files:
35        execute("add .")
36    else:
37        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):
40def commit_files(files: list[str], message: str):
41    """Stage and commit a list of files with commit message `message`."""
42    add(files)
43    commit(f'-m "{message}"')

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

def initcommit():
46def initcommit():
47    """Equivalent to
48    >>> git add .
49    >>> git commit -m "Initial commit" """
50    add()
51    commit('-m "Initial commit"')

Equivalent to

>>> git add .
>>> git commit -m "Initial commit"
def amend(files: list[str] | None = None):
54def amend(files: list[str] | None = None):
55    """Stage and commit changes to the previous commit.
56
57    If `files` is `None`, all files will be staged.
58
59    Equivalent to:
60    >>> git add {files}
61    >>> git commit --amend --no-edit
62    """
63    add(files)
64    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):
67def tag(id_: str):
68    """Tag the current commit with `id_`.
69
70    Equivalent to `git tag {id_}`."""
71    execute(f"tag {id_}")

Tag the current commit with id_.

Equivalent to git tag {id_}.

def add_remote_url(url: str, name: str = 'origin'):
75def add_remote_url(url: str, name: str = "origin"):
76    """Add remote url to repo."""
77    execute(f"remote add {name} {url}")

Add remote url to repo.

def push(args: str = ''):
80def push(args: str = ""):
81    """Equivalent to `git push {args}`."""
82    execute(f"push {args}")

Equivalent to git push {args}.

def pull(args: str = ''):
85def pull(args: str = ""):
86    """Equivalent to `git pull {args}`."""
87    execute(f"pull {args}")

Equivalent to git pull {args}.

def push_new_branch(branch: str):
90def push_new_branch(branch: str):
91    """Push a new branch to origin with tracking.
92
93    Equivalent to `git push -u origin {branch}`."""
94    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):
97def pull_branch(branch: str):
98    """Pull `branch` from origin."""
99    pull(f"origin {branch}")

Pull branch from origin.

def branch(args: str):
103def branch(args: str):
104    """Equivalent to `git branch {args}`."""
105    execute(f"branch {args}")

Equivalent to git branch {args}.

def list_branches():
108def list_branches():
109    """Print a list of branches."""
110    branch("-vva")

Print a list of branches.

def checkout(args: str):
113def checkout(args: str):
114    """Equivalent to `git checkout {args}`."""
115    execute(f"checkout {args}")

Equivalent to git checkout {args}.

def switch_branch(branch_name: str):
118def switch_branch(branch_name: str):
119    """Switch to the branch specified by `branch_name`.
120
121    Equivalent to `git checkout {branch_name}`."""
122    checkout(branch_name)

Switch to the branch specified by branch_name.

Equivalent to git checkout {branch_name}.

def create_new_branch(branch_name: str):
125def create_new_branch(branch_name: str):
126    """Create and switch to a new branch named with `branch_name`.
127
128    Equivalent to `git checkout -b {branch_name} --track`."""
129    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):
132def delete_branch(branch_name: str, local_only: bool = True):
133    """Delete `branch_name` from repo.
134
135    #### :params:
136
137    `local_only`: Only delete the local copy of `branch`, otherwise also delete the remote branch on origin and remote-tracking branch."""
138    branch(f"--delete {branch_name}")
139    if not local_only:
140        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():
143def undo():
144    """Undo uncommitted changes.
145
146    Equivalent to `git checkout .`."""
147    checkout(".")

Undo uncommitted changes.

Equivalent to git checkout ..

def merge(branch_name: str):
150def merge(branch_name: str):
151    """Merge branch `branch_name` with currently active branch."""
152    execute(f"merge {branch_name}")

Merge branch branch_name with currently active branch.

def create_remote(name: str, public: bool = False):
158def create_remote(name: str, public: bool = False):
159    """Uses GitHub CLI (must be installed and configured) to create a remote GitHub repo.
160
161    #### :params:
162
163    `name`: The name for the repo.
164
165    `public`: Set to `True` to create the repo as public, otherwise it'll be created as private."""
166    visibility = "--public" if public else "--private"
167    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 make_private(owner: str, name: str):
170def make_private(owner: str, name: str):
171    """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to private.
172
173    #### :params:
174
175    `owner`: The repo owner.
176
177    `name`: The name of the repo to edit."""
178    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):
181def make_public(owner: str, name: str):
182    """Uses GitHub CLI (must be installed and configured) to set the repo's visibility to public.
183
184    #### :params:
185
186    `owner`: The repo owner.
187
188    `name`: The name of the repo to edit."""
189    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):
192def delete_remote(owner: str, name: str):
193    """Uses GitHub CLI (must be isntalled and configured) to delete the remote for this repo.
194
195    #### :params:
196
197    `owner`: The repo owner.
198
199    `name`: The name of the remote repo to delete."""
200    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.