Class: Git::EscapedPath Private

Inherits:
Object
  • Object
show all
Defined in:
lib/git/escaped_path.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents an escaped Git path string

Git commands that output paths (e.g. ls-files, diff), will escape unusual characters in the path with backslashes in the same way C escapes control characters (e.g. \t for TAB, \n for LF, \ for backslash) or bytes with values larger than 0x80 (e.g. octal \302\265 for "micro" in UTF-8).

Examples:

Decode octal UTF-8 bytes

Git::EscapedPath.new('\302\265').unescape # => "µ"

Constant Summary collapse

UNESCAPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maps single-character escapes to their decoded byte values

Returns:

  • (Hash<String, Integer>)

    escape characters mapped to byte values

{
  'a' => 0x07,
  'b' => 0x08,
  't' => 0x09,
  'n' => 0x0a,
  'v' => 0x0b,
  'f' => 0x0c,
  'r' => 0x0d,
  'e' => 0x1b,
  '\\' => 0x5c,
  '"' => 0x22,
  "'" => 0x27
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes an escaped path wrapper

Parameters:

  • path (String)

    the path string with Git-style escape sequences



44
45
46
# File 'lib/git/escaped_path.rb', line 44

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the escaped path as provided by git output

Returns:

  • (String)

    the escaped path string



37
38
39
# File 'lib/git/escaped_path.rb', line 37

def path
  @path
end

Instance Method Details

#unescapeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts an escaped path to an unescaped UTF-8 path

Examples:

Decode escaped path output

Git::EscapedPath.new("dir/\\342\\230\\240\\n").unescape
# => "dir/☠\n"

Returns:

  • (String)

    the decoded path string



55
56
57
58
59
# File 'lib/git/escaped_path.rb', line 55

def unescape
  bytes = escaped_path_to_bytes(path)
  str = bytes.pack('C*')
  str.force_encoding(Encoding::UTF_8)
end