From d1a877a22bad5667795fa146b6b5b6564eb5b695 Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Tue, 27 Sep 2022 12:50:25 -0400 Subject: [PATCH 1/3] Add ability to display changed files --- CHANGELOG.md | 2 + mepo.d/cmdline/parser.py | 16 ++++++ mepo.d/command/changed-files/changed-files.py | 51 +++++++++++++++++++ mepo.d/repository/git.py | 14 +++-- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 mepo.d/command/changed-files/changed-files.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 28806b7b..1bea3740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Add new `changed-files` command to list all changed files vs original state + ### Changed ### Removed diff --git a/mepo.d/cmdline/parser.py b/mepo.d/cmdline/parser.py index a4081d05..48936a36 100644 --- a/mepo.d/cmdline/parser.py +++ b/mepo.d/cmdline/parser.py @@ -28,6 +28,7 @@ def parse(self): self.__fetch() self.__checkout() self.__checkout_if_exists() + self.__changed_files() self.__branch() self.__tag() self.__stash() @@ -211,6 +212,21 @@ def __checkout_if_exists(self): action = 'store_true', help = 'Dry-run only (lists repos where branch exists)') + def __changed_files(self): + changed_files = self.subparsers.add_parser( + 'changed-files', + description = 'List files that have changes versus the state. By default runs against all components.', + aliases=mepoconfig.get_command_alias('changed-files')) + changed_files.add_argument( + '--full-path', + action = 'store_true', + help = 'Print with full path') + changed_files.add_argument( + 'comp_name', + metavar = 'comp-name', + nargs = '*', + help = 'Component to list branches in') + def __fetch(self): fetch = self.subparsers.add_parser( 'fetch', diff --git a/mepo.d/command/changed-files/changed-files.py b/mepo.d/command/changed-files/changed-files.py new file mode 100644 index 00000000..c2a1e2a9 --- /dev/null +++ b/mepo.d/command/changed-files/changed-files.py @@ -0,0 +1,51 @@ +from state.state import MepoState +from utilities import colors +from utilities import verify +from utilities.version import version_to_string, sanitize_version_string +from repository.git import GitRepository +from shutil import get_terminal_size +from state.component import MepoVersion +import os + +VER_LEN = 30 + +def run(args): + allcomps = MepoState.read_state() + + if any_differing_repos(allcomps): + comps2diff = _get_comps_to_diff(args.comp_name, allcomps) + + for comp in comps2diff: + git = GitRepository(comp.remote, comp.local) + orig_ver = version_to_string(comp.version,git).split()[1] + orig_type = comp.version.type + changed_files = git.get_changed_files(untracked=True, orig_ver=orig_ver, comp_type=orig_type) + + # If there are changed files, print them with the local path + if changed_files: + for file in changed_files: + if args.full_path: + print(os.path.abspath(os.path.join(comp.local, file))) + else: + print(os.path.join(comp.local, file)) + +def _get_comps_to_diff(specified_comps, allcomps): + comps_to_diff = allcomps + if specified_comps: + verify.valid_components(specified_comps, allcomps) + comps_to_diff = [x for x in allcomps if x.name in specified_comps] + return comps_to_diff + +def any_differing_repos(allcomps): + for comp in allcomps: + git = GitRepository(comp.remote, comp.local) + curr_ver = version_to_string(git.get_version(),git) + orig_ver = version_to_string(comp.version,git) + + # This command is to try and work with git tag oddities + curr_ver = sanitize_version_string(orig_ver,curr_ver,git) + + if curr_ver not in orig_ver: + return True + + return False diff --git a/mepo.d/repository/git.py b/mepo.d/repository/git.py index 939dc21c..fff50213 100644 --- a/mepo.d/repository/git.py +++ b/mepo.d/repository/git.py @@ -283,8 +283,14 @@ def check_status(self, ignore_permissions=False): return output.rstrip() - def __get_modified_files(self): - cmd = self.__git + ' diff --name-only' + def __get_modified_files(self, orig_ver, comp_type): + if not orig_ver: + cmd = self.__git + ' diff --name-only' + else: + if comp_type == "b": + cmd = self.__git + ' diff --name-only origin/{}'.format(orig_ver) + else: + cmd = self.__git + ' diff --name-only {}'.format(orig_ver) output = shellcmd.run(shlex.split(cmd), output=True).strip() return output.split('\n') if output else [] @@ -293,8 +299,8 @@ def __get_untracked_files(self): output = shellcmd.run(shlex.split(cmd), output=True).strip() return output.split('\n') if output else [] - def get_changed_files(self, untracked=False): - changed_files = self.__get_modified_files() + def get_changed_files(self, untracked=False, orig_ver=None, comp_type=None): + changed_files = self.__get_modified_files(orig_ver, comp_type) if untracked: changed_files += self.__get_untracked_files() return changed_files From 80bedfb84a514b192e9b93c8b6ec4b353bf78fe8 Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Tue, 27 Sep 2022 12:57:09 -0400 Subject: [PATCH 2/3] Be careful with other get_changed_files call --- mepo.d/command/stage/stage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mepo.d/command/stage/stage.py b/mepo.d/command/stage/stage.py index 157016ec..ea93d9a7 100644 --- a/mepo.d/command/stage/stage.py +++ b/mepo.d/command/stage/stage.py @@ -15,7 +15,7 @@ def stage_files(git, comp, untracked=False, commit=False): curr_ver = MepoVersion(*git.get_version()) if curr_ver.detached: # detached head raise Exception(f"{comp.name} has detached head! Cannot stage.") - for myfile in git.get_changed_files(untracked): + for myfile in git.get_changed_files(untracked=untracked): git.stage_file(myfile) print_output = f"{comp.name}: {myfile}" if commit: From 8b3bb7f91bc16ee72f5c58bbb31a8f64c886221c Mon Sep 17 00:00:00 2001 From: Matthew Thompson Date: Tue, 18 Oct 2022 08:23:39 -0400 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bea3740..abb9402a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,12 +11,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Add new `changed-files` command to list all changed files vs original state - ### Changed ### Removed +## [1.46.0] - 2022-10-18 + +### Added + +- Add new `changed-files` command to list all changed files vs original state + ## [1.45.0] - 2022-08-10 ### Changed