Class: Daytona::Git

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/daytona/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

included

Constructor Details

#initialize(sandbox_id:, toolbox_api:, otel_state: nil) ⇒ Git

Initializes a new Git handler instance.

Parameters:

  • sandbox_id (String)

    The Sandbox ID.

  • toolbox_api (DaytonaToolboxApiClient::GitApi)

    API client for Sandbox operations.

  • otel_state (Daytona::OtelState, nil) (defaults to: nil)


18
19
20
21
22
# File 'lib/daytona/git.rb', line 18

def initialize(sandbox_id:, toolbox_api:, otel_state: nil)
  @sandbox_id = sandbox_id
  @toolbox_api = toolbox_api
  @otel_state = otel_state
end

Instance Attribute Details

#sandbox_idString (readonly)

Returns The Sandbox ID.

Returns:

  • (String)

    The Sandbox ID



8
9
10
# File 'lib/daytona/git.rb', line 8

def sandbox_id
  @sandbox_id
end

#toolbox_apiDaytonaToolboxApiClient::GitApi (readonly)

Returns API client for Sandbox operations.

Returns:

  • (DaytonaToolboxApiClient::GitApi)

    API client for Sandbox operations



11
12
13
# File 'lib/daytona/git.rb', line 11

def toolbox_api
  @toolbox_api
end

Instance Method Details

#add(path, files) ⇒ void

This method returns an undefined value.

Stages the specified files for the next commit, similar to running ‘git add’ on the command line.

Examples:

# Stage a single file
sandbox.git.add("workspace/repo", ["file.txt"])

# Stage multiple files
sandbox.git.add("workspace/repo", [
  "src/main.rb",
  "spec/main_spec.rb",
  "README.md"
])

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • files (Array<String>)

    List of file paths or directories to stage, relative to the repository root.

Raises:



43
44
45
46
47
# File 'lib/daytona/git.rb', line 43

def add(path, files)
  toolbox_api.add_files(DaytonaToolboxApiClient::GitAddRequest.new(path:, files:))
rescue StandardError => e
  raise Sdk::Error, "Failed to add files: #{e.message}"
end

#branches(path) ⇒ DaytonaApiClient::ListBranchResponse

Lists branches in the repository.

Examples:

response = sandbox.git.branches("workspace/repo")
puts "Branches: #{response.branches}"

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

Returns:

  • (DaytonaApiClient::ListBranchResponse)

    List of branches in the repository.

Raises:



59
60
61
62
63
# File 'lib/daytona/git.rb', line 59

def branches(path)
  toolbox_api.list_branches(path)
rescue StandardError => e
  raise Sdk::Error, "Failed to list branches: #{e.message}"
end

#checkout_branch(path, branch) ⇒ void

This method returns an undefined value.

Checkout branch in the repository.

Examples:

# Checkout a branch
sandbox.git.checkout_branch("workspace/repo", "feature-branch")

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • branch (String)

    Name of the branch to checkout

Raises:



237
238
239
240
241
242
243
# File 'lib/daytona/git.rb', line 237

