|
11 | 11 | [](https://github.com/ruby-git/ruby-git/actions?query=workflow%3ACI)
|
12 | 12 | [](https://codeclimate.com/github/ruby-git/ruby-git)
|
13 | 13 |
|
| 14 | +* [Summary](#summary) |
| 15 | +* [v2.0.0 pre-release](#v200-pre-release) |
| 16 | +* [Install](#install) |
| 17 | +* [Major Objects](#major-objects) |
| 18 | +* [Errors Raised By This Gem](#errors-raised-by-this-gem) |
| 19 | +* [Specifying And Handling Timeouts](#specifying-and-handling-timeouts) |
| 20 | +* [Examples](#examples) |
| 21 | +* [Ruby version support policy](#ruby-version-support-policy) |
| 22 | +* [License](#license) |
| 23 | + |
| 24 | +## Summary |
| 25 | + |
14 | 26 | The [git gem](https://rubygems.org/gems/git) provides an API that can be used to
|
15 | 27 | create, read, and manipulate Git repositories by wrapping system calls to the `git`
|
16 | 28 | command line. The API can be used for working with Git in complex interactions
|
@@ -140,6 +152,60 @@ rescue Git::TimeoutError => e # Catch the more specific error first!
|
140 | 152 | puts "Git clone took too long and timed out #{e}"
|
141 | 153 | rescue Git::Error => e
|
142 | 154 | puts "Received the following error: #{e}"
|
| 155 | +``` |
| 156 | + |
| 157 | +## Specifying And Handling Timeouts |
| 158 | + |
| 159 | +The timeout feature was added in git gem version `2.0.0`. |
| 160 | + |
| 161 | +A timeout for git operations can be set either globally or for specific method calls |
| 162 | +that accept a `:timeout` parameter. |
| 163 | + |
| 164 | +The timeout value must be a real, non-negative `Numeric` value that specifies a |
| 165 | +number of seconds a `git` command will be given to complete before being sent a KILL |
| 166 | +signal. This library may hang if the `git` command does not terminate after receiving |
| 167 | +the KILL signal. |
| 168 | + |
| 169 | +When a command times out, a `Git::TimeoutError` is raised. |
| 170 | + |
| 171 | +If the timeout value is `0` or `nil`, no timeout will be enforced. |
| 172 | + |
| 173 | +If a method accepts a `:timeout` parameter and a receives a non-nil value, it will |
| 174 | +override the global timeout value. In this context, a value of `nil` (which is |
| 175 | +usually the default) will use the global timeout value and a value of `0` will turn |
| 176 | +off timeout enforcement for that method call no matter what the global value is. |
| 177 | + |
| 178 | +To set a global timeout, use the `Git.config` object: |
| 179 | + |
| 180 | +```ruby |
| 181 | +Git.config.timeout = nil # a value of nil or 0 means no timeout is enforced |
| 182 | +Git.config.timeout = 1.5 # can be any real, non-negative Numeric interpreted as number of seconds |
| 183 | +``` |
| 184 | + |
| 185 | +The global timeout can be overridden for a specific method if the method accepts a |
| 186 | +`:timeout` parameter: |
| 187 | + |
| 188 | +```ruby |
| 189 | +repo_url = 'https://github.com/ruby-git/ruby-git.git' |
| 190 | +Git.clone(repo_url) # Use the global timeout value |
| 191 | +Git.clone(repo_url, timeout: nil) # Also uses the global timeout value |
| 192 | +Git.clone(repo_url, timeout: 0) # Do not enforce a timeout |
| 193 | +Git.clone(repo_url, timeout: 10.5) # Timeout after 10.5 seconds raising Git::SignaledError |
| 194 | +``` |
| 195 | + |
| 196 | +If the command takes too long, a `Git::SignaledError` will be raised: |
| 197 | + |
| 198 | +```ruby |
| 199 | +begin |
| 200 | + Git.clone(repo_url, timeout: 10) |
| 201 | +rescue Git::TimeoutError => e |
| 202 | + result = e.result |
| 203 | + result.class #=> Git::CommandLineResult |
| 204 | + result.status #=> #<Process::Status: pid 62173 SIGKILL (signal 9)> |
| 205 | + result.status.timeout? #=> true |
| 206 | + result.git_cmd # The git command ran as an array of strings |
| 207 | + result.stdout # The command's output to stdout until it was terminated |
| 208 | + result.stderr # The command's output to stderr until it was terminated |
143 | 209 | end
|
144 | 210 | ```
|
145 | 211 |
|
|
0 commit comments