git-crrev-parse 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/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. # This git extension converts a chromium commit number to its git commit hash.
  6. # It accepts the following input formats:
  7. #
  8. # $ git crrev-parse Cr-Commit-Position: refs/heads/main@{#311769}
  9. # $ git crrev-parse ' Cr-Commit-Position: refs/heads/main@{#311769}'
  10. # $ git crrev-parse 'Cr-Commit-Position: refs/heads/main@{#311769}'
  11. # $ git crrev-parse refs/heads/main@{#311769}
  12. #
  13. # It also works for branches (assuming you have branches in your local
  14. # checkout):
  15. #
  16. # $ git crrev-parse refs/branch-heads/2278@{#2}
  17. #
  18. # If you don't specify a branch, refs/heads/main is assumed:
  19. #
  20. # $ git crrev-parse @{#311769}
  21. # $ git crrev-parse 311769
  22. # Developer note: this script makes heavy use of prefix/suffix/pattern
  23. # substitution for bash variables. Refer to the "Parameter Expansion"
  24. # section of the man page for bash.
  25. while [ -n "$1" ]; do
  26. if [[ "$1" = "Cr-Commit-Position:" ]] && [[ "$2" =~ .*@\{#[0-9][0-9]*\} ]]; then
  27. commit_pos="$2"
  28. shift
  29. else
  30. commit_pos="${1#*Cr-Commit-Position: }"
  31. fi
  32. ref="${commit_pos%@\{#*\}}"
  33. if [ "$ref" = "$commit_pos" -o -z "$ref" ]; then
  34. ref="refs/heads/main"
  35. fi
  36. remote_ref="${ref/refs\/heads/refs\/remotes\/origin}"
  37. remote_ref="${remote_ref/refs\/branch-heads/refs\/remotes\/branch-heads}"
  38. remote_ref="${remote_ref//\\}"
  39. num="${commit_pos#*@\{\#}"
  40. num="${num%\}}"
  41. if [ -z "$ref" -o -z "$num" ]; then
  42. git rev-parse "$1"
  43. else
  44. # When the ref is not specified on the command-line, accept either
  45. # Cr-refs/heads/master@{#$NUM} or refs/heads/main@{#$NUM}.
  46. if [[ "$ref" = "refs/heads/main" ]]; then
  47. grep_str="^Cr-Commit-Position: refs/heads/(master|main)@\\{#$num\\}"
  48. else
  49. grep_str="^Cr-Commit-Position: $ref@\\{#$num\\}"
  50. fi
  51. git rev-list -n 1 --extended-regexp --grep="$grep_str" "$remote_ref"
  52. fi
  53. shift
  54. done