-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
103 lines (87 loc) · 3.34 KB
/
Copy path__init__.py
File metadata and controls
103 lines (87 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
#
# Copyright (c) 2020-2026 James Cherti
# URL: https://github.com/jamescherti/git-commitflow
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
"""Git commit and push helper."""
import logging
import os
import subprocess
import sys
from pathlib import Path
try:
import colorama
HAS_COLORAMA = True
except ImportError:
HAS_COLORAMA = False
from .git_commitflow import GitCommitFlow
def flush_stdin() -> None:
"""
Clear any pending input from the standard input buffer.
This function ensures that no stale or unintended data remains in stdin
before reading user input interactively. On Windows, it uses the msvcrt
module to discard characters from the input buffer. On POSIX-compliant
systems (e.g., Linux, macOS...), it uses select to check for available
input without blocking and either discards the data by reading or flushes
it using termios.tcflush if stdin is a terminal.
"""
try:
if os.name == "nt":
import msvcrt # pylint: disable=import-outside-toplevel
# For Windows systems, Check if there is any pending input in the
# buffer Discard characters one at a time until the buffer is empty
while msvcrt.kbhit():
msvcrt.getch()
elif os.name == "posix":
import select # pylint: disable=import-outside-toplevel
# For Unix-like systems, check if there's any pending input in
# stdin without blocking
stdin, _, _ = select.select([sys.stdin], [], [], 0)
if stdin:
if sys.stdin.isatty():
# pylint: disable=import-outside-toplevel
from termios import TCIFLUSH, tcflush
# Flush the input buffer
tcflush(sys.stdin.fileno(), TCIFLUSH)
else:
# Read and discard input (in chunks)
while sys.stdin.read(1024):
pass
except ImportError:
pass
def git_commitflow_cli() -> None:
"""
The git-commitflow command-line interface.
"""
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
format="%(asctime)s %(name)s: %(message)s")
# Optional feature: setproctitle
try:
# pylint: disable=import-outside-toplevel
from setproctitle import setproctitle
setproctitle(Path(sys.argv[0]).name) # type: ignore
except ImportError:
# Optional dependency 'setproctitle' is not installed.
pass
if HAS_COLORAMA:
colorama.init()
flush_stdin()
try:
GitCommitFlow().main()
except subprocess.CalledProcessError as main_proc_err:
print(f"Error: {main_proc_err}")
except KeyboardInterrupt:
print()