blob: 5296573f2baee3b6fb4d3c9d91c05a4b56ca65e4 [file] [log] [blame]
szager@chromium.orgf9379b92015-01-23 00:56:04 +00001#!/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:
Jeff Carpenter056bef22017-01-25 12:51:26 -08009#
szager@chromium.orgf9379b92015-01-23 00:56:04 +000010# $ 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}
Jeff Carpenter056bef22017-01-25 12:51:26 -080014#
szager@chromium.orgf9379b92015-01-23 00:56:04 +000015# It also works for branches (assuming you have branches in your local
16# checkout):
Jeff Carpenter056bef22017-01-25 12:51:26 -080017#
szager@chromium.orgf9379b92015-01-23 00:56:04 +000018# $ git crrev-parse refs/branch-heads/2278@{#2}
Jeff Carpenter056bef22017-01-25 12:51:26 -080019#
szager@chromium.orgf9379b92015-01-23 00:56:04 +000020# If you don't specify a branch, refs/heads/master is assumed:
Jeff Carpenter056bef22017-01-25 12:51:26 -080021#
szager@chromium.orgf9379b92015-01-23 00:56:04 +000022# $ 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}"
Yuke Liao383ace42017-07-14 13:46:25 -070042 remote_ref="${remote_ref//\\}"
szager@chromium.orgf9379b92015-01-23 00:56:04 +000043 num="${commit_pos#*@\{\#}"
44 num="${num%\}}"
szager@chromium.orgf9379b92015-01-23 00:56:04 +000045 if [ -z "$ref" -o -z "$num" ]; then
46 git rev-parse "$1"
47 else
Jeff Carpenter056bef22017-01-25 12:51:26 -080048 grep_str="^Cr-Commit-Position: $ref@{#$num}"
szager@chromium.orgf9379b92015-01-23 00:56:04 +000049 git rev-list -n 1 --grep="$grep_str" "$remote_ref"
50 fi
51
52 shift
53done