# -*- coding: utf-8 -*-
# @Date:   2018-08-09 14:02:06

#!/usr/bin/python
#coding=utf-8

import os
import subprocess
import pdb

import sys
#sys.setdefaultencoding('utf-8')

from subprocess import Popen, PIPE, STDOUT

def printContent(info):
	print(info)

def printInfo(info):
	print("[Info] " + info)

def printWarn(info):
	print("[Warn] " + info)

def printError(info):
	print("[Error] " + info)

def printResult(resultCode, cmd, strExt):
	if strExt == None:
		strExt = ""
	if resultCode > 0:
		printWarn("<Error> %s code:%s cmd:%s " % (strExt,resultCode,cmd))
		raise Exception("error!!! %s" % (strExt))
	else:
		printWarn("<Ok> %s code:%s cmd:%s " % (strExt,resultCode,cmd))

def isExitGit(path):
	gitPath = ( "%s/.git" % path)
	if os.path.exists(gitPath):
		return True
	else:
		return False

def printCmdResult(p, cmd, strExt, showError):
	stdout, stderr = p.communicate()
	returncode = p.returncode
	if not stdout is None :
		stdout = stdout.decode('utf-8')
		printInfo(stdout[0:-1])
	if not stderr is None :
		stderr = stderr.decode('utf-8')
		printError(stderr[0:-1])
		raise Exception("error!!! %s" % (stderr))
	if showError == None or showError == True:
		printResult(returncode, cmd, strExt)

	return stdout

def callCmd(cmd, localPath, strExt, showError=None):
	if localPath == "":
		p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
		print("callCmd==>", cmd, localPath)
		return printCmdResult(p, cmd, strExt, showError)
	else:
		p = Popen(cmd, cwd=localPath, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
		print("callCmd==>", cmd, localPath)
		return printCmdResult(p, cmd, strExt, showError)

def pushChanges(branch, gitUrl, localPath):
	cmd = ("git push origin %s" % branch)
	callCmd(cmd, localPath, "pushChanges:")

def pushDeleteBranch(branch, gitUrl, localPath):
	cmd = ("git push origin :%s" % branch)
	callCmd(cmd, localPath, "pushDeleteBranch:")

def createBranch(branch, gitUrl, localPath):
	cmd = ("git checkout -b %s" % (branch))
	callCmd(cmd, localPath, "createBranch:")

def switchBranch(branch, gitUrl, localPath):
	cmd = ("git checkout master")
	callCmd(cmd, localPath, "switchBranch:_1")

	cmd = ("git pull origin")
	callCmd(cmd, localPath, "switchBranch:_2")

	if git_project_config.isDebugBranch:
		if git_project_branch.branch_config.has_key(gitUrl):
			branch = git_project_branch.branch_config[gitUrl]
			print("has branch ext config :" + branch)

	cmd = ("git checkout %s" % (branch))
	callCmd(cmd, localPath, "switchBranch:_3")

	remoteLastCommitKey = getLastCommitShaKey(branch, localPath)

	cmd = ("git reset --hard %s" % remoteLastCommitKey)
	callCmd(cmd, localPath, "switchBranch:_4")

def cloneBranch(branch, gitUrl, localPath):
	cmd = ("git clone -b %s %s %s" % (branch, gitUrl, localPath))
	returncode = subprocess.call(cmd, shell=True)
	printResult(returncode, cmd, "cloneBranch:")

def deleteBranch(branch, gitUrl, localPath):
	cmd = ("git branch -D %s" % (branch))
	callCmd(cmd, localPath, "deleteBranch:")

def cloneOrSwitch(branch, gitUrl, localPath):
	if isExitGit(localPath):
		printInfo("has exit local path : %s and then switch branch : %s" % (localPath,branch) )
		switchBranch(branch, gitUrl, localPath)
	else:
		printInfo("not exit local path : %s and then clone branch : %s" % (localPath,branch) )
		cloneBranch(branch, gitUrl, localPath)

def onlyUpdate(branch, gitUrl, localPath):
	cmd = "git checkout --"
	returncode = subprocess.call(cmd, cwd=localPath, shell=True)
	printResult(returncode, cmd, "onlyUpdate:")

def update(branch, gitUrl, localPath):
	pullAndResetOrigin(branch, localPath)

	cmd = "git checkout --"
	returncode = subprocess.call(cmd, cwd=localPath, shell=True)
	printResult(returncode, cmd, "update:_2")

def diffVersion(branch, gitUrl, localPath, diff1, diff2):
	cmd = ("git diff --name-status %s %s" % (diff1,diff2))
	return callCmd(cmd, localPath, "diffVersion:")

def diffBranch(branch, gitUrl, localPath, diff1, diff2):
	cmd = ("git diff --name-status %s %s" % (diff1,diff2))
	return callCmd(cmd, localPath, "diffBranch:")

def diffLocalAndRemote(branch, gitUrl, localPath):
	switchBranch(branch, gitUrl, localPath)
	cmd = ("git diff --name-status")
	return callCmd(cmd, localPath, "diffLocalAndRemote:")

def commitFolder(localPath, folderPath, message):
	cmd = ("git add -- %s" % folderPath)
	callCmd(cmd, localPath, "commitFolder_1:")
	cmd = ("git commit -m \"%s\" -- %s" % (message, folderPath))
	callCmd(cmd, localPath, "commitFolder_2:", False)
	cmd = ("git push origin")
	callCmd(cmd, localPath, "commitFolder_4:")

def commitFile(localPath, filePath, message):
	cmd = ("git add -- %s" % filePath)
	callCmd(cmd, localPath, "commitFile_1:")
	cmd = ("git commit -m \"%s\" -- %s" % (message, filePath))
	callCmd(cmd, localPath, "commitFile_2:", False)
	cmd = ("git push origin")
	callCmd(cmd, localPath, "commitFile_3:")

def addFile(localPath, filePath):
   cmd = ("git add -- %s" % filePath)
   callCmd(cmd, localPath, "commitFile_3:")

def commitFileNotAdd(localPath, filePath, message):
	cmd = ("git commit -m \"%s\" -- %s" % (message, filePath))
	callCmd(cmd, localPath, "commitFileNotAdd_1:", False)
	cmd = ("git push origin")
	callCmd(cmd, localPath, "commitFileNotAdd_2:")

def cleanUpFolder(localPath, folderPath):
	if not isExitGit(localPath):
		return

	cmd = ("git clean -f -d -- %s" % folderPath)
	callCmd(cmd, localPath, "cleanUpFolder_1:")

def cleanUpAndReset(localPath):
	if not isExitGit(localPath):
		return

	cmd = ("git clean -f -d")
	callCmd(cmd, localPath, "cleanUpAndReset_1:")
	cmd = ("git reset --hard")
	callCmd(cmd, localPath, "cleanUpAndReset_2:")
	pass

def updateFolder(branch, localPath, folderPath):
	pullAndResetOrigin(branch, localPath)

	cmd = ("git checkout HEAD -- %s" % folderPath)
	callCmd(cmd, localPath, "updateFolder:_1")

def updateFile(branch, localPath, singleFile):
	pullAndResetOrigin(branch, localPath)

	cmd = ("git checkout HEAD -- %s" % singleFile)
	callCmd(cmd, localPath, "updateFile:_1")

def getLastCommitShaKey(branch, localPath):
	cmd = ("git rev-parse origin/%s" % branch)
	string = callCmd(cmd, localPath, "getLastCommitShaKey")
	string = string[0:-1]
	return string

def getLocalCommitShaKey(branch, localPath):
	cmd = ("git rev-parse HEAD")
	return callCmd(cmd, localPath, "getLocalCommitShaKey")

def fetchOrigin(branch, localPath):
	cmd = ("git fetch origin")
	callCmd(cmd, localPath, "fetchOrigin")

def pullOrigin(localPath):
	cmd = ("git pull origin")
	callCmd(cmd, localPath, "pullOrigin")

def pullAndResetOrigin(branch, localPath):
	cmd = ("git pull origin")
	callCmd(cmd, localPath, "pullAndResetOrigin:_1")

	remoteLastCommitKey = getLastCommitShaKey(branch, localPath)

	cmd = ("git reset --hard %s" % remoteLastCommitKey)
	callCmd(cmd, localPath, "pullAndResetOrigin:_2")


def pullAndResetOrigin(branch, localPath):
	cmd = ("git pull origin")
	callCmd(cmd, localPath, "pullAndResetOrigin:_1")

	remoteLastCommitKey = getLastCommitShaKey(branch, localPath)

	cmd = ("git reset --hard %s" % remoteLastCommitKey)
	callCmd(cmd, localPath, "pullAndResetOrigin:_2")

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