def checkout_branch(path, branch)
  toolbox_api.checkout_branch(
    DaytonaToolboxApiClient::GitCheckoutRequest.new(path:, branch:)
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to checkout branch: #{e.message}"
end

#clone(url:, path:, branch: nil, commit_id: nil, username: nil, password: nil) ⇒ void

This method returns an undefined value.

Clones a Git repository into the specified path. It supports cloning specific branches or commits, and can authenticate with the remote repository if credentials are provided.

Examples:

# Clone the default branch
sandbox.git.clone(
  url: "https://github.com/user/repo.git",
  path: "workspace/repo"
)

# Clone a specific branch with authentication
sandbox.git.clone(
  url: "https://github.com/user/private-repo.git",
  path: "workspace/private",
  branch: "develop",
  username: "user",
  password: "token"
)

# Clone a specific commit
sandbox.git.clone(
  url: "https://github.com/user/repo.git",
  path: "workspace/repo-old",
  commit_id: "abc123"
)

Parameters:

  • url (String)

    Repository URL to clone from.

  • path (String)

    Path where the repository should be cloned. Relative paths are resolved based on the sandbox working directory.

  • branch (String, nil) (defaults to: nil)

    Specific branch to clone. If not specified, clones the default branch.

  • commit_id (String, nil) (defaults to: nil)

    Specific commit to clone. If specified, the repository will be left in a detached HEAD state at this commit.

  • username (String, nil) (defaults to: nil)

    Git username for authentication.

  • password (String, nil) (defaults to: nil)

    Git password or token for authentication.

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/daytona/git.rb', line 103

def clone(url:, path:, branch: nil, commit_id: nil, username: nil, password: nil) # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
  toolbox_api.clone_repository(
    DaytonaToolboxApiClient::GitCloneRequest.new(
      url: url,
      branch: branch,
      path: path,
      username: username,
      password: password,
      commit_id: commit_id
    )
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to clone repository: #{e.message}"
end

#commit(path:, message:, author:, email:, allow_empty: false) ⇒ GitCommitResponse

Creates a new commit with the staged changes. Make sure to stage changes using the add() method before committing.

Examples:

# Stage and commit changes
sandbox.git.add("workspace/repo", ["README.md"])
commit_response = sandbox.git.commit(
  path: "workspace/repo",
  message: "Update documentation",
  author: "John Doe",
  email: "john@example.com",
  allow_empty: true
)
puts "Commit SHA: #{commit_response.sha}"

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • message (String)

    Commit message describing the changes.

  • author (String)

    Name of the commit author.

  • email (String)

    Email address of the commit author.

  • allow_empty (Boolean) (defaults to: false)

    Allow creating an empty commit when no changes are staged. Defaults to false.

Returns:

Raises:



141
142
143
144
145
146
147
148
# File 'lib/daytona/git.rb', line 141

def commit(path:, message:, author:, email:, allow_empty: false)
  response = toolbox_api.commit_changes(
    DaytonaToolboxApiClient::GitCommitRequest.new(path:, message:, author:, email:, allow_empty:)
  )
  GitCommitResponse.new(sha: response.hash)
rescue StandardError => e
  raise Sdk::Error, "Failed to commit changes: #{e.message}"
end

#create_branch(path, name) ⇒ void

This method returns an undefined value.

Create branch in the repository.

Examples:

# Create a new branch
sandbox.git.create_branch("workspace/repo", "new-feature")

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • name (String)

    Name of the new branch to create

Raises:



257
258
259
260
261
262
263
# File 'lib/daytona/git.rb', line 257

def create_branch(path, name)
  toolbox_api.create_branch(
    DaytonaToolboxApiClient::GitBranchRequest.new(path:, name:)
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to create branch: #{e.message}"
end

#delete_branch(path, name) ⇒ void

This method returns an undefined value.

Delete branch in the repository.

Examples:

# Delete a branch
sandbox.git.delete_branch("workspace/repo", "old-feature")

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • name (String)

    Name of the branch to delete

Raises:



276
277
278
279
280
281
282
# File 'lib/daytona/git.rb', line 276

def delete_branch(path, name)
  toolbox_api.delete_branch(
    DaytonaToolboxApiClient::GitDeleteBranchRequest.new(path:, name:)
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to delete branch: #{e.message}"
end

#pull(path:, username: nil, password: nil) ⇒ void

This method returns an undefined value.

Pulls changes from the remote repository. If the remote repository requires authentication, provide username and password/token.

Examples:

# Pull without authentication
sandbox.git.pull("workspace/repo")

# Pull with authentication
sandbox.git.pull(
  path: "workspace/repo",
  username: "user",
  password: "github_token"
)

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • username (String, nil) (defaults to: nil)

    Git username for authentication.

  • password (String, nil) (defaults to: nil)

    Git password or token for authentication.

Raises:



200
201
202
203
204
205
206
# File 'lib/daytona/git.rb', line 200

def pull(path:, username: nil, password: nil)
  toolbox_api.pull_changes(
    DaytonaToolboxApiClient::GitRepoRequest.new(path:, username:, password:)
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to pull changes: #{e.message}"
end

#push(path:, username: nil, password: nil) ⇒ void

This method returns an undefined value.

Pushes all local commits on the current branch to the remote repository. If the remote repository requires authentication, provide username and password/token.

Examples:

# Push without authentication (for public repos or SSH)
sandbox.git.push("workspace/repo")

# Push with authentication
sandbox.git.push(
  path: "workspace/repo",
  username: "user",
  password: "github_token"
)

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

  • username (String, nil) (defaults to: nil)

    Git username for authentication.

  • password (String, nil) (defaults to: nil)

    Git password or token for authentication.

Raises:



171
172
173
174
175
176
177
# File 'lib/daytona/git.rb', line 171

def push(path:, username: nil, password: nil)
  toolbox_api.push_changes(
    DaytonaToolboxApiClient::GitRepoRequest.new(path:, username:, password:)
  )
rescue StandardError => e
  raise Sdk::Error, "Failed to push changes: #{e.message}"
end

#status(path) ⇒ DaytonaToolboxApiClient::GitStatus

Gets the current Git repository status.

Examples:

status = sandbox.git.status("workspace/repo")
puts "On branch: #{status.current_branch}"
puts "Commits ahead: #{status.ahead}"
puts "Commits behind: #{status.behind}"

Parameters:

  • path (String)

    Path to the Git repository root. Relative paths are resolved based on the sandbox working directory.

Returns:

  • (DaytonaToolboxApiClient::GitStatus)

    Repository status information including:

Raises:



220
221
222
223
224
# File 'lib/daytona/git.rb', line 220

def status(path)
  toolbox_api.get_status(path)
rescue StandardError => e
  raise Sdk::Error, "Failed to get status: #{e.message}"
end