Skip to content

Commit

Permalink
Add support for getting commits of line range
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmonMode committed Oct 2, 2020
1 parent e224f24 commit c857409
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -135,6 +135,10 @@ Show only commits in the specified branch or revision range.

By default uses the current branch and defaults to `HEAD` (i.e. the whole history leading to the current commit).

### fileLineRange

Optional field for getting only the commits that affected a specific line range of a given file.

### file

Optional file filter for the `git log` command
Expand Down
22 changes: 21 additions & 1 deletion src/index.ts
Expand Up @@ -29,6 +29,20 @@ export type CommitField = keyof typeof fieldMap;
const notOptFields = ["status", "files"] as const;
type NotOptField = typeof notOptFields[number];

export interface FileLineRange {
/** Will be pass as -L <startLine>,<endLine>:<file> */

/** The file to get the commits for */
file: string;
/** The number of the first line in the desired range */
startLine: number;
/**
* Either the absolute line number for the end of the desired range,
* or the offset from the startLine
*/
endLine: number | string;
}

const defaultFields = [
"abbrevHash",
"hash",
Expand Down Expand Up @@ -82,6 +96,8 @@ export interface GitlogOptions<Fields extends string = DefaultField> {
* the whole history leading to the current commit).
*/
branch?: string;
/** Range of lines for a given file to find the commits for */
fileLineRange?: FileLineRange;
/** File filter for the git log command */
file?: string;
/** Limit the commits output to ones with author header lines that match the specified pattern. */
Expand Down Expand Up @@ -263,10 +279,14 @@ function createCommand<T extends CommitField | DefaultField = DefaultField>(
}

// File and file status
if (options.nameStatus) {
if (options.nameStatus && !options.fileLineRange) {
command += " --name-status";
}

if (options.fileLineRange) {
command += ` -L ${options.fileLineRange.startLine},${options.fileLineRange.endLine}:${options.fileLineRange.file}`;
}

if (options.file) {
command += ` -- ${options.file}`;
}
Expand Down

0 comments on commit c857409

Please sign in to comment.