1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-10 14:56:14 +01:00

Merge pull request #1314 from dmach/backup-edited-messages

Backup edited messages and notify user about them when osc errors out
This commit is contained in:
Marco Strigl 2023-05-04 11:42:48 +02:00 committed by GitHub
commit ba1b3c791b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import urllib3.exceptions
from . import _private
from . import commandline
from . import conf as osc_conf
from . import core as osc_core
from . import oscerr
from .OscConfigParser import configparser
from .oscssl import CertVerificationError
@ -196,6 +197,11 @@ def run(prg, argv=None):
print(e, file=sys.stderr)
except oscerr.OscBaseError as e:
print('*** Error:', e, file=sys.stderr)
if osc_core.MESSAGE_BACKUPS:
print()
print("If you lost any edited commit messages due to an error, you may find them here:")
for path in osc_core.MESSAGE_BACKUPS:
print(f" - {path}")
return 1

View File

@ -4408,6 +4408,11 @@ def _editor_command():
return cmd
# list of files with message backups
# we'll show this list when osc errors out
MESSAGE_BACKUPS = []
def _edit_message_open_editor(filename, data, orig_mtime):
editor = _editor_command()
mtime = os.stat(filename).st_mtime
@ -4428,7 +4433,34 @@ def _edit_message_open_editor(filename, data, orig_mtime):
run_editor(filename)
else:
run_editor(filename)
return os.stat(filename).st_mtime != orig_mtime
if os.stat(filename).st_mtime != orig_mtime:
# file has changed
cache_dir = os.path.expanduser("~/.cache/osc/edited-messages")
try:
os.makedirs(cache_dir, mode=0o700)
except FileExistsError:
pass
# remove any stored messages older than 1 day
now = datetime.datetime.now()
epoch = datetime.datetime.timestamp(now - datetime.timedelta(days=1))
for fn in os.listdir(cache_dir):
path = os.path.join(cache_dir, fn)
if not os.path.isfile(path):
continue
mtime = os.path.getmtime(path)
if mtime < epoch:
os.unlink(path)
# store the current message's backup to the cache dir
message_backup_path = os.path.join(cache_dir, str(now).replace(" ", "_"))
shutil.copyfile(filename, message_backup_path)
MESSAGE_BACKUPS.append(message_backup_path)
return True
return False
def edit_message(footer='', template='', templatelen=30):