def setRemoteURL(remote, url, path, logger): """ Sets a git remote's url. :param str remote: Which git remote to alter :param str url: What to set the url to :param str path: git directory to reset :param logger: An initialized logger object :raises CommandFailedError: if git set-url fails """ assert logger assert isinstance(path, basestring), ( "path must be a string, but is %r" % path) assert isinstance(remote, basestring), ( "remote must be a string, but is %r" % (remote)) assert isinstance(url, basestring), "url must be a string, but is %r" % (url) logger.debug("* Setting url for %s to %s in %s", remote, url, path) with changeToWorkingDir(path): return executeCommand(command="git set-url %s %s" % (remote, url), logger=logger)
def getShaFromRemoteBranch(gitRemoteRepo, gitRemoteBranch, logger): """ Get the actual SHA of the current HEAD of a remote repo / branch. :param str gitRemoteRepo: The URL of the remote repo, e.g., [email protected]:numenta/nupic.git :param str gitRemoteBranch: The name of the remote branch, e.g., master :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :returns: A `String` representing the SHA :rtype: str """ command = ("git", "ls-remote", gitRemoteRepo) shaList = executeCommand(command=command, logger=logger) if gitRemoteBranch == "master": return shaList.split("\t")[0] else: formattedBranchName = "refs/heads/%s" % gitRemoteBranch for refShaPair in shaList.split("\n"): pair = refShaPair.split() if pair[1] == formattedBranchName: return pair[0] raise CommandFailedError( "Could not find the specified branch %s on %s" % gitRemoteBranch, gitRemoteRepo)
def reset(sha, logger, **kwargs): """ Resets the repository to a optional SHA. Optional argument for --hard :param logger: logger for additional debug info :param bool hard: Boolean. Defaults to False. If true, resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :returns: The exit code :rtype: int """ assert logger validKwargs = {"hard"} checkKwargs(kwargs=kwargs, validKwargs=validKwargs) command = ["git", "reset"] if checkIfOptionSet("hard", kwargs): command.append("--hard") command.append(sha) return executeCommand(command=command, logger=logger)
def getShaFromRemoteBranch(gitRemoteRepo, gitRemoteBranch, logger): """ Get the actual SHA of the current HEAD of a remote repo / branch. :param str gitRemoteRepo: The URL of the remote repo, e.g., [email protected]:numenta/nupic.git :param str gitRemoteBranch: The name of the remote branch, e.g., master :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :returns: A `String` representing the SHA :rtype: str """ command = ("git", "ls-remote", gitRemoteRepo) shaList = executeCommand(command=command, logger=logger) if gitRemoteBranch == "master": return shaList.split("\t")[0] else: formattedBranchName = "refs/heads/%s" % gitRemoteBranch for refShaPair in shaList.split("\n"): pair = refShaPair.split() if pair[1] == formattedBranchName: return pair[0] raise CommandFailedError("Could not find the specified branch %s on %s" % gitRemoteBranch, gitRemoteRepo)
def pull(logger, arguments=None): """ Do a git pull, with optional parameters. Example usage: pull(arguments=["origin", "next"]) pull(arguments=["origin", "next", "--ff-only"]) pull(arguments=["[email protected]:username/repo.git", "next"]) :param str arguments: Any extra arguments you want to pass to git pull :param logger: An initialized logger object """ if arguments: assert (isinstance(arguments, basestring) or isinstance(arguments, tuple) or isinstance(arguments, list)), ( "arguments must be a string or list, but is %r" % arguments) assert logger gitCommand = ["git", "pull"] if arguments: if isinstance(arguments, list) or isinstance(arguments, tuple): gitCommand.extend(arguments) if isinstance(arguments, basestring): arguments.extend(str.split(arguments)) return executeCommand(command=gitCommand, logger=logger)
def getShaFromRemoteBranch(YOMPRemoteRepo, YOMPRemoteBranch): """ Get the actual SHA of the current HEAD of a remote repo / branch. @param YOMPRemoteRepo: The URL of the remote repo, e.g., [email protected]:numenta/nupic.YOMP @param YOMPRemoteBranch: The name of the remote branch, e.g., master @raises: @return: A `String` representing the SHA @rtype: String """ shaList = executeCommand("YOMP ls-remote %s" % YOMPRemoteRepo) if YOMPRemoteBranch == "master": return shaList.split("\t")[0] else: formattedBranchName = "refs/heads/%s" % YOMPRemoteBranch for refShaPair in shaList.split("\n"): pair = refShaPair.split() if pair[1] == formattedBranchName: return pair[0] raise CommandFailedError( "Could not find the specified branch %s on %s" % YOMPRemoteBranch, YOMPRemoteRepo)
def checkout(pathspec, **kwargs): """ Switches to a given commit-ish @param pathspec: The name of the branch (commit-ish) @param kwargs: @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails @returns: The text blob output of YOMP checkout @rtype: string """ command = "YOMP checkout" if checkIfOptionSet("new", **kwargs): command += " -b " elif checkIfOptionSet("orphan", **kwargs): command += " --orphan" elif checkIfOptionSet("theirs", **kwargs): command += " --theirs" command += " %s" % pathspec return executeCommand(command)
def getShaFromRemoteBranch(YOMPRemoteRepo, YOMPRemoteBranch): """ Get the actual SHA of the current HEAD of a remote repo / branch. @param YOMPRemoteRepo: The URL of the remote repo, e.g., [email protected]:numenta/nupic.YOMP @param YOMPRemoteBranch: The name of the remote branch, e.g., master @raises: @return: A `String` representing the SHA @rtype: String """ shaList = executeCommand("YOMP ls-remote %s" % YOMPRemoteRepo) if YOMPRemoteBranch == "master": return shaList.split("\t")[0] else: formattedBranchName = "refs/heads/%s" % YOMPRemoteBranch for refShaPair in shaList.split("\n"): pair = refShaPair.split() if pair[1] == formattedBranchName: return pair[0] raise CommandFailedError("Could not find the specified branch %s on %s" % YOMPRemoteBranch, YOMPRemoteRepo)
def setRemoteURL(remote, url, path, logger): """ Sets a git remote's url. :param str remote: Which git remote to alter :param str url: What to set the url to :param str path: git directory to reset :param logger: An initialized logger object :raises CommandFailedError: if git set-url fails """ assert logger assert isinstance(path, basestring), ("path must be a string, but is %r" % path) assert isinstance( remote, basestring), ("remote must be a string, but is %r" % (remote)) assert isinstance(url, basestring), "url must be a string, but is %r" % (url) logger.debug("* Setting url for %s to %s in %s", remote, url, path) with changeToWorkingDir(path): return executeCommand(command="git set-url %s %s" % (remote, url), logger=logger)
def clean(path, arguments, logger): """ Changes to path, then runs git clean. :param str path: git directory to clean :param str arguments: str containing optional extra command line arguments for git clean, as you would type them on the command line. If you wanted to do `git clean -fd`, you'd set arguments to "-fd". :param logger: An initialized logger object :raises CommandFailedError: if git clean fails """ assert logger assert isinstance(arguments, basestring), ("arguments must be a string, but is %r" % arguments) assert isinstance(path, basestring), "path must be a string, but is %r" % path command = ["git", "clean"] if arguments: command.append(arguments) logger.debug("* Running %s in %s", command, path) with changeToWorkingDir(path): return executeCommand(command=command, logger=logger)
def clean(path, arguments, logger): """ Changes to path, then runs git clean. :param str path: git directory to clean :param str arguments: str containing optional extra command line arguments for git clean, as you would type them on the command line. If you wanted to do `git clean -fd`, you'd set arguments to "-fd". :param logger: An initialized logger object :raises CommandFailedError: if git clean fails """ assert logger assert isinstance(arguments, basestring), ( "arguments must be a string, but is %r" % arguments) assert isinstance(path, basestring), "path must be a string, but is %r" % path command = ["git", "clean"] if arguments: command.append(arguments) logger.debug("* Running %s in %s", command, path) with changeToWorkingDir(path): return executeCommand(command=command, logger=logger)
def getGitRootFolder(): """ Return the root folder of the current YOMP repo @raises: infrastructure.utilities.exceptions.CommandFailedError if the command fails @returns: The full path of the root folder of the current YOMP repo @rtype: string """ return executeCommand("YOMP rev-parse --show-toplevel")
def add(pathspec): """ Add file contents to the index @param pathspec: The file that is to be added to YOMP. @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails """ command = "YOMP add %s" % pathspec return executeCommand(command)
def revParse(commitish, logger, **kwargs): """ Helper method to execute git rev-parse commands. Used to print the SHA1 given a revision specifier (e.g HEAD). This function can return the output of the command executed or the exit code of the command executed if "exitcode" = True is passed as a keyword argument. :param str commitish: The commit-ish. :param logger: logger for additional debug info :param bool verify: Boolean. Defaults to False. If True, verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. :param bool quiet: Boolean. Defaults to False. Only valid with verify. If True, do not output an error message if the first argument is not a valid object name; instead exit with non-zero status silently. 'verify' must be True. :param bool abbrevRef: Boolean. Defaults to False. If True, a non-ambiguous short name of the objects name. 'TypeError' exception will be raised if 'verify' and/or 'quiet' parameters are passed. :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :raises TypeError: Raised in following cases. - Invalid parameter is passed in **kwargs - "quiet" parameter is set without setting "verify" parameter - Both "verify" and "abbrevRef" parameters are set. :returns: A `string` representing a SHA or the exit code of the command. :rtype: str or int """ validKwargs = {"verify", "quiet", "abbrevRef"} checkKwargs(kwargs=kwargs, validKwargs=validKwargs) if (("quiet" in kwargs and "verify" not in kwargs) or ("abbrevRef" in kwargs and "verify" in kwargs)): raise TypeError("Invalid parameters passed.") command = ["git", "rev-parse"] if checkIfOptionSet("verify", kwargs): command.append("--verify") if checkIfOptionSet("quiet", kwargs): command.append("--quiet") elif checkIfOptionSet("abbrevRef", kwargs): command.append("--abbrev-ref") command.append(commitish) return executeCommand(command=command, logger=logger)
def getCurrentSha(): """ Get the current SHA of a given repo @raises: infrastructure.utilities.exceptions.CommandFailedError if the command fails; typically because you are not executing from within a YOMP repository @returns: The current SHA @rtype: string """ return executeCommand("YOMP log -n1 --pretty=%H")
def add(pathspec, logger): """ Add file contents to the index :param str pathspec: The file that is to be added to git. :param logger: logger for additional debug info :raises: infrastructure.utilities.exceptions.CommandFailedError: if the command fails """ command = ("git", "add", pathspec) return executeCommand(command=command, logger=logger)
def fetch(repository, refspec): """ Download objects and refs from another repository @param repository: Name of YOMP repository (e.g origin) @param refspec: Name of the refspec (e.g. master) @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails """ command = "YOMP fetch %s %s" % (repository, refspec) return executeCommand(command)
def getCommitCount(path): """ Get the commit count from a YOMP directory tree @param: path to YOMP directory @raises: infrastructure.utilities.exceptions.CommandFailedError: if path isn't in a YOMP checkout @returns: total commit count for the YOMP directory @rtype: string """ with changeToWorkingDir(path): return executeCommand("YOMP rev-list HEAD --count")
def getGitRootFolder(logger): """ Return the root folder of the current git repo :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :returns: The full path of the root folder of the current git repo :rtype: str """ command = ("git", "rev-parse", "--show-toplevel") return executeCommand(command=command, logger=logger)
def fetch(repository, refspec, logger): """ Download objects and refs from another repository :param str repository: Name of git repository (e.g origin) :param str refspec: Name of the refspec (e.g. master) :param logger: logger for additional debug info :raises: infrastructure.utilities.exceptions.CommandFailedError: if the command fails """ command = ("git", "fetch", repository, refspec) return executeCommand(command=command, logger=logger)
def getCurrentSha(logger): """ Get the current SHA of a given repo :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails; typically because you are not executing from within a git repository :returns: The current SHA :rtype: str """ command = ("git", "log", "-n1", "--pretty=%H") return executeCommand(command=command, logger=logger)
def removeFileFromGit(path): """ Remove files from the working tree and from the index. @param path: The file or path that has to be removed. @raises: infrastructure.utilities.exceptions.CommandFailedError: if the command fails @returns output of YOMP rm @rtype: str """ command = "YOMP rm -rf %s" % path return executeCommand(command)
def removeFileFromGit(path, logger): """ Remove files from the working tree and from the index. :param str path: The file or path that has to be removed. :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :returns: output of git rm :rtype: str """ command = ("git", "rm", "-rf", path) return executeCommand(command=command, logger=logger)
def getActiveBranch(): """ Get the active branch name for the repository @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails @returns: The active branch name or the current SHA if in a detached head state @rtype: string """ branch = executeCommand("git rev-parse --abbrev-ref HEAD") if branch == "HEAD": branch = getCurrentSha() return branch
def commit(message, **kwargs): """ Record changes to the repository Current implementation is supporting options like --amend This could be extended for other options as when required @param message: Commit message. @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails """ command = "YOMP commit " if checkIfOptionSet("amend", **kwargs): command += " --amend" command += " %s" % message return executeCommand(command)
def getModifiedFilesBetweenRevisions(startSha, endSha): """ Get a list of all files modified between revisions @param startSha: SHA to start searching from @param endSha: SHA to search until @raises: infrastructure.utilities.exceptions.CommandFailedError if the command fails; typically because you are not executing from within a git repository @returns: A `set` of modified files or None if the command fails @rtype: set """ return set(executeCommand("git diff --name-only %s...%s" % (startSha, endSha)).split("\n"))
def getCommitCount(path, logger): """ Get the commit count from a git directory tree :param str path: path to git directory :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if path isn't in a git checkout :returns: total commit count for the git directory :rtype: str """ with changeToWorkingDir(path): command = ("git", "rev-list", "HEAD", "--count") return executeCommand(command=command, logger=logger)
def reset(sha="", **kwargs): """ Resets the repository to a optional SHA. Optional argument for --hard @param kwargs: @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails @returns: The exit code @rtype: int """ command = "YOMP reset " if checkIfOptionSet("hard", **kwargs): command += "--hard" command += " %s" % sha return executeCommand(command)
def getActiveBranch(): """ Get the active branch name for the repository @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails infrastructure.utilities.exceptions.DetachedHeadError: if the YOMP checkout is in a detached head state @returns: The active branch name or the current SHA if in a detached head state @rtype: string """ branch = executeCommand("YOMP rev-parse --abbrev-ref HEAD") if branch == "HEAD": raise DetachedHeadError("There is no active branch; the head is detached.") return branch
def getModifiedFilesBetweenRevisions(startSha, endSha): """ Get a list of all files modified between revisions @param startSha: SHA to start searching from @param endSha: SHA to search until @raises: infrastructure.utilities.exceptions.CommandFailedError if the command fails; typically because you are not executing from within a YOMP repository @returns: A `set` of modified files or None if the command fails @rtype: set """ return set( executeCommand("YOMP diff --name-only %s...%s" % (startSha, endSha)).split("\n"))
def merge(path, message, **kwargs): """ Join two or more development histories together Current implementation supports --no-ff This could be extended for other options as when required. @param path: @param message: Merge commit message. @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails """ command = "YOMP merge " if checkIfOptionSet("noFF", **kwargs): command += " --no-ff" command += " -m %s %s" % (message, path) return executeCommand(command)
def getActiveBranch(): """ Get the active branch name for the repository @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails infrastructure.utilities.exceptions.DetachedHeadError: if the YOMP checkout is in a detached head state @returns: The active branch name or the current SHA if in a detached head state @rtype: string """ branch = executeCommand("YOMP rev-parse --abbrev-ref HEAD") if branch == "HEAD": raise DetachedHeadError( "There is no active branch; the head is detached.") return branch
def getModifiedFilesBetweenRevisions(startSha, endSha, logger): """ Get a list of all files modified between revisions :param str startSha: SHA to start searching from :param str endSha: SHA to search until :param logger: logger for additional debug info :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails; typically because you are not executing from within a git repository :returns: A `set` of modified files or None if the command fails :rtype: set """ command = ("git", "diff", "--name-only", "%s...%s" % (startSha, endSha)) return set(executeCommand(command=command, logger=logger).split("\n"))
def checkout(pathspec, logger, **kwargs): """ Switches to a given commit-ish :param str pathspec: The name of the branch (commit-ish) :param logger: logger for additional debug info :param bool new: Boolean. Defaults to False. If True, create a new branch. :param bool orphan: Boolean. Defaults to False. If True, create a new orphan branch. :param bool theirs: Boolean. Defaults to False. If True, when checking out paths from the index, check out stage #3 (theirs) for unmerged paths. :raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails :raises TypeError: Raised in following cases. - If invalid parameter is passed in **kwargs - More than one arguments are passed in **kwargs :returns: The text blob output of git checkout :rtype: str """ validKwargs = {"new", "orphan", "theirs"} checkKwargs(kwargs=kwargs, validKwargs=validKwargs) command = ["git", "checkout"] if len(kwargs) > 1: raise TypeError("Invalid parameters passed.") if checkIfOptionSet("new", kwargs): command.append("-b") elif checkIfOptionSet("orphan", kwargs): command.append("--orphan") elif checkIfOptionSet("theirs", kwargs): command.append("--theirs") command.append(pathspec) return executeCommand(command=command, logger=logger)
def clone(YOMPURL, **kwargs): """ Clones the given YOMP repository @param YOMPURL: The repository URL. @param kwargs: Various options to YOMP clone YOMPURL can be passed as keyword arguments. For now only directory option is handled. e.g. clone(YOMPURL, directory=nameOfDirectory) @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails @returns: The blob output of YOMP clone @rtype: string """ command = "YOMP clone %s" % YOMPURL if checkIfOptionSet("directory", **kwargs): command += " " + kwargs["directory"] return executeCommand(command)
def revParse(commitish, **kwargs): """ Helper method to execute YOMP rev-parse commands. Used to print the SHA1 given a revision specifier (e.g HEAD). This function can return the output of the command executed or the exit code of the command executed if "exitcode" = True is passed as a keyword argument. @param commitish: The commit-ish. @param kwargs: Various options to YOMP rev-parse can be passed as keyword arguments. The following options are currently supported: verify: Verify that exactly one parameter is provided, and that it can be turned into a raw 20-byte SHA-1 that can be used to access the object database. quiet: Only valid with verify. Do not output an error message if the first argument is not a valid object name; instead exit with non-zero status silently. abbrevRef: A non-ambiguous short name of the objects name @raises infrastructure.utilities.exceptions.CommandFailedError: if the command fails @returns: A `string` representing a SHA or the exit code of the command. @rtype: string or int """ command = "YOMP rev-parse" if checkIfOptionSet("verify", **kwargs): command += " --verify" elif checkIfOptionSet("quiet", **kwargs): command += " --quiet" elif checkIfOptionSet("abbrevRef", **kwargs): command += " --abbrev-ref" command += " %s" % commitish return executeCommand(command)