diff --git a/recipes/README.recipes.md b/recipes/README.recipes.md index 30731bcc5..3878f1568 100644 --- a/recipes/README.recipes.md +++ b/recipes/README.recipes.md @@ -383,7 +383,7 @@ Returns: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes ### *recipe_modules* / [git](/recipes/recipe_modules/git) -[DEPS](/recipes/recipe_modules/git/__init__.py#1): [infra\_paths](#recipe_modules-infra_paths), [recipe\_engine/context][recipe_engine/recipe_modules/context], [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/platform][recipe_engine/recipe_modules/platform], [recipe\_engine/properties][recipe_engine/recipe_modules/properties], [recipe\_engine/python][recipe_engine/recipe_modules/python], [recipe\_engine/raw\_io][recipe_engine/recipe_modules/raw_io], [recipe\_engine/runtime][recipe_engine/recipe_modules/runtime], [recipe\_engine/step][recipe_engine/recipe_modules/step] +[DEPS](/recipes/recipe_modules/git/__init__.py#1): [infra\_paths](#recipe_modules-infra_paths), [recipe\_engine/buildbucket][recipe_engine/recipe_modules/buildbucket], [recipe\_engine/context][recipe_engine/recipe_modules/context], [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/platform][recipe_engine/recipe_modules/platform], [recipe\_engine/properties][recipe_engine/recipe_modules/properties], [recipe\_engine/python][recipe_engine/recipe_modules/python], [recipe\_engine/raw\_io][recipe_engine/recipe_modules/raw_io], [recipe\_engine/runtime][recipe_engine/recipe_modules/runtime], [recipe\_engine/step][recipe_engine/recipe_modules/step] #### **class [GitApi](/recipes/recipe_modules/git/api.py#10)([RecipeApi][recipe_engine/wkt/RecipeApi]):** @@ -391,7 +391,7 @@ Returns: Return a git command step. -— **def [bundle\_create](/recipes/recipe_modules/git/api.py#383)(self, bundle_path, rev_list_args=None, \*\*kwargs):** +— **def [bundle\_create](/recipes/recipe_modules/git/api.py#382)(self, bundle_path, rev_list_args=None, \*\*kwargs):** Run 'git bundle create' on a Git repository. @@ -444,7 +444,7 @@ Args: Returns: If the checkout was successful, this returns the commit hash of the checked-out-repo. Otherwise this returns None. -— **def [config\_get](/recipes/recipe_modules/git/api.py#356)(self, prop_name, \*\*kwargs):** +— **def [config\_get](/recipes/recipe_modules/git/api.py#355)(self, prop_name, \*\*kwargs):** Returns: (str) The Git config output, or None if no output was generated. @@ -469,7 +469,7 @@ Returns: Fetches all tags from the remote. -— **def [get\_remote\_url](/recipes/recipe_modules/git/api.py#373)(self, remote_name=None, \*\*kwargs):** +— **def [get\_remote\_url](/recipes/recipe_modules/git/api.py#372)(self, remote_name=None, \*\*kwargs):** Returns: (str) The URL of the remote Git repository, or None. @@ -477,11 +477,11 @@ Args: remote_name: (str) The name of the remote to query, defaults to 'origin'. kwargs: Forwarded to '__call__'. -— **def [get\_timestamp](/recipes/recipe_modules/git/api.py#328)(self, commit='HEAD', test_data=None, \*\*kwargs):** +— **def [get\_timestamp](/recipes/recipe_modules/git/api.py#327)(self, commit='HEAD', test_data=None, \*\*kwargs):** Find and return the timestamp of the given commit. -— **def [new\_branch](/recipes/recipe_modules/git/api.py#396)(self, branch, name=None, upstream=None, \*\*kwargs):** +— **def [new\_branch](/recipes/recipe_modules/git/api.py#395)(self, branch, name=None, upstream=None, \*\*kwargs):** Runs git new-branch on a Git repository, to be used before git cl upload. @@ -491,7 +491,7 @@ Args: upstream (str): to origin/master. kwargs: Forwarded to '__call__'. -— **def [rebase](/recipes/recipe_modules/git/api.py#337)(self, name_prefix, branch, dir_path, remote_name=None, \*\*kwargs):** +— **def [rebase](/recipes/recipe_modules/git/api.py#336)(self, name_prefix, branch, dir_path, remote_name=None, \*\*kwargs):** Run rebase HEAD onto branch Args: @@ -924,9 +924,9 @@ Raises: — **def [RunSteps](/recipes/recipe_modules/gerrit/examples/full.py#11)(api):** ### *recipes* / [git:examples/full](/recipes/recipe_modules/git/examples/full.py) -[DEPS](/recipes/recipe_modules/git/examples/full.py#5): [git](#recipe_modules-git), [recipe\_engine/context][recipe_engine/recipe_modules/context], [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/platform][recipe_engine/recipe_modules/platform], [recipe\_engine/properties][recipe_engine/recipe_modules/properties], [recipe\_engine/raw\_io][recipe_engine/recipe_modules/raw_io], [recipe\_engine/runtime][recipe_engine/recipe_modules/runtime], [recipe\_engine/step][recipe_engine/recipe_modules/step] +[DEPS](/recipes/recipe_modules/git/examples/full.py#5): [git](#recipe_modules-git), [recipe\_engine/buildbucket][recipe_engine/recipe_modules/buildbucket], [recipe\_engine/context][recipe_engine/recipe_modules/context], [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/platform][recipe_engine/recipe_modules/platform], [recipe\_engine/properties][recipe_engine/recipe_modules/properties], [recipe\_engine/raw\_io][recipe_engine/recipe_modules/raw_io], [recipe\_engine/runtime][recipe_engine/recipe_modules/runtime], [recipe\_engine/step][recipe_engine/recipe_modules/step] -— **def [RunSteps](/recipes/recipe_modules/git/examples/full.py#18)(api):** +— **def [RunSteps](/recipes/recipe_modules/git/examples/full.py#19)(api):** ### *recipes* / [git\_cl:examples/full](/recipes/recipe_modules/git_cl/examples/full.py) [DEPS](/recipes/recipe_modules/git_cl/examples/full.py#9): [git\_cl](#recipe_modules-git_cl), [recipe\_engine/path][recipe_engine/recipe_modules/path], [recipe\_engine/raw\_io][recipe_engine/recipe_modules/raw_io], [recipe\_engine/step][recipe_engine/recipe_modules/step] diff --git a/recipes/recipe_modules/git/__init__.py b/recipes/recipe_modules/git/__init__.py index f8faf3672..019c635d1 100644 --- a/recipes/recipe_modules/git/__init__.py +++ b/recipes/recipe_modules/git/__init__.py @@ -1,4 +1,5 @@ DEPS = [ + 'recipe_engine/buildbucket', 'recipe_engine/context', 'recipe_engine/runtime', 'recipe_engine/path', diff --git a/recipes/recipe_modules/git/api.py b/recipes/recipe_modules/git/api.py index 6b86546cc..2cdb0ec93 100644 --- a/recipes/recipe_modules/git/api.py +++ b/recipes/recipe_modules/git/api.py @@ -217,29 +217,28 @@ class GitApi(recipe_api.RecipeApi): can_fail_build=can_fail_build) # There are five kinds of refs we can be handed: - # 0) None. In this case, we default to properties['branch']. - # 1) A 40-character SHA1 hash. - # 2) A fully-qualifed arbitrary ref, e.g. 'refs/foo/bar/baz'. - # 3) A fully qualified branch name, e.g. 'refs/heads/master'. - # Chop off 'refs/heads' and now it matches case (4). + # 0) None. In this case, we default to api.buildbucket.gitiles_commit.ref. + # 1) A fully qualified branch name, e.g. 'refs/heads/master'. + # Chop off 'refs/heads/' and now it matches case (4). + # 2) A 40-character SHA1 hash. + # 3) A fully-qualifed arbitrary ref, e.g. 'refs/foo/bar/baz'. # 4) A branch name, e.g. 'master'. # Note that 'FETCH_HEAD' can be many things (and therefore not a valid # checkout target) if many refs are fetched, but we only explicitly fetch # one ref here, so this is safe. + if not ref: # Case 0. + ref = self.m.buildbucket.gitiles_commit.ref or 'master' + + # If it's a fully-qualified branch name, trim the 'refs/heads/' prefix. + if ref.startswith('refs/heads/'): # Case 1. + ref = ref[len('refs/heads/'):] + fetch_args = [] - if not ref: # Case 0 - fetch_remote = remote_name - fetch_ref = self.m.properties.get('branch') or 'master' - checkout_ref = 'FETCH_HEAD' - elif self._GIT_HASH_RE.match(ref): # Case 1. + if self._GIT_HASH_RE.match(ref): # Case 2. fetch_remote = remote_name fetch_ref = '' checkout_ref = ref - elif ref.startswith('refs/heads/'): # Case 3. - fetch_remote = remote_name - fetch_ref = ref[len('refs/heads/'):] - checkout_ref = 'FETCH_HEAD' - else: # Cases 2 and 4. + else: # Cases 3 and 4. fetch_remote = remote_name fetch_ref = ref checkout_ref = 'FETCH_HEAD' diff --git a/recipes/recipe_modules/git/examples/full.py b/recipes/recipe_modules/git/examples/full.py index 6461ef5bb..77321f017 100644 --- a/recipes/recipe_modules/git/examples/full.py +++ b/recipes/recipe_modules/git/examples/full.py @@ -3,6 +3,7 @@ # found in the LICENSE file. DEPS = [ + 'recipe_engine/buildbucket', 'recipe_engine/context', 'recipe_engine/path', 'recipe_engine/platform', @@ -29,9 +30,11 @@ def RunSteps(api): True) # You can use api.git.checkout to perform all the steps of a safe checkout. + revision = (api.buildbucket.gitiles_commit.ref or + api.buildbucket.gitiles_commit.id) retVal = api.git.checkout( url, - ref=api.properties.get('revision'), + ref=revision, recursive=True, submodule_update_force=submodule_update_force, set_got_revision=api.properties.get('set_got_revision'), @@ -84,7 +87,7 @@ def RunSteps(api): if api.properties.get('cat_file', None): step_result = api.git.cat_file_at_commit(api.properties['cat_file'], - api.properties['revision'], + revision, stdout=api.raw_io.output()) if 'TestOutput' in step_result.stdout: pass # Success! @@ -101,18 +104,22 @@ def GenTests(api): api.runtime(is_luci=True, is_experimental=False) ) yield api.test('basic_tags') + api.properties(tags=True) - yield api.test('basic_ref') + api.properties(revision='refs/foo/bar') - yield api.test('basic_branch') + api.properties(revision='refs/heads/testing') - yield api.test('basic_hash') + api.properties( - revision='abcdef0123456789abcdef0123456789abcdef01') + yield api.test('basic_ref') + api.buildbucket.ci_build(git_ref='refs/foo/bar') + yield api.test('basic_branch') + api.buildbucket.ci_build( + git_ref='refs/heads/testing') + yield api.test('basic_hash') + api.buildbucket.ci_build( + revision='abcdef0123456789abcdef0123456789abcdef01', git_ref=None) yield api.test('basic_file_name') + api.properties(checkout_file_name='DEPS') yield api.test('basic_submodule_update_force') + api.properties( submodule_update_force=True) yield api.test('platform_win') + api.platform.name('win') - yield api.test('curl_trace_file') + api.properties( - revision='refs/foo/bar', use_curl_trace=True) + yield ( + api.test('curl_trace_file') + + api.properties(use_curl_trace=True) + + api.buildbucket.ci_build(git_ref='refs/foo/bar') + ) yield ( api.test('can_fail_build') + @@ -160,7 +167,8 @@ def GenTests(api): api.test('cat-file_test') + api.step_data('git cat-file abcdef12345:TestFile', stdout=api.raw_io.output('TestOutput')) + - api.properties(revision='abcdef12345', cat_file='TestFile')) + api.buildbucket.ci_build(revision='abcdef12345', git_ref=None) + + api.properties(cat_file='TestFile')) yield ( api.test('git-cache-checkout') +