1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
import { Commit as NodeGitCommit, Oid as NodeGitOid } from "nodegit";
import { Author } from "api";
import { Diff } from "./diff";
import { Repository } from "./repository";
import { Tree } from "./tree";
export type CommitSummary = {
id: string,
message: string,
date: number
}
type DiffStats = {
insertions: number,
deletions: number,
files_changed: number
}
/**
* A representation of a commit
*/
export class Commit {
private _ng_commit: NodeGitCommit;
private _owner: Repository;
public id: string;
public author: Author;
public date: number;
public message: string;
/**
* @param owner - The repository which the commit is in
* @param commit - An instance of a Nodegit commit
*/
constructor(owner: Repository, commit: NodeGitCommit) {
this._ng_commit = commit;
this._owner = owner;
this.id = commit.sha();
this.author = {
name: commit.author().name(),
email: commit.author().email()
};
this.date = commit.time();
this.message = commit.message();
}
/**
* Returns the commit's diff
*
* @returns An instance of a diff
*/
public async diff(): Promise<Diff> {
return new Diff((await this._ng_commit.getDiff())[0]);
}
/**
* Returns the commit's stats
*
* @returns A diff stats instance
*/
public async stats(): Promise<DiffStats> {
const stats = await (await this._ng_commit.getDiff())[0].getStats();
return {
insertions: <number>stats.insertions(),
deletions: <number>stats.deletions(),
files_changed: <number>stats.filesChanged()
};
}
/**
* Returns the commit's tree
*
* @returns An instance of a tree
*/
public async tree(): Promise<Tree> {
return new Tree(this._owner, await this._ng_commit.getTree());
}
/**
* Lookup a commit
*
* @param repository - The repository which the commit is in
* @param id - The SHA of a commit
* @returns An instance of a commit
*/
public static async lookup(repository: Repository, id: string | NodeGitOid): Promise<Commit> {
const commit = await NodeGitCommit.lookup(repository.ng_repository, id instanceof NodeGitOid ? id : NodeGitOid.fromString(id));
return new Commit(repository, commit);
}
/**
* Returns if an commit exists or not
*
* @param repository - The repository which the commit is in
* @param id - The sha of a commit
* @returns Whether or not the commit exists
*/
public static lookupExists(repository: Repository, id: string): Promise<boolean> {
return NodeGitCommit.lookup(repository.ng_repository, NodeGitOid.fromString(id))
.then(() => true)
.catch(() => false);
}
/**
* Returns the master commit of a repository
*
* @param owner - A repository
* @returns An instance of a commit
*/
public static async masterCommit(owner: Repository): Promise<Commit> {
return new Commit(owner, await owner.ng_repository.getMasterCommit());
}
}
|