|
42 | 42 | from .ffi import ffi, C
|
43 | 43 | from .index import Index
|
44 | 44 | from .remote import Remote
|
45 |
| -from .utils import to_bytes |
| 45 | +from .blame import Blame |
| 46 | +from .utils import to_bytes, to_str |
46 | 47 |
|
47 | 48 |
|
48 | 49 | class Repository(_Repository):
|
@@ -370,6 +371,61 @@ def state_cleanup(self):
|
370 | 371 | """
|
371 | 372 | C.git_repository_state_cleanup(self._repo)
|
372 | 373 |
|
| 374 | + # |
| 375 | + # blame |
| 376 | + # |
| 377 | + def blame(self, path, flags=None, min_match_characters=None, newest_commit=None, oldest_commit=None, min_line=None, max_line=None): |
| 378 | + """blame(path, [flags, min_match_characters, newest_commit, oldest_commit,\n" |
| 379 | + min_line, max_line]) -> Blame |
| 380 | +
|
| 381 | + Get the blame for a single file. |
| 382 | +
|
| 383 | + Arguments: |
| 384 | +
|
| 385 | + path |
| 386 | + Path to the file to blame. |
| 387 | + flags |
| 388 | + A GIT_BLAME_* constant. |
| 389 | + min_match_characters |
| 390 | + The number of alphanum chars that must be detected as moving/copying |
| 391 | + within a file for it to associate those lines with the parent commit. |
| 392 | + newest_commit |
| 393 | + The id of the newest commit to consider. |
| 394 | + oldest_commit |
| 395 | + The id of the oldest commit to consider. |
| 396 | + min_line |
| 397 | + The first line in the file to blame. |
| 398 | + max_line |
| 399 | + The last line in the file to blame. |
| 400 | +
|
| 401 | + Examples:: |
| 402 | +
|
| 403 | + repo.blame('foo.c', flags=GIT_BLAME_TRACK_COPIES_SAME_FILE)"); |
| 404 | + """ |
| 405 | + |
| 406 | + options = ffi.new('git_blame_options *') |
| 407 | + C.git_blame_init_options(options, C.GIT_BLAME_OPTIONS_VERSION) |
| 408 | + if min_match_characters: |
| 409 | + options.min_match_characters = min_match_characters |
| 410 | + if newest_commit: |
| 411 | + if not isinstance(newest_commit, Oid): |
| 412 | + newest_commit = Oid(hex=newest_commit) |
| 413 | + ffi.buffer(ffi.addressof(options, 'newest_commit'))[:] = newest_commit.raw |
| 414 | + if oldest_commit: |
| 415 | + if not isinstance(oldest_commit, Oid): |
| 416 | + oldest_commit = Oid(hex=oldest_commit) |
| 417 | + ffi.buffer(ffi.addressof(options, 'oldest_commit'))[:] = oldest_commit.raw |
| 418 | + if min_line: |
| 419 | + options.min_line = min_line |
| 420 | + if max_line: |
| 421 | + options.max_line = max_line |
| 422 | + |
| 423 | + cblame = ffi.new('git_blame **') |
| 424 | + err = C.git_blame_file(cblame, self._repo, to_str(path), options) |
| 425 | + check_error(err) |
| 426 | + |
| 427 | + return Blame._from_c(self, cblame[0]) |
| 428 | + |
373 | 429 | #
|
374 | 430 | # Index
|
375 | 431 | #
|
|
0 commit comments