tgit 0.4.2__tar.gz → 0.4.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tgit
3
- Version: 0.4.2
3
+ Version: 0.4.3
4
4
  Summary: Tool for Git Interaction Temptation (tgit): An elegant CLI tool that simplifies and streamlines your Git workflow, making version control a breeze.
5
5
  License: MIT
6
6
  Keywords: git,tool,changelog,version,commit
@@ -24,6 +24,8 @@ Description-Content-Type: text/markdown
24
24
 
25
25
  # tgit
26
26
 
27
+ [![CodeTime Badge](https://img.shields.io/endpoint?style=social&color=222&url=https%3A%2F%2Fapi.codetime.dev%2Fshield%3Fid%3D2%26project%3Dtgit%26in=0)](https://codetime.dev)
28
+
27
29
  Tool for managing git repositories.
28
30
 
29
31
  ## Installation
tgit-0.4.3/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # tgit
2
+
3
+ [![CodeTime Badge](https://img.shields.io/endpoint?style=social&color=222&url=https%3A%2F%2Fapi.codetime.dev%2Fshield%3Fid%3D2%26project%3Dtgit%26in=0)](https://codetime.dev)
4
+
5
+ Tool for managing git repositories.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install tgit
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```bash
16
+ tgit --help
17
+ ```
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "tgit"
3
- version = "0.4.2"
3
+ version = "0.4.3"
4
4
  description = "Tool for Git Interaction Temptation (tgit): An elegant CLI tool that simplifies and streamlines your Git workflow, making version control a breeze."
5
5
  authors = ["Jannchie <jannchie@gmail.com>"]
6
6
  readme = "README.md"
@@ -1,10 +1,10 @@
1
- import difflib
2
1
  import os
3
2
  import re
4
3
  import subprocess
5
4
  import tomllib
6
5
  from copy import deepcopy
7
6
  from dataclasses import dataclass
7
+ from difflib import Differ
8
8
  from typing import Optional
9
9
 
10
10
  import inquirer
@@ -124,18 +124,29 @@ def get_prev_version():
124
124
 
125
125
 
126
126
  def handle_version(args: VersionArgs):
127
-
128
127
  verbose = args.verbose
129
128
 
130
- # check if there is uncommitted changes
131
- # status = subprocess.run(["git", "status", "--porcelain"], capture_output=True)
132
- # if status.returncode != 0:
133
- # console.print("Error getting git status")
134
- # return
135
- # if status.stdout:
136
- # console.print("There are uncommitted changes, please commit or stash them first")
137
- # return
129
+ # 注释掉的部分
130
+ # check_uncommitted_changes(verbose)
131
+
132
+ prev_version = get_current_version(verbose)
133
+ if next_version := get_next_version(args, prev_version, verbose):
134
+ update_version_files(next_version, verbose)
135
+ execute_git_commands(args, next_version, verbose)
136
+
137
+
138
+ def check_uncommitted_changes(verbose: int):
139
+ status = subprocess.run(["git", "status", "--porcelain"], capture_output=True)
140
+ if status.returncode != 0:
141
+ console.print("Error getting git status")
142
+ return False
143
+ if status.stdout:
144
+ console.print("There are uncommitted changes, please commit or stash them first")
145
+ return False
146
+ return True
138
147
 
148
+
149
+ def get_current_version(verbose: int) -> Optional[Version]:
139
150
  if verbose > 0:
140
151
  console.print("Bumping version...")
141
152
  console.print("Getting current version...")
@@ -143,7 +154,10 @@ def handle_version(args: VersionArgs):
143
154
  prev_version = get_prev_version()
144
155
 
145
156
  console.print(f"Previous version: [cyan bold]{prev_version}")
146
- # get next version
157
+ return prev_version
158
+
159
+
160
+ def get_next_version(args, prev_version, verbose):
147
161
  next_version = deepcopy(prev_version)
148
162
  if not any([args.version, args.patch, args.minor, args.major, args.prepatch, args.preminor, args.premajor]):
149
163
  ans = inquirer.prompt(
@@ -167,174 +181,162 @@ def handle_version(args: VersionArgs):
167
181
  console.print(f"Selected target: [cyan bold]{target}")
168
182
 
169
183
  # bump the version
170
- if target.bump in ["patch", "prepatch"]:
171
- next_version.patch += 1
172
- elif target.bump in ["minor", "preminor"]:
173
- next_version.minor += 1
174
- next_version.patch = 0
175
- elif target.bump in ["major", "premajor"]:
176
- next_version.major += 1
177
- next_version.minor = 0
178
- next_version.patch = 0
184
+ bump_version(target, next_version)
179
185
 
180
186
  if target.bump in ["prepatch", "preminor", "premajor"]:
181
- ans = inquirer.prompt(
182
- [
183
- inquirer.Text(
184
- "identifier",
185
- message="Enter the pre-release identifier",
186
- default="alpha",
187
- validate=lambda _, x: re.match(r"[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*", x).group() == x,
188
- )
189
- ]
190
- )
191
- if not ans:
187
+ if release := get_pre_release_identifier():
188
+ next_version.release = release
189
+ else:
192
190
  return
193
- release = ans["identifier"]
194
- next_version.release = release
195
191
  if target.bump == "custom":
192
+ if custom_version := get_custom_version():
193
+ next_version = custom_version
194
+ else:
195
+ return
196
+ return next_version
197
+
198
+
199
+ def bump_version(target, next_version):
200
+ if target.bump in ["patch", "prepatch"]:
201
+ next_version.patch += 1
202
+ elif target.bump in ["minor", "preminor"]:
203
+ next_version.minor += 1
204
+ next_version.patch = 0
205
+ elif target.bump in ["major", "premajor"]:
206
+ next_version.major += 1
207
+ next_version.minor = 0
208
+ next_version.patch = 0
209
+
210
+
211
+ def get_pre_release_identifier() -> Optional[str]:
212
+ ans = inquirer.prompt(
213
+ [
214
+ inquirer.Text(
215
+ "identifier",
216
+ message="Enter the pre-release identifier",
217
+ default="alpha",
218
+ validate=lambda _, x: re.match(r"[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*", x).group() == x,
219
+ )
220
+ ]
221
+ )
222
+ return ans["identifier"] if ans else None
223
+
224
+
225
+ def get_custom_version() -> Optional[Version]:
226
+ def validate_semver(_, x):
227
+ res = semver_regex.match(x)
228
+ return res and res.group() == x
229
+
230
+ ans = inquirer.prompt(
231
+ [
232
+ inquirer.Text(
233
+ "version",
234
+ message="Enter the version",
235
+ validate=validate_semver,
236
+ )
237
+ ]
238
+ )
239
+ if not ans:
240
+ return None
241
+ version = ans["version"]
242
+ return Version.from_str(version)
243
+
244
+
245
+ def update_version_files(next_version: Version, verbose: int):
246
+ next_version_str = str(next_version)
247
+
248
+ if verbose > 0:
249
+ current_path = os.getcwd()
250
+ console.print(f"Current path: [cyan bold]{current_path}")
251
+
252
+ update_file("package.json", r'"version":\s*".*?"', f'"version": "{next_version_str}"', verbose)
253
+ update_file("pyproject.toml", r'version\s*=\s*".*?"', f'version = "{next_version_str}"', verbose)
254
+ update_file("setup.py", r"version=['\"].*?['\"]", f"version='{next_version_str}'", verbose)
255
+ update_file("Cargo.toml", r'version\s*=\s*".*?"', f'version = "{next_version_str}"', verbose)
256
+ update_file("VERSION", None, next_version_str, verbose)
257
+ update_file("VERSION.txt", None, next_version_str, verbose)
196
258
 
197
- def validate_semver(_, x):
198
- res = semver_regex.match(x)
199
- return res and res.group() == x
200
-
201
- ans = inquirer.prompt(
202
- [
203
- inquirer.Text(
204
- "version",
205
- message="Enter the version",
206
- validate=validate_semver,
207
- )
208
- ]
259
+
260
+ def update_file(filename: str, search_pattern: Optional[str], replace_text: str, verbose: int, show_diff: bool = True):
261
+ if not os.path.exists(filename):
262
+ return
263
+ if verbose > 0:
264
+ console.print(f"Updating {filename}")
265
+ with open(filename, "r") as f:
266
+ content = f.read()
267
+ new_content = re.sub(search_pattern, replace_text, content) if search_pattern else replace_text
268
+ if show_diff:
269
+ show_file_diff(content, new_content, filename)
270
+ with open(filename, "w") as f:
271
+ f.write(new_content)
272
+
273
+
274
+ def show_file_diff(old_content: str, new_content: str, filename: str):
275
+ old_lines = old_content.splitlines()
276
+ new_lines = new_content.splitlines()
277
+ diff = list(Differ().compare(old_lines, new_lines))
278
+ print_lines = {}
279
+ for i, line in enumerate(diff):
280
+ if line.startswith("+") or line.startswith("-"):
281
+ for j in range(i - 3, i + 3):
282
+ if j >= 0 and j < len(diff):
283
+ print_lines[j] = diff[j][0]
284
+
285
+ diffs = []
286
+ for i, line in enumerate(diff):
287
+ line = line.replace("[", "\\[")
288
+ if i in print_lines:
289
+ if print_lines[i] == "+":
290
+ diffs.append(f"[green]{line}[/green]")
291
+ elif print_lines[i] == "-":
292
+ diffs.append(f"[red]{line}[/red]")
293
+ elif print_lines[i] == "?":
294
+ line = line.replace("?", " ")
295
+ line = line.replace("\n", "")
296
+ diffs.append(f"[yellow]{line}[/yellow]")
297
+ else:
298
+ diffs.append(line)
299
+ if diffs:
300
+ console.print(
301
+ Panel.fit(
302
+ "\n".join(diffs),
303
+ border_style="cyan",
304
+ title=f"Diff for {filename}",
305
+ title_align="left",
306
+ padding=(1, 4),
209
307
  )
210
- version = ans["version"]
211
- next_version = Version.from_str(version)
212
- next_version_str = str(next_version)
308
+ )
309
+
310
+ ok = inquirer.prompt([inquirer.Confirm("continue", message="Do you want to continue?", default=True)])
311
+ if not ok or not ok["continue"]:
312
+ exit()
213
313
 
214
- # edit files
215
314
 
315
+ def execute_git_commands(args: VersionArgs, next_version: Version, verbose: int):
316
+ git_tag = f"v{next_version}"
317
+
318
+ commands = []
319
+ if args.no_commit:
216
320
  if verbose > 0:
217
- current_path = os.getcwd()
218
- console.print(f"Current path: [cyan bold]{current_path}")
219
-
220
- # check package.json
221
- if os.path.exists("package.json"):
222
- if verbose > 0:
223
- console.print("Updating package.json")
224
- with open("package.json", "r") as f:
225
- package_json = f.read()
226
- package_json = re.sub(r'"version":\s*".*?"', f'"version": "{next_version_str}"', package_json)
227
-
228
- with open("package.json", "w") as f:
229
- f.write(package_json)
230
-
231
- if os.path.exists("pyproject.toml"):
232
- if verbose > 0:
233
- console.print("Updating pyproject.toml")
234
- with open("pyproject.toml", "r") as f:
235
- pyproject_toml = f.read()
236
- new_pyproject_toml = re.sub(r'version\s*=\s*".*?"', f'version = "{next_version_str}"', pyproject_toml)
237
- # print diff between the two files
238
- old_lines = pyproject_toml.splitlines()
239
- new_lines = new_pyproject_toml.splitlines()
240
- diff = list(difflib.Differ().compare(old_lines, new_lines))
241
- print_lines = {}
242
- for i, line in enumerate(diff):
243
- if line.startswith("+") or line.startswith("-"):
244
- # console.print(f"[green]{line}")
245
- for j in range(i - 3, i + 3):
246
- if j >= 0 and j < len(diff):
247
- print_lines[j] = diff[j][0]
248
-
249
- diffs = []
250
- for i, line in enumerate(diff):
251
- line = line.replace("[", "\\[")
252
- line = line.strip()
253
- if i in print_lines:
254
- if print_lines[i] == "+":
255
- diffs.append(f"[green]{line}[/green]")
256
- elif print_lines[i] == "-":
257
- diffs.append(f"[red]{line}[/red]")
258
- elif print_lines[i] == "?":
259
- # replace the ? with a space
260
- line = line.replace("?", " ")
261
- diffs.append(f"[yellow]{line}[/yellow]")
262
- else:
263
- diffs.append(line)
264
- if diffs:
265
- console.print(
266
- Panel.fit(
267
- "\n".join(diffs),
268
- border_style="cyan",
269
- title="Diff for pyproject.toml",
270
- title_align="left",
271
- padding=(1, 4),
272
- )
273
- )
274
-
275
- ok = inquirer.prompt([inquirer.Confirm("continue", message="Do you want to continue?", default=True)])
276
- if not ok or not ok["continue"]:
277
- return
278
-
279
- with open("pyproject.toml", "w") as f:
280
- f.write(new_pyproject_toml)
281
-
282
- if os.path.exists("setup.py"):
283
- if verbose > 0:
284
- console.print("Updating setup.py")
285
- with open("setup.py", "r") as f:
286
- setup_py = f.read()
287
- new_setup_py = re.sub(r"version=['\"].*?['\"]", f"version='{next_version_str}'", setup_py)
288
- with open("setup.py", "w") as f:
289
- f.write(new_setup_py)
290
-
291
- if os.path.exists("Cargo.toml"):
292
- if verbose > 0:
293
- console.print("Updating Cargo.toml")
294
- with open("Cargo.toml", "r") as f:
295
- cargo_toml = f.read()
296
- new_cargo_toml = re.sub(r"version\s*=\s*\".*?\"", f'version = "{next_version_str}"', cargo_toml)
297
- with open("Cargo.toml", "w") as f:
298
- f.write(new_cargo_toml)
299
-
300
- if os.path.exists("VERSION"):
301
- if verbose > 0:
302
- console.print("Updating VERSION")
303
- with open("VERSION", "w") as f:
304
- f.write(next_version_str)
305
-
306
- if os.path.exists("VERSION.txt"):
307
- if verbose > 0:
308
- console.print("Updating VERSION.txt")
309
- with open("VERSION.txt", "w") as f:
310
- f.write(next_version_str)
311
-
312
- git_tag = f"v{next_version_str}"
313
-
314
- commands = []
315
- if args.no_commit:
316
- if verbose > 0:
317
- console.print("Skipping commit")
318
- else:
319
- commands.append("git add .")
320
- use_emoji = settings.get("commit", {}).get("emoji", False)
321
- commands.append(get_commit_command("version", None, f"{git_tag}", use_emoji=use_emoji))
321
+ console.print("Skipping commit")
322
+ else:
323
+ commands.append("git add .")
324
+ use_emoji = settings.get("commit", {}).get("emoji", False)
325
+ commands.append(get_commit_command("version", None, f"{git_tag}", use_emoji=use_emoji))
322
326
 
323
- if args.no_tag:
324
- if verbose > 0:
325
- console.print("Skipping tag")
326
- else:
327
- commands.append(f"git tag {git_tag}")
327
+ if args.no_tag:
328
+ if verbose > 0:
329
+ console.print("Skipping tag")
330
+ else:
331
+ commands.append(f"git tag {git_tag}")
328
332
 
329
- if args.no_push:
330
- if verbose > 0:
331
- console.print("Skipping push")
332
- else:
333
- commands.append("git push")
334
- commands.append("git push --tag")
335
- commands_str = "\n".join(commands)
336
- run_command(commands_str)
337
- return
333
+ if args.no_push:
334
+ if verbose > 0:
335
+ console.print("Skipping push")
336
+ else:
337
+ commands.extend(("git push", "git push --tag"))
338
+ commands_str = "\n".join(commands)
339
+ run_command(commands_str)
338
340
 
339
341
 
340
342
  class VersionChoice:
@@ -364,28 +366,28 @@ class VersionChoice:
364
366
  major=previous_version.major + 1,
365
367
  minor=0,
366
368
  patch=0,
367
- release="RELEASE",
369
+ release="{RELEASE}",
368
370
  )
369
371
  elif bump == "preminor":
370
372
  self.next_version = Version(
371
373
  major=previous_version.major,
372
374
  minor=previous_version.minor + 1,
373
375
  patch=0,
374
- release="RELEASE",
376
+ release="{RELEASE}",
375
377
  )
376
378
  elif bump == "prepatch":
377
379
  self.next_version = Version(
378
380
  major=previous_version.major,
379
381
  minor=previous_version.minor,
380
382
  patch=previous_version.patch + 1,
381
- release="RELEASE",
383
+ release="{RELEASE}",
382
384
  )
383
385
  elif bump == "previous":
384
386
  self.next_version = previous_version
385
387
 
386
388
  def __str__(self):
387
389
  if "next_version" in self.__dict__:
388
- return f"{self.bump} -> {self.next_version}"
390
+ return f"{self.bump} ({self.next_version})"
389
391
  else:
390
392
  return self.bump
391
393
 
tgit-0.4.2/README.md DELETED
@@ -1,15 +0,0 @@
1
- # tgit
2
-
3
- Tool for managing git repositories.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- pip install tgit
9
- ```
10
-
11
- ## Usage
12
-
13
- ```bash
14
- tgit --help
15
- ```
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes