From 6b344a094bf63b08ef31ebf3da5d76a45d5b1cc9 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Wed, 31 Aug 2022 07:50:09 -0300 Subject: [PATCH] [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 --- kibot/out_diff.py | 10 ++++++++-- tests/test_plot/test_misc.py | 35 ++++++++++++++++++++++++++++++++++- tests/utils/context.py | 2 +- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/kibot/out_diff.py b/kibot/out_diff.py index 4ced6510..50ea9972 100644 --- a/kibot/out_diff.py +++ b/kibot/out_diff.py @@ -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']) diff --git a/tests/test_plot/test_misc.py b/tests/test_plot/test_misc.py index aa31fb13..354bb32e 100644 --- a/tests/test_plot/test_misc.py +++ b/tests/test_plot/test_misc.py @@ -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) diff --git a/tests/utils/context.py b/tests/utils/context.py index c83647c9..6a073a93 100644 --- a/tests/utils/context.py +++ b/tests/utils/context.py @@ -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: