本文整理汇总了Python中git.Git.stash方法的典型用法代码示例。如果您正苦于以下问题:Python Git.stash方法的具体用法?Python Git.stash怎么用?Python Git.stash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类git.Git
的用法示例。
在下文中一共展示了Git.stash方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plant_seed
# 需要导入模块: from git import Git [as 别名]
# 或者: from git.Git import stash [as 别名]
def plant_seed(self, seed_id=None, target_dir=None):
if not seed_id or not target_dir:
self.rage_quit("Missing arguments, seed plant failed")
seeds = self.get_seeds()
tagref = seeds.get(seed_id, None)
if not tagref:
self.rage_quit("Seed id {} not found".format(seed_id))
git = Git(tagref.repo.working_dir)
current_commit = str(Repo(tagref.repo.working_dir).commit())
cprint("Current commit: {}".format(current_commit), Fore.GREEN)
dirty = tagref.repo.is_dirty()
cprint("Working directory {}".format('dirty' if dirty else 'clean'), Fore.GREEN)
if dirty:
cprint("--> git stash", Fore.YELLOW)
git.stash()
cprint("--> git checkout {}".format(seed_id), Fore.YELLOW)
git.checkout(seed_id)
try:
cprint("Copying seed directory: {}".format(tagref.repo.working_dir), Fore.GREEN)
call(["cp", "-r", tagref.repo.working_dir, target_dir])
except OSError as error:
cprint("Copying directory failed:\n{}".format(error), Fore.RED)
finally:
if dirty:
cprint("--> git stash apply", Fore.YELLOW)
git.stash('apply')
cprint("--> git checkout {}".format(current_commit), Fore.YELLOW)
git.checkout(current_commit)
示例2: initialize
# 需要导入模块: from git import Git [as 别名]
# 或者: from git.Git import stash [as 别名]
def initialize(path, branch):
try:
repo = Repo(path)
git = Git(path)
except InvalidGitRepositoryError as err:
print "Not a valid Git repo: %s" % err
sys.exit("Run `git init %s` first" % err)
output = ['Initializing in %s [%s]' % (path, branch)]
current_branch = repo.active_branch
if branch in [b.name for b in repo.branches]:
output.append("error: A branch named '%s' already exists in this repo!"
% branch)
return output
output.append("--> Stashing current branch contents [%s]" % current_branch)
try:
cmd = git.stash(u=True)
for line in cmd.splitlines():
output.append(" %s" % line)
except GitCommandError as err:
output.append("error: %s" % err)
return output
output.append("--> Switching to branch '%s'" % branch)
try:
git.checkout(B=branch)
except GitCommandError as err:
output.append("error: %s" % err)
return output
output.append("--> Clearing current files and committing")
try:
files = os.listdir(path)
files.remove('.git')
for entry in files:
git.rm(entry, r=True, f=True)
cmd = git.commit(m="Clearing files in preparation for git-pm")
for line in cmd.splitlines():
output.append(" %s" % line)
except GitCommandError as err:
output.append("error: %s" % err)
return output
output.append("--> Creating git-pm file structure and committing")
try:
for directory in DIRECTORIES:
dir_path = os.path.join(path, directory)
gitkeep = os.path.join(dir_path, '.gitkeep')
os.mkdir(dir_path)
with open(gitkeep, 'a'):
os.utime(gitkeep, None)
cmd = git.commit(m="Created git-pm file structure")
for line in cmd.splitlines():
output.append(" %s" % line)
except GitCommandError as err:
output.append("error: %s" % err)
return output
output.append("--> Returning to previous branch and popping stash")
try:
git.checkout(current_branch)
git.stash("pop")
except GitCommandError as err:
output.append("error: %s" % err)
return output
return output
示例3: GitWrapper
# 需要导入模块: from git import Git [as 别名]
# 或者: from git.Git import stash [as 别名]
class GitWrapper(object):
"""
A wrapper for repo.git providing better stdout handling + better exeptions.
It is preferred to repo.git because it doesn't print to stdout
in real time. In addition, this wrapper provides better error
handling (it provides stdout messages inside the exception, too).
"""
def __init__(self, repo):
if repo:
#: :type: git.Repo
self.repo = repo
#: :type: git.Git
self.git = self.repo.git
else:
#: :type: git.Git
self.git = Git()
def __del__(self):
# Is the following true?
# GitPython runs persistent git processes in the working directory.
# Therefore, when we use 'git up' in something like a test environment,
# this might cause troubles because of the open file handlers (like
# trying to remove the directory right after the test has finished).
# 'clear_cache' kills the processes...
if platform.system() == 'Windows': # pragma: no cover
pass
# ... or rather "should kill", because but somehow it recently
# started to not kill cat_file_header out of the blue (I even
# tried running old code, but the once working code failed).
# Thus, we kill it manually here.
if self.git.cat_file_header is not None:
subprocess.call(("TASKKILL /F /T /PID {0} 2>nul 1>nul".format(
str(self.git.cat_file_header.proc.pid)
)), shell=True)
if self.git.cat_file_all is not None:
subprocess.call(("TASKKILL /F /T /PID {0} 2>nul 1>nul".format(
str(self.git.cat_file_all.proc.pid)
)), shell=True)
self.git.clear_cache()
def run(self, name, *args, **kwargs):
""" Run a git command specified by name and args/kwargs. """
tostdout = kwargs.pop('tostdout', False)
stdout = six.b('')
# Execute command
cmd = getattr(self.git, name)(as_process=True, *args, **kwargs)
# Capture output
while True:
output = cmd.stdout.read(1)
# Print to stdout
if tostdout:
sys.stdout.write(output.decode('utf-8'))
sys.stdout.flush()
stdout += output
if output == six.b(""):
break
# Wait for the process to quit
try:
cmd.wait()
except GitCommandError as error:
# Add more meta-information to errors
message = "'{0}' returned exit status {1}".format(
' '.join(str(c) for c in error.command),
error.status
)
raise GitError(message, stderr=error.stderr, stdout=stdout)
return stdout.strip()
def __getattr__(self, name):
return lambda *args, **kwargs: self.run(name, *args, **kwargs)
###########################################################################
# Overwrite some methods and add new ones
###########################################################################
@contextmanager
def stash(self):
"""
A stashing contextmanager.
It stashes all changes inside and unstashed when done.
"""
stashed = False
if self.repo.is_dirty(submodules=False):
if self.change_count > 1:
message = 'stashing {0} changes'
#.........这里部分代码省略.........