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
|
/*
* libgit2 "last-changed" example - get last commit modifying a file
*
* Written by the libgit2 contributors
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "common.h"
static void usage(void)
{
fprintf(stderr, "usage: last-changed [--git-dir=DIR] pathname ...\n");
exit(1);
}
static int mark_pathspec_match(
const git_diff *, const git_diff_delta *, const char *, void *);
typedef struct {
git_diff_options opts;
git_oid oid;
char str[GIT_OID_HEXSZ + 1];
} change_info;
int main(int argc, char *argv[])
{
const char *repodir = ".";
change_info info = { GIT_DIFF_OPTIONS_INIT };
int start_pathspec = 1;
size_t i;
git_repository *repo;
git_revwalk *walker;
git_threads_init();
/* allow you to specific a git repo other than the current one */
if (argc > 1 && !strncmp(argv[1], "--git-dir=", strlen("--git-dir="))) {
repodir = argv[1] + strlen("--git-dir=");
start_pathspec++;
}
/* convert arguments to a "pathspec" of interesting files */
info.opts.pathspec.strings = &argv[start_pathspec];
info.opts.pathspec.count = argc - start_pathspec;
if (!info.opts.pathspec.count)
usage();
info.opts.ignore_submodules = GIT_SUBMODULE_IGNORE_DIRTY |
GIT_DIFF_DISABLE_PATHSPEC_MATCH;
info.opts.notify_cb = mark_pathspec_match;
info.opts.notify_payload = &info;
/* create repo and walker */
check_lg2(git_repository_open_ext(&repo, repodir, 0, NULL),
"Could not open repository", repodir);
check_lg2(git_revwalk_new(&walker, repo),
"Could not create revision walker", NULL);
/* start at HEAD and walk backwards through time */
git_revwalk_sorting(walker, GIT_SORT_TOPOLOGICAL | GIT_SORT_TIME);
check_lg2(git_revwalk_push_head(walker),
"Could not find repository HEAD", NULL);
while (info.opts.pathspec.count > 0 &&
!git_revwalk_next(&info.oid, walker))
{
git_commit *commit;
git_diff *diff;
git_oid_tostr(info.str, sizeof(info.str), &info.oid);
check_lg2(git_commit_lookup(&commit, repo, &info.oid),
"Failed to look up commit", NULL);
check_lg2(git_diff_commit(&diff, commit, &info.opts),
"Failed to get diff for commit", NULL);
/* notification callback will take care of reporting on
* items in the diff and reducing the pathspec count
*/
git_diff_free(diff);
git_commit_free(commit);
}
for (i = 0; i < info.opts.pathspec.count; ++i) {
const char *path = info.opts.pathspec.strings[i];
if (path)
printf("never found %s\n", path);
}
git_revwalk_free(walker);
git_repository_free(repo);
git_threads_shutdown();
return 0;
}
static int mark_pathspec_match(
const git_diff *diff_so_far,
const git_diff_delta *delta_to_add,
const char *matched_pathspec,
void *payload)
{
change_info *info = payload;
git_strarray *paths = &info->opts.pathspec;
size_t i;
int found = 0;
(void)diff_so_far; (void)delta_to_add;
for (i = 0; i < paths->count; ++i) {
/* remove matched item from list */
if (found)
paths->strings[i - 1] = paths->strings[i];
else if (!strcmp(paths->strings[i], matched_pathspec))
found = 1;
}
if (found) {
const char *verb = "modified";
if (delta_to_add->status == GIT_DELTA_ADDED)
verb = "added";
else if (delta_to_add->status == GIT_DELTA_DELETED)
verb = "deleted";
printf("%s has %s %s\n", info->str, verb, matched_pathspec);
paths->count--;
}
return 0;
}
|