[Diff][Fixed] Problems when no changes to stash

- "git stash push" always returns 0, even if nothing was stashed.
- But "git stash pop" returns 1 if nothing stashed.
- So now we check using "git stash list".

Related to #265
This commit is contained in:
Salvador E. Tropea 2022-08-31 07:50:09 -03:00
parent bc793c8637
commit 6b344a094b
3 changed files with 43 additions and 4 deletions

View File

@ -6,6 +6,7 @@
"""
Dependencies:
- name: KiCad PCB/SCH Diff
version: 2.4.1
role: mandatory
github: INTI-CMNB/KiDiff
command: kicad-diff.py
@ -30,6 +31,7 @@ from .macros import macros, document, output_class # noqa: F401
from . import log
logger = log.get_logger()
STASH_MSG = 'KiBot_Changes_Entry'
def debug_output(res):
@ -145,7 +147,11 @@ class DiffOptions(BaseOptions):
self.run_git(['checkout', self.branch])
if self.stashed:
logger.debug('Restoring changes')
self.run_git(['stash', 'pop'])
# We don't know if we stashed anything (push always returns 0)
# So we check that the last stash contains our message
res = self.run_git(['stash', 'list', 'stash@{0}'])
if STASH_MSG in res:
self.run_git(['stash', 'pop', '--index'])
def solve_git_name(self, name):
ori = name
@ -184,7 +190,7 @@ class DiffOptions(BaseOptions):
try:
# Save current changes
logger.debug('Saving current changes')
self.run_git(['stash', 'push'])
self.run_git(['stash', 'push', '-m', STASH_MSG])
self.stashed = True
# Find the current branch
self.branch = self.run_git(['rev-parse', '--abbrev-ref', 'HEAD'])

View File

@ -1309,7 +1309,7 @@ def test_diff_git_1(test_dir):
def test_diff_git_2(test_dir):
""" Difference between the two repo points """
""" Difference between the two repo points, wipe the current file """
prj = 'light_control'
yaml = 'diff_git_2'
ctx = context.TestContext(test_dir, prj, yaml)
@ -1341,3 +1341,36 @@ def test_diff_git_2(test_dir):
ctx.run(extra=['-b', file], no_board_file=True, extra_debug=True)
ctx.compare_pdf(prj+'-diff.pdf')
ctx.clean_up(keep_project=True)
def test_diff_git_3(test_dir):
""" Difference between the two repo points, no changes to stash """
prj = 'light_control'
yaml = 'diff_git_2'
ctx = context.TestContext(test_dir, prj, yaml)
# Create a git repo
git_init(ctx)
# Copy the "old" file
pcb = prj+'.kicad_pcb'
file = ctx.get_out_path(pcb)
shutil.copy2(ctx.board_file, file)
shutil.copy2(ctx.board_file.replace('.kicad_pcb', context.KICAD_SCH_EXT),
file.replace('.kicad_pcb', context.KICAD_SCH_EXT))
# Add it to the repo
ctx.run_command(['git', 'add', pcb], chdir_out=True)
ctx.run_command(['git', 'commit', '-m', 'Reference'], chdir_out=True)
# Add an extra commit
dummy = ctx.get_out_path('dummy')
with open(dummy, 'wt') as f:
f.write('dummy\n')
ctx.run_command(['git', 'add', 'dummy'], chdir_out=True)
ctx.run_command(['git', 'commit', '-m', 'Dummy noise'], chdir_out=True)
# Copy the "new" file
shutil.copy2(ctx.board_file.replace(prj, prj+'_diff'), file)
# Add it to the repo
ctx.run_command(['git', 'add', pcb], chdir_out=True)
ctx.run_command(['git', 'commit', '-m', 'New version'], chdir_out=True)
# Run the test
ctx.run(extra=['-b', file], no_board_file=True, extra_debug=True)
ctx.compare_pdf(prj+'-diff.pdf')
ctx.clean_up(keep_project=True)

View File

@ -333,7 +333,7 @@ class TestContext(object):
if chdir_out:
cwd = os.getcwd()
os.chdir(self.output_dir)
logging.debug('Executing: '+str(cmd))
logging.debug('Executing: '+usable_cmd(cmd))
try:
res = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e: