Skip to content

Commit fe9f732

Browse files
committed
Adds more advanced data for contributor stats
1 parent 7d68c2d commit fe9f732

File tree

11 files changed

+270
-55
lines changed

11 files changed

+270
-55
lines changed

src/env/node/git/sub-providers/branches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
270270
let latestCommitTimestamp;
271271

272272
for (const c of contributors) {
273-
totalCommits += c.commits;
273+
totalCommits += c.contributionCount;
274274
totalFiles += c.stats?.files ?? 0;
275275
totalAdditions += c.stats?.additions ?? 0;
276276
totalDeletions += c.stats?.deletions ?? 0;

src/env/node/git/sub-providers/contributors.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
2929
async getContributors(
3030
repoPath: string,
3131
rev?: string | undefined,
32-
options?: { all?: boolean; merges?: boolean | 'first-parent'; stats?: boolean },
32+
options?: {
33+
all?: boolean;
34+
merges?: boolean | 'first-parent';
35+
pathspec?: string;
36+
since?: string;
37+
stats?: boolean;
38+
},
3339
): Promise<GitContributor[]> {
3440
if (repoPath == null) return [];
3541

@@ -38,7 +44,13 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
3844
key += ':all';
3945
}
4046
if (options?.merges) {
41-
key += `:merges:${options.merges}`;
47+
key += `:merges=${options.merges}`;
48+
}
49+
if (options?.pathspec) {
50+
key += `:pathspec=${options.pathspec}`;
51+
}
52+
if (options?.since) {
53+
key += `:since=${options.since}`;
4254
}
4355
if (options?.stats) {
4456
key += ':stats';
@@ -67,12 +79,24 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
6779
args.push('--all', '--single-worktree');
6880
}
6981

82+
if (options?.since) {
83+
args.push(`--since="${options.since}"`);
84+
}
85+
86+
if (rev && !isUncommittedStaged(rev)) {
87+
args.push(rev);
88+
}
89+
90+
if (options?.pathspec) {
91+
args.push('--', options.pathspec);
92+
} else {
93+
args.push('--');
94+
}
95+
7096
const result = await this.git.exec(
7197
{ cwd: repoPath, configs: gitLogDefaultConfigs },
7298
'log',
7399
...args,
74-
rev && !isUncommittedStaged(rev) ? rev : undefined,
75-
'--',
76100
);
77101

78102
const contributors = new Map<string, GitContributor>();
@@ -89,25 +113,43 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
89113
c.email,
90114
isUserMatch(currentUser, c.author, c.email),
91115
1,
116+
[
117+
{
118+
sha: c.sha,
119+
date: new Date(timestamp),
120+
message: c.message,
121+
files: c.stats?.files,
122+
additions: c.stats?.additions,
123+
deletions: c.stats?.deletions,
124+
},
125+
],
92126
new Date(timestamp),
93127
new Date(timestamp),
94128
c.stats
95-
? {
96-
...c.stats,
97-
contributionScore: calculateContributionScore(c.stats, timestamp),
98-
}
129+
? { ...c.stats, contributionScore: calculateContributionScore(c.stats, timestamp) }
99130
: undefined,
100131
);
101132
contributors.set(key, contributor);
102133
} else {
103-
contributor.commits++;
134+
contributor.contributionCount++;
104135
const date = new Date(timestamp);
105136
if (date > contributor.latestCommitDate!) {
106137
contributor.latestCommitDate = date;
107138
}
108139
if (date < contributor.firstCommitDate!) {
109140
contributor.firstCommitDate = date;
110141
}
142+
143+
contributor.contributions ??= [];
144+
contributor.contributions.push({
145+
sha: c.sha,
146+
date: new Date(timestamp),
147+
message: c.message,
148+
files: c.stats?.files,
149+
additions: c.stats?.additions,
150+
deletions: c.stats?.deletions,
151+
});
152+
111153
if (options?.stats && c.stats != null) {
112154
if (contributor.stats == null) {
113155
contributor.stats = {

src/git/gitProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ export interface GitContributorsSubProvider {
355355
options?: {
356356
all?: boolean | undefined;
357357
merges?: boolean | 'first-parent';
358+
pathspec?: string;
359+
since?: string;
358360
stats?: boolean | undefined;
359361
},
360362
): Promise<GitContributor[]>;

src/git/models/contributor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export class GitContributor {
1616
public readonly name: string,
1717
public readonly email: string | undefined,
1818
public readonly current: boolean,
19-
public readonly commits: number,
19+
public readonly contributionCount: number,
20+
public readonly contributions?: GitContributorContribution[],
2021
public readonly latestCommitDate?: Date,
2122
public readonly firstCommitDate?: Date,
2223
public readonly stats?: GitContributorStats,
@@ -49,6 +50,12 @@ export class GitContributor {
4950
}
5051
}
5152

53+
interface GitContributorContribution extends Partial<GitCommitStats<number>> {
54+
readonly sha: string;
55+
readonly date: Date;
56+
readonly message: string;
57+
}
58+
5259
export interface GitContributorStats extends GitCommitStats<number> {
5360
readonly contributionScore: number;
5461
}

src/git/parsers/logParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function getCommitsLogParser(
4747
return _commitsParser;
4848
}
4949

50-
const contributorsMapping = { sha: '%H', author: '%aN', email: '%aE', date: '%at' };
50+
const contributorsMapping = { sha: '%H', author: '%aN', email: '%aE', date: '%at', message: '%B' };
5151

5252
type ContributorsLogParser = LogParser<typeof contributorsMapping>;
5353
let _contributorsParser: ContributorsLogParser | undefined;

src/git/utils/-webview/sorting.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export function sortContributors(
130130
return (
131131
pickedCompare ||
132132
(options.current ? (a.current ? -1 : 1) - (b.current ? -1 : 1) : 0) ||
133-
a.commits - b.commits ||
133+
a.contributionCount - b.contributionCount ||
134134
(a.latestCommitDate?.getTime() ?? 0) - (b.latestCommitDate?.getTime() ?? 0)
135135
);
136136
});
@@ -144,7 +144,7 @@ export function sortContributors(
144144
pickedCompare ||
145145
(options.current ? (a.current ? -1 : 1) - (b.current ? -1 : 1) : 0) ||
146146
(b.latestCommitDate?.getTime() ?? 0) - (a.latestCommitDate?.getTime() ?? 0) ||
147-
b.commits - a.commits
147+
b.contributionCount - a.contributionCount
148148
);
149149
});
150150
case 'date:asc':
@@ -157,7 +157,7 @@ export function sortContributors(
157157
pickedCompare ||
158158
(options.current ? (a.current ? -1 : 1) - (b.current ? -1 : 1) : 0) ||
159159
(a.latestCommitDate?.getTime() ?? 0) - (b.latestCommitDate?.getTime() ?? 0) ||
160-
b.commits - a.commits
160+
b.contributionCount - a.contributionCount
161161
);
162162
});
163163
case 'name:asc':
@@ -194,7 +194,7 @@ export function sortContributors(
194194
pickedCompare ||
195195
(options.current ? (a.current ? -1 : 1) - (b.current ? -1 : 1) : 0) ||
196196
(b.stats?.contributionScore ?? 0) - (a.stats?.contributionScore ?? 0) ||
197-
b.commits - a.commits ||
197+
b.contributionCount - a.contributionCount ||
198198
(b.latestCommitDate?.getTime() ?? 0) - (a.latestCommitDate?.getTime() ?? 0)
199199
);
200200
});
@@ -208,7 +208,7 @@ export function sortContributors(
208208
pickedCompare ||
209209
(options.current ? (a.current ? -1 : 1) - (b.current ? -1 : 1) : 0) ||
210210
(a.stats?.contributionScore ?? 0) - (b.stats?.contributionScore ?? 0) ||
211-
a.commits - b.commits ||
211+
a.contributionCount - b.contributionCount ||
212212
(a.latestCommitDate?.getTime() ?? 0) - (b.latestCommitDate?.getTime() ?? 0)
213213
);
214214
});
@@ -222,7 +222,7 @@ export function sortContributors(
222222
return (
223223
pickedCompare ||
224224
(options.current ? (a.current ? -1 : 1) - (b.current ? -1 : 1) : 0) ||
225-
b.commits - a.commits ||
225+
b.contributionCount - a.contributionCount ||
226226
(b.latestCommitDate?.getTime() ?? 0) - (a.latestCommitDate?.getTime() ?? 0)
227227
);
228228
});

src/git/utils/contributor.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const defaultContributorScoreOptions: ContributorScoreOptions = {
2424
};
2525

2626
export function calculateContributionScore(
27-
stats: GitCommitStats<number> | undefined,
27+
stats: GitCommitStats | undefined,
2828
timestamp: number,
2929
options: ContributorScoreOptions = defaultContributorScoreOptions,
3030
): number {

src/plus/integrations/providers/github/sub-providers/branches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
240240
let latestCommitTimestamp;
241241

242242
for (const c of contributors) {
243-
totalCommits += c.commits;
243+
totalCommits += c.contributionCount;
244244
totalFiles += c.stats?.files ?? 0;
245245
totalAdditions += c.stats?.additions ?? 0;
246246
totalDeletions += c.stats?.deletions ?? 0;

0 commit comments

Comments
 (0)