diff --git a/git-helpers/git-kclone b/git-helpers/git-kclone index 0da88e3e..3c86f4ea 100755 --- a/git-helpers/git-kclone +++ b/git-helpers/git-kclone @@ -1,136 +1,136 @@ #!/usr/bin/env python3 import os import sys import yaml import fnmatch import subprocess # Settings gitClonePattern = "https://invent.kde.org/{0}" gitPushPattern = "git@invent.kde.org:{0}" # First we need to make sure we have at least one argument if len(sys.argv) < 2: print("Error: Missing name of repository to clone") print("Usage: git kde-clone ") sys.exit(10) # Determine where the repository metadata tree lives # We are at git-helpers/ while the metadata is at projects-invent/ # So first we locate ourselves and then adjust the location accordingly fileLocation = os.path.dirname( os.path.realpath(__file__) ) relativeChange = '../projects-invent/' repositoryMetadataPrefix = os.path.realpath( os.path.join(fileLocation, relativeChange) ) # Prepare to load the metadata knownProjects = [] # Start going over all the known repositories... for currentPath, subdirectories, filesInFolder in os.walk( repositoryMetadataPrefix, topdown=False, followlinks=False ): # Do we have a metadata.yaml file? if 'metadata.yaml' not in filesInFolder: # We're not interested then.... continue # Now that we know we have something to work with.... # Lets load the current metadata up metadataPath = os.path.join( currentPath, 'metadata.yaml' ) metadataFile = open( metadataPath, 'r' ) - metadata = yaml.load( metadataFile ) + metadata = yaml.safe_load( metadataFile ) # Have we found a repository? # If it is a repository, is it active? if not metadata['hasrepo'] or not metadata['repoactive']: # Then we should skip it as well... continue # We have something we can work with! # Grab the identifier and path... identifier = metadata['identifier'] repoPath = metadata['repopath'] # Add it to our list of known repositories knownProjects.append( metadata ) # Now that we know about everything, we can start determining what to clone... reposToClone = [] # Go over all the projects we discovered to search for our match for project in knownProjects: # Do we have an exact match by identifier? if project['identifier'] == sys.argv[1]: # Then add it to the list reposToClone.append( project ) continue # Otherwise, do we have an exact match by path? if project['repopath'] == sys.argv[1]: # Add it to the list! reposToClone.append( project ) continue # Next we check whether the argument we have been passed could potentially be a shell glob pattern # If it doesn't, then there is nothing further for us to do if "*" not in sys.argv[1]: continue # First we check against identifiers (which cannot contain a slash) if '/' not in sys.argv[1] and fnmatch.fnmatch( project['identifier'], sys.argv[1] ): reposToClone.append( project ) continue # Then we check against paths (which cannot contain a slash) if '/' in sys.argv[1] and fnmatch.fnmatch( project['repopath'], sys.argv[1] ): reposToClone.append( project ) continue # Just in case, did we find nothing to clone? # If we failed to find something, then before we bail out, check to see if we were passed a path with a slash in it # If the path has a slash in it, then as a final fallback assume that it could be a personal repository.... if len(reposToClone) == 0 and '/' in sys.argv[1] and '*' not in sys.argv[1]: # Note that we are making this assumption print("WARNING: Unable to find any project matching the path specified - assuming this is a personal repository") # Mock up the metadata project = { 'description': '', 'hasrepo': true, 'name': sys.argv[1], 'projectpath': sys.argv[1], 'repopath': sys.argv[1], 'identifier': sys.argv[1].replace('/', '-') } # Add it to the list of projects to clone reposToClone.append( project ) # Finally, check to see if we have nothing to do if len(reposToClone) == 0: # Looks like it is time to bail print("KClone could not locate the project you specified") sys.exit(10) # Now it is time to start cloning repositories! for repository in reposToClone: # Check to see if we already have something on disk.... if os.path.exists( repository['identifier'] ): print("WARNING: The repository '{0}' already exists - skipping".format( repository['identifier'] )) continue # Now perform the clone! gitCloneCommand = 'git clone "{repositoryUrl}" {pathOnDisk}' commandToRun = gitCloneCommand.format( repositoryUrl=gitClonePattern.format( repository['repopath'] ), pathOnDisk=repository['identifier'] ) subprocess.check_call( commandToRun, stdout=sys.stdout, stderr=sys.stderr, shell=True, cwd=os.getcwd() ) # With that having succeeded, we now override the push url gitPushUrlCommand = 'git remote set-url --push origin "{repositoryUrl}"' commandToRun = gitPushUrlCommand.format( repositoryUrl=gitPushPattern.format( repository['repopath'] ) ) subprocess.check_call( commandToRun, stdout=sys.stdout, stderr=sys.stderr, shell=True, cwd=os.path.join( os.getcwd(), repository['identifier'] ) ) # Finally we are done! sys.exit(0) diff --git a/git-helpers/git-kpull b/git-helpers/git-kpull index 5254d9f3..2479c62f 100755 --- a/git-helpers/git-kpull +++ b/git-helpers/git-kpull @@ -1,105 +1,105 @@ #!/usr/bin/env python3 import os import re import sys import yaml import subprocess # Urls to update what we have currently to newPullPattern = "https://invent.kde.org/{0}" newPushPattern = "git@invent.kde.org:{0}" # Old Url to match on when checking to see if we need to update something.... oldPushUrlRegex = ".*git@git.kde.org[/:](.*)" # Determine where the repository metadata tree lives # We are at git-helpers/ while the metadata is at projects-invent/ # So first we locate ourselves and then adjust the location accordingly fileLocation = os.path.dirname( os.path.realpath(__file__) ) relativeChange = '../projects-invent/' repositoryMetadataPrefix = os.path.realpath( os.path.join(fileLocation, relativeChange) ) # Prepare to load the metadata knownProjects = {} # Start going over all the known repositories... for currentPath, subdirectories, filesInFolder in os.walk( repositoryMetadataPrefix, topdown=False, followlinks=False ): # Do we have a metadata.yaml file? if 'metadata.yaml' not in filesInFolder: # We're not interested then.... continue # Now that we know we have something to work with.... # Lets load the current metadata up metadataPath = os.path.join( currentPath, 'metadata.yaml' ) metadataFile = open( metadataPath, 'r' ) - metadata = yaml.load( metadataFile ) + metadata = yaml.safe_load( metadataFile ) # Have we found a repository? # If it is a repository, is it active? if not metadata['hasrepo'] or not metadata['repoactive']: # Then we should skip it as well... continue # We have something we can work with! identifier = metadata['identifier'] # Add it to our list of known repositories knownProjects[ identifier ] = metadata # Now that we know about everything, we can try to determine which repository we are in here... # Get the current url in use gitGetPushUrlCommand = 'git remote get-url --push origin' process = subprocess.Popen( gitGetPushUrlCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=os.getcwd() ) currentPushUrl = process.stdout.readline().decode('utf-8').strip() # Pull the repository identifier out of that currentPushMatch = re.match( oldPushUrlRegex, currentPushUrl ) # If that failed then we can't help the user if currentPushMatch is None: print("Sorry, we were unable to determine the name of the repository as it is not configured to push to the old server (git.kde.org)") sys.exit(5) # Extract the repository identifier repositoryIdentifier = currentPushMatch.group(1) # Setup storage for the updated repository path newRepositoryPath = None # Is this a repository known to us via the metadata? if repositoryIdentifier in knownProjects: # THen use the metadata to determine the new path newRepositoryPath = knownProjects[ repositoryIdentifier ]['repopath'] # Otherwise, is this a website or sysadmin repository? elif repositoryIdentifier.startswith( 'sysadmin/' ) or repositoryIdentifier.startswith( 'websites/' ): # For these, it is a straight swap of git.kde.org to invent.kde.org newRepositoryPath = repositoryIdentifier # Maybe it is a scratch repository? elif repositoryIdentifier.startswith( 'scratch/' ): # We have to do some additional work here.... # Scratch repositories are moving from scratch/username/repo to username/repo so chop off the scratch/... newRepositoryPath = repositoryIdentifier[8:] # If it was none of these then we failed! else: print("Sorry we were unable to determine where this repository may now live") sys.exit(10) # Now that we know where the repository lives on invent.kde.org, we can update this repository # Start by replacing the pull (fetch) url gitPullUrlCommand = 'git remote set-url origin "{repositoryUrl}"' commandToRun = gitPullUrlCommand.format( repositoryUrl=newPullPattern.format( newRepositoryPath ) ) subprocess.check_call( commandToRun, stdout=sys.stdout, stderr=sys.stderr, shell=True, cwd=os.getcwd() ) # Then do the push url gitPushUrlCommand = 'git remote set-url --push origin "{repositoryUrl}"' commandToRun = gitPushUrlCommand.format( repositoryUrl=newPushPattern.format( newRepositoryPath ) ) subprocess.check_call( commandToRun, stdout=sys.stdout, stderr=sys.stderr, shell=True, cwd=os.getcwd() ) # All done! sys.exit(0)