Skip to content

Commit da9b45e

Browse files
authored
✨ feat: Add sign flag to GPG sign and start command (#13)
* ✨ feat: Add `sign` flag to GPG sign Equivalent to `git commit -S` * ✨ feat: Add `start` command Acts as default commit with config-based defaults * 🏗 build: Bump version to 0.6.0 * 📝 docs: Update Readme to document `start` * ✨ feat: Improve start args feedback
1 parent 3789bc9 commit da9b45e

File tree

6 files changed

+228
-44
lines changed

6 files changed

+228
-44
lines changed

README.md

+104-41
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Use your own emojis by editing the "custom emojis" file listed by `gitmopy info`
3535

3636
```bash
3737
alias gpy="gitmopy"
38+
alias gpys="gitmopy start"
3839
alias gpyc="gitmopy commit"
3940
alias gpya="gitmopy commit --add"
4041
alias gpyk="gitmopy commit --add --keep-alive"
@@ -54,6 +55,9 @@ $ gitmopy commit --add --keep-alive
5455
# same using an alias, + push after every commit (could be dangerous)
5556
$ gpyk --push
5657

58+
# use your default commit settings (configured via `gitmopy config`)
59+
$ gitmopy start
60+
5761

5862
# Specific usage
5963
# --------------
@@ -98,32 +102,51 @@ $ gitmopy commit --help
98102
$ gitmopy info
99103
100104
gitmopy info:
101-
version : 0.4.2
105+
version : 0.6.0
102106
app path : /Users/victor/.gitmopy
103107
history : /Users/victor/.gitmopy/history.json
104108
config : /Users/victor/.gitmopy/config.yaml
105109
custom emojis: /Users/victor/.gitmopy/custom_gitmojis.yaml
106110
107111
Current configuration:
108-
skip_scope : False
109-
skip_message : False
110-
capitalize_title: True
111-
enable_history : True
112-
emoji_set : gitmoji
112+
skip_scope : False
113+
skip_message : False
114+
capitalize_title : True
115+
enable_history : True
116+
emoji_set : gitmoji
117+
default_commit_flags: ['add', 'keep_alive', 'sign', 'simple']
118+
default_commit_args : {'remote': 'origin', 'repo': '.'}
113119
```
114120

115121
Update configuration with
116122

117123
```text
118124
$ gitmopy config
119-
$ gitmopy config
125+
120126
❓ Configure gitmopy locally. Use 'space' to (de-)select, 'enter' to validate.
121127
❯ ○ Skip commit scope
122128
○ Skip commit message
123129
◉ Capitalize commit title
124130
◉ Remember commit history for auto-complete and emoji sorting
125131
126132
Config will be saved in /Users/victor/.gitmopy/config.yaml.
133+
134+
✓ Configure gitmopy locally.
135+
❓ Emoji set to use for commits
136+
❯ gitmoji
137+
ai-devmojis
138+
139+
❓ Default commit binary flags used in `gitmopy start`
140+
❯ ◉ add
141+
○ dry
142+
◉ keep_alive
143+
○ push
144+
◉ sign
145+
◉ simple
146+
147+
❓ repo (Path to the git repository): .
148+
149+
❓ remote (Comma-separated list of remotes to push to): origin
127150
```
128151

129152
Get help with
@@ -133,18 +156,22 @@ $ gitmopy --help
133156
134157
Usage: gitmopy [OPTIONS] COMMAND [ARGS]...
135158
136-
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
137-
│ --install-completion Install completion for the current shell. │
138-
│ --show-completion Show completion for the current shell, to copy it or │
139-
│ customize the installation. │
140-
│ --help Show this message and exit. │
141-
╰─────────────────────────────────────────────────────────────────────────────────────╯
142-
╭─ Commands ──────────────────────────────────────────────────────────────────────────╮
143-
│ commit Commit staged files. Use --add to interactively select files to stage if │
144-
│ none is already staged │
145-
│ config Configure gitmopy │
146-
│ info Print gitmopy info │
147-
╰─────────────────────────────────────────────────────────────────────────────────────╯
159+
╭─ Options ───────────────────────────────────────────────────────────────────────╮
160+
│ --install-completion Install completion for the current shell. │
161+
│ --show-completion Show completion for the current shell, to copy it │
162+
│ or customize the installation. │
163+
│ --help Show this message and exit. │
164+
╰─────────────────────────────────────────────────────────────────────────────────╯
165+
╭─ Commands ──────────────────────────────────────────────────────────────────────╮
166+
│ commit Commit staged files. Use --add to interactively select files to stage │
167+
│ if none is already staged │
168+
│ config Configure gitmopy │
169+
│ info Print gitmopy info │
170+
│ start Runs the commit command with the default arguments you have set in the │
171+
│ configuration file. If no such arguments are set, you will be prompted │
172+
│ to set them interactively. │
173+
╰─────────────────────────────────────────────────────────────────────────────────╯
174+
148175
149176
$ gitmopy commit --help
150177
@@ -153,30 +180,66 @@ $ gitmopy commit --help
153180
Commit staged files. Use --add to interactively select files to stage if none is
154181
already staged
155182
156-
╭─ Options ───────────────────────────────────────────────────────────────────────────╮
157-
│ --repo TEXT Path to the git repository [default: .] │
158-
│ --add --no-add Whether or not to interactively select │
159-
│ files to stage if none is already staged │
160-
│ [default: no-add] │
161-
│ --push --no-push Whether to `git push` after commit. If │
162-
│ multiple remotes exist, you will be asked │
163-
│ to interactively choose the ones to push │
164-
│ to. Use --remote to skip interactive │
165-
│ selection. Disabled by default. │
166-
│ [default: no-push] │
167-
│ --dry --no-dry Whether or not to actually commit. │
168-
│ [default: no-dry] │
169-
│ --remote TEXT Remote to push to after commit. Use to │
170-
│ skip interactive remote selection when │
171-
│ several exist. Use several '--remote │
172-
│ {remote name}' to push to multiple remotes │
173-
│ --keep-alive --no-keep-alive Whether or not to keep the app alive after │
174-
│ commit, to be ready for another one. │
175-
│ [default: no-keep-alive] │
176-
│ --help Show this message and exit. │
177-
╰─────────────────────────────────────────────────────────────────────────────────────╯
183+
╭─ Options ───────────────────────────────────────────────────────────────────────╮
184+
│ --repo TEXT Path to the git repository │
185+
│ [default: .] │
186+
│ --add --no-add Whether or not to interactively select │
187+
│ files to stage if none is already │
188+
│ staged │
189+
│ [default: no-add] │
190+
│ --push --no-push Whether to `git push` after commit. If │
191+
│ multiple remotes exist, you will be │
192+
│ asked to interactively choose the ones │
193+
│ to push to. Use --remote to skip │
194+
│ interactive selection. Disabled by │
195+
│ default. │
196+
│ [default: no-push] │
197+
│ --dry --no-dry Whether or not to actually commit. │
198+
│ [default: no-dry] │
199+
│ --remote TEXT Remote to push to after commit. Use to │
200+
│ skip interactive remote selection when │
201+
│ several exist. Use several '--remote │
202+
│ {remote name}' to push to multiple │
203+
│ remotes │
204+
│ --keep-alive --no-keep-alive Whether or not to keep the app alive │
205+
│ after commit, to be ready for another │
206+
│ one. │
207+
│ [default: no-keep-alive] │
208+
│ --simple --no-simple Whether or not to use a simple commit │
209+
│ which merges conventional commits and │
210+
│ gitmoji. │
211+
│ [default: no-simple] │
212+
│ --sign --no-sign Whether or not to sign the commit with │
213+
│ GPG. Equivalent to `git commit -S`. │
214+
│ [default: no-sign] │
215+
│ --help Show this message and exit. │
216+
╰─────────────────────────────────────────────────────────────────────────────────╯
178217
```
179218

219+
## Default commit settings
220+
221+
You can configure default settings for the `gitmopy start` command which will run `gitmopy commit` with your preferred arguments. To set these up:
222+
223+
1. Run `gitmopy config`
224+
2. Navigate to "Default commit binary flags used in `gitmopy start`"
225+
3. Select the flags you want enabled by default (e.g., `add`, `keep-alive`, `push`, etc.)
226+
4. Navigate to "Default commit arguments used in `gitmopy start`"
227+
5. Configure:
228+
- `repo`: Path to your git repository (default: ".")
229+
- `remote`: Comma-separated list of remotes to push to (default: "origin")
230+
231+
Then simply run `gitmopy start` to use your default settings!
232+
233+
For example, if you always want to:
234+
235+
- Work in the current directory
236+
- Be able to select files to stage
237+
- Keep committing until you're done
238+
- Sign your commits
239+
- Push to origin
240+
241+
Configure these settings once with `gitmopy config` and just run `gitmopy start` instead of `gitmopy commit --add --keep-alive --push --remote origin --sign`!
242+
180243
## To Do
181244

182245
- Features

gitmopy/cli.py

+50-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
print,
4141
print_staged_files,
4242
resolve_path,
43+
set_start_options,
4344
terminal_separator,
4445
)
4546

@@ -310,6 +311,12 @@ def commit(
310311
help="Whether or not to use a simple commit which merges conventional commits and gitmoji."
311312
),
312313
] = False,
314+
sign: Annotated[
315+
bool,
316+
typer.Option(
317+
help="Whether or not to sign the commit with GPG. Equivalent to `git commit -S`."
318+
),
319+
] = False,
313320
):
314321
"""
315322
Main command: commit staged files, and staging files if need be.
@@ -433,7 +440,10 @@ def commit(
433440
save_to_history(commit_dict)
434441

435442
# commit
436-
repo.index.commit(commit_message)
443+
if sign:
444+
repo.git.commit("-S", "-m", commit_message)
445+
else:
446+
repo.index.commit(commit_message)
437447

438448
if push:
439449
push_cli(repo, remote)
@@ -454,6 +464,7 @@ def config():
454464
"""
455465
Command to setup gitmopy's configuration.
456466
"""
467+
set_start_options()
457468
config_prompt()
458469

459470

@@ -481,5 +492,43 @@ def info():
481492
print()
482493

483494

495+
@app.command()
496+
def start():
497+
"""
498+
Runs the commit command with the default arguments you have set in the
499+
configuration file. If no such arguments are set, you will be prompted to
500+
set them interactively.
501+
"""
502+
conf = load_config()
503+
if "default_commit_args" not in conf:
504+
config()
505+
conf = load_config()
506+
commit_args = {
507+
"repo": conf["default_commit_args"]["repo"],
508+
"remote": list(
509+
map(str.strip, conf["default_commit_args"]["remote"].split(","))
510+
),
511+
}
512+
for flag in conf["default_commit_flags"]:
513+
commit_args[flag] = True
514+
if not commit_args.get("push"):
515+
commit_args.pop("remote", None)
516+
cmd = " $ gitmopy commit"
517+
for flag, v in commit_args.items():
518+
if isinstance(v, bool):
519+
if v:
520+
cmd += f" --{flag}"
521+
elif isinstance(v, list):
522+
for value in v:
523+
cmd += f" --{flag} {value}"
524+
else:
525+
cmd += f" --{flag} {v}"
526+
print(f"{col('Running:', 'b', True)} {cmd.strip()}")
527+
print()
528+
print(terminal_separator())
529+
print()
530+
commit(**commit_args)
531+
532+
484533
if __name__ == "__main__":
485534
app()

gitmopy/constants.py

+16
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,27 @@
7979
"default": "gitmoji",
8080
"options": ["gitmoji", "ai-devmojis"],
8181
},
82+
{
83+
"value": "default_commit_flags",
84+
"name": "Default commit binary flags used in `gitmopy start`",
85+
"default": [],
86+
"options": [], # must be set programmatically by `set_start_options`
87+
},
88+
{
89+
"value": "default_commit_args",
90+
"name": "Default commit arguments used in `gitmopy start`",
91+
"default": {"repo": ".", "remote": "origin"},
92+
"options": {
93+
"repo": "Path to the git repository",
94+
"remote": "Comma-separated list of remotes to push to",
95+
},
96+
},
8297
]
8398
"""
8499
Choices for the setup prompt.
85100
"""
86101

102+
87103
DEFAULT_CONFIG = {c["value"]: c["default"] for c in DEFAULT_CHOICES}
88104
"""
89105
Default gitmopy configuration.

gitmopy/prompt.py

+30
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,36 @@ def config_prompt() -> None:
257257
).execute()
258258
options[ldict["value"]] = option
259259

260+
multiple_choices = [
261+
c
262+
for c in gpyc.DEFAULT_CHOICES
263+
if isinstance(c["default"], list) and "options" in c
264+
]
265+
266+
for c in multiple_choices:
267+
option = inquirer.checkbox(
268+
message=c["name"],
269+
choices=c["options"],
270+
default=c["default"],
271+
qmark="❓",
272+
amark="✓",
273+
).execute()
274+
options[c["value"]] = option
275+
276+
string_inputs = [c for c in gpyc.DEFAULT_CHOICES if isinstance(c["default"], dict)]
277+
278+
for c in string_inputs:
279+
for k, v in c["options"].items():
280+
option = inquirer.text(
281+
message=f"{k} ({v}):",
282+
qmark="❓",
283+
amark="✓",
284+
default=c["default"][k],
285+
).execute()
286+
if c["value"] not in options:
287+
options[c["value"]] = {}
288+
options[c["value"]][k] = option
289+
260290
for c in choices:
261291
config[c.value] = c.value in selected
262292
config.update(options)

gitmopy/utils.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
from rich.console import Console
1414
from yaml import safe_dump, safe_load
1515

16-
from gitmopy.constants import APP_PATH, COLORS, DEFAULT_CONFIG, USER_EMOJIS_PATH
16+
from gitmopy.constants import (
17+
APP_PATH,
18+
COLORS,
19+
DEFAULT_CHOICES,
20+
DEFAULT_CONFIG,
21+
USER_EMOJIS_PATH,
22+
)
1723

1824
console = Console()
1925
print = console.print
@@ -309,3 +315,23 @@ def standard_width_emoji(emoji: str, to=2) -> str:
309315
if len(emoji) > 1:
310316
emoji = emoji[0]
311317
return emoji + " " * (to - wcwidth.wcwidth(emoji))
318+
319+
320+
def set_start_options():
321+
"""
322+
Set the options for the default commit arguments automatically from the ``gitmopy commit`` command's signature.
323+
"""
324+
global DEFAULT_CHOICES
325+
import inspect
326+
327+
from gitmopy.cli import commit
328+
329+
start_idx = next(
330+
i for i, c in enumerate(DEFAULT_CHOICES) if c["value"] == "default_commit_flags"
331+
)
332+
333+
DEFAULT_CHOICES[start_idx]["options"] = sorted(
334+
arg
335+
for arg in inspect.signature(commit).parameters.keys()
336+
if arg not in {"repo", "remote"}
337+
)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "gitmopy"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
description = "A python command-line for gitmoji"
55
authors = [{ name = "vict0rsch", email = "[email protected]" }]
66
license = { text = "MIT" }

0 commit comments

Comments
 (0)