def do_all_projects_remotes(args): """Run the given git-command in every project, once for each remote. Pass -p to do it in parallel""" man = load_manifest() if args[0] == '-p': parallel = True del args[0] else: parallel = False towait = [] for (name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) for remote_name in project.remotes.keys(): cmd = args + [remote_name] print >> sys.stderr, "In project: ", name, " running ", " ".join( cmd) p = repo.command_process(cmd) if not parallel: p.Wait() print >> sys.stderr else: towait.append(p) for p in towait: p.Wait()
def do_all_projects_remotes(args): """Run the given git-command in every project, once for each remote. Pass -p to do it in parallel""" man = load_manifest() if args[0] == '-p': parallel = True del args[0] else: parallel = False towait = [] for (name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) for remote_name in project.remotes.keys(): cmd = args + [remote_name] print >>sys.stderr, "In project: ", name, " running ", " ".join(cmd) p = repo.command_process(cmd) if not parallel: p.Wait() print >>sys.stderr else: towait.append(p) for p in towait: p.Wait()
def hard_reset_branches(args): """Hard-resets your tracking branches to match the remotes.""" checkout_branches(args) man = load_manifest() for (name, project) in man.projects.iteritems(): print >> sys.stderr, "Hard resetting tracking branch in project: %s" % name repo = GitRepo(workdir_for_project(project)) repo.check_command(["reset", "--hard", project.remote_refspec])
def hard_reset_branches(args): """Hard-resets your tracking branches to match the remotes.""" checkout_branches(args) man = load_manifest() for (name, project) in man.projects.iteritems(): print >>sys.stderr, "Hard resetting tracking branch in project: %s" % name repo = GitRepo(workdir_for_project(project)) repo.check_command(["reset", "--hard", project.remote_refspec])
def setup_git_repos(backup_repo, secret_repo, working_dir, private_key=None): backup_dir_str = backup_dir(working_dir) secret_dir_str = secret_dir(working_dir) remove_dir(backup_dir_str) remove_dir(secret_dir_str) backup = GitRepo( backup_repo, backup_dir_str, private_key) secret = GitRepo( secret_repo, secret_dir_str, private_key) return backup, secret
def checkout_branches(args): """Checks out the tracking branches listed in the manifest.""" ensure_tracking_branches([]) if check_dirty([]) and '-f' not in args: raise Exception("Cannot checkout new branches with dirty projects.") man = load_manifest() for (name, project) in man.projects.iteritems(): print >> sys.stderr, "Checking out tracking branch in project: %s" % name repo = GitRepo(workdir_for_project(project)) # Check that sucker out repo.check_command(["checkout", project.tracking_branch])
def checkout_branches(args): """Checks out the tracking branches listed in the manifest.""" ensure_tracking_branches([]) if check_dirty([]) and '-f' not in args: raise Exception("Cannot checkout new branches with dirty projects.") man = load_manifest() for (name, project) in man.projects.iteritems(): print >>sys.stderr, "Checking out tracking branch in project: %s" % name repo = GitRepo(workdir_for_project(project)) # Check that sucker out repo.check_command(["checkout", project.tracking_branch])
def ensure_tracking_branches(args): """Ensures that the tracking branches are set up""" man = load_manifest() for (name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) branch_missing = repo.command( ["rev-parse", "--verify", "-q", project.refspec], capture_stdout=True) if branch_missing: logging.warn("Branch %s does not exist in project %s. checking out." % (project.refspec, name)) repo.command(["branch", "--track", project.tracking_branch, project.remote_refspec])
def dump_refs(args): """ Output a list of all repositories along with their checked out branches and their hashes. """ man = load_manifest() first = True for (name, project) in man.projects.iteritems(): if not first: print first = False print "Project %s:" % name repo = GitRepo(workdir_for_project(project)) print " HEAD: %s" % repo.rev_parse("HEAD") print " Symbolic: %s" % repo.current_branch() project_status(project, indent=2) repo = get_manifest_repo() if repo: print print "Manifest repo:" print " HEAD: %s" % repo.rev_parse("HEAD") print " Symbolic: %s" % repo.current_branch() repo_status(repo, repo.current_branch(), "origin/" + repo.current_branch(), indent=2) check_dirty_repo(repo, indent=2)
def get_manifest_repo(): """ Return a GitRepo object pointing to the repository that contains the crepo manifest. """ # root dir is cwd for now cdup = GitRepo(".").command_process(["rev-parse", "--show-cdup"], capture_stdout=True, capture_stderr=True) if cdup.Wait() != 0: return None cdup_path = cdup.stdout.strip() if cdup_path: return GitRepo(cdup_path) else: return GitRepo(".")
def get_cache(name): filepath = cache_path(name); if os.path.exists(filepath): c = pd.read_csv(filepath, sep='\t', encoding='utf-8'); fp = name_to_filepath(name); ans = GitRepo(name=name, url=fp, cache=c); return ans; return None;
def ensure_tracking_branches(args): """Ensures that the tracking branches are set up""" man = load_manifest() for (name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) branch_missing = repo.command( ["rev-parse", "--verify", "-q", project.refspec], capture_stdout=True) if branch_missing: logging.warn( "Branch %s does not exist in project %s. checking out." % (project.refspec, name)) repo.command([ "branch", "--track", project.tracking_branch, project.remote_refspec ])
def check_dirty(args): """Prints output if any projects have dirty working dirs or indexes.""" man = load_manifest() any_dirty = False for (name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) any_dirty = check_dirty_repo(repo) or any_dirty return any_dirty
def init(args): """Initializes repository""" man = load_manifest() for (name, project) in man.projects.iteritems(): logging.warn("Initializing project: %s" % name) clone_remote = man.remotes[project.from_remote] clone_url = clone_remote.fetch % name p = GitCommand( None, ["clone", "-o", project.from_remote, "-n", clone_url, project.dir]) p.Wait() repo = GitRepo(workdir_for_project(project)) if repo.command(["show-ref", "-q", "HEAD"]) != 0: # There is no HEAD (maybe origin/master doesnt exist) so check out the tracking # branch repo.check_command([ "checkout", "--track", "-b", project.tracking_branch, project.remote_refspec ]) else: repo.check_command(["checkout"]) ensure_remotes([]) fetch([]) checkout_branches([])
def get_repo(repo_in, in_type='name', update=False): # Input is name if in_type == 'name': filepath = name_to_filepath(repo_in) ans = None if not update: ans = get_cache(repo_in) if ans is not None: return ans print("Checking for " + str(repo_in) + " at " + str(filepath)) ans = get_repo(filepath, 'local', update) if isinstance(ans, GitRepo): ans.commit_data.to_csv( cache_path(repo_in), sep='\t', encoding='utf-8') # We cache it hopefully??? else: print("We failed to find a local copy of this repo") return ans # Input is a local file if in_type == 'local': if repo_already_exists(repo_in): name = filepath_to_name(repo_in) return GitRepo(url=repo_in, name=name) else: print("Invalid filepath: " + repo_in) return None if in_type == 'remote': name = url_to_name(repo_in) filepath = name_to_filepath(name) if not repo_already_exists(filepath): print("Gloning the repo from remote") fetch_repo(repo_in) return get_repo(name, 'name', update) else: print("Invalid input") # TODO: Clarify this error return None
def status(args): """Shows where your branches have diverged from the specified remotes.""" ensure_tracking_branches([]) man = load_manifest() first = True for (name, project) in man.projects.iteritems(): if not first: print first = False print "Project %s:" % name project_status(project, indent=2) check_dirty_repo(GitRepo(workdir_for_project(project)), indent=2) man_repo = get_manifest_repo() if man_repo: print print "Manifest repo:" repo_status(man_repo, man_repo.current_branch(), "origin/" + man_repo.current_branch(), indent=2) check_dirty_repo(man_repo, indent=2)
def ensure_remotes(args): """Ensure that remotes are set up""" man = load_manifest() for (proj_name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) for remote_name in project.remotes: remote = man.remotes[remote_name] new_url = remote.fetch % proj_name p = repo.command_process(["config", "--get", "remote.%s.url" % remote_name], capture_stdout=True) if p.Wait() == 0: cur_url = p.stdout.strip() if cur_url != new_url: repo.check_command(["config", "--set", "remote.%s.url" % remote_name, new_url]) else: repo.check_command(["remote", "add", remote_name, new_url])
def ensure_remotes(args): """Ensure that remotes are set up""" man = load_manifest() for (proj_name, project) in man.projects.iteritems(): repo = GitRepo(workdir_for_project(project)) for remote_name in project.remotes: remote = man.remotes[remote_name] new_url = remote.fetch % proj_name p = repo.command_process( ["config", "--get", "remote.%s.url" % remote_name], capture_stdout=True) if p.Wait() == 0: cur_url = p.stdout.strip() if cur_url != new_url: repo.check_command([ "config", "--set", "remote.%s.url" % remote_name, new_url ]) else: repo.check_command(["remote", "add", remote_name, new_url])
def init(args): """Initializes repository""" man = load_manifest() for (name, project) in man.projects.iteritems(): logging.warn("Initializing project: %s" % name) clone_remote = man.remotes[project.from_remote] clone_url = clone_remote.fetch % name p = GitCommand(None, ["clone", "-o", project.from_remote, "-n", clone_url, project.dir]) p.Wait() repo = GitRepo(workdir_for_project(project)) if repo.command(["show-ref", "-q", "HEAD"]) != 0: # There is no HEAD (maybe origin/master doesnt exist) so check out the tracking # branch repo.check_command(["checkout", "--track", "-b", project.tracking_branch, project.remote_refspec]) else: repo.check_command(["checkout"]) ensure_remotes([]) fetch([]) checkout_branches([])
def run(): for project in projects.keys(): name = projects[project]['name'] version_file_path = projects[project]['version_file_path'] git_url = projects[project]['git_url'] git_branch = projects[project]['git_branch'] app_name = projects[project]['app_name'] app_namespace = projects[project]['app_namespace'] repo = GitRepo(name, git_url, git_branch, './') if path.isdir(name): repo.pull_git_repo() else: repo.clone_git_repo() app = Project(name, repo.repo_name + version_file_path, git_url) deployment = cluster.get_deployment(app_name, app_namespace) image_name = deployment.spec.template.spec.containers[0].image.split( ':')[0] image_tag_in_cluster = deployment.spec.template.spec.containers[ 0].image.split(':')[1] image_tag_in_git = app.get_app_version() if image_tag_in_git != image_tag_in_cluster: cluster.patch_deployment( deployment, '{0}:{1}'.format(image_name, image_tag_in_git), app_name, app_namespace) print( '''Image version in cluster: {0}, image version in git: {1}. Updating deployment... '''.format(image_tag_in_cluster, image_tag_in_git)) else: print("Nothing was changed. Versions are equal.")
def test_init(self): self.repo = GitRepo.init('/tmp/new_project', '/tmp/new_project.git') self._temp_dir = self.repo.path assert os.path.exists('/tmp/new_project.git') shutil.rmtree('/tmp/new_project.git')
def git_repo(self): return GitRepo(self.dir)
def project_status(project, indent=0): repo = GitRepo(workdir_for_project(project)) repo_status(repo, project.tracking_branch, project.remote_refspec, indent=indent)
def test_init(self): self.repo = GitRepo.init('/tmp/new_project') self._temp_dir = self.repo.path assert os.path.exists(os.path.join(self.repo.path, '.git'))