Last active
January 13, 2016 17:49
-
-
Save Knetic/68c55092b30904baca9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# syncUnchangeP4.py | |
# Date: 2016-01 | |
# Author; George Lester | |
# Recurses all folders underneath the path given by the first positional parameter, | |
# finding all git repos. As it finds repos, it checks to see if they're gitp4 repos. | |
# If the repo is gitp4 and contains no local uncommitted changes, this performs a `git p4 rebase`. | |
import sys | |
import os | |
import subprocess | |
class colors: | |
OKBLUE = '\033[94m' | |
WARNING = '\033[93m' | |
ENDC = '\033[0m' | |
def main(): | |
targetPath = determineTargetPath() | |
if(targetPath == None): | |
print("First positional argument must be a path to check for git repositories") | |
return 1 | |
total, dirty = recurseDirectory(targetPath) | |
print("\nDone. Checked " + colors.OKBLUE + str(total) + colors.ENDC + " p4 repositories, found " + colors.WARNING + str(dirty) + colors.ENDC + " dirty repos.") | |
return 0 | |
# Recurses the given [path] looking for git repositories. | |
def recurseDirectory(path): | |
total = dirty = 0 | |
path = os.path.abspath(path) | |
for node in os.listdir(path): | |
node = os.path.join(path, node) | |
if(os.path.isdir(node)): | |
if(isRepo(node)): | |
if(isRepoP4(node)): | |
total += 1 | |
if(isRepoDirty(node)): | |
print("\n" + colors.WARNING + "Not updating repository because of uncommitted changes: '" + node + "'" + colors.ENDC) | |
dirty += 1 | |
else: | |
syncRepo(node) | |
else: | |
lower = recurseDirectory(node) | |
total += lower[0] | |
dirty += lower[1] | |
return (total, dirty) | |
# Returns True if the repo rooted at the given [repoPath] is a "git p4" repo. | |
def isRepoP4(repoPath): | |
remotePath = os.path.join(repoPath, ".git", "refs", "remotes", "p4") | |
return os.path.exists(remotePath) | |
# Returns True if the given [path] is a git repository. | |
def isRepo(path): | |
repoPath = os.path.join(path, ".git") | |
return os.path.exists(repoPath) | |
# Returns True if the given [path] (which is a git repo) contains uncommitted changes. | |
def isRepoDirty(path): | |
command = ["git", "status", "--porcelain"] | |
output = subprocess.check_output(command, cwd=path) | |
return len(output) > 0 | |
# Runs 'git p4 rebase' | |
def syncRepo(path): | |
print("\n" + colors.OKBLUE + " Rebasing '" + path + "'" + colors.ENDC) | |
command = ["git", "p4", "rebase"] | |
return subprocess.call(command, cwd=path) | |
# Determines the first positional parameter, returning it if it was specified. | |
# Otherwise returns None. | |
def determineTargetPath(): | |
if(len(sys.argv) != 2): | |
return None | |
return sys.argv[1] | |
status = main() | |
if(status != 0): | |
sys.exit(status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment