Skip to content

Commit

Permalink
Merge pull request #237 from GEOS-ESM/develop
Browse files Browse the repository at this point in the history
GitFlow: Merge Develop into Main for release
  • Loading branch information
mathomp4 committed Oct 18, 2022
2 parents 50e0c2d + d6b90de commit 03d2656
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 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
Expand Down
16 changes: 16 additions & 0 deletions mepo.d/cmdline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def parse(self):
self.__fetch()
self.__checkout()
self.__checkout_if_exists()
self.__changed_files()
self.__branch()
self.__tag()
self.__stash()
Expand Down Expand Up @@ -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',
Expand Down
51 changes: 51 additions & 0 deletions mepo.d/command/changed-files/changed-files.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion mepo.d/command/stage/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions mepo.d/repository/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []

Expand All @@ -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
Expand Down

0 comments on commit 03d2656

Please sign in to comment.