Class: Git::FileRef Private

Inherits:
Data
  • Object
show all
Defined in:
lib/git/file_ref.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.

Immutable value object representing a reference to a file at a specific point in time

FileRef encapsulates the mode, SHA, and path of a file as it exists on one side of a diff. This is used to represent either the source (before) or destination (after) state of a file in a diff operation.

When a file doesn't exist on a side of the diff (e.g., src for new files, dst for deleted files), the entire FileRef should be nil rather than having a FileRef with nil attributes.

Work in progress; this class is internal for now and may be made public in a future release.

Examples:

A modified file's source reference

src = Git::FileRef.new(mode: '100644', sha: 'abc1234', path: 'lib/foo.rb')

A new file (src would be nil, not a FileRef)

# src = nil
dst = Git::FileRef.new(mode: '100644', sha: 'def5678', path: 'lib/new_file.rb')

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modeString (readonly)

Returns the file mode (e.g., '100644' for regular file, '100755' for executable, '120000' for symlink).

Returns:

  • (String)

    the file mode (e.g., '100644' for regular file, '100755' for executable, '120000' for symlink)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/git/file_ref.rb', line 36

FileRef = Data.define(:mode, :sha, :path) do
  # Check if this is a regular file (not executable, symlink, etc.)
  #
  # @return [Boolean] true if mode is 100644
  #
  def regular_file?
    mode == '100644'
  end

  # Check if this is an executable file
  #
  # @return [Boolean] true if mode is 100755
  #
  def executable?
    mode == '100755'
  end

  # Check if this is a symbolic link
  #
  # @return [Boolean] true if mode is 120000
  #
  def symlink?
    mode == '120000'
  end

  # Return the mode as an integer (parsed as octal)
  #
  # Useful for bit operations on file permissions.
  #
  # @example Check file permissions
  #   ref.mode_bits & 0o777  # => 0o644 (420 decimal)
  #
  # @example Check if group writable
  #   (ref.mode_bits & 0o020) != 0
  #
  # @return [Integer] the mode as an integer
  #
  def mode_bits
    mode.to_i(8)
  end
end

#pathString (readonly)

Returns the file path relative to repository root.

Returns:

  • (String)

    the file path relative to repository root



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/git/file_ref.rb', line 36

FileRef = Data.define(:mode, :sha, :path) do
  # Check if this is a regular file (not executable, symlink, etc.)
  #
  # @return [Boolean] true if mode is 100644
  #
  def regular_file?
    mode == '100644'
  end

  # Check if this is an executable file
  #
  # @return [Boolean] true if mode is 100755
  #
  def executable?
    mode == '100755'
  end

  # Check if this is a symbolic link
  #
  # @return [Boolean] true if mode is 120000
  #
  def symlink?
    mode == '120000'
  end

  # Return the mode as an integer (parsed as octal)
  #
  # Useful for bit operations on file permissions.
  #
  # @example Check file permissions
  #   ref.mode_bits & 0o777  # => 0o644 (420 decimal)
  #
  # @example Check if group writable
  #   (ref.mode_bits & 0o020) != 0
  #
  # @return [Integer] the mode as an integer
  #
  def mode_bits
    mode.to_i(8)
  end
end

#shaString (readonly)

Returns the blob SHA (object identifier).

Returns:

  • (String)

    the blob SHA (object identifier)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/git/file_ref.rb', line 36

FileRef = Data.define(:mode, :sha, :path) do
  # Check if this is a regular file (not executable, symlink, etc.)
  #
  # @return [Boolean] true if mode is 100644
  #
  def regular_file?
    mode == '100644'
  end

  # Check if this is an executable file
  #
  # @return [Boolean] true if mode is 100755
  #
  def executable?
    mode == '100755'
  end

  # Check if this is a symbolic link
  #
  # @return [Boolean] true if mode is 120000
  #
  def symlink?
    mode == '120000'
  end

  # Return the mode as an integer (parsed as octal)
  #
  # Useful for bit operations on file permissions.
  #
  # @example Check file permissions
  #   ref.mode_bits & 0o777  # => 0o644 (420 decimal)
  #
  # @example Check if group writable
  #   (ref.mode_bits & 0o020) != 0
  #
  # @return [Integer] the mode as an integer
  #
  def mode_bits
    mode.to_i(8)
  end
end

Instance Method Details

#executable?Boolean

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.

Check if this is an executable file

Returns:

  • (Boolean)

    true if mode is 100755



49
50
51
# File 'lib/git/file_ref.rb', line 49

def executable?
  mode == '100755'
end

#mode_bitsInteger

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.

Return the mode as an integer (parsed as octal)

Useful for bit operations on file permissions.

Examples:

Check file permissions

ref.mode_bits & 0o777  # => 0o644 (420 decimal)

Check if group writable

(ref.mode_bits & 0o020) != 0

Returns:

  • (Integer)

    the mode as an integer



73
74
75
# File 'lib/git/file_ref.rb', line 73

def mode_bits
  mode.to_i(8)
end

#regular_file?Boolean

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.

Check if this is a regular file (not executable, symlink, etc.)

Returns:

  • (Boolean)

    true if mode is 100644



41
42
43
# File 'lib/git/file_ref.rb', line 41

def regular_file?
  mode == '100644'
end

#symlink?Boolean

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.

Check if this is a symbolic link

Returns:

  • (Boolean)

    true if mode is 120000



57
58
59
# File 'lib/git/file_ref.rb', line 57

def symlink?
  mode == '120000'
end