Skip to content

Commit 458013d

Browse files
authored
feat: implements blame features (#107)
1 parent 22576a9 commit 458013d

33 files changed

+877
-1
lines changed

index.d.ts

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,72 @@
33

44
/* auto-generated by NAPI-RS */
55

6+
/**
7+
* Represents a hunk of a blame operation, which is a range of lines
8+
* and information about who last modified them.
9+
*/
10+
export interface BlameHunk {
11+
/** The oid of the commit where this line was last changed. */
12+
finalCommitId: string
13+
/** The 1-based line number in the final file where this hunk starts. */
14+
finalStartLineNumber: number
15+
/** The number of lines in this hunk. */
16+
linesInHunk: number
17+
/** The signature of the commit where this line was last changed. */
18+
finalSignature?: Signature
19+
/** The path to the file where this line was originally written. */
20+
path?: string
21+
/** The 1-based line number in the original file where this hunk starts. */
22+
origStartLineNumber: number
23+
/** The oid of the commit where this line was originally written. */
24+
origCommitId: string
25+
/** The signature of the commit where this line was originally written. */
26+
origSignature?: Signature
27+
/**
28+
* True if the hunk has been determined to be a boundary commit (the commit
29+
* when the file was first introduced to the repository).
30+
*/
31+
isBoundary: boolean
32+
}
33+
/** Options for controlling blame behavior */
34+
export interface BlameOptions {
35+
/** The minimum line number to blame (1-based index) */
36+
minLine?: number
37+
/** The maximum line number to blame (1-based index) */
38+
maxLine?: number
39+
/**
40+
* The oid of the newest commit to consider. The blame algorithm will stop
41+
* when this commit is reached.
42+
*/
43+
newestCommit?: string
44+
/**
45+
* The oid of the oldest commit to consider. The blame algorithm will
46+
* stop when this commit is reached.
47+
*/
48+
oldestCommit?: string
49+
/**
50+
* The path to the file being worked on. Path has to be relative to the
51+
* repo root.
52+
*/
53+
path?: string
54+
/**
55+
* Track lines that have moved within a file. This is the git-blame -M
56+
* option.
57+
*/
58+
trackLinesMovement?: boolean
59+
/** Restrict search to commits reachable following only first parents. */
60+
firstParent?: boolean
61+
/** Ignore whitespace differences. */
62+
ignoreWhitespace?: boolean
63+
/** Track lines that have been copied from another file that exists in any commit. */
64+
trackCopiesAnyCommitCopies?: boolean
65+
/** Track lines that have been copied from another file that exists in the same commit. */
66+
trackCopiesSameCommitCopies?: boolean
67+
/** Track lines that have moved across files in the same commit. */
68+
trackCopiesSameCommitMoves?: boolean
69+
/** Use mailmap file to map author and committer names and email addresses to canonical real names and email addresses. */
70+
useMailmap?: boolean
71+
}
672
/**
773
* Ensure the branch name is well-formed.
874
*
@@ -1420,6 +1486,146 @@ export interface CreateLightweightTagOptions {
14201486
* - `PostOrder` : Runs the traversal in post-order.
14211487
*/
14221488
export type TreeWalkMode = 'PreOrder' | 'PostOrder';
1489+
/** A wrapper around git2::Blame providing Node.js bindings */
1490+
export declare class Blame {
1491+
/**
1492+
* Gets the number of hunks in the blame result
1493+
*
1494+
* @category Blame/Methods
1495+
* @signature
1496+
* ```ts
1497+
* class Blame {
1498+
* getHunkCount(): number;
1499+
* }
1500+
* ```
1501+
*
1502+
* @returns The number of hunks in the blame result
1503+
*/
1504+
getHunkCount(): number
1505+
/**
1506+
* Checks if the blame result is empty
1507+
*
1508+
* @category Blame/Methods
1509+
* @signature
1510+
* ```ts
1511+
* class Blame {
1512+
* isEmpty(): boolean;
1513+
* }
1514+
* ```
1515+
*
1516+
* @returns True if the blame result contains no hunks
1517+
*/
1518+
isEmpty(): boolean
1519+
/**
1520+
* Gets blame information for the specified index
1521+
*
1522+
* @category Blame/Methods
1523+
* @signature
1524+
* ```ts
1525+
* class Blame {
1526+
* getHunkByIndex(index: number): BlameHunk;
1527+
* }
1528+
* ```
1529+
*
1530+
* @param {number} index - Index of the hunk to get (0-based)
1531+
* @returns Blame information for the specified index
1532+
* @throws If no hunk is found at the index
1533+
*/
1534+
getHunkByIndex(index: number): BlameHunk
1535+
/**
1536+
* Gets blame information for the specified line
1537+
*
1538+
* @category Blame/Methods
1539+
* @signature
1540+
* ```ts
1541+
* class Blame {
1542+
* getHunkByLine(line: number): BlameHunk;
1543+
* }
1544+
* ```
1545+
*
1546+
* @param {number} line - Line number to get blame information for (1-based)
1547+
* @returns Blame information for the specified line
1548+
* @throws If no hunk is found for the line
1549+
*/
1550+
getHunkByLine(line: number): BlameHunk
1551+
/**
1552+
* Gets all blame hunks as an iterator
1553+
*
1554+
* @category Blame/Methods
1555+
* @signature
1556+
* ```ts
1557+
* class Blame {
1558+
* iter(): Generator<BlameHunk>;
1559+
* }
1560+
* ```
1561+
*
1562+
* @returns Iterator of all blame hunks
1563+
* @example
1564+
* ```ts
1565+
* // Using for...of loop
1566+
* for (const hunk of blame.iter()) {
1567+
* console.log(hunk.finalCommitId);
1568+
* }
1569+
*
1570+
* // Using spread operator to collect all hunks
1571+
* const hunks = [...blame.iter()];
1572+
* ```
1573+
*/
1574+
iter(): BlameHunks
1575+
/**
1576+
* Collects blame hunks by scanning file lines as an iterator
1577+
*
1578+
* @category Blame/Methods
1579+
* @signature
1580+
* ```ts
1581+
* class Blame {
1582+
* iterByLine(): Generator<BlameHunk>;
1583+
* }
1584+
* ```
1585+
*
1586+
* @returns Iterator of blame hunks collected by line scanning
1587+
* @example
1588+
* ```ts
1589+
* // Using for...of loop
1590+
* for (const hunk of blame.iterByLine()) {
1591+
* console.log(hunk.finalCommitId);
1592+
* }
1593+
*
1594+
* // Using spread operator to collect all hunks
1595+
* const hunks = [...blame.iterByLine()];
1596+
* ```
1597+
*/
1598+
iterByLine(): BlameHunksByLine
1599+
/**
1600+
* Generates blame information from an in-memory buffer
1601+
*
1602+
* @category Blame/Methods
1603+
* @signature
1604+
* ```ts
1605+
* class Blame {
1606+
* buffer(buffer: Buffer): Blame;
1607+
* }
1608+
* ```
1609+
*
1610+
* @example
1611+
* ```ts
1612+
* const buffer = Buffer.from('modified content');
1613+
* const bufferBlame = blame.buffer(buffer);
1614+
* ```
1615+
*
1616+
* @param {Buffer} buffer - Buffer containing file content to blame
1617+
* @returns A new Blame object for the buffer content
1618+
*/
1619+
buffer(buffer: Buffer): Blame
1620+
}
1621+
/** An iterator over blame hunks. */
1622+
export declare class BlameHunks {
1623+
[Symbol.iterator](): Iterator<BlameHunk, void, void>
1624+
}
1625+
/** Iterator over blame hunks collected line by line. */
1626+
export declare class BlameHunksByLine {
1627+
[Symbol.iterator](): Iterator<BlameHunk, void, void>
1628+
}
14231629
/**
14241630
* A class to represent a git [blob][1].
14251631
* [1]: https://git-scm.com/book/en/Git-Internals-Git-Objects
@@ -3413,6 +3619,35 @@ export declare class Remote {
34133619
* This class corresponds to a git repository in libgit2.
34143620
*/
34153621
export declare class Repository {
3622+
/**
3623+
* Creates a blame object for the file at the given path
3624+
*
3625+
* @category Repository/Methods
3626+
* @signature
3627+
* ```ts
3628+
* class Repository {
3629+
* blameFile(path: string, options?: BlameOptions): Blame;
3630+
* }
3631+
* ```
3632+
*
3633+
* @example
3634+
* ```ts
3635+
* // Blame the entire file
3636+
* const blame = repo.blameFile('path/to/file.js');
3637+
*
3638+
* // Blame a single line
3639+
* const lineBlame = repo.blameFile('path/to/file.js', { minLine: 10, maxLine: 10 });
3640+
*
3641+
* // Blame a range of lines
3642+
* const rangeBlame = repo.blameFile('path/to/file.js', { minLine: 5, maxLine: 15 });
3643+
* ```
3644+
*
3645+
* @param {string} path - Path to the file to blame
3646+
* @param {BlameOptions} [options] - Options to control blame behavior
3647+
* @returns Blame object for the specified file
3648+
* @throws If the file doesn't exist or can't be opened
3649+
*/
3650+
blameFile(path: string, options?: BlameOptions | undefined | null): Blame
34163651
/**
34173652
* Create a new branch pointing at a target commit
34183653
*

index.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)