blob: 77318fe6e05f6e8de2c1cfd679a4a5b1c2dce62b [file] [log] [blame]
[email protected]f9379b92015-01-23 00:56:041#!/usr/bin/env bash
2# Copyright 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7# This git extension converts a chromium commit number to its git commit hash.
8# It accepts the following input formats:
9#
10# $ git crrev-parse Cr-Commit-Position: refs/heads/master@{#311769}
11# $ git crrev-parse ' Cr-Commit-Position: refs/heads/master@{#311769}'
12# $ git crrev-parse 'Cr-Commit-Position: refs/heads/master@{#311769}'
13# $ git crrev-parse refs/heads/master@{#311769}
14#
15# It also works for branches (assuming you have branches in your local
16# checkout):
17#
18# $ git crrev-parse refs/branch-heads/2278@{#2}
19#
20# If you don't specify a branch, refs/heads/master is assumed:
21#
22# $ git crrev-parse @{#311769}
23# $ git crrev-parse 311769
24
25# Developer note: this script makes heavy use of prefix/suffix/pattern
26# substitution for bash variables. Refer to the "Parameter Expansion"
27# section of the man page for bash.
28
29while [ -n "$1" ]; do
30 if [[ "$1" = "Cr-Commit-Position:" ]] && [[ "$2" =~ .*@\{#[0-9][0-9]*\} ]]; then
31 commit_pos="$2"
32 shift
33 else
34 commit_pos="${1#*Cr-Commit-Position: }"
35 fi
36 ref="${commit_pos%@\{#*\}}"
37 if [ "$ref" = "$commit_pos" -o -z "$ref" ]; then
38 ref="refs/heads/master"
39 fi
40 remote_ref="${ref/refs\/heads/refs\/remotes\/origin}"
41 remote_ref="${remote_ref/refs\/branch-heads/refs\/remotes\/branch-heads}"
42 num="${commit_pos#*@\{\#}"
43 num="${num%\}}"
44
45 if [ -z "$ref" -o -z "$num" ]; then
46 git rev-parse "$1"
47 else
48 grep_str="Cr-Commit-Position: $ref@{#$num}"
49 git rev-list -n 1 --grep="$grep_str" "$remote_ref"
50 fi
51
52 shift
53done