本文整理汇总了Python中pants.scm.git.Git类的典型用法代码示例。如果您正苦于以下问题:Python Git类的具体用法?Python Git怎么用?Python Git使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Git类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_detect_worktree_failing_git
def test_detect_worktree_failing_git(self):
with self.executable_git() as git:
with open(git, 'w') as fp:
fp.write('#!/bin/sh\n')
fp.write('exit 1')
self.assertIsNone(Git.detect_worktree())
self.assertIsNone(Git.detect_worktree(git))
示例2: test_detect_worktree_working_git
def test_detect_worktree_working_git(self):
expected_worktree_dir = '/a/fake/worktree/dir'
with self.executable_git() as git:
with open(git, 'w') as fp:
fp.write('#!/bin/sh\n')
fp.write('echo ' + expected_worktree_dir)
self.assertEqual(expected_worktree_dir, Git.detect_worktree())
self.assertEqual(expected_worktree_dir, Git.detect_worktree(binary=git))
示例3: test_detect_worktree_somewhere_else
def test_detect_worktree_somewhere_else(self):
with temporary_dir() as somewhere_else:
with pushd(somewhere_else):
loc = Git.detect_worktree(dir=somewhere_else)
self.assertEquals(None, loc)
subprocess.check_call(['git', 'init'])
loc = Git.detect_worktree(dir=somewhere_else)
self.assertEquals(os.path.realpath(somewhere_else), loc)
示例4: test
def test(self):
self.assertEqual(set(), self.git.changed_files())
self.assertEqual(set(['README']), self.git.changed_files(from_commit='HEAD^'))
tip_sha = self.git.commit_id
self.assertTrue(tip_sha)
self.assertTrue(tip_sha in self.git.changelog())
self.assertTrue(self.git.tag_name.startswith('first-'), msg='un-annotated tags should be found')
self.assertEqual('master', self.git.branch_name)
def edit_readme():
with open(self.readme_file, 'a') as readme:
readme.write('More data.')
edit_readme()
with open(os.path.join(self.worktree, 'INSTALL'), 'w') as untracked:
untracked.write('make install')
self.assertEqual(set(['README']), self.git.changed_files())
self.assertEqual(set(['README', 'INSTALL']), self.git.changed_files(include_untracked=True))
try:
# These changes should be rejected because our branch point from origin is 1 commit behind
# the changes pushed there in clone 2.
self.git.commit('API Changes.')
except Scm.RemoteException:
with environment_as(GIT_DIR=self.gitdir, GIT_WORK_TREE=self.worktree):
subprocess.check_call(['git', 'reset', '--hard', 'depot/master'])
self.git.refresh()
edit_readme()
self.git.commit('''API '"' " Changes.''')
self.git.tag('second', message='''Tagged ' " Changes''')
with temporary_dir() as clone:
with pushd(clone):
self.init_repo('origin', self.origin)
subprocess.check_call(['git', 'pull', '--tags', 'origin', 'master:master'])
with open(os.path.realpath('README')) as readme:
self.assertEqual('--More data.', readme.read())
git = Git()
# Check that we can pick up committed and uncommitted changes.
with safe_open(os.path.realpath('CHANGES'), 'w') as changes:
changes.write('none')
subprocess.check_call(['git', 'add', 'CHANGES'])
self.assertEqual(set(['README', 'CHANGES']), git.changed_files(from_commit='first'))
self.assertEqual('master', git.branch_name)
self.assertEqual('second', git.tag_name, msg='annotated tags should be found')
示例5: select
def select(self, context):
self.get_options()
workdir = os.path.join(self.get_options().pants_workdir, self.options_scope,
'versions', self.get_options().version)
tool_path = os.path.join(workdir, 'bin/protoc-gen-go')
if not os.path.exists(tool_path):
safe_mkdir(workdir, clean=True)
# Checkout the git repo at a given version. `go get` always gets master.
repo = Git.clone('https://github.com/golang/protobuf.git',
os.path.join(workdir, 'src/github.com/golang/protobuf'))
repo.set_state(self.get_options().version)
go = GoDistribution.global_instance()
result, go_cmd = go.execute_go_cmd(
cmd='install',
gopath=workdir,
args=['github.com/golang/protobuf/protoc-gen-go'],
workunit_factory=context.new_workunit,
workunit_labels=[WorkUnitLabel.BOOTSTRAP],
)
if result != 0:
raise SubsystemError('{} failed with exit code {}'.format(go_cmd, result))
logger.info('Selected {} binary bootstrapped to: {}'.format(self.options_scope, tool_path))
return tool_path
示例6: project_template
def project_template(self):
target_levels = {Revision.lenient(platform['target_level'])
for platform in self.blob['jvm_platforms']['platforms'].values()}
lang_level = max(target_levels) if target_levels else Revision(1, 8)
configured_project = TemplateData(
root_dir=get_buildroot(),
outdir=self.output_directory,
git_root=Git.detect_worktree(),
modules=self.module_templates_by_filename.values(),
java=TemplateData(
encoding=self.java_encoding,
maximum_heap_size=self.java_maximum_heap_size,
jdk='{0}.{1}'.format(*lang_level.components[:2]),
language_level='JDK_{0}_{1}'.format(*lang_level.components[:2]),
),
resource_extensions=[],
scala=None,
checkstyle_classpath=';'.join([]),
debug_port=self.debug_port,
annotation_processing=self.annotation_processing_template,
extra_components=[],
junit_tests=self._junit_tests_config(),
global_junit_vm_parameters=' '.join(self.global_junit_jvm_options),
)
return configured_project
示例7: setUp
def setUp(self):
self.origin = safe_mkdtemp()
with pushd(self.origin):
subprocess.check_call(['git', 'init', '--bare'])
self.gitdir = safe_mkdtemp()
self.worktree = safe_mkdtemp()
self.readme_file = os.path.join(self.worktree, 'README')
with environment_as(GIT_DIR=self.gitdir, GIT_WORK_TREE=self.worktree):
self.init_repo('depot', self.origin)
touch(self.readme_file)
subprocess.check_call(['git', 'add', 'README'])
subprocess.check_call(['git', 'commit', '-am', 'initial commit with decode -> \x81b'])
subprocess.check_call(['git', 'tag', 'first'])
subprocess.check_call(['git', 'push', '--tags', 'depot', 'master'])
subprocess.check_call(['git', 'branch', '--set-upstream', 'master', 'depot/master'])
with safe_open(self.readme_file, 'w') as readme:
readme.write('Hello World.')
subprocess.check_call(['git', 'commit', '-am', 'Update README.'])
self.clone2 = safe_mkdtemp()
with pushd(self.clone2):
self.init_repo('origin', self.origin)
subprocess.check_call(['git', 'pull', '--tags', 'origin', 'master:master'])
with safe_open(os.path.realpath('README'), 'a') as readme:
readme.write('--')
subprocess.check_call(['git', 'commit', '-am', 'Update README 2.'])
subprocess.check_call(['git', 'push', '--tags', 'origin', 'master'])
self.git = Git(gitdir=self.gitdir, worktree=self.worktree)
示例8: worktree_relative_to
def worktree_relative_to(some_dir, expected):
# Given a directory relative to the worktree, tests that the worktree is detected as 'expected'.
subdir = os.path.join(clone, some_dir)
if not os.path.isdir(subdir):
os.mkdir(subdir)
actual = Git.detect_worktree(subdir=subdir)
self.assertEqual(expected, actual)
示例9: setUp
def setUp(self):
self.origin = safe_mkdtemp()
with pushd(self.origin):
subprocess.check_call(['git', 'init', '--bare'])
self.gitdir = safe_mkdtemp()
self.worktree = safe_mkdtemp()
self.readme_file = os.path.join(self.worktree, 'README')
with environment_as(GIT_DIR=self.gitdir, GIT_WORK_TREE=self.worktree):
self.init_repo('depot', self.origin)
touch(self.readme_file)
subprocess.check_call(['git', 'add', 'README'])
safe_mkdir(os.path.join(self.worktree, 'dir'))
with open(os.path.join(self.worktree, 'dir', 'f'), 'w') as f:
f.write("file in subdir")
# Make some symlinks
os.symlink('f', os.path.join(self.worktree, 'dir', 'relative-symlink'))
os.symlink('no-such-file', os.path.join(self.worktree, 'dir', 'relative-nonexistent'))
os.symlink('dir/f', os.path.join(self.worktree, 'dir', 'not-absolute\u2764'))
os.symlink('../README', os.path.join(self.worktree, 'dir', 'relative-dotdot'))
os.symlink('dir', os.path.join(self.worktree, 'link-to-dir'))
os.symlink('README/f', os.path.join(self.worktree, 'not-a-dir'))
os.symlink('loop1', os.path.join(self.worktree, 'loop2'))
os.symlink('loop2', os.path.join(self.worktree, 'loop1'))
subprocess.check_call(['git', 'add', 'README', 'dir', 'loop1', 'loop2',
'link-to-dir', 'not-a-dir'])
subprocess.check_call(['git', 'commit', '-am', 'initial commit with decode -> \x81b'])
self.initial_rev = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
subprocess.check_call(['git', 'tag', 'first'])
subprocess.check_call(['git', 'push', '--tags', 'depot', 'master'])
subprocess.check_call(['git', 'branch', '--set-upstream-to', 'depot/master'])
with safe_open(self.readme_file, 'w') as readme:
readme.write('Hello World.\u2764'.encode('utf-8'))
subprocess.check_call(['git', 'commit', '-am', 'Update README.'])
self.current_rev = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip()
self.clone2 = safe_mkdtemp()
with pushd(self.clone2):
self.init_repo('origin', self.origin)
subprocess.check_call(['git', 'pull', '--tags', 'origin', 'master:master'])
with safe_open(os.path.realpath('README'), 'a') as readme:
readme.write('--')
subprocess.check_call(['git', 'commit', '-am', 'Update README 2.'])
subprocess.check_call(['git', 'push', '--tags', 'origin', 'master'])
self.git = Git(gitdir=self.gitdir, worktree=self.worktree)
示例10: __call__
def __call__(self, manifest_entries=None):
"""Returns a dict suitable for passing to 'manifest_entries' in a 'jvm_binary() definition"""
manifest_entries = manifest_entries or {}
buildroot = get_buildroot()
worktree = Git.detect_worktree(subdir=os.path.join(buildroot,
self._parse_context.rel_path))
if worktree:
git = Git(worktree=worktree)
manifest_entries['Implementation-Version'] = git.commit_id
manifest_entries['Built-By'] = pwd.getpwuid(os.getuid()).pw_name
return manifest_entries
示例11: worktree_relative_to
def worktree_relative_to(cwd, expected):
"""Given a cwd relative to the worktree, tests that the worktree is detected as 'expected'."""
orig_cwd = os.getcwd()
try:
abs_cwd = os.path.join(clone, cwd)
if not os.path.isdir(abs_cwd):
os.mkdir(abs_cwd)
os.chdir(abs_cwd)
actual = Git.detect_worktree()
self.assertEqual(expected, actual)
finally:
os.chdir(orig_cwd)
示例12: fetch
def fetch(self, dest, rev=None):
imported_repo = self._meta_tag_reader.get_imported_repo(self.import_path)
if not imported_repo:
raise FetchError('No <meta name="go-import"> tag found, so cannot fetch repo '
'at {}'.format(self.import_path))
if imported_repo.vcs != 'git':
# TODO: Support other vcs systems as needed.
raise FetchError("Don't know how to fetch for vcs type {}.".format(imported_repo.vcs))
# TODO: Do this in a workunit (see https://github.com/pantsbuild/pants/issues/3502).
logger.info('Cloning {} into {}'.format(imported_repo.url, dest))
repo = Git.clone(imported_repo.url, dest)
if rev:
repo.set_state(rev)
示例13: get_scm
def get_scm():
"""Returns the pants Scm if any."""
# TODO(John Sirois): Extract a module/class to carry the bootstrap logic.
global _SCM
if not _SCM:
from pants.scm.git import Git
# We know about git, so attempt an auto-configure
worktree = Git.detect_worktree()
if worktree and os.path.isdir(worktree):
git = Git(worktree=worktree)
try:
logger.info('Detected git repository at {} on branch {}'.format(worktree, git.branch_name))
set_scm(git)
except git.LocalException as e:
logger.info('Failed to load git repository at {}: {}'.format(worktree, e))
return _SCM
示例14: generate_project
def generate_project(self, project):
def create_content_root(source_set):
root_relative_path = os.path.join(source_set.source_base, source_set.path) \
if source_set.path else source_set.source_base
if self.get_options().infer_test_from_siblings:
is_test = IdeaGen._sibling_is_test(source_set)
else:
is_test = source_set.is_test
if source_set.resources_only:
if source_set.is_test:
content_type = 'java-test-resource'
else:
content_type = 'java-resource'
else:
content_type = ''
sources = TemplateData(
path=root_relative_path,
package_prefix=source_set.path.replace('/', '.') if source_set.path else None,
is_test=is_test,
content_type=content_type
)
return TemplateData(
path=root_relative_path,
sources=[sources],
exclude_paths=[os.path.join(source_set.source_base, x) for x in source_set.excludes],
)
content_roots = [create_content_root(source_set) for source_set in project.sources]
if project.has_python:
content_roots.extend(create_content_root(source_set) for source_set in project.py_sources)
scala = None
if project.has_scala:
scala = TemplateData(
language_level=self.scala_language_level,
maximum_heap_size=self.scala_maximum_heap_size,
fsc=self.fsc,
compiler_classpath=project.scala_compiler_classpath
)
exclude_folders = []
if self.get_options().exclude_maven_target:
exclude_folders += IdeaGen._maven_targets_excludes(get_buildroot())
exclude_folders += self.get_options().exclude_folders
java_language_level = None
for target in project.targets:
if isinstance(target, JvmTarget):
if java_language_level is None or java_language_level < target.platform.source_level:
java_language_level = target.platform.source_level
if java_language_level is not None:
java_language_level = 'JDK_{0}_{1}'.format(*java_language_level.components[:2])
configured_module = TemplateData(
root_dir=get_buildroot(),
path=self.module_filename,
content_roots=content_roots,
bash=self.bash,
python=project.has_python,
scala=scala,
internal_jars=[cp_entry.jar for cp_entry in project.internal_jars],
internal_source_jars=[cp_entry.source_jar for cp_entry in project.internal_jars
if cp_entry.source_jar],
external_jars=[cp_entry.jar for cp_entry in project.external_jars],
external_javadoc_jars=[cp_entry.javadoc_jar for cp_entry in project.external_jars
if cp_entry.javadoc_jar],
external_source_jars=[cp_entry.source_jar for cp_entry in project.external_jars
if cp_entry.source_jar],
annotation_processing=self.annotation_processing_template,
extra_components=[],
exclude_folders=exclude_folders,
java_language_level=java_language_level,
)
outdir = os.path.abspath(self.intellij_output_dir)
if not os.path.exists(outdir):
os.makedirs(outdir)
configured_project = TemplateData(
root_dir=get_buildroot(),
outdir=outdir,
git_root=Git.detect_worktree(),
modules=[configured_module],
java=TemplateData(
encoding=self.java_encoding,
maximum_heap_size=self.java_maximum_heap_size,
jdk=self.java_jdk,
language_level='JDK_1_{}'.format(self.java_language_level)
),
resource_extensions=list(project.resource_extensions),
scala=scala,
checkstyle_classpath=';'.join(project.checkstyle_classpath),
debug_port=project.debug_port,
annotation_processing=self.annotation_processing_template,
extra_components=[],
#.........这里部分代码省略.........
示例15: GitTest
class GitTest(unittest.TestCase):
@staticmethod
def init_repo(remote_name, remote):
subprocess.check_call(['git', 'init'])
subprocess.check_call(['git', 'config', 'user.email', '[email protected]'])
subprocess.check_call(['git', 'config', 'user.name', 'Your Name'])
subprocess.check_call(['git', 'remote', 'add', remote_name, remote])
@classmethod
def setUp(self):
self.origin = safe_mkdtemp()
with pushd(self.origin):
subprocess.check_call(['git', 'init', '--bare'])
self.gitdir = safe_mkdtemp()
self.worktree = safe_mkdtemp()
self.readme_file = os.path.join(self.worktree, 'README')
with environment_as(GIT_DIR=self.gitdir, GIT_WORK_TREE=self.worktree):
self.init_repo('depot', self.origin)
touch(self.readme_file)
subprocess.check_call(['git', 'add', 'README'])
subprocess.check_call(['git', 'commit', '-am', 'initial commit with decode -> \x81b'])
subprocess.check_call(['git', 'tag', 'first'])
subprocess.check_call(['git', 'push', '--tags', 'depot', 'master'])
subprocess.check_call(['git', 'branch', '--set-upstream', 'master', 'depot/master'])
with safe_open(self.readme_file, 'w') as readme:
readme.write('Hello World.')
subprocess.check_call(['git', 'commit', '-am', 'Update README.'])
self.clone2 = safe_mkdtemp()
with pushd(self.clone2):
self.init_repo('origin', self.origin)
subprocess.check_call(['git', 'pull', '--tags', 'origin', 'master:master'])
with safe_open(os.path.realpath('README'), 'a') as readme:
readme.write('--')
subprocess.check_call(['git', 'commit', '-am', 'Update README 2.'])
subprocess.check_call(['git', 'push', '--tags', 'origin', 'master'])
self.git = Git(gitdir=self.gitdir, worktree=self.worktree)
@staticmethod
@contextmanager
def mkremote(remote_name):
with temporary_dir() as remote_uri:
subprocess.check_call(['git', 'remote', 'add', remote_name, remote_uri])
try:
yield remote_uri
finally:
subprocess.check_call(['git', 'remote', 'remove', remote_name])
@classmethod
def tearDown(self):
safe_rmtree(self.origin)
safe_rmtree(self.gitdir)
safe_rmtree(self.worktree)
safe_rmtree(self.clone2)
def test_integration(self):
self.assertEqual(set(), self.git.changed_files())
self.assertEqual(set(['README']), self.git.changed_files(from_commit='HEAD^'))
tip_sha = self.git.commit_id
self.assertTrue(tip_sha)
self.assertTrue(tip_sha in self.git.changelog())
merge_base = self.git.merge_base()
self.assertTrue(merge_base)
self.assertTrue(merge_base in self.git.changelog())
with pytest.raises(Scm.LocalException):
self.git.server_url
with environment_as(GIT_DIR=self.gitdir, GIT_WORK_TREE=self.worktree):
with self.mkremote('origin') as origin_uri:
origin_url = self.git.server_url
self.assertEqual(origin_url, origin_uri)
self.assertTrue(self.git.tag_name.startswith('first-'), msg='un-annotated tags should be found')
self.assertEqual('master', self.git.branch_name)
def edit_readme():
with open(self.readme_file, 'a') as readme:
readme.write('More data.')
edit_readme()
with open(os.path.join(self.worktree, 'INSTALL'), 'w') as untracked:
untracked.write('make install')
self.assertEqual(set(['README']), self.git.changed_files())
self.assertEqual(set(['README', 'INSTALL']), self.git.changed_files(include_untracked=True))
# confirm that files outside of a given relative_to path are ignored
self.assertEqual(set(), self.git.changed_files(relative_to='non-existent'))
#.........这里部分代码省略.........