Skip to content

Commit

Permalink
Merge branch 'develop' into feature/mathomp4/#30-add-develop-override
Browse files Browse the repository at this point in the history
  • Loading branch information
mathomp4 authored Nov 20, 2020
2 parents 6656b1e + caed0f9 commit 32081e7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
11 changes: 9 additions & 2 deletions mepo.d/cmdline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def __clone(self):
'--config',
metavar = 'config-file',
nargs = '?',
default = 'components.yaml',
help = 'Configuration file (ignored if init already called, default: %(default)s)')
default = None,
help = 'Configuration file (ignored if init already called)')

def __list(self):
listcomps = self.subparsers.add_parser(
Expand All @@ -108,6 +108,10 @@ def __diff(self):
'--name-only',
action = 'store_true',
help = 'Show only names of changed files')
diff.add_argument(
'--staged',
action = 'store_true',
help = 'Show diff of staged changes')
diff.add_argument(
'comp_name',
metavar = 'comp-name',
Expand Down Expand Up @@ -141,6 +145,7 @@ def __fetch(self):
fetch.add_argument('--all', action = 'store_true', help = 'Fetch all remotes.')
fetch.add_argument('--prune','-p', action = 'store_true', help = 'Prune remote branches.')
fetch.add_argument('--tags','-t', action = 'store_true', help = 'Fetch tags.')
fetch.add_argument('--force','-f', action = 'store_true', help = 'Force action.')

def __fetch_all(self):
fetch_all = self.subparsers.add_parser(
Expand All @@ -150,6 +155,7 @@ def __fetch_all(self):
fetch_all.add_argument('--all', action = 'store_true', help = 'Fetch all remotes.')
fetch_all.add_argument('--prune','-p', action = 'store_true', help = 'Prune remote branches.')
fetch_all.add_argument('--tags','-t', action = 'store_true', help = 'Fetch tags.')
fetch_all.add_argument('--force','-f', action = 'store_true', help = 'Force action.')

def __branch(self):
branch = self.subparsers.add_parser(
Expand Down Expand Up @@ -229,6 +235,7 @@ def __commit(self):
commit = self.subparsers.add_parser(
'commit',
description = 'Commit staged files in the specified components')
commit.add_argument('-a', '--all', action = 'store_true', help = 'stage all tracked files and then commit')
commit.add_argument('-m', '--message', type=str, metavar = 'message', default=None)
commit.add_argument(
'comp_name',
Expand Down
21 changes: 20 additions & 1 deletion mepo.d/command/clone/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
from repository.git import GitRepository
from command.init import init as mepo_init
from utilities import shellcmd
from urllib.parse import urlparse
from urllib.parse import urlparse

import os
import pathlib
import shutil

def run(args):

# This protects against someone using branch without a URL
if args.branch and not args.repo_url:
raise RuntimeError("The branch argument can only be used with a URL")

# If you pass in a config, with clone, it could be outside the repo.
# So use the full path
passed_in_config = False
if args.config:
passed_in_config = True
args.config = os.path.abspath(args.config)
else:
# If we don't pass in a config, we need to "reset" the arg to the
# default name because we pass args to mepo_init
args.config = 'components.yaml'

if args.repo_url:
p = urlparse(args.repo_url)
last_url_node = p.path.rsplit('/')[-1]
Expand All @@ -29,6 +41,13 @@ def run(args):
local_clone(args.repo_url,args.branch)
os.chdir(git_url_directory)

# Copy the new file into the repo only if we pass it in
if passed_in_config:
try:
shutil.copy(args.config,os.getcwd())
except shutil.SameFileError as e:
pass

# This tries to read the state and if not, calls init,
# loops back, and reads the state
while True:
Expand Down
4 changes: 4 additions & 0 deletions mepo.d/command/commit/commit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from state.state import MepoState
from utilities import verify
from repository.git import GitRepository
from command.stage.stage import stage_files

# Popping up an EDITOR is based on https://stackoverflow.com/a/39989442
import os, tempfile, subprocess
Expand All @@ -26,6 +27,9 @@ def run(args):

for comp in comps2commit:
git = GitRepository(comp.remote, comp.local)
if args.all:
stage_files(git, comp, commit=True)

staged_files = git.get_staged_files()
if staged_files:
git.commit_files(args.message,tf_file)
Expand Down
20 changes: 14 additions & 6 deletions mepo.d/command/stage/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ def run(args):
comps2stg = [x for x in allcomps if x.name in args.comp_name]
for comp in comps2stg:
git = GitRepository(comp.remote, comp.local)
curr_ver = MepoVersion(*git.get_version())
if curr_ver.detached: # detached head
raise Exception('{} has detached head! Cannot stage.'.format(comp.name))
for myfile in git.get_changed_files(args.untracked):
git.stage_file(myfile)
print('+ {}: {}'.format(comp.name, myfile))
stage_files(git, comp, args.untracked)

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):
git.stage_file(myfile)
print_output = f"{comp.name}: {myfile}"
if commit:
print(f"Staged: {print_output}")
else:
print(f"+ {print_output}")

10 changes: 10 additions & 0 deletions mepo.d/repository/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def run_diff(self, args=None):
cmd = self.__git + ' diff --color'
if args.name_only:
cmd += ' --name-only'
if args.staged:
cmd += ' --staged'
output = shellcmd.run(cmd.split(),output=True)
return output.rstrip()

Expand All @@ -107,6 +109,8 @@ def fetch(self, args=None):
cmd += ' --prune'
if args.tags:
cmd += ' --tags'
if args.force:
cmd += ' --force'
return shellcmd.run(cmd.split(), output=True)

def create_branch(self, branch_name):
Expand Down Expand Up @@ -317,6 +321,12 @@ def get_version(self):
detached = True
name = hash_out.rstrip()
tYpe = 'h'
elif output.startswith('grafted'):
cmd = self.__git + ' describe --always'
hash_out = shellcmd.run(cmd.split(), output=True)
detached = True
name = hash_out.rstrip()
tYpe = 'h'
return (name, tYpe, detached)

def get_current_remote_url():
Expand Down
24 changes: 21 additions & 3 deletions mepo.d/state/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,45 @@ def __repr__(self):
def __set_original_version(self, comp_details):
if self.fixture:
cmd_if_branch = 'git symbolic-ref HEAD'
# Have to use 'if not' since 0 is a good status
if not shellcmd.run(cmd_if_branch.split(),status=True):
output = shellcmd.run(cmd_if_branch.split(),output=True).rstrip()
ver_name = output.replace('refs/heads/','')
ver_type = 'b'
is_detached = False
else:
# On some CI systems, git is handled oddly. As such, sometimes
# tags aren't found due to shallow clones
cmd_for_tag = 'git describe --tags'
ver_name = shellcmd.run(cmd_for_tag.split(),output=True).rstrip()
ver_type = 't'
# Have to use 'if not' since 0 is a good status
if not shellcmd.run(cmd_for_tag.split(),status=True):
ver_name = shellcmd.run(cmd_for_tag.split(),output=True).rstrip()
ver_type = 't'
is_detached = True
else:
# Per internet, describe always should always work, though mepo
# will return weirdness (a grafted branch, probably a hash)
cmd_for_always = 'git describe --always'
ver_name = shellcmd.run(cmd_for_always.split(),output=True).rstrip()
ver_type = 'h'
is_detached = True
else:
if comp_details.get('branch', None):
# SPECIAL HANDLING of 'detached head' branches
ver_name = 'origin/' + comp_details['branch']
ver_type = 'b'
# we always detach branches from components.yaml
is_detached = True
elif comp_details.get('hash', None):
# Hashes don't have to exist
ver_name = comp_details['hash']
ver_type = 'h'
is_detached = True
else:
ver_name = comp_details['tag'] # 'tag' key has to exist
ver_type = 't'
self.version = MepoVersion(ver_name, ver_type, True)
is_detached = True
self.version = MepoVersion(ver_name, ver_type, is_detached)

def __validate_fixture(self, comp_details):
unallowed_keys = ['remote', 'local', 'branch', 'hash', 'tag', 'sparse', 'recurse_submodules']
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyYAML==5.1.2

0 comments on commit 32081e7

Please sign in to comment.