xref: /aosp_15_r20/external/skia/experimental/tools/gerrit_percent_encode (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker#! /usr/bin/env python
2*c8dee2aaSAndroid Build Coastguard Worker# Copyright 2019 Google LLC.
3*c8dee2aaSAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*c8dee2aaSAndroid Build Coastguard Worker# found in the LICENSE file.
5*c8dee2aaSAndroid Build Coastguard Workerimport re
6*c8dee2aaSAndroid Build Coastguard Workerimport sys
7*c8dee2aaSAndroid Build Coastguard Workerdef gerrit_percent_encode(value):
8*c8dee2aaSAndroid Build Coastguard Worker    '''
9*c8dee2aaSAndroid Build Coastguard Worker    https://gerrit-review.googlesource.com/Documentation/user-upload.html
10*c8dee2aaSAndroid Build Coastguard Worker    "To avoid confusion in parsing the git ref, at least the following characters
11*c8dee2aaSAndroid Build Coastguard Worker    must be percent-encoded: " %^@.~-+_:/!". Note that some of the reserved
12*c8dee2aaSAndroid Build Coastguard Worker    characters (like tilde) are not escaped in the standard URL encoding rules..."
13*c8dee2aaSAndroid Build Coastguard Worker    '''
14*c8dee2aaSAndroid Build Coastguard Worker    good = re.compile('^[A-Za-z0-9]$')
15*c8dee2aaSAndroid Build Coastguard Worker    return ''.join(c if good.match(c) else '%%%02X' % ord(c) for c in value)
16*c8dee2aaSAndroid Build Coastguard Workerif __name__ == '__main__':
17*c8dee2aaSAndroid Build Coastguard Worker    sys.stdout.write(gerrit_percent_encode(' '.join(sys.argv[1:])) + '\n')
18