This repository was archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-hours.c
More file actions
221 lines (182 loc) · 6.13 KB
/
git-hours.c
File metadata and controls
221 lines (182 loc) · 6.13 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <ctype.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "helpers.h"
unsigned short MAX_DIFF_MINUTES = 120;
unsigned short FIRST_COMMIT_MINUTES = 120;
/* to get result of diff_file_cb() */
bool _is_file_changed_in_commit = false; // TODO: how to make not global?
/* https://libgit2.org/libgit2/#HEAD/group/callback/git_diff_file_cb
* can't return or point useful value, so I use global variable here
*/
int _diff_file_cb(const git_diff_delta *delta, float progress, void *file_name) {
/* check git-diff(1) for --diff-filter */
const char status = git_diff_status_char(delta -> status);
const bool is_changed = status == *"A" || status == *"M";
if (!is_changed) return 0;
const bool is_in_files = !strcmp(file_name, delta -> old_file.path) ||
!strcmp(file_name, delta -> new_file.path);
if (is_in_files) _is_file_changed_in_commit = true;
return 0;
}
const int check_that_file_changed_in_commit(
git_repository *repo, /* required for git_diff_tree_to_tree() */
const git_commit *commit,
char *file_name,
bool *out_is_file_changed_in_commit
) {
int return_code = 0;
/* to track changes from all parents */
unsigned parentcount = git_commit_parentcount(commit);
for (unsigned i = 0; i < parentcount; ++i) {
git_commit *parent_commit = NULL;
git_commit_parent(&parent_commit, commit, i);
/* commit trees to get diff */
git_tree *parent_tree = NULL;
git_tree *tree = NULL;
git_commit_tree(&parent_tree, parent_commit);
git_commit_tree(&tree, commit);
/* https://libgit2.org/libgit2/#HEAD/group/diff/git_diff_tree_to_tree */
git_diff *diff = NULL;
git_diff_tree_to_tree(&diff, repo, parent_tree, tree, NULL);
_is_file_changed_in_commit = false;
return_code = git_diff_foreach(diff, _diff_file_cb, NULL, NULL, NULL, file_name);
*out_is_file_changed_in_commit = _is_file_changed_in_commit;
/* free */
git_diff_free(diff);
git_tree_free(tree);
git_tree_free(parent_tree);
git_commit_free(parent_commit);
}
return return_code;
}
void get_hours(
unsigned long *hours,
unsigned long *commits,
const char *author_email,
char *file_name
) {
git_repository *repo = NULL;
git_revwalk *walker = NULL;
git_libgit2_init();
git_repository_open(&repo, cwd());
if (repo == NULL) {
perror("Error opening repository");
exit(EXIT_FAILURE);
}
/* set up history walker */
git_revwalk_new(&walker, repo);
/* commits from old to new */
git_revwalk_sorting(walker, GIT_SORT_REVERSE);
git_revwalk_push_head(walker);
git_oid oid;
float minutes_total = 0.0f;
unsigned long commits_total = 0;
unsigned prev_time = 0;
while(!git_revwalk_next(&oid, walker)) {
git_commit *commit = NULL;
git_commit_lookup(&commit, repo, &oid);
/* filter commits - only from one author */
const git_signature author = *git_commit_author(commit);
if (strcmp(author_email, author.email)) {
git_commit_free(commit);
continue;
}
/* filter commits - only with specified file */
if (file_name != NULL) { /* if file name provided */
bool is_file_changed_in_commit = false;
check_that_file_changed_in_commit(
repo, commit, file_name, &is_file_changed_in_commit);
if (!is_file_changed_in_commit) {
git_commit_free(commit);
continue;
}
}
/* now we can start to count commits and time */
commits_total++;
const unsigned time = author.when.time;
/* difference between commits in minutes */
const float diff = (time - prev_time) / 60.0f;
prev_time = time;
/* skip first commit difference */
if (commits_total == 1) {
git_commit_free(commit);
continue;
}
/* if difference <= our group limit - add it,
* otherwise this is first commit in group,
* and just add default value
*/
if (diff <= MAX_DIFF_MINUTES) minutes_total += diff;
else minutes_total += FIRST_COMMIT_MINUTES;
git_commit_free(commit);
}
/* to balance last commit minutes */
if (minutes_total >= FIRST_COMMIT_MINUTES)
minutes_total -= FIRST_COMMIT_MINUTES;
/* free */
git_revwalk_free(walker);
git_repository_free(repo);
git_libgit2_shutdown();
/* point results */
*hours = (unsigned long)(minutes_total / 60.0f);
*commits = commits_total;
}
/* https://gnu.org/savannah-checkouts/gnu/libc/manual/html_node/Example-of-Getopt.html */
void parse_opts(const int argc, char **argv, char **email, char **file_name) {
int c;
opterr = 0;
while ((c = getopt(argc, argv, "d:e:f:hv")) != -1) {
switch (c) {
case 'd':
MAX_DIFF_MINUTES = atoi(optarg);
if (!MAX_DIFF_MINUTES) {
fprintf(stderr, "Option -%c requires number > 0\n", c);
exit(EXIT_FAILURE);
} else break;
case 'e':
*email = optarg;
break;
case 'f':
FIRST_COMMIT_MINUTES = atoi(optarg);
if (!FIRST_COMMIT_MINUTES) {
fprintf(stderr, "Option -%c requires number > 0\n", c);
exit(EXIT_FAILURE);
} else break;
case 'h':
if (!(system("man git hours"))) exit(EXIT_SUCCESS);
else {
fprintf(stderr, "Command processor doesn't exists\n");
exit(EXIT_FAILURE);
}
case 'v':
printf("%s\n", VERSION);
exit(EXIT_SUCCESS);
case '?':
if (optopt == 'd' || optopt == 'e' || optopt == 'f')
fprintf(stderr, "Option -%c requires an argument\n", optopt);
else if (isprint(optopt))
fprintf (stderr, "Unknown option `-%c`\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x`\n", optopt);
exit(EXIT_FAILURE);
default:
abort();
}
}
/* get non-optional file_name arg */
*file_name = argv[optind];
}
int main(const int argc, char **argv) {
char *email_opt_val = NULL;
char *file_name = NULL;
parse_opts(argc, argv, &email_opt_val, &file_name);
const char *email = email_opt_val == NULL
? get_default_email() : email_opt_val;
unsigned long commits = 0, hours = 0;
get_hours(&hours, &commits, email, file_name);
printf("%s\t%li\t%li\n", email, hours, commits);
exit(EXIT_SUCCESS);
}